blob: 635df4cbefb7536e371ec9fb1a59e4fa02d318d0 [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;
1222
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001223 public HistoryStepDetails() {
1224 clear();
1225 }
1226
1227 public void clear() {
1228 userTime = systemTime = 0;
1229 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1230 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1231 = appCpuUTime3 = appCpuSTime3 = 0;
1232 }
1233
1234 public void writeToParcel(Parcel out) {
1235 out.writeInt(userTime);
1236 out.writeInt(systemTime);
1237 out.writeInt(appCpuUid1);
1238 out.writeInt(appCpuUTime1);
1239 out.writeInt(appCpuSTime1);
1240 out.writeInt(appCpuUid2);
1241 out.writeInt(appCpuUTime2);
1242 out.writeInt(appCpuSTime2);
1243 out.writeInt(appCpuUid3);
1244 out.writeInt(appCpuUTime3);
1245 out.writeInt(appCpuSTime3);
1246 out.writeInt(statUserTime);
1247 out.writeInt(statSystemTime);
1248 out.writeInt(statIOWaitTime);
1249 out.writeInt(statIrqTime);
1250 out.writeInt(statSoftIrqTime);
1251 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001252 out.writeString(statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001253 }
1254
1255 public void readFromParcel(Parcel in) {
1256 userTime = in.readInt();
1257 systemTime = in.readInt();
1258 appCpuUid1 = in.readInt();
1259 appCpuUTime1 = in.readInt();
1260 appCpuSTime1 = in.readInt();
1261 appCpuUid2 = in.readInt();
1262 appCpuUTime2 = in.readInt();
1263 appCpuSTime2 = in.readInt();
1264 appCpuUid3 = in.readInt();
1265 appCpuUTime3 = in.readInt();
1266 appCpuSTime3 = in.readInt();
1267 statUserTime = in.readInt();
1268 statSystemTime = in.readInt();
1269 statIOWaitTime = in.readInt();
1270 statIrqTime = in.readInt();
1271 statSoftIrqTime = in.readInt();
1272 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001273 statPlatformIdleState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001274 }
1275 }
1276
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001277 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001278 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001279
1280 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001281 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001282
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001283 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001284 public static final byte CMD_NULL = -1;
1285 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001286 public static final byte CMD_CURRENT_TIME = 5;
1287 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001288 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001289 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001290
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001291 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001292
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001293 /**
1294 * Return whether the command code is a delta data update.
1295 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001296 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001297 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001298 }
1299
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001300 public byte batteryLevel;
1301 public byte batteryStatus;
1302 public byte batteryHealth;
1303 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001304
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001305 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001306 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001307
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001308 // The charge of the battery in micro-Ampere-hours.
1309 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001310
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001311 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001312 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001313 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001314 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001315 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1316 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001317 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001318 public static final int STATE_PHONE_STATE_SHIFT = 6;
1319 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001320 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001321 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001322 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001323
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001324 // These states always appear directly in the first int token
1325 // of a delta change; they should be ones that change relatively
1326 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001327 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1328 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001329 public static final int STATE_GPS_ON_FLAG = 1<<29;
1330 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001331 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001332 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001333 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001334 // Do not use, this is used for coulomb delta count.
1335 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001336 // These are on the lower bits used for the command; if they change
1337 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001338 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001339 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001340 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001341 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1342 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1343 // empty slot
1344 // empty slot
1345 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001346
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001347 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001348 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1349
1350 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001351
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001352 public int states;
1353
Dianne Hackborn3251b902014-06-20 14:40:53 -07001354 // Constants from WIFI_SUPPL_STATE_*
1355 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1356 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1357 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1358 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1359 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1360 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1361
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001362 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001363 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1364 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1365 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001366 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001367 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1368 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1369 public static final int STATE2_CHARGING_FLAG = 1<<24;
1370 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1371 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1372 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001373 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001374
1375 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001376 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001377 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1378
1379 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001380
Dianne Hackborn40c87252014-03-19 16:55:40 -07001381 public int states2;
1382
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001383 // The wake lock that was acquired at this point.
1384 public HistoryTag wakelockTag;
1385
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001386 // Kernel wakeup reason at this point.
1387 public HistoryTag wakeReasonTag;
1388
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001389 // Non-null when there is more detailed information at this step.
1390 public HistoryStepDetails stepDetails;
1391
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001392 public static final int EVENT_FLAG_START = 0x8000;
1393 public static final int EVENT_FLAG_FINISH = 0x4000;
1394
1395 // No event in this item.
1396 public static final int EVENT_NONE = 0x0000;
1397 // Event is about a process that is running.
1398 public static final int EVENT_PROC = 0x0001;
1399 // Event is about an application package that is in the foreground.
1400 public static final int EVENT_FOREGROUND = 0x0002;
1401 // Event is about an application package that is at the top of the screen.
1402 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001403 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001404 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001405 // Events for all additional wake locks aquired/release within a wake block.
1406 // These are not generated by default.
1407 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001408 // Event is about an application executing a scheduled job.
1409 public static final int EVENT_JOB = 0x0006;
1410 // Events for users running.
1411 public static final int EVENT_USER_RUNNING = 0x0007;
1412 // Events for foreground user.
1413 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001414 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001415 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001416 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001417 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001418 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001419 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001420 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001421 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001422 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001423 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001424 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001425 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001426 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001427 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001428 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001429 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001430 // Event for a package being on the temporary whitelist.
1431 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001432 // Event for the screen waking up.
1433 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001434 // Event for the UID that woke up the application processor.
1435 // Used for wakeups coming from WiFi, modem, etc.
1436 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001437 // Event for reporting that a specific partial wake lock has been held for a long duration.
1438 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001439
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001440 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001441 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001442 // Mask to extract out only the type part of the event.
1443 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001444
1445 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1446 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1447 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1448 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1449 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1450 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001451 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1452 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001453 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1454 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001455 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1456 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1457 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1458 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1459 public static final int EVENT_USER_FOREGROUND_START =
1460 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1461 public static final int EVENT_USER_FOREGROUND_FINISH =
1462 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001463 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1464 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001465 public static final int EVENT_TEMP_WHITELIST_START =
1466 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1467 public static final int EVENT_TEMP_WHITELIST_FINISH =
1468 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001469 public static final int EVENT_LONG_WAKE_LOCK_START =
1470 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1471 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1472 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001473
1474 // For CMD_EVENT.
1475 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001476 public HistoryTag eventTag;
1477
Dianne Hackborn9a755432014-05-15 17:05:22 -07001478 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001479 public long currentTime;
1480
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001481 // Meta-data when reading.
1482 public int numReadInts;
1483
1484 // Pre-allocated objects.
1485 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001486 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001487 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001488
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001489 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001490 }
Bookatzc8c44962017-05-11 12:12:54 -07001491
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001492 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001493 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001494 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001495 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001496 }
Bookatzc8c44962017-05-11 12:12:54 -07001497
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001498 public int describeContents() {
1499 return 0;
1500 }
1501
1502 public void writeToParcel(Parcel dest, int flags) {
1503 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001504 int bat = (((int)cmd)&0xff)
1505 | ((((int)batteryLevel)<<8)&0xff00)
1506 | ((((int)batteryStatus)<<16)&0xf0000)
1507 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001508 | ((((int)batteryPlugType)<<24)&0xf000000)
1509 | (wakelockTag != null ? 0x10000000 : 0)
1510 | (wakeReasonTag != null ? 0x20000000 : 0)
1511 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001512 dest.writeInt(bat);
1513 bat = (((int)batteryTemperature)&0xffff)
1514 | ((((int)batteryVoltage)<<16)&0xffff0000);
1515 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001516 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001517 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001518 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001519 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001520 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001521 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001522 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001523 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001524 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001525 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001526 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001527 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001528 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001529 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001530 dest.writeLong(currentTime);
1531 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001532 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001533
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001534 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001535 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001536 int bat = src.readInt();
1537 cmd = (byte)(bat&0xff);
1538 batteryLevel = (byte)((bat>>8)&0xff);
1539 batteryStatus = (byte)((bat>>16)&0xf);
1540 batteryHealth = (byte)((bat>>20)&0xf);
1541 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001542 int bat2 = src.readInt();
1543 batteryTemperature = (short)(bat2&0xffff);
1544 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001545 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001546 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001547 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001548 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001549 wakelockTag = localWakelockTag;
1550 wakelockTag.readFromParcel(src);
1551 } else {
1552 wakelockTag = null;
1553 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001554 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001555 wakeReasonTag = localWakeReasonTag;
1556 wakeReasonTag.readFromParcel(src);
1557 } else {
1558 wakeReasonTag = null;
1559 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001560 if ((bat&0x40000000) != 0) {
1561 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001562 eventTag = localEventTag;
1563 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001564 } else {
1565 eventCode = EVENT_NONE;
1566 eventTag = null;
1567 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001568 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001569 currentTime = src.readLong();
1570 } else {
1571 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001572 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001573 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001574 }
1575
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001576 public void clear() {
1577 time = 0;
1578 cmd = CMD_NULL;
1579 batteryLevel = 0;
1580 batteryStatus = 0;
1581 batteryHealth = 0;
1582 batteryPlugType = 0;
1583 batteryTemperature = 0;
1584 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001585 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001586 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001587 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001588 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001589 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001590 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001591 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001592 }
Bookatzc8c44962017-05-11 12:12:54 -07001593
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001594 public void setTo(HistoryItem o) {
1595 time = o.time;
1596 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001597 setToCommon(o);
1598 }
1599
1600 public void setTo(long time, byte cmd, HistoryItem o) {
1601 this.time = time;
1602 this.cmd = cmd;
1603 setToCommon(o);
1604 }
1605
1606 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001607 batteryLevel = o.batteryLevel;
1608 batteryStatus = o.batteryStatus;
1609 batteryHealth = o.batteryHealth;
1610 batteryPlugType = o.batteryPlugType;
1611 batteryTemperature = o.batteryTemperature;
1612 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001613 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001614 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001615 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001616 if (o.wakelockTag != null) {
1617 wakelockTag = localWakelockTag;
1618 wakelockTag.setTo(o.wakelockTag);
1619 } else {
1620 wakelockTag = null;
1621 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001622 if (o.wakeReasonTag != null) {
1623 wakeReasonTag = localWakeReasonTag;
1624 wakeReasonTag.setTo(o.wakeReasonTag);
1625 } else {
1626 wakeReasonTag = null;
1627 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001628 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001629 if (o.eventTag != null) {
1630 eventTag = localEventTag;
1631 eventTag.setTo(o.eventTag);
1632 } else {
1633 eventTag = null;
1634 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001635 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001636 }
1637
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001638 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001639 return batteryLevel == o.batteryLevel
1640 && batteryStatus == o.batteryStatus
1641 && batteryHealth == o.batteryHealth
1642 && batteryPlugType == o.batteryPlugType
1643 && batteryTemperature == o.batteryTemperature
1644 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001645 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001646 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001647 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001648 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001649 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001650
1651 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001652 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001653 return false;
1654 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001655 if (wakelockTag != o.wakelockTag) {
1656 if (wakelockTag == null || o.wakelockTag == null) {
1657 return false;
1658 }
1659 if (!wakelockTag.equals(o.wakelockTag)) {
1660 return false;
1661 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001662 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001663 if (wakeReasonTag != o.wakeReasonTag) {
1664 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1665 return false;
1666 }
1667 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1668 return false;
1669 }
1670 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001671 if (eventTag != o.eventTag) {
1672 if (eventTag == null || o.eventTag == null) {
1673 return false;
1674 }
1675 if (!eventTag.equals(o.eventTag)) {
1676 return false;
1677 }
1678 }
1679 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001680 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001681 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001682
1683 public final static class HistoryEventTracker {
1684 private final HashMap<String, SparseIntArray>[] mActiveEvents
1685 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1686
1687 public boolean updateState(int code, String name, int uid, int poolIdx) {
1688 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1689 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1690 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1691 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001692 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001693 mActiveEvents[idx] = active;
1694 }
1695 SparseIntArray uids = active.get(name);
1696 if (uids == null) {
1697 uids = new SparseIntArray();
1698 active.put(name, uids);
1699 }
1700 if (uids.indexOfKey(uid) >= 0) {
1701 // Already set, nothing to do!
1702 return false;
1703 }
1704 uids.put(uid, poolIdx);
1705 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1706 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1707 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1708 if (active == null) {
1709 // not currently active, nothing to do.
1710 return false;
1711 }
1712 SparseIntArray uids = active.get(name);
1713 if (uids == null) {
1714 // not currently active, nothing to do.
1715 return false;
1716 }
1717 idx = uids.indexOfKey(uid);
1718 if (idx < 0) {
1719 // not currently active, nothing to do.
1720 return false;
1721 }
1722 uids.removeAt(idx);
1723 if (uids.size() <= 0) {
1724 active.remove(name);
1725 }
1726 }
1727 return true;
1728 }
1729
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001730 public void removeEvents(int code) {
1731 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1732 mActiveEvents[idx] = null;
1733 }
1734
Dianne Hackborn37de0982014-05-09 09:32:18 -07001735 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1736 return mActiveEvents[code];
1737 }
1738 }
1739
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001740 public static final class BitDescription {
1741 public final int mask;
1742 public final int shift;
1743 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001744 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001745 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001746 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07001747
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001748 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001749 this.mask = mask;
1750 this.shift = -1;
1751 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001752 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001753 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001754 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001755 }
Bookatzc8c44962017-05-11 12:12:54 -07001756
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001757 public BitDescription(int mask, int shift, String name, String shortName,
1758 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001759 this.mask = mask;
1760 this.shift = shift;
1761 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001762 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001763 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001764 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001765 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001766 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001767
Dianne Hackbornfc064132014-06-02 12:42:12 -07001768 /**
1769 * Don't allow any more batching in to the current history event. This
1770 * is called when printing partial histories, so to ensure that the next
1771 * history event will go in to a new batch after what was printed in the
1772 * last partial history.
1773 */
1774 public abstract void commitCurrentHistoryBatchLocked();
1775
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001776 public abstract int getHistoryTotalSize();
1777
1778 public abstract int getHistoryUsedSize();
1779
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001780 public abstract boolean startIteratingHistoryLocked();
1781
Dianne Hackborn099bc622014-01-22 13:39:16 -08001782 public abstract int getHistoryStringPoolSize();
1783
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001784 public abstract int getHistoryStringPoolBytes();
1785
1786 public abstract String getHistoryTagPoolString(int index);
1787
1788 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001789
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001790 public abstract boolean getNextHistoryLocked(HistoryItem out);
1791
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001792 public abstract void finishIteratingHistoryLocked();
1793
1794 public abstract boolean startIteratingOldHistoryLocked();
1795
1796 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1797
1798 public abstract void finishIteratingOldHistoryLocked();
1799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001801 * Return the base time offset for the battery history.
1802 */
1803 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07001804
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001805 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 * Returns the number of times the device has been started.
1807 */
1808 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07001809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001811 * 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 -08001812 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07001813 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 * {@hide}
1815 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001816 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07001817
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001818 /**
1819 * Returns the number of times the screen was turned on.
1820 *
1821 * {@hide}
1822 */
1823 public abstract int getScreenOnCount(int which);
1824
Jeff Browne95c3cd2014-05-02 16:59:26 -07001825 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1826
Dianne Hackborn617f8772009-03-31 15:04:46 -07001827 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1828 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1829 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1830 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1831 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07001832
Dianne Hackborn617f8772009-03-31 15:04:46 -07001833 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1834 "dark", "dim", "medium", "light", "bright"
1835 };
Bookatzc8c44962017-05-11 12:12:54 -07001836
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001837 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1838 "0", "1", "2", "3", "4"
1839 };
1840
Dianne Hackborn617f8772009-03-31 15:04:46 -07001841 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001842
Dianne Hackborn617f8772009-03-31 15:04:46 -07001843 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001844 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001845 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07001846 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07001847 * {@hide}
1848 */
1849 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001850 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001853 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001854 * running on battery.
1855 *
1856 * {@hide}
1857 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001858 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001859
1860 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001861 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001862 *
1863 * {@hide}
1864 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001865 public abstract int getPowerSaveModeEnabledCount(int which);
1866
1867 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001868 * Constant for device idle mode: not active.
1869 */
1870 public static final int DEVICE_IDLE_MODE_OFF = 0;
1871
1872 /**
1873 * Constant for device idle mode: active in lightweight mode.
1874 */
1875 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1876
1877 /**
1878 * Constant for device idle mode: active in full mode.
1879 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07001880 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001881
1882 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001883 * Returns the time in microseconds that device has been in idle mode while
1884 * running on battery.
1885 *
1886 * {@hide}
1887 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001888 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001889
1890 /**
1891 * Returns the number of times that the devie has gone in to idle mode.
1892 *
1893 * {@hide}
1894 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001895 public abstract int getDeviceIdleModeCount(int mode, int which);
1896
1897 /**
1898 * Return the longest duration we spent in a particular device idle mode (fully in the
1899 * mode, not in idle maintenance etc).
1900 */
1901 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001902
1903 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001904 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001905 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001906 * counts all of the time that we consider the device to be idle, whether or not
1907 * it is currently in the actual device idle mode.
1908 *
1909 * {@hide}
1910 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001911 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001912
1913 /**
1914 * Returns the number of times that the devie has started idling.
1915 *
1916 * {@hide}
1917 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001918 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001919
1920 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001921 * Returns the number of times that connectivity state changed.
1922 *
1923 * {@hide}
1924 */
1925 public abstract int getNumConnectivityChange(int which);
1926
1927 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001928 * 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 -08001929 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07001930 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 * {@hide}
1932 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001933 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07001934
Dianne Hackborn627bba72009-03-24 22:32:56 -07001935 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001936 * Returns the number of times a phone call was activated.
1937 *
1938 * {@hide}
1939 */
1940 public abstract int getPhoneOnCount(int which);
1941
1942 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001943 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001944 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07001945 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07001946 * {@hide}
1947 */
1948 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001949 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001950
Dianne Hackborn617f8772009-03-31 15:04:46 -07001951 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001952 * Returns the time in microseconds that the phone has been trying to
1953 * acquire a signal.
1954 *
1955 * {@hide}
1956 */
1957 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001958 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001959
1960 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001961 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07001962 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07001963 * {@hide}
1964 */
1965 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1966
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001967 /**
1968 * Returns the time in microseconds that the mobile network has been active
1969 * (in a high power state).
1970 *
1971 * {@hide}
1972 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001973 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001974
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001975 /**
1976 * Returns the number of times that the mobile network has transitioned to the
1977 * active state.
1978 *
1979 * {@hide}
1980 */
1981 public abstract int getMobileRadioActiveCount(int which);
1982
1983 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001984 * Returns the time in microseconds that is the difference between the mobile radio
1985 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1986 * from the radio.
1987 *
1988 * {@hide}
1989 */
1990 public abstract long getMobileRadioActiveAdjustedTime(int which);
1991
1992 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001993 * Returns the time in microseconds that the mobile network has been active
1994 * (in a high power state) but not being able to blame on an app.
1995 *
1996 * {@hide}
1997 */
1998 public abstract long getMobileRadioActiveUnknownTime(int which);
1999
2000 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002001 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002002 *
2003 * {@hide}
2004 */
2005 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002006
Dianne Hackborn627bba72009-03-24 22:32:56 -07002007 public static final int DATA_CONNECTION_NONE = 0;
2008 public static final int DATA_CONNECTION_GPRS = 1;
2009 public static final int DATA_CONNECTION_EDGE = 2;
2010 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002011 public static final int DATA_CONNECTION_CDMA = 4;
2012 public static final int DATA_CONNECTION_EVDO_0 = 5;
2013 public static final int DATA_CONNECTION_EVDO_A = 6;
2014 public static final int DATA_CONNECTION_1xRTT = 7;
2015 public static final int DATA_CONNECTION_HSDPA = 8;
2016 public static final int DATA_CONNECTION_HSUPA = 9;
2017 public static final int DATA_CONNECTION_HSPA = 10;
2018 public static final int DATA_CONNECTION_IDEN = 11;
2019 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002020 public static final int DATA_CONNECTION_LTE = 13;
2021 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08002022 public static final int DATA_CONNECTION_HSPAP = 15;
2023 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002024
Dianne Hackborn627bba72009-03-24 22:32:56 -07002025 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002026 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002027 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08002028 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002029 };
Bookatzc8c44962017-05-11 12:12:54 -07002030
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002031 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002032
Dianne Hackborn627bba72009-03-24 22:32:56 -07002033 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002034 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002035 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002036 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002037 * {@hide}
2038 */
2039 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002040 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002043 * Returns the number of times the phone has entered the given data
2044 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002045 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002046 * {@hide}
2047 */
2048 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002049
Dianne Hackborn3251b902014-06-20 14:40:53 -07002050 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2051 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2052 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2053 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2054 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2055 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2056 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2057 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2058 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2059 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2060 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2061 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2062 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2063
2064 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2065
2066 static final String[] WIFI_SUPPL_STATE_NAMES = {
2067 "invalid", "disconn", "disabled", "inactive", "scanning",
2068 "authenticating", "associating", "associated", "4-way-handshake",
2069 "group-handshake", "completed", "dormant", "uninit"
2070 };
2071
2072 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2073 "inv", "dsc", "dis", "inact", "scan",
2074 "auth", "ascing", "asced", "4-way",
2075 "group", "compl", "dorm", "uninit"
2076 };
2077
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002078 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
2079 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002080 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002081 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2082 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002083 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002084 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2085 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2086 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002087 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002088 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002089 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002090 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002091 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2092 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002093 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2094 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2095 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2096 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2097 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2098 new String[] {"in", "out", "emergency", "off"},
2099 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002100 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2101 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2102 SignalStrength.SIGNAL_STRENGTH_NAMES,
2103 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002104 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2105 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2106 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002107 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002108
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002109 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2110 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002111 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002112 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002113 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002114 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002115 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002116 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2117 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2118 new String[] { "off", "light", "full", "???" },
2119 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002120 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2121 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2122 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002123 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2124 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2125 new String[] { "0", "1", "2", "3", "4" },
2126 new String[] { "0", "1", "2", "3", "4" }),
2127 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2128 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2129 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002130 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002131 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002132 };
2133
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002134 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002135 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002136 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2137 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002138 };
2139
2140 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002141 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002142 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002143 "Esw", "Ewa", "Elw", "Eec"
2144 };
2145
2146 @FunctionalInterface
2147 public interface IntToString {
2148 String applyAsString(int val);
2149 }
2150
2151 private static final IntToString sUidToString = UserHandle::formatUid;
2152 private static final IntToString sIntToString = Integer::toString;
2153
2154 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2155 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2156 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2157 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2158 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002159 };
2160
Dianne Hackborn617f8772009-03-31 15:04:46 -07002161 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002162 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002163 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002164 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002165 * {@hide}
2166 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002167 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002168
2169 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002170 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002171 * been in the running state while the device was running on battery.
2172 *
2173 * {@hide}
2174 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002175 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002176
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002177 public static final int WIFI_STATE_OFF = 0;
2178 public static final int WIFI_STATE_OFF_SCANNING = 1;
2179 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2180 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2181 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2182 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2183 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2184 public static final int WIFI_STATE_SOFT_AP = 7;
2185
2186 static final String[] WIFI_STATE_NAMES = {
2187 "off", "scanning", "no_net", "disconn",
2188 "sta", "p2p", "sta_p2p", "soft_ap"
2189 };
2190
2191 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2192
2193 /**
2194 * Returns the time in microseconds that WiFi has been running in the given state.
2195 *
2196 * {@hide}
2197 */
2198 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002199 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002200
2201 /**
2202 * Returns the number of times that WiFi has entered the given state.
2203 *
2204 * {@hide}
2205 */
2206 public abstract int getWifiStateCount(int wifiState, int which);
2207
The Android Open Source Project10592532009-03-18 17:39:46 -07002208 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002209 * Returns the time in microseconds that the wifi supplicant has been
2210 * in a given state.
2211 *
2212 * {@hide}
2213 */
2214 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2215
2216 /**
2217 * Returns the number of times that the wifi supplicant has transitioned
2218 * to a given state.
2219 *
2220 * {@hide}
2221 */
2222 public abstract int getWifiSupplStateCount(int state, int which);
2223
2224 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2225
2226 /**
2227 * Returns the time in microseconds that WIFI has been running with
2228 * the given signal strength.
2229 *
2230 * {@hide}
2231 */
2232 public abstract long getWifiSignalStrengthTime(int strengthBin,
2233 long elapsedRealtimeUs, int which);
2234
2235 /**
2236 * Returns the number of times WIFI has entered the given signal strength.
2237 *
2238 * {@hide}
2239 */
2240 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2241
2242 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002243 * Returns the time in microseconds that the flashlight has been on while the device was
2244 * running on battery.
2245 *
2246 * {@hide}
2247 */
2248 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2249
2250 /**
2251 * Returns the number of times that the flashlight has been turned on while the device was
2252 * running on battery.
2253 *
2254 * {@hide}
2255 */
2256 public abstract long getFlashlightOnCount(int which);
2257
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002258 /**
2259 * Returns the time in microseconds that the camera has been on while the device was
2260 * running on battery.
2261 *
2262 * {@hide}
2263 */
2264 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2265
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002266 /**
2267 * Returns the time in microseconds that bluetooth scans were running while the device was
2268 * on battery.
2269 *
2270 * {@hide}
2271 */
2272 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002273
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002274 public static final int NETWORK_MOBILE_RX_DATA = 0;
2275 public static final int NETWORK_MOBILE_TX_DATA = 1;
2276 public static final int NETWORK_WIFI_RX_DATA = 2;
2277 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002278 public static final int NETWORK_BT_RX_DATA = 4;
2279 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002280 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2281 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2282 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2283 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2284 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002285
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002286 public abstract long getNetworkActivityBytes(int type, int which);
2287 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002288
Adam Lesinskie08af192015-03-25 16:42:59 -07002289 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002290 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002291 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002292 * actual power data.
2293 */
2294 public abstract boolean hasWifiActivityReporting();
2295
2296 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002297 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2298 * in various radio controller states, such as transmit, receive, and idle.
2299 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002300 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002301 public abstract ControllerActivityCounter getWifiControllerActivity();
2302
2303 /**
2304 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2305 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2306 * actual power data.
2307 */
2308 public abstract boolean hasBluetoothActivityReporting();
2309
2310 /**
2311 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2312 * in various radio controller states, such as transmit, receive, and idle.
2313 * @return non-null {@link ControllerActivityCounter}
2314 */
2315 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2316
2317 /**
2318 * Returns true if the BatteryStats object has detailed modem power reports.
2319 * When true, calling {@link #getModemControllerActivity()} will yield the
2320 * actual power data.
2321 */
2322 public abstract boolean hasModemActivityReporting();
2323
2324 /**
2325 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2326 * in various radio controller states, such as transmit, receive, and idle.
2327 * @return non-null {@link ControllerActivityCounter}
2328 */
2329 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002330
The Android Open Source Project10592532009-03-18 17:39:46 -07002331 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002332 * Return the wall clock time when battery stats data collection started.
2333 */
2334 public abstract long getStartClockTime();
2335
2336 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002337 * Return platform version tag that we were running in when the battery stats started.
2338 */
2339 public abstract String getStartPlatformVersion();
2340
2341 /**
2342 * Return platform version tag that we were running in when the battery stats ended.
2343 */
2344 public abstract String getEndPlatformVersion();
2345
2346 /**
2347 * Return the internal version code of the parcelled format.
2348 */
2349 public abstract int getParcelVersion();
2350
2351 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 * Return whether we are currently running on battery.
2353 */
2354 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 /**
2357 * Returns a SparseArray containing the statistics for each uid.
2358 */
2359 public abstract SparseArray<? extends Uid> getUidStats();
2360
2361 /**
2362 * Returns the current battery uptime in microseconds.
2363 *
2364 * @param curTime the amount of elapsed realtime in microseconds.
2365 */
2366 public abstract long getBatteryUptime(long curTime);
2367
2368 /**
2369 * Returns the current battery realtime in microseconds.
2370 *
2371 * @param curTime the amount of elapsed realtime in microseconds.
2372 */
2373 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002374
The Android Open Source Project10592532009-03-18 17:39:46 -07002375 /**
Evan Millar633a1742009-04-02 16:36:33 -07002376 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002377 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002378 */
Evan Millar633a1742009-04-02 16:36:33 -07002379 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002380
The Android Open Source Project10592532009-03-18 17:39:46 -07002381 /**
Evan Millar633a1742009-04-02 16:36:33 -07002382 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2383 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002384 */
Evan Millar633a1742009-04-02 16:36:33 -07002385 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386
2387 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002388 * Get the amount the battery has discharged since the stats were
2389 * last reset after charging, as a lower-end approximation.
2390 */
2391 public abstract int getLowDischargeAmountSinceCharge();
2392
2393 /**
2394 * Get the amount the battery has discharged since the stats were
2395 * last reset after charging, as an upper-end approximation.
2396 */
2397 public abstract int getHighDischargeAmountSinceCharge();
2398
2399 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002400 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2401 */
2402 public abstract int getDischargeAmount(int which);
2403
2404 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002405 * Get the amount the battery has discharged while the screen was on,
2406 * since the last time power was unplugged.
2407 */
2408 public abstract int getDischargeAmountScreenOn();
2409
2410 /**
2411 * Get the amount the battery has discharged while the screen was on,
2412 * since the last time the device was charged.
2413 */
2414 public abstract int getDischargeAmountScreenOnSinceCharge();
2415
2416 /**
2417 * Get the amount the battery has discharged while the screen was off,
2418 * since the last time power was unplugged.
2419 */
2420 public abstract int getDischargeAmountScreenOff();
2421
2422 /**
2423 * Get the amount the battery has discharged while the screen was off,
2424 * since the last time the device was charged.
2425 */
2426 public abstract int getDischargeAmountScreenOffSinceCharge();
2427
2428 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 * Returns the total, last, or current battery uptime in microseconds.
2430 *
2431 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002432 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 */
2434 public abstract long computeBatteryUptime(long curTime, int which);
2435
2436 /**
2437 * Returns the total, last, or current battery realtime in microseconds.
2438 *
2439 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002440 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 */
2442 public abstract long computeBatteryRealtime(long curTime, int which);
2443
2444 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002445 * Returns the total, last, or current battery screen off uptime in microseconds.
2446 *
2447 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002448 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002449 */
2450 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2451
2452 /**
2453 * Returns the total, last, or current battery screen off realtime in microseconds.
2454 *
2455 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002456 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002457 */
2458 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2459
2460 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 * Returns the total, last, or current uptime in microseconds.
2462 *
2463 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002464 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465 */
2466 public abstract long computeUptime(long curTime, int which);
2467
2468 /**
2469 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002470 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002472 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 */
2474 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002475
2476 /**
2477 * Compute an approximation for how much run time (in microseconds) is remaining on
2478 * the battery. Returns -1 if no time can be computed: either there is not
2479 * enough current data to make a decision, or the battery is currently
2480 * charging.
2481 *
2482 * @param curTime The current elepsed realtime in microseconds.
2483 */
2484 public abstract long computeBatteryTimeRemaining(long curTime);
2485
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002486 // The part of a step duration that is the actual time.
2487 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2488
2489 // Bits in a step duration that are the new battery level we are at.
2490 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002491 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002492
2493 // Bits in a step duration that are the initial mode we were in at that step.
2494 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002495 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002496
2497 // Bits in a step duration that indicate which modes changed during that step.
2498 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002499 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002500
2501 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2502 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2503
Santos Cordone94f0502017-02-24 12:31:20 -08002504 // The largest value for screen state that is tracked in battery states. Any values above
2505 // this should be mapped back to one of the tracked values before being tracked here.
2506 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2507
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002508 // Step duration mode: power save is on.
2509 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2510
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002511 // Step duration mode: device is currently in idle mode.
2512 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2513
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002514 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2515 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002516 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2517 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002518 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2519 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2520 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2521 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2522 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002523 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2524 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002525 };
2526 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2527 (Display.STATE_OFF-1),
2528 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002529 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002530 (Display.STATE_ON-1),
2531 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2532 (Display.STATE_DOZE-1),
2533 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2534 (Display.STATE_DOZE_SUSPEND-1),
2535 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002536 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002537 };
2538 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2539 "screen off",
2540 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002541 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002542 "screen on",
2543 "screen on power save",
2544 "screen doze",
2545 "screen doze power save",
2546 "screen doze-suspend",
2547 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002548 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002549 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002550
2551 /**
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002552 * Return the counter keeping track of the amount of battery discharge while the screen was off,
2553 * measured in micro-Ampere-hours. This will be non-zero only if the device's battery has
2554 * a coulomb counter.
2555 */
2556 public abstract LongCounter getDischargeScreenOffCoulombCounter();
2557
2558 /**
2559 * Return the counter keeping track of the amount of battery discharge measured in
2560 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2561 * a coulomb counter.
2562 */
2563 public abstract LongCounter getDischargeCoulombCounter();
2564
2565 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002566 * Returns the estimated real battery capacity, which may be less than the capacity
2567 * declared by the PowerProfile.
2568 * @return The estimated battery capacity in mAh.
2569 */
2570 public abstract int getEstimatedBatteryCapacity();
2571
2572 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002573 * @return The minimum learned battery capacity in uAh.
2574 */
2575 public abstract int getMinLearnedBatteryCapacity();
2576
2577 /**
2578 * @return The maximum learned battery capacity in uAh.
2579 */
2580 public abstract int getMaxLearnedBatteryCapacity() ;
2581
2582 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002583 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002584 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002585 public abstract LevelStepTracker getDischargeLevelStepTracker();
2586
2587 /**
2588 * Return the array of daily discharge step durations.
2589 */
2590 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002591
2592 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002593 * Compute an approximation for how much time (in microseconds) remains until the battery
2594 * is fully charged. Returns -1 if no time can be computed: either there is not
2595 * enough current data to make a decision, or the battery is currently
2596 * discharging.
2597 *
2598 * @param curTime The current elepsed realtime in microseconds.
2599 */
2600 public abstract long computeChargeTimeRemaining(long curTime);
2601
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002602 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002603 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002604 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002605 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002606
2607 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002608 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002609 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002610 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002611
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002612 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2613
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002614 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002615
Evan Millarc64edde2009-04-18 12:26:32 -07002616 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617
James Carr2dd7e5e2016-07-20 18:48:39 -07002618 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2619
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002620 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2621
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002622 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 long days = seconds / (60 * 60 * 24);
2624 if (days != 0) {
2625 out.append(days);
2626 out.append("d ");
2627 }
2628 long used = days * 60 * 60 * 24;
2629
2630 long hours = (seconds - used) / (60 * 60);
2631 if (hours != 0 || used != 0) {
2632 out.append(hours);
2633 out.append("h ");
2634 }
2635 used += hours * 60 * 60;
2636
2637 long mins = (seconds-used) / 60;
2638 if (mins != 0 || used != 0) {
2639 out.append(mins);
2640 out.append("m ");
2641 }
2642 used += mins * 60;
2643
2644 if (seconds != 0 || used != 0) {
2645 out.append(seconds-used);
2646 out.append("s ");
2647 }
2648 }
2649
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002650 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002652 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 sb.append(time - (sec * 1000));
2654 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 }
2656
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002657 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002658 long sec = time / 1000;
2659 formatTimeRaw(sb, sec);
2660 sb.append(time - (sec * 1000));
2661 sb.append("ms");
2662 }
2663
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002664 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002666 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 }
2668 float perc = ((float)num) / ((float)den) * 100;
2669 mFormatBuilder.setLength(0);
2670 mFormatter.format("%.1f%%", perc);
2671 return mFormatBuilder.toString();
2672 }
2673
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002674 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002675 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07002676
Evan Millar22ac0432009-03-31 11:33:18 -07002677 if (bytes < BYTES_PER_KB) {
2678 return bytes + "B";
2679 } else if (bytes < BYTES_PER_MB) {
2680 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2681 return mFormatBuilder.toString();
2682 } else if (bytes < BYTES_PER_GB){
2683 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2684 return mFormatBuilder.toString();
2685 } else {
2686 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2687 return mFormatBuilder.toString();
2688 }
2689 }
2690
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002691 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002692 if (timer != null) {
2693 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002694 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002695 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2696 return totalTimeMillis;
2697 }
2698 return 0;
2699 }
2700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 /**
2702 *
2703 * @param sb a StringBuilder object.
2704 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002705 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002707 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 * @param linePrefix a String to be prepended to each line of output.
2709 * @return the line prefix
2710 */
2711 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002712 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002715 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002716
Evan Millarc64edde2009-04-18 12:26:32 -07002717 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 if (totalTimeMillis != 0) {
2719 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002720 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002721 if (name != null) {
2722 sb.append(name);
2723 sb.append(' ');
2724 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 sb.append('(');
2726 sb.append(count);
2727 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002728 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2729 if (maxDurationMs >= 0) {
2730 sb.append(" max=");
2731 sb.append(maxDurationMs);
2732 }
Bookatz506a8182017-05-01 14:18:42 -07002733 // Put actual time if it is available and different from totalTimeMillis.
2734 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
2735 if (totalDurMs > totalTimeMillis) {
2736 sb.append(" actual=");
2737 sb.append(totalDurMs);
2738 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07002739 if (timer.isRunningLocked()) {
2740 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2741 if (currentMs >= 0) {
2742 sb.append(" (running for ");
2743 sb.append(currentMs);
2744 sb.append("ms)");
2745 } else {
2746 sb.append(" (running)");
2747 }
2748 }
2749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 return ", ";
2751 }
2752 }
2753 return linePrefix;
2754 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002755
2756 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07002757 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002758 *
2759 * @param pw a PrintWriter object to print to.
2760 * @param sb a StringBuilder object.
2761 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08002762 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002763 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2764 * @param prefix a String to be prepended to each line of output.
2765 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002766 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002767 */
2768 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07002769 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002770 if (timer != null) {
2771 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07002772 final long totalTimeMs = (timer.getTotalTimeLocked(
2773 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002774 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002775 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002776 sb.setLength(0);
2777 sb.append(prefix);
2778 sb.append(" ");
2779 sb.append(type);
2780 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002781 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002782 sb.append("realtime (");
2783 sb.append(count);
2784 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002785 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
2786 if (maxDurationMs >= 0) {
2787 sb.append(" max=");
2788 sb.append(maxDurationMs);
2789 }
2790 if (timer.isRunningLocked()) {
2791 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
2792 if (currentMs >= 0) {
2793 sb.append(" (running for ");
2794 sb.append(currentMs);
2795 sb.append("ms)");
2796 } else {
2797 sb.append(" (running)");
2798 }
2799 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002800 pw.println(sb.toString());
2801 return true;
2802 }
2803 }
2804 return false;
2805 }
Bookatzc8c44962017-05-11 12:12:54 -07002806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 /**
2808 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07002809 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 * @param sb a StringBuilder object.
2811 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002812 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002814 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 * @param linePrefix a String to be prepended to each line of output.
2816 * @return the line prefix
2817 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002818 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2819 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 long totalTimeMicros = 0;
2821 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07002822 long max = 0;
2823 long current = 0;
2824 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002826 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07002827 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002828 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2829 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07002830 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 }
2832 sb.append(linePrefix);
2833 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2834 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002835 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002837 sb.append(',');
2838 sb.append(current);
2839 sb.append(',');
2840 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07002841 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
2842 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
2843 // totalDuration independent of totalTimeMicros (since they are not pooled).
2844 if (name != null) {
2845 sb.append(',');
2846 sb.append(totalDuration);
2847 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 return ",";
2849 }
Bookatz506a8182017-05-01 14:18:42 -07002850
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002851 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
2852 String type) {
2853 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2854 pw.print(',');
2855 pw.print(uid);
2856 pw.print(',');
2857 pw.print(category);
2858 pw.print(',');
2859 pw.print(type);
2860 }
2861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 /**
2863 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07002864 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 * @param pw the PageWriter to dump log to
2866 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2867 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2868 * @param args type-dependent data arguments
2869 */
Bookatzc8c44962017-05-11 12:12:54 -07002870 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002872 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002873 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002874 pw.print(',');
2875 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002877 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002879
2880 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002881 * Dump a given timer stat for terse checkin mode.
2882 *
2883 * @param pw the PageWriter to dump log to
2884 * @param uid the UID to log
2885 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2886 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2887 * @param timer a {@link Timer} to dump stats for
2888 * @param rawRealtime the current elapsed realtime of the system in microseconds
2889 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2890 */
2891 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2892 Timer timer, long rawRealtime, int which) {
2893 if (timer != null) {
2894 // Convert from microseconds to milliseconds with rounding
2895 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2896 / 1000;
2897 final int count = timer.getCountLocked(which);
2898 if (totalTime != 0) {
2899 dumpLine(pw, uid, category, type, totalTime, count);
2900 }
2901 }
2902 }
2903
2904 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002905 * Checks if the ControllerActivityCounter has any data worth dumping.
2906 */
2907 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
2908 if (counter == null) {
2909 return false;
2910 }
2911
2912 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
2913 || counter.getRxTimeCounter().getCountLocked(which) != 0
2914 || counter.getPowerCounter().getCountLocked(which) != 0) {
2915 return true;
2916 }
2917
2918 for (LongCounter c : counter.getTxTimeCounters()) {
2919 if (c.getCountLocked(which) != 0) {
2920 return true;
2921 }
2922 }
2923 return false;
2924 }
2925
2926 /**
2927 * Dumps the ControllerActivityCounter if it has any data worth dumping.
2928 * The order of the arguments in the final check in line is:
2929 *
2930 * idle, rx, power, tx...
2931 *
2932 * where tx... is one or more transmit level times.
2933 */
2934 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
2935 String type,
2936 ControllerActivityCounter counter,
2937 int which) {
2938 if (!controllerActivityHasData(counter, which)) {
2939 return;
2940 }
2941
2942 dumpLineHeader(pw, uid, category, type);
2943 pw.print(",");
2944 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
2945 pw.print(",");
2946 pw.print(counter.getRxTimeCounter().getCountLocked(which));
2947 pw.print(",");
2948 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
2949 for (LongCounter c : counter.getTxTimeCounters()) {
2950 pw.print(",");
2951 pw.print(c.getCountLocked(which));
2952 }
2953 pw.println();
2954 }
2955
2956 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
2957 String prefix, String controllerName,
2958 ControllerActivityCounter counter,
2959 int which) {
2960 if (controllerActivityHasData(counter, which)) {
2961 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
2962 }
2963 }
2964
2965 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
2966 String controllerName,
2967 ControllerActivityCounter counter, int which) {
2968 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
2969 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
2970 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
2971 long totalTxTimeMs = 0;
2972 for (LongCounter txState : counter.getTxTimeCounters()) {
2973 totalTxTimeMs += txState.getCountLocked(which);
2974 }
2975
2976 final long totalTimeMs = idleTimeMs + rxTimeMs + totalTxTimeMs;
2977
2978 sb.setLength(0);
2979 sb.append(prefix);
2980 sb.append(" ");
2981 sb.append(controllerName);
2982 sb.append(" Idle time: ");
2983 formatTimeMs(sb, idleTimeMs);
2984 sb.append("(");
2985 sb.append(formatRatioLocked(idleTimeMs, totalTimeMs));
2986 sb.append(")");
2987 pw.println(sb.toString());
2988
2989 sb.setLength(0);
2990 sb.append(prefix);
2991 sb.append(" ");
2992 sb.append(controllerName);
2993 sb.append(" Rx time: ");
2994 formatTimeMs(sb, rxTimeMs);
2995 sb.append("(");
2996 sb.append(formatRatioLocked(rxTimeMs, totalTimeMs));
2997 sb.append(")");
2998 pw.println(sb.toString());
2999
3000 sb.setLength(0);
3001 sb.append(prefix);
3002 sb.append(" ");
3003 sb.append(controllerName);
3004 sb.append(" Tx time: ");
3005 formatTimeMs(sb, totalTxTimeMs);
3006 sb.append("(");
3007 sb.append(formatRatioLocked(totalTxTimeMs, totalTimeMs));
3008 sb.append(")");
3009 pw.println(sb.toString());
3010
3011 final int numTxLvls = counter.getTxTimeCounters().length;
3012 if (numTxLvls > 1) {
3013 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3014 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3015 sb.setLength(0);
3016 sb.append(prefix);
3017 sb.append(" [");
3018 sb.append(lvl);
3019 sb.append("] ");
3020 formatTimeMs(sb, txLvlTimeMs);
3021 sb.append("(");
3022 sb.append(formatRatioLocked(txLvlTimeMs, totalTxTimeMs));
3023 sb.append(")");
3024 pw.println(sb.toString());
3025 }
3026 }
3027
3028 sb.setLength(0);
3029 sb.append(prefix);
3030 sb.append(" ");
3031 sb.append(controllerName);
3032 sb.append(" Power drain: ").append(
3033 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
3034 sb.append("mAh");
3035 pw.println(sb.toString());
3036 }
3037
3038 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003039 * Temporary for settings.
3040 */
3041 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3042 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3043 }
3044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003045 /**
3046 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003047 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003049 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003050 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3051 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3053 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07003054 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3057 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003058 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3059 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3060 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003061 final long totalRealtime = computeRealtime(rawRealtime, which);
3062 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003063 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003064 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003065 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003066 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3067 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003068 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003069 rawRealtime, which);
3070 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3071 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003072 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003073 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003074 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003075 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003076 final long dischargeCount = getDischargeCoulombCounter().getCountLocked(which);
3077 final long dischargeScreenOffCount = getDischargeScreenOffCoulombCounter()
3078 .getCountLocked(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003079
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003080 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003081
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003082 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003083 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003084
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003085 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003088 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003089 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003090 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003091 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003092 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003093 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003094 getEstimatedBatteryCapacity(),
3095 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity());
Adam Lesinski67c134f2016-06-10 15:15:08 -07003096
Bookatzc8c44962017-05-11 12:12:54 -07003097
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003098 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003099 long fullWakeLockTimeTotal = 0;
3100 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003101
Evan Millar22ac0432009-03-31 11:33:18 -07003102 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003103 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003104
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003105 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3106 = u.getWakelockStats();
3107 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3108 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003109
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003110 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3111 if (fullWakeTimer != null) {
3112 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3113 which);
3114 }
3115
3116 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3117 if (partialWakeTimer != null) {
3118 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3119 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003120 }
3121 }
3122 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003123
3124 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003125 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3126 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3127 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3128 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3129 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3130 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3131 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3132 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003133 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3134 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003135 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3136 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003137 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3138 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003139
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003140 // Dump Modem controller stats
3141 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3142 getModemControllerActivity(), which);
3143
Adam Lesinskie283d332015-04-16 12:29:25 -07003144 // Dump Wifi controller stats
3145 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3146 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003147 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003148 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003149
3150 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3151 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003152
3153 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003154 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3155 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157 // Dump misc stats
3158 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003159 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003160 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003161 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003162 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003163 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003164 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3165 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003166 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003167 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3168 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3169 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3170 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003171 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003172
Dianne Hackborn617f8772009-03-31 15:04:46 -07003173 // Dump screen brightness stats
3174 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3175 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003176 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003177 }
3178 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003179
Dianne Hackborn627bba72009-03-24 22:32:56 -07003180 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003181 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3182 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003183 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003184 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003185 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003186 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003187 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003188 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003189 args[i] = getPhoneSignalStrengthCount(i, which);
3190 }
3191 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003192
Dianne Hackborn627bba72009-03-24 22:32:56 -07003193 // Dump network type stats
3194 args = new Object[NUM_DATA_CONNECTION_TYPES];
3195 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003196 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003197 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003198 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3199 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3200 args[i] = getPhoneDataConnectionCount(i, which);
3201 }
3202 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003203
3204 // Dump wifi state stats
3205 args = new Object[NUM_WIFI_STATES];
3206 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003207 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003208 }
3209 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3210 for (int i=0; i<NUM_WIFI_STATES; i++) {
3211 args[i] = getWifiStateCount(i, which);
3212 }
3213 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3214
Dianne Hackborn3251b902014-06-20 14:40:53 -07003215 // Dump wifi suppl state stats
3216 args = new Object[NUM_WIFI_SUPPL_STATES];
3217 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3218 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3219 }
3220 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3221 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3222 args[i] = getWifiSupplStateCount(i, which);
3223 }
3224 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3225
3226 // Dump wifi signal strength stats
3227 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3228 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3229 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3230 }
3231 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3232 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3233 args[i] = getWifiSignalStrengthCount(i, which);
3234 }
3235 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3236
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003237 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003238 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003239 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003240 }
Bookatzc8c44962017-05-11 12:12:54 -07003241
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003242 if (which == STATS_SINCE_UNPLUGGED) {
3243 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3244 getDischargeStartLevel()-getDischargeCurrentLevel(),
3245 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003246 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
3247 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003248 } else {
3249 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3250 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003251 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003252 getDischargeAmountScreenOffSinceCharge(),
3253 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003254 }
Bookatzc8c44962017-05-11 12:12:54 -07003255
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003256 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003257 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003258 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003259 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003260 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003261 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003262 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3263 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003264 }
Evan Millarc64edde2009-04-18 12:26:32 -07003265 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003266 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003267 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003268 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3269 // Not doing the regular wake lock formatting to remain compatible
3270 // with the old checkin format.
3271 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3272 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003273 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003274 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003275 }
3276 }
Evan Millarc64edde2009-04-18 12:26:32 -07003277 }
Bookatzc8c44962017-05-11 12:12:54 -07003278
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003279 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003280 helper.create(this);
3281 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003282 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003283 if (sippers != null && sippers.size() > 0) {
3284 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3285 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003286 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003287 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3288 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
3289 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003290 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003291 int uid = 0;
3292 String label;
3293 switch (bs.drainType) {
3294 case IDLE:
3295 label="idle";
3296 break;
3297 case CELL:
3298 label="cell";
3299 break;
3300 case PHONE:
3301 label="phone";
3302 break;
3303 case WIFI:
3304 label="wifi";
3305 break;
3306 case BLUETOOTH:
3307 label="blue";
3308 break;
3309 case SCREEN:
3310 label="scrn";
3311 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003312 case FLASHLIGHT:
3313 label="flashlight";
3314 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003315 case APP:
3316 uid = bs.uidObj.getUid();
3317 label = "uid";
3318 break;
3319 case USER:
3320 uid = UserHandle.getUid(bs.userId, 0);
3321 label = "user";
3322 break;
3323 case UNACCOUNTED:
3324 label = "unacc";
3325 break;
3326 case OVERCOUNTED:
3327 label = "over";
3328 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003329 case CAMERA:
3330 label = "camera";
3331 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003332 default:
3333 label = "???";
3334 }
3335 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Adam Lesinskie08af192015-03-25 16:42:59 -07003336 BatteryStatsHelper.makemAh(bs.totalPowerMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003337 }
3338 }
3339
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003340 final long[] cpuFreqs = getCpuFreqs();
3341 if (cpuFreqs != null) {
3342 sb.setLength(0);
3343 for (int i = 0; i < cpuFreqs.length; ++i) {
3344 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3345 }
3346 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3347 }
3348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 for (int iu = 0; iu < NU; iu++) {
3350 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003351 if (reqUid >= 0 && uid != reqUid) {
3352 continue;
3353 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003354 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003357 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3358 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3359 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3360 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3361 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3362 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3363 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3364 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003365 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003366 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3367 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003368 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003369 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3370 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003371 // Background data transfers
3372 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3373 which);
3374 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3375 which);
3376 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3377 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3378 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3379 which);
3380 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3381 which);
3382 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3383 which);
3384 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3385 which);
3386
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003387 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3388 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003389 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003390 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3391 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3392 || wifiBytesBgTx > 0
3393 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3394 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003395 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3396 wifiBytesRx, wifiBytesTx,
3397 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003398 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003399 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003400 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3401 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3402 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3403 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003404 }
3405
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003406 // Dump modem controller data, per UID.
3407 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3408 u.getModemControllerActivity(), which);
3409
3410 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003411 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3412 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3413 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003414 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3415 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003416 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3417 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3418 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003419 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003420 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003421 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3422 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003423 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3424 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003425 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003426 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003429 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3430 u.getWifiControllerActivity(), which);
3431
Bookatz867c0d72017-03-07 18:23:42 -08003432 final Timer bleTimer = u.getBluetoothScanTimer();
3433 if (bleTimer != null) {
3434 // Convert from microseconds to milliseconds with rounding
3435 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3436 / 1000;
3437 if (totalTime != 0) {
3438 final int count = bleTimer.getCountLocked(which);
3439 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3440 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003441 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3442 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3443 final long actualTimeBg = bleTimerBg != null ?
3444 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003445 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003446 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3447 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003448 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3449 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3450 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3451 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3452 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
3453 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3454 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
3455 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3456 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
3457 final Timer unoptimizedScanTimerBg =
3458 u.getBluetoothUnoptimizedScanBackgroundTimer();
3459 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
3460 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3461 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
3462 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3463
Bookatz867c0d72017-03-07 18:23:42 -08003464 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07003465 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
3466 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
3467 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08003468 }
3469 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003470
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003471 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3472 u.getBluetoothControllerActivity(), which);
3473
Dianne Hackborn617f8772009-03-31 15:04:46 -07003474 if (u.hasUserActivity()) {
3475 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3476 boolean hasData = false;
3477 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3478 int val = u.getUserActivityCount(i, which);
3479 args[i] = val;
3480 if (val != 0) hasData = true;
3481 }
3482 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003483 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003484 }
3485 }
Bookatzc8c44962017-05-11 12:12:54 -07003486
3487 if (u.getAggregatedPartialWakelockTimer() != null) {
3488 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07003489 // Times are since reset (regardless of 'which')
3490 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07003491 final Timer bgTimer = timer.getSubTimer();
3492 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003493 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07003494 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
3495 }
3496
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003497 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3498 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3499 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3500 String linePrefix = "";
3501 sb.setLength(0);
3502 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3503 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003504 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3505 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003506 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003507 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
3508 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003509 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3510 rawRealtime, "w", which, linePrefix);
3511
3512 // Only log if we had at lease one wakelock...
3513 if (sb.length() > 0) {
3514 String name = wakelocks.keyAt(iw);
3515 if (name.indexOf(',') >= 0) {
3516 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003518 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 }
3520 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003521
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003522 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3523 for (int isy=syncs.size()-1; isy>=0; isy--) {
3524 final Timer timer = syncs.valueAt(isy);
3525 // Convert from microseconds to milliseconds with rounding
3526 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3527 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07003528 final Timer bgTimer = timer.getSubTimer();
3529 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003530 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07003531 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003532 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003533 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07003534 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003535 }
3536 }
3537
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003538 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3539 for (int ij=jobs.size()-1; ij>=0; ij--) {
3540 final Timer timer = jobs.valueAt(ij);
3541 // Convert from microseconds to milliseconds with rounding
3542 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3543 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07003544 final Timer bgTimer = timer.getSubTimer();
3545 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003546 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07003547 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003548 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003549 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07003550 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003551 }
3552 }
3553
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003554 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3555 rawRealtime, which);
3556 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3557 rawRealtime, which);
3558 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3559 rawRealtime, which);
3560 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3561 rawRealtime, which);
3562
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003563 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3564 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003565 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003566 final Uid.Sensor se = sensors.valueAt(ise);
3567 final int sensorNumber = sensors.keyAt(ise);
3568 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003569 if (timer != null) {
3570 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003571 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3572 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003573 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08003574 final int count = timer.getCountLocked(which);
3575 final Timer bgTimer = se.getSensorBackgroundTime();
3576 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003577 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3578 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
3579 final long bgActualTime = bgTimer != null ?
3580 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3581 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
3582 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003583 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003584 }
3585 }
3586
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003587 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3588 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003589
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003590 dumpTimer(pw, uid, category, FOREGROUND_DATA, u.getForegroundActivityTimer(),
3591 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003592
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003593 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003594 long totalStateTime = 0;
3595 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003596 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3597 totalStateTime += time;
3598 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003599 }
3600 if (totalStateTime > 0) {
3601 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3602 }
3603
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003604 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3605 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003606 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003607 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003608 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003609 }
3610
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003611 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
3612 // If total cpuFreqTimes is null, then we don't need to check for screenOffCpuFreqTimes.
3613 if (cpuFreqTimeMs != null) {
3614 sb.setLength(0);
3615 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
3616 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
3617 }
3618 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
3619 if (screenOffCpuFreqTimeMs != null) {
3620 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
3621 sb.append("," + screenOffCpuFreqTimeMs[i]);
3622 }
3623 } else {
3624 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
3625 sb.append(",0");
3626 }
3627 }
3628 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
3629 cpuFreqTimeMs.length, sb.toString());
3630 }
3631
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003632 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3633 = u.getProcessStats();
3634 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3635 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003636
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003637 final long userMillis = ps.getUserTime(which);
3638 final long systemMillis = ps.getSystemTime(which);
3639 final long foregroundMillis = ps.getForegroundTime(which);
3640 final int starts = ps.getStarts(which);
3641 final int numCrashes = ps.getNumCrashes(which);
3642 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003643
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003644 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3645 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003646 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
3647 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 }
3649 }
3650
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003651 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3652 = u.getPackageStats();
3653 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3654 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3655 int wakeups = 0;
3656 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3657 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07003658 int count = alarms.valueAt(iwa).getCountLocked(which);
3659 wakeups += count;
3660 String name = alarms.keyAt(iwa).replace(',', '_');
3661 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003662 }
3663 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3664 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3665 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3666 final long startTime = ss.getStartTime(batteryUptime, which);
3667 final int starts = ss.getStarts(which);
3668 final int launches = ss.getLaunches(which);
3669 if (startTime != 0 || starts != 0 || launches != 0) {
3670 dumpLine(pw, uid, category, APK_DATA,
3671 wakeups, // wakeup alarms
3672 packageStats.keyAt(ipkg), // Apk
3673 serviceStats.keyAt(isvc), // service
3674 startTime / 1000, // time spent started, in ms
3675 starts,
3676 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003677 }
3678 }
3679 }
3680 }
3681 }
3682
Dianne Hackborn81038902012-11-26 17:04:09 -08003683 static final class TimerEntry {
3684 final String mName;
3685 final int mId;
3686 final BatteryStats.Timer mTimer;
3687 final long mTime;
3688 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3689 mName = name;
3690 mId = id;
3691 mTimer = timer;
3692 mTime = time;
3693 }
3694 }
3695
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003696 private void printmAh(PrintWriter printer, double power) {
3697 printer.print(BatteryStatsHelper.makemAh(power));
3698 }
3699
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003700 private void printmAh(StringBuilder sb, double power) {
3701 sb.append(BatteryStatsHelper.makemAh(power));
3702 }
3703
Dianne Hackbornd953c532014-08-16 18:17:38 -07003704 /**
3705 * Temporary for settings.
3706 */
3707 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3708 int reqUid) {
3709 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3710 }
3711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003712 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003713 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003714 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003715 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3716 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07003717 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003718 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719
3720 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3721 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3722 final long totalRealtime = computeRealtime(rawRealtime, which);
3723 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003724 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3725 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3726 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003727 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3728 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003729
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003730 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003731
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003732 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003733 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003734
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003735 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
3736 if (estimatedBatteryCapacity > 0) {
3737 sb.setLength(0);
3738 sb.append(prefix);
3739 sb.append(" Estimated battery capacity: ");
3740 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
3741 sb.append(" mAh");
3742 pw.println(sb.toString());
3743 }
3744
Jocelyn Dangc627d102017-04-14 13:15:14 -07003745 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
3746 if (minLearnedBatteryCapacity > 0) {
3747 sb.setLength(0);
3748 sb.append(prefix);
3749 sb.append(" Min learned battery capacity: ");
3750 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
3751 sb.append(" mAh");
3752 pw.println(sb.toString());
3753 }
3754 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
3755 if (maxLearnedBatteryCapacity > 0) {
3756 sb.setLength(0);
3757 sb.append(prefix);
3758 sb.append(" Max learned battery capacity: ");
3759 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
3760 sb.append(" mAh");
3761 pw.println(sb.toString());
3762 }
3763
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003764 sb.setLength(0);
3765 sb.append(prefix);
3766 sb.append(" Time on battery: ");
3767 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3768 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3769 sb.append(") realtime, ");
3770 formatTimeMs(sb, whichBatteryUptime / 1000);
3771 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3772 sb.append(") uptime");
3773 pw.println(sb.toString());
3774 sb.setLength(0);
3775 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003776 sb.append(" Time on battery screen off: ");
3777 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3778 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3779 sb.append(") realtime, ");
3780 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3781 sb.append("(");
3782 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3783 sb.append(") uptime");
3784 pw.println(sb.toString());
3785 sb.setLength(0);
3786 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003787 sb.append(" Total run time: ");
3788 formatTimeMs(sb, totalRealtime / 1000);
3789 sb.append("realtime, ");
3790 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003791 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003792 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003793 if (batteryTimeRemaining >= 0) {
3794 sb.setLength(0);
3795 sb.append(prefix);
3796 sb.append(" Battery time remaining: ");
3797 formatTimeMs(sb, batteryTimeRemaining / 1000);
3798 pw.println(sb.toString());
3799 }
3800 if (chargeTimeRemaining >= 0) {
3801 sb.setLength(0);
3802 sb.append(prefix);
3803 sb.append(" Charge time remaining: ");
3804 formatTimeMs(sb, chargeTimeRemaining / 1000);
3805 pw.println(sb.toString());
3806 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003807
3808 final LongCounter dischargeCounter = getDischargeCoulombCounter();
3809 final long dischargeCount = dischargeCounter.getCountLocked(which);
3810 if (dischargeCount >= 0) {
3811 sb.setLength(0);
3812 sb.append(prefix);
3813 sb.append(" Discharge: ");
3814 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
3815 sb.append(" mAh");
3816 pw.println(sb.toString());
3817 }
3818
3819 final LongCounter dischargeScreenOffCounter = getDischargeScreenOffCoulombCounter();
3820 final long dischargeScreenOffCount = dischargeScreenOffCounter.getCountLocked(which);
3821 if (dischargeScreenOffCount >= 0) {
3822 sb.setLength(0);
3823 sb.append(prefix);
3824 sb.append(" Screen off discharge: ");
3825 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
3826 sb.append(" mAh");
3827 pw.println(sb.toString());
3828 }
3829
3830 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
3831 if (dischargeScreenOnCount >= 0) {
3832 sb.setLength(0);
3833 sb.append(prefix);
3834 sb.append(" Screen on discharge: ");
3835 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
3836 sb.append(" mAh");
3837 pw.println(sb.toString());
3838 }
3839
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003840 pw.print(" Start clock time: ");
3841 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3842
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003843 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003844 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003845 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003846 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3847 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003848 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003849 rawRealtime, which);
3850 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3851 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003852 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003853 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003854 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3855 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3856 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003857 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003858 sb.append(prefix);
3859 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3860 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003861 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003862 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3863 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003864 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003865 pw.println(sb.toString());
3866 sb.setLength(0);
3867 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003868 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003869 boolean didOne = false;
3870 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003871 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003872 if (time == 0) {
3873 continue;
3874 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003875 sb.append("\n ");
3876 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003877 didOne = true;
3878 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3879 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003880 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003881 sb.append("(");
3882 sb.append(formatRatioLocked(time, screenOnTime));
3883 sb.append(")");
3884 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003885 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003886 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003887 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003888 sb.setLength(0);
3889 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003890 sb.append(" Power save mode enabled: ");
3891 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003892 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003893 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003894 sb.append(")");
3895 pw.println(sb.toString());
3896 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003897 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003898 sb.setLength(0);
3899 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003900 sb.append(" Device light idling: ");
3901 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003902 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003903 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
3904 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003905 sb.append("x");
3906 pw.println(sb.toString());
3907 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003908 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003909 sb.setLength(0);
3910 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003911 sb.append(" Idle mode light time: ");
3912 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003913 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003914 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
3915 sb.append(") ");
3916 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003917 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003918 sb.append(" -- longest ");
3919 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
3920 pw.println(sb.toString());
3921 }
3922 if (deviceIdlingTime != 0) {
3923 sb.setLength(0);
3924 sb.append(prefix);
3925 sb.append(" Device full idling: ");
3926 formatTimeMs(sb, deviceIdlingTime / 1000);
3927 sb.append("(");
3928 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003929 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003930 sb.append("x");
3931 pw.println(sb.toString());
3932 }
3933 if (deviceIdleModeFullTime != 0) {
3934 sb.setLength(0);
3935 sb.append(prefix);
3936 sb.append(" Idle mode full time: ");
3937 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
3938 sb.append("(");
3939 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
3940 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003941 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003942 sb.append("x");
3943 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003944 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003945 pw.println(sb.toString());
3946 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003947 if (phoneOnTime != 0) {
3948 sb.setLength(0);
3949 sb.append(prefix);
3950 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3951 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003952 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003953 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003954 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003955 if (connChanges != 0) {
3956 pw.print(prefix);
3957 pw.print(" Connectivity changes: "); pw.println(connChanges);
3958 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003959
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003960 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003961 long fullWakeLockTimeTotalMicros = 0;
3962 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003963
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003964 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003965
Evan Millar22ac0432009-03-31 11:33:18 -07003966 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003967 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003968
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003969 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3970 = u.getWakelockStats();
3971 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3972 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003973
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003974 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3975 if (fullWakeTimer != null) {
3976 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3977 rawRealtime, which);
3978 }
3979
3980 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3981 if (partialWakeTimer != null) {
3982 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3983 rawRealtime, which);
3984 if (totalTimeMicros > 0) {
3985 if (reqUid < 0) {
3986 // Only show the ordered list of all wake
3987 // locks if the caller is not asking for data
3988 // about a specific uid.
3989 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3990 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003991 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003992 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003993 }
3994 }
3995 }
3996 }
Bookatzc8c44962017-05-11 12:12:54 -07003997
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003998 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3999 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4000 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4001 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4002 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4003 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4004 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4005 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004006 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4007 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004008
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004009 if (fullWakeLockTimeTotalMicros != 0) {
4010 sb.setLength(0);
4011 sb.append(prefix);
4012 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4013 (fullWakeLockTimeTotalMicros + 500) / 1000);
4014 pw.println(sb.toString());
4015 }
4016
4017 if (partialWakeLockTimeTotalMicros != 0) {
4018 sb.setLength(0);
4019 sb.append(prefix);
4020 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4021 (partialWakeLockTimeTotalMicros + 500) / 1000);
4022 pw.println(sb.toString());
4023 }
4024
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004025 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004026 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
4027 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
4028 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
4029 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004030 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004031 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004032 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004033 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08004034 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004035 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004036 if (time == 0) {
4037 continue;
4038 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004039 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004040 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004041 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08004042 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004043 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004044 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004045 sb.append("(");
4046 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004047 sb.append(") ");
4048 sb.append(getPhoneSignalStrengthCount(i, which));
4049 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004050 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004051 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004052 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004053
4054 sb.setLength(0);
4055 sb.append(prefix);
4056 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004057 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004058 pw.println(sb.toString());
4059
Dianne Hackborn627bba72009-03-24 22:32:56 -07004060 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004061 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004062 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004063 didOne = false;
4064 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004065 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004066 if (time == 0) {
4067 continue;
4068 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004069 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004070 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004071 didOne = true;
4072 sb.append(DATA_CONNECTION_NAMES[i]);
4073 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004074 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004075 sb.append("(");
4076 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004077 sb.append(") ");
4078 sb.append(getPhoneDataConnectionCount(i, which));
4079 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004080 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004081 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004082 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004083
4084 sb.setLength(0);
4085 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004086 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004087 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004088 formatTimeMs(sb, mobileActiveTime / 1000);
4089 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4090 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
4091 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004092 pw.println(sb.toString());
4093
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004094 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
4095 if (mobileActiveUnknownTime != 0) {
4096 sb.setLength(0);
4097 sb.append(prefix);
4098 sb.append(" Mobile radio active unknown time: ");
4099 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
4100 sb.append("(");
4101 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
4102 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
4103 sb.append("x");
4104 pw.println(sb.toString());
4105 }
4106
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004107 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
4108 if (mobileActiveAdjustedTime != 0) {
4109 sb.setLength(0);
4110 sb.append(prefix);
4111 sb.append(" Mobile radio active adjusted time: ");
4112 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
4113 sb.append("(");
4114 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
4115 sb.append(")");
4116 pw.println(sb.toString());
4117 }
4118
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004119 printControllerActivity(pw, sb, prefix, "Radio", getModemControllerActivity(), which);
4120
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004121 pw.print(prefix);
4122 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
4123 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
4124 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
4125 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004126 sb.setLength(0);
4127 sb.append(prefix);
4128 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
4129 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
4130 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
4131 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004132 sb.append(")");
4133 pw.println(sb.toString());
4134
4135 sb.setLength(0);
4136 sb.append(prefix);
4137 sb.append(" Wifi states:");
4138 didOne = false;
4139 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004140 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004141 if (time == 0) {
4142 continue;
4143 }
4144 sb.append("\n ");
4145 didOne = true;
4146 sb.append(WIFI_STATE_NAMES[i]);
4147 sb.append(" ");
4148 formatTimeMs(sb, time/1000);
4149 sb.append("(");
4150 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4151 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004152 sb.append(getWifiStateCount(i, which));
4153 sb.append("x");
4154 }
4155 if (!didOne) sb.append(" (no activity)");
4156 pw.println(sb.toString());
4157
4158 sb.setLength(0);
4159 sb.append(prefix);
4160 sb.append(" Wifi supplicant states:");
4161 didOne = false;
4162 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4163 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4164 if (time == 0) {
4165 continue;
4166 }
4167 sb.append("\n ");
4168 didOne = true;
4169 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4170 sb.append(" ");
4171 formatTimeMs(sb, time/1000);
4172 sb.append("(");
4173 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4174 sb.append(") ");
4175 sb.append(getWifiSupplStateCount(i, which));
4176 sb.append("x");
4177 }
4178 if (!didOne) sb.append(" (no activity)");
4179 pw.println(sb.toString());
4180
4181 sb.setLength(0);
4182 sb.append(prefix);
4183 sb.append(" Wifi signal levels:");
4184 didOne = false;
4185 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4186 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4187 if (time == 0) {
4188 continue;
4189 }
4190 sb.append("\n ");
4191 sb.append(prefix);
4192 didOne = true;
4193 sb.append("level(");
4194 sb.append(i);
4195 sb.append(") ");
4196 formatTimeMs(sb, time/1000);
4197 sb.append("(");
4198 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4199 sb.append(") ");
4200 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004201 sb.append("x");
4202 }
4203 if (!didOne) sb.append(" (no activity)");
4204 pw.println(sb.toString());
4205
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004206 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004207
Adam Lesinski50e47602015-12-04 17:04:54 -08004208 pw.print(prefix);
4209 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4210 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4211
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004212 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4213 sb.setLength(0);
4214 sb.append(prefix);
4215 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4216 pw.println(sb.toString());
4217
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004218 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4219 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004220
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004221 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004222
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004223 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004224 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004225 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004226 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004227 pw.println(getDischargeStartLevel());
4228 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4229 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004230 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004231 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004232 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004233 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004234 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004235 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004236 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004237 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4238 pw.println(getDischargeAmountScreenOn());
4239 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4240 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004241 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004242 } else {
4243 pw.print(prefix); pw.println(" Device battery use since last full charge");
4244 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
4245 pw.println(getLowDischargeAmountSinceCharge());
4246 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
4247 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004248 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4249 pw.println(getDischargeAmountScreenOnSinceCharge());
4250 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4251 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004252 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004253 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004254
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004255 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004256 helper.create(this);
4257 helper.refreshStats(which, UserHandle.USER_ALL);
4258 List<BatterySipper> sippers = helper.getUsageList();
4259 if (sippers != null && sippers.size() > 0) {
4260 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4261 pw.print(prefix); pw.print(" Capacity: ");
4262 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004263 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004264 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4265 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4266 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4267 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004268 pw.println();
4269 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004270 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004271 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004272 switch (bs.drainType) {
4273 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004274 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004275 break;
4276 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004277 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004278 break;
4279 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004280 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004281 break;
4282 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004283 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004284 break;
4285 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004286 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004287 break;
4288 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004289 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004290 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004291 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004292 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004293 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004294 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004295 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004296 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004297 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004298 break;
4299 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004300 pw.print(" User "); pw.print(bs.userId);
4301 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004302 break;
4303 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004304 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004305 break;
4306 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004307 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004308 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004309 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004310 pw.print(" Camera: ");
4311 break;
4312 default:
4313 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004314 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004315 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004316 printmAh(pw, bs.totalPowerMah);
4317
Adam Lesinski57123002015-06-12 16:12:07 -07004318 if (bs.usagePowerMah != bs.totalPowerMah) {
4319 // If the usage (generic power) isn't the whole amount, we list out
4320 // what components are involved in the calculation.
4321
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004322 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004323 if (bs.usagePowerMah != 0) {
4324 pw.print(" usage=");
4325 printmAh(pw, bs.usagePowerMah);
4326 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004327 if (bs.cpuPowerMah != 0) {
4328 pw.print(" cpu=");
4329 printmAh(pw, bs.cpuPowerMah);
4330 }
4331 if (bs.wakeLockPowerMah != 0) {
4332 pw.print(" wake=");
4333 printmAh(pw, bs.wakeLockPowerMah);
4334 }
4335 if (bs.mobileRadioPowerMah != 0) {
4336 pw.print(" radio=");
4337 printmAh(pw, bs.mobileRadioPowerMah);
4338 }
4339 if (bs.wifiPowerMah != 0) {
4340 pw.print(" wifi=");
4341 printmAh(pw, bs.wifiPowerMah);
4342 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004343 if (bs.bluetoothPowerMah != 0) {
4344 pw.print(" bt=");
4345 printmAh(pw, bs.bluetoothPowerMah);
4346 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004347 if (bs.gpsPowerMah != 0) {
4348 pw.print(" gps=");
4349 printmAh(pw, bs.gpsPowerMah);
4350 }
4351 if (bs.sensorPowerMah != 0) {
4352 pw.print(" sensor=");
4353 printmAh(pw, bs.sensorPowerMah);
4354 }
4355 if (bs.cameraPowerMah != 0) {
4356 pw.print(" camera=");
4357 printmAh(pw, bs.cameraPowerMah);
4358 }
4359 if (bs.flashlightPowerMah != 0) {
4360 pw.print(" flash=");
4361 printmAh(pw, bs.flashlightPowerMah);
4362 }
4363 pw.print(" )");
4364 }
4365 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004366 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004367 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004368 }
4369
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004370 sippers = helper.getMobilemsppList();
4371 if (sippers != null && sippers.size() > 0) {
4372 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004373 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004374 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004375 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004376 sb.setLength(0);
4377 sb.append(prefix); sb.append(" Uid ");
4378 UserHandle.formatUid(sb, bs.uidObj.getUid());
4379 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4380 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4381 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004382 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004383 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004384 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004385 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004386 sb.setLength(0);
4387 sb.append(prefix);
4388 sb.append(" TOTAL TIME: ");
4389 formatTimeMs(sb, totalTime);
4390 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4391 sb.append(")");
4392 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004393 pw.println();
4394 }
4395
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004396 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4397 @Override
4398 public int compare(TimerEntry lhs, TimerEntry rhs) {
4399 long lhsTime = lhs.mTime;
4400 long rhsTime = rhs.mTime;
4401 if (lhsTime < rhsTime) {
4402 return 1;
4403 }
4404 if (lhsTime > rhsTime) {
4405 return -1;
4406 }
4407 return 0;
4408 }
4409 };
4410
4411 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004412 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4413 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004414 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004415 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4416 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4417 : kernelWakelocks.entrySet()) {
4418 final BatteryStats.Timer timer = ent.getValue();
4419 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004420 if (totalTimeMillis > 0) {
4421 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4422 }
4423 }
4424 if (ktimers.size() > 0) {
4425 Collections.sort(ktimers, timerComparator);
4426 pw.print(prefix); pw.println(" All kernel wake locks:");
4427 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004428 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004429 String linePrefix = ": ";
4430 sb.setLength(0);
4431 sb.append(prefix);
4432 sb.append(" Kernel Wake lock ");
4433 sb.append(timer.mName);
4434 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4435 which, linePrefix);
4436 if (!linePrefix.equals(": ")) {
4437 sb.append(" realtime");
4438 // Only print out wake locks that were held
4439 pw.println(sb.toString());
4440 }
4441 }
4442 pw.println();
4443 }
4444 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004445
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004446 if (timers.size() > 0) {
4447 Collections.sort(timers, timerComparator);
4448 pw.print(prefix); pw.println(" All partial wake locks:");
4449 for (int i=0; i<timers.size(); i++) {
4450 TimerEntry timer = timers.get(i);
4451 sb.setLength(0);
4452 sb.append(" Wake lock ");
4453 UserHandle.formatUid(sb, timer.mId);
4454 sb.append(" ");
4455 sb.append(timer.mName);
4456 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4457 sb.append(" realtime");
4458 pw.println(sb.toString());
4459 }
4460 timers.clear();
4461 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08004462 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004463
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004464 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004465 if (wakeupReasons.size() > 0) {
4466 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004467 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004468 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004469 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004470 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
4471 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004472 }
4473 Collections.sort(reasons, timerComparator);
4474 for (int i=0; i<reasons.size(); i++) {
4475 TimerEntry timer = reasons.get(i);
4476 String linePrefix = ": ";
4477 sb.setLength(0);
4478 sb.append(prefix);
4479 sb.append(" Wakeup reason ");
4480 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004481 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4482 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004483 pw.println(sb.toString());
4484 }
4485 pw.println();
4486 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004487 }
Evan Millar22ac0432009-03-31 11:33:18 -07004488
James Carr2dd7e5e2016-07-20 18:48:39 -07004489 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
4490 pw.println("Memory Stats");
4491 for (int i = 0; i < mMemoryStats.size(); i++) {
4492 sb.setLength(0);
4493 sb.append("Bandwidth ");
4494 sb.append(mMemoryStats.keyAt(i));
4495 sb.append(" Time ");
4496 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
4497 pw.println(sb.toString());
4498 }
4499
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004500 final long[] cpuFreqs = getCpuFreqs();
4501 if (cpuFreqs != null) {
4502 sb.setLength(0);
4503 sb.append("CPU freqs:");
4504 for (int i = 0; i < cpuFreqs.length; ++i) {
4505 sb.append(" " + cpuFreqs[i]);
4506 }
4507 pw.println(sb.toString());
4508 }
4509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004510 for (int iu=0; iu<NU; iu++) {
4511 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004512 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004513 continue;
4514 }
Bookatzc8c44962017-05-11 12:12:54 -07004515
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004516 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07004517
4518 pw.print(prefix);
4519 pw.print(" ");
4520 UserHandle.formatUid(pw, uid);
4521 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004522 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004523
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004524 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4525 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4526 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4527 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004528 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4529 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
4530
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004531 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4532 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004533 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4534 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004535
4536 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
4537 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
4538
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004539 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4540 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4541 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08004542 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
4543 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4544 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
4545 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004546 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004547
Adam Lesinski5f056f62016-07-14 16:56:08 -07004548 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
4549 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
4550
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004551 if (mobileRxBytes > 0 || mobileTxBytes > 0
4552 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004553 pw.print(prefix); pw.print(" Mobile network: ");
4554 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004555 pw.print(formatBytesLocked(mobileTxBytes));
4556 pw.print(" sent (packets "); pw.print(mobileRxPackets);
4557 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004558 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004559 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
4560 sb.setLength(0);
4561 sb.append(prefix); sb.append(" Mobile radio active: ");
4562 formatTimeMs(sb, uidMobileActiveTime / 1000);
4563 sb.append("(");
4564 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
4565 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
4566 long packets = mobileRxPackets + mobileTxPackets;
4567 if (packets == 0) {
4568 packets = 1;
4569 }
4570 sb.append(" @ ");
4571 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
4572 sb.append(" mspp");
4573 pw.println(sb.toString());
4574 }
4575
Adam Lesinski5f056f62016-07-14 16:56:08 -07004576 if (mobileWakeup > 0) {
4577 sb.setLength(0);
4578 sb.append(prefix);
4579 sb.append(" Mobile radio AP wakeups: ");
4580 sb.append(mobileWakeup);
4581 pw.println(sb.toString());
4582 }
4583
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004584 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
4585 u.getModemControllerActivity(), which);
4586
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004587 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004588 pw.print(prefix); pw.print(" Wi-Fi network: ");
4589 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004590 pw.print(formatBytesLocked(wifiTxBytes));
4591 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4592 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004593 }
4594
Dianne Hackborn62793e42015-03-09 11:15:41 -07004595 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08004596 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004597 || uidWifiRunningTime != 0) {
4598 sb.setLength(0);
4599 sb.append(prefix); sb.append(" Wifi Running: ");
4600 formatTimeMs(sb, uidWifiRunningTime / 1000);
4601 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4602 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07004603 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004604 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4605 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4606 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08004607 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004608 formatTimeMs(sb, wifiScanTime / 1000);
4609 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004610 whichBatteryRealtime)); sb.append(") ");
4611 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08004612 sb.append("x\n");
4613 // actual and background times are unpooled and since reset (regardless of 'which')
4614 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
4615 formatTimeMs(sb, wifiScanActualTime / 1000);
4616 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
4617 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4618 sb.append(") ");
4619 sb.append(wifiScanCount);
4620 sb.append("x\n");
4621 sb.append(prefix); sb.append(" Background Wifi Scan: ");
4622 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
4623 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
4624 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4625 sb.append(") ");
4626 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07004627 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004628 pw.println(sb.toString());
4629 }
4630
Adam Lesinski5f056f62016-07-14 16:56:08 -07004631 if (wifiWakeup > 0) {
4632 sb.setLength(0);
4633 sb.append(prefix);
4634 sb.append(" WiFi AP wakeups: ");
4635 sb.append(wifiWakeup);
4636 pw.println(sb.toString());
4637 }
4638
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004639 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
4640 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07004641
Adam Lesinski50e47602015-12-04 17:04:54 -08004642 if (btRxBytes > 0 || btTxBytes > 0) {
4643 pw.print(prefix); pw.print(" Bluetooth network: ");
4644 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
4645 pw.print(formatBytesLocked(btTxBytes));
4646 pw.println(" sent");
4647 }
4648
Bookatz867c0d72017-03-07 18:23:42 -08004649 final Timer bleTimer = u.getBluetoothScanTimer();
4650 if (bleTimer != null) {
4651 // Convert from microseconds to milliseconds with rounding
4652 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
4653 / 1000;
4654 if (totalTimeMs != 0) {
4655 final int count = bleTimer.getCountLocked(which);
4656 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
4657 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004658 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4659 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
4660 final long actualTimeMsBg = bleTimerBg != null ?
4661 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07004662 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07004663 final int resultCount = u.getBluetoothScanResultCounter() != null ?
4664 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07004665 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
4666 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
4667 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
4668 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
4669 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
4670 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4671 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
4672 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
4673 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
4674 final Timer unoptimizedScanTimerBg =
4675 u.getBluetoothUnoptimizedScanBackgroundTimer();
4676 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
4677 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4678 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
4679 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004680
4681 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08004682 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07004683 sb.append(prefix);
4684 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08004685 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07004686 sb.append(" (");
4687 sb.append(count);
4688 sb.append(" times)");
4689 if (bleTimer.isRunningLocked()) {
4690 sb.append(" (currently running)");
4691 }
4692 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08004693 }
Bookatzb1f04f32017-05-19 13:57:32 -07004694
4695 sb.append(prefix);
4696 sb.append(" Bluetooth Scan (total actual realtime): ");
4697 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
4698 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08004699 sb.append(count);
4700 sb.append(" times)");
4701 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07004702 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08004703 }
Bookatzb1f04f32017-05-19 13:57:32 -07004704 sb.append("\n");
4705 if (actualTimeMsBg > 0 || countBg > 0) {
4706 sb.append(prefix);
4707 sb.append(" Bluetooth Scan (background realtime): ");
4708 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
4709 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08004710 sb.append(countBg);
4711 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07004712 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
4713 sb.append(" (currently running in background)");
4714 }
4715 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08004716 }
Bookatzb1f04f32017-05-19 13:57:32 -07004717
4718 sb.append(prefix);
4719 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07004720 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07004721 sb.append(" (");
4722 sb.append(resultCountBg);
4723 sb.append(" in background)");
4724
4725 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
4726 sb.append("\n");
4727 sb.append(prefix);
4728 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
4729 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
4730 sb.append(" (max ");
4731 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
4732 sb.append(")");
4733 if (unoptimizedScanTimer != null
4734 && unoptimizedScanTimer.isRunningLocked()) {
4735 sb.append(" (currently running unoptimized)");
4736 }
4737 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
4738 sb.append("\n");
4739 sb.append(prefix);
4740 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
4741 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
4742 sb.append(" (max ");
4743 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
4744 sb.append(")");
4745 if (unoptimizedScanTimerBg.isRunningLocked()) {
4746 sb.append(" (currently running unoptimized in background)");
4747 }
4748 }
4749 }
Bookatz867c0d72017-03-07 18:23:42 -08004750 pw.println(sb.toString());
4751 uidActivity = true;
4752 }
4753 }
4754
4755
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004756
Dianne Hackborn617f8772009-03-31 15:04:46 -07004757 if (u.hasUserActivity()) {
4758 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07004759 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004760 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004761 if (val != 0) {
4762 if (!hasData) {
4763 sb.setLength(0);
4764 sb.append(" User activity: ");
4765 hasData = true;
4766 } else {
4767 sb.append(", ");
4768 }
4769 sb.append(val);
4770 sb.append(" ");
4771 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
4772 }
4773 }
4774 if (hasData) {
4775 pw.println(sb.toString());
4776 }
4777 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004778
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004779 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4780 = u.getWakelockStats();
4781 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004782 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004783 int countWakelock = 0;
4784 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4785 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4786 String linePrefix = ": ";
4787 sb.setLength(0);
4788 sb.append(prefix);
4789 sb.append(" Wake lock ");
4790 sb.append(wakelocks.keyAt(iw));
4791 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
4792 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004793 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4794 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004795 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004796 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
4797 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004798 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
4799 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004800 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
4801 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07004802 sb.append(" realtime");
4803 pw.println(sb.toString());
4804 uidActivity = true;
4805 countWakelock++;
4806
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004807 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
4808 rawRealtime, which);
4809 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
4810 rawRealtime, which);
4811 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
4812 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004813 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07004814 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004815 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004816 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07004817 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
4818 // pooled and therefore just a lower bound)
4819 long actualTotalPartialWakelock = 0;
4820 long actualBgPartialWakelock = 0;
4821 if (u.getAggregatedPartialWakelockTimer() != null) {
4822 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
4823 // Convert from microseconds to milliseconds with rounding
4824 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07004825 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07004826 final Timer bgAggTimer = aggTimer.getSubTimer();
4827 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004828 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07004829 }
4830
4831 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
4832 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
4833 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004834 sb.setLength(0);
4835 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004836 sb.append(" TOTAL wake: ");
4837 boolean needComma = false;
4838 if (totalFullWakelock != 0) {
4839 needComma = true;
4840 formatTimeMs(sb, totalFullWakelock);
4841 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004842 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004843 if (totalPartialWakelock != 0) {
4844 if (needComma) {
4845 sb.append(", ");
4846 }
4847 needComma = true;
4848 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07004849 sb.append("blamed partial");
4850 }
4851 if (actualTotalPartialWakelock != 0) {
4852 if (needComma) {
4853 sb.append(", ");
4854 }
4855 needComma = true;
4856 formatTimeMs(sb, actualTotalPartialWakelock);
4857 sb.append("actual partial");
4858 }
4859 if (actualBgPartialWakelock != 0) {
4860 if (needComma) {
4861 sb.append(", ");
4862 }
4863 needComma = true;
4864 formatTimeMs(sb, actualBgPartialWakelock);
4865 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004866 }
4867 if (totalWindowWakelock != 0) {
4868 if (needComma) {
4869 sb.append(", ");
4870 }
4871 needComma = true;
4872 formatTimeMs(sb, totalWindowWakelock);
4873 sb.append("window");
4874 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004875 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07004876 if (needComma) {
4877 sb.append(",");
4878 }
4879 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004880 formatTimeMs(sb, totalDrawWakelock);
4881 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07004882 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004883 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004884 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004885 }
4886 }
4887
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004888 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4889 for (int isy=syncs.size()-1; isy>=0; isy--) {
4890 final Timer timer = syncs.valueAt(isy);
4891 // Convert from microseconds to milliseconds with rounding
4892 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4893 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07004894 final Timer bgTimer = timer.getSubTimer();
4895 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004896 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07004897 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004898 sb.setLength(0);
4899 sb.append(prefix);
4900 sb.append(" Sync ");
4901 sb.append(syncs.keyAt(isy));
4902 sb.append(": ");
4903 if (totalTime != 0) {
4904 formatTimeMs(sb, totalTime);
4905 sb.append("realtime (");
4906 sb.append(count);
4907 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07004908 if (bgTime > 0) {
4909 sb.append(", ");
4910 formatTimeMs(sb, bgTime);
4911 sb.append("background (");
4912 sb.append(bgCount);
4913 sb.append(" times)");
4914 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004915 } else {
4916 sb.append("(not used)");
4917 }
4918 pw.println(sb.toString());
4919 uidActivity = true;
4920 }
4921
4922 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4923 for (int ij=jobs.size()-1; ij>=0; ij--) {
4924 final Timer timer = jobs.valueAt(ij);
4925 // Convert from microseconds to milliseconds with rounding
4926 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4927 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07004928 final Timer bgTimer = timer.getSubTimer();
4929 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004930 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07004931 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004932 sb.setLength(0);
4933 sb.append(prefix);
4934 sb.append(" Job ");
4935 sb.append(jobs.keyAt(ij));
4936 sb.append(": ");
4937 if (totalTime != 0) {
4938 formatTimeMs(sb, totalTime);
4939 sb.append("realtime (");
4940 sb.append(count);
4941 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07004942 if (bgTime > 0) {
4943 sb.append(", ");
4944 formatTimeMs(sb, bgTime);
4945 sb.append("background (");
4946 sb.append(bgCount);
4947 sb.append(" times)");
4948 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004949 } else {
4950 sb.append("(not used)");
4951 }
4952 pw.println(sb.toString());
4953 uidActivity = true;
4954 }
4955
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004956 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
4957 prefix, "Flashlight");
4958 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
4959 prefix, "Camera");
4960 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
4961 prefix, "Video");
4962 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
4963 prefix, "Audio");
4964
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004965 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4966 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004967 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004968 final Uid.Sensor se = sensors.valueAt(ise);
4969 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004970 sb.setLength(0);
4971 sb.append(prefix);
4972 sb.append(" Sensor ");
4973 int handle = se.getHandle();
4974 if (handle == Uid.Sensor.GPS) {
4975 sb.append("GPS");
4976 } else {
4977 sb.append(handle);
4978 }
4979 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004980
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004981 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004982 if (timer != null) {
4983 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08004984 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4985 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004986 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08004987 final Timer bgTimer = se.getSensorBackgroundTime();
4988 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004989 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4990 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4991 final long bgActualTime = bgTimer != null ?
4992 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4993
Dianne Hackborn61659e52014-07-09 16:13:01 -07004994 //timer.logState();
4995 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004996 if (actualTime != totalTime) {
4997 formatTimeMs(sb, totalTime);
4998 sb.append("blamed realtime, ");
4999 }
5000
5001 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005002 sb.append("realtime (");
5003 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005004 sb.append(" times)");
5005
5006 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005007 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005008 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5009 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005010 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005011 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005013 } else {
5014 sb.append("(not used)");
5015 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005016 } else {
5017 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005018 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005019
5020 pw.println(sb.toString());
5021 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005022 }
5023
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005024 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5025 "Vibrator");
5026 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5027 prefix, "Foreground activities");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005028
Dianne Hackborn61659e52014-07-09 16:13:01 -07005029 long totalStateTime = 0;
5030 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5031 long time = u.getProcessStateTime(ips, rawRealtime, which);
5032 if (time > 0) {
5033 totalStateTime += time;
5034 sb.setLength(0);
5035 sb.append(prefix);
5036 sb.append(" ");
5037 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5038 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005039 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005040 pw.println(sb.toString());
5041 uidActivity = true;
5042 }
5043 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005044 if (totalStateTime > 0) {
5045 sb.setLength(0);
5046 sb.append(prefix);
5047 sb.append(" Total running: ");
5048 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5049 pw.println(sb.toString());
5050 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005051
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005052 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5053 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005054 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005055 sb.setLength(0);
5056 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005057 sb.append(" Total cpu time: u=");
5058 formatTimeMs(sb, userCpuTimeUs / 1000);
5059 sb.append("s=");
5060 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005061 pw.println(sb.toString());
5062 }
5063
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005064 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5065 if (cpuFreqTimes != null) {
5066 sb.setLength(0);
5067 sb.append(" Total cpu time per freq:");
5068 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5069 sb.append(" " + cpuFreqTimes[i]);
5070 }
5071 pw.println(sb.toString());
5072 }
5073 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5074 if (screenOffCpuFreqTimes != null) {
5075 sb.setLength(0);
5076 sb.append(" Total screen-off cpu time per freq:");
5077 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5078 sb.append(" " + screenOffCpuFreqTimes[i]);
5079 }
5080 pw.println(sb.toString());
5081 }
5082
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005083 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5084 = u.getProcessStats();
5085 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5086 final Uid.Proc ps = processStats.valueAt(ipr);
5087 long userTime;
5088 long systemTime;
5089 long foregroundTime;
5090 int starts;
5091 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005092
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005093 userTime = ps.getUserTime(which);
5094 systemTime = ps.getSystemTime(which);
5095 foregroundTime = ps.getForegroundTime(which);
5096 starts = ps.getStarts(which);
5097 final int numCrashes = ps.getNumCrashes(which);
5098 final int numAnrs = ps.getNumAnrs(which);
5099 numExcessive = which == STATS_SINCE_CHARGED
5100 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005101
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005102 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5103 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5104 sb.setLength(0);
5105 sb.append(prefix); sb.append(" Proc ");
5106 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5107 sb.append(prefix); sb.append(" CPU: ");
5108 formatTimeMs(sb, userTime); sb.append("usr + ");
5109 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5110 formatTimeMs(sb, foregroundTime); sb.append("fg");
5111 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5112 sb.append("\n"); sb.append(prefix); sb.append(" ");
5113 boolean hasOne = false;
5114 if (starts != 0) {
5115 hasOne = true;
5116 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005117 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005118 if (numCrashes != 0) {
5119 if (hasOne) {
5120 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005121 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005122 hasOne = true;
5123 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005124 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005125 if (numAnrs != 0) {
5126 if (hasOne) {
5127 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005128 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005129 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005130 }
5131 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005132 pw.println(sb.toString());
5133 for (int e=0; e<numExcessive; e++) {
5134 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5135 if (ew != null) {
5136 pw.print(prefix); pw.print(" * Killed for ");
5137 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
5138 pw.print("wake lock");
5139 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
5140 pw.print("cpu");
5141 } else {
5142 pw.print("unknown");
5143 }
5144 pw.print(" use: ");
5145 TimeUtils.formatDuration(ew.usedTime, pw);
5146 pw.print(" over ");
5147 TimeUtils.formatDuration(ew.overTime, pw);
5148 if (ew.overTime != 0) {
5149 pw.print(" (");
5150 pw.print((ew.usedTime*100)/ew.overTime);
5151 pw.println("%)");
5152 }
5153 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005154 }
5155 uidActivity = true;
5156 }
5157 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005158
5159 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5160 = u.getPackageStats();
5161 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
5162 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
5163 pw.println(":");
5164 boolean apkActivity = false;
5165 final Uid.Pkg ps = packageStats.valueAt(ipkg);
5166 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
5167 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
5168 pw.print(prefix); pw.print(" Wakeup alarm ");
5169 pw.print(alarms.keyAt(iwa)); pw.print(": ");
5170 pw.print(alarms.valueAt(iwa).getCountLocked(which));
5171 pw.println(" times");
5172 apkActivity = true;
5173 }
5174 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
5175 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
5176 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
5177 final long startTime = ss.getStartTime(batteryUptime, which);
5178 final int starts = ss.getStarts(which);
5179 final int launches = ss.getLaunches(which);
5180 if (startTime != 0 || starts != 0 || launches != 0) {
5181 sb.setLength(0);
5182 sb.append(prefix); sb.append(" Service ");
5183 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
5184 sb.append(prefix); sb.append(" Created for: ");
5185 formatTimeMs(sb, startTime / 1000);
5186 sb.append("uptime\n");
5187 sb.append(prefix); sb.append(" Starts: ");
5188 sb.append(starts);
5189 sb.append(", launches: "); sb.append(launches);
5190 pw.println(sb.toString());
5191 apkActivity = true;
5192 }
5193 }
5194 if (!apkActivity) {
5195 pw.print(prefix); pw.println(" (nothing executed)");
5196 }
5197 uidActivity = true;
5198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005199 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005200 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005201 }
5202 }
5203 }
5204
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005205 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005206 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005207 int diff = oldval ^ newval;
5208 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005209 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005210 for (int i=0; i<descriptions.length; i++) {
5211 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005212 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005213 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005214 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005215 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005216 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005217 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
5218 didWake = true;
5219 pw.print("=");
5220 if (longNames) {
5221 UserHandle.formatUid(pw, wakelockTag.uid);
5222 pw.print(":\"");
5223 pw.print(wakelockTag.string);
5224 pw.print("\"");
5225 } else {
5226 pw.print(wakelockTag.poolIdx);
5227 }
5228 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005229 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005230 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005231 pw.print("=");
5232 int val = (newval&bd.mask)>>bd.shift;
5233 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005234 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005235 } else {
5236 pw.print(val);
5237 }
5238 }
5239 }
5240 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005241 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07005242 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005243 if (longNames) {
5244 UserHandle.formatUid(pw, wakelockTag.uid);
5245 pw.print(":\"");
5246 pw.print(wakelockTag.string);
5247 pw.print("\"");
5248 } else {
5249 pw.print(wakelockTag.poolIdx);
5250 }
5251 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005252 }
5253
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005254 public void prepareForDumpLocked() {
5255 }
5256
5257 public static class HistoryPrinter {
5258 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005259 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005260 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005261 int oldStatus = -1;
5262 int oldHealth = -1;
5263 int oldPlug = -1;
5264 int oldTemp = -1;
5265 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005266 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005267 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005268
Dianne Hackborn3251b902014-06-20 14:40:53 -07005269 void reset() {
5270 oldState = oldState2 = 0;
5271 oldLevel = -1;
5272 oldStatus = -1;
5273 oldHealth = -1;
5274 oldPlug = -1;
5275 oldTemp = -1;
5276 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005277 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07005278 }
5279
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005280 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005281 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005282 if (!checkin) {
5283 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005284 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005285 pw.print(" (");
5286 pw.print(rec.numReadInts);
5287 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005288 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005289 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5290 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005291 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005292 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005293 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005294 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005295 }
5296 lastTime = rec.time;
5297 }
5298 if (rec.cmd == HistoryItem.CMD_START) {
5299 if (checkin) {
5300 pw.print(":");
5301 }
5302 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005303 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005304 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
5305 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005306 if (checkin) {
5307 pw.print(":");
5308 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005309 if (rec.cmd == HistoryItem.CMD_RESET) {
5310 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005311 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005312 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005313 pw.print("TIME:");
5314 if (checkin) {
5315 pw.println(rec.currentTime);
5316 } else {
5317 pw.print(" ");
5318 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5319 rec.currentTime).toString());
5320 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005321 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
5322 if (checkin) {
5323 pw.print(":");
5324 }
5325 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005326 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
5327 if (checkin) {
5328 pw.print(":");
5329 }
5330 pw.println("*OVERFLOW*");
5331 } else {
5332 if (!checkin) {
5333 if (rec.batteryLevel < 10) pw.print("00");
5334 else if (rec.batteryLevel < 100) pw.print("0");
5335 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005336 if (verbose) {
5337 pw.print(" ");
5338 if (rec.states < 0) ;
5339 else if (rec.states < 0x10) pw.print("0000000");
5340 else if (rec.states < 0x100) pw.print("000000");
5341 else if (rec.states < 0x1000) pw.print("00000");
5342 else if (rec.states < 0x10000) pw.print("0000");
5343 else if (rec.states < 0x100000) pw.print("000");
5344 else if (rec.states < 0x1000000) pw.print("00");
5345 else if (rec.states < 0x10000000) pw.print("0");
5346 pw.print(Integer.toHexString(rec.states));
5347 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005348 } else {
5349 if (oldLevel != rec.batteryLevel) {
5350 oldLevel = rec.batteryLevel;
5351 pw.print(",Bl="); pw.print(rec.batteryLevel);
5352 }
5353 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005354 if (oldStatus != rec.batteryStatus) {
5355 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005356 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005357 switch (oldStatus) {
5358 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005359 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005360 break;
5361 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005362 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005363 break;
5364 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005365 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005366 break;
5367 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005368 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005369 break;
5370 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005371 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005372 break;
5373 default:
5374 pw.print(oldStatus);
5375 break;
5376 }
5377 }
5378 if (oldHealth != rec.batteryHealth) {
5379 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005380 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005381 switch (oldHealth) {
5382 case BatteryManager.BATTERY_HEALTH_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_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005386 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005387 break;
5388 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005389 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005390 break;
5391 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005392 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005393 break;
5394 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005395 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005396 break;
5397 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005398 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005399 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005400 case BatteryManager.BATTERY_HEALTH_COLD:
5401 pw.print(checkin ? "c" : "cold");
5402 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005403 default:
5404 pw.print(oldHealth);
5405 break;
5406 }
5407 }
5408 if (oldPlug != rec.batteryPlugType) {
5409 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005410 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005411 switch (oldPlug) {
5412 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005413 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005414 break;
5415 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005416 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005417 break;
5418 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005419 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005420 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005421 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005422 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005423 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005424 default:
5425 pw.print(oldPlug);
5426 break;
5427 }
5428 }
5429 if (oldTemp != rec.batteryTemperature) {
5430 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005431 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005432 pw.print(oldTemp);
5433 }
5434 if (oldVolt != rec.batteryVoltage) {
5435 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005436 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005437 pw.print(oldVolt);
5438 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005439 final int chargeMAh = rec.batteryChargeUAh / 1000;
5440 if (oldChargeMAh != chargeMAh) {
5441 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07005442 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005443 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07005444 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005445 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005446 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005447 printBitDescriptions(pw, oldState2, rec.states2, null,
5448 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005449 if (rec.wakeReasonTag != null) {
5450 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005451 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005452 pw.print(rec.wakeReasonTag.poolIdx);
5453 } else {
5454 pw.print(" wake_reason=");
5455 pw.print(rec.wakeReasonTag.uid);
5456 pw.print(":\"");
5457 pw.print(rec.wakeReasonTag.string);
5458 pw.print("\"");
5459 }
5460 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08005461 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005462 pw.print(checkin ? "," : " ");
5463 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
5464 pw.print("+");
5465 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
5466 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005467 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005468 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
5469 : HISTORY_EVENT_NAMES;
5470 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
5471 | HistoryItem.EVENT_FLAG_FINISH);
5472 if (idx >= 0 && idx < eventNames.length) {
5473 pw.print(eventNames[idx]);
5474 } else {
5475 pw.print(checkin ? "Ev" : "event");
5476 pw.print(idx);
5477 }
5478 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005479 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005480 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08005481 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08005482 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
5483 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005484 pw.print(":\"");
5485 pw.print(rec.eventTag.string);
5486 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005487 }
5488 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005489 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005490 if (rec.stepDetails != null) {
5491 if (!checkin) {
5492 pw.print(" Details: cpu=");
5493 pw.print(rec.stepDetails.userTime);
5494 pw.print("u+");
5495 pw.print(rec.stepDetails.systemTime);
5496 pw.print("s");
5497 if (rec.stepDetails.appCpuUid1 >= 0) {
5498 pw.print(" (");
5499 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
5500 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5501 if (rec.stepDetails.appCpuUid2 >= 0) {
5502 pw.print(", ");
5503 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
5504 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5505 }
5506 if (rec.stepDetails.appCpuUid3 >= 0) {
5507 pw.print(", ");
5508 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
5509 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5510 }
5511 pw.print(')');
5512 }
5513 pw.println();
5514 pw.print(" /proc/stat=");
5515 pw.print(rec.stepDetails.statUserTime);
5516 pw.print(" usr, ");
5517 pw.print(rec.stepDetails.statSystemTime);
5518 pw.print(" sys, ");
5519 pw.print(rec.stepDetails.statIOWaitTime);
5520 pw.print(" io, ");
5521 pw.print(rec.stepDetails.statIrqTime);
5522 pw.print(" irq, ");
5523 pw.print(rec.stepDetails.statSoftIrqTime);
5524 pw.print(" sirq, ");
5525 pw.print(rec.stepDetails.statIdlTime);
5526 pw.print(" idle");
5527 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
5528 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
5529 + rec.stepDetails.statSoftIrqTime;
5530 int total = totalRun + rec.stepDetails.statIdlTime;
5531 if (total > 0) {
5532 pw.print(" (");
5533 float perc = ((float)totalRun) / ((float)total) * 100;
5534 pw.print(String.format("%.1f%%", perc));
5535 pw.print(" of ");
5536 StringBuilder sb = new StringBuilder(64);
5537 formatTimeMsNoSpace(sb, total*10);
5538 pw.print(sb);
5539 pw.print(")");
5540 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005541 pw.print(", PlatformIdleStat ");
5542 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005543 pw.println();
5544 } else {
5545 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5546 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
5547 pw.print(rec.stepDetails.userTime);
5548 pw.print(":");
5549 pw.print(rec.stepDetails.systemTime);
5550 if (rec.stepDetails.appCpuUid1 >= 0) {
5551 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
5552 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5553 if (rec.stepDetails.appCpuUid2 >= 0) {
5554 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
5555 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5556 }
5557 if (rec.stepDetails.appCpuUid3 >= 0) {
5558 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
5559 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5560 }
5561 }
5562 pw.println();
5563 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5564 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
5565 pw.print(rec.stepDetails.statUserTime);
5566 pw.print(',');
5567 pw.print(rec.stepDetails.statSystemTime);
5568 pw.print(',');
5569 pw.print(rec.stepDetails.statIOWaitTime);
5570 pw.print(',');
5571 pw.print(rec.stepDetails.statIrqTime);
5572 pw.print(',');
5573 pw.print(rec.stepDetails.statSoftIrqTime);
5574 pw.print(',');
5575 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005576 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005577 if (rec.stepDetails.statPlatformIdleState != null) {
5578 pw.print(rec.stepDetails.statPlatformIdleState);
5579 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005580 pw.println();
5581 }
5582 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005583 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005584 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005585 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005586 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005587
5588 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
5589 UserHandle.formatUid(pw, uid);
5590 pw.print("=");
5591 pw.print(utime);
5592 pw.print("u+");
5593 pw.print(stime);
5594 pw.print("s");
5595 }
5596
5597 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
5598 pw.print('/');
5599 pw.print(uid);
5600 pw.print(":");
5601 pw.print(utime);
5602 pw.print(":");
5603 pw.print(stime);
5604 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005605 }
5606
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005607 private void printSizeValue(PrintWriter pw, long size) {
5608 float result = size;
5609 String suffix = "";
5610 if (result >= 10*1024) {
5611 suffix = "KB";
5612 result = result / 1024;
5613 }
5614 if (result >= 10*1024) {
5615 suffix = "MB";
5616 result = result / 1024;
5617 }
5618 if (result >= 10*1024) {
5619 suffix = "GB";
5620 result = result / 1024;
5621 }
5622 if (result >= 10*1024) {
5623 suffix = "TB";
5624 result = result / 1024;
5625 }
5626 if (result >= 10*1024) {
5627 suffix = "PB";
5628 result = result / 1024;
5629 }
5630 pw.print((int)result);
5631 pw.print(suffix);
5632 }
5633
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005634 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
5635 String label3, long estimatedTime) {
5636 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005637 return false;
5638 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005639 pw.print(label1);
5640 pw.print(label2);
5641 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005642 StringBuilder sb = new StringBuilder(64);
5643 formatTimeMs(sb, estimatedTime);
5644 pw.print(sb);
5645 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005646 return true;
5647 }
5648
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005649 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
5650 LevelStepTracker steps, boolean checkin) {
5651 if (steps == null) {
5652 return false;
5653 }
5654 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005655 if (count <= 0) {
5656 return false;
5657 }
5658 if (!checkin) {
5659 pw.println(header);
5660 }
Kweku Adams030980a2015-04-01 16:07:48 -07005661 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005662 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005663 long duration = steps.getDurationAt(i);
5664 int level = steps.getLevelAt(i);
5665 long initMode = steps.getInitModeAt(i);
5666 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005667 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005668 lineArgs[0] = Long.toString(duration);
5669 lineArgs[1] = Integer.toString(level);
5670 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5671 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5672 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
5673 case Display.STATE_ON: lineArgs[2] = "s+"; break;
5674 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
5675 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07005676 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005677 }
5678 } else {
5679 lineArgs[2] = "";
5680 }
5681 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5682 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
5683 } else {
5684 lineArgs[3] = "";
5685 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005686 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07005687 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005688 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07005689 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005690 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005691 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
5692 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005693 pw.print(prefix);
5694 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005695 TimeUtils.formatDuration(duration, pw);
5696 pw.print(" to "); pw.print(level);
5697 boolean haveModes = false;
5698 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5699 pw.print(" (");
5700 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5701 case Display.STATE_OFF: pw.print("screen-off"); break;
5702 case Display.STATE_ON: pw.print("screen-on"); break;
5703 case Display.STATE_DOZE: pw.print("screen-doze"); break;
5704 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07005705 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005706 }
5707 haveModes = true;
5708 }
5709 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5710 pw.print(haveModes ? ", " : " (");
5711 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
5712 ? "power-save-on" : "power-save-off");
5713 haveModes = true;
5714 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005715 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
5716 pw.print(haveModes ? ", " : " (");
5717 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
5718 ? "device-idle-on" : "device-idle-off");
5719 haveModes = true;
5720 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005721 if (haveModes) {
5722 pw.print(")");
5723 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005724 pw.println();
5725 }
5726 }
5727 return true;
5728 }
5729
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005730 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005731 public static final int DUMP_DAILY_ONLY = 1<<2;
5732 public static final int DUMP_HISTORY_ONLY = 1<<3;
5733 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
5734 public static final int DUMP_VERBOSE = 1<<5;
5735 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005736
Dianne Hackborn37de0982014-05-09 09:32:18 -07005737 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
5738 final HistoryPrinter hprinter = new HistoryPrinter();
5739 final HistoryItem rec = new HistoryItem();
5740 long lastTime = -1;
5741 long baseTime = -1;
5742 boolean printed = false;
5743 HistoryEventTracker tracker = null;
5744 while (getNextHistoryLocked(rec)) {
5745 lastTime = rec.time;
5746 if (baseTime < 0) {
5747 baseTime = lastTime;
5748 }
5749 if (rec.time >= histStart) {
5750 if (histStart >= 0 && !printed) {
5751 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07005752 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005753 || rec.cmd == HistoryItem.CMD_START
5754 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005755 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005756 hprinter.printNextItem(pw, rec, baseTime, checkin,
5757 (flags&DUMP_VERBOSE) != 0);
5758 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005759 } else if (rec.currentTime != 0) {
5760 printed = true;
5761 byte cmd = rec.cmd;
5762 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005763 hprinter.printNextItem(pw, rec, baseTime, checkin,
5764 (flags&DUMP_VERBOSE) != 0);
5765 rec.cmd = cmd;
5766 }
5767 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005768 if (rec.cmd != HistoryItem.CMD_UPDATE) {
5769 hprinter.printNextItem(pw, rec, baseTime, checkin,
5770 (flags&DUMP_VERBOSE) != 0);
5771 rec.cmd = HistoryItem.CMD_UPDATE;
5772 }
5773 int oldEventCode = rec.eventCode;
5774 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005775 rec.eventTag = new HistoryTag();
5776 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
5777 HashMap<String, SparseIntArray> active
5778 = tracker.getStateForEvent(i);
5779 if (active == null) {
5780 continue;
5781 }
5782 for (HashMap.Entry<String, SparseIntArray> ent
5783 : active.entrySet()) {
5784 SparseIntArray uids = ent.getValue();
5785 for (int j=0; j<uids.size(); j++) {
5786 rec.eventCode = i;
5787 rec.eventTag.string = ent.getKey();
5788 rec.eventTag.uid = uids.keyAt(j);
5789 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07005790 hprinter.printNextItem(pw, rec, baseTime, checkin,
5791 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005792 rec.wakeReasonTag = null;
5793 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005794 }
5795 }
5796 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005797 rec.eventCode = oldEventCode;
5798 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005799 tracker = null;
5800 }
5801 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005802 hprinter.printNextItem(pw, rec, baseTime, checkin,
5803 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07005804 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
5805 // This is an attempt to aggregate the previous state and generate
5806 // fake events to reflect that state at the point where we start
5807 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07005808 if (tracker == null) {
5809 tracker = new HistoryEventTracker();
5810 }
5811 tracker.updateState(rec.eventCode, rec.eventTag.string,
5812 rec.eventTag.uid, rec.eventTag.poolIdx);
5813 }
5814 }
5815 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07005816 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005817 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
5818 }
5819 }
5820
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005821 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
5822 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
5823 if (steps == null) {
5824 return;
5825 }
5826 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
5827 if (timeRemaining >= 0) {
5828 pw.print(prefix); pw.print(label); pw.print(" total time: ");
5829 tmpSb.setLength(0);
5830 formatTimeMs(tmpSb, timeRemaining);
5831 pw.print(tmpSb);
5832 pw.print(" (from "); pw.print(tmpOutInt[0]);
5833 pw.println(" steps)");
5834 }
5835 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5836 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5837 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
5838 if (estimatedTime > 0) {
5839 pw.print(prefix); pw.print(label); pw.print(" ");
5840 pw.print(STEP_LEVEL_MODE_LABELS[i]);
5841 pw.print(" time: ");
5842 tmpSb.setLength(0);
5843 formatTimeMs(tmpSb, estimatedTime);
5844 pw.print(tmpSb);
5845 pw.print(" (from "); pw.print(tmpOutInt[0]);
5846 pw.println(" steps)");
5847 }
5848 }
5849 }
5850
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005851 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
5852 ArrayList<PackageChange> changes) {
5853 if (changes == null) {
5854 return;
5855 }
5856 pw.print(prefix); pw.println("Package changes:");
5857 for (int i=0; i<changes.size(); i++) {
5858 PackageChange pc = changes.get(i);
5859 if (pc.mUpdate) {
5860 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
5861 pw.print(" vers="); pw.println(pc.mVersionCode);
5862 } else {
5863 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
5864 }
5865 }
5866 }
5867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005868 /**
5869 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
5870 *
5871 * @param pw a Printer to receive the dump output.
5872 */
5873 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005874 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005875 prepareForDumpLocked();
5876
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005877 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005878 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005879
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005880 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005881 final long historyTotalSize = getHistoryTotalSize();
5882 final long historyUsedSize = getHistoryUsedSize();
5883 if (startIteratingHistoryLocked()) {
5884 try {
5885 pw.print("Battery History (");
5886 pw.print((100*historyUsedSize)/historyTotalSize);
5887 pw.print("% used, ");
5888 printSizeValue(pw, historyUsedSize);
5889 pw.print(" used of ");
5890 printSizeValue(pw, historyTotalSize);
5891 pw.print(", ");
5892 pw.print(getHistoryStringPoolSize());
5893 pw.print(" strings using ");
5894 printSizeValue(pw, getHistoryStringPoolBytes());
5895 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07005896 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005897 pw.println();
5898 } finally {
5899 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005900 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005901 }
5902
5903 if (startIteratingOldHistoryLocked()) {
5904 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005905 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005906 pw.println("Old battery History:");
5907 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005908 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005909 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005910 if (baseTime < 0) {
5911 baseTime = rec.time;
5912 }
5913 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005914 }
5915 pw.println();
5916 } finally {
5917 finishIteratingOldHistoryLocked();
5918 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005919 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005920 }
5921
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005922 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005923 return;
5924 }
5925
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005926 if (!filtering) {
5927 SparseArray<? extends Uid> uidStats = getUidStats();
5928 final int NU = uidStats.size();
5929 boolean didPid = false;
5930 long nowRealtime = SystemClock.elapsedRealtime();
5931 for (int i=0; i<NU; i++) {
5932 Uid uid = uidStats.valueAt(i);
5933 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
5934 if (pids != null) {
5935 for (int j=0; j<pids.size(); j++) {
5936 Uid.Pid pid = pids.valueAt(j);
5937 if (!didPid) {
5938 pw.println("Per-PID Stats:");
5939 didPid = true;
5940 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005941 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
5942 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005943 pw.print(" PID "); pw.print(pids.keyAt(j));
5944 pw.print(" wake time: ");
5945 TimeUtils.formatDuration(time, pw);
5946 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005947 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005948 }
5949 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005950 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005951 pw.println();
5952 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005953 }
5954
5955 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005956 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
5957 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005958 long timeRemaining = computeBatteryTimeRemaining(
5959 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005960 if (timeRemaining >= 0) {
5961 pw.print(" Estimated discharge time remaining: ");
5962 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5963 pw.println();
5964 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005965 final LevelStepTracker steps = getDischargeLevelStepTracker();
5966 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5967 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
5968 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5969 STEP_LEVEL_MODE_VALUES[i], null));
5970 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005971 pw.println();
5972 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005973 if (dumpDurationSteps(pw, " ", "Charge step durations:",
5974 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005975 long timeRemaining = computeChargeTimeRemaining(
5976 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005977 if (timeRemaining >= 0) {
5978 pw.print(" Estimated charge time remaining: ");
5979 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5980 pw.println();
5981 }
5982 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005983 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005984 }
5985 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
5986 pw.println("Daily stats:");
5987 pw.print(" Current start time: ");
5988 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5989 getCurrentDailyStartTime()).toString());
5990 pw.print(" Next min deadline: ");
5991 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5992 getNextMinDailyDeadline()).toString());
5993 pw.print(" Next max deadline: ");
5994 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5995 getNextMaxDailyDeadline()).toString());
5996 StringBuilder sb = new StringBuilder(64);
5997 int[] outInt = new int[1];
5998 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
5999 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006000 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
6001 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006002 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006003 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
6004 dsteps, false)) {
6005 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6006 sb, outInt);
6007 }
6008 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6009 csteps, false)) {
6010 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6011 sb, outInt);
6012 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006013 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006014 } else {
6015 pw.println(" Current daily steps:");
6016 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6017 sb, outInt);
6018 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6019 sb, outInt);
6020 }
6021 }
6022 DailyItem dit;
6023 int curIndex = 0;
6024 while ((dit=getDailyItemLocked(curIndex)) != null) {
6025 curIndex++;
6026 if ((flags&DUMP_DAILY_ONLY) != 0) {
6027 pw.println();
6028 }
6029 pw.print(" Daily from ");
6030 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6031 pw.print(" to ");
6032 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6033 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006034 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006035 if (dumpDurationSteps(pw, " ",
6036 " Discharge step durations:", dit.mDischargeSteps, false)) {
6037 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6038 sb, outInt);
6039 }
6040 if (dumpDurationSteps(pw, " ",
6041 " Charge step durations:", dit.mChargeSteps, false)) {
6042 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6043 sb, outInt);
6044 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006045 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006046 } else {
6047 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6048 sb, outInt);
6049 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6050 sb, outInt);
6051 }
6052 }
6053 pw.println();
6054 }
6055 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006056 pw.println("Statistics since last charge:");
6057 pw.println(" System starts: " + getStartCount()
6058 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006059 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6060 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006061 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006063 }
6064
6065 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006066 public void dumpCheckinLocked(Context context, PrintWriter pw,
6067 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006068 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006069
6070 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006071 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6072 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006073
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006074 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6075
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006076 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006077 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006078
6079 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07006080 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006081 try {
6082 for (int i=0; i<getHistoryStringPoolSize(); i++) {
6083 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6084 pw.print(HISTORY_STRING_POOL); pw.print(',');
6085 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006086 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006087 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006088 pw.print(",\"");
6089 String str = getHistoryTagPoolString(i);
6090 str = str.replace("\\", "\\\\");
6091 str = str.replace("\"", "\\\"");
6092 pw.print(str);
6093 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006094 pw.println();
6095 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006096 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006097 } finally {
6098 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08006099 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006100 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006101 }
6102
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006103 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006104 return;
6105 }
6106
Dianne Hackborne4a59512010-12-07 11:08:07 -08006107 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006108 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08006109 for (int i=0; i<apps.size(); i++) {
6110 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006111 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
6112 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08006113 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006114 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
6115 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006116 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006117 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006118 }
6119 SparseArray<? extends Uid> uidStats = getUidStats();
6120 final int NU = uidStats.size();
6121 String[] lineArgs = new String[2];
6122 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006123 int uid = UserHandle.getAppId(uidStats.keyAt(i));
6124 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
6125 if (pkgs != null && !pkgs.second.value) {
6126 pkgs.second.value = true;
6127 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08006128 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006129 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006130 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
6131 (Object[])lineArgs);
6132 }
6133 }
6134 }
6135 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006136 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006137 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006138 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07006139 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006140 if (timeRemaining >= 0) {
6141 lineArgs[0] = Long.toString(timeRemaining);
6142 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
6143 (Object[])lineArgs);
6144 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006145 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07006146 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006147 if (timeRemaining >= 0) {
6148 lineArgs[0] = Long.toString(timeRemaining);
6149 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
6150 (Object[])lineArgs);
6151 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006152 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
6153 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006154 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006156}