blob: c578b5f7299e744a1f1b05a1919921861bb539b3 [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);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003022 // Battery real time
3023 final long totalControllerActivityTimeMs
3024 = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003025 long totalTxTimeMs = 0;
3026 for (LongCounter txState : counter.getTxTimeCounters()) {
3027 totalTxTimeMs += txState.getCountLocked(which);
3028 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003029 final long sleepTimeMs
3030 = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + totalTxTimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003031
3032 sb.setLength(0);
3033 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003034 sb.append(" ");
3035 sb.append(controllerName);
3036 sb.append(" Sleep time: ");
3037 formatTimeMs(sb, sleepTimeMs);
3038 sb.append("(");
3039 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3040 sb.append(")");
3041 pw.println(sb.toString());
3042
3043 sb.setLength(0);
3044 sb.append(prefix);
3045 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003046 sb.append(controllerName);
3047 sb.append(" Idle time: ");
3048 formatTimeMs(sb, idleTimeMs);
3049 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003050 sb.append(formatRatioLocked(idleTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003051 sb.append(")");
3052 pw.println(sb.toString());
3053
3054 sb.setLength(0);
3055 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003056 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003057 sb.append(controllerName);
3058 sb.append(" Rx time: ");
3059 formatTimeMs(sb, rxTimeMs);
3060 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003061 sb.append(formatRatioLocked(rxTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003062 sb.append(")");
3063 pw.println(sb.toString());
3064
3065 sb.setLength(0);
3066 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003067 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003068 sb.append(controllerName);
3069 sb.append(" Tx time: ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003070
Siddharth Ray3c648c42017-10-02 17:30:58 -07003071 String [] powerLevel;
3072 switch(controllerName) {
3073 case "Cellular":
3074 powerLevel = new String[] {
3075 " less than 0dBm: ",
3076 " 0dBm to 8dBm: ",
3077 " 8dBm to 15dBm: ",
3078 " 15dBm to 20dBm: ",
3079 " above 20dBm: "};
3080 break;
3081 default:
3082 powerLevel = new String[] {"[0]", "[1]", "[2]", "[3]", "[4]"};
3083 break;
3084 }
3085 final int numTxLvls = Math.min(counter.getTxTimeCounters().length, powerLevel.length);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003086 if (numTxLvls > 1) {
Siddharth Ray3c648c42017-10-02 17:30:58 -07003087 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003088 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3089 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3090 sb.setLength(0);
3091 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003092 sb.append(" ");
3093 sb.append(powerLevel[lvl]);
3094 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003095 formatTimeMs(sb, txLvlTimeMs);
3096 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003097 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003098 sb.append(")");
3099 pw.println(sb.toString());
3100 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003101 } else {
3102 final long txLvlTimeMs = counter.getTxTimeCounters()[0].getCountLocked(which);
3103 formatTimeMs(sb, txLvlTimeMs);
3104 sb.append("(");
3105 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
3106 sb.append(")");
3107 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003108 }
3109
Siddharth Ray3c648c42017-10-02 17:30:58 -07003110 if (powerDrainMaMs > 0) {
3111 sb.setLength(0);
3112 sb.append(prefix);
3113 sb.append(" ");
3114 sb.append(controllerName);
3115 sb.append(" Battery drain: ").append(
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003116 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
Siddharth Ray3c648c42017-10-02 17:30:58 -07003117 sb.append("mAh");
3118 pw.println(sb.toString());
3119 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003120 }
3121
3122 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003123 * Temporary for settings.
3124 */
3125 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3126 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3127 }
3128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003129 /**
3130 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003131 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003134 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3135 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003136 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3137 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07003138 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003140 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3141 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003142 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3143 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3144 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 final long totalRealtime = computeRealtime(rawRealtime, which);
3146 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003147 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003148 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003149 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003150 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3151 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003152 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003153 rawRealtime, which);
3154 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3155 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003156 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003157 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003158 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003159 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003160 final long dischargeCount = getDischargeCoulombCounter().getCountLocked(which);
3161 final long dischargeScreenOffCount = getDischargeScreenOffCoulombCounter()
3162 .getCountLocked(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003163
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003164 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003165
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003166 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003167 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003168
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003169 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003172 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003173 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003174 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003175 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003176 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003177 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003178 getEstimatedBatteryCapacity(),
3179 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity());
Adam Lesinski67c134f2016-06-10 15:15:08 -07003180
Bookatzc8c44962017-05-11 12:12:54 -07003181
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003182 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003183 long fullWakeLockTimeTotal = 0;
3184 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003185
Evan Millar22ac0432009-03-31 11:33:18 -07003186 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003187 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003188
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003189 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3190 = u.getWakelockStats();
3191 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3192 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003193
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003194 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3195 if (fullWakeTimer != null) {
3196 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3197 which);
3198 }
3199
3200 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3201 if (partialWakeTimer != null) {
3202 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3203 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003204 }
3205 }
3206 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003207
3208 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003209 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3210 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3211 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3212 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3213 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3214 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3215 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3216 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003217 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3218 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003219 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3220 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003221 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3222 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003223
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003224 // Dump Modem controller stats
3225 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3226 getModemControllerActivity(), which);
3227
Adam Lesinskie283d332015-04-16 12:29:25 -07003228 // Dump Wifi controller stats
3229 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3230 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003231 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003232 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003233
3234 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3235 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003236
3237 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003238 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3239 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241 // Dump misc stats
3242 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003243 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003244 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003245 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003246 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003247 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003248 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3249 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003250 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003251 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3252 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3253 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3254 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003255 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003256
Dianne Hackborn617f8772009-03-31 15:04:46 -07003257 // Dump screen brightness stats
3258 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3259 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003260 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003261 }
3262 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003263
Dianne Hackborn627bba72009-03-24 22:32:56 -07003264 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003265 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3266 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003267 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003268 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003269 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003270 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003271 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003272 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003273 args[i] = getPhoneSignalStrengthCount(i, which);
3274 }
3275 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003276
Dianne Hackborn627bba72009-03-24 22:32:56 -07003277 // Dump network type stats
3278 args = new Object[NUM_DATA_CONNECTION_TYPES];
3279 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003280 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003281 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003282 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3283 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3284 args[i] = getPhoneDataConnectionCount(i, which);
3285 }
3286 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003287
3288 // Dump wifi state stats
3289 args = new Object[NUM_WIFI_STATES];
3290 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003291 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003292 }
3293 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3294 for (int i=0; i<NUM_WIFI_STATES; i++) {
3295 args[i] = getWifiStateCount(i, which);
3296 }
3297 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3298
Dianne Hackborn3251b902014-06-20 14:40:53 -07003299 // Dump wifi suppl state stats
3300 args = new Object[NUM_WIFI_SUPPL_STATES];
3301 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3302 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3303 }
3304 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3305 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3306 args[i] = getWifiSupplStateCount(i, which);
3307 }
3308 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3309
3310 // Dump wifi signal strength stats
3311 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3312 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3313 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3314 }
3315 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3316 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3317 args[i] = getWifiSignalStrengthCount(i, which);
3318 }
3319 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3320
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003321 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003322 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003323 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003324 }
Bookatzc8c44962017-05-11 12:12:54 -07003325
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003326 if (which == STATS_SINCE_UNPLUGGED) {
3327 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3328 getDischargeStartLevel()-getDischargeCurrentLevel(),
3329 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003330 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
3331 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003332 } else {
3333 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3334 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003335 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003336 getDischargeAmountScreenOffSinceCharge(),
3337 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003338 }
Bookatzc8c44962017-05-11 12:12:54 -07003339
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003340 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003341 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003342 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003343 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003344 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003345 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003346 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3347 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003348 }
Evan Millarc64edde2009-04-18 12:26:32 -07003349 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003350 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003351 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003352 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3353 // Not doing the regular wake lock formatting to remain compatible
3354 // with the old checkin format.
3355 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3356 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003357 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003358 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003359 }
3360 }
Evan Millarc64edde2009-04-18 12:26:32 -07003361 }
Bookatzc8c44962017-05-11 12:12:54 -07003362
Bookatz50df7112017-08-04 14:53:26 -07003363 final Map<String, ? extends Timer> rpmStats = getRpmStats();
3364 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
3365 if (rpmStats.size() > 0) {
3366 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
3367 sb.setLength(0);
3368 Timer totalTimer = ent.getValue();
3369 long timeMs = (totalTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3370 int count = totalTimer.getCountLocked(which);
3371 Timer screenOffTimer = screenOffRpmStats.get(ent.getKey());
3372 long screenOffTimeMs = screenOffTimer != null
3373 ? (screenOffTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3374 int screenOffCount = screenOffTimer != null
3375 ? screenOffTimer.getCountLocked(which) : 0;
Bookatz82b341172017-09-07 19:06:08 -07003376 if (SCREEN_OFF_RPM_STATS_ENABLED) {
3377 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3378 "\"" + ent.getKey() + "\"", timeMs, count, screenOffTimeMs,
3379 screenOffCount);
3380 } else {
3381 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3382 "\"" + ent.getKey() + "\"", timeMs, count);
3383 }
Bookatz50df7112017-08-04 14:53:26 -07003384 }
3385 }
3386
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003387 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003388 helper.create(this);
3389 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003390 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003391 if (sippers != null && sippers.size() > 0) {
3392 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3393 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003394 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003395 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3396 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
3397 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003398 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003399 int uid = 0;
3400 String label;
3401 switch (bs.drainType) {
3402 case IDLE:
3403 label="idle";
3404 break;
3405 case CELL:
3406 label="cell";
3407 break;
3408 case PHONE:
3409 label="phone";
3410 break;
3411 case WIFI:
3412 label="wifi";
3413 break;
3414 case BLUETOOTH:
3415 label="blue";
3416 break;
3417 case SCREEN:
3418 label="scrn";
3419 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003420 case FLASHLIGHT:
3421 label="flashlight";
3422 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003423 case APP:
3424 uid = bs.uidObj.getUid();
3425 label = "uid";
3426 break;
3427 case USER:
3428 uid = UserHandle.getUid(bs.userId, 0);
3429 label = "user";
3430 break;
3431 case UNACCOUNTED:
3432 label = "unacc";
3433 break;
3434 case OVERCOUNTED:
3435 label = "over";
3436 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003437 case CAMERA:
3438 label = "camera";
3439 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003440 default:
3441 label = "???";
3442 }
3443 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07003444 BatteryStatsHelper.makemAh(bs.totalPowerMah),
3445 bs.shouldHide ? 1 : 0,
3446 BatteryStatsHelper.makemAh(bs.screenPowerMah),
3447 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003448 }
3449 }
3450
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003451 final long[] cpuFreqs = getCpuFreqs();
3452 if (cpuFreqs != null) {
3453 sb.setLength(0);
3454 for (int i = 0; i < cpuFreqs.length; ++i) {
3455 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3456 }
3457 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3458 }
3459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 for (int iu = 0; iu < NU; iu++) {
3461 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003462 if (reqUid >= 0 && uid != reqUid) {
3463 continue;
3464 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003465 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003467 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003468 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3469 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3470 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3471 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3472 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3473 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3474 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3475 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003476 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003477 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3478 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003479 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003480 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3481 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003482 // Background data transfers
3483 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3484 which);
3485 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3486 which);
3487 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3488 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3489 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3490 which);
3491 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3492 which);
3493 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3494 which);
3495 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3496 which);
3497
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003498 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3499 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003500 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003501 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3502 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3503 || wifiBytesBgTx > 0
3504 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3505 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003506 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3507 wifiBytesRx, wifiBytesTx,
3508 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003509 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003510 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003511 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3512 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3513 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3514 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003515 }
3516
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003517 // Dump modem controller data, per UID.
3518 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3519 u.getModemControllerActivity(), which);
3520
3521 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003522 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3523 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3524 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003525 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3526 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003527 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3528 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3529 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003530 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003531 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003532 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3533 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003534 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3535 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003536 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003537 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003540 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3541 u.getWifiControllerActivity(), which);
3542
Bookatz867c0d72017-03-07 18:23:42 -08003543 final Timer bleTimer = u.getBluetoothScanTimer();
3544 if (bleTimer != null) {
3545 // Convert from microseconds to milliseconds with rounding
3546 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3547 / 1000;
3548 if (totalTime != 0) {
3549 final int count = bleTimer.getCountLocked(which);
3550 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3551 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003552 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3553 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3554 final long actualTimeBg = bleTimerBg != null ?
3555 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003556 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003557 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3558 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003559 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3560 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3561 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3562 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3563 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
3564 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3565 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
3566 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3567 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
3568 final Timer unoptimizedScanTimerBg =
3569 u.getBluetoothUnoptimizedScanBackgroundTimer();
3570 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
3571 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3572 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
3573 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3574
Bookatz867c0d72017-03-07 18:23:42 -08003575 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07003576 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
3577 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
3578 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08003579 }
3580 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003581
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003582 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3583 u.getBluetoothControllerActivity(), which);
3584
Dianne Hackborn617f8772009-03-31 15:04:46 -07003585 if (u.hasUserActivity()) {
3586 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3587 boolean hasData = false;
3588 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3589 int val = u.getUserActivityCount(i, which);
3590 args[i] = val;
3591 if (val != 0) hasData = true;
3592 }
3593 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003594 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003595 }
3596 }
Bookatzc8c44962017-05-11 12:12:54 -07003597
3598 if (u.getAggregatedPartialWakelockTimer() != null) {
3599 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07003600 // Times are since reset (regardless of 'which')
3601 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07003602 final Timer bgTimer = timer.getSubTimer();
3603 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003604 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07003605 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
3606 }
3607
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003608 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3609 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3610 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3611 String linePrefix = "";
3612 sb.setLength(0);
3613 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3614 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003615 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3616 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003617 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003618 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
3619 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003620 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3621 rawRealtime, "w", which, linePrefix);
3622
3623 // Only log if we had at lease one wakelock...
3624 if (sb.length() > 0) {
3625 String name = wakelocks.keyAt(iw);
3626 if (name.indexOf(',') >= 0) {
3627 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003628 }
Yi Jin02483362017-08-04 11:30:44 -07003629 if (name.indexOf('\n') >= 0) {
3630 name = name.replace('\n', '_');
3631 }
3632 if (name.indexOf('\r') >= 0) {
3633 name = name.replace('\r', '_');
3634 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003635 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003636 }
3637 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003638
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003639 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3640 for (int isy=syncs.size()-1; isy>=0; isy--) {
3641 final Timer timer = syncs.valueAt(isy);
3642 // Convert from microseconds to milliseconds with rounding
3643 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3644 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07003645 final Timer bgTimer = timer.getSubTimer();
3646 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003647 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07003648 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003649 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003650 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07003651 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003652 }
3653 }
3654
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003655 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3656 for (int ij=jobs.size()-1; ij>=0; ij--) {
3657 final Timer timer = jobs.valueAt(ij);
3658 // Convert from microseconds to milliseconds with rounding
3659 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3660 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07003661 final Timer bgTimer = timer.getSubTimer();
3662 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003663 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07003664 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003665 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003666 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07003667 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003668 }
3669 }
3670
Dianne Hackborn94326cb2017-06-28 16:17:20 -07003671 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
3672 for (int ic=completions.size()-1; ic>=0; ic--) {
3673 SparseIntArray types = completions.valueAt(ic);
3674 if (types != null) {
3675 dumpLine(pw, uid, category, JOB_COMPLETION_DATA,
3676 "\"" + completions.keyAt(ic) + "\"",
3677 types.get(JobParameters.REASON_CANCELED, 0),
3678 types.get(JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, 0),
3679 types.get(JobParameters.REASON_PREEMPT, 0),
3680 types.get(JobParameters.REASON_TIMEOUT, 0),
3681 types.get(JobParameters.REASON_DEVICE_IDLE, 0));
3682 }
3683 }
3684
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003685 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3686 rawRealtime, which);
3687 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3688 rawRealtime, which);
3689 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3690 rawRealtime, which);
3691 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3692 rawRealtime, which);
3693
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003694 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3695 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003696 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003697 final Uid.Sensor se = sensors.valueAt(ise);
3698 final int sensorNumber = sensors.keyAt(ise);
3699 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003700 if (timer != null) {
3701 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003702 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3703 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003704 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08003705 final int count = timer.getCountLocked(which);
3706 final Timer bgTimer = se.getSensorBackgroundTime();
3707 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003708 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3709 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
3710 final long bgActualTime = bgTimer != null ?
3711 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3712 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
3713 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003715 }
3716 }
3717
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003718 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3719 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003720
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07003721 dumpTimer(pw, uid, category, FOREGROUND_ACTIVITY_DATA, u.getForegroundActivityTimer(),
3722 rawRealtime, which);
3723
3724 dumpTimer(pw, uid, category, FOREGROUND_SERVICE_DATA, u.getForegroundServiceTimer(),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003725 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003726
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003727 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003728 long totalStateTime = 0;
3729 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003730 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3731 totalStateTime += time;
3732 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003733 }
3734 if (totalStateTime > 0) {
3735 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3736 }
3737
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003738 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3739 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003740 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003741 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003742 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003743 }
3744
Sudheer Shankaa87245d2017-08-10 12:02:31 -07003745 // If the cpuFreqs is null, then don't bother checking for cpu freq times.
3746 if (cpuFreqs != null) {
3747 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
3748 // If total cpuFreqTimes is null, then we don't need to check for
3749 // screenOffCpuFreqTimes.
3750 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
3751 sb.setLength(0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003752 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
Sudheer Shankaa87245d2017-08-10 12:02:31 -07003753 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003754 }
Sudheer Shankaa87245d2017-08-10 12:02:31 -07003755 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
3756 if (screenOffCpuFreqTimeMs != null) {
3757 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
3758 sb.append("," + screenOffCpuFreqTimeMs[i]);
3759 }
3760 } else {
3761 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
3762 sb.append(",0");
3763 }
3764 }
3765 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
3766 cpuFreqTimeMs.length, sb.toString());
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003767 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003768 }
3769
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003770 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3771 = u.getProcessStats();
3772 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3773 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003774
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003775 final long userMillis = ps.getUserTime(which);
3776 final long systemMillis = ps.getSystemTime(which);
3777 final long foregroundMillis = ps.getForegroundTime(which);
3778 final int starts = ps.getStarts(which);
3779 final int numCrashes = ps.getNumCrashes(which);
3780 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003781
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003782 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3783 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003784 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
3785 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003786 }
3787 }
3788
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003789 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3790 = u.getPackageStats();
3791 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3792 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3793 int wakeups = 0;
3794 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3795 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07003796 int count = alarms.valueAt(iwa).getCountLocked(which);
3797 wakeups += count;
3798 String name = alarms.keyAt(iwa).replace(',', '_');
3799 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003800 }
3801 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3802 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3803 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3804 final long startTime = ss.getStartTime(batteryUptime, which);
3805 final int starts = ss.getStarts(which);
3806 final int launches = ss.getLaunches(which);
3807 if (startTime != 0 || starts != 0 || launches != 0) {
3808 dumpLine(pw, uid, category, APK_DATA,
3809 wakeups, // wakeup alarms
3810 packageStats.keyAt(ipkg), // Apk
3811 serviceStats.keyAt(isvc), // service
3812 startTime / 1000, // time spent started, in ms
3813 starts,
3814 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 }
3816 }
3817 }
3818 }
3819 }
3820
Dianne Hackborn81038902012-11-26 17:04:09 -08003821 static final class TimerEntry {
3822 final String mName;
3823 final int mId;
3824 final BatteryStats.Timer mTimer;
3825 final long mTime;
3826 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3827 mName = name;
3828 mId = id;
3829 mTimer = timer;
3830 mTime = time;
3831 }
3832 }
3833
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003834 private void printmAh(PrintWriter printer, double power) {
3835 printer.print(BatteryStatsHelper.makemAh(power));
3836 }
3837
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003838 private void printmAh(StringBuilder sb, double power) {
3839 sb.append(BatteryStatsHelper.makemAh(power));
3840 }
3841
Dianne Hackbornd953c532014-08-16 18:17:38 -07003842 /**
3843 * Temporary for settings.
3844 */
3845 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3846 int reqUid) {
3847 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3848 }
3849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003850 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003851 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003852 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003853 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3854 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07003855 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003856 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003857
3858 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3859 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3860 final long totalRealtime = computeRealtime(rawRealtime, which);
3861 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003862 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3863 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3864 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003865 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3866 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003867
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003868 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003869
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003870 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003871 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003873 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
3874 if (estimatedBatteryCapacity > 0) {
3875 sb.setLength(0);
3876 sb.append(prefix);
3877 sb.append(" Estimated battery capacity: ");
3878 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
3879 sb.append(" mAh");
3880 pw.println(sb.toString());
3881 }
3882
Jocelyn Dangc627d102017-04-14 13:15:14 -07003883 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
3884 if (minLearnedBatteryCapacity > 0) {
3885 sb.setLength(0);
3886 sb.append(prefix);
3887 sb.append(" Min learned battery capacity: ");
3888 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
3889 sb.append(" mAh");
3890 pw.println(sb.toString());
3891 }
3892 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
3893 if (maxLearnedBatteryCapacity > 0) {
3894 sb.setLength(0);
3895 sb.append(prefix);
3896 sb.append(" Max learned battery capacity: ");
3897 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
3898 sb.append(" mAh");
3899 pw.println(sb.toString());
3900 }
3901
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003902 sb.setLength(0);
3903 sb.append(prefix);
3904 sb.append(" Time on battery: ");
3905 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3906 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3907 sb.append(") realtime, ");
3908 formatTimeMs(sb, whichBatteryUptime / 1000);
3909 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3910 sb.append(") uptime");
3911 pw.println(sb.toString());
3912 sb.setLength(0);
3913 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003914 sb.append(" Time on battery screen off: ");
3915 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3916 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3917 sb.append(") realtime, ");
3918 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3919 sb.append("(");
3920 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3921 sb.append(") uptime");
3922 pw.println(sb.toString());
3923 sb.setLength(0);
3924 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003925 sb.append(" Total run time: ");
3926 formatTimeMs(sb, totalRealtime / 1000);
3927 sb.append("realtime, ");
3928 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003929 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003930 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003931 if (batteryTimeRemaining >= 0) {
3932 sb.setLength(0);
3933 sb.append(prefix);
3934 sb.append(" Battery time remaining: ");
3935 formatTimeMs(sb, batteryTimeRemaining / 1000);
3936 pw.println(sb.toString());
3937 }
3938 if (chargeTimeRemaining >= 0) {
3939 sb.setLength(0);
3940 sb.append(prefix);
3941 sb.append(" Charge time remaining: ");
3942 formatTimeMs(sb, chargeTimeRemaining / 1000);
3943 pw.println(sb.toString());
3944 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003945
3946 final LongCounter dischargeCounter = getDischargeCoulombCounter();
3947 final long dischargeCount = dischargeCounter.getCountLocked(which);
3948 if (dischargeCount >= 0) {
3949 sb.setLength(0);
3950 sb.append(prefix);
3951 sb.append(" Discharge: ");
3952 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
3953 sb.append(" mAh");
3954 pw.println(sb.toString());
3955 }
3956
3957 final LongCounter dischargeScreenOffCounter = getDischargeScreenOffCoulombCounter();
3958 final long dischargeScreenOffCount = dischargeScreenOffCounter.getCountLocked(which);
3959 if (dischargeScreenOffCount >= 0) {
3960 sb.setLength(0);
3961 sb.append(prefix);
3962 sb.append(" Screen off discharge: ");
3963 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
3964 sb.append(" mAh");
3965 pw.println(sb.toString());
3966 }
3967
3968 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
3969 if (dischargeScreenOnCount >= 0) {
3970 sb.setLength(0);
3971 sb.append(prefix);
3972 sb.append(" Screen on discharge: ");
3973 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
3974 sb.append(" mAh");
3975 pw.println(sb.toString());
3976 }
3977
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003978 pw.print(" Start clock time: ");
3979 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3980
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003981 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003982 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003983 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003984 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3985 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003986 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003987 rawRealtime, which);
3988 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3989 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003990 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003991 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003992 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3993 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3994 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003995 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003996 sb.append(prefix);
3997 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3998 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003999 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004000 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
4001 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004002 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004003 pw.println(sb.toString());
4004 sb.setLength(0);
4005 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004006 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004007 boolean didOne = false;
4008 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004009 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004010 if (time == 0) {
4011 continue;
4012 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004013 sb.append("\n ");
4014 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004015 didOne = true;
4016 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
4017 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004018 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004019 sb.append("(");
4020 sb.append(formatRatioLocked(time, screenOnTime));
4021 sb.append(")");
4022 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004023 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004024 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004025 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004026 sb.setLength(0);
4027 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004028 sb.append(" Power save mode enabled: ");
4029 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004030 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004031 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004032 sb.append(")");
4033 pw.println(sb.toString());
4034 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004035 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004036 sb.setLength(0);
4037 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004038 sb.append(" Device light idling: ");
4039 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004040 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004041 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
4042 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004043 sb.append("x");
4044 pw.println(sb.toString());
4045 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004046 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004047 sb.setLength(0);
4048 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004049 sb.append(" Idle mode light time: ");
4050 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004051 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004052 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
4053 sb.append(") ");
4054 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004055 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004056 sb.append(" -- longest ");
4057 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
4058 pw.println(sb.toString());
4059 }
4060 if (deviceIdlingTime != 0) {
4061 sb.setLength(0);
4062 sb.append(prefix);
4063 sb.append(" Device full idling: ");
4064 formatTimeMs(sb, deviceIdlingTime / 1000);
4065 sb.append("(");
4066 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004067 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004068 sb.append("x");
4069 pw.println(sb.toString());
4070 }
4071 if (deviceIdleModeFullTime != 0) {
4072 sb.setLength(0);
4073 sb.append(prefix);
4074 sb.append(" Idle mode full time: ");
4075 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
4076 sb.append("(");
4077 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
4078 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004079 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004080 sb.append("x");
4081 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004082 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004083 pw.println(sb.toString());
4084 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004085 if (phoneOnTime != 0) {
4086 sb.setLength(0);
4087 sb.append(prefix);
4088 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
4089 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004090 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004091 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004092 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004093 if (connChanges != 0) {
4094 pw.print(prefix);
4095 pw.print(" Connectivity changes: "); pw.println(connChanges);
4096 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004097
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004098 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07004099 long fullWakeLockTimeTotalMicros = 0;
4100 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08004101
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004102 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08004103
Evan Millar22ac0432009-03-31 11:33:18 -07004104 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004105 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004106
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004107 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4108 = u.getWakelockStats();
4109 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4110 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07004111
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004112 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
4113 if (fullWakeTimer != null) {
4114 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
4115 rawRealtime, which);
4116 }
4117
4118 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4119 if (partialWakeTimer != null) {
4120 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
4121 rawRealtime, which);
4122 if (totalTimeMicros > 0) {
4123 if (reqUid < 0) {
4124 // Only show the ordered list of all wake
4125 // locks if the caller is not asking for data
4126 // about a specific uid.
4127 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
4128 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08004129 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004130 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07004131 }
4132 }
4133 }
4134 }
Bookatzc8c44962017-05-11 12:12:54 -07004135
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004136 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4137 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4138 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4139 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4140 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4141 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4142 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4143 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004144 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4145 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004146
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004147 if (fullWakeLockTimeTotalMicros != 0) {
4148 sb.setLength(0);
4149 sb.append(prefix);
4150 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4151 (fullWakeLockTimeTotalMicros + 500) / 1000);
4152 pw.println(sb.toString());
4153 }
4154
4155 if (partialWakeLockTimeTotalMicros != 0) {
4156 sb.setLength(0);
4157 sb.append(prefix);
4158 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4159 (partialWakeLockTimeTotalMicros + 500) / 1000);
4160 pw.println(sb.toString());
4161 }
4162
Siddharth Ray3c648c42017-10-02 17:30:58 -07004163 pw.println("");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004164 pw.print(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004165 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004166 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004167 sb.append(" CONNECTIVITY POWER SUMMARY START");
4168 pw.println(sb.toString());
4169
4170 pw.print(prefix);
4171 sb.setLength(0);
4172 sb.append(prefix);
4173 sb.append(" Logging duration for connectivity statistics: ");
4174 formatTimeMs(sb, whichBatteryRealtime / 1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004175 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004176
4177 sb.setLength(0);
4178 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004179 sb.append(" Cellular Statistics:");
Amith Yamasanif37447b2009-10-08 18:28:01 -07004180 pw.println(sb.toString());
4181
Siddharth Ray3c648c42017-10-02 17:30:58 -07004182 pw.print(prefix);
4183 sb.setLength(0);
4184 sb.append(prefix);
4185 sb.append(" Cellular kernel active time: ");
4186 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
4187 formatTimeMs(sb, mobileActiveTime / 1000);
4188 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4189 sb.append(")");
4190 pw.println(sb.toString());
4191
4192 pw.print(" Cellular data received: "); pw.println(formatBytesLocked(mobileRxTotalBytes));
4193 pw.print(" Cellular data sent: "); pw.println(formatBytesLocked(mobileTxTotalBytes));
4194 pw.print(" Cellular packets received: "); pw.println(mobileRxTotalPackets);
4195 pw.print(" Cellular packets sent: "); pw.println(mobileTxTotalPackets);
4196
Dianne Hackborn627bba72009-03-24 22:32:56 -07004197 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004198 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004199 sb.append(" Cellular Radio Access Technology:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004200 didOne = false;
4201 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004202 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004203 if (time == 0) {
4204 continue;
4205 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004206 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004207 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004208 didOne = true;
4209 sb.append(DATA_CONNECTION_NAMES[i]);
4210 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004211 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004212 sb.append("(");
4213 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004214 sb.append(") ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004215 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004216 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004217 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004218
4219 sb.setLength(0);
4220 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004221 sb.append(" Cellular Rx signal strength (RSRP):");
4222 final String[] cellularRxSignalStrengthDescription = new String[]{
4223 "very poor (less than -128dBm): ",
4224 "poor (-128dBm to -118dBm): ",
4225 "moderate (-118dBm to -108dBm): ",
4226 "good (-108dBm to -98dBm): ",
4227 "great (greater than -98dBm): "};
4228 didOne = false;
4229 final int numCellularRxBins = Math.min(SignalStrength.NUM_SIGNAL_STRENGTH_BINS,
4230 cellularRxSignalStrengthDescription.length);
4231 for (int i=0; i<numCellularRxBins; i++) {
4232 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
4233 if (time == 0) {
4234 continue;
4235 }
4236 sb.append("\n ");
4237 sb.append(prefix);
4238 didOne = true;
4239 sb.append(cellularRxSignalStrengthDescription[i]);
4240 sb.append(" ");
4241 formatTimeMs(sb, time/1000);
4242 sb.append("(");
4243 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4244 sb.append(") ");
4245 }
4246 if (!didOne) sb.append(" (no activity)");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004247 pw.println(sb.toString());
4248
Siddharth Ray3c648c42017-10-02 17:30:58 -07004249 printControllerActivity(pw, sb, prefix, "Cellular",
4250 getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004251
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004252 pw.print(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004253 sb.setLength(0);
4254 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004255 sb.append(" Wifi Statistics:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004256 pw.println(sb.toString());
4257
Siddharth Ray3c648c42017-10-02 17:30:58 -07004258 pw.print(" Wifi data received: "); pw.println(formatBytesLocked(wifiRxTotalBytes));
4259 pw.print(" Wifi data sent: "); pw.println(formatBytesLocked(wifiTxTotalBytes));
4260 pw.print(" Wifi packets received: "); pw.println(wifiRxTotalPackets);
4261 pw.print(" Wifi packets sent: "); pw.println(wifiTxTotalPackets);
4262
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004263 sb.setLength(0);
4264 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004265 sb.append(" Wifi states:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004266 didOne = false;
4267 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004268 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004269 if (time == 0) {
4270 continue;
4271 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004272 sb.append("\n ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004273 didOne = true;
4274 sb.append(WIFI_STATE_NAMES[i]);
4275 sb.append(" ");
4276 formatTimeMs(sb, time/1000);
4277 sb.append("(");
4278 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4279 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004280 }
4281 if (!didOne) sb.append(" (no activity)");
4282 pw.println(sb.toString());
4283
4284 sb.setLength(0);
4285 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004286 sb.append(" Wifi supplicant states:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004287 didOne = false;
4288 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4289 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4290 if (time == 0) {
4291 continue;
4292 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004293 sb.append("\n ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004294 didOne = true;
4295 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4296 sb.append(" ");
4297 formatTimeMs(sb, time/1000);
4298 sb.append("(");
4299 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4300 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004301 }
4302 if (!didOne) sb.append(" (no activity)");
4303 pw.println(sb.toString());
4304
4305 sb.setLength(0);
4306 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004307 sb.append(" Wifi Rx signal strength (RSSI):");
4308 final String[] wifiRxSignalStrengthDescription = new String[]{
4309 "very poor (less than -88.75dBm): ",
4310 "poor (-88.75 to -77.5dBm): ",
4311 "moderate (-77.5dBm to -66.25dBm): ",
4312 "good (-66.25dBm to -55dBm): ",
4313 "great (greater than -55dBm): "};
Dianne Hackborn3251b902014-06-20 14:40:53 -07004314 didOne = false;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004315 final int numWifiRxBins = Math.min(NUM_WIFI_SIGNAL_STRENGTH_BINS,
4316 wifiRxSignalStrengthDescription.length);
4317 for (int i=0; i<numWifiRxBins; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004318 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4319 if (time == 0) {
4320 continue;
4321 }
4322 sb.append("\n ");
4323 sb.append(prefix);
4324 didOne = true;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004325 sb.append(" ");
4326 sb.append(wifiRxSignalStrengthDescription[i]);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004327 formatTimeMs(sb, time/1000);
4328 sb.append("(");
4329 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4330 sb.append(") ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004331 }
4332 if (!didOne) sb.append(" (no activity)");
4333 pw.println(sb.toString());
4334
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004335 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004336
Adam Lesinski50e47602015-12-04 17:04:54 -08004337 pw.print(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004338 sb.setLength(0);
4339 sb.append(prefix);
4340 sb.append(" CONNECTIVITY POWER SUMMARY END");
4341 pw.println(sb.toString());
4342 pw.println("");
4343
4344 pw.print(prefix);
Adam Lesinski50e47602015-12-04 17:04:54 -08004345 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4346 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4347
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004348 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4349 sb.setLength(0);
4350 sb.append(prefix);
4351 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4352 pw.println(sb.toString());
4353
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004354 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4355 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004356
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004357 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004358
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004359 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004360 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004361 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004362 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004363 pw.println(getDischargeStartLevel());
4364 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4365 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004366 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004367 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004368 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004369 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004370 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004371 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004372 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004373 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4374 pw.println(getDischargeAmountScreenOn());
4375 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4376 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004377 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004378 } else {
4379 pw.print(prefix); pw.println(" Device battery use since last full charge");
4380 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
4381 pw.println(getLowDischargeAmountSinceCharge());
4382 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
4383 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004384 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4385 pw.println(getDischargeAmountScreenOnSinceCharge());
4386 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4387 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004388 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004389 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004390
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004391 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004392 helper.create(this);
4393 helper.refreshStats(which, UserHandle.USER_ALL);
4394 List<BatterySipper> sippers = helper.getUsageList();
4395 if (sippers != null && sippers.size() > 0) {
4396 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4397 pw.print(prefix); pw.print(" Capacity: ");
4398 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004399 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004400 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4401 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4402 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4403 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004404 pw.println();
4405 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004406 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004407 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004408 switch (bs.drainType) {
4409 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004410 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004411 break;
4412 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004413 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004414 break;
4415 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004416 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004417 break;
4418 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004419 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004420 break;
4421 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004422 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004423 break;
4424 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004425 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004426 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004427 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004428 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004429 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004430 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004431 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004432 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004433 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004434 break;
4435 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004436 pw.print(" User "); pw.print(bs.userId);
4437 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004438 break;
4439 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004440 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004441 break;
4442 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004443 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004444 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004445 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004446 pw.print(" Camera: ");
4447 break;
4448 default:
4449 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004450 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004451 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004452 printmAh(pw, bs.totalPowerMah);
4453
Adam Lesinski57123002015-06-12 16:12:07 -07004454 if (bs.usagePowerMah != bs.totalPowerMah) {
4455 // If the usage (generic power) isn't the whole amount, we list out
4456 // what components are involved in the calculation.
4457
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004458 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004459 if (bs.usagePowerMah != 0) {
4460 pw.print(" usage=");
4461 printmAh(pw, bs.usagePowerMah);
4462 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004463 if (bs.cpuPowerMah != 0) {
4464 pw.print(" cpu=");
4465 printmAh(pw, bs.cpuPowerMah);
4466 }
4467 if (bs.wakeLockPowerMah != 0) {
4468 pw.print(" wake=");
4469 printmAh(pw, bs.wakeLockPowerMah);
4470 }
4471 if (bs.mobileRadioPowerMah != 0) {
4472 pw.print(" radio=");
4473 printmAh(pw, bs.mobileRadioPowerMah);
4474 }
4475 if (bs.wifiPowerMah != 0) {
4476 pw.print(" wifi=");
4477 printmAh(pw, bs.wifiPowerMah);
4478 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004479 if (bs.bluetoothPowerMah != 0) {
4480 pw.print(" bt=");
4481 printmAh(pw, bs.bluetoothPowerMah);
4482 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004483 if (bs.gpsPowerMah != 0) {
4484 pw.print(" gps=");
4485 printmAh(pw, bs.gpsPowerMah);
4486 }
4487 if (bs.sensorPowerMah != 0) {
4488 pw.print(" sensor=");
4489 printmAh(pw, bs.sensorPowerMah);
4490 }
4491 if (bs.cameraPowerMah != 0) {
4492 pw.print(" camera=");
4493 printmAh(pw, bs.cameraPowerMah);
4494 }
4495 if (bs.flashlightPowerMah != 0) {
4496 pw.print(" flash=");
4497 printmAh(pw, bs.flashlightPowerMah);
4498 }
4499 pw.print(" )");
4500 }
Bookatz17d7d9d2017-06-08 14:50:46 -07004501
4502 // If there is additional smearing information, include it.
4503 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
4504 pw.print(" Including smearing: ");
4505 printmAh(pw, bs.totalSmearedPowerMah);
4506 pw.print(" (");
4507 if (bs.screenPowerMah != 0) {
4508 pw.print(" screen=");
4509 printmAh(pw, bs.screenPowerMah);
4510 }
4511 if (bs.proportionalSmearMah != 0) {
4512 pw.print(" proportional=");
4513 printmAh(pw, bs.proportionalSmearMah);
4514 }
4515 pw.print(" )");
4516 }
4517 if (bs.shouldHide) {
4518 pw.print(" Excluded from smearing");
4519 }
4520
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004521 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004522 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004523 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004524 }
4525
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004526 sippers = helper.getMobilemsppList();
4527 if (sippers != null && sippers.size() > 0) {
4528 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004529 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004530 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004531 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004532 sb.setLength(0);
4533 sb.append(prefix); sb.append(" Uid ");
4534 UserHandle.formatUid(sb, bs.uidObj.getUid());
4535 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4536 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4537 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004538 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004539 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004540 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004541 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004542 sb.setLength(0);
4543 sb.append(prefix);
4544 sb.append(" TOTAL TIME: ");
4545 formatTimeMs(sb, totalTime);
4546 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4547 sb.append(")");
4548 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004549 pw.println();
4550 }
4551
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004552 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4553 @Override
4554 public int compare(TimerEntry lhs, TimerEntry rhs) {
4555 long lhsTime = lhs.mTime;
4556 long rhsTime = rhs.mTime;
4557 if (lhsTime < rhsTime) {
4558 return 1;
4559 }
4560 if (lhsTime > rhsTime) {
4561 return -1;
4562 }
4563 return 0;
4564 }
4565 };
4566
4567 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004568 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4569 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004570 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004571 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4572 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4573 : kernelWakelocks.entrySet()) {
4574 final BatteryStats.Timer timer = ent.getValue();
4575 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004576 if (totalTimeMillis > 0) {
4577 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4578 }
4579 }
4580 if (ktimers.size() > 0) {
4581 Collections.sort(ktimers, timerComparator);
4582 pw.print(prefix); pw.println(" All kernel wake locks:");
4583 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004584 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004585 String linePrefix = ": ";
4586 sb.setLength(0);
4587 sb.append(prefix);
4588 sb.append(" Kernel Wake lock ");
4589 sb.append(timer.mName);
4590 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4591 which, linePrefix);
4592 if (!linePrefix.equals(": ")) {
4593 sb.append(" realtime");
4594 // Only print out wake locks that were held
4595 pw.println(sb.toString());
4596 }
4597 }
4598 pw.println();
4599 }
4600 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004601
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004602 if (timers.size() > 0) {
4603 Collections.sort(timers, timerComparator);
4604 pw.print(prefix); pw.println(" All partial wake locks:");
4605 for (int i=0; i<timers.size(); i++) {
4606 TimerEntry timer = timers.get(i);
4607 sb.setLength(0);
4608 sb.append(" Wake lock ");
4609 UserHandle.formatUid(sb, timer.mId);
4610 sb.append(" ");
4611 sb.append(timer.mName);
4612 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4613 sb.append(" realtime");
4614 pw.println(sb.toString());
4615 }
4616 timers.clear();
4617 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08004618 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004619
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004620 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004621 if (wakeupReasons.size() > 0) {
4622 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004623 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004624 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004625 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004626 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
4627 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004628 }
4629 Collections.sort(reasons, timerComparator);
4630 for (int i=0; i<reasons.size(); i++) {
4631 TimerEntry timer = reasons.get(i);
4632 String linePrefix = ": ";
4633 sb.setLength(0);
4634 sb.append(prefix);
4635 sb.append(" Wakeup reason ");
4636 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004637 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4638 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004639 pw.println(sb.toString());
4640 }
4641 pw.println();
4642 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004643 }
Evan Millar22ac0432009-03-31 11:33:18 -07004644
James Carr2dd7e5e2016-07-20 18:48:39 -07004645 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
Bookatz50df7112017-08-04 14:53:26 -07004646 if (mMemoryStats.size() > 0) {
4647 pw.println(" Memory Stats");
4648 for (int i = 0; i < mMemoryStats.size(); i++) {
4649 sb.setLength(0);
4650 sb.append(" Bandwidth ");
4651 sb.append(mMemoryStats.keyAt(i));
4652 sb.append(" Time ");
4653 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
4654 pw.println(sb.toString());
4655 }
4656 pw.println();
4657 }
4658
4659 final Map<String, ? extends Timer> rpmStats = getRpmStats();
4660 if (rpmStats.size() > 0) {
4661 pw.print(prefix); pw.println(" Resource Power Manager Stats");
4662 if (rpmStats.size() > 0) {
4663 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
4664 final String timerName = ent.getKey();
4665 final Timer timer = ent.getValue();
4666 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
4667 }
4668 }
4669 pw.println();
4670 }
Bookatz82b341172017-09-07 19:06:08 -07004671 if (SCREEN_OFF_RPM_STATS_ENABLED) {
4672 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
Bookatz50df7112017-08-04 14:53:26 -07004673 if (screenOffRpmStats.size() > 0) {
Bookatz82b341172017-09-07 19:06:08 -07004674 pw.print(prefix);
4675 pw.println(" Resource Power Manager Stats for when screen was off");
4676 if (screenOffRpmStats.size() > 0) {
4677 for (Map.Entry<String, ? extends Timer> ent : screenOffRpmStats.entrySet()) {
4678 final String timerName = ent.getKey();
4679 final Timer timer = ent.getValue();
4680 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
4681 }
Bookatz50df7112017-08-04 14:53:26 -07004682 }
Bookatz82b341172017-09-07 19:06:08 -07004683 pw.println();
Bookatz50df7112017-08-04 14:53:26 -07004684 }
James Carr2dd7e5e2016-07-20 18:48:39 -07004685 }
4686
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004687 final long[] cpuFreqs = getCpuFreqs();
4688 if (cpuFreqs != null) {
4689 sb.setLength(0);
Bookatz50df7112017-08-04 14:53:26 -07004690 sb.append(" CPU freqs:");
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004691 for (int i = 0; i < cpuFreqs.length; ++i) {
4692 sb.append(" " + cpuFreqs[i]);
4693 }
4694 pw.println(sb.toString());
Bookatz50df7112017-08-04 14:53:26 -07004695 pw.println();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004696 }
4697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004698 for (int iu=0; iu<NU; iu++) {
4699 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004700 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004701 continue;
4702 }
Bookatzc8c44962017-05-11 12:12:54 -07004703
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004704 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07004705
4706 pw.print(prefix);
4707 pw.print(" ");
4708 UserHandle.formatUid(pw, uid);
4709 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004710 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004711
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004712 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4713 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4714 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4715 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004716 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4717 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
4718
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004719 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4720 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004721 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4722 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004723
4724 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
4725 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
4726
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004727 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4728 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4729 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08004730 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
4731 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4732 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
4733 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004734 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004735
Adam Lesinski5f056f62016-07-14 16:56:08 -07004736 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
4737 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
4738
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004739 if (mobileRxBytes > 0 || mobileTxBytes > 0
4740 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004741 pw.print(prefix); pw.print(" Mobile network: ");
4742 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004743 pw.print(formatBytesLocked(mobileTxBytes));
4744 pw.print(" sent (packets "); pw.print(mobileRxPackets);
4745 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004746 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004747 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
4748 sb.setLength(0);
4749 sb.append(prefix); sb.append(" Mobile radio active: ");
4750 formatTimeMs(sb, uidMobileActiveTime / 1000);
4751 sb.append("(");
4752 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
4753 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
4754 long packets = mobileRxPackets + mobileTxPackets;
4755 if (packets == 0) {
4756 packets = 1;
4757 }
4758 sb.append(" @ ");
4759 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
4760 sb.append(" mspp");
4761 pw.println(sb.toString());
4762 }
4763
Adam Lesinski5f056f62016-07-14 16:56:08 -07004764 if (mobileWakeup > 0) {
4765 sb.setLength(0);
4766 sb.append(prefix);
4767 sb.append(" Mobile radio AP wakeups: ");
4768 sb.append(mobileWakeup);
4769 pw.println(sb.toString());
4770 }
4771
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004772 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
4773 u.getModemControllerActivity(), which);
4774
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004775 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004776 pw.print(prefix); pw.print(" Wi-Fi network: ");
4777 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004778 pw.print(formatBytesLocked(wifiTxBytes));
4779 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4780 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004781 }
4782
Dianne Hackborn62793e42015-03-09 11:15:41 -07004783 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08004784 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004785 || uidWifiRunningTime != 0) {
4786 sb.setLength(0);
4787 sb.append(prefix); sb.append(" Wifi Running: ");
4788 formatTimeMs(sb, uidWifiRunningTime / 1000);
4789 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4790 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07004791 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004792 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4793 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4794 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08004795 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004796 formatTimeMs(sb, wifiScanTime / 1000);
4797 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004798 whichBatteryRealtime)); sb.append(") ");
4799 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08004800 sb.append("x\n");
4801 // actual and background times are unpooled and since reset (regardless of 'which')
4802 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
4803 formatTimeMs(sb, wifiScanActualTime / 1000);
4804 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
4805 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4806 sb.append(") ");
4807 sb.append(wifiScanCount);
4808 sb.append("x\n");
4809 sb.append(prefix); sb.append(" Background Wifi Scan: ");
4810 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
4811 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
4812 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4813 sb.append(") ");
4814 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07004815 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004816 pw.println(sb.toString());
4817 }
4818
Adam Lesinski5f056f62016-07-14 16:56:08 -07004819 if (wifiWakeup > 0) {
4820 sb.setLength(0);
4821 sb.append(prefix);
4822 sb.append(" WiFi AP wakeups: ");
4823 sb.append(wifiWakeup);
4824 pw.println(sb.toString());
4825 }
4826
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004827 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
4828 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07004829
Adam Lesinski50e47602015-12-04 17:04:54 -08004830 if (btRxBytes > 0 || btTxBytes > 0) {
4831 pw.print(prefix); pw.print(" Bluetooth network: ");
4832 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
4833 pw.print(formatBytesLocked(btTxBytes));
4834 pw.println(" sent");
4835 }
4836
Bookatz867c0d72017-03-07 18:23:42 -08004837 final Timer bleTimer = u.getBluetoothScanTimer();
4838 if (bleTimer != null) {
4839 // Convert from microseconds to milliseconds with rounding
4840 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
4841 / 1000;
4842 if (totalTimeMs != 0) {
4843 final int count = bleTimer.getCountLocked(which);
4844 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
4845 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004846 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4847 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
4848 final long actualTimeMsBg = bleTimerBg != null ?
4849 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07004850 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07004851 final int resultCount = u.getBluetoothScanResultCounter() != null ?
4852 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07004853 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
4854 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
4855 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
4856 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
4857 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
4858 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4859 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
4860 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
4861 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
4862 final Timer unoptimizedScanTimerBg =
4863 u.getBluetoothUnoptimizedScanBackgroundTimer();
4864 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
4865 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4866 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
4867 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004868
4869 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08004870 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07004871 sb.append(prefix);
4872 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08004873 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07004874 sb.append(" (");
4875 sb.append(count);
4876 sb.append(" times)");
4877 if (bleTimer.isRunningLocked()) {
4878 sb.append(" (currently running)");
4879 }
4880 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08004881 }
Bookatzb1f04f32017-05-19 13:57:32 -07004882
4883 sb.append(prefix);
4884 sb.append(" Bluetooth Scan (total actual realtime): ");
4885 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
4886 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08004887 sb.append(count);
4888 sb.append(" times)");
4889 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07004890 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08004891 }
Bookatzb1f04f32017-05-19 13:57:32 -07004892 sb.append("\n");
4893 if (actualTimeMsBg > 0 || countBg > 0) {
4894 sb.append(prefix);
4895 sb.append(" Bluetooth Scan (background realtime): ");
4896 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
4897 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08004898 sb.append(countBg);
4899 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07004900 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
4901 sb.append(" (currently running in background)");
4902 }
4903 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08004904 }
Bookatzb1f04f32017-05-19 13:57:32 -07004905
4906 sb.append(prefix);
4907 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07004908 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07004909 sb.append(" (");
4910 sb.append(resultCountBg);
4911 sb.append(" in background)");
4912
4913 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
4914 sb.append("\n");
4915 sb.append(prefix);
4916 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
4917 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
4918 sb.append(" (max ");
4919 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
4920 sb.append(")");
4921 if (unoptimizedScanTimer != null
4922 && unoptimizedScanTimer.isRunningLocked()) {
4923 sb.append(" (currently running unoptimized)");
4924 }
4925 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
4926 sb.append("\n");
4927 sb.append(prefix);
4928 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
4929 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
4930 sb.append(" (max ");
4931 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
4932 sb.append(")");
4933 if (unoptimizedScanTimerBg.isRunningLocked()) {
4934 sb.append(" (currently running unoptimized in background)");
4935 }
4936 }
4937 }
Bookatz867c0d72017-03-07 18:23:42 -08004938 pw.println(sb.toString());
4939 uidActivity = true;
4940 }
4941 }
4942
4943
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004944
Dianne Hackborn617f8772009-03-31 15:04:46 -07004945 if (u.hasUserActivity()) {
4946 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07004947 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004948 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004949 if (val != 0) {
4950 if (!hasData) {
4951 sb.setLength(0);
4952 sb.append(" User activity: ");
4953 hasData = true;
4954 } else {
4955 sb.append(", ");
4956 }
4957 sb.append(val);
4958 sb.append(" ");
4959 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
4960 }
4961 }
4962 if (hasData) {
4963 pw.println(sb.toString());
4964 }
4965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004966
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004967 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4968 = u.getWakelockStats();
4969 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004970 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004971 int countWakelock = 0;
4972 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4973 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4974 String linePrefix = ": ";
4975 sb.setLength(0);
4976 sb.append(prefix);
4977 sb.append(" Wake lock ");
4978 sb.append(wakelocks.keyAt(iw));
4979 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
4980 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004981 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4982 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004983 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004984 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
4985 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004986 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
4987 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004988 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
4989 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07004990 sb.append(" realtime");
4991 pw.println(sb.toString());
4992 uidActivity = true;
4993 countWakelock++;
4994
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004995 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
4996 rawRealtime, which);
4997 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
4998 rawRealtime, which);
4999 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
5000 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005001 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07005002 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005003 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005004 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07005005 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
5006 // pooled and therefore just a lower bound)
5007 long actualTotalPartialWakelock = 0;
5008 long actualBgPartialWakelock = 0;
5009 if (u.getAggregatedPartialWakelockTimer() != null) {
5010 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
5011 // Convert from microseconds to milliseconds with rounding
5012 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07005013 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07005014 final Timer bgAggTimer = aggTimer.getSubTimer();
5015 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005016 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07005017 }
5018
5019 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
5020 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
5021 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005022 sb.setLength(0);
5023 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005024 sb.append(" TOTAL wake: ");
5025 boolean needComma = false;
5026 if (totalFullWakelock != 0) {
5027 needComma = true;
5028 formatTimeMs(sb, totalFullWakelock);
5029 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005030 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005031 if (totalPartialWakelock != 0) {
5032 if (needComma) {
5033 sb.append(", ");
5034 }
5035 needComma = true;
5036 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07005037 sb.append("blamed partial");
5038 }
5039 if (actualTotalPartialWakelock != 0) {
5040 if (needComma) {
5041 sb.append(", ");
5042 }
5043 needComma = true;
5044 formatTimeMs(sb, actualTotalPartialWakelock);
5045 sb.append("actual partial");
5046 }
5047 if (actualBgPartialWakelock != 0) {
5048 if (needComma) {
5049 sb.append(", ");
5050 }
5051 needComma = true;
5052 formatTimeMs(sb, actualBgPartialWakelock);
5053 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005054 }
5055 if (totalWindowWakelock != 0) {
5056 if (needComma) {
5057 sb.append(", ");
5058 }
5059 needComma = true;
5060 formatTimeMs(sb, totalWindowWakelock);
5061 sb.append("window");
5062 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005063 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07005064 if (needComma) {
5065 sb.append(",");
5066 }
5067 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005068 formatTimeMs(sb, totalDrawWakelock);
5069 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07005070 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005071 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005072 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005073 }
5074 }
5075
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005076 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
5077 for (int isy=syncs.size()-1; isy>=0; isy--) {
5078 final Timer timer = syncs.valueAt(isy);
5079 // Convert from microseconds to milliseconds with rounding
5080 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5081 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07005082 final Timer bgTimer = timer.getSubTimer();
5083 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005084 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07005085 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005086 sb.setLength(0);
5087 sb.append(prefix);
5088 sb.append(" Sync ");
5089 sb.append(syncs.keyAt(isy));
5090 sb.append(": ");
5091 if (totalTime != 0) {
5092 formatTimeMs(sb, totalTime);
5093 sb.append("realtime (");
5094 sb.append(count);
5095 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07005096 if (bgTime > 0) {
5097 sb.append(", ");
5098 formatTimeMs(sb, bgTime);
5099 sb.append("background (");
5100 sb.append(bgCount);
5101 sb.append(" times)");
5102 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005103 } else {
5104 sb.append("(not used)");
5105 }
5106 pw.println(sb.toString());
5107 uidActivity = true;
5108 }
5109
5110 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
5111 for (int ij=jobs.size()-1; ij>=0; ij--) {
5112 final Timer timer = jobs.valueAt(ij);
5113 // Convert from microseconds to milliseconds with rounding
5114 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5115 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07005116 final Timer bgTimer = timer.getSubTimer();
5117 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005118 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07005119 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005120 sb.setLength(0);
5121 sb.append(prefix);
5122 sb.append(" Job ");
5123 sb.append(jobs.keyAt(ij));
5124 sb.append(": ");
5125 if (totalTime != 0) {
5126 formatTimeMs(sb, totalTime);
5127 sb.append("realtime (");
5128 sb.append(count);
5129 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07005130 if (bgTime > 0) {
5131 sb.append(", ");
5132 formatTimeMs(sb, bgTime);
5133 sb.append("background (");
5134 sb.append(bgCount);
5135 sb.append(" times)");
5136 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005137 } else {
5138 sb.append("(not used)");
5139 }
5140 pw.println(sb.toString());
5141 uidActivity = true;
5142 }
5143
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005144 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
5145 for (int ic=completions.size()-1; ic>=0; ic--) {
5146 SparseIntArray types = completions.valueAt(ic);
5147 if (types != null) {
5148 pw.print(prefix);
5149 pw.print(" Job Completions ");
5150 pw.print(completions.keyAt(ic));
5151 pw.print(":");
5152 for (int it=0; it<types.size(); it++) {
5153 pw.print(" ");
5154 pw.print(JobParameters.getReasonName(types.keyAt(it)));
5155 pw.print("(");
5156 pw.print(types.valueAt(it));
5157 pw.print("x)");
5158 }
5159 pw.println();
5160 }
5161 }
5162
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005163 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
5164 prefix, "Flashlight");
5165 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
5166 prefix, "Camera");
5167 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
5168 prefix, "Video");
5169 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
5170 prefix, "Audio");
5171
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005172 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
5173 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005174 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005175 final Uid.Sensor se = sensors.valueAt(ise);
5176 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005177 sb.setLength(0);
5178 sb.append(prefix);
5179 sb.append(" Sensor ");
5180 int handle = se.getHandle();
5181 if (handle == Uid.Sensor.GPS) {
5182 sb.append("GPS");
5183 } else {
5184 sb.append(handle);
5185 }
5186 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005187
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005188 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005189 if (timer != null) {
5190 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005191 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5192 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005193 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005194 final Timer bgTimer = se.getSensorBackgroundTime();
5195 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005196 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5197 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5198 final long bgActualTime = bgTimer != null ?
5199 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5200
Dianne Hackborn61659e52014-07-09 16:13:01 -07005201 //timer.logState();
5202 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005203 if (actualTime != totalTime) {
5204 formatTimeMs(sb, totalTime);
5205 sb.append("blamed realtime, ");
5206 }
5207
5208 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005209 sb.append("realtime (");
5210 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005211 sb.append(" times)");
5212
5213 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005214 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005215 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5216 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005217 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005218 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005220 } else {
5221 sb.append("(not used)");
5222 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005223 } else {
5224 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005226
5227 pw.println(sb.toString());
5228 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005229 }
5230
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005231 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5232 "Vibrator");
5233 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5234 prefix, "Foreground activities");
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005235 uidActivity |= printTimer(pw, sb, u.getForegroundServiceTimer(), rawRealtime, which,
5236 prefix, "Foreground services");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005237
Dianne Hackborn61659e52014-07-09 16:13:01 -07005238 long totalStateTime = 0;
5239 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5240 long time = u.getProcessStateTime(ips, rawRealtime, which);
5241 if (time > 0) {
5242 totalStateTime += time;
5243 sb.setLength(0);
5244 sb.append(prefix);
5245 sb.append(" ");
5246 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5247 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005248 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005249 pw.println(sb.toString());
5250 uidActivity = true;
5251 }
5252 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005253 if (totalStateTime > 0) {
5254 sb.setLength(0);
5255 sb.append(prefix);
5256 sb.append(" Total running: ");
5257 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5258 pw.println(sb.toString());
5259 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005260
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005261 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5262 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005263 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005264 sb.setLength(0);
5265 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005266 sb.append(" Total cpu time: u=");
5267 formatTimeMs(sb, userCpuTimeUs / 1000);
5268 sb.append("s=");
5269 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005270 pw.println(sb.toString());
5271 }
5272
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005273 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5274 if (cpuFreqTimes != null) {
5275 sb.setLength(0);
5276 sb.append(" Total cpu time per freq:");
5277 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5278 sb.append(" " + cpuFreqTimes[i]);
5279 }
5280 pw.println(sb.toString());
5281 }
5282 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5283 if (screenOffCpuFreqTimes != null) {
5284 sb.setLength(0);
5285 sb.append(" Total screen-off cpu time per freq:");
5286 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5287 sb.append(" " + screenOffCpuFreqTimes[i]);
5288 }
5289 pw.println(sb.toString());
5290 }
5291
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005292 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5293 = u.getProcessStats();
5294 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5295 final Uid.Proc ps = processStats.valueAt(ipr);
5296 long userTime;
5297 long systemTime;
5298 long foregroundTime;
5299 int starts;
5300 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005302 userTime = ps.getUserTime(which);
5303 systemTime = ps.getSystemTime(which);
5304 foregroundTime = ps.getForegroundTime(which);
5305 starts = ps.getStarts(which);
5306 final int numCrashes = ps.getNumCrashes(which);
5307 final int numAnrs = ps.getNumAnrs(which);
5308 numExcessive = which == STATS_SINCE_CHARGED
5309 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005310
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005311 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5312 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5313 sb.setLength(0);
5314 sb.append(prefix); sb.append(" Proc ");
5315 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5316 sb.append(prefix); sb.append(" CPU: ");
5317 formatTimeMs(sb, userTime); sb.append("usr + ");
5318 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5319 formatTimeMs(sb, foregroundTime); sb.append("fg");
5320 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5321 sb.append("\n"); sb.append(prefix); sb.append(" ");
5322 boolean hasOne = false;
5323 if (starts != 0) {
5324 hasOne = true;
5325 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005326 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005327 if (numCrashes != 0) {
5328 if (hasOne) {
5329 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005330 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005331 hasOne = true;
5332 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005333 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005334 if (numAnrs != 0) {
5335 if (hasOne) {
5336 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005337 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005338 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005339 }
5340 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005341 pw.println(sb.toString());
5342 for (int e=0; e<numExcessive; e++) {
5343 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5344 if (ew != null) {
5345 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07005346 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005347 pw.print("cpu");
5348 } else {
5349 pw.print("unknown");
5350 }
5351 pw.print(" use: ");
5352 TimeUtils.formatDuration(ew.usedTime, pw);
5353 pw.print(" over ");
5354 TimeUtils.formatDuration(ew.overTime, pw);
5355 if (ew.overTime != 0) {
5356 pw.print(" (");
5357 pw.print((ew.usedTime*100)/ew.overTime);
5358 pw.println("%)");
5359 }
5360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005361 }
5362 uidActivity = true;
5363 }
5364 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005365
5366 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5367 = u.getPackageStats();
5368 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
5369 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
5370 pw.println(":");
5371 boolean apkActivity = false;
5372 final Uid.Pkg ps = packageStats.valueAt(ipkg);
5373 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
5374 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
5375 pw.print(prefix); pw.print(" Wakeup alarm ");
5376 pw.print(alarms.keyAt(iwa)); pw.print(": ");
5377 pw.print(alarms.valueAt(iwa).getCountLocked(which));
5378 pw.println(" times");
5379 apkActivity = true;
5380 }
5381 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
5382 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
5383 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
5384 final long startTime = ss.getStartTime(batteryUptime, which);
5385 final int starts = ss.getStarts(which);
5386 final int launches = ss.getLaunches(which);
5387 if (startTime != 0 || starts != 0 || launches != 0) {
5388 sb.setLength(0);
5389 sb.append(prefix); sb.append(" Service ");
5390 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
5391 sb.append(prefix); sb.append(" Created for: ");
5392 formatTimeMs(sb, startTime / 1000);
5393 sb.append("uptime\n");
5394 sb.append(prefix); sb.append(" Starts: ");
5395 sb.append(starts);
5396 sb.append(", launches: "); sb.append(launches);
5397 pw.println(sb.toString());
5398 apkActivity = true;
5399 }
5400 }
5401 if (!apkActivity) {
5402 pw.print(prefix); pw.println(" (nothing executed)");
5403 }
5404 uidActivity = true;
5405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005406 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005407 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005408 }
5409 }
5410 }
5411
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005412 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005413 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005414 int diff = oldval ^ newval;
5415 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005416 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005417 for (int i=0; i<descriptions.length; i++) {
5418 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005419 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005420 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005421 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005422 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005423 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005424 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
5425 didWake = true;
5426 pw.print("=");
5427 if (longNames) {
5428 UserHandle.formatUid(pw, wakelockTag.uid);
5429 pw.print(":\"");
5430 pw.print(wakelockTag.string);
5431 pw.print("\"");
5432 } else {
5433 pw.print(wakelockTag.poolIdx);
5434 }
5435 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005436 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005437 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005438 pw.print("=");
5439 int val = (newval&bd.mask)>>bd.shift;
5440 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005441 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005442 } else {
5443 pw.print(val);
5444 }
5445 }
5446 }
5447 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005448 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07005449 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005450 if (longNames) {
5451 UserHandle.formatUid(pw, wakelockTag.uid);
5452 pw.print(":\"");
5453 pw.print(wakelockTag.string);
5454 pw.print("\"");
5455 } else {
5456 pw.print(wakelockTag.poolIdx);
5457 }
5458 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005459 }
5460
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005461 public void prepareForDumpLocked() {
5462 }
5463
5464 public static class HistoryPrinter {
5465 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005466 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005467 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005468 int oldStatus = -1;
5469 int oldHealth = -1;
5470 int oldPlug = -1;
5471 int oldTemp = -1;
5472 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005473 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005474 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005475
Dianne Hackborn3251b902014-06-20 14:40:53 -07005476 void reset() {
5477 oldState = oldState2 = 0;
5478 oldLevel = -1;
5479 oldStatus = -1;
5480 oldHealth = -1;
5481 oldPlug = -1;
5482 oldTemp = -1;
5483 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005484 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07005485 }
5486
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005487 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005488 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005489 if (!checkin) {
5490 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005491 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005492 pw.print(" (");
5493 pw.print(rec.numReadInts);
5494 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005495 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005496 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5497 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005498 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005499 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005500 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005501 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005502 }
5503 lastTime = rec.time;
5504 }
5505 if (rec.cmd == HistoryItem.CMD_START) {
5506 if (checkin) {
5507 pw.print(":");
5508 }
5509 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005510 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005511 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
5512 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005513 if (checkin) {
5514 pw.print(":");
5515 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005516 if (rec.cmd == HistoryItem.CMD_RESET) {
5517 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005518 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005519 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005520 pw.print("TIME:");
5521 if (checkin) {
5522 pw.println(rec.currentTime);
5523 } else {
5524 pw.print(" ");
5525 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5526 rec.currentTime).toString());
5527 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005528 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
5529 if (checkin) {
5530 pw.print(":");
5531 }
5532 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005533 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
5534 if (checkin) {
5535 pw.print(":");
5536 }
5537 pw.println("*OVERFLOW*");
5538 } else {
5539 if (!checkin) {
5540 if (rec.batteryLevel < 10) pw.print("00");
5541 else if (rec.batteryLevel < 100) pw.print("0");
5542 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005543 if (verbose) {
5544 pw.print(" ");
5545 if (rec.states < 0) ;
5546 else if (rec.states < 0x10) pw.print("0000000");
5547 else if (rec.states < 0x100) pw.print("000000");
5548 else if (rec.states < 0x1000) pw.print("00000");
5549 else if (rec.states < 0x10000) pw.print("0000");
5550 else if (rec.states < 0x100000) pw.print("000");
5551 else if (rec.states < 0x1000000) pw.print("00");
5552 else if (rec.states < 0x10000000) pw.print("0");
5553 pw.print(Integer.toHexString(rec.states));
5554 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005555 } else {
5556 if (oldLevel != rec.batteryLevel) {
5557 oldLevel = rec.batteryLevel;
5558 pw.print(",Bl="); pw.print(rec.batteryLevel);
5559 }
5560 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005561 if (oldStatus != rec.batteryStatus) {
5562 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005563 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005564 switch (oldStatus) {
5565 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005566 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005567 break;
5568 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005569 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005570 break;
5571 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005572 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005573 break;
5574 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005575 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005576 break;
5577 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005578 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005579 break;
5580 default:
5581 pw.print(oldStatus);
5582 break;
5583 }
5584 }
5585 if (oldHealth != rec.batteryHealth) {
5586 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005587 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005588 switch (oldHealth) {
5589 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005590 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005591 break;
5592 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005593 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005594 break;
5595 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005596 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005597 break;
5598 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005599 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005600 break;
5601 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005602 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005603 break;
5604 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005605 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005606 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005607 case BatteryManager.BATTERY_HEALTH_COLD:
5608 pw.print(checkin ? "c" : "cold");
5609 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005610 default:
5611 pw.print(oldHealth);
5612 break;
5613 }
5614 }
5615 if (oldPlug != rec.batteryPlugType) {
5616 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005617 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005618 switch (oldPlug) {
5619 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005620 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005621 break;
5622 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005623 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005624 break;
5625 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005626 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005627 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005628 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005629 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005630 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005631 default:
5632 pw.print(oldPlug);
5633 break;
5634 }
5635 }
5636 if (oldTemp != rec.batteryTemperature) {
5637 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005638 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005639 pw.print(oldTemp);
5640 }
5641 if (oldVolt != rec.batteryVoltage) {
5642 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005643 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005644 pw.print(oldVolt);
5645 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005646 final int chargeMAh = rec.batteryChargeUAh / 1000;
5647 if (oldChargeMAh != chargeMAh) {
5648 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07005649 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005650 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07005651 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005652 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005653 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005654 printBitDescriptions(pw, oldState2, rec.states2, null,
5655 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005656 if (rec.wakeReasonTag != null) {
5657 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005658 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005659 pw.print(rec.wakeReasonTag.poolIdx);
5660 } else {
5661 pw.print(" wake_reason=");
5662 pw.print(rec.wakeReasonTag.uid);
5663 pw.print(":\"");
5664 pw.print(rec.wakeReasonTag.string);
5665 pw.print("\"");
5666 }
5667 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08005668 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005669 pw.print(checkin ? "," : " ");
5670 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
5671 pw.print("+");
5672 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
5673 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005674 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005675 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
5676 : HISTORY_EVENT_NAMES;
5677 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
5678 | HistoryItem.EVENT_FLAG_FINISH);
5679 if (idx >= 0 && idx < eventNames.length) {
5680 pw.print(eventNames[idx]);
5681 } else {
5682 pw.print(checkin ? "Ev" : "event");
5683 pw.print(idx);
5684 }
5685 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005686 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005687 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08005688 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08005689 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
5690 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005691 pw.print(":\"");
5692 pw.print(rec.eventTag.string);
5693 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005694 }
5695 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005696 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005697 if (rec.stepDetails != null) {
5698 if (!checkin) {
5699 pw.print(" Details: cpu=");
5700 pw.print(rec.stepDetails.userTime);
5701 pw.print("u+");
5702 pw.print(rec.stepDetails.systemTime);
5703 pw.print("s");
5704 if (rec.stepDetails.appCpuUid1 >= 0) {
5705 pw.print(" (");
5706 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
5707 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5708 if (rec.stepDetails.appCpuUid2 >= 0) {
5709 pw.print(", ");
5710 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
5711 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5712 }
5713 if (rec.stepDetails.appCpuUid3 >= 0) {
5714 pw.print(", ");
5715 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
5716 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5717 }
5718 pw.print(')');
5719 }
5720 pw.println();
5721 pw.print(" /proc/stat=");
5722 pw.print(rec.stepDetails.statUserTime);
5723 pw.print(" usr, ");
5724 pw.print(rec.stepDetails.statSystemTime);
5725 pw.print(" sys, ");
5726 pw.print(rec.stepDetails.statIOWaitTime);
5727 pw.print(" io, ");
5728 pw.print(rec.stepDetails.statIrqTime);
5729 pw.print(" irq, ");
5730 pw.print(rec.stepDetails.statSoftIrqTime);
5731 pw.print(" sirq, ");
5732 pw.print(rec.stepDetails.statIdlTime);
5733 pw.print(" idle");
5734 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
5735 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
5736 + rec.stepDetails.statSoftIrqTime;
5737 int total = totalRun + rec.stepDetails.statIdlTime;
5738 if (total > 0) {
5739 pw.print(" (");
5740 float perc = ((float)totalRun) / ((float)total) * 100;
5741 pw.print(String.format("%.1f%%", perc));
5742 pw.print(" of ");
5743 StringBuilder sb = new StringBuilder(64);
5744 formatTimeMsNoSpace(sb, total*10);
5745 pw.print(sb);
5746 pw.print(")");
5747 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005748 pw.print(", PlatformIdleStat ");
5749 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005750 pw.println();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00005751
5752 pw.print(", SubsystemPowerState ");
5753 pw.print(rec.stepDetails.statSubsystemPowerState);
5754 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005755 } else {
5756 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5757 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
5758 pw.print(rec.stepDetails.userTime);
5759 pw.print(":");
5760 pw.print(rec.stepDetails.systemTime);
5761 if (rec.stepDetails.appCpuUid1 >= 0) {
5762 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
5763 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5764 if (rec.stepDetails.appCpuUid2 >= 0) {
5765 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
5766 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5767 }
5768 if (rec.stepDetails.appCpuUid3 >= 0) {
5769 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
5770 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5771 }
5772 }
5773 pw.println();
5774 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5775 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
5776 pw.print(rec.stepDetails.statUserTime);
5777 pw.print(',');
5778 pw.print(rec.stepDetails.statSystemTime);
5779 pw.print(',');
5780 pw.print(rec.stepDetails.statIOWaitTime);
5781 pw.print(',');
5782 pw.print(rec.stepDetails.statIrqTime);
5783 pw.print(',');
5784 pw.print(rec.stepDetails.statSoftIrqTime);
5785 pw.print(',');
5786 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005787 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005788 if (rec.stepDetails.statPlatformIdleState != null) {
5789 pw.print(rec.stepDetails.statPlatformIdleState);
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07005790 if (rec.stepDetails.statSubsystemPowerState != null) {
5791 pw.print(',');
5792 }
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005793 }
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00005794
5795 if (rec.stepDetails.statSubsystemPowerState != null) {
5796 pw.print(rec.stepDetails.statSubsystemPowerState);
5797 }
5798 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005799 }
5800 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005801 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005802 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005803 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005804 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005805
5806 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
5807 UserHandle.formatUid(pw, uid);
5808 pw.print("=");
5809 pw.print(utime);
5810 pw.print("u+");
5811 pw.print(stime);
5812 pw.print("s");
5813 }
5814
5815 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
5816 pw.print('/');
5817 pw.print(uid);
5818 pw.print(":");
5819 pw.print(utime);
5820 pw.print(":");
5821 pw.print(stime);
5822 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005823 }
5824
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005825 private void printSizeValue(PrintWriter pw, long size) {
5826 float result = size;
5827 String suffix = "";
5828 if (result >= 10*1024) {
5829 suffix = "KB";
5830 result = result / 1024;
5831 }
5832 if (result >= 10*1024) {
5833 suffix = "MB";
5834 result = result / 1024;
5835 }
5836 if (result >= 10*1024) {
5837 suffix = "GB";
5838 result = result / 1024;
5839 }
5840 if (result >= 10*1024) {
5841 suffix = "TB";
5842 result = result / 1024;
5843 }
5844 if (result >= 10*1024) {
5845 suffix = "PB";
5846 result = result / 1024;
5847 }
5848 pw.print((int)result);
5849 pw.print(suffix);
5850 }
5851
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005852 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
5853 String label3, long estimatedTime) {
5854 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005855 return false;
5856 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005857 pw.print(label1);
5858 pw.print(label2);
5859 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005860 StringBuilder sb = new StringBuilder(64);
5861 formatTimeMs(sb, estimatedTime);
5862 pw.print(sb);
5863 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005864 return true;
5865 }
5866
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005867 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
5868 LevelStepTracker steps, boolean checkin) {
5869 if (steps == null) {
5870 return false;
5871 }
5872 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005873 if (count <= 0) {
5874 return false;
5875 }
5876 if (!checkin) {
5877 pw.println(header);
5878 }
Kweku Adams030980a2015-04-01 16:07:48 -07005879 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005880 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005881 long duration = steps.getDurationAt(i);
5882 int level = steps.getLevelAt(i);
5883 long initMode = steps.getInitModeAt(i);
5884 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005885 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005886 lineArgs[0] = Long.toString(duration);
5887 lineArgs[1] = Integer.toString(level);
5888 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5889 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5890 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
5891 case Display.STATE_ON: lineArgs[2] = "s+"; break;
5892 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
5893 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07005894 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005895 }
5896 } else {
5897 lineArgs[2] = "";
5898 }
5899 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5900 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
5901 } else {
5902 lineArgs[3] = "";
5903 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005904 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07005905 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005906 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07005907 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005908 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005909 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
5910 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005911 pw.print(prefix);
5912 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005913 TimeUtils.formatDuration(duration, pw);
5914 pw.print(" to "); pw.print(level);
5915 boolean haveModes = false;
5916 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5917 pw.print(" (");
5918 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5919 case Display.STATE_OFF: pw.print("screen-off"); break;
5920 case Display.STATE_ON: pw.print("screen-on"); break;
5921 case Display.STATE_DOZE: pw.print("screen-doze"); break;
5922 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07005923 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005924 }
5925 haveModes = true;
5926 }
5927 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5928 pw.print(haveModes ? ", " : " (");
5929 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
5930 ? "power-save-on" : "power-save-off");
5931 haveModes = true;
5932 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005933 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
5934 pw.print(haveModes ? ", " : " (");
5935 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
5936 ? "device-idle-on" : "device-idle-off");
5937 haveModes = true;
5938 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005939 if (haveModes) {
5940 pw.print(")");
5941 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005942 pw.println();
5943 }
5944 }
5945 return true;
5946 }
5947
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005948 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005949 public static final int DUMP_DAILY_ONLY = 1<<2;
5950 public static final int DUMP_HISTORY_ONLY = 1<<3;
5951 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
5952 public static final int DUMP_VERBOSE = 1<<5;
5953 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005954
Dianne Hackborn37de0982014-05-09 09:32:18 -07005955 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
5956 final HistoryPrinter hprinter = new HistoryPrinter();
5957 final HistoryItem rec = new HistoryItem();
5958 long lastTime = -1;
5959 long baseTime = -1;
5960 boolean printed = false;
5961 HistoryEventTracker tracker = null;
5962 while (getNextHistoryLocked(rec)) {
5963 lastTime = rec.time;
5964 if (baseTime < 0) {
5965 baseTime = lastTime;
5966 }
5967 if (rec.time >= histStart) {
5968 if (histStart >= 0 && !printed) {
5969 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07005970 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005971 || rec.cmd == HistoryItem.CMD_START
5972 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005973 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005974 hprinter.printNextItem(pw, rec, baseTime, checkin,
5975 (flags&DUMP_VERBOSE) != 0);
5976 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005977 } else if (rec.currentTime != 0) {
5978 printed = true;
5979 byte cmd = rec.cmd;
5980 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005981 hprinter.printNextItem(pw, rec, baseTime, checkin,
5982 (flags&DUMP_VERBOSE) != 0);
5983 rec.cmd = cmd;
5984 }
5985 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005986 if (rec.cmd != HistoryItem.CMD_UPDATE) {
5987 hprinter.printNextItem(pw, rec, baseTime, checkin,
5988 (flags&DUMP_VERBOSE) != 0);
5989 rec.cmd = HistoryItem.CMD_UPDATE;
5990 }
5991 int oldEventCode = rec.eventCode;
5992 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005993 rec.eventTag = new HistoryTag();
5994 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
5995 HashMap<String, SparseIntArray> active
5996 = tracker.getStateForEvent(i);
5997 if (active == null) {
5998 continue;
5999 }
6000 for (HashMap.Entry<String, SparseIntArray> ent
6001 : active.entrySet()) {
6002 SparseIntArray uids = ent.getValue();
6003 for (int j=0; j<uids.size(); j++) {
6004 rec.eventCode = i;
6005 rec.eventTag.string = ent.getKey();
6006 rec.eventTag.uid = uids.keyAt(j);
6007 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006008 hprinter.printNextItem(pw, rec, baseTime, checkin,
6009 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006010 rec.wakeReasonTag = null;
6011 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006012 }
6013 }
6014 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006015 rec.eventCode = oldEventCode;
6016 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006017 tracker = null;
6018 }
6019 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006020 hprinter.printNextItem(pw, rec, baseTime, checkin,
6021 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07006022 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
6023 // This is an attempt to aggregate the previous state and generate
6024 // fake events to reflect that state at the point where we start
6025 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07006026 if (tracker == null) {
6027 tracker = new HistoryEventTracker();
6028 }
6029 tracker.updateState(rec.eventCode, rec.eventTag.string,
6030 rec.eventTag.uid, rec.eventTag.poolIdx);
6031 }
6032 }
6033 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07006034 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006035 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
6036 }
6037 }
6038
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006039 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
6040 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
6041 if (steps == null) {
6042 return;
6043 }
6044 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
6045 if (timeRemaining >= 0) {
6046 pw.print(prefix); pw.print(label); pw.print(" total time: ");
6047 tmpSb.setLength(0);
6048 formatTimeMs(tmpSb, timeRemaining);
6049 pw.print(tmpSb);
6050 pw.print(" (from "); pw.print(tmpOutInt[0]);
6051 pw.println(" steps)");
6052 }
6053 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6054 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6055 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
6056 if (estimatedTime > 0) {
6057 pw.print(prefix); pw.print(label); pw.print(" ");
6058 pw.print(STEP_LEVEL_MODE_LABELS[i]);
6059 pw.print(" time: ");
6060 tmpSb.setLength(0);
6061 formatTimeMs(tmpSb, estimatedTime);
6062 pw.print(tmpSb);
6063 pw.print(" (from "); pw.print(tmpOutInt[0]);
6064 pw.println(" steps)");
6065 }
6066 }
6067 }
6068
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006069 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
6070 ArrayList<PackageChange> changes) {
6071 if (changes == null) {
6072 return;
6073 }
6074 pw.print(prefix); pw.println("Package changes:");
6075 for (int i=0; i<changes.size(); i++) {
6076 PackageChange pc = changes.get(i);
6077 if (pc.mUpdate) {
6078 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
6079 pw.print(" vers="); pw.println(pc.mVersionCode);
6080 } else {
6081 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
6082 }
6083 }
6084 }
6085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006086 /**
6087 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
6088 *
6089 * @param pw a Printer to receive the dump output.
6090 */
6091 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006092 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006093 prepareForDumpLocked();
6094
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006095 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006096 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006097
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006098 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006099 final long historyTotalSize = getHistoryTotalSize();
6100 final long historyUsedSize = getHistoryUsedSize();
6101 if (startIteratingHistoryLocked()) {
6102 try {
6103 pw.print("Battery History (");
6104 pw.print((100*historyUsedSize)/historyTotalSize);
6105 pw.print("% used, ");
6106 printSizeValue(pw, historyUsedSize);
6107 pw.print(" used of ");
6108 printSizeValue(pw, historyTotalSize);
6109 pw.print(", ");
6110 pw.print(getHistoryStringPoolSize());
6111 pw.print(" strings using ");
6112 printSizeValue(pw, getHistoryStringPoolBytes());
6113 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07006114 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006115 pw.println();
6116 } finally {
6117 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006118 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006119 }
6120
6121 if (startIteratingOldHistoryLocked()) {
6122 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006123 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006124 pw.println("Old battery History:");
6125 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006126 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006127 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006128 if (baseTime < 0) {
6129 baseTime = rec.time;
6130 }
6131 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006132 }
6133 pw.println();
6134 } finally {
6135 finishIteratingOldHistoryLocked();
6136 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006137 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006138 }
6139
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006140 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006141 return;
6142 }
6143
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006144 if (!filtering) {
6145 SparseArray<? extends Uid> uidStats = getUidStats();
6146 final int NU = uidStats.size();
6147 boolean didPid = false;
6148 long nowRealtime = SystemClock.elapsedRealtime();
6149 for (int i=0; i<NU; i++) {
6150 Uid uid = uidStats.valueAt(i);
6151 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
6152 if (pids != null) {
6153 for (int j=0; j<pids.size(); j++) {
6154 Uid.Pid pid = pids.valueAt(j);
6155 if (!didPid) {
6156 pw.println("Per-PID Stats:");
6157 didPid = true;
6158 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006159 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
6160 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006161 pw.print(" PID "); pw.print(pids.keyAt(j));
6162 pw.print(" wake time: ");
6163 TimeUtils.formatDuration(time, pw);
6164 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006165 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006166 }
6167 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006168 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006169 pw.println();
6170 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006171 }
6172
6173 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006174 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
6175 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006176 long timeRemaining = computeBatteryTimeRemaining(
6177 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006178 if (timeRemaining >= 0) {
6179 pw.print(" Estimated discharge time remaining: ");
6180 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6181 pw.println();
6182 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006183 final LevelStepTracker steps = getDischargeLevelStepTracker();
6184 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6185 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
6186 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6187 STEP_LEVEL_MODE_VALUES[i], null));
6188 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006189 pw.println();
6190 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006191 if (dumpDurationSteps(pw, " ", "Charge step durations:",
6192 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006193 long timeRemaining = computeChargeTimeRemaining(
6194 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006195 if (timeRemaining >= 0) {
6196 pw.print(" Estimated charge time remaining: ");
6197 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6198 pw.println();
6199 }
6200 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006201 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006202 }
6203 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
6204 pw.println("Daily stats:");
6205 pw.print(" Current start time: ");
6206 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6207 getCurrentDailyStartTime()).toString());
6208 pw.print(" Next min deadline: ");
6209 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6210 getNextMinDailyDeadline()).toString());
6211 pw.print(" Next max deadline: ");
6212 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6213 getNextMaxDailyDeadline()).toString());
6214 StringBuilder sb = new StringBuilder(64);
6215 int[] outInt = new int[1];
6216 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
6217 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006218 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
6219 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006220 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006221 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
6222 dsteps, false)) {
6223 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6224 sb, outInt);
6225 }
6226 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6227 csteps, false)) {
6228 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6229 sb, outInt);
6230 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006231 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006232 } else {
6233 pw.println(" Current daily steps:");
6234 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6235 sb, outInt);
6236 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6237 sb, outInt);
6238 }
6239 }
6240 DailyItem dit;
6241 int curIndex = 0;
6242 while ((dit=getDailyItemLocked(curIndex)) != null) {
6243 curIndex++;
6244 if ((flags&DUMP_DAILY_ONLY) != 0) {
6245 pw.println();
6246 }
6247 pw.print(" Daily from ");
6248 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6249 pw.print(" to ");
6250 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6251 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006252 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006253 if (dumpDurationSteps(pw, " ",
6254 " Discharge step durations:", dit.mDischargeSteps, false)) {
6255 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6256 sb, outInt);
6257 }
6258 if (dumpDurationSteps(pw, " ",
6259 " Charge step durations:", dit.mChargeSteps, false)) {
6260 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6261 sb, outInt);
6262 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006263 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006264 } else {
6265 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6266 sb, outInt);
6267 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6268 sb, outInt);
6269 }
6270 }
6271 pw.println();
6272 }
6273 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006274 pw.println("Statistics since last charge:");
6275 pw.println(" System starts: " + getStartCount()
6276 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006277 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6278 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006279 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006281 }
6282
6283 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006284 public void dumpCheckinLocked(Context context, PrintWriter pw,
6285 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006286 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006287
6288 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006289 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6290 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006291
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006292 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6293
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006294 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006295 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006296
6297 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07006298 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006299 try {
6300 for (int i=0; i<getHistoryStringPoolSize(); i++) {
6301 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6302 pw.print(HISTORY_STRING_POOL); pw.print(',');
6303 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006304 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006305 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006306 pw.print(",\"");
6307 String str = getHistoryTagPoolString(i);
6308 str = str.replace("\\", "\\\\");
6309 str = str.replace("\"", "\\\"");
6310 pw.print(str);
6311 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006312 pw.println();
6313 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006314 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006315 } finally {
6316 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08006317 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006318 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006319 }
6320
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006321 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006322 return;
6323 }
6324
Dianne Hackborne4a59512010-12-07 11:08:07 -08006325 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006326 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08006327 for (int i=0; i<apps.size(); i++) {
6328 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006329 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
6330 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08006331 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006332 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
6333 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006334 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006335 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006336 }
6337 SparseArray<? extends Uid> uidStats = getUidStats();
6338 final int NU = uidStats.size();
6339 String[] lineArgs = new String[2];
6340 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006341 int uid = UserHandle.getAppId(uidStats.keyAt(i));
6342 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
6343 if (pkgs != null && !pkgs.second.value) {
6344 pkgs.second.value = true;
6345 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08006346 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006347 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006348 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
6349 (Object[])lineArgs);
6350 }
6351 }
6352 }
6353 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006354 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006355 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006356 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07006357 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006358 if (timeRemaining >= 0) {
6359 lineArgs[0] = Long.toString(timeRemaining);
6360 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
6361 (Object[])lineArgs);
6362 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006363 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07006364 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006365 if (timeRemaining >= 0) {
6366 lineArgs[0] = Long.toString(timeRemaining);
6367 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
6368 (Object[])lineArgs);
6369 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006370 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
6371 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006374}