blob: 66b6b47887b9377b8c23411b3b424fb74cda74c4 [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
Dianne Hackborn94326cb2017-06-28 16:17:20 -070019import android.app.job.JobParameters;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080020import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080021import android.content.pm.ApplicationInfo;
Wink Saville52840902011-02-18 12:40:47 -080022import android.telephony.SignalStrength;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080023import android.text.format.DateFormat;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070024import android.util.ArrayMap;
James Carr2dd7e5e2016-07-20 18:48:39 -070025import android.util.LongSparseArray;
Dianne Hackborn9cfba352016-03-24 17:31:28 -070026import android.util.MutableBoolean;
27import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.util.Printer;
29import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070030import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070031import android.util.TimeUtils;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070032import android.view.Display;
Amith Yamasaniab9ad192016-12-06 12:46:59 -080033
Dianne Hackborna7c837f2014-01-15 16:20:44 -080034import com.android.internal.os.BatterySipper;
35import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070037import java.io.PrintWriter;
38import java.util.ArrayList;
39import java.util.Collections;
40import java.util.Comparator;
41import java.util.Formatter;
42import java.util.HashMap;
43import java.util.List;
44import java.util.Map;
45
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046/**
47 * A class providing access to battery usage statistics, including information on
48 * wakelocks, processes, packages, and services. All times are represented in microseconds
49 * except where indicated otherwise.
50 * @hide
51 */
52public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070053 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
55 private static final boolean LOCAL_LOGV = false;
Bookatz82b341172017-09-07 19:06:08 -070056 /** Fetching RPM stats is too slow to do each time screen changes, so disable it. */
57 protected static final boolean SCREEN_OFF_RPM_STATS_ENABLED = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070058
59 /** @hide */
60 public static final String SERVICE_NAME = "batterystats";
61
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 /**
63 * A constant indicating a partial wake lock timer.
64 */
65 public static final int WAKE_TYPE_PARTIAL = 0;
66
67 /**
68 * A constant indicating a full wake lock timer.
69 */
70 public static final int WAKE_TYPE_FULL = 1;
71
72 /**
73 * A constant indicating a window wake lock timer.
74 */
75 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 /**
78 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 */
80 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070081
82 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070083 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070084 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070085 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070086
87 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070088 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070089 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070090 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070091
92 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070093 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070094 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070095 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
Dianne Hackborn62793e42015-03-09 11:15:41 -070097 /**
98 * A constant indicating a wifi multicast timer
99 */
100 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700103 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700104 */
105 public static final int VIDEO_TURNED_ON = 8;
106
107 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800108 * A constant indicating a vibrator on timer
109 */
110 public static final int VIBRATOR_ON = 9;
111
112 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700113 * A constant indicating a foreground activity timer
114 */
115 public static final int FOREGROUND_ACTIVITY = 10;
116
117 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700118 * A constant indicating a wifi batched scan is active
119 */
120 public static final int WIFI_BATCHED_SCAN = 11;
121
122 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700123 * A constant indicating a process state timer
124 */
125 public static final int PROCESS_STATE = 12;
126
127 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700128 * A constant indicating a sync timer
129 */
130 public static final int SYNC = 13;
131
132 /**
133 * A constant indicating a job timer
134 */
135 public static final int JOB = 14;
136
137 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800138 * A constant indicating an audio turn on timer
139 */
140 public static final int AUDIO_TURNED_ON = 15;
141
142 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700143 * A constant indicating a flashlight turn on timer
144 */
145 public static final int FLASHLIGHT_TURNED_ON = 16;
146
147 /**
148 * A constant indicating a camera turn on timer
149 */
150 public static final int CAMERA_TURNED_ON = 17;
151
152 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700153 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700154 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700155 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700156
157 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800158 * A constant indicating a bluetooth scan timer.
159 */
160 public static final int BLUETOOTH_SCAN_ON = 19;
161
162 /**
Bookatzc8c44962017-05-11 12:12:54 -0700163 * A constant indicating an aggregated partial wake lock timer.
164 */
165 public static final int AGGREGATED_WAKE_TYPE_PARTIAL = 20;
166
167 /**
Bookatzb1f04f32017-05-19 13:57:32 -0700168 * A constant indicating a bluetooth scan timer for unoptimized scans.
169 */
170 public static final int BLUETOOTH_UNOPTIMIZED_SCAN_ON = 21;
171
172 /**
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700173 * A constant indicating a foreground service timer
174 */
175 public static final int FOREGROUND_SERVICE = 22;
176
177 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 * Include all of the data in the stats, including previously saved data.
179 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700180 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181
182 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 * Include only the current run in the stats.
184 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700185 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186
187 /**
188 * Include only the run since the last time the device was unplugged in the stats.
189 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700190 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700191
192 // NOTE: Update this list if you add/change any stats above.
193 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700194 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700195 private static final String[] STAT_NAMES = { "l", "c", "u" };
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700198 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700199 *
200 * New in version 19:
201 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800202 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700203 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Bookatz506a8182017-05-01 14:18:42 -0700204 * New in version 21:
205 * - Actual (not just apportioned) Wakelock time is also recorded.
Bookatzc8c44962017-05-11 12:12:54 -0700206 * - Aggregated partial wakelock time (per uid, instead of per wakelock) is recorded.
Bookatzb1f04f32017-05-19 13:57:32 -0700207 * - BLE scan result count
208 * - CPU frequency time per uid
209 * New in version 22:
210 * - BLE scan result background count, BLE unoptimized scan time
Bookatz98d4d5c2017-08-01 19:07:54 -0700211 * - Background partial wakelock time & count
212 * New in version 23:
213 * - Logging smeared power model values
214 * New in version 24:
215 * - Fixed bugs in background timers and BLE scan time
216 * New in version 25:
217 * - Package wakeup alarms are now on screen-off timebase
Bookatz50df7112017-08-04 14:53:26 -0700218 * New in version 26:
Bookatz82b341172017-09-07 19:06:08 -0700219 * - Resource power manager (rpm) states [but screenOffRpm is disabled from working properly]
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700220 */
Bookatz50df7112017-08-04 14:53:26 -0700221 static final String CHECKIN_VERSION = "26";
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700222
223 /**
224 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700226 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700227
Evan Millar22ac0432009-03-31 11:33:18 -0700228 private static final long BYTES_PER_KB = 1024;
229 private static final long BYTES_PER_MB = 1048576; // 1024^2
230 private static final long BYTES_PER_GB = 1073741824; //1024^3
Bookatz506a8182017-05-01 14:18:42 -0700231
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700232 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800233 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700234 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700236 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700237 private static final String CPU_DATA = "cpu";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700238 private static final String GLOBAL_CPU_FREQ_DATA = "gcf";
239 private static final String CPU_TIMES_AT_FREQ_DATA = "ctf";
Bookatz50df7112017-08-04 14:53:26 -0700240 // rpm line is:
241 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "rpm", state/voter name, total time, total count,
242 // screen-off time, screen-off count
243 private static final String RESOURCE_POWER_MANAGER_DATA = "rpm";
Evan Millare84de8d2009-04-02 22:16:12 -0700244 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800245 private static final String VIBRATOR_DATA = "vib";
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700246 private static final String FOREGROUND_ACTIVITY_DATA = "fg";
247 // fgs line is:
248 // BATTERY_STATS_CHECKIN_VERSION, uid, category, "fgs",
249 // foreground service time, count
250 private static final String FOREGROUND_SERVICE_DATA = "fgs";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700251 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700252 // wl line is:
253 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
Bookatz5b5ec322017-05-26 09:40:38 -0700254 // full totalTime, 'f', count, current duration, max duration, total duration,
255 // partial totalTime, 'p', count, current duration, max duration, total duration,
256 // bg partial totalTime, 'bp', count, current duration, max duration, total duration,
257 // window totalTime, 'w', count, current duration, max duration, total duration
Bookatz506a8182017-05-01 14:18:42 -0700258 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700259 private static final String WAKELOCK_DATA = "wl";
Bookatzc8c44962017-05-11 12:12:54 -0700260 // awl line is:
261 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "awl",
262 // cumulative partial wakelock duration, cumulative background partial wakelock duration
263 private static final String AGGREGATED_WAKELOCK_DATA = "awl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700264 private static final String SYNC_DATA = "sy";
265 private static final String JOB_DATA = "jb";
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700266 private static final String JOB_COMPLETION_DATA = "jbc";
Evan Millarc64edde2009-04-18 12:26:32 -0700267 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700268 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700269 private static final String NETWORK_DATA = "nt";
270 private static final String USER_ACTIVITY_DATA = "ua";
271 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800272 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700273 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700274 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700275 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800276 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
277 private static final String WIFI_CONTROLLER_DATA = "wfcd";
278 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
279 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700280 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700281 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800282 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800283 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
284 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800285 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700286 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700287 private static final String SCREEN_BRIGHTNESS_DATA = "br";
288 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700289 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700290 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
291 private static final String DATA_CONNECTION_TIME_DATA = "dct";
292 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800293 private static final String WIFI_STATE_TIME_DATA = "wst";
294 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700295 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
296 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
297 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
298 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800299 private static final String POWER_USE_SUMMARY_DATA = "pws";
300 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700301 private static final String DISCHARGE_STEP_DATA = "dsd";
302 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700303 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
304 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700305 private static final String FLASHLIGHT_DATA = "fla";
306 private static final String CAMERA_DATA = "cam";
307 private static final String VIDEO_DATA = "vid";
308 private static final String AUDIO_DATA = "aud";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309
Adam Lesinski010bf372016-04-11 12:18:18 -0700310 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
311
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700312 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 private final Formatter mFormatter = new Formatter(mFormatBuilder);
314
315 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700316 * Indicates times spent by the uid at each cpu frequency in all process states.
317 *
318 * Other types might include times spent in foreground, background etc.
319 */
320 private final String UID_TIMES_TYPE_ALL = "A";
321
322 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700323 * State for keeping track of counting information.
324 */
325 public static abstract class Counter {
326
327 /**
328 * Returns the count associated with this Counter for the
329 * selected type of statistics.
330 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700331 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700332 */
Evan Millarc64edde2009-04-18 12:26:32 -0700333 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700334
335 /**
336 * Temporary for debugging.
337 */
338 public abstract void logState(Printer pw, String prefix);
339 }
340
341 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700342 * State for keeping track of long counting information.
343 */
344 public static abstract class LongCounter {
345
346 /**
347 * Returns the count associated with this Counter for the
348 * selected type of statistics.
349 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700350 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700351 */
352 public abstract long getCountLocked(int which);
353
354 /**
355 * Temporary for debugging.
356 */
357 public abstract void logState(Printer pw, String prefix);
358 }
359
360 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700361 * State for keeping track of array of long counting information.
362 */
363 public static abstract class LongCounterArray {
364 /**
365 * Returns the counts associated with this Counter for the
366 * selected type of statistics.
367 *
368 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
369 */
370 public abstract long[] getCountsLocked(int which);
371
372 /**
373 * Temporary for debugging.
374 */
375 public abstract void logState(Printer pw, String prefix);
376 }
377
378 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800379 * Container class that aggregates counters for transmit, receive, and idle state of a
380 * radio controller.
381 */
382 public static abstract class ControllerActivityCounter {
383 /**
384 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
385 * idle state.
386 */
387 public abstract LongCounter getIdleTimeCounter();
388
389 /**
390 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
391 * receive state.
392 */
393 public abstract LongCounter getRxTimeCounter();
394
395 /**
396 * An array of {@link LongCounter}, representing various transmit levels, where each level
397 * may draw a different amount of power. The levels themselves are controller-specific.
398 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
399 * various transmit level states.
400 */
401 public abstract LongCounter[] getTxTimeCounters();
402
403 /**
404 * @return a non-null {@link LongCounter} representing the power consumed by the controller
405 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
406 * yield a value of 0 if the device doesn't support power calculations.
407 */
408 public abstract LongCounter getPowerCounter();
409 }
410
411 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 * State for keeping track of timing information.
413 */
414 public static abstract class Timer {
415
416 /**
417 * Returns the count associated with this Timer for the
418 * selected type of statistics.
419 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700420 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 */
Evan Millarc64edde2009-04-18 12:26:32 -0700422 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423
424 /**
425 * Returns the total time in microseconds associated with this Timer for the
426 * selected type of statistics.
427 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800428 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700429 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 * @return a time in microseconds
431 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800432 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700435 * Returns the total time in microseconds associated with this Timer since the
436 * 'mark' was last set.
437 *
438 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
439 * @return a time in microseconds
440 */
441 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
442
443 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700444 * Returns the max duration if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800445 * Not all Timer subclasses track the max, total, current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700446
447 */
448 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
449 return -1;
450 }
451
452 /**
453 * Returns the current time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800454 * Not all Timer subclasses track the max, total, current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700455 */
456 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
457 return -1;
458 }
459
460 /**
Bookatz867c0d72017-03-07 18:23:42 -0800461 * Returns the current time the timer has been active, if it is being tracked.
462 *
463 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
464 * been on since reset.
465 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
466 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
467 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
468 * the actual total time.
469 * Not all Timer subclasses track the max, total, current durations.
470 */
471 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
472 return -1;
473 }
474
475 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700476 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
477 * used, for example, for tracking background usage. Secondary timers are never pooled.
478 *
479 * Not all Timer subclasses have a secondary timer; those that don't return null.
480 */
481 public Timer getSubTimer() {
482 return null;
483 }
484
485 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700486 * Returns whether the timer is currently running. Some types of timers
487 * (e.g. BatchTimers) don't know whether the event is currently active,
488 * and report false.
489 */
490 public boolean isRunningLocked() {
491 return false;
492 }
493
494 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 * Temporary for debugging.
496 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700497 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 }
499
500 /**
501 * The statistics associated with a particular uid.
502 */
503 public static abstract class Uid {
504
505 /**
506 * Returns a mapping containing wakelock statistics.
507 *
508 * @return a Map from Strings to Uid.Wakelock objects.
509 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700510 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511
512 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700513 * Returns a mapping containing sync statistics.
514 *
515 * @return a Map from Strings to Timer objects.
516 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700517 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700518
519 /**
520 * Returns a mapping containing scheduled job statistics.
521 *
522 * @return a Map from Strings to Timer objects.
523 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700524 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700525
526 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700527 * Returns statistics about how jobs have completed.
528 *
529 * @return A Map of String job names to completion type -> count mapping.
530 */
531 public abstract ArrayMap<String, SparseIntArray> getJobCompletionStats();
532
533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 * The statistics associated with a particular wake lock.
535 */
536 public static abstract class Wakelock {
537 public abstract Timer getWakeTime(int type);
538 }
539
540 /**
Bookatzc8c44962017-05-11 12:12:54 -0700541 * The cumulative time the uid spent holding any partial wakelocks. This will generally
542 * differ from summing over the Wakelocks in getWakelockStats since the latter may have
543 * wakelocks that overlap in time (and therefore over-counts).
544 */
545 public abstract Timer getAggregatedPartialWakelockTimer();
546
547 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 * Returns a mapping containing sensor statistics.
549 *
550 * @return a Map from Integer sensor ids to Uid.Sensor objects.
551 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700552 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553
554 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700555 * Returns a mapping containing active process data.
556 */
557 public abstract SparseArray<? extends Pid> getPidStats();
Bookatzc8c44962017-05-11 12:12:54 -0700558
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700559 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 * Returns a mapping containing process statistics.
561 *
562 * @return a Map from Strings to Uid.Proc objects.
563 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700564 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565
566 /**
567 * Returns a mapping containing package statistics.
568 *
569 * @return a Map from Strings to Uid.Pkg objects.
570 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700571 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700572
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800573 public abstract ControllerActivityCounter getWifiControllerActivity();
574 public abstract ControllerActivityCounter getBluetoothControllerActivity();
575 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800576
577 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 * {@hide}
579 */
580 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700581
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800582 public abstract void noteWifiRunningLocked(long elapsedRealtime);
583 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
584 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
585 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
586 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
587 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
588 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
589 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
590 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
591 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800592 public abstract void noteActivityResumedLocked(long elapsedRealtime);
593 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800594 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
595 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
596 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700597 public abstract int getWifiScanCount(int which);
Bookatz867c0d72017-03-07 18:23:42 -0800598 public abstract int getWifiScanBackgroundCount(int which);
599 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
600 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800601 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700602 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800603 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700604 public abstract Timer getAudioTurnedOnTimer();
605 public abstract Timer getVideoTurnedOnTimer();
606 public abstract Timer getFlashlightTurnedOnTimer();
607 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700608 public abstract Timer getForegroundActivityTimer();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700609
610 /**
611 * Returns the timer keeping track of Foreground Service time
612 */
613 public abstract Timer getForegroundServiceTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800614 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800615 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatzb1f04f32017-05-19 13:57:32 -0700616 public abstract Timer getBluetoothUnoptimizedScanTimer();
617 public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700618 public abstract Counter getBluetoothScanResultCounter();
Bookatzb1f04f32017-05-19 13:57:32 -0700619 public abstract Counter getBluetoothScanResultBgCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700620
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700621 public abstract long[] getCpuFreqTimes(int which);
622 public abstract long[] getScreenOffCpuFreqTimes(int which);
623
Dianne Hackborna0200e32016-03-30 18:01:41 -0700624 // Note: the following times are disjoint. They can be added together to find the
625 // total time a uid has had any processes running at all.
626
627 /**
628 * Time this uid has any processes in the top state (or above such as persistent).
629 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800630 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700631 /**
632 * Time this uid has any process with a started out bound foreground service, but
633 * none in the "top" state.
634 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800635 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700636 /**
637 * Time this uid has any process that is top while the device is sleeping, but none
638 * in the "foreground service" or better state.
639 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800640 public static final int PROCESS_STATE_TOP_SLEEPING = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700641 /**
642 * Time this uid has any process in an active foreground state, but none in the
643 * "top sleeping" or better state.
644 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800645 public static final int PROCESS_STATE_FOREGROUND = 3;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700646 /**
647 * Time this uid has any process in an active background state, but none in the
648 * "foreground" or better state.
649 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800650 public static final int PROCESS_STATE_BACKGROUND = 4;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700651 /**
652 * Time this uid has any processes that are sitting around cached, not in one of the
653 * other active states.
654 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800655 public static final int PROCESS_STATE_CACHED = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700656 /**
657 * Total number of process states we track.
658 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800659 public static final int NUM_PROCESS_STATE = 6;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700660
661 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackborna8d10942015-11-19 17:55:19 -0800662 "Top", "Fg Service", "Top Sleeping", "Foreground", "Background", "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700663 };
664
665 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800666 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700667
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800668 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669
Robert Greenwalta029ea12013-09-25 16:38:12 -0700670 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
671
Dianne Hackborn617f8772009-03-31 15:04:46 -0700672 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700673 * Note that these must match the constants in android.os.PowerManager.
674 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
675 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700676 */
677 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700678 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700679 };
Bookatzc8c44962017-05-11 12:12:54 -0700680
Phil Weaverda80d672016-03-15 16:25:46 -0700681 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700682
Dianne Hackborn617f8772009-03-31 15:04:46 -0700683 public abstract void noteUserActivityLocked(int type);
684 public abstract boolean hasUserActivity();
685 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700686
687 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800688 public abstract long getNetworkActivityBytes(int type, int which);
689 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800690 public abstract long getMobileRadioActiveTime(int which);
691 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700692
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700693 /**
694 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
695 */
696 public abstract long getUserCpuTimeUs(int which);
697
698 /**
699 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
700 */
701 public abstract long getSystemCpuTimeUs(int which);
702
703 /**
Sudheer Shanka71f34b32017-07-21 00:14:24 -0700704 * Returns the approximate cpu time (in microseconds) spent at a certain CPU speed for a
Adam Lesinski6832f392015-09-05 18:05:40 -0700705 * given CPU cluster.
706 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700707 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700708 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700709 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
710 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700711 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700712 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700713
Adam Lesinski5f056f62016-07-14 16:56:08 -0700714 /**
715 * Returns the number of times this UID woke up the Application Processor to
716 * process a mobile radio packet.
717 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
718 */
719 public abstract long getMobileRadioApWakeupCount(int which);
720
721 /**
722 * Returns the number of times this UID woke up the Application Processor to
723 * process a WiFi packet.
724 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
725 */
726 public abstract long getWifiRadioApWakeupCount(int which);
727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800729 /*
730 * FIXME: it's not correct to use this magic value because it
731 * could clash with a sensor handle (which are defined by
732 * the sensor HAL, and therefore out of our control
733 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 // Magic sensor number for the GPS.
735 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800740
Bookatz867c0d72017-03-07 18:23:42 -0800741 /** Returns a Timer for sensor usage when app is in the background. */
742 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 }
744
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700745 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800746 public int mWakeNesting;
747 public long mWakeSumMs;
748 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700749 }
750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 /**
752 * The statistics associated with a particular process.
753 */
754 public static abstract class Proc {
755
Dianne Hackborn287952c2010-09-22 22:34:31 -0700756 public static class ExcessivePower {
757 public static final int TYPE_WAKE = 1;
758 public static final int TYPE_CPU = 2;
759
760 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700761 public long overTime;
762 public long usedTime;
763 }
764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800766 * Returns true if this process is still active in the battery stats.
767 */
768 public abstract boolean isActive();
769
770 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700771 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700773 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 */
775 public abstract long getUserTime(int which);
776
777 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700778 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700780 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 */
782 public abstract long getSystemTime(int which);
783
784 /**
785 * Returns the number of times the process has been started.
786 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700787 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 */
789 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700790
791 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800792 * Returns the number of times the process has crashed.
793 *
794 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
795 */
796 public abstract int getNumCrashes(int which);
797
798 /**
799 * Returns the number of times the process has ANRed.
800 *
801 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
802 */
803 public abstract int getNumAnrs(int which);
804
805 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700806 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700807 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700808 * @return foreground cpu time in microseconds
809 */
810 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700811
Dianne Hackborn287952c2010-09-22 22:34:31 -0700812 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700813
Dianne Hackborn287952c2010-09-22 22:34:31 -0700814 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 }
816
817 /**
818 * The statistics associated with a particular package.
819 */
820 public static abstract class Pkg {
821
822 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700823 * Returns information about all wakeup alarms that have been triggered for this
824 * package. The mapping keys are tag names for the alarms, the counter contains
825 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700827 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828
829 /**
830 * Returns a mapping containing service statistics.
831 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700832 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833
834 /**
835 * The statistics associated with a particular service.
836 */
Joe Onoratoabded112016-02-08 16:49:39 -0800837 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838
839 /**
840 * Returns the amount of time spent started.
841 *
842 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700843 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 * @return
845 */
846 public abstract long getStartTime(long batteryUptime, int which);
847
848 /**
849 * Returns the total number of times startService() has been called.
850 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700851 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 */
853 public abstract int getStarts(int which);
854
855 /**
856 * Returns the total number times the service has been launched.
857 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700858 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 */
860 public abstract int getLaunches(int which);
861 }
862 }
863 }
864
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800865 public static final class LevelStepTracker {
866 public long mLastStepTime = -1;
867 public int mNumStepDurations;
868 public final long[] mStepDurations;
869
870 public LevelStepTracker(int maxLevelSteps) {
871 mStepDurations = new long[maxLevelSteps];
872 }
873
874 public LevelStepTracker(int numSteps, long[] steps) {
875 mNumStepDurations = numSteps;
876 mStepDurations = new long[numSteps];
877 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
878 }
879
880 public long getDurationAt(int index) {
881 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
882 }
883
884 public int getLevelAt(int index) {
885 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
886 >> STEP_LEVEL_LEVEL_SHIFT);
887 }
888
889 public int getInitModeAt(int index) {
890 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
891 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
892 }
893
894 public int getModModeAt(int index) {
895 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
896 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
897 }
898
899 private void appendHex(long val, int topOffset, StringBuilder out) {
900 boolean hasData = false;
901 while (topOffset >= 0) {
902 int digit = (int)( (val>>topOffset) & 0xf );
903 topOffset -= 4;
904 if (!hasData && digit == 0) {
905 continue;
906 }
907 hasData = true;
908 if (digit >= 0 && digit <= 9) {
909 out.append((char)('0' + digit));
910 } else {
911 out.append((char)('a' + digit - 10));
912 }
913 }
914 }
915
916 public void encodeEntryAt(int index, StringBuilder out) {
917 long item = mStepDurations[index];
918 long duration = item & STEP_LEVEL_TIME_MASK;
919 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
920 >> STEP_LEVEL_LEVEL_SHIFT);
921 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
922 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
923 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
924 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
925 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
926 case Display.STATE_OFF: out.append('f'); break;
927 case Display.STATE_ON: out.append('o'); break;
928 case Display.STATE_DOZE: out.append('d'); break;
929 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
930 }
931 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
932 out.append('p');
933 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700934 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
935 out.append('i');
936 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800937 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
938 case Display.STATE_OFF: out.append('F'); break;
939 case Display.STATE_ON: out.append('O'); break;
940 case Display.STATE_DOZE: out.append('D'); break;
941 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
942 }
943 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
944 out.append('P');
945 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700946 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
947 out.append('I');
948 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800949 out.append('-');
950 appendHex(level, 4, out);
951 out.append('-');
952 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
953 }
954
955 public void decodeEntryAt(int index, String value) {
956 final int N = value.length();
957 int i = 0;
958 char c;
959 long out = 0;
960 while (i < N && (c=value.charAt(i)) != '-') {
961 i++;
962 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800963 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800964 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800965 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800966 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800967 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800968 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800969 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
970 << STEP_LEVEL_INITIAL_MODE_SHIFT);
971 break;
972 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
973 << STEP_LEVEL_INITIAL_MODE_SHIFT);
974 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700975 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
976 << STEP_LEVEL_INITIAL_MODE_SHIFT);
977 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800978 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
979 break;
980 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
981 break;
982 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
983 break;
984 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
985 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
986 break;
987 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
988 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800989 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700990 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
991 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
992 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800993 }
994 }
995 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800996 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800997 while (i < N && (c=value.charAt(i)) != '-') {
998 i++;
999 level <<= 4;
1000 if (c >= '0' && c <= '9') {
1001 level += c - '0';
1002 } else if (c >= 'a' && c <= 'f') {
1003 level += c - 'a' + 10;
1004 } else if (c >= 'A' && c <= 'F') {
1005 level += c - 'A' + 10;
1006 }
1007 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001008 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001009 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
1010 long duration = 0;
1011 while (i < N && (c=value.charAt(i)) != '-') {
1012 i++;
1013 duration <<= 4;
1014 if (c >= '0' && c <= '9') {
1015 duration += c - '0';
1016 } else if (c >= 'a' && c <= 'f') {
1017 duration += c - 'a' + 10;
1018 } else if (c >= 'A' && c <= 'F') {
1019 duration += c - 'A' + 10;
1020 }
1021 }
1022 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
1023 }
1024
1025 public void init() {
1026 mLastStepTime = -1;
1027 mNumStepDurations = 0;
1028 }
1029
1030 public void clearTime() {
1031 mLastStepTime = -1;
1032 }
1033
1034 public long computeTimePerLevel() {
1035 final long[] steps = mStepDurations;
1036 final int numSteps = mNumStepDurations;
1037
1038 // For now we'll do a simple average across all steps.
1039 if (numSteps <= 0) {
1040 return -1;
1041 }
1042 long total = 0;
1043 for (int i=0; i<numSteps; i++) {
1044 total += steps[i] & STEP_LEVEL_TIME_MASK;
1045 }
1046 return total / numSteps;
1047 /*
1048 long[] buckets = new long[numSteps];
1049 int numBuckets = 0;
1050 int numToAverage = 4;
1051 int i = 0;
1052 while (i < numSteps) {
1053 long totalTime = 0;
1054 int num = 0;
1055 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1056 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1057 num++;
1058 }
1059 buckets[numBuckets] = totalTime / num;
1060 numBuckets++;
1061 numToAverage *= 2;
1062 i += num;
1063 }
1064 if (numBuckets < 1) {
1065 return -1;
1066 }
1067 long averageTime = buckets[numBuckets-1];
1068 for (i=numBuckets-2; i>=0; i--) {
1069 averageTime = (averageTime + buckets[i]) / 2;
1070 }
1071 return averageTime;
1072 */
1073 }
1074
1075 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1076 int[] outNumOfInterest) {
1077 final long[] steps = mStepDurations;
1078 final int count = mNumStepDurations;
1079 if (count <= 0) {
1080 return -1;
1081 }
1082 long total = 0;
1083 int numOfInterest = 0;
1084 for (int i=0; i<count; i++) {
1085 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1086 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1087 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1088 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1089 // If the modes of interest didn't change during this step period...
1090 if ((modMode&modesOfInterest) == 0) {
1091 // And the mode values during this period match those we are measuring...
1092 if ((initMode&modesOfInterest) == modeValues) {
1093 // Then this can be used to estimate the total time!
1094 numOfInterest++;
1095 total += steps[i] & STEP_LEVEL_TIME_MASK;
1096 }
1097 }
1098 }
1099 if (numOfInterest <= 0) {
1100 return -1;
1101 }
1102
1103 if (outNumOfInterest != null) {
1104 outNumOfInterest[0] = numOfInterest;
1105 }
1106
1107 // The estimated time is the average time we spend in each level, multipled
1108 // by 100 -- the total number of battery levels
1109 return (total / numOfInterest) * 100;
1110 }
1111
1112 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1113 int stepCount = mNumStepDurations;
1114 final long lastStepTime = mLastStepTime;
1115 if (lastStepTime >= 0 && numStepLevels > 0) {
1116 final long[] steps = mStepDurations;
1117 long duration = elapsedRealtime - lastStepTime;
1118 for (int i=0; i<numStepLevels; i++) {
1119 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1120 long thisDuration = duration / (numStepLevels-i);
1121 duration -= thisDuration;
1122 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1123 thisDuration = STEP_LEVEL_TIME_MASK;
1124 }
1125 steps[0] = thisDuration | modeBits;
1126 }
1127 stepCount += numStepLevels;
1128 if (stepCount > steps.length) {
1129 stepCount = steps.length;
1130 }
1131 }
1132 mNumStepDurations = stepCount;
1133 mLastStepTime = elapsedRealtime;
1134 }
1135
1136 public void readFromParcel(Parcel in) {
1137 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001138 if (N > mStepDurations.length) {
1139 throw new ParcelFormatException("more step durations than available: " + N);
1140 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001141 mNumStepDurations = N;
1142 for (int i=0; i<N; i++) {
1143 mStepDurations[i] = in.readLong();
1144 }
1145 }
1146
1147 public void writeToParcel(Parcel out) {
1148 final int N = mNumStepDurations;
1149 out.writeInt(N);
1150 for (int i=0; i<N; i++) {
1151 out.writeLong(mStepDurations[i]);
1152 }
1153 }
1154 }
1155
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001156 public static final class PackageChange {
1157 public String mPackageName;
1158 public boolean mUpdate;
1159 public int mVersionCode;
1160 }
1161
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001162 public static final class DailyItem {
1163 public long mStartTime;
1164 public long mEndTime;
1165 public LevelStepTracker mDischargeSteps;
1166 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001167 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001168 }
1169
1170 public abstract DailyItem getDailyItemLocked(int daysAgo);
1171
1172 public abstract long getCurrentDailyStartTime();
1173
1174 public abstract long getNextMinDailyDeadline();
1175
1176 public abstract long getNextMaxDailyDeadline();
1177
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001178 public abstract long[] getCpuFreqs();
1179
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001180 public final static class HistoryTag {
1181 public String string;
1182 public int uid;
1183
1184 public int poolIdx;
1185
1186 public void setTo(HistoryTag o) {
1187 string = o.string;
1188 uid = o.uid;
1189 poolIdx = o.poolIdx;
1190 }
1191
1192 public void setTo(String _string, int _uid) {
1193 string = _string;
1194 uid = _uid;
1195 poolIdx = -1;
1196 }
1197
1198 public void writeToParcel(Parcel dest, int flags) {
1199 dest.writeString(string);
1200 dest.writeInt(uid);
1201 }
1202
1203 public void readFromParcel(Parcel src) {
1204 string = src.readString();
1205 uid = src.readInt();
1206 poolIdx = -1;
1207 }
1208
1209 @Override
1210 public boolean equals(Object o) {
1211 if (this == o) return true;
1212 if (o == null || getClass() != o.getClass()) return false;
1213
1214 HistoryTag that = (HistoryTag) o;
1215
1216 if (uid != that.uid) return false;
1217 if (!string.equals(that.string)) return false;
1218
1219 return true;
1220 }
1221
1222 @Override
1223 public int hashCode() {
1224 int result = string.hashCode();
1225 result = 31 * result + uid;
1226 return result;
1227 }
1228 }
1229
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001230 /**
1231 * Optional detailed information that can go into a history step. This is typically
1232 * generated each time the battery level changes.
1233 */
1234 public final static class HistoryStepDetails {
1235 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1236 public int userTime;
1237 public int systemTime;
1238
1239 // Top three apps using CPU in the last step, with times in 1/100 second.
1240 public int appCpuUid1;
1241 public int appCpuUTime1;
1242 public int appCpuSTime1;
1243 public int appCpuUid2;
1244 public int appCpuUTime2;
1245 public int appCpuSTime2;
1246 public int appCpuUid3;
1247 public int appCpuUTime3;
1248 public int appCpuSTime3;
1249
1250 // Information from /proc/stat
1251 public int statUserTime;
1252 public int statSystemTime;
1253 public int statIOWaitTime;
1254 public int statIrqTime;
1255 public int statSoftIrqTime;
1256 public int statIdlTime;
1257
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001258 // Platform-level low power state stats
1259 public String statPlatformIdleState;
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001260 public String statSubsystemPowerState;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001261
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001262 public HistoryStepDetails() {
1263 clear();
1264 }
1265
1266 public void clear() {
1267 userTime = systemTime = 0;
1268 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1269 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1270 = appCpuUTime3 = appCpuSTime3 = 0;
1271 }
1272
1273 public void writeToParcel(Parcel out) {
1274 out.writeInt(userTime);
1275 out.writeInt(systemTime);
1276 out.writeInt(appCpuUid1);
1277 out.writeInt(appCpuUTime1);
1278 out.writeInt(appCpuSTime1);
1279 out.writeInt(appCpuUid2);
1280 out.writeInt(appCpuUTime2);
1281 out.writeInt(appCpuSTime2);
1282 out.writeInt(appCpuUid3);
1283 out.writeInt(appCpuUTime3);
1284 out.writeInt(appCpuSTime3);
1285 out.writeInt(statUserTime);
1286 out.writeInt(statSystemTime);
1287 out.writeInt(statIOWaitTime);
1288 out.writeInt(statIrqTime);
1289 out.writeInt(statSoftIrqTime);
1290 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001291 out.writeString(statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001292 out.writeString(statSubsystemPowerState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001293 }
1294
1295 public void readFromParcel(Parcel in) {
1296 userTime = in.readInt();
1297 systemTime = in.readInt();
1298 appCpuUid1 = in.readInt();
1299 appCpuUTime1 = in.readInt();
1300 appCpuSTime1 = in.readInt();
1301 appCpuUid2 = in.readInt();
1302 appCpuUTime2 = in.readInt();
1303 appCpuSTime2 = in.readInt();
1304 appCpuUid3 = in.readInt();
1305 appCpuUTime3 = in.readInt();
1306 appCpuSTime3 = in.readInt();
1307 statUserTime = in.readInt();
1308 statSystemTime = in.readInt();
1309 statIOWaitTime = in.readInt();
1310 statIrqTime = in.readInt();
1311 statSoftIrqTime = in.readInt();
1312 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001313 statPlatformIdleState = in.readString();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001314 statSubsystemPowerState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001315 }
1316 }
1317
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001318 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001319 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001320
1321 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001322 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001323
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001324 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001325 public static final byte CMD_NULL = -1;
1326 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001327 public static final byte CMD_CURRENT_TIME = 5;
1328 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001329 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001330 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001331
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001332 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001333
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001334 /**
1335 * Return whether the command code is a delta data update.
1336 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001337 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001338 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001339 }
1340
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001341 public byte batteryLevel;
1342 public byte batteryStatus;
1343 public byte batteryHealth;
1344 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001345
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001346 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001347 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001348
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001349 // The charge of the battery in micro-Ampere-hours.
1350 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001351
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001352 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001353 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001354 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001355 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001356 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1357 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001358 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001359 public static final int STATE_PHONE_STATE_SHIFT = 6;
1360 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001361 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001362 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001363 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001364
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001365 // These states always appear directly in the first int token
1366 // of a delta change; they should be ones that change relatively
1367 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001368 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1369 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001370 public static final int STATE_GPS_ON_FLAG = 1<<29;
1371 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001372 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001373 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001374 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001375 // Do not use, this is used for coulomb delta count.
1376 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001377 // These are on the lower bits used for the command; if they change
1378 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001379 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001380 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001381 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001382 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1383 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1384 // empty slot
1385 // empty slot
1386 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001387
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001388 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001389 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1390
1391 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001392
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001393 public int states;
1394
Dianne Hackborn3251b902014-06-20 14:40:53 -07001395 // Constants from WIFI_SUPPL_STATE_*
1396 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1397 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1398 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1399 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1400 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1401 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1402
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001403 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001404 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1405 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1406 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001407 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001408 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1409 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1410 public static final int STATE2_CHARGING_FLAG = 1<<24;
1411 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1412 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1413 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001414 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001415
1416 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001417 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001418 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1419
1420 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001421
Dianne Hackborn40c87252014-03-19 16:55:40 -07001422 public int states2;
1423
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001424 // The wake lock that was acquired at this point.
1425 public HistoryTag wakelockTag;
1426
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001427 // Kernel wakeup reason at this point.
1428 public HistoryTag wakeReasonTag;
1429
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001430 // Non-null when there is more detailed information at this step.
1431 public HistoryStepDetails stepDetails;
1432
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001433 public static final int EVENT_FLAG_START = 0x8000;
1434 public static final int EVENT_FLAG_FINISH = 0x4000;
1435
1436 // No event in this item.
1437 public static final int EVENT_NONE = 0x0000;
1438 // Event is about a process that is running.
1439 public static final int EVENT_PROC = 0x0001;
1440 // Event is about an application package that is in the foreground.
1441 public static final int EVENT_FOREGROUND = 0x0002;
1442 // Event is about an application package that is at the top of the screen.
1443 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001444 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001445 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001446 // Events for all additional wake locks aquired/release within a wake block.
1447 // These are not generated by default.
1448 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001449 // Event is about an application executing a scheduled job.
1450 public static final int EVENT_JOB = 0x0006;
1451 // Events for users running.
1452 public static final int EVENT_USER_RUNNING = 0x0007;
1453 // Events for foreground user.
1454 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001455 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001456 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001457 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001458 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001459 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001460 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001461 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001462 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001463 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001464 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001465 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001466 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001467 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001468 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001469 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001470 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001471 // Event for a package being on the temporary whitelist.
1472 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001473 // Event for the screen waking up.
1474 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001475 // Event for the UID that woke up the application processor.
1476 // Used for wakeups coming from WiFi, modem, etc.
1477 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001478 // Event for reporting that a specific partial wake lock has been held for a long duration.
1479 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001480
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001481 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001482 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001483 // Mask to extract out only the type part of the event.
1484 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001485
1486 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1487 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1488 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1489 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1490 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1491 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001492 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1493 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001494 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1495 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001496 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1497 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1498 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1499 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1500 public static final int EVENT_USER_FOREGROUND_START =
1501 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1502 public static final int EVENT_USER_FOREGROUND_FINISH =
1503 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001504 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1505 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001506 public static final int EVENT_TEMP_WHITELIST_START =
1507 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1508 public static final int EVENT_TEMP_WHITELIST_FINISH =
1509 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001510 public static final int EVENT_LONG_WAKE_LOCK_START =
1511 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1512 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1513 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001514
1515 // For CMD_EVENT.
1516 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001517 public HistoryTag eventTag;
1518
Dianne Hackborn9a755432014-05-15 17:05:22 -07001519 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001520 public long currentTime;
1521
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001522 // Meta-data when reading.
1523 public int numReadInts;
1524
1525 // Pre-allocated objects.
1526 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001527 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001528 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001529
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001530 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001531 }
Bookatzc8c44962017-05-11 12:12:54 -07001532
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001533 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001534 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001535 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001536 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001537 }
Bookatzc8c44962017-05-11 12:12:54 -07001538
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001539 public int describeContents() {
1540 return 0;
1541 }
1542
1543 public void writeToParcel(Parcel dest, int flags) {
1544 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001545 int bat = (((int)cmd)&0xff)
1546 | ((((int)batteryLevel)<<8)&0xff00)
1547 | ((((int)batteryStatus)<<16)&0xf0000)
1548 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001549 | ((((int)batteryPlugType)<<24)&0xf000000)
1550 | (wakelockTag != null ? 0x10000000 : 0)
1551 | (wakeReasonTag != null ? 0x20000000 : 0)
1552 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001553 dest.writeInt(bat);
1554 bat = (((int)batteryTemperature)&0xffff)
1555 | ((((int)batteryVoltage)<<16)&0xffff0000);
1556 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001557 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001558 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001559 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001560 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001561 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001562 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001563 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001564 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001565 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001566 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001567 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001568 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001569 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001570 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001571 dest.writeLong(currentTime);
1572 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001573 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001574
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001575 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001576 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001577 int bat = src.readInt();
1578 cmd = (byte)(bat&0xff);
1579 batteryLevel = (byte)((bat>>8)&0xff);
1580 batteryStatus = (byte)((bat>>16)&0xf);
1581 batteryHealth = (byte)((bat>>20)&0xf);
1582 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001583 int bat2 = src.readInt();
1584 batteryTemperature = (short)(bat2&0xffff);
1585 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001586 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001587 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001588 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001589 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001590 wakelockTag = localWakelockTag;
1591 wakelockTag.readFromParcel(src);
1592 } else {
1593 wakelockTag = null;
1594 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001595 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001596 wakeReasonTag = localWakeReasonTag;
1597 wakeReasonTag.readFromParcel(src);
1598 } else {
1599 wakeReasonTag = null;
1600 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001601 if ((bat&0x40000000) != 0) {
1602 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001603 eventTag = localEventTag;
1604 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001605 } else {
1606 eventCode = EVENT_NONE;
1607 eventTag = null;
1608 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001609 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001610 currentTime = src.readLong();
1611 } else {
1612 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001613 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001614 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001615 }
1616
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001617 public void clear() {
1618 time = 0;
1619 cmd = CMD_NULL;
1620 batteryLevel = 0;
1621 batteryStatus = 0;
1622 batteryHealth = 0;
1623 batteryPlugType = 0;
1624 batteryTemperature = 0;
1625 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001626 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001627 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001628 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001629 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001630 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001631 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001632 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001633 }
Bookatzc8c44962017-05-11 12:12:54 -07001634
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001635 public void setTo(HistoryItem o) {
1636 time = o.time;
1637 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001638 setToCommon(o);
1639 }
1640
1641 public void setTo(long time, byte cmd, HistoryItem o) {
1642 this.time = time;
1643 this.cmd = cmd;
1644 setToCommon(o);
1645 }
1646
1647 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001648 batteryLevel = o.batteryLevel;
1649 batteryStatus = o.batteryStatus;
1650 batteryHealth = o.batteryHealth;
1651 batteryPlugType = o.batteryPlugType;
1652 batteryTemperature = o.batteryTemperature;
1653 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001654 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001655 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001656 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001657 if (o.wakelockTag != null) {
1658 wakelockTag = localWakelockTag;
1659 wakelockTag.setTo(o.wakelockTag);
1660 } else {
1661 wakelockTag = null;
1662 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001663 if (o.wakeReasonTag != null) {
1664 wakeReasonTag = localWakeReasonTag;
1665 wakeReasonTag.setTo(o.wakeReasonTag);
1666 } else {
1667 wakeReasonTag = null;
1668 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001669 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001670 if (o.eventTag != null) {
1671 eventTag = localEventTag;
1672 eventTag.setTo(o.eventTag);
1673 } else {
1674 eventTag = null;
1675 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001676 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001677 }
1678
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001679 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001680 return batteryLevel == o.batteryLevel
1681 && batteryStatus == o.batteryStatus
1682 && batteryHealth == o.batteryHealth
1683 && batteryPlugType == o.batteryPlugType
1684 && batteryTemperature == o.batteryTemperature
1685 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001686 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001687 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001688 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001689 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001690 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001691
1692 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001693 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001694 return false;
1695 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001696 if (wakelockTag != o.wakelockTag) {
1697 if (wakelockTag == null || o.wakelockTag == null) {
1698 return false;
1699 }
1700 if (!wakelockTag.equals(o.wakelockTag)) {
1701 return false;
1702 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001703 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001704 if (wakeReasonTag != o.wakeReasonTag) {
1705 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1706 return false;
1707 }
1708 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1709 return false;
1710 }
1711 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001712 if (eventTag != o.eventTag) {
1713 if (eventTag == null || o.eventTag == null) {
1714 return false;
1715 }
1716 if (!eventTag.equals(o.eventTag)) {
1717 return false;
1718 }
1719 }
1720 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001721 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001722 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001723
1724 public final static class HistoryEventTracker {
1725 private final HashMap<String, SparseIntArray>[] mActiveEvents
1726 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1727
1728 public boolean updateState(int code, String name, int uid, int poolIdx) {
1729 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1730 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1731 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1732 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001733 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001734 mActiveEvents[idx] = active;
1735 }
1736 SparseIntArray uids = active.get(name);
1737 if (uids == null) {
1738 uids = new SparseIntArray();
1739 active.put(name, uids);
1740 }
1741 if (uids.indexOfKey(uid) >= 0) {
1742 // Already set, nothing to do!
1743 return false;
1744 }
1745 uids.put(uid, poolIdx);
1746 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1747 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1748 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1749 if (active == null) {
1750 // not currently active, nothing to do.
1751 return false;
1752 }
1753 SparseIntArray uids = active.get(name);
1754 if (uids == null) {
1755 // not currently active, nothing to do.
1756 return false;
1757 }
1758 idx = uids.indexOfKey(uid);
1759 if (idx < 0) {
1760 // not currently active, nothing to do.
1761 return false;
1762 }
1763 uids.removeAt(idx);
1764 if (uids.size() <= 0) {
1765 active.remove(name);
1766 }
1767 }
1768 return true;
1769 }
1770
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001771 public void removeEvents(int code) {
1772 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1773 mActiveEvents[idx] = null;
1774 }
1775
Dianne Hackborn37de0982014-05-09 09:32:18 -07001776 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1777 return mActiveEvents[code];
1778 }
1779 }
1780
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001781 public static final class BitDescription {
1782 public final int mask;
1783 public final int shift;
1784 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001785 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001786 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001787 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07001788
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001789 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001790 this.mask = mask;
1791 this.shift = -1;
1792 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001793 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001794 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001795 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001796 }
Bookatzc8c44962017-05-11 12:12:54 -07001797
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001798 public BitDescription(int mask, int shift, String name, String shortName,
1799 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001800 this.mask = mask;
1801 this.shift = shift;
1802 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001803 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001804 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001805 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001806 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001807 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001808
Dianne Hackbornfc064132014-06-02 12:42:12 -07001809 /**
1810 * Don't allow any more batching in to the current history event. This
1811 * is called when printing partial histories, so to ensure that the next
1812 * history event will go in to a new batch after what was printed in the
1813 * last partial history.
1814 */
1815 public abstract void commitCurrentHistoryBatchLocked();
1816
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001817 public abstract int getHistoryTotalSize();
1818
1819 public abstract int getHistoryUsedSize();
1820
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001821 public abstract boolean startIteratingHistoryLocked();
1822
Dianne Hackborn099bc622014-01-22 13:39:16 -08001823 public abstract int getHistoryStringPoolSize();
1824
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001825 public abstract int getHistoryStringPoolBytes();
1826
1827 public abstract String getHistoryTagPoolString(int index);
1828
1829 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001830
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001831 public abstract boolean getNextHistoryLocked(HistoryItem out);
1832
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001833 public abstract void finishIteratingHistoryLocked();
1834
1835 public abstract boolean startIteratingOldHistoryLocked();
1836
1837 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1838
1839 public abstract void finishIteratingOldHistoryLocked();
1840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001842 * Return the base time offset for the battery history.
1843 */
1844 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07001845
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001846 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 * Returns the number of times the device has been started.
1848 */
1849 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07001850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001852 * 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 -08001853 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07001854 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 * {@hide}
1856 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001857 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07001858
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001859 /**
1860 * Returns the number of times the screen was turned on.
1861 *
1862 * {@hide}
1863 */
1864 public abstract int getScreenOnCount(int which);
1865
Jeff Browne95c3cd2014-05-02 16:59:26 -07001866 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1867
Dianne Hackborn617f8772009-03-31 15:04:46 -07001868 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1869 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1870 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1871 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1872 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07001873
Dianne Hackborn617f8772009-03-31 15:04:46 -07001874 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1875 "dark", "dim", "medium", "light", "bright"
1876 };
Bookatzc8c44962017-05-11 12:12:54 -07001877
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001878 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1879 "0", "1", "2", "3", "4"
1880 };
1881
Dianne Hackborn617f8772009-03-31 15:04:46 -07001882 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001883
Dianne Hackborn617f8772009-03-31 15:04:46 -07001884 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001885 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001886 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07001887 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07001888 * {@hide}
1889 */
1890 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001891 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001894 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001895 * running on battery.
1896 *
1897 * {@hide}
1898 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001899 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001900
1901 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001902 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001903 *
1904 * {@hide}
1905 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001906 public abstract int getPowerSaveModeEnabledCount(int which);
1907
1908 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001909 * Constant for device idle mode: not active.
1910 */
1911 public static final int DEVICE_IDLE_MODE_OFF = 0;
1912
1913 /**
1914 * Constant for device idle mode: active in lightweight mode.
1915 */
1916 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1917
1918 /**
1919 * Constant for device idle mode: active in full mode.
1920 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07001921 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001922
1923 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001924 * Returns the time in microseconds that device has been in idle mode while
1925 * running on battery.
1926 *
1927 * {@hide}
1928 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001929 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001930
1931 /**
1932 * Returns the number of times that the devie has gone in to idle mode.
1933 *
1934 * {@hide}
1935 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001936 public abstract int getDeviceIdleModeCount(int mode, int which);
1937
1938 /**
1939 * Return the longest duration we spent in a particular device idle mode (fully in the
1940 * mode, not in idle maintenance etc).
1941 */
1942 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001943
1944 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001945 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001946 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001947 * counts all of the time that we consider the device to be idle, whether or not
1948 * it is currently in the actual device idle mode.
1949 *
1950 * {@hide}
1951 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001952 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001953
1954 /**
1955 * Returns the number of times that the devie has started idling.
1956 *
1957 * {@hide}
1958 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001959 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001960
1961 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001962 * Returns the number of times that connectivity state changed.
1963 *
1964 * {@hide}
1965 */
1966 public abstract int getNumConnectivityChange(int which);
1967
1968 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001969 * 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 -08001970 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07001971 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 * {@hide}
1973 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001974 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07001975
Dianne Hackborn627bba72009-03-24 22:32:56 -07001976 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001977 * Returns the number of times a phone call was activated.
1978 *
1979 * {@hide}
1980 */
1981 public abstract int getPhoneOnCount(int which);
1982
1983 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001984 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001985 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07001986 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07001987 * {@hide}
1988 */
1989 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001990 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001991
Dianne Hackborn617f8772009-03-31 15:04:46 -07001992 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001993 * Returns the time in microseconds that the phone has been trying to
1994 * acquire a signal.
1995 *
1996 * {@hide}
1997 */
1998 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001999 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002000
2001 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002002 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002003 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002004 * {@hide}
2005 */
2006 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
2007
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002008 /**
2009 * Returns the time in microseconds that the mobile network has been active
2010 * (in a high power state).
2011 *
2012 * {@hide}
2013 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002014 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002015
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002016 /**
2017 * Returns the number of times that the mobile network has transitioned to the
2018 * active state.
2019 *
2020 * {@hide}
2021 */
2022 public abstract int getMobileRadioActiveCount(int which);
2023
2024 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002025 * Returns the time in microseconds that is the difference between the mobile radio
2026 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
2027 * from the radio.
2028 *
2029 * {@hide}
2030 */
2031 public abstract long getMobileRadioActiveAdjustedTime(int which);
2032
2033 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002034 * Returns the time in microseconds that the mobile network has been active
2035 * (in a high power state) but not being able to blame on an app.
2036 *
2037 * {@hide}
2038 */
2039 public abstract long getMobileRadioActiveUnknownTime(int which);
2040
2041 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002042 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002043 *
2044 * {@hide}
2045 */
2046 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002047
Dianne Hackborn627bba72009-03-24 22:32:56 -07002048 public static final int DATA_CONNECTION_NONE = 0;
2049 public static final int DATA_CONNECTION_GPRS = 1;
2050 public static final int DATA_CONNECTION_EDGE = 2;
2051 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002052 public static final int DATA_CONNECTION_CDMA = 4;
2053 public static final int DATA_CONNECTION_EVDO_0 = 5;
2054 public static final int DATA_CONNECTION_EVDO_A = 6;
2055 public static final int DATA_CONNECTION_1xRTT = 7;
2056 public static final int DATA_CONNECTION_HSDPA = 8;
2057 public static final int DATA_CONNECTION_HSUPA = 9;
2058 public static final int DATA_CONNECTION_HSPA = 10;
2059 public static final int DATA_CONNECTION_IDEN = 11;
2060 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002061 public static final int DATA_CONNECTION_LTE = 13;
2062 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08002063 public static final int DATA_CONNECTION_HSPAP = 15;
2064 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002065
Dianne Hackborn627bba72009-03-24 22:32:56 -07002066 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002067 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002068 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08002069 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002070 };
Bookatzc8c44962017-05-11 12:12:54 -07002071
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002072 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002073
Dianne Hackborn627bba72009-03-24 22:32:56 -07002074 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002075 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002076 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002077 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002078 * {@hide}
2079 */
2080 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002081 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002084 * Returns the number of times the phone has entered the given data
2085 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002086 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002087 * {@hide}
2088 */
2089 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002090
Dianne Hackborn3251b902014-06-20 14:40:53 -07002091 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2092 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2093 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2094 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2095 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2096 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2097 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2098 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2099 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2100 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2101 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2102 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2103 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2104
2105 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2106
2107 static final String[] WIFI_SUPPL_STATE_NAMES = {
2108 "invalid", "disconn", "disabled", "inactive", "scanning",
2109 "authenticating", "associating", "associated", "4-way-handshake",
2110 "group-handshake", "completed", "dormant", "uninit"
2111 };
2112
2113 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2114 "inv", "dsc", "dis", "inact", "scan",
2115 "auth", "ascing", "asced", "4-way",
2116 "group", "compl", "dorm", "uninit"
2117 };
2118
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002119 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
2120 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002121 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002122 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2123 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002124 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002125 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2126 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2127 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002128 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002129 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002130 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002131 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002132 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2133 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002134 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2135 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2136 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2137 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2138 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2139 new String[] {"in", "out", "emergency", "off"},
2140 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002141 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2142 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2143 SignalStrength.SIGNAL_STRENGTH_NAMES,
2144 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002145 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2146 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2147 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002148 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002149
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002150 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2151 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002152 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002153 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002154 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002155 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002156 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002157 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2158 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2159 new String[] { "off", "light", "full", "???" },
2160 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002161 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2162 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2163 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002164 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2165 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2166 new String[] { "0", "1", "2", "3", "4" },
2167 new String[] { "0", "1", "2", "3", "4" }),
2168 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2169 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2170 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002171 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002172 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002173 };
2174
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002175 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002176 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002177 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2178 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002179 };
2180
2181 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002182 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002183 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002184 "Esw", "Ewa", "Elw", "Eec"
2185 };
2186
2187 @FunctionalInterface
2188 public interface IntToString {
2189 String applyAsString(int val);
2190 }
2191
2192 private static final IntToString sUidToString = UserHandle::formatUid;
2193 private static final IntToString sIntToString = Integer::toString;
2194
2195 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2196 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2197 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2198 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2199 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002200 };
2201
Dianne Hackborn617f8772009-03-31 15:04:46 -07002202 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002203 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002204 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002205 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002206 * {@hide}
2207 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002208 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002209
2210 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002211 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002212 * been in the running state while the device was running on battery.
2213 *
2214 * {@hide}
2215 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002216 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002217
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002218 public static final int WIFI_STATE_OFF = 0;
2219 public static final int WIFI_STATE_OFF_SCANNING = 1;
2220 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2221 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2222 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2223 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2224 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2225 public static final int WIFI_STATE_SOFT_AP = 7;
2226
2227 static final String[] WIFI_STATE_NAMES = {
2228 "off", "scanning", "no_net", "disconn",
2229 "sta", "p2p", "sta_p2p", "soft_ap"
2230 };
2231
2232 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2233
2234 /**
2235 * Returns the time in microseconds that WiFi has been running in the given state.
2236 *
2237 * {@hide}
2238 */
2239 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002240 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002241
2242 /**
2243 * Returns the number of times that WiFi has entered the given state.
2244 *
2245 * {@hide}
2246 */
2247 public abstract int getWifiStateCount(int wifiState, int which);
2248
The Android Open Source Project10592532009-03-18 17:39:46 -07002249 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002250 * Returns the time in microseconds that the wifi supplicant has been
2251 * in a given state.
2252 *
2253 * {@hide}
2254 */
2255 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2256
2257 /**
2258 * Returns the number of times that the wifi supplicant has transitioned
2259 * to a given state.
2260 *
2261 * {@hide}
2262 */
2263 public abstract int getWifiSupplStateCount(int state, int which);
2264
2265 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2266
2267 /**
2268 * Returns the time in microseconds that WIFI has been running with
2269 * the given signal strength.
2270 *
2271 * {@hide}
2272 */
2273 public abstract long getWifiSignalStrengthTime(int strengthBin,
2274 long elapsedRealtimeUs, int which);
2275
2276 /**
2277 * Returns the number of times WIFI has entered the given signal strength.
2278 *
2279 * {@hide}
2280 */
2281 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2282
2283 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002284 * Returns the time in microseconds that the flashlight has been on while the device was
2285 * running on battery.
2286 *
2287 * {@hide}
2288 */
2289 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2290
2291 /**
2292 * Returns the number of times that the flashlight has been turned on while the device was
2293 * running on battery.
2294 *
2295 * {@hide}
2296 */
2297 public abstract long getFlashlightOnCount(int which);
2298
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002299 /**
2300 * Returns the time in microseconds that the camera has been on while the device was
2301 * running on battery.
2302 *
2303 * {@hide}
2304 */
2305 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2306
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002307 /**
2308 * Returns the time in microseconds that bluetooth scans were running while the device was
2309 * on battery.
2310 *
2311 * {@hide}
2312 */
2313 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002314
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002315 public static final int NETWORK_MOBILE_RX_DATA = 0;
2316 public static final int NETWORK_MOBILE_TX_DATA = 1;
2317 public static final int NETWORK_WIFI_RX_DATA = 2;
2318 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002319 public static final int NETWORK_BT_RX_DATA = 4;
2320 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002321 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2322 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2323 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2324 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2325 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002326
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002327 public abstract long getNetworkActivityBytes(int type, int which);
2328 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002329
Adam Lesinskie08af192015-03-25 16:42:59 -07002330 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002331 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002332 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002333 * actual power data.
2334 */
2335 public abstract boolean hasWifiActivityReporting();
2336
2337 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002338 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2339 * in various radio controller states, such as transmit, receive, and idle.
2340 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002341 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002342 public abstract ControllerActivityCounter getWifiControllerActivity();
2343
2344 /**
2345 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2346 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2347 * actual power data.
2348 */
2349 public abstract boolean hasBluetoothActivityReporting();
2350
2351 /**
2352 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2353 * in various radio controller states, such as transmit, receive, and idle.
2354 * @return non-null {@link ControllerActivityCounter}
2355 */
2356 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2357
2358 /**
2359 * Returns true if the BatteryStats object has detailed modem power reports.
2360 * When true, calling {@link #getModemControllerActivity()} will yield the
2361 * actual power data.
2362 */
2363 public abstract boolean hasModemActivityReporting();
2364
2365 /**
2366 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2367 * in various radio controller states, such as transmit, receive, and idle.
2368 * @return non-null {@link ControllerActivityCounter}
2369 */
2370 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002371
The Android Open Source Project10592532009-03-18 17:39:46 -07002372 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002373 * Return the wall clock time when battery stats data collection started.
2374 */
2375 public abstract long getStartClockTime();
2376
2377 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002378 * Return platform version tag that we were running in when the battery stats started.
2379 */
2380 public abstract String getStartPlatformVersion();
2381
2382 /**
2383 * Return platform version tag that we were running in when the battery stats ended.
2384 */
2385 public abstract String getEndPlatformVersion();
2386
2387 /**
2388 * Return the internal version code of the parcelled format.
2389 */
2390 public abstract int getParcelVersion();
2391
2392 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 * Return whether we are currently running on battery.
2394 */
2395 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 /**
2398 * Returns a SparseArray containing the statistics for each uid.
2399 */
2400 public abstract SparseArray<? extends Uid> getUidStats();
2401
2402 /**
2403 * Returns the current battery uptime in microseconds.
2404 *
2405 * @param curTime the amount of elapsed realtime in microseconds.
2406 */
2407 public abstract long getBatteryUptime(long curTime);
2408
2409 /**
2410 * Returns the current battery realtime in microseconds.
2411 *
2412 * @param curTime the amount of elapsed realtime in microseconds.
2413 */
2414 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002415
The Android Open Source Project10592532009-03-18 17:39:46 -07002416 /**
Evan Millar633a1742009-04-02 16:36:33 -07002417 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002418 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002419 */
Evan Millar633a1742009-04-02 16:36:33 -07002420 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002421
The Android Open Source Project10592532009-03-18 17:39:46 -07002422 /**
Evan Millar633a1742009-04-02 16:36:33 -07002423 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2424 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002425 */
Evan Millar633a1742009-04-02 16:36:33 -07002426 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427
2428 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002429 * Get the amount the battery has discharged since the stats were
2430 * last reset after charging, as a lower-end approximation.
2431 */
2432 public abstract int getLowDischargeAmountSinceCharge();
2433
2434 /**
2435 * Get the amount the battery has discharged since the stats were
2436 * last reset after charging, as an upper-end approximation.
2437 */
2438 public abstract int getHighDischargeAmountSinceCharge();
2439
2440 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002441 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2442 */
2443 public abstract int getDischargeAmount(int which);
2444
2445 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002446 * Get the amount the battery has discharged while the screen was on,
2447 * since the last time power was unplugged.
2448 */
2449 public abstract int getDischargeAmountScreenOn();
2450
2451 /**
2452 * Get the amount the battery has discharged while the screen was on,
2453 * since the last time the device was charged.
2454 */
2455 public abstract int getDischargeAmountScreenOnSinceCharge();
2456
2457 /**
2458 * Get the amount the battery has discharged while the screen was off,
2459 * since the last time power was unplugged.
2460 */
2461 public abstract int getDischargeAmountScreenOff();
2462
2463 /**
2464 * Get the amount the battery has discharged while the screen was off,
2465 * since the last time the device was charged.
2466 */
2467 public abstract int getDischargeAmountScreenOffSinceCharge();
2468
2469 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 * Returns the total, last, or current battery uptime in microseconds.
2471 *
2472 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002473 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 */
2475 public abstract long computeBatteryUptime(long curTime, int which);
2476
2477 /**
2478 * Returns the total, last, or current battery realtime in microseconds.
2479 *
2480 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002481 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 */
2483 public abstract long computeBatteryRealtime(long curTime, int which);
2484
2485 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002486 * Returns the total, last, or current battery screen off uptime in microseconds.
2487 *
2488 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002489 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002490 */
2491 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2492
2493 /**
2494 * Returns the total, last, or current battery screen off realtime in microseconds.
2495 *
2496 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002497 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002498 */
2499 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2500
2501 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 * Returns the total, last, or current uptime in microseconds.
2503 *
2504 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002505 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 */
2507 public abstract long computeUptime(long curTime, int which);
2508
2509 /**
2510 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002511 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002513 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 */
2515 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002516
2517 /**
2518 * Compute an approximation for how much run time (in microseconds) is remaining on
2519 * the battery. Returns -1 if no time can be computed: either there is not
2520 * enough current data to make a decision, or the battery is currently
2521 * charging.
2522 *
2523 * @param curTime The current elepsed realtime in microseconds.
2524 */
2525 public abstract long computeBatteryTimeRemaining(long curTime);
2526
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002527 // The part of a step duration that is the actual time.
2528 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2529
2530 // Bits in a step duration that are the new battery level we are at.
2531 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002532 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002533
2534 // Bits in a step duration that are the initial mode we were in at that step.
2535 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002536 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002537
2538 // Bits in a step duration that indicate which modes changed during that step.
2539 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002540 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002541
2542 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2543 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2544
Santos Cordone94f0502017-02-24 12:31:20 -08002545 // The largest value for screen state that is tracked in battery states. Any values above
2546 // this should be mapped back to one of the tracked values before being tracked here.
2547 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2548
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002549 // Step duration mode: power save is on.
2550 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2551
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002552 // Step duration mode: device is currently in idle mode.
2553 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2554
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002555 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2556 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002557 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2558 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002559 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2560 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2561 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2562 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2563 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002564 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2565 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002566 };
2567 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2568 (Display.STATE_OFF-1),
2569 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002570 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002571 (Display.STATE_ON-1),
2572 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2573 (Display.STATE_DOZE-1),
2574 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2575 (Display.STATE_DOZE_SUSPEND-1),
2576 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002577 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002578 };
2579 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2580 "screen off",
2581 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002582 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002583 "screen on",
2584 "screen on power save",
2585 "screen doze",
2586 "screen doze power save",
2587 "screen doze-suspend",
2588 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002589 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002590 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002591
2592 /**
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002593 * Return the counter keeping track of the amount of battery discharge while the screen was off,
2594 * measured in micro-Ampere-hours. This will be non-zero only if the device's battery has
2595 * a coulomb counter.
2596 */
2597 public abstract LongCounter getDischargeScreenOffCoulombCounter();
2598
2599 /**
2600 * Return the counter keeping track of the amount of battery discharge measured in
2601 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2602 * a coulomb counter.
2603 */
2604 public abstract LongCounter getDischargeCoulombCounter();
2605
2606 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002607 * Returns the estimated real battery capacity, which may be less than the capacity
2608 * declared by the PowerProfile.
2609 * @return The estimated battery capacity in mAh.
2610 */
2611 public abstract int getEstimatedBatteryCapacity();
2612
2613 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002614 * @return The minimum learned battery capacity in uAh.
2615 */
2616 public abstract int getMinLearnedBatteryCapacity();
2617
2618 /**
2619 * @return The maximum learned battery capacity in uAh.
2620 */
2621 public abstract int getMaxLearnedBatteryCapacity() ;
2622
2623 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002624 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002625 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002626 public abstract LevelStepTracker getDischargeLevelStepTracker();
2627
2628 /**
2629 * Return the array of daily discharge step durations.
2630 */
2631 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002632
2633 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002634 * Compute an approximation for how much time (in microseconds) remains until the battery
2635 * is fully charged. Returns -1 if no time can be computed: either there is not
2636 * enough current data to make a decision, or the battery is currently
2637 * discharging.
2638 *
2639 * @param curTime The current elepsed realtime in microseconds.
2640 */
2641 public abstract long computeChargeTimeRemaining(long curTime);
2642
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002643 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002644 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002645 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002646 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002647
2648 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002649 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002650 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002651 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002652
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002653 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2654
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002655 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002656
Evan Millarc64edde2009-04-18 12:26:32 -07002657 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658
Bookatz50df7112017-08-04 14:53:26 -07002659 /**
2660 * Returns Timers tracking the total time of each Resource Power Manager state and voter.
2661 */
2662 public abstract Map<String, ? extends Timer> getRpmStats();
2663 /**
2664 * Returns Timers tracking the screen-off time of each Resource Power Manager state and voter.
2665 */
2666 public abstract Map<String, ? extends Timer> getScreenOffRpmStats();
2667
2668
James Carr2dd7e5e2016-07-20 18:48:39 -07002669 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2670
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002671 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2672
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002673 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 long days = seconds / (60 * 60 * 24);
2675 if (days != 0) {
2676 out.append(days);
2677 out.append("d ");
2678 }
2679 long used = days * 60 * 60 * 24;
2680
2681 long hours = (seconds - used) / (60 * 60);
2682 if (hours != 0 || used != 0) {
2683 out.append(hours);
2684 out.append("h ");
2685 }
2686 used += hours * 60 * 60;
2687
2688 long mins = (seconds-used) / 60;
2689 if (mins != 0 || used != 0) {
2690 out.append(mins);
2691 out.append("m ");
2692 }
2693 used += mins * 60;
2694
2695 if (seconds != 0 || used != 0) {
2696 out.append(seconds-used);
2697 out.append("s ");
2698 }
2699 }
2700
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002701 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002703 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 sb.append(time - (sec * 1000));
2705 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 }
2707
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002708 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002709 long sec = time / 1000;
2710 formatTimeRaw(sb, sec);
2711 sb.append(time - (sec * 1000));
2712 sb.append("ms");
2713 }
2714
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002715 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002717 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 }
2719 float perc = ((float)num) / ((float)den) * 100;
2720 mFormatBuilder.setLength(0);
2721 mFormatter.format("%.1f%%", perc);
2722 return mFormatBuilder.toString();
2723 }
2724
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002725 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002726 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07002727
Evan Millar22ac0432009-03-31 11:33:18 -07002728 if (bytes < BYTES_PER_KB) {
2729 return bytes + "B";
2730 } else if (bytes < BYTES_PER_MB) {
2731 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2732 return mFormatBuilder.toString();
2733 } else if (bytes < BYTES_PER_GB){
2734 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2735 return mFormatBuilder.toString();
2736 } else {
2737 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2738 return mFormatBuilder.toString();
2739 }
2740 }
2741
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002742 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002743 if (timer != null) {
2744 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002745 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002746 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2747 return totalTimeMillis;
2748 }
2749 return 0;
2750 }
2751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 /**
2753 *
2754 * @param sb a StringBuilder object.
2755 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002756 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002758 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 * @param linePrefix a String to be prepended to each line of output.
2760 * @return the line prefix
2761 */
2762 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002763 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002766 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002767
Evan Millarc64edde2009-04-18 12:26:32 -07002768 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 if (totalTimeMillis != 0) {
2770 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002771 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002772 if (name != null) {
2773 sb.append(name);
2774 sb.append(' ');
2775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 sb.append('(');
2777 sb.append(count);
2778 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002779 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2780 if (maxDurationMs >= 0) {
2781 sb.append(" max=");
2782 sb.append(maxDurationMs);
2783 }
Bookatz506a8182017-05-01 14:18:42 -07002784 // Put actual time if it is available and different from totalTimeMillis.
2785 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
2786 if (totalDurMs > totalTimeMillis) {
2787 sb.append(" actual=");
2788 sb.append(totalDurMs);
2789 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07002790 if (timer.isRunningLocked()) {
2791 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/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 }
2800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 return ", ";
2802 }
2803 }
2804 return linePrefix;
2805 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002806
2807 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07002808 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002809 *
2810 * @param pw a PrintWriter object to print to.
2811 * @param sb a StringBuilder object.
2812 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08002813 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002814 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2815 * @param prefix a String to be prepended to each line of output.
2816 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002817 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002818 */
2819 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07002820 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002821 if (timer != null) {
2822 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07002823 final long totalTimeMs = (timer.getTotalTimeLocked(
2824 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002825 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002826 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002827 sb.setLength(0);
2828 sb.append(prefix);
2829 sb.append(" ");
2830 sb.append(type);
2831 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002832 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002833 sb.append("realtime (");
2834 sb.append(count);
2835 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002836 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
2837 if (maxDurationMs >= 0) {
2838 sb.append(" max=");
2839 sb.append(maxDurationMs);
2840 }
2841 if (timer.isRunningLocked()) {
2842 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
2843 if (currentMs >= 0) {
2844 sb.append(" (running for ");
2845 sb.append(currentMs);
2846 sb.append("ms)");
2847 } else {
2848 sb.append(" (running)");
2849 }
2850 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002851 pw.println(sb.toString());
2852 return true;
2853 }
2854 }
2855 return false;
2856 }
Bookatzc8c44962017-05-11 12:12:54 -07002857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 /**
2859 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07002860 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 * @param sb a StringBuilder object.
2862 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002863 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002865 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 * @param linePrefix a String to be prepended to each line of output.
2867 * @return the line prefix
2868 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002869 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2870 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 long totalTimeMicros = 0;
2872 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07002873 long max = 0;
2874 long current = 0;
2875 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002877 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07002878 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002879 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2880 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07002881 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 }
2883 sb.append(linePrefix);
2884 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2885 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002886 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002888 sb.append(',');
2889 sb.append(current);
2890 sb.append(',');
2891 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07002892 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
2893 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
2894 // totalDuration independent of totalTimeMicros (since they are not pooled).
2895 if (name != null) {
2896 sb.append(',');
2897 sb.append(totalDuration);
2898 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 return ",";
2900 }
Bookatz506a8182017-05-01 14:18:42 -07002901
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002902 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
2903 String type) {
2904 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2905 pw.print(',');
2906 pw.print(uid);
2907 pw.print(',');
2908 pw.print(category);
2909 pw.print(',');
2910 pw.print(type);
2911 }
2912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 /**
2914 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07002915 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 * @param pw the PageWriter to dump log to
2917 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2918 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2919 * @param args type-dependent data arguments
2920 */
Bookatzc8c44962017-05-11 12:12:54 -07002921 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002923 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002924 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002925 pw.print(',');
2926 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002928 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002930
2931 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002932 * Dump a given timer stat for terse checkin mode.
2933 *
2934 * @param pw the PageWriter to dump log to
2935 * @param uid the UID to log
2936 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2937 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2938 * @param timer a {@link Timer} to dump stats for
2939 * @param rawRealtime the current elapsed realtime of the system in microseconds
2940 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2941 */
2942 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2943 Timer timer, long rawRealtime, int which) {
2944 if (timer != null) {
2945 // Convert from microseconds to milliseconds with rounding
2946 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2947 / 1000;
2948 final int count = timer.getCountLocked(which);
2949 if (totalTime != 0) {
2950 dumpLine(pw, uid, category, type, totalTime, count);
2951 }
2952 }
2953 }
2954
2955 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002956 * Checks if the ControllerActivityCounter has any data worth dumping.
2957 */
2958 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
2959 if (counter == null) {
2960 return false;
2961 }
2962
2963 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
2964 || counter.getRxTimeCounter().getCountLocked(which) != 0
2965 || counter.getPowerCounter().getCountLocked(which) != 0) {
2966 return true;
2967 }
2968
2969 for (LongCounter c : counter.getTxTimeCounters()) {
2970 if (c.getCountLocked(which) != 0) {
2971 return true;
2972 }
2973 }
2974 return false;
2975 }
2976
2977 /**
2978 * Dumps the ControllerActivityCounter if it has any data worth dumping.
2979 * The order of the arguments in the final check in line is:
2980 *
2981 * idle, rx, power, tx...
2982 *
2983 * where tx... is one or more transmit level times.
2984 */
2985 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
2986 String type,
2987 ControllerActivityCounter counter,
2988 int which) {
2989 if (!controllerActivityHasData(counter, which)) {
2990 return;
2991 }
2992
2993 dumpLineHeader(pw, uid, category, type);
2994 pw.print(",");
2995 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
2996 pw.print(",");
2997 pw.print(counter.getRxTimeCounter().getCountLocked(which));
2998 pw.print(",");
2999 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3000 for (LongCounter c : counter.getTxTimeCounters()) {
3001 pw.print(",");
3002 pw.print(c.getCountLocked(which));
3003 }
3004 pw.println();
3005 }
3006
3007 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
3008 String prefix, String controllerName,
3009 ControllerActivityCounter counter,
3010 int which) {
3011 if (controllerActivityHasData(counter, which)) {
3012 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
3013 }
3014 }
3015
3016 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
3017 String controllerName,
3018 ControllerActivityCounter counter, int which) {
3019 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
3020 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
3021 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
3022 long totalTxTimeMs = 0;
3023 for (LongCounter txState : counter.getTxTimeCounters()) {
3024 totalTxTimeMs += txState.getCountLocked(which);
3025 }
3026
3027 final long totalTimeMs = idleTimeMs + rxTimeMs + totalTxTimeMs;
3028
3029 sb.setLength(0);
3030 sb.append(prefix);
3031 sb.append(" ");
3032 sb.append(controllerName);
3033 sb.append(" Idle time: ");
3034 formatTimeMs(sb, idleTimeMs);
3035 sb.append("(");
3036 sb.append(formatRatioLocked(idleTimeMs, totalTimeMs));
3037 sb.append(")");
3038 pw.println(sb.toString());
3039
3040 sb.setLength(0);
3041 sb.append(prefix);
3042 sb.append(" ");
3043 sb.append(controllerName);
3044 sb.append(" Rx time: ");
3045 formatTimeMs(sb, rxTimeMs);
3046 sb.append("(");
3047 sb.append(formatRatioLocked(rxTimeMs, totalTimeMs));
3048 sb.append(")");
3049 pw.println(sb.toString());
3050
3051 sb.setLength(0);
3052 sb.append(prefix);
3053 sb.append(" ");
3054 sb.append(controllerName);
3055 sb.append(" Tx time: ");
3056 formatTimeMs(sb, totalTxTimeMs);
3057 sb.append("(");
3058 sb.append(formatRatioLocked(totalTxTimeMs, totalTimeMs));
3059 sb.append(")");
3060 pw.println(sb.toString());
3061
3062 final int numTxLvls = counter.getTxTimeCounters().length;
3063 if (numTxLvls > 1) {
3064 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3065 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3066 sb.setLength(0);
3067 sb.append(prefix);
3068 sb.append(" [");
3069 sb.append(lvl);
3070 sb.append("] ");
3071 formatTimeMs(sb, txLvlTimeMs);
3072 sb.append("(");
3073 sb.append(formatRatioLocked(txLvlTimeMs, totalTxTimeMs));
3074 sb.append(")");
3075 pw.println(sb.toString());
3076 }
3077 }
3078
3079 sb.setLength(0);
3080 sb.append(prefix);
3081 sb.append(" ");
3082 sb.append(controllerName);
3083 sb.append(" Power drain: ").append(
3084 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
3085 sb.append("mAh");
3086 pw.println(sb.toString());
3087 }
3088
3089 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003090 * Temporary for settings.
3091 */
3092 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3093 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3094 }
3095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096 /**
3097 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003098 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003101 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3102 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3104 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07003105 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3108 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003109 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3110 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3111 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 final long totalRealtime = computeRealtime(rawRealtime, which);
3113 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003114 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003115 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003116 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003117 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3118 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003119 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003120 rawRealtime, which);
3121 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3122 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003123 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003124 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003125 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003126 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003127 final long dischargeCount = getDischargeCoulombCounter().getCountLocked(which);
3128 final long dischargeScreenOffCount = getDischargeScreenOffCoulombCounter()
3129 .getCountLocked(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003130
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003131 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003132
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003133 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003134 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003135
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003136 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003138 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003139 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003140 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003141 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003142 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003143 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003144 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003145 getEstimatedBatteryCapacity(),
3146 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity());
Adam Lesinski67c134f2016-06-10 15:15:08 -07003147
Bookatzc8c44962017-05-11 12:12:54 -07003148
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003149 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003150 long fullWakeLockTimeTotal = 0;
3151 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003152
Evan Millar22ac0432009-03-31 11:33:18 -07003153 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003154 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003155
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003156 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3157 = u.getWakelockStats();
3158 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3159 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003160
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003161 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3162 if (fullWakeTimer != null) {
3163 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3164 which);
3165 }
3166
3167 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3168 if (partialWakeTimer != null) {
3169 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3170 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003171 }
3172 }
3173 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003174
3175 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003176 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3177 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3178 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3179 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3180 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3181 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3182 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3183 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003184 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3185 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003186 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3187 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003188 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3189 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003190
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003191 // Dump Modem controller stats
3192 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3193 getModemControllerActivity(), which);
3194
Adam Lesinskie283d332015-04-16 12:29:25 -07003195 // Dump Wifi controller stats
3196 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3197 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003198 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003199 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003200
3201 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3202 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003203
3204 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003205 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3206 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 // Dump misc stats
3209 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003210 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003211 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003212 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003213 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003214 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003215 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3216 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003217 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003218 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3219 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3220 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3221 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003222 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003223
Dianne Hackborn617f8772009-03-31 15:04:46 -07003224 // Dump screen brightness stats
3225 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3226 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003227 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003228 }
3229 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003230
Dianne Hackborn627bba72009-03-24 22:32:56 -07003231 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003232 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3233 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003234 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003235 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003236 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003237 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003238 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003239 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003240 args[i] = getPhoneSignalStrengthCount(i, which);
3241 }
3242 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003243
Dianne Hackborn627bba72009-03-24 22:32:56 -07003244 // Dump network type stats
3245 args = new Object[NUM_DATA_CONNECTION_TYPES];
3246 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003247 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003248 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003249 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3250 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3251 args[i] = getPhoneDataConnectionCount(i, which);
3252 }
3253 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003254
3255 // Dump wifi state stats
3256 args = new Object[NUM_WIFI_STATES];
3257 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003258 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003259 }
3260 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3261 for (int i=0; i<NUM_WIFI_STATES; i++) {
3262 args[i] = getWifiStateCount(i, which);
3263 }
3264 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3265
Dianne Hackborn3251b902014-06-20 14:40:53 -07003266 // Dump wifi suppl state stats
3267 args = new Object[NUM_WIFI_SUPPL_STATES];
3268 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3269 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3270 }
3271 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3272 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3273 args[i] = getWifiSupplStateCount(i, which);
3274 }
3275 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3276
3277 // Dump wifi signal strength stats
3278 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3279 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3280 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3281 }
3282 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3283 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3284 args[i] = getWifiSignalStrengthCount(i, which);
3285 }
3286 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3287
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003288 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003289 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003290 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003291 }
Bookatzc8c44962017-05-11 12:12:54 -07003292
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003293 if (which == STATS_SINCE_UNPLUGGED) {
3294 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3295 getDischargeStartLevel()-getDischargeCurrentLevel(),
3296 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003297 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
3298 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003299 } else {
3300 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3301 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003302 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003303 getDischargeAmountScreenOffSinceCharge(),
3304 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003305 }
Bookatzc8c44962017-05-11 12:12:54 -07003306
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003307 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003308 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003309 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003310 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003311 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003312 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003313 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3314 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003315 }
Evan Millarc64edde2009-04-18 12:26:32 -07003316 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003317 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003318 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003319 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3320 // Not doing the regular wake lock formatting to remain compatible
3321 // with the old checkin format.
3322 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3323 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003324 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003325 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003326 }
3327 }
Evan Millarc64edde2009-04-18 12:26:32 -07003328 }
Bookatzc8c44962017-05-11 12:12:54 -07003329
Bookatz50df7112017-08-04 14:53:26 -07003330 final Map<String, ? extends Timer> rpmStats = getRpmStats();
3331 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
3332 if (rpmStats.size() > 0) {
3333 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
3334 sb.setLength(0);
3335 Timer totalTimer = ent.getValue();
3336 long timeMs = (totalTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3337 int count = totalTimer.getCountLocked(which);
3338 Timer screenOffTimer = screenOffRpmStats.get(ent.getKey());
3339 long screenOffTimeMs = screenOffTimer != null
3340 ? (screenOffTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3341 int screenOffCount = screenOffTimer != null
3342 ? screenOffTimer.getCountLocked(which) : 0;
Bookatz82b341172017-09-07 19:06:08 -07003343 if (SCREEN_OFF_RPM_STATS_ENABLED) {
3344 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3345 "\"" + ent.getKey() + "\"", timeMs, count, screenOffTimeMs,
3346 screenOffCount);
3347 } else {
3348 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3349 "\"" + ent.getKey() + "\"", timeMs, count);
3350 }
Bookatz50df7112017-08-04 14:53:26 -07003351 }
3352 }
3353
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003354 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003355 helper.create(this);
3356 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003357 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003358 if (sippers != null && sippers.size() > 0) {
3359 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3360 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003361 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003362 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3363 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
3364 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003365 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003366 int uid = 0;
3367 String label;
3368 switch (bs.drainType) {
3369 case IDLE:
3370 label="idle";
3371 break;
3372 case CELL:
3373 label="cell";
3374 break;
3375 case PHONE:
3376 label="phone";
3377 break;
3378 case WIFI:
3379 label="wifi";
3380 break;
3381 case BLUETOOTH:
3382 label="blue";
3383 break;
3384 case SCREEN:
3385 label="scrn";
3386 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003387 case FLASHLIGHT:
3388 label="flashlight";
3389 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003390 case APP:
3391 uid = bs.uidObj.getUid();
3392 label = "uid";
3393 break;
3394 case USER:
3395 uid = UserHandle.getUid(bs.userId, 0);
3396 label = "user";
3397 break;
3398 case UNACCOUNTED:
3399 label = "unacc";
3400 break;
3401 case OVERCOUNTED:
3402 label = "over";
3403 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003404 case CAMERA:
3405 label = "camera";
3406 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003407 default:
3408 label = "???";
3409 }
3410 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07003411 BatteryStatsHelper.makemAh(bs.totalPowerMah),
3412 bs.shouldHide ? 1 : 0,
3413 BatteryStatsHelper.makemAh(bs.screenPowerMah),
3414 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003415 }
3416 }
3417
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003418 final long[] cpuFreqs = getCpuFreqs();
3419 if (cpuFreqs != null) {
3420 sb.setLength(0);
3421 for (int i = 0; i < cpuFreqs.length; ++i) {
3422 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3423 }
3424 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3425 }
3426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 for (int iu = 0; iu < NU; iu++) {
3428 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003429 if (reqUid >= 0 && uid != reqUid) {
3430 continue;
3431 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003432 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003435 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3436 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3437 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3438 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3439 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3440 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3441 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3442 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003443 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003444 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3445 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003446 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003447 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3448 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003449 // Background data transfers
3450 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3451 which);
3452 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3453 which);
3454 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3455 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3456 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3457 which);
3458 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3459 which);
3460 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3461 which);
3462 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3463 which);
3464
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003465 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3466 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003467 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003468 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3469 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3470 || wifiBytesBgTx > 0
3471 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3472 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003473 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3474 wifiBytesRx, wifiBytesTx,
3475 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003476 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003477 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003478 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3479 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3480 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3481 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003482 }
3483
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003484 // Dump modem controller data, per UID.
3485 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3486 u.getModemControllerActivity(), which);
3487
3488 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003489 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3490 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3491 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003492 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3493 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003494 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3495 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3496 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003497 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003498 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003499 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3500 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003501 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3502 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003503 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003504 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003507 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3508 u.getWifiControllerActivity(), which);
3509
Bookatz867c0d72017-03-07 18:23:42 -08003510 final Timer bleTimer = u.getBluetoothScanTimer();
3511 if (bleTimer != null) {
3512 // Convert from microseconds to milliseconds with rounding
3513 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3514 / 1000;
3515 if (totalTime != 0) {
3516 final int count = bleTimer.getCountLocked(which);
3517 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3518 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003519 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3520 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3521 final long actualTimeBg = bleTimerBg != null ?
3522 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003523 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003524 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3525 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003526 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3527 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3528 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3529 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3530 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
3531 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3532 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
3533 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3534 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
3535 final Timer unoptimizedScanTimerBg =
3536 u.getBluetoothUnoptimizedScanBackgroundTimer();
3537 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
3538 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3539 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
3540 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3541
Bookatz867c0d72017-03-07 18:23:42 -08003542 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07003543 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
3544 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
3545 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08003546 }
3547 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003548
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003549 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3550 u.getBluetoothControllerActivity(), which);
3551
Dianne Hackborn617f8772009-03-31 15:04:46 -07003552 if (u.hasUserActivity()) {
3553 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3554 boolean hasData = false;
3555 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3556 int val = u.getUserActivityCount(i, which);
3557 args[i] = val;
3558 if (val != 0) hasData = true;
3559 }
3560 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003561 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003562 }
3563 }
Bookatzc8c44962017-05-11 12:12:54 -07003564
3565 if (u.getAggregatedPartialWakelockTimer() != null) {
3566 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07003567 // Times are since reset (regardless of 'which')
3568 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07003569 final Timer bgTimer = timer.getSubTimer();
3570 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003571 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07003572 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
3573 }
3574
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003575 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3576 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3577 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3578 String linePrefix = "";
3579 sb.setLength(0);
3580 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3581 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003582 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3583 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003584 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003585 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
3586 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003587 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3588 rawRealtime, "w", which, linePrefix);
3589
3590 // Only log if we had at lease one wakelock...
3591 if (sb.length() > 0) {
3592 String name = wakelocks.keyAt(iw);
3593 if (name.indexOf(',') >= 0) {
3594 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003595 }
Yi Jin02483362017-08-04 11:30:44 -07003596 if (name.indexOf('\n') >= 0) {
3597 name = name.replace('\n', '_');
3598 }
3599 if (name.indexOf('\r') >= 0) {
3600 name = name.replace('\r', '_');
3601 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003602 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003603 }
3604 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003605
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003606 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3607 for (int isy=syncs.size()-1; isy>=0; isy--) {
3608 final Timer timer = syncs.valueAt(isy);
3609 // Convert from microseconds to milliseconds with rounding
3610 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3611 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07003612 final Timer bgTimer = timer.getSubTimer();
3613 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003614 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07003615 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003616 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003617 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07003618 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003619 }
3620 }
3621
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003622 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3623 for (int ij=jobs.size()-1; ij>=0; ij--) {
3624 final Timer timer = jobs.valueAt(ij);
3625 // Convert from microseconds to milliseconds with rounding
3626 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3627 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07003628 final Timer bgTimer = timer.getSubTimer();
3629 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003630 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07003631 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003632 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003633 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07003634 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003635 }
3636 }
3637
Dianne Hackborn94326cb2017-06-28 16:17:20 -07003638 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
3639 for (int ic=completions.size()-1; ic>=0; ic--) {
3640 SparseIntArray types = completions.valueAt(ic);
3641 if (types != null) {
3642 dumpLine(pw, uid, category, JOB_COMPLETION_DATA,
3643 "\"" + completions.keyAt(ic) + "\"",
3644 types.get(JobParameters.REASON_CANCELED, 0),
3645 types.get(JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, 0),
3646 types.get(JobParameters.REASON_PREEMPT, 0),
3647 types.get(JobParameters.REASON_TIMEOUT, 0),
3648 types.get(JobParameters.REASON_DEVICE_IDLE, 0));
3649 }
3650 }
3651
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003652 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3653 rawRealtime, which);
3654 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3655 rawRealtime, which);
3656 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3657 rawRealtime, which);
3658 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3659 rawRealtime, which);
3660
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003661 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3662 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003663 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003664 final Uid.Sensor se = sensors.valueAt(ise);
3665 final int sensorNumber = sensors.keyAt(ise);
3666 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003667 if (timer != null) {
3668 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003669 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3670 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003671 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08003672 final int count = timer.getCountLocked(which);
3673 final Timer bgTimer = se.getSensorBackgroundTime();
3674 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003675 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3676 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
3677 final long bgActualTime = bgTimer != null ?
3678 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3679 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
3680 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003682 }
3683 }
3684
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003685 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3686 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003687
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07003688 dumpTimer(pw, uid, category, FOREGROUND_ACTIVITY_DATA, u.getForegroundActivityTimer(),
3689 rawRealtime, which);
3690
3691 dumpTimer(pw, uid, category, FOREGROUND_SERVICE_DATA, u.getForegroundServiceTimer(),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003692 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003693
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003694 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003695 long totalStateTime = 0;
3696 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003697 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3698 totalStateTime += time;
3699 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003700 }
3701 if (totalStateTime > 0) {
3702 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3703 }
3704
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003705 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3706 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003707 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003708 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003709 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003710 }
3711
Sudheer Shankaa87245d2017-08-10 12:02:31 -07003712 // If the cpuFreqs is null, then don't bother checking for cpu freq times.
3713 if (cpuFreqs != null) {
3714 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
3715 // If total cpuFreqTimes is null, then we don't need to check for
3716 // screenOffCpuFreqTimes.
3717 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
3718 sb.setLength(0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003719 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
Sudheer Shankaa87245d2017-08-10 12:02:31 -07003720 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003721 }
Sudheer Shankaa87245d2017-08-10 12:02:31 -07003722 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
3723 if (screenOffCpuFreqTimeMs != null) {
3724 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
3725 sb.append("," + screenOffCpuFreqTimeMs[i]);
3726 }
3727 } else {
3728 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
3729 sb.append(",0");
3730 }
3731 }
3732 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
3733 cpuFreqTimeMs.length, sb.toString());
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003734 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003735 }
3736
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003737 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3738 = u.getProcessStats();
3739 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3740 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003741
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003742 final long userMillis = ps.getUserTime(which);
3743 final long systemMillis = ps.getSystemTime(which);
3744 final long foregroundMillis = ps.getForegroundTime(which);
3745 final int starts = ps.getStarts(which);
3746 final int numCrashes = ps.getNumCrashes(which);
3747 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003748
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003749 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3750 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003751 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
3752 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003753 }
3754 }
3755
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003756 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3757 = u.getPackageStats();
3758 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3759 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3760 int wakeups = 0;
3761 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3762 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07003763 int count = alarms.valueAt(iwa).getCountLocked(which);
3764 wakeups += count;
3765 String name = alarms.keyAt(iwa).replace(',', '_');
3766 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003767 }
3768 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3769 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3770 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3771 final long startTime = ss.getStartTime(batteryUptime, which);
3772 final int starts = ss.getStarts(which);
3773 final int launches = ss.getLaunches(which);
3774 if (startTime != 0 || starts != 0 || launches != 0) {
3775 dumpLine(pw, uid, category, APK_DATA,
3776 wakeups, // wakeup alarms
3777 packageStats.keyAt(ipkg), // Apk
3778 serviceStats.keyAt(isvc), // service
3779 startTime / 1000, // time spent started, in ms
3780 starts,
3781 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003782 }
3783 }
3784 }
3785 }
3786 }
3787
Dianne Hackborn81038902012-11-26 17:04:09 -08003788 static final class TimerEntry {
3789 final String mName;
3790 final int mId;
3791 final BatteryStats.Timer mTimer;
3792 final long mTime;
3793 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3794 mName = name;
3795 mId = id;
3796 mTimer = timer;
3797 mTime = time;
3798 }
3799 }
3800
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003801 private void printmAh(PrintWriter printer, double power) {
3802 printer.print(BatteryStatsHelper.makemAh(power));
3803 }
3804
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003805 private void printmAh(StringBuilder sb, double power) {
3806 sb.append(BatteryStatsHelper.makemAh(power));
3807 }
3808
Dianne Hackbornd953c532014-08-16 18:17:38 -07003809 /**
3810 * Temporary for settings.
3811 */
3812 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3813 int reqUid) {
3814 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3815 }
3816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003817 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003818 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003819 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003820 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3821 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07003822 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824
3825 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3826 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3827 final long totalRealtime = computeRealtime(rawRealtime, which);
3828 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003829 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3830 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3831 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003832 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3833 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003834
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003835 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003836
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003837 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003838 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003840 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
3841 if (estimatedBatteryCapacity > 0) {
3842 sb.setLength(0);
3843 sb.append(prefix);
3844 sb.append(" Estimated battery capacity: ");
3845 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
3846 sb.append(" mAh");
3847 pw.println(sb.toString());
3848 }
3849
Jocelyn Dangc627d102017-04-14 13:15:14 -07003850 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
3851 if (minLearnedBatteryCapacity > 0) {
3852 sb.setLength(0);
3853 sb.append(prefix);
3854 sb.append(" Min learned battery capacity: ");
3855 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
3856 sb.append(" mAh");
3857 pw.println(sb.toString());
3858 }
3859 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
3860 if (maxLearnedBatteryCapacity > 0) {
3861 sb.setLength(0);
3862 sb.append(prefix);
3863 sb.append(" Max learned battery capacity: ");
3864 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
3865 sb.append(" mAh");
3866 pw.println(sb.toString());
3867 }
3868
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003869 sb.setLength(0);
3870 sb.append(prefix);
3871 sb.append(" Time on battery: ");
3872 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3873 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3874 sb.append(") realtime, ");
3875 formatTimeMs(sb, whichBatteryUptime / 1000);
3876 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3877 sb.append(") uptime");
3878 pw.println(sb.toString());
3879 sb.setLength(0);
3880 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003881 sb.append(" Time on battery screen off: ");
3882 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3883 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3884 sb.append(") realtime, ");
3885 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3886 sb.append("(");
3887 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3888 sb.append(") uptime");
3889 pw.println(sb.toString());
3890 sb.setLength(0);
3891 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003892 sb.append(" Total run time: ");
3893 formatTimeMs(sb, totalRealtime / 1000);
3894 sb.append("realtime, ");
3895 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003896 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003897 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003898 if (batteryTimeRemaining >= 0) {
3899 sb.setLength(0);
3900 sb.append(prefix);
3901 sb.append(" Battery time remaining: ");
3902 formatTimeMs(sb, batteryTimeRemaining / 1000);
3903 pw.println(sb.toString());
3904 }
3905 if (chargeTimeRemaining >= 0) {
3906 sb.setLength(0);
3907 sb.append(prefix);
3908 sb.append(" Charge time remaining: ");
3909 formatTimeMs(sb, chargeTimeRemaining / 1000);
3910 pw.println(sb.toString());
3911 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003912
3913 final LongCounter dischargeCounter = getDischargeCoulombCounter();
3914 final long dischargeCount = dischargeCounter.getCountLocked(which);
3915 if (dischargeCount >= 0) {
3916 sb.setLength(0);
3917 sb.append(prefix);
3918 sb.append(" Discharge: ");
3919 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
3920 sb.append(" mAh");
3921 pw.println(sb.toString());
3922 }
3923
3924 final LongCounter dischargeScreenOffCounter = getDischargeScreenOffCoulombCounter();
3925 final long dischargeScreenOffCount = dischargeScreenOffCounter.getCountLocked(which);
3926 if (dischargeScreenOffCount >= 0) {
3927 sb.setLength(0);
3928 sb.append(prefix);
3929 sb.append(" Screen off discharge: ");
3930 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
3931 sb.append(" mAh");
3932 pw.println(sb.toString());
3933 }
3934
3935 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
3936 if (dischargeScreenOnCount >= 0) {
3937 sb.setLength(0);
3938 sb.append(prefix);
3939 sb.append(" Screen on discharge: ");
3940 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
3941 sb.append(" mAh");
3942 pw.println(sb.toString());
3943 }
3944
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003945 pw.print(" Start clock time: ");
3946 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3947
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003948 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003949 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003950 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003951 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3952 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003953 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003954 rawRealtime, which);
3955 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3956 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003957 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003958 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003959 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3960 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3961 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003962 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003963 sb.append(prefix);
3964 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3965 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003966 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003967 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3968 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003969 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003970 pw.println(sb.toString());
3971 sb.setLength(0);
3972 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003973 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003974 boolean didOne = false;
3975 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003976 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003977 if (time == 0) {
3978 continue;
3979 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003980 sb.append("\n ");
3981 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003982 didOne = true;
3983 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3984 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003985 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003986 sb.append("(");
3987 sb.append(formatRatioLocked(time, screenOnTime));
3988 sb.append(")");
3989 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003990 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003991 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003992 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003993 sb.setLength(0);
3994 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003995 sb.append(" Power save mode enabled: ");
3996 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003997 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003998 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003999 sb.append(")");
4000 pw.println(sb.toString());
4001 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004002 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004003 sb.setLength(0);
4004 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004005 sb.append(" Device light idling: ");
4006 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004007 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004008 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
4009 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004010 sb.append("x");
4011 pw.println(sb.toString());
4012 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004013 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004014 sb.setLength(0);
4015 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004016 sb.append(" Idle mode light time: ");
4017 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004018 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004019 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
4020 sb.append(") ");
4021 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004022 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004023 sb.append(" -- longest ");
4024 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
4025 pw.println(sb.toString());
4026 }
4027 if (deviceIdlingTime != 0) {
4028 sb.setLength(0);
4029 sb.append(prefix);
4030 sb.append(" Device full idling: ");
4031 formatTimeMs(sb, deviceIdlingTime / 1000);
4032 sb.append("(");
4033 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004034 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004035 sb.append("x");
4036 pw.println(sb.toString());
4037 }
4038 if (deviceIdleModeFullTime != 0) {
4039 sb.setLength(0);
4040 sb.append(prefix);
4041 sb.append(" Idle mode full time: ");
4042 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
4043 sb.append("(");
4044 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
4045 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004046 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004047 sb.append("x");
4048 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004049 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004050 pw.println(sb.toString());
4051 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004052 if (phoneOnTime != 0) {
4053 sb.setLength(0);
4054 sb.append(prefix);
4055 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
4056 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004057 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004058 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004059 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004060 if (connChanges != 0) {
4061 pw.print(prefix);
4062 pw.print(" Connectivity changes: "); pw.println(connChanges);
4063 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004064
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004065 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07004066 long fullWakeLockTimeTotalMicros = 0;
4067 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08004068
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004069 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08004070
Evan Millar22ac0432009-03-31 11:33:18 -07004071 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004072 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004073
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004074 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4075 = u.getWakelockStats();
4076 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4077 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07004078
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004079 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
4080 if (fullWakeTimer != null) {
4081 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
4082 rawRealtime, which);
4083 }
4084
4085 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4086 if (partialWakeTimer != null) {
4087 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
4088 rawRealtime, which);
4089 if (totalTimeMicros > 0) {
4090 if (reqUid < 0) {
4091 // Only show the ordered list of all wake
4092 // locks if the caller is not asking for data
4093 // about a specific uid.
4094 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
4095 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08004096 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004097 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07004098 }
4099 }
4100 }
4101 }
Bookatzc8c44962017-05-11 12:12:54 -07004102
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004103 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4104 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4105 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4106 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4107 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4108 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4109 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4110 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004111 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4112 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004113
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004114 if (fullWakeLockTimeTotalMicros != 0) {
4115 sb.setLength(0);
4116 sb.append(prefix);
4117 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4118 (fullWakeLockTimeTotalMicros + 500) / 1000);
4119 pw.println(sb.toString());
4120 }
4121
4122 if (partialWakeLockTimeTotalMicros != 0) {
4123 sb.setLength(0);
4124 sb.append(prefix);
4125 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4126 (partialWakeLockTimeTotalMicros + 500) / 1000);
4127 pw.println(sb.toString());
4128 }
4129
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004130 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004131 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
4132 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
4133 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
4134 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004135 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004136 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004137 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004138 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08004139 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004140 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004141 if (time == 0) {
4142 continue;
4143 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004144 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004145 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004146 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08004147 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004148 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004149 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004150 sb.append("(");
4151 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004152 sb.append(") ");
4153 sb.append(getPhoneSignalStrengthCount(i, which));
4154 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004155 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004156 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004157 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004158
4159 sb.setLength(0);
4160 sb.append(prefix);
4161 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004162 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004163 pw.println(sb.toString());
4164
Dianne Hackborn627bba72009-03-24 22:32:56 -07004165 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004166 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004167 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004168 didOne = false;
4169 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004170 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004171 if (time == 0) {
4172 continue;
4173 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004174 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004175 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004176 didOne = true;
4177 sb.append(DATA_CONNECTION_NAMES[i]);
4178 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004179 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004180 sb.append("(");
4181 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004182 sb.append(") ");
4183 sb.append(getPhoneDataConnectionCount(i, which));
4184 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004185 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004186 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004187 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004188
4189 sb.setLength(0);
4190 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004191 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004192 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004193 formatTimeMs(sb, mobileActiveTime / 1000);
4194 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4195 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
4196 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004197 pw.println(sb.toString());
4198
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004199 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
4200 if (mobileActiveUnknownTime != 0) {
4201 sb.setLength(0);
4202 sb.append(prefix);
4203 sb.append(" Mobile radio active unknown time: ");
4204 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
4205 sb.append("(");
4206 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
4207 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
4208 sb.append("x");
4209 pw.println(sb.toString());
4210 }
4211
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004212 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
4213 if (mobileActiveAdjustedTime != 0) {
4214 sb.setLength(0);
4215 sb.append(prefix);
4216 sb.append(" Mobile radio active adjusted time: ");
4217 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
4218 sb.append("(");
4219 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
4220 sb.append(")");
4221 pw.println(sb.toString());
4222 }
4223
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004224 printControllerActivity(pw, sb, prefix, "Radio", getModemControllerActivity(), which);
4225
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004226 pw.print(prefix);
4227 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
4228 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
4229 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
4230 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004231 sb.setLength(0);
4232 sb.append(prefix);
4233 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
4234 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
4235 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
4236 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004237 sb.append(")");
4238 pw.println(sb.toString());
4239
4240 sb.setLength(0);
4241 sb.append(prefix);
4242 sb.append(" Wifi states:");
4243 didOne = false;
4244 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004245 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004246 if (time == 0) {
4247 continue;
4248 }
4249 sb.append("\n ");
4250 didOne = true;
4251 sb.append(WIFI_STATE_NAMES[i]);
4252 sb.append(" ");
4253 formatTimeMs(sb, time/1000);
4254 sb.append("(");
4255 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4256 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004257 sb.append(getWifiStateCount(i, which));
4258 sb.append("x");
4259 }
4260 if (!didOne) sb.append(" (no activity)");
4261 pw.println(sb.toString());
4262
4263 sb.setLength(0);
4264 sb.append(prefix);
4265 sb.append(" Wifi supplicant states:");
4266 didOne = false;
4267 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4268 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4269 if (time == 0) {
4270 continue;
4271 }
4272 sb.append("\n ");
4273 didOne = true;
4274 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4275 sb.append(" ");
4276 formatTimeMs(sb, time/1000);
4277 sb.append("(");
4278 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4279 sb.append(") ");
4280 sb.append(getWifiSupplStateCount(i, which));
4281 sb.append("x");
4282 }
4283 if (!didOne) sb.append(" (no activity)");
4284 pw.println(sb.toString());
4285
4286 sb.setLength(0);
4287 sb.append(prefix);
4288 sb.append(" Wifi signal levels:");
4289 didOne = false;
4290 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4291 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4292 if (time == 0) {
4293 continue;
4294 }
4295 sb.append("\n ");
4296 sb.append(prefix);
4297 didOne = true;
4298 sb.append("level(");
4299 sb.append(i);
4300 sb.append(") ");
4301 formatTimeMs(sb, time/1000);
4302 sb.append("(");
4303 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4304 sb.append(") ");
4305 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004306 sb.append("x");
4307 }
4308 if (!didOne) sb.append(" (no activity)");
4309 pw.println(sb.toString());
4310
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004311 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004312
Adam Lesinski50e47602015-12-04 17:04:54 -08004313 pw.print(prefix);
4314 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4315 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4316
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004317 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4318 sb.setLength(0);
4319 sb.append(prefix);
4320 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4321 pw.println(sb.toString());
4322
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004323 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4324 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004325
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004326 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004327
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004328 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004329 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004330 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004331 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004332 pw.println(getDischargeStartLevel());
4333 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4334 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004335 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004336 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004337 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004338 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004339 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004340 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004341 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004342 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4343 pw.println(getDischargeAmountScreenOn());
4344 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4345 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004346 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004347 } else {
4348 pw.print(prefix); pw.println(" Device battery use since last full charge");
4349 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
4350 pw.println(getLowDischargeAmountSinceCharge());
4351 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
4352 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004353 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4354 pw.println(getDischargeAmountScreenOnSinceCharge());
4355 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4356 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004357 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004358 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004359
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004360 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004361 helper.create(this);
4362 helper.refreshStats(which, UserHandle.USER_ALL);
4363 List<BatterySipper> sippers = helper.getUsageList();
4364 if (sippers != null && sippers.size() > 0) {
4365 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4366 pw.print(prefix); pw.print(" Capacity: ");
4367 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004368 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004369 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4370 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4371 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4372 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004373 pw.println();
4374 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004375 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004376 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004377 switch (bs.drainType) {
4378 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004379 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004380 break;
4381 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004382 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004383 break;
4384 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004385 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004386 break;
4387 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004388 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004389 break;
4390 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004391 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004392 break;
4393 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004394 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004395 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004396 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004397 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004398 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004399 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004400 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004401 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004402 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004403 break;
4404 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004405 pw.print(" User "); pw.print(bs.userId);
4406 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004407 break;
4408 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004409 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004410 break;
4411 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004412 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004413 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004414 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004415 pw.print(" Camera: ");
4416 break;
4417 default:
4418 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004419 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004420 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004421 printmAh(pw, bs.totalPowerMah);
4422
Adam Lesinski57123002015-06-12 16:12:07 -07004423 if (bs.usagePowerMah != bs.totalPowerMah) {
4424 // If the usage (generic power) isn't the whole amount, we list out
4425 // what components are involved in the calculation.
4426
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004427 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004428 if (bs.usagePowerMah != 0) {
4429 pw.print(" usage=");
4430 printmAh(pw, bs.usagePowerMah);
4431 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004432 if (bs.cpuPowerMah != 0) {
4433 pw.print(" cpu=");
4434 printmAh(pw, bs.cpuPowerMah);
4435 }
4436 if (bs.wakeLockPowerMah != 0) {
4437 pw.print(" wake=");
4438 printmAh(pw, bs.wakeLockPowerMah);
4439 }
4440 if (bs.mobileRadioPowerMah != 0) {
4441 pw.print(" radio=");
4442 printmAh(pw, bs.mobileRadioPowerMah);
4443 }
4444 if (bs.wifiPowerMah != 0) {
4445 pw.print(" wifi=");
4446 printmAh(pw, bs.wifiPowerMah);
4447 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004448 if (bs.bluetoothPowerMah != 0) {
4449 pw.print(" bt=");
4450 printmAh(pw, bs.bluetoothPowerMah);
4451 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004452 if (bs.gpsPowerMah != 0) {
4453 pw.print(" gps=");
4454 printmAh(pw, bs.gpsPowerMah);
4455 }
4456 if (bs.sensorPowerMah != 0) {
4457 pw.print(" sensor=");
4458 printmAh(pw, bs.sensorPowerMah);
4459 }
4460 if (bs.cameraPowerMah != 0) {
4461 pw.print(" camera=");
4462 printmAh(pw, bs.cameraPowerMah);
4463 }
4464 if (bs.flashlightPowerMah != 0) {
4465 pw.print(" flash=");
4466 printmAh(pw, bs.flashlightPowerMah);
4467 }
4468 pw.print(" )");
4469 }
Bookatz17d7d9d2017-06-08 14:50:46 -07004470
4471 // If there is additional smearing information, include it.
4472 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
4473 pw.print(" Including smearing: ");
4474 printmAh(pw, bs.totalSmearedPowerMah);
4475 pw.print(" (");
4476 if (bs.screenPowerMah != 0) {
4477 pw.print(" screen=");
4478 printmAh(pw, bs.screenPowerMah);
4479 }
4480 if (bs.proportionalSmearMah != 0) {
4481 pw.print(" proportional=");
4482 printmAh(pw, bs.proportionalSmearMah);
4483 }
4484 pw.print(" )");
4485 }
4486 if (bs.shouldHide) {
4487 pw.print(" Excluded from smearing");
4488 }
4489
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004490 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004491 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004492 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004493 }
4494
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004495 sippers = helper.getMobilemsppList();
4496 if (sippers != null && sippers.size() > 0) {
4497 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004498 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004499 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004500 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004501 sb.setLength(0);
4502 sb.append(prefix); sb.append(" Uid ");
4503 UserHandle.formatUid(sb, bs.uidObj.getUid());
4504 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4505 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4506 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004507 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004508 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004509 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004510 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004511 sb.setLength(0);
4512 sb.append(prefix);
4513 sb.append(" TOTAL TIME: ");
4514 formatTimeMs(sb, totalTime);
4515 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4516 sb.append(")");
4517 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004518 pw.println();
4519 }
4520
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004521 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4522 @Override
4523 public int compare(TimerEntry lhs, TimerEntry rhs) {
4524 long lhsTime = lhs.mTime;
4525 long rhsTime = rhs.mTime;
4526 if (lhsTime < rhsTime) {
4527 return 1;
4528 }
4529 if (lhsTime > rhsTime) {
4530 return -1;
4531 }
4532 return 0;
4533 }
4534 };
4535
4536 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004537 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4538 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004539 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004540 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4541 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4542 : kernelWakelocks.entrySet()) {
4543 final BatteryStats.Timer timer = ent.getValue();
4544 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004545 if (totalTimeMillis > 0) {
4546 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4547 }
4548 }
4549 if (ktimers.size() > 0) {
4550 Collections.sort(ktimers, timerComparator);
4551 pw.print(prefix); pw.println(" All kernel wake locks:");
4552 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004553 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004554 String linePrefix = ": ";
4555 sb.setLength(0);
4556 sb.append(prefix);
4557 sb.append(" Kernel Wake lock ");
4558 sb.append(timer.mName);
4559 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4560 which, linePrefix);
4561 if (!linePrefix.equals(": ")) {
4562 sb.append(" realtime");
4563 // Only print out wake locks that were held
4564 pw.println(sb.toString());
4565 }
4566 }
4567 pw.println();
4568 }
4569 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004570
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004571 if (timers.size() > 0) {
4572 Collections.sort(timers, timerComparator);
4573 pw.print(prefix); pw.println(" All partial wake locks:");
4574 for (int i=0; i<timers.size(); i++) {
4575 TimerEntry timer = timers.get(i);
4576 sb.setLength(0);
4577 sb.append(" Wake lock ");
4578 UserHandle.formatUid(sb, timer.mId);
4579 sb.append(" ");
4580 sb.append(timer.mName);
4581 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4582 sb.append(" realtime");
4583 pw.println(sb.toString());
4584 }
4585 timers.clear();
4586 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08004587 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004588
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004589 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004590 if (wakeupReasons.size() > 0) {
4591 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004592 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004593 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004594 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004595 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
4596 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004597 }
4598 Collections.sort(reasons, timerComparator);
4599 for (int i=0; i<reasons.size(); i++) {
4600 TimerEntry timer = reasons.get(i);
4601 String linePrefix = ": ";
4602 sb.setLength(0);
4603 sb.append(prefix);
4604 sb.append(" Wakeup reason ");
4605 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004606 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4607 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004608 pw.println(sb.toString());
4609 }
4610 pw.println();
4611 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004612 }
Evan Millar22ac0432009-03-31 11:33:18 -07004613
James Carr2dd7e5e2016-07-20 18:48:39 -07004614 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
Bookatz50df7112017-08-04 14:53:26 -07004615 if (mMemoryStats.size() > 0) {
4616 pw.println(" Memory Stats");
4617 for (int i = 0; i < mMemoryStats.size(); i++) {
4618 sb.setLength(0);
4619 sb.append(" Bandwidth ");
4620 sb.append(mMemoryStats.keyAt(i));
4621 sb.append(" Time ");
4622 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
4623 pw.println(sb.toString());
4624 }
4625 pw.println();
4626 }
4627
4628 final Map<String, ? extends Timer> rpmStats = getRpmStats();
4629 if (rpmStats.size() > 0) {
4630 pw.print(prefix); pw.println(" Resource Power Manager Stats");
4631 if (rpmStats.size() > 0) {
4632 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
4633 final String timerName = ent.getKey();
4634 final Timer timer = ent.getValue();
4635 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
4636 }
4637 }
4638 pw.println();
4639 }
Bookatz82b341172017-09-07 19:06:08 -07004640 if (SCREEN_OFF_RPM_STATS_ENABLED) {
4641 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
Bookatz50df7112017-08-04 14:53:26 -07004642 if (screenOffRpmStats.size() > 0) {
Bookatz82b341172017-09-07 19:06:08 -07004643 pw.print(prefix);
4644 pw.println(" Resource Power Manager Stats for when screen was off");
4645 if (screenOffRpmStats.size() > 0) {
4646 for (Map.Entry<String, ? extends Timer> ent : screenOffRpmStats.entrySet()) {
4647 final String timerName = ent.getKey();
4648 final Timer timer = ent.getValue();
4649 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
4650 }
Bookatz50df7112017-08-04 14:53:26 -07004651 }
Bookatz82b341172017-09-07 19:06:08 -07004652 pw.println();
Bookatz50df7112017-08-04 14:53:26 -07004653 }
James Carr2dd7e5e2016-07-20 18:48:39 -07004654 }
4655
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004656 final long[] cpuFreqs = getCpuFreqs();
4657 if (cpuFreqs != null) {
4658 sb.setLength(0);
Bookatz50df7112017-08-04 14:53:26 -07004659 sb.append(" CPU freqs:");
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004660 for (int i = 0; i < cpuFreqs.length; ++i) {
4661 sb.append(" " + cpuFreqs[i]);
4662 }
4663 pw.println(sb.toString());
Bookatz50df7112017-08-04 14:53:26 -07004664 pw.println();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004665 }
4666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004667 for (int iu=0; iu<NU; iu++) {
4668 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004669 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004670 continue;
4671 }
Bookatzc8c44962017-05-11 12:12:54 -07004672
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004673 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07004674
4675 pw.print(prefix);
4676 pw.print(" ");
4677 UserHandle.formatUid(pw, uid);
4678 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004679 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004680
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004681 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4682 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4683 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4684 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004685 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4686 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
4687
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004688 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4689 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004690 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4691 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004692
4693 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
4694 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
4695
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004696 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4697 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4698 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08004699 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
4700 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4701 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
4702 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004703 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004704
Adam Lesinski5f056f62016-07-14 16:56:08 -07004705 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
4706 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
4707
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004708 if (mobileRxBytes > 0 || mobileTxBytes > 0
4709 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004710 pw.print(prefix); pw.print(" Mobile network: ");
4711 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004712 pw.print(formatBytesLocked(mobileTxBytes));
4713 pw.print(" sent (packets "); pw.print(mobileRxPackets);
4714 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004715 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004716 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
4717 sb.setLength(0);
4718 sb.append(prefix); sb.append(" Mobile radio active: ");
4719 formatTimeMs(sb, uidMobileActiveTime / 1000);
4720 sb.append("(");
4721 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
4722 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
4723 long packets = mobileRxPackets + mobileTxPackets;
4724 if (packets == 0) {
4725 packets = 1;
4726 }
4727 sb.append(" @ ");
4728 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
4729 sb.append(" mspp");
4730 pw.println(sb.toString());
4731 }
4732
Adam Lesinski5f056f62016-07-14 16:56:08 -07004733 if (mobileWakeup > 0) {
4734 sb.setLength(0);
4735 sb.append(prefix);
4736 sb.append(" Mobile radio AP wakeups: ");
4737 sb.append(mobileWakeup);
4738 pw.println(sb.toString());
4739 }
4740
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004741 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
4742 u.getModemControllerActivity(), which);
4743
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004744 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004745 pw.print(prefix); pw.print(" Wi-Fi network: ");
4746 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004747 pw.print(formatBytesLocked(wifiTxBytes));
4748 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4749 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004750 }
4751
Dianne Hackborn62793e42015-03-09 11:15:41 -07004752 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08004753 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004754 || uidWifiRunningTime != 0) {
4755 sb.setLength(0);
4756 sb.append(prefix); sb.append(" Wifi Running: ");
4757 formatTimeMs(sb, uidWifiRunningTime / 1000);
4758 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4759 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07004760 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004761 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4762 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4763 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08004764 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004765 formatTimeMs(sb, wifiScanTime / 1000);
4766 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004767 whichBatteryRealtime)); sb.append(") ");
4768 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08004769 sb.append("x\n");
4770 // actual and background times are unpooled and since reset (regardless of 'which')
4771 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
4772 formatTimeMs(sb, wifiScanActualTime / 1000);
4773 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
4774 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4775 sb.append(") ");
4776 sb.append(wifiScanCount);
4777 sb.append("x\n");
4778 sb.append(prefix); sb.append(" Background Wifi Scan: ");
4779 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
4780 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
4781 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4782 sb.append(") ");
4783 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07004784 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004785 pw.println(sb.toString());
4786 }
4787
Adam Lesinski5f056f62016-07-14 16:56:08 -07004788 if (wifiWakeup > 0) {
4789 sb.setLength(0);
4790 sb.append(prefix);
4791 sb.append(" WiFi AP wakeups: ");
4792 sb.append(wifiWakeup);
4793 pw.println(sb.toString());
4794 }
4795
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004796 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
4797 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07004798
Adam Lesinski50e47602015-12-04 17:04:54 -08004799 if (btRxBytes > 0 || btTxBytes > 0) {
4800 pw.print(prefix); pw.print(" Bluetooth network: ");
4801 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
4802 pw.print(formatBytesLocked(btTxBytes));
4803 pw.println(" sent");
4804 }
4805
Bookatz867c0d72017-03-07 18:23:42 -08004806 final Timer bleTimer = u.getBluetoothScanTimer();
4807 if (bleTimer != null) {
4808 // Convert from microseconds to milliseconds with rounding
4809 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
4810 / 1000;
4811 if (totalTimeMs != 0) {
4812 final int count = bleTimer.getCountLocked(which);
4813 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
4814 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004815 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4816 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
4817 final long actualTimeMsBg = bleTimerBg != null ?
4818 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07004819 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07004820 final int resultCount = u.getBluetoothScanResultCounter() != null ?
4821 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07004822 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
4823 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
4824 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
4825 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
4826 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
4827 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4828 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
4829 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
4830 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
4831 final Timer unoptimizedScanTimerBg =
4832 u.getBluetoothUnoptimizedScanBackgroundTimer();
4833 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
4834 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4835 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
4836 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004837
4838 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08004839 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07004840 sb.append(prefix);
4841 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08004842 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07004843 sb.append(" (");
4844 sb.append(count);
4845 sb.append(" times)");
4846 if (bleTimer.isRunningLocked()) {
4847 sb.append(" (currently running)");
4848 }
4849 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08004850 }
Bookatzb1f04f32017-05-19 13:57:32 -07004851
4852 sb.append(prefix);
4853 sb.append(" Bluetooth Scan (total actual realtime): ");
4854 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
4855 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08004856 sb.append(count);
4857 sb.append(" times)");
4858 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07004859 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08004860 }
Bookatzb1f04f32017-05-19 13:57:32 -07004861 sb.append("\n");
4862 if (actualTimeMsBg > 0 || countBg > 0) {
4863 sb.append(prefix);
4864 sb.append(" Bluetooth Scan (background realtime): ");
4865 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
4866 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08004867 sb.append(countBg);
4868 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07004869 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
4870 sb.append(" (currently running in background)");
4871 }
4872 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08004873 }
Bookatzb1f04f32017-05-19 13:57:32 -07004874
4875 sb.append(prefix);
4876 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07004877 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07004878 sb.append(" (");
4879 sb.append(resultCountBg);
4880 sb.append(" in background)");
4881
4882 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
4883 sb.append("\n");
4884 sb.append(prefix);
4885 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
4886 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
4887 sb.append(" (max ");
4888 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
4889 sb.append(")");
4890 if (unoptimizedScanTimer != null
4891 && unoptimizedScanTimer.isRunningLocked()) {
4892 sb.append(" (currently running unoptimized)");
4893 }
4894 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
4895 sb.append("\n");
4896 sb.append(prefix);
4897 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
4898 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
4899 sb.append(" (max ");
4900 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
4901 sb.append(")");
4902 if (unoptimizedScanTimerBg.isRunningLocked()) {
4903 sb.append(" (currently running unoptimized in background)");
4904 }
4905 }
4906 }
Bookatz867c0d72017-03-07 18:23:42 -08004907 pw.println(sb.toString());
4908 uidActivity = true;
4909 }
4910 }
4911
4912
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004913
Dianne Hackborn617f8772009-03-31 15:04:46 -07004914 if (u.hasUserActivity()) {
4915 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07004916 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004917 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004918 if (val != 0) {
4919 if (!hasData) {
4920 sb.setLength(0);
4921 sb.append(" User activity: ");
4922 hasData = true;
4923 } else {
4924 sb.append(", ");
4925 }
4926 sb.append(val);
4927 sb.append(" ");
4928 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
4929 }
4930 }
4931 if (hasData) {
4932 pw.println(sb.toString());
4933 }
4934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004935
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004936 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4937 = u.getWakelockStats();
4938 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004939 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004940 int countWakelock = 0;
4941 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4942 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4943 String linePrefix = ": ";
4944 sb.setLength(0);
4945 sb.append(prefix);
4946 sb.append(" Wake lock ");
4947 sb.append(wakelocks.keyAt(iw));
4948 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
4949 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004950 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4951 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004952 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004953 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
4954 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004955 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
4956 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004957 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
4958 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07004959 sb.append(" realtime");
4960 pw.println(sb.toString());
4961 uidActivity = true;
4962 countWakelock++;
4963
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004964 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
4965 rawRealtime, which);
4966 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
4967 rawRealtime, which);
4968 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
4969 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004970 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07004971 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004972 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004973 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07004974 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
4975 // pooled and therefore just a lower bound)
4976 long actualTotalPartialWakelock = 0;
4977 long actualBgPartialWakelock = 0;
4978 if (u.getAggregatedPartialWakelockTimer() != null) {
4979 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
4980 // Convert from microseconds to milliseconds with rounding
4981 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07004982 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07004983 final Timer bgAggTimer = aggTimer.getSubTimer();
4984 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004985 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07004986 }
4987
4988 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
4989 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
4990 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004991 sb.setLength(0);
4992 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004993 sb.append(" TOTAL wake: ");
4994 boolean needComma = false;
4995 if (totalFullWakelock != 0) {
4996 needComma = true;
4997 formatTimeMs(sb, totalFullWakelock);
4998 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004999 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005000 if (totalPartialWakelock != 0) {
5001 if (needComma) {
5002 sb.append(", ");
5003 }
5004 needComma = true;
5005 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07005006 sb.append("blamed partial");
5007 }
5008 if (actualTotalPartialWakelock != 0) {
5009 if (needComma) {
5010 sb.append(", ");
5011 }
5012 needComma = true;
5013 formatTimeMs(sb, actualTotalPartialWakelock);
5014 sb.append("actual partial");
5015 }
5016 if (actualBgPartialWakelock != 0) {
5017 if (needComma) {
5018 sb.append(", ");
5019 }
5020 needComma = true;
5021 formatTimeMs(sb, actualBgPartialWakelock);
5022 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005023 }
5024 if (totalWindowWakelock != 0) {
5025 if (needComma) {
5026 sb.append(", ");
5027 }
5028 needComma = true;
5029 formatTimeMs(sb, totalWindowWakelock);
5030 sb.append("window");
5031 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005032 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07005033 if (needComma) {
5034 sb.append(",");
5035 }
5036 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005037 formatTimeMs(sb, totalDrawWakelock);
5038 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07005039 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005040 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005041 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005042 }
5043 }
5044
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005045 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
5046 for (int isy=syncs.size()-1; isy>=0; isy--) {
5047 final Timer timer = syncs.valueAt(isy);
5048 // Convert from microseconds to milliseconds with rounding
5049 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5050 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07005051 final Timer bgTimer = timer.getSubTimer();
5052 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005053 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07005054 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005055 sb.setLength(0);
5056 sb.append(prefix);
5057 sb.append(" Sync ");
5058 sb.append(syncs.keyAt(isy));
5059 sb.append(": ");
5060 if (totalTime != 0) {
5061 formatTimeMs(sb, totalTime);
5062 sb.append("realtime (");
5063 sb.append(count);
5064 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07005065 if (bgTime > 0) {
5066 sb.append(", ");
5067 formatTimeMs(sb, bgTime);
5068 sb.append("background (");
5069 sb.append(bgCount);
5070 sb.append(" times)");
5071 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005072 } else {
5073 sb.append("(not used)");
5074 }
5075 pw.println(sb.toString());
5076 uidActivity = true;
5077 }
5078
5079 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
5080 for (int ij=jobs.size()-1; ij>=0; ij--) {
5081 final Timer timer = jobs.valueAt(ij);
5082 // Convert from microseconds to milliseconds with rounding
5083 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5084 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07005085 final Timer bgTimer = timer.getSubTimer();
5086 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005087 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07005088 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005089 sb.setLength(0);
5090 sb.append(prefix);
5091 sb.append(" Job ");
5092 sb.append(jobs.keyAt(ij));
5093 sb.append(": ");
5094 if (totalTime != 0) {
5095 formatTimeMs(sb, totalTime);
5096 sb.append("realtime (");
5097 sb.append(count);
5098 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07005099 if (bgTime > 0) {
5100 sb.append(", ");
5101 formatTimeMs(sb, bgTime);
5102 sb.append("background (");
5103 sb.append(bgCount);
5104 sb.append(" times)");
5105 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005106 } else {
5107 sb.append("(not used)");
5108 }
5109 pw.println(sb.toString());
5110 uidActivity = true;
5111 }
5112
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005113 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
5114 for (int ic=completions.size()-1; ic>=0; ic--) {
5115 SparseIntArray types = completions.valueAt(ic);
5116 if (types != null) {
5117 pw.print(prefix);
5118 pw.print(" Job Completions ");
5119 pw.print(completions.keyAt(ic));
5120 pw.print(":");
5121 for (int it=0; it<types.size(); it++) {
5122 pw.print(" ");
5123 pw.print(JobParameters.getReasonName(types.keyAt(it)));
5124 pw.print("(");
5125 pw.print(types.valueAt(it));
5126 pw.print("x)");
5127 }
5128 pw.println();
5129 }
5130 }
5131
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005132 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
5133 prefix, "Flashlight");
5134 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
5135 prefix, "Camera");
5136 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
5137 prefix, "Video");
5138 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
5139 prefix, "Audio");
5140
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005141 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
5142 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005143 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005144 final Uid.Sensor se = sensors.valueAt(ise);
5145 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005146 sb.setLength(0);
5147 sb.append(prefix);
5148 sb.append(" Sensor ");
5149 int handle = se.getHandle();
5150 if (handle == Uid.Sensor.GPS) {
5151 sb.append("GPS");
5152 } else {
5153 sb.append(handle);
5154 }
5155 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005156
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005157 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005158 if (timer != null) {
5159 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005160 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5161 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005162 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005163 final Timer bgTimer = se.getSensorBackgroundTime();
5164 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005165 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5166 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5167 final long bgActualTime = bgTimer != null ?
5168 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5169
Dianne Hackborn61659e52014-07-09 16:13:01 -07005170 //timer.logState();
5171 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005172 if (actualTime != totalTime) {
5173 formatTimeMs(sb, totalTime);
5174 sb.append("blamed realtime, ");
5175 }
5176
5177 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005178 sb.append("realtime (");
5179 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005180 sb.append(" times)");
5181
5182 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005183 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005184 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5185 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005186 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005187 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005189 } else {
5190 sb.append("(not used)");
5191 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005192 } else {
5193 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005194 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005195
5196 pw.println(sb.toString());
5197 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005198 }
5199
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005200 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5201 "Vibrator");
5202 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5203 prefix, "Foreground activities");
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005204 uidActivity |= printTimer(pw, sb, u.getForegroundServiceTimer(), rawRealtime, which,
5205 prefix, "Foreground services");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005206
Dianne Hackborn61659e52014-07-09 16:13:01 -07005207 long totalStateTime = 0;
5208 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5209 long time = u.getProcessStateTime(ips, rawRealtime, which);
5210 if (time > 0) {
5211 totalStateTime += time;
5212 sb.setLength(0);
5213 sb.append(prefix);
5214 sb.append(" ");
5215 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5216 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005217 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005218 pw.println(sb.toString());
5219 uidActivity = true;
5220 }
5221 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005222 if (totalStateTime > 0) {
5223 sb.setLength(0);
5224 sb.append(prefix);
5225 sb.append(" Total running: ");
5226 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5227 pw.println(sb.toString());
5228 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005229
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005230 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5231 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005232 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005233 sb.setLength(0);
5234 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005235 sb.append(" Total cpu time: u=");
5236 formatTimeMs(sb, userCpuTimeUs / 1000);
5237 sb.append("s=");
5238 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005239 pw.println(sb.toString());
5240 }
5241
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005242 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5243 if (cpuFreqTimes != null) {
5244 sb.setLength(0);
5245 sb.append(" Total cpu time per freq:");
5246 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5247 sb.append(" " + cpuFreqTimes[i]);
5248 }
5249 pw.println(sb.toString());
5250 }
5251 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5252 if (screenOffCpuFreqTimes != null) {
5253 sb.setLength(0);
5254 sb.append(" Total screen-off cpu time per freq:");
5255 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5256 sb.append(" " + screenOffCpuFreqTimes[i]);
5257 }
5258 pw.println(sb.toString());
5259 }
5260
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005261 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5262 = u.getProcessStats();
5263 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5264 final Uid.Proc ps = processStats.valueAt(ipr);
5265 long userTime;
5266 long systemTime;
5267 long foregroundTime;
5268 int starts;
5269 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005270
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005271 userTime = ps.getUserTime(which);
5272 systemTime = ps.getSystemTime(which);
5273 foregroundTime = ps.getForegroundTime(which);
5274 starts = ps.getStarts(which);
5275 final int numCrashes = ps.getNumCrashes(which);
5276 final int numAnrs = ps.getNumAnrs(which);
5277 numExcessive = which == STATS_SINCE_CHARGED
5278 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005279
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005280 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5281 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5282 sb.setLength(0);
5283 sb.append(prefix); sb.append(" Proc ");
5284 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5285 sb.append(prefix); sb.append(" CPU: ");
5286 formatTimeMs(sb, userTime); sb.append("usr + ");
5287 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5288 formatTimeMs(sb, foregroundTime); sb.append("fg");
5289 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5290 sb.append("\n"); sb.append(prefix); sb.append(" ");
5291 boolean hasOne = false;
5292 if (starts != 0) {
5293 hasOne = true;
5294 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005295 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005296 if (numCrashes != 0) {
5297 if (hasOne) {
5298 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005299 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005300 hasOne = true;
5301 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005302 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005303 if (numAnrs != 0) {
5304 if (hasOne) {
5305 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005306 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005307 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005308 }
5309 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005310 pw.println(sb.toString());
5311 for (int e=0; e<numExcessive; e++) {
5312 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5313 if (ew != null) {
5314 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07005315 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005316 pw.print("cpu");
5317 } else {
5318 pw.print("unknown");
5319 }
5320 pw.print(" use: ");
5321 TimeUtils.formatDuration(ew.usedTime, pw);
5322 pw.print(" over ");
5323 TimeUtils.formatDuration(ew.overTime, pw);
5324 if (ew.overTime != 0) {
5325 pw.print(" (");
5326 pw.print((ew.usedTime*100)/ew.overTime);
5327 pw.println("%)");
5328 }
5329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005330 }
5331 uidActivity = true;
5332 }
5333 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005334
5335 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5336 = u.getPackageStats();
5337 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
5338 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
5339 pw.println(":");
5340 boolean apkActivity = false;
5341 final Uid.Pkg ps = packageStats.valueAt(ipkg);
5342 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
5343 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
5344 pw.print(prefix); pw.print(" Wakeup alarm ");
5345 pw.print(alarms.keyAt(iwa)); pw.print(": ");
5346 pw.print(alarms.valueAt(iwa).getCountLocked(which));
5347 pw.println(" times");
5348 apkActivity = true;
5349 }
5350 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
5351 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
5352 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
5353 final long startTime = ss.getStartTime(batteryUptime, which);
5354 final int starts = ss.getStarts(which);
5355 final int launches = ss.getLaunches(which);
5356 if (startTime != 0 || starts != 0 || launches != 0) {
5357 sb.setLength(0);
5358 sb.append(prefix); sb.append(" Service ");
5359 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
5360 sb.append(prefix); sb.append(" Created for: ");
5361 formatTimeMs(sb, startTime / 1000);
5362 sb.append("uptime\n");
5363 sb.append(prefix); sb.append(" Starts: ");
5364 sb.append(starts);
5365 sb.append(", launches: "); sb.append(launches);
5366 pw.println(sb.toString());
5367 apkActivity = true;
5368 }
5369 }
5370 if (!apkActivity) {
5371 pw.print(prefix); pw.println(" (nothing executed)");
5372 }
5373 uidActivity = true;
5374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005376 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005377 }
5378 }
5379 }
5380
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005381 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005382 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005383 int diff = oldval ^ newval;
5384 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005385 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005386 for (int i=0; i<descriptions.length; i++) {
5387 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005388 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005389 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005390 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005391 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005392 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005393 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
5394 didWake = true;
5395 pw.print("=");
5396 if (longNames) {
5397 UserHandle.formatUid(pw, wakelockTag.uid);
5398 pw.print(":\"");
5399 pw.print(wakelockTag.string);
5400 pw.print("\"");
5401 } else {
5402 pw.print(wakelockTag.poolIdx);
5403 }
5404 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005405 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005406 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005407 pw.print("=");
5408 int val = (newval&bd.mask)>>bd.shift;
5409 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005410 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005411 } else {
5412 pw.print(val);
5413 }
5414 }
5415 }
5416 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005417 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07005418 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005419 if (longNames) {
5420 UserHandle.formatUid(pw, wakelockTag.uid);
5421 pw.print(":\"");
5422 pw.print(wakelockTag.string);
5423 pw.print("\"");
5424 } else {
5425 pw.print(wakelockTag.poolIdx);
5426 }
5427 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005428 }
5429
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005430 public void prepareForDumpLocked() {
5431 }
5432
5433 public static class HistoryPrinter {
5434 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005435 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005436 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005437 int oldStatus = -1;
5438 int oldHealth = -1;
5439 int oldPlug = -1;
5440 int oldTemp = -1;
5441 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005442 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005443 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005444
Dianne Hackborn3251b902014-06-20 14:40:53 -07005445 void reset() {
5446 oldState = oldState2 = 0;
5447 oldLevel = -1;
5448 oldStatus = -1;
5449 oldHealth = -1;
5450 oldPlug = -1;
5451 oldTemp = -1;
5452 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005453 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07005454 }
5455
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005456 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005457 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005458 if (!checkin) {
5459 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005460 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005461 pw.print(" (");
5462 pw.print(rec.numReadInts);
5463 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005464 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005465 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5466 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005467 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005468 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005469 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005470 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005471 }
5472 lastTime = rec.time;
5473 }
5474 if (rec.cmd == HistoryItem.CMD_START) {
5475 if (checkin) {
5476 pw.print(":");
5477 }
5478 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005479 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005480 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
5481 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005482 if (checkin) {
5483 pw.print(":");
5484 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005485 if (rec.cmd == HistoryItem.CMD_RESET) {
5486 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005487 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005488 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005489 pw.print("TIME:");
5490 if (checkin) {
5491 pw.println(rec.currentTime);
5492 } else {
5493 pw.print(" ");
5494 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5495 rec.currentTime).toString());
5496 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005497 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
5498 if (checkin) {
5499 pw.print(":");
5500 }
5501 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005502 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
5503 if (checkin) {
5504 pw.print(":");
5505 }
5506 pw.println("*OVERFLOW*");
5507 } else {
5508 if (!checkin) {
5509 if (rec.batteryLevel < 10) pw.print("00");
5510 else if (rec.batteryLevel < 100) pw.print("0");
5511 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005512 if (verbose) {
5513 pw.print(" ");
5514 if (rec.states < 0) ;
5515 else if (rec.states < 0x10) pw.print("0000000");
5516 else if (rec.states < 0x100) pw.print("000000");
5517 else if (rec.states < 0x1000) pw.print("00000");
5518 else if (rec.states < 0x10000) pw.print("0000");
5519 else if (rec.states < 0x100000) pw.print("000");
5520 else if (rec.states < 0x1000000) pw.print("00");
5521 else if (rec.states < 0x10000000) pw.print("0");
5522 pw.print(Integer.toHexString(rec.states));
5523 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005524 } else {
5525 if (oldLevel != rec.batteryLevel) {
5526 oldLevel = rec.batteryLevel;
5527 pw.print(",Bl="); pw.print(rec.batteryLevel);
5528 }
5529 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005530 if (oldStatus != rec.batteryStatus) {
5531 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005532 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005533 switch (oldStatus) {
5534 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005535 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005536 break;
5537 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005538 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005539 break;
5540 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005541 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005542 break;
5543 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005544 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005545 break;
5546 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005547 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005548 break;
5549 default:
5550 pw.print(oldStatus);
5551 break;
5552 }
5553 }
5554 if (oldHealth != rec.batteryHealth) {
5555 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005556 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005557 switch (oldHealth) {
5558 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005559 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005560 break;
5561 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005562 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005563 break;
5564 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005565 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005566 break;
5567 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005568 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005569 break;
5570 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005571 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005572 break;
5573 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005574 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005575 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005576 case BatteryManager.BATTERY_HEALTH_COLD:
5577 pw.print(checkin ? "c" : "cold");
5578 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005579 default:
5580 pw.print(oldHealth);
5581 break;
5582 }
5583 }
5584 if (oldPlug != rec.batteryPlugType) {
5585 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005586 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005587 switch (oldPlug) {
5588 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005589 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005590 break;
5591 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005592 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005593 break;
5594 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005595 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005596 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005597 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005598 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005599 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005600 default:
5601 pw.print(oldPlug);
5602 break;
5603 }
5604 }
5605 if (oldTemp != rec.batteryTemperature) {
5606 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005607 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005608 pw.print(oldTemp);
5609 }
5610 if (oldVolt != rec.batteryVoltage) {
5611 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005612 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005613 pw.print(oldVolt);
5614 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005615 final int chargeMAh = rec.batteryChargeUAh / 1000;
5616 if (oldChargeMAh != chargeMAh) {
5617 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07005618 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005619 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07005620 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005621 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005622 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005623 printBitDescriptions(pw, oldState2, rec.states2, null,
5624 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005625 if (rec.wakeReasonTag != null) {
5626 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005627 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005628 pw.print(rec.wakeReasonTag.poolIdx);
5629 } else {
5630 pw.print(" wake_reason=");
5631 pw.print(rec.wakeReasonTag.uid);
5632 pw.print(":\"");
5633 pw.print(rec.wakeReasonTag.string);
5634 pw.print("\"");
5635 }
5636 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08005637 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005638 pw.print(checkin ? "," : " ");
5639 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
5640 pw.print("+");
5641 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
5642 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005643 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005644 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
5645 : HISTORY_EVENT_NAMES;
5646 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
5647 | HistoryItem.EVENT_FLAG_FINISH);
5648 if (idx >= 0 && idx < eventNames.length) {
5649 pw.print(eventNames[idx]);
5650 } else {
5651 pw.print(checkin ? "Ev" : "event");
5652 pw.print(idx);
5653 }
5654 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005655 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005656 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08005657 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08005658 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
5659 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005660 pw.print(":\"");
5661 pw.print(rec.eventTag.string);
5662 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005663 }
5664 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005665 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005666 if (rec.stepDetails != null) {
5667 if (!checkin) {
5668 pw.print(" Details: cpu=");
5669 pw.print(rec.stepDetails.userTime);
5670 pw.print("u+");
5671 pw.print(rec.stepDetails.systemTime);
5672 pw.print("s");
5673 if (rec.stepDetails.appCpuUid1 >= 0) {
5674 pw.print(" (");
5675 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
5676 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5677 if (rec.stepDetails.appCpuUid2 >= 0) {
5678 pw.print(", ");
5679 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
5680 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5681 }
5682 if (rec.stepDetails.appCpuUid3 >= 0) {
5683 pw.print(", ");
5684 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
5685 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5686 }
5687 pw.print(')');
5688 }
5689 pw.println();
5690 pw.print(" /proc/stat=");
5691 pw.print(rec.stepDetails.statUserTime);
5692 pw.print(" usr, ");
5693 pw.print(rec.stepDetails.statSystemTime);
5694 pw.print(" sys, ");
5695 pw.print(rec.stepDetails.statIOWaitTime);
5696 pw.print(" io, ");
5697 pw.print(rec.stepDetails.statIrqTime);
5698 pw.print(" irq, ");
5699 pw.print(rec.stepDetails.statSoftIrqTime);
5700 pw.print(" sirq, ");
5701 pw.print(rec.stepDetails.statIdlTime);
5702 pw.print(" idle");
5703 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
5704 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
5705 + rec.stepDetails.statSoftIrqTime;
5706 int total = totalRun + rec.stepDetails.statIdlTime;
5707 if (total > 0) {
5708 pw.print(" (");
5709 float perc = ((float)totalRun) / ((float)total) * 100;
5710 pw.print(String.format("%.1f%%", perc));
5711 pw.print(" of ");
5712 StringBuilder sb = new StringBuilder(64);
5713 formatTimeMsNoSpace(sb, total*10);
5714 pw.print(sb);
5715 pw.print(")");
5716 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005717 pw.print(", PlatformIdleStat ");
5718 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005719 pw.println();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00005720
5721 pw.print(", SubsystemPowerState ");
5722 pw.print(rec.stepDetails.statSubsystemPowerState);
5723 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005724 } else {
5725 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5726 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
5727 pw.print(rec.stepDetails.userTime);
5728 pw.print(":");
5729 pw.print(rec.stepDetails.systemTime);
5730 if (rec.stepDetails.appCpuUid1 >= 0) {
5731 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
5732 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5733 if (rec.stepDetails.appCpuUid2 >= 0) {
5734 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
5735 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5736 }
5737 if (rec.stepDetails.appCpuUid3 >= 0) {
5738 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
5739 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5740 }
5741 }
5742 pw.println();
5743 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5744 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
5745 pw.print(rec.stepDetails.statUserTime);
5746 pw.print(',');
5747 pw.print(rec.stepDetails.statSystemTime);
5748 pw.print(',');
5749 pw.print(rec.stepDetails.statIOWaitTime);
5750 pw.print(',');
5751 pw.print(rec.stepDetails.statIrqTime);
5752 pw.print(',');
5753 pw.print(rec.stepDetails.statSoftIrqTime);
5754 pw.print(',');
5755 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005756 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005757 if (rec.stepDetails.statPlatformIdleState != null) {
5758 pw.print(rec.stepDetails.statPlatformIdleState);
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07005759 if (rec.stepDetails.statSubsystemPowerState != null) {
5760 pw.print(',');
5761 }
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005762 }
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00005763
5764 if (rec.stepDetails.statSubsystemPowerState != null) {
5765 pw.print(rec.stepDetails.statSubsystemPowerState);
5766 }
5767 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005768 }
5769 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005770 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005771 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005772 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005773 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005774
5775 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
5776 UserHandle.formatUid(pw, uid);
5777 pw.print("=");
5778 pw.print(utime);
5779 pw.print("u+");
5780 pw.print(stime);
5781 pw.print("s");
5782 }
5783
5784 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
5785 pw.print('/');
5786 pw.print(uid);
5787 pw.print(":");
5788 pw.print(utime);
5789 pw.print(":");
5790 pw.print(stime);
5791 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005792 }
5793
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005794 private void printSizeValue(PrintWriter pw, long size) {
5795 float result = size;
5796 String suffix = "";
5797 if (result >= 10*1024) {
5798 suffix = "KB";
5799 result = result / 1024;
5800 }
5801 if (result >= 10*1024) {
5802 suffix = "MB";
5803 result = result / 1024;
5804 }
5805 if (result >= 10*1024) {
5806 suffix = "GB";
5807 result = result / 1024;
5808 }
5809 if (result >= 10*1024) {
5810 suffix = "TB";
5811 result = result / 1024;
5812 }
5813 if (result >= 10*1024) {
5814 suffix = "PB";
5815 result = result / 1024;
5816 }
5817 pw.print((int)result);
5818 pw.print(suffix);
5819 }
5820
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005821 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
5822 String label3, long estimatedTime) {
5823 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005824 return false;
5825 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005826 pw.print(label1);
5827 pw.print(label2);
5828 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005829 StringBuilder sb = new StringBuilder(64);
5830 formatTimeMs(sb, estimatedTime);
5831 pw.print(sb);
5832 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005833 return true;
5834 }
5835
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005836 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
5837 LevelStepTracker steps, boolean checkin) {
5838 if (steps == null) {
5839 return false;
5840 }
5841 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005842 if (count <= 0) {
5843 return false;
5844 }
5845 if (!checkin) {
5846 pw.println(header);
5847 }
Kweku Adams030980a2015-04-01 16:07:48 -07005848 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005849 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005850 long duration = steps.getDurationAt(i);
5851 int level = steps.getLevelAt(i);
5852 long initMode = steps.getInitModeAt(i);
5853 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005854 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005855 lineArgs[0] = Long.toString(duration);
5856 lineArgs[1] = Integer.toString(level);
5857 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5858 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5859 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
5860 case Display.STATE_ON: lineArgs[2] = "s+"; break;
5861 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
5862 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07005863 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005864 }
5865 } else {
5866 lineArgs[2] = "";
5867 }
5868 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5869 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
5870 } else {
5871 lineArgs[3] = "";
5872 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005873 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07005874 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005875 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07005876 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005877 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005878 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
5879 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005880 pw.print(prefix);
5881 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005882 TimeUtils.formatDuration(duration, pw);
5883 pw.print(" to "); pw.print(level);
5884 boolean haveModes = false;
5885 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5886 pw.print(" (");
5887 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5888 case Display.STATE_OFF: pw.print("screen-off"); break;
5889 case Display.STATE_ON: pw.print("screen-on"); break;
5890 case Display.STATE_DOZE: pw.print("screen-doze"); break;
5891 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07005892 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005893 }
5894 haveModes = true;
5895 }
5896 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5897 pw.print(haveModes ? ", " : " (");
5898 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
5899 ? "power-save-on" : "power-save-off");
5900 haveModes = true;
5901 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005902 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
5903 pw.print(haveModes ? ", " : " (");
5904 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
5905 ? "device-idle-on" : "device-idle-off");
5906 haveModes = true;
5907 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005908 if (haveModes) {
5909 pw.print(")");
5910 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005911 pw.println();
5912 }
5913 }
5914 return true;
5915 }
5916
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005917 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005918 public static final int DUMP_DAILY_ONLY = 1<<2;
5919 public static final int DUMP_HISTORY_ONLY = 1<<3;
5920 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
5921 public static final int DUMP_VERBOSE = 1<<5;
5922 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005923
Dianne Hackborn37de0982014-05-09 09:32:18 -07005924 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
5925 final HistoryPrinter hprinter = new HistoryPrinter();
5926 final HistoryItem rec = new HistoryItem();
5927 long lastTime = -1;
5928 long baseTime = -1;
5929 boolean printed = false;
5930 HistoryEventTracker tracker = null;
5931 while (getNextHistoryLocked(rec)) {
5932 lastTime = rec.time;
5933 if (baseTime < 0) {
5934 baseTime = lastTime;
5935 }
5936 if (rec.time >= histStart) {
5937 if (histStart >= 0 && !printed) {
5938 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07005939 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005940 || rec.cmd == HistoryItem.CMD_START
5941 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005942 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005943 hprinter.printNextItem(pw, rec, baseTime, checkin,
5944 (flags&DUMP_VERBOSE) != 0);
5945 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005946 } else if (rec.currentTime != 0) {
5947 printed = true;
5948 byte cmd = rec.cmd;
5949 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005950 hprinter.printNextItem(pw, rec, baseTime, checkin,
5951 (flags&DUMP_VERBOSE) != 0);
5952 rec.cmd = cmd;
5953 }
5954 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005955 if (rec.cmd != HistoryItem.CMD_UPDATE) {
5956 hprinter.printNextItem(pw, rec, baseTime, checkin,
5957 (flags&DUMP_VERBOSE) != 0);
5958 rec.cmd = HistoryItem.CMD_UPDATE;
5959 }
5960 int oldEventCode = rec.eventCode;
5961 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005962 rec.eventTag = new HistoryTag();
5963 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
5964 HashMap<String, SparseIntArray> active
5965 = tracker.getStateForEvent(i);
5966 if (active == null) {
5967 continue;
5968 }
5969 for (HashMap.Entry<String, SparseIntArray> ent
5970 : active.entrySet()) {
5971 SparseIntArray uids = ent.getValue();
5972 for (int j=0; j<uids.size(); j++) {
5973 rec.eventCode = i;
5974 rec.eventTag.string = ent.getKey();
5975 rec.eventTag.uid = uids.keyAt(j);
5976 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07005977 hprinter.printNextItem(pw, rec, baseTime, checkin,
5978 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005979 rec.wakeReasonTag = null;
5980 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005981 }
5982 }
5983 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005984 rec.eventCode = oldEventCode;
5985 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005986 tracker = null;
5987 }
5988 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005989 hprinter.printNextItem(pw, rec, baseTime, checkin,
5990 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07005991 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
5992 // This is an attempt to aggregate the previous state and generate
5993 // fake events to reflect that state at the point where we start
5994 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07005995 if (tracker == null) {
5996 tracker = new HistoryEventTracker();
5997 }
5998 tracker.updateState(rec.eventCode, rec.eventTag.string,
5999 rec.eventTag.uid, rec.eventTag.poolIdx);
6000 }
6001 }
6002 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07006003 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006004 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
6005 }
6006 }
6007
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006008 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
6009 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
6010 if (steps == null) {
6011 return;
6012 }
6013 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
6014 if (timeRemaining >= 0) {
6015 pw.print(prefix); pw.print(label); pw.print(" total time: ");
6016 tmpSb.setLength(0);
6017 formatTimeMs(tmpSb, timeRemaining);
6018 pw.print(tmpSb);
6019 pw.print(" (from "); pw.print(tmpOutInt[0]);
6020 pw.println(" steps)");
6021 }
6022 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6023 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6024 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
6025 if (estimatedTime > 0) {
6026 pw.print(prefix); pw.print(label); pw.print(" ");
6027 pw.print(STEP_LEVEL_MODE_LABELS[i]);
6028 pw.print(" time: ");
6029 tmpSb.setLength(0);
6030 formatTimeMs(tmpSb, estimatedTime);
6031 pw.print(tmpSb);
6032 pw.print(" (from "); pw.print(tmpOutInt[0]);
6033 pw.println(" steps)");
6034 }
6035 }
6036 }
6037
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006038 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
6039 ArrayList<PackageChange> changes) {
6040 if (changes == null) {
6041 return;
6042 }
6043 pw.print(prefix); pw.println("Package changes:");
6044 for (int i=0; i<changes.size(); i++) {
6045 PackageChange pc = changes.get(i);
6046 if (pc.mUpdate) {
6047 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
6048 pw.print(" vers="); pw.println(pc.mVersionCode);
6049 } else {
6050 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
6051 }
6052 }
6053 }
6054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006055 /**
6056 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
6057 *
6058 * @param pw a Printer to receive the dump output.
6059 */
6060 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006061 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006062 prepareForDumpLocked();
6063
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006064 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006065 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006066
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006067 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006068 final long historyTotalSize = getHistoryTotalSize();
6069 final long historyUsedSize = getHistoryUsedSize();
6070 if (startIteratingHistoryLocked()) {
6071 try {
6072 pw.print("Battery History (");
6073 pw.print((100*historyUsedSize)/historyTotalSize);
6074 pw.print("% used, ");
6075 printSizeValue(pw, historyUsedSize);
6076 pw.print(" used of ");
6077 printSizeValue(pw, historyTotalSize);
6078 pw.print(", ");
6079 pw.print(getHistoryStringPoolSize());
6080 pw.print(" strings using ");
6081 printSizeValue(pw, getHistoryStringPoolBytes());
6082 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07006083 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006084 pw.println();
6085 } finally {
6086 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006087 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006088 }
6089
6090 if (startIteratingOldHistoryLocked()) {
6091 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006092 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006093 pw.println("Old battery History:");
6094 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006095 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006096 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006097 if (baseTime < 0) {
6098 baseTime = rec.time;
6099 }
6100 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006101 }
6102 pw.println();
6103 } finally {
6104 finishIteratingOldHistoryLocked();
6105 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006106 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006107 }
6108
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006109 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006110 return;
6111 }
6112
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006113 if (!filtering) {
6114 SparseArray<? extends Uid> uidStats = getUidStats();
6115 final int NU = uidStats.size();
6116 boolean didPid = false;
6117 long nowRealtime = SystemClock.elapsedRealtime();
6118 for (int i=0; i<NU; i++) {
6119 Uid uid = uidStats.valueAt(i);
6120 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
6121 if (pids != null) {
6122 for (int j=0; j<pids.size(); j++) {
6123 Uid.Pid pid = pids.valueAt(j);
6124 if (!didPid) {
6125 pw.println("Per-PID Stats:");
6126 didPid = true;
6127 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006128 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
6129 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006130 pw.print(" PID "); pw.print(pids.keyAt(j));
6131 pw.print(" wake time: ");
6132 TimeUtils.formatDuration(time, pw);
6133 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006134 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006135 }
6136 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006137 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006138 pw.println();
6139 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006140 }
6141
6142 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006143 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
6144 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006145 long timeRemaining = computeBatteryTimeRemaining(
6146 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006147 if (timeRemaining >= 0) {
6148 pw.print(" Estimated discharge time remaining: ");
6149 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6150 pw.println();
6151 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006152 final LevelStepTracker steps = getDischargeLevelStepTracker();
6153 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6154 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
6155 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6156 STEP_LEVEL_MODE_VALUES[i], null));
6157 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006158 pw.println();
6159 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006160 if (dumpDurationSteps(pw, " ", "Charge step durations:",
6161 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006162 long timeRemaining = computeChargeTimeRemaining(
6163 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006164 if (timeRemaining >= 0) {
6165 pw.print(" Estimated charge time remaining: ");
6166 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6167 pw.println();
6168 }
6169 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006170 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006171 }
6172 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
6173 pw.println("Daily stats:");
6174 pw.print(" Current start time: ");
6175 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6176 getCurrentDailyStartTime()).toString());
6177 pw.print(" Next min deadline: ");
6178 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6179 getNextMinDailyDeadline()).toString());
6180 pw.print(" Next max deadline: ");
6181 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6182 getNextMaxDailyDeadline()).toString());
6183 StringBuilder sb = new StringBuilder(64);
6184 int[] outInt = new int[1];
6185 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
6186 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006187 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
6188 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006189 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006190 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
6191 dsteps, false)) {
6192 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6193 sb, outInt);
6194 }
6195 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6196 csteps, false)) {
6197 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6198 sb, outInt);
6199 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006200 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006201 } else {
6202 pw.println(" Current daily steps:");
6203 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6204 sb, outInt);
6205 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6206 sb, outInt);
6207 }
6208 }
6209 DailyItem dit;
6210 int curIndex = 0;
6211 while ((dit=getDailyItemLocked(curIndex)) != null) {
6212 curIndex++;
6213 if ((flags&DUMP_DAILY_ONLY) != 0) {
6214 pw.println();
6215 }
6216 pw.print(" Daily from ");
6217 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6218 pw.print(" to ");
6219 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6220 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006221 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006222 if (dumpDurationSteps(pw, " ",
6223 " Discharge step durations:", dit.mDischargeSteps, false)) {
6224 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6225 sb, outInt);
6226 }
6227 if (dumpDurationSteps(pw, " ",
6228 " Charge step durations:", dit.mChargeSteps, false)) {
6229 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6230 sb, outInt);
6231 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006232 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006233 } else {
6234 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6235 sb, outInt);
6236 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6237 sb, outInt);
6238 }
6239 }
6240 pw.println();
6241 }
6242 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006243 pw.println("Statistics since last charge:");
6244 pw.println(" System starts: " + getStartCount()
6245 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006246 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6247 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006248 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006250 }
6251
6252 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006253 public void dumpCheckinLocked(Context context, PrintWriter pw,
6254 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006255 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006256
6257 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006258 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6259 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006260
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006261 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6262
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006263 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006264 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006265
6266 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07006267 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006268 try {
6269 for (int i=0; i<getHistoryStringPoolSize(); i++) {
6270 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6271 pw.print(HISTORY_STRING_POOL); pw.print(',');
6272 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006273 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006274 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006275 pw.print(",\"");
6276 String str = getHistoryTagPoolString(i);
6277 str = str.replace("\\", "\\\\");
6278 str = str.replace("\"", "\\\"");
6279 pw.print(str);
6280 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006281 pw.println();
6282 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006283 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006284 } finally {
6285 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08006286 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006287 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006288 }
6289
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006290 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006291 return;
6292 }
6293
Dianne Hackborne4a59512010-12-07 11:08:07 -08006294 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006295 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08006296 for (int i=0; i<apps.size(); i++) {
6297 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006298 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
6299 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08006300 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006301 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
6302 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006303 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006304 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006305 }
6306 SparseArray<? extends Uid> uidStats = getUidStats();
6307 final int NU = uidStats.size();
6308 String[] lineArgs = new String[2];
6309 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006310 int uid = UserHandle.getAppId(uidStats.keyAt(i));
6311 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
6312 if (pkgs != null && !pkgs.second.value) {
6313 pkgs.second.value = true;
6314 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08006315 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006316 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006317 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
6318 (Object[])lineArgs);
6319 }
6320 }
6321 }
6322 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006323 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006324 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006325 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07006326 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006327 if (timeRemaining >= 0) {
6328 lineArgs[0] = Long.toString(timeRemaining);
6329 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
6330 (Object[])lineArgs);
6331 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006332 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07006333 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006334 if (timeRemaining >= 0) {
6335 lineArgs[0] = Long.toString(timeRemaining);
6336 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
6337 (Object[])lineArgs);
6338 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006339 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
6340 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006341 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006343}