blob: 03a8dba5a82c924537b1facae21e24b5aa8f182c [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
Sudheer Shankab2f83c12017-11-13 19:25:01 -080019import android.app.ActivityManager;
Dianne Hackborn94326cb2017-06-28 16:17:20 -070020import android.app.job.JobParameters;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080021import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080022import android.content.pm.ApplicationInfo;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070023import android.service.batterystats.BatteryStatsServiceDumpProto;
Wink Saville52840902011-02-18 12:40:47 -080024import android.telephony.SignalStrength;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080025import android.text.format.DateFormat;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070026import android.util.ArrayMap;
James Carr2dd7e5e2016-07-20 18:48:39 -070027import android.util.LongSparseArray;
Dianne Hackborn9cfba352016-03-24 17:31:28 -070028import android.util.MutableBoolean;
29import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.util.Printer;
31import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070032import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070033import android.util.TimeUtils;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070034import android.util.proto.ProtoOutputStream;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070035import android.view.Display;
Amith Yamasaniab9ad192016-12-06 12:46:59 -080036
Sudheer Shankab2f83c12017-11-13 19:25:01 -080037import com.android.internal.annotations.VisibleForTesting;
Siddharth Ray78ccaf52017-12-23 16:16:21 -080038import com.android.internal.location.gnssmetrics.GnssMetrics;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080039import com.android.internal.os.BatterySipper;
40import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
Kweku Adams2f73ecd2017-09-27 16:59:19 -070042import java.io.FileDescriptor;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070043import java.io.PrintWriter;
44import java.util.ArrayList;
45import java.util.Collections;
46import java.util.Comparator;
47import java.util.Formatter;
48import java.util.HashMap;
49import java.util.List;
50import java.util.Map;
51
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052/**
53 * A class providing access to battery usage statistics, including information on
54 * wakelocks, processes, packages, and services. All times are represented in microseconds
55 * except where indicated otherwise.
56 * @hide
57 */
58public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070059 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
61 private static final boolean LOCAL_LOGV = false;
Bookatz82b341172017-09-07 19:06:08 -070062 /** Fetching RPM stats is too slow to do each time screen changes, so disable it. */
63 protected static final boolean SCREEN_OFF_RPM_STATS_ENABLED = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070064
65 /** @hide */
66 public static final String SERVICE_NAME = "batterystats";
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 /**
69 * A constant indicating a partial wake lock timer.
70 */
71 public static final int WAKE_TYPE_PARTIAL = 0;
72
73 /**
74 * A constant indicating a full wake lock timer.
75 */
76 public static final int WAKE_TYPE_FULL = 1;
77
78 /**
79 * A constant indicating a window wake lock timer.
80 */
81 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 /**
84 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 */
86 public static final int SENSOR = 3;
Mike Mac2f518a2017-09-19 16:06:03 -070087
The Android Open Source Project10592532009-03-18 17:39:46 -070088 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070089 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070090 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070091 public static final int WIFI_RUNNING = 4;
Mike Mac2f518a2017-09-19 16:06:03 -070092
Dianne Hackborn617f8772009-03-31 15:04:46 -070093 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070094 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070095 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070096 public static final int FULL_WIFI_LOCK = 5;
Mike Mac2f518a2017-09-19 16:06:03 -070097
The Android Open Source Project10592532009-03-18 17:39:46 -070098 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070099 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -0700100 */
Nick Pelly6ccaa542012-06-15 15:22:47 -0700101 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
Dianne Hackborn62793e42015-03-09 11:15:41 -0700103 /**
104 * A constant indicating a wifi multicast timer
105 */
106 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700109 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700110 */
111 public static final int VIDEO_TURNED_ON = 8;
112
113 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800114 * A constant indicating a vibrator on timer
115 */
116 public static final int VIBRATOR_ON = 9;
117
118 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700119 * A constant indicating a foreground activity timer
120 */
121 public static final int FOREGROUND_ACTIVITY = 10;
122
123 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700124 * A constant indicating a wifi batched scan is active
125 */
126 public static final int WIFI_BATCHED_SCAN = 11;
127
128 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700129 * A constant indicating a process state timer
130 */
131 public static final int PROCESS_STATE = 12;
132
133 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700134 * A constant indicating a sync timer
135 */
136 public static final int SYNC = 13;
137
138 /**
139 * A constant indicating a job timer
140 */
141 public static final int JOB = 14;
142
143 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800144 * A constant indicating an audio turn on timer
145 */
146 public static final int AUDIO_TURNED_ON = 15;
147
148 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700149 * A constant indicating a flashlight turn on timer
150 */
151 public static final int FLASHLIGHT_TURNED_ON = 16;
152
153 /**
154 * A constant indicating a camera turn on timer
155 */
156 public static final int CAMERA_TURNED_ON = 17;
157
158 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700159 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700160 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700161 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700162
163 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800164 * A constant indicating a bluetooth scan timer.
165 */
166 public static final int BLUETOOTH_SCAN_ON = 19;
167
168 /**
Bookatzc8c44962017-05-11 12:12:54 -0700169 * A constant indicating an aggregated partial wake lock timer.
170 */
171 public static final int AGGREGATED_WAKE_TYPE_PARTIAL = 20;
172
173 /**
Bookatzb1f04f32017-05-19 13:57:32 -0700174 * A constant indicating a bluetooth scan timer for unoptimized scans.
175 */
176 public static final int BLUETOOTH_UNOPTIMIZED_SCAN_ON = 21;
177
178 /**
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700179 * A constant indicating a foreground service timer
180 */
181 public static final int FOREGROUND_SERVICE = 22;
182
183 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -0800184 * A constant indicating an aggregate wifi multicast timer
185 */
186 public static final int WIFI_AGGREGATE_MULTICAST_ENABLED = 23;
187
188 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 * Include all of the data in the stats, including previously saved data.
190 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700191 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192
193 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 * Include only the current run in the stats.
195 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700196 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197
198 /**
199 * Include only the run since the last time the device was unplugged in the stats.
200 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700201 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700202
203 // NOTE: Update this list if you add/change any stats above.
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700204 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700205 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700206 private static final String[] STAT_NAMES = { "l", "c", "u" };
207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700209 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700210 *
211 * New in version 19:
212 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800213 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700214 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Bookatz506a8182017-05-01 14:18:42 -0700215 * New in version 21:
216 * - Actual (not just apportioned) Wakelock time is also recorded.
Bookatzc8c44962017-05-11 12:12:54 -0700217 * - Aggregated partial wakelock time (per uid, instead of per wakelock) is recorded.
Bookatzb1f04f32017-05-19 13:57:32 -0700218 * - BLE scan result count
219 * - CPU frequency time per uid
220 * New in version 22:
221 * - BLE scan result background count, BLE unoptimized scan time
Bookatz98d4d5c2017-08-01 19:07:54 -0700222 * - Background partial wakelock time & count
223 * New in version 23:
224 * - Logging smeared power model values
225 * New in version 24:
226 * - Fixed bugs in background timers and BLE scan time
227 * New in version 25:
228 * - Package wakeup alarms are now on screen-off timebase
Bookatz50df7112017-08-04 14:53:26 -0700229 * New in version 26:
Bookatz82b341172017-09-07 19:06:08 -0700230 * - Resource power manager (rpm) states [but screenOffRpm is disabled from working properly]
Mike Mac2f518a2017-09-19 16:06:03 -0700231 * New in version 27:
232 * - Always On Display (screen doze mode) time and power
Mike Ma15313c92017-11-15 17:58:21 -0800233 * New in version 28:
234 * - Light/Deep Doze power
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700235 * - WiFi Multicast Wakelock statistics (count & duration)
Kweku Adamsa8943cb2017-12-22 13:21:06 -0800236 * New in version 29:
237 * - Process states re-ordered. TOP_SLEEPING now below BACKGROUND. HEAVY_WEIGHT introduced.
238 * - CPU times per UID process state
zhouwenjie46712bc2018-01-11 15:21:27 -0800239 * New in version 30:
240 * - Uid.PROCESS_STATE_FOREGROUND_SERVICE only tracks
241 * ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE.
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700242 */
zhouwenjie46712bc2018-01-11 15:21:27 -0800243 static final int CHECKIN_VERSION = 30;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700244
245 /**
246 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700248 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700249
Evan Millar22ac0432009-03-31 11:33:18 -0700250 private static final long BYTES_PER_KB = 1024;
251 private static final long BYTES_PER_MB = 1048576; // 1024^2
252 private static final long BYTES_PER_GB = 1073741824; //1024^3
Bookatz506a8182017-05-01 14:18:42 -0700253
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700254 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800255 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700256 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700258 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700259 private static final String CPU_DATA = "cpu";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700260 private static final String GLOBAL_CPU_FREQ_DATA = "gcf";
261 private static final String CPU_TIMES_AT_FREQ_DATA = "ctf";
Bookatz50df7112017-08-04 14:53:26 -0700262 // rpm line is:
263 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "rpm", state/voter name, total time, total count,
264 // screen-off time, screen-off count
265 private static final String RESOURCE_POWER_MANAGER_DATA = "rpm";
Evan Millare84de8d2009-04-02 22:16:12 -0700266 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800267 private static final String VIBRATOR_DATA = "vib";
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700268 private static final String FOREGROUND_ACTIVITY_DATA = "fg";
269 // fgs line is:
270 // BATTERY_STATS_CHECKIN_VERSION, uid, category, "fgs",
271 // foreground service time, count
272 private static final String FOREGROUND_SERVICE_DATA = "fgs";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700273 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700274 // wl line is:
275 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
Bookatz5b5ec322017-05-26 09:40:38 -0700276 // full totalTime, 'f', count, current duration, max duration, total duration,
277 // partial totalTime, 'p', count, current duration, max duration, total duration,
278 // bg partial totalTime, 'bp', count, current duration, max duration, total duration,
279 // window totalTime, 'w', count, current duration, max duration, total duration
Bookatz506a8182017-05-01 14:18:42 -0700280 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700281 private static final String WAKELOCK_DATA = "wl";
Bookatzc8c44962017-05-11 12:12:54 -0700282 // awl line is:
283 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "awl",
284 // cumulative partial wakelock duration, cumulative background partial wakelock duration
285 private static final String AGGREGATED_WAKELOCK_DATA = "awl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700286 private static final String SYNC_DATA = "sy";
287 private static final String JOB_DATA = "jb";
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700288 private static final String JOB_COMPLETION_DATA = "jbc";
Evan Millarc64edde2009-04-18 12:26:32 -0700289 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700290 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700291 private static final String NETWORK_DATA = "nt";
292 private static final String USER_ACTIVITY_DATA = "ua";
293 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800294 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700295 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700296 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700297 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800298 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
299 private static final String WIFI_CONTROLLER_DATA = "wfcd";
300 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
301 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700302 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700303 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800304 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800305 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
306 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800307 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700308 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700309 private static final String SCREEN_BRIGHTNESS_DATA = "br";
310 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700311 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700312 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
313 private static final String DATA_CONNECTION_TIME_DATA = "dct";
314 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800315 private static final String WIFI_STATE_TIME_DATA = "wst";
316 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700317 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
318 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
319 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
320 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800321 private static final String POWER_USE_SUMMARY_DATA = "pws";
322 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700323 private static final String DISCHARGE_STEP_DATA = "dsd";
324 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700325 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
326 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700327 private static final String FLASHLIGHT_DATA = "fla";
328 private static final String CAMERA_DATA = "cam";
329 private static final String VIDEO_DATA = "vid";
330 private static final String AUDIO_DATA = "aud";
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700331 private static final String WIFI_MULTICAST_TOTAL_DATA = "wmct";
332 private static final String WIFI_MULTICAST_DATA = "wmc";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333
Adam Lesinski010bf372016-04-11 12:18:18 -0700334 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
335
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700336 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 private final Formatter mFormatter = new Formatter(mFormatBuilder);
338
Siddharth Rayb50a6842017-12-14 15:15:28 -0800339 private static final String CELLULAR_CONTROLLER_NAME = "Cellular";
340 private static final String WIFI_CONTROLLER_NAME = "WiFi";
341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700343 * Indicates times spent by the uid at each cpu frequency in all process states.
344 *
345 * Other types might include times spent in foreground, background etc.
346 */
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800347 @VisibleForTesting
348 public static final String UID_TIMES_TYPE_ALL = "A";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700349
350 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700351 * State for keeping track of counting information.
352 */
353 public static abstract class Counter {
354
355 /**
356 * Returns the count associated with this Counter for the
357 * selected type of statistics.
358 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700359 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700360 */
Evan Millarc64edde2009-04-18 12:26:32 -0700361 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700362
363 /**
364 * Temporary for debugging.
365 */
366 public abstract void logState(Printer pw, String prefix);
367 }
368
369 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700370 * State for keeping track of long counting information.
371 */
372 public static abstract class LongCounter {
373
374 /**
375 * Returns the count associated with this Counter for the
376 * selected type of statistics.
377 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700378 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700379 */
380 public abstract long getCountLocked(int which);
381
382 /**
383 * Temporary for debugging.
384 */
385 public abstract void logState(Printer pw, String prefix);
386 }
387
388 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700389 * State for keeping track of array of long counting information.
390 */
391 public static abstract class LongCounterArray {
392 /**
393 * Returns the counts associated with this Counter for the
394 * selected type of statistics.
395 *
396 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
397 */
398 public abstract long[] getCountsLocked(int which);
399
400 /**
401 * Temporary for debugging.
402 */
403 public abstract void logState(Printer pw, String prefix);
404 }
405
406 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800407 * Container class that aggregates counters for transmit, receive, and idle state of a
408 * radio controller.
409 */
410 public static abstract class ControllerActivityCounter {
411 /**
412 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
413 * idle state.
414 */
415 public abstract LongCounter getIdleTimeCounter();
416
417 /**
418 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
Siddharth Rayb50a6842017-12-14 15:15:28 -0800419 * scan state.
420 */
421 public abstract LongCounter getScanTimeCounter();
422
423
424 /**
425 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800426 * receive state.
427 */
428 public abstract LongCounter getRxTimeCounter();
429
430 /**
431 * An array of {@link LongCounter}, representing various transmit levels, where each level
432 * may draw a different amount of power. The levels themselves are controller-specific.
433 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
434 * various transmit level states.
435 */
436 public abstract LongCounter[] getTxTimeCounters();
437
438 /**
439 * @return a non-null {@link LongCounter} representing the power consumed by the controller
440 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
441 * yield a value of 0 if the device doesn't support power calculations.
442 */
443 public abstract LongCounter getPowerCounter();
444 }
445
446 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 * State for keeping track of timing information.
448 */
449 public static abstract class Timer {
450
451 /**
452 * Returns the count associated with this Timer for the
453 * selected type of statistics.
454 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700455 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 */
Evan Millarc64edde2009-04-18 12:26:32 -0700457 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458
459 /**
460 * Returns the total time in microseconds associated with this Timer for the
461 * selected type of statistics.
462 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800463 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700464 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 * @return a time in microseconds
466 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800467 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700470 * Returns the total time in microseconds associated with this Timer since the
471 * 'mark' was last set.
472 *
473 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
474 * @return a time in microseconds
475 */
476 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
477
478 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700479 * Returns the max duration if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700480 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700481 */
482 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
483 return -1;
484 }
485
486 /**
487 * Returns the current time the timer has been active, if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700488 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700489 */
490 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
491 return -1;
492 }
493
494 /**
Kweku Adams103351f2017-10-16 14:39:34 -0700495 * Returns the total time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800496 *
497 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
498 * been on since reset.
499 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
500 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
501 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
502 * the actual total time.
Kweku Adams103351f2017-10-16 14:39:34 -0700503 * Not all Timer subclasses track the max, total, and current durations.
Bookatz867c0d72017-03-07 18:23:42 -0800504 */
505 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
506 return -1;
507 }
508
509 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700510 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
511 * used, for example, for tracking background usage. Secondary timers are never pooled.
512 *
513 * Not all Timer subclasses have a secondary timer; those that don't return null.
514 */
515 public Timer getSubTimer() {
516 return null;
517 }
518
519 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700520 * Returns whether the timer is currently running. Some types of timers
521 * (e.g. BatchTimers) don't know whether the event is currently active,
522 * and report false.
523 */
524 public boolean isRunningLocked() {
525 return false;
526 }
527
528 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 * Temporary for debugging.
530 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700531 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 }
533
534 /**
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800535 * Maps the ActivityManager procstate into corresponding BatteryStats procstate.
536 */
537 public static int mapToInternalProcessState(int procState) {
538 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
539 return ActivityManager.PROCESS_STATE_NONEXISTENT;
540 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
541 return Uid.PROCESS_STATE_TOP;
Dianne Hackborn10fc4fd2017-12-19 17:23:13 -0800542 } else if (procState == ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
543 // State when app has put itself in the foreground.
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800544 return Uid.PROCESS_STATE_FOREGROUND_SERVICE;
545 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
546 // Persistent and other foreground states go here.
547 return Uid.PROCESS_STATE_FOREGROUND;
548 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
549 return Uid.PROCESS_STATE_BACKGROUND;
550 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
551 return Uid.PROCESS_STATE_TOP_SLEEPING;
552 } else if (procState <= ActivityManager.PROCESS_STATE_HEAVY_WEIGHT) {
553 return Uid.PROCESS_STATE_HEAVY_WEIGHT;
554 } else {
555 return Uid.PROCESS_STATE_CACHED;
556 }
557 }
558
559 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 * The statistics associated with a particular uid.
561 */
562 public static abstract class Uid {
563
564 /**
565 * Returns a mapping containing wakelock statistics.
566 *
567 * @return a Map from Strings to Uid.Wakelock objects.
568 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700569 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570
571 /**
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700572 * Returns the WiFi Multicast Wakelock statistics.
573 *
574 * @return a Timer Object for the per uid Multicast statistics.
575 */
576 public abstract Timer getMulticastWakelockStats();
577
578 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700579 * Returns a mapping containing sync statistics.
580 *
581 * @return a Map from Strings to Timer objects.
582 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700583 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700584
585 /**
586 * Returns a mapping containing scheduled job statistics.
587 *
588 * @return a Map from Strings to Timer objects.
589 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700590 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700591
592 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700593 * Returns statistics about how jobs have completed.
594 *
595 * @return A Map of String job names to completion type -> count mapping.
596 */
597 public abstract ArrayMap<String, SparseIntArray> getJobCompletionStats();
598
599 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 * The statistics associated with a particular wake lock.
601 */
602 public static abstract class Wakelock {
603 public abstract Timer getWakeTime(int type);
604 }
605
606 /**
Bookatzc8c44962017-05-11 12:12:54 -0700607 * The cumulative time the uid spent holding any partial wakelocks. This will generally
608 * differ from summing over the Wakelocks in getWakelockStats since the latter may have
609 * wakelocks that overlap in time (and therefore over-counts).
610 */
611 public abstract Timer getAggregatedPartialWakelockTimer();
612
613 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 * Returns a mapping containing sensor statistics.
615 *
616 * @return a Map from Integer sensor ids to Uid.Sensor objects.
617 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700618 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619
620 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700621 * Returns a mapping containing active process data.
622 */
623 public abstract SparseArray<? extends Pid> getPidStats();
Bookatzc8c44962017-05-11 12:12:54 -0700624
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700625 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 * Returns a mapping containing process statistics.
627 *
628 * @return a Map from Strings to Uid.Proc objects.
629 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700630 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631
632 /**
633 * Returns a mapping containing package statistics.
634 *
635 * @return a Map from Strings to Uid.Pkg objects.
636 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700637 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700638
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800639 public abstract ControllerActivityCounter getWifiControllerActivity();
640 public abstract ControllerActivityCounter getBluetoothControllerActivity();
641 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800642
643 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 * {@hide}
645 */
646 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700647
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800648 public abstract void noteWifiRunningLocked(long elapsedRealtime);
649 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
650 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
651 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
652 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
653 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
654 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
655 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
656 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
657 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800658 public abstract void noteActivityResumedLocked(long elapsedRealtime);
659 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800660 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
661 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
662 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700663 public abstract int getWifiScanCount(int which);
Kweku Adams103351f2017-10-16 14:39:34 -0700664 /**
665 * Returns the timer keeping track of wifi scans.
666 */
667 public abstract Timer getWifiScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800668 public abstract int getWifiScanBackgroundCount(int which);
669 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
670 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -0700671 /**
672 * Returns the timer keeping track of background wifi scans.
673 */
674 public abstract Timer getWifiScanBackgroundTimer();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800675 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700676 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800677 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700678 public abstract Timer getAudioTurnedOnTimer();
679 public abstract Timer getVideoTurnedOnTimer();
680 public abstract Timer getFlashlightTurnedOnTimer();
681 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700682 public abstract Timer getForegroundActivityTimer();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700683
684 /**
685 * Returns the timer keeping track of Foreground Service time
686 */
687 public abstract Timer getForegroundServiceTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800688 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800689 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatzb1f04f32017-05-19 13:57:32 -0700690 public abstract Timer getBluetoothUnoptimizedScanTimer();
691 public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700692 public abstract Counter getBluetoothScanResultCounter();
Bookatzb1f04f32017-05-19 13:57:32 -0700693 public abstract Counter getBluetoothScanResultBgCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700694
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700695 public abstract long[] getCpuFreqTimes(int which);
696 public abstract long[] getScreenOffCpuFreqTimes(int which);
Mike Ma3d422c32017-10-25 11:08:57 -0700697 /**
698 * Returns cpu active time of an uid.
699 */
700 public abstract long getCpuActiveTime();
701 /**
702 * Returns cpu times of an uid on each cluster
703 */
704 public abstract long[] getCpuClusterTimes();
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700705
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800706 /**
707 * Returns cpu times of an uid at a particular process state.
708 */
709 public abstract long[] getCpuFreqTimes(int which, int procState);
710 /**
711 * Returns cpu times of an uid while the screen if off at a particular process state.
712 */
713 public abstract long[] getScreenOffCpuFreqTimes(int which, int procState);
714
Dianne Hackborna0200e32016-03-30 18:01:41 -0700715 // Note: the following times are disjoint. They can be added together to find the
716 // total time a uid has had any processes running at all.
717
718 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800719 * Time this uid has any processes in the top state.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700720 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800721 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700722 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800723 * Time this uid has any process with a started foreground service, but
Dianne Hackborna0200e32016-03-30 18:01:41 -0700724 * none in the "top" state.
725 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800726 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700727 /**
Dianne Hackborna0200e32016-03-30 18:01:41 -0700728 * Time this uid has any process in an active foreground state, but none in the
zhouwenjie46712bc2018-01-11 15:21:27 -0800729 * "foreground service" or better state. Persistent and other foreground states go here.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700730 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800731 public static final int PROCESS_STATE_FOREGROUND = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700732 /**
733 * Time this uid has any process in an active background state, but none in the
734 * "foreground" or better state.
735 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800736 public static final int PROCESS_STATE_BACKGROUND = 3;
737 /**
738 * Time this uid has any process that is top while the device is sleeping, but not
739 * active for any other reason. We kind-of consider it a kind of cached process
740 * for execution restrictions.
741 */
742 public static final int PROCESS_STATE_TOP_SLEEPING = 4;
743 /**
744 * Time this uid has any process that is in the background but it has an activity
745 * marked as "can't save state". This is essentially a cached process, though the
746 * system will try much harder than normal to avoid killing it.
747 */
748 public static final int PROCESS_STATE_HEAVY_WEIGHT = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700749 /**
750 * Time this uid has any processes that are sitting around cached, not in one of the
751 * other active states.
752 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800753 public static final int PROCESS_STATE_CACHED = 6;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700754 /**
755 * Total number of process states we track.
756 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800757 public static final int NUM_PROCESS_STATE = 7;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700758
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800759 // Used in dump
Dianne Hackborn61659e52014-07-09 16:13:01 -0700760 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800761 "Top", "Fg Service", "Foreground", "Background", "Top Sleeping", "Heavy Weight",
762 "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700763 };
764
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800765 // Used in checkin dump
766 @VisibleForTesting
767 public static final String[] UID_PROCESS_TYPES = {
768 "T", // TOP
769 "FS", // FOREGROUND_SERVICE
770 "F", // FOREGROUND
771 "B", // BACKGROUND
772 "TS", // TOP_SLEEPING
773 "HW", // HEAVY_WEIGHT
774 "C" // CACHED
775 };
776
777 /**
778 * When the process exits one of these states, we need to make sure cpu time in this state
779 * is not attributed to any non-critical process states.
780 */
781 public static final int[] CRITICAL_PROC_STATES = {
782 PROCESS_STATE_TOP, PROCESS_STATE_FOREGROUND_SERVICE, PROCESS_STATE_FOREGROUND
783 };
784
Dianne Hackborn61659e52014-07-09 16:13:01 -0700785 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800786 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700787
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800788 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789
Robert Greenwalta029ea12013-09-25 16:38:12 -0700790 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
791
Dianne Hackborn617f8772009-03-31 15:04:46 -0700792 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700793 * Note that these must match the constants in android.os.PowerManager.
794 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
795 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700796 */
797 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700798 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700799 };
Bookatzc8c44962017-05-11 12:12:54 -0700800
Phil Weaverda80d672016-03-15 16:25:46 -0700801 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700802
Dianne Hackborn617f8772009-03-31 15:04:46 -0700803 public abstract void noteUserActivityLocked(int type);
804 public abstract boolean hasUserActivity();
805 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700806
807 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800808 public abstract long getNetworkActivityBytes(int type, int which);
809 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800810 public abstract long getMobileRadioActiveTime(int which);
811 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700812
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700813 /**
814 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
815 */
816 public abstract long getUserCpuTimeUs(int which);
817
818 /**
819 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
820 */
821 public abstract long getSystemCpuTimeUs(int which);
822
823 /**
Sudheer Shanka71f34b32017-07-21 00:14:24 -0700824 * Returns the approximate cpu time (in microseconds) spent at a certain CPU speed for a
Adam Lesinski6832f392015-09-05 18:05:40 -0700825 * given CPU cluster.
826 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700827 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700828 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700829 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
830 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700831 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700832 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700833
Adam Lesinski5f056f62016-07-14 16:56:08 -0700834 /**
835 * Returns the number of times this UID woke up the Application Processor to
836 * process a mobile radio packet.
837 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
838 */
839 public abstract long getMobileRadioApWakeupCount(int which);
840
841 /**
842 * Returns the number of times this UID woke up the Application Processor to
843 * process a WiFi packet.
844 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
845 */
846 public abstract long getWifiRadioApWakeupCount(int which);
847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800849 /*
850 * FIXME: it's not correct to use this magic value because it
851 * could clash with a sensor handle (which are defined by
852 * the sensor HAL, and therefore out of our control
853 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 // Magic sensor number for the GPS.
855 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800860
Bookatz867c0d72017-03-07 18:23:42 -0800861 /** Returns a Timer for sensor usage when app is in the background. */
862 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 }
864
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700865 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800866 public int mWakeNesting;
867 public long mWakeSumMs;
868 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700869 }
870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 /**
872 * The statistics associated with a particular process.
873 */
874 public static abstract class Proc {
875
Dianne Hackborn287952c2010-09-22 22:34:31 -0700876 public static class ExcessivePower {
877 public static final int TYPE_WAKE = 1;
878 public static final int TYPE_CPU = 2;
879
880 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700881 public long overTime;
882 public long usedTime;
883 }
884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800886 * Returns true if this process is still active in the battery stats.
887 */
888 public abstract boolean isActive();
889
890 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700891 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700893 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 */
895 public abstract long getUserTime(int which);
896
897 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700898 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700900 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 */
902 public abstract long getSystemTime(int which);
903
904 /**
905 * Returns the number of times the process has been started.
906 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700907 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 */
909 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700910
911 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800912 * Returns the number of times the process has crashed.
913 *
914 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
915 */
916 public abstract int getNumCrashes(int which);
917
918 /**
919 * Returns the number of times the process has ANRed.
920 *
921 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
922 */
923 public abstract int getNumAnrs(int which);
924
925 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700926 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700927 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700928 * @return foreground cpu time in microseconds
929 */
930 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700931
Dianne Hackborn287952c2010-09-22 22:34:31 -0700932 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700933
Dianne Hackborn287952c2010-09-22 22:34:31 -0700934 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 }
936
937 /**
938 * The statistics associated with a particular package.
939 */
940 public static abstract class Pkg {
941
942 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700943 * Returns information about all wakeup alarms that have been triggered for this
944 * package. The mapping keys are tag names for the alarms, the counter contains
945 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700947 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948
949 /**
950 * Returns a mapping containing service statistics.
951 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700952 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953
954 /**
955 * The statistics associated with a particular service.
956 */
Joe Onoratoabded112016-02-08 16:49:39 -0800957 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958
959 /**
960 * Returns the amount of time spent started.
961 *
962 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700963 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 * @return
965 */
966 public abstract long getStartTime(long batteryUptime, int which);
967
968 /**
969 * Returns the total number of times startService() has been called.
970 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700971 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 */
973 public abstract int getStarts(int which);
974
975 /**
976 * Returns the total number times the service has been launched.
977 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700978 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 */
980 public abstract int getLaunches(int which);
981 }
982 }
983 }
984
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800985 public static final class LevelStepTracker {
986 public long mLastStepTime = -1;
987 public int mNumStepDurations;
988 public final long[] mStepDurations;
989
990 public LevelStepTracker(int maxLevelSteps) {
991 mStepDurations = new long[maxLevelSteps];
992 }
993
994 public LevelStepTracker(int numSteps, long[] steps) {
995 mNumStepDurations = numSteps;
996 mStepDurations = new long[numSteps];
997 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
998 }
999
1000 public long getDurationAt(int index) {
1001 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
1002 }
1003
1004 public int getLevelAt(int index) {
1005 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
1006 >> STEP_LEVEL_LEVEL_SHIFT);
1007 }
1008
1009 public int getInitModeAt(int index) {
1010 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
1011 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1012 }
1013
1014 public int getModModeAt(int index) {
1015 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
1016 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1017 }
1018
1019 private void appendHex(long val, int topOffset, StringBuilder out) {
1020 boolean hasData = false;
1021 while (topOffset >= 0) {
1022 int digit = (int)( (val>>topOffset) & 0xf );
1023 topOffset -= 4;
1024 if (!hasData && digit == 0) {
1025 continue;
1026 }
1027 hasData = true;
1028 if (digit >= 0 && digit <= 9) {
1029 out.append((char)('0' + digit));
1030 } else {
1031 out.append((char)('a' + digit - 10));
1032 }
1033 }
1034 }
1035
1036 public void encodeEntryAt(int index, StringBuilder out) {
1037 long item = mStepDurations[index];
1038 long duration = item & STEP_LEVEL_TIME_MASK;
1039 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
1040 >> STEP_LEVEL_LEVEL_SHIFT);
1041 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
1042 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1043 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
1044 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1045 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1046 case Display.STATE_OFF: out.append('f'); break;
1047 case Display.STATE_ON: out.append('o'); break;
1048 case Display.STATE_DOZE: out.append('d'); break;
1049 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
1050 }
1051 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1052 out.append('p');
1053 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001054 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1055 out.append('i');
1056 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001057 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1058 case Display.STATE_OFF: out.append('F'); break;
1059 case Display.STATE_ON: out.append('O'); break;
1060 case Display.STATE_DOZE: out.append('D'); break;
1061 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
1062 }
1063 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1064 out.append('P');
1065 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001066 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1067 out.append('I');
1068 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001069 out.append('-');
1070 appendHex(level, 4, out);
1071 out.append('-');
1072 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
1073 }
1074
1075 public void decodeEntryAt(int index, String value) {
1076 final int N = value.length();
1077 int i = 0;
1078 char c;
1079 long out = 0;
1080 while (i < N && (c=value.charAt(i)) != '-') {
1081 i++;
1082 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001083 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001084 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001085 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001086 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001087 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001088 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001089 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1090 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1091 break;
1092 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1093 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1094 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001095 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1096 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1097 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001098 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1099 break;
1100 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1101 break;
1102 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1103 break;
1104 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1105 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1106 break;
1107 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1108 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001109 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001110 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1111 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1112 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001113 }
1114 }
1115 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001116 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001117 while (i < N && (c=value.charAt(i)) != '-') {
1118 i++;
1119 level <<= 4;
1120 if (c >= '0' && c <= '9') {
1121 level += c - '0';
1122 } else if (c >= 'a' && c <= 'f') {
1123 level += c - 'a' + 10;
1124 } else if (c >= 'A' && c <= 'F') {
1125 level += c - 'A' + 10;
1126 }
1127 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001128 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001129 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
1130 long duration = 0;
1131 while (i < N && (c=value.charAt(i)) != '-') {
1132 i++;
1133 duration <<= 4;
1134 if (c >= '0' && c <= '9') {
1135 duration += c - '0';
1136 } else if (c >= 'a' && c <= 'f') {
1137 duration += c - 'a' + 10;
1138 } else if (c >= 'A' && c <= 'F') {
1139 duration += c - 'A' + 10;
1140 }
1141 }
1142 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
1143 }
1144
1145 public void init() {
1146 mLastStepTime = -1;
1147 mNumStepDurations = 0;
1148 }
1149
1150 public void clearTime() {
1151 mLastStepTime = -1;
1152 }
1153
1154 public long computeTimePerLevel() {
1155 final long[] steps = mStepDurations;
1156 final int numSteps = mNumStepDurations;
1157
1158 // For now we'll do a simple average across all steps.
1159 if (numSteps <= 0) {
1160 return -1;
1161 }
1162 long total = 0;
1163 for (int i=0; i<numSteps; i++) {
1164 total += steps[i] & STEP_LEVEL_TIME_MASK;
1165 }
1166 return total / numSteps;
1167 /*
1168 long[] buckets = new long[numSteps];
1169 int numBuckets = 0;
1170 int numToAverage = 4;
1171 int i = 0;
1172 while (i < numSteps) {
1173 long totalTime = 0;
1174 int num = 0;
1175 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1176 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1177 num++;
1178 }
1179 buckets[numBuckets] = totalTime / num;
1180 numBuckets++;
1181 numToAverage *= 2;
1182 i += num;
1183 }
1184 if (numBuckets < 1) {
1185 return -1;
1186 }
1187 long averageTime = buckets[numBuckets-1];
1188 for (i=numBuckets-2; i>=0; i--) {
1189 averageTime = (averageTime + buckets[i]) / 2;
1190 }
1191 return averageTime;
1192 */
1193 }
1194
1195 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1196 int[] outNumOfInterest) {
1197 final long[] steps = mStepDurations;
1198 final int count = mNumStepDurations;
1199 if (count <= 0) {
1200 return -1;
1201 }
1202 long total = 0;
1203 int numOfInterest = 0;
1204 for (int i=0; i<count; i++) {
1205 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1206 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1207 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1208 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1209 // If the modes of interest didn't change during this step period...
1210 if ((modMode&modesOfInterest) == 0) {
1211 // And the mode values during this period match those we are measuring...
1212 if ((initMode&modesOfInterest) == modeValues) {
1213 // Then this can be used to estimate the total time!
1214 numOfInterest++;
1215 total += steps[i] & STEP_LEVEL_TIME_MASK;
1216 }
1217 }
1218 }
1219 if (numOfInterest <= 0) {
1220 return -1;
1221 }
1222
1223 if (outNumOfInterest != null) {
1224 outNumOfInterest[0] = numOfInterest;
1225 }
1226
1227 // The estimated time is the average time we spend in each level, multipled
1228 // by 100 -- the total number of battery levels
1229 return (total / numOfInterest) * 100;
1230 }
1231
1232 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1233 int stepCount = mNumStepDurations;
1234 final long lastStepTime = mLastStepTime;
1235 if (lastStepTime >= 0 && numStepLevels > 0) {
1236 final long[] steps = mStepDurations;
1237 long duration = elapsedRealtime - lastStepTime;
1238 for (int i=0; i<numStepLevels; i++) {
1239 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1240 long thisDuration = duration / (numStepLevels-i);
1241 duration -= thisDuration;
1242 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1243 thisDuration = STEP_LEVEL_TIME_MASK;
1244 }
1245 steps[0] = thisDuration | modeBits;
1246 }
1247 stepCount += numStepLevels;
1248 if (stepCount > steps.length) {
1249 stepCount = steps.length;
1250 }
1251 }
1252 mNumStepDurations = stepCount;
1253 mLastStepTime = elapsedRealtime;
1254 }
1255
1256 public void readFromParcel(Parcel in) {
1257 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001258 if (N > mStepDurations.length) {
1259 throw new ParcelFormatException("more step durations than available: " + N);
1260 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001261 mNumStepDurations = N;
1262 for (int i=0; i<N; i++) {
1263 mStepDurations[i] = in.readLong();
1264 }
1265 }
1266
1267 public void writeToParcel(Parcel out) {
1268 final int N = mNumStepDurations;
1269 out.writeInt(N);
1270 for (int i=0; i<N; i++) {
1271 out.writeLong(mStepDurations[i]);
1272 }
1273 }
1274 }
1275
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001276 public static final class PackageChange {
1277 public String mPackageName;
1278 public boolean mUpdate;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001279 public long mVersionCode;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001280 }
1281
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001282 public static final class DailyItem {
1283 public long mStartTime;
1284 public long mEndTime;
1285 public LevelStepTracker mDischargeSteps;
1286 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001287 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001288 }
1289
1290 public abstract DailyItem getDailyItemLocked(int daysAgo);
1291
1292 public abstract long getCurrentDailyStartTime();
1293
1294 public abstract long getNextMinDailyDeadline();
1295
1296 public abstract long getNextMaxDailyDeadline();
1297
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001298 public abstract long[] getCpuFreqs();
1299
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001300 public final static class HistoryTag {
1301 public String string;
1302 public int uid;
1303
1304 public int poolIdx;
1305
1306 public void setTo(HistoryTag o) {
1307 string = o.string;
1308 uid = o.uid;
1309 poolIdx = o.poolIdx;
1310 }
1311
1312 public void setTo(String _string, int _uid) {
1313 string = _string;
1314 uid = _uid;
1315 poolIdx = -1;
1316 }
1317
1318 public void writeToParcel(Parcel dest, int flags) {
1319 dest.writeString(string);
1320 dest.writeInt(uid);
1321 }
1322
1323 public void readFromParcel(Parcel src) {
1324 string = src.readString();
1325 uid = src.readInt();
1326 poolIdx = -1;
1327 }
1328
1329 @Override
1330 public boolean equals(Object o) {
1331 if (this == o) return true;
1332 if (o == null || getClass() != o.getClass()) return false;
1333
1334 HistoryTag that = (HistoryTag) o;
1335
1336 if (uid != that.uid) return false;
1337 if (!string.equals(that.string)) return false;
1338
1339 return true;
1340 }
1341
1342 @Override
1343 public int hashCode() {
1344 int result = string.hashCode();
1345 result = 31 * result + uid;
1346 return result;
1347 }
1348 }
1349
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001350 /**
1351 * Optional detailed information that can go into a history step. This is typically
1352 * generated each time the battery level changes.
1353 */
1354 public final static class HistoryStepDetails {
1355 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1356 public int userTime;
1357 public int systemTime;
1358
1359 // Top three apps using CPU in the last step, with times in 1/100 second.
1360 public int appCpuUid1;
1361 public int appCpuUTime1;
1362 public int appCpuSTime1;
1363 public int appCpuUid2;
1364 public int appCpuUTime2;
1365 public int appCpuSTime2;
1366 public int appCpuUid3;
1367 public int appCpuUTime3;
1368 public int appCpuSTime3;
1369
1370 // Information from /proc/stat
1371 public int statUserTime;
1372 public int statSystemTime;
1373 public int statIOWaitTime;
1374 public int statIrqTime;
1375 public int statSoftIrqTime;
1376 public int statIdlTime;
1377
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001378 // Platform-level low power state stats
1379 public String statPlatformIdleState;
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001380 public String statSubsystemPowerState;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001381
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001382 public HistoryStepDetails() {
1383 clear();
1384 }
1385
1386 public void clear() {
1387 userTime = systemTime = 0;
1388 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1389 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1390 = appCpuUTime3 = appCpuSTime3 = 0;
1391 }
1392
1393 public void writeToParcel(Parcel out) {
1394 out.writeInt(userTime);
1395 out.writeInt(systemTime);
1396 out.writeInt(appCpuUid1);
1397 out.writeInt(appCpuUTime1);
1398 out.writeInt(appCpuSTime1);
1399 out.writeInt(appCpuUid2);
1400 out.writeInt(appCpuUTime2);
1401 out.writeInt(appCpuSTime2);
1402 out.writeInt(appCpuUid3);
1403 out.writeInt(appCpuUTime3);
1404 out.writeInt(appCpuSTime3);
1405 out.writeInt(statUserTime);
1406 out.writeInt(statSystemTime);
1407 out.writeInt(statIOWaitTime);
1408 out.writeInt(statIrqTime);
1409 out.writeInt(statSoftIrqTime);
1410 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001411 out.writeString(statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001412 out.writeString(statSubsystemPowerState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001413 }
1414
1415 public void readFromParcel(Parcel in) {
1416 userTime = in.readInt();
1417 systemTime = in.readInt();
1418 appCpuUid1 = in.readInt();
1419 appCpuUTime1 = in.readInt();
1420 appCpuSTime1 = in.readInt();
1421 appCpuUid2 = in.readInt();
1422 appCpuUTime2 = in.readInt();
1423 appCpuSTime2 = in.readInt();
1424 appCpuUid3 = in.readInt();
1425 appCpuUTime3 = in.readInt();
1426 appCpuSTime3 = in.readInt();
1427 statUserTime = in.readInt();
1428 statSystemTime = in.readInt();
1429 statIOWaitTime = in.readInt();
1430 statIrqTime = in.readInt();
1431 statSoftIrqTime = in.readInt();
1432 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001433 statPlatformIdleState = in.readString();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001434 statSubsystemPowerState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001435 }
1436 }
1437
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001438 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001439 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001440
1441 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001442 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001443
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001444 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001445 public static final byte CMD_NULL = -1;
1446 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001447 public static final byte CMD_CURRENT_TIME = 5;
1448 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001449 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001450 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001451
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001452 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001453
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001454 /**
1455 * Return whether the command code is a delta data update.
1456 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001457 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001458 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001459 }
1460
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001461 public byte batteryLevel;
1462 public byte batteryStatus;
1463 public byte batteryHealth;
1464 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001465
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001466 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001467 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001468
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001469 // The charge of the battery in micro-Ampere-hours.
1470 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001471
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001472 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001473 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001474 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001475 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001476 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1477 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001478 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001479 public static final int STATE_PHONE_STATE_SHIFT = 6;
1480 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001481 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001482 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001483 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001484
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001485 // These states always appear directly in the first int token
1486 // of a delta change; they should be ones that change relatively
1487 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001488 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1489 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001490 public static final int STATE_GPS_ON_FLAG = 1<<29;
1491 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001492 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001493 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001494 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001495 // Do not use, this is used for coulomb delta count.
1496 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001497 // These are on the lower bits used for the command; if they change
1498 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001499 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001500 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001501 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001502 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1503 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
Mike Mac2f518a2017-09-19 16:06:03 -07001504 public static final int STATE_SCREEN_DOZE_FLAG = 1 << 18;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001505 // empty slot
1506 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001507
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001508 public static final int MOST_INTERESTING_STATES =
Mike Mac2f518a2017-09-19 16:06:03 -07001509 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG | STATE_SCREEN_DOZE_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001510
1511 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001512
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001513 public int states;
1514
Dianne Hackborn3251b902014-06-20 14:40:53 -07001515 // Constants from WIFI_SUPPL_STATE_*
1516 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1517 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1518 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1519 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1520 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1521 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
Siddharth Ray78ccaf52017-12-23 16:16:21 -08001522 // Values for NUM_GPS_SIGNAL_QUALITY_LEVELS
1523 public static final int STATE2_GPS_SIGNAL_QUALITY_SHIFT = 7;
1524 public static final int STATE2_GPS_SIGNAL_QUALITY_MASK =
1525 0x1 << STATE2_GPS_SIGNAL_QUALITY_SHIFT;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001526
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001527 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001528 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1529 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1530 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001531 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001532 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1533 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1534 public static final int STATE2_CHARGING_FLAG = 1<<24;
1535 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1536 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1537 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001538 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001539
1540 public static final int MOST_INTERESTING_STATES2 =
Mike Mac2f518a2017-09-19 16:06:03 -07001541 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
1542 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001543
1544 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001545
Dianne Hackborn40c87252014-03-19 16:55:40 -07001546 public int states2;
1547
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001548 // The wake lock that was acquired at this point.
1549 public HistoryTag wakelockTag;
1550
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001551 // Kernel wakeup reason at this point.
1552 public HistoryTag wakeReasonTag;
1553
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001554 // Non-null when there is more detailed information at this step.
1555 public HistoryStepDetails stepDetails;
1556
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001557 public static final int EVENT_FLAG_START = 0x8000;
1558 public static final int EVENT_FLAG_FINISH = 0x4000;
1559
1560 // No event in this item.
1561 public static final int EVENT_NONE = 0x0000;
1562 // Event is about a process that is running.
1563 public static final int EVENT_PROC = 0x0001;
1564 // Event is about an application package that is in the foreground.
1565 public static final int EVENT_FOREGROUND = 0x0002;
1566 // Event is about an application package that is at the top of the screen.
1567 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001568 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001569 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001570 // Events for all additional wake locks aquired/release within a wake block.
1571 // These are not generated by default.
1572 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001573 // Event is about an application executing a scheduled job.
1574 public static final int EVENT_JOB = 0x0006;
1575 // Events for users running.
1576 public static final int EVENT_USER_RUNNING = 0x0007;
1577 // Events for foreground user.
1578 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001579 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001580 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001581 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001582 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001583 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001584 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001585 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001586 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001587 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001588 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001589 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001590 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001591 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001592 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001593 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001594 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001595 // Event for a package being on the temporary whitelist.
1596 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001597 // Event for the screen waking up.
1598 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001599 // Event for the UID that woke up the application processor.
1600 // Used for wakeups coming from WiFi, modem, etc.
1601 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001602 // Event for reporting that a specific partial wake lock has been held for a long duration.
1603 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001604
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001605 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001606 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001607 // Mask to extract out only the type part of the event.
1608 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001609
1610 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1611 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1612 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1613 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1614 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1615 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001616 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1617 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001618 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1619 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001620 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1621 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1622 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1623 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1624 public static final int EVENT_USER_FOREGROUND_START =
1625 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1626 public static final int EVENT_USER_FOREGROUND_FINISH =
1627 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001628 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1629 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001630 public static final int EVENT_TEMP_WHITELIST_START =
1631 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1632 public static final int EVENT_TEMP_WHITELIST_FINISH =
1633 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001634 public static final int EVENT_LONG_WAKE_LOCK_START =
1635 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1636 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1637 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001638
1639 // For CMD_EVENT.
1640 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001641 public HistoryTag eventTag;
1642
Dianne Hackborn9a755432014-05-15 17:05:22 -07001643 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001644 public long currentTime;
1645
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001646 // Meta-data when reading.
1647 public int numReadInts;
1648
1649 // Pre-allocated objects.
1650 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001651 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001652 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001653
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001654 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001655 }
Bookatzc8c44962017-05-11 12:12:54 -07001656
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001657 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001658 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001659 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001660 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001661 }
Bookatzc8c44962017-05-11 12:12:54 -07001662
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001663 public int describeContents() {
1664 return 0;
1665 }
1666
1667 public void writeToParcel(Parcel dest, int flags) {
1668 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001669 int bat = (((int)cmd)&0xff)
1670 | ((((int)batteryLevel)<<8)&0xff00)
1671 | ((((int)batteryStatus)<<16)&0xf0000)
1672 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001673 | ((((int)batteryPlugType)<<24)&0xf000000)
1674 | (wakelockTag != null ? 0x10000000 : 0)
1675 | (wakeReasonTag != null ? 0x20000000 : 0)
1676 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001677 dest.writeInt(bat);
1678 bat = (((int)batteryTemperature)&0xffff)
1679 | ((((int)batteryVoltage)<<16)&0xffff0000);
1680 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001681 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001682 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001683 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001684 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001685 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001686 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001687 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001688 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001689 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001690 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001691 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001692 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001693 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001694 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001695 dest.writeLong(currentTime);
1696 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001697 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001698
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001699 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001700 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001701 int bat = src.readInt();
1702 cmd = (byte)(bat&0xff);
1703 batteryLevel = (byte)((bat>>8)&0xff);
1704 batteryStatus = (byte)((bat>>16)&0xf);
1705 batteryHealth = (byte)((bat>>20)&0xf);
1706 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001707 int bat2 = src.readInt();
1708 batteryTemperature = (short)(bat2&0xffff);
1709 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001710 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001711 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001712 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001713 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001714 wakelockTag = localWakelockTag;
1715 wakelockTag.readFromParcel(src);
1716 } else {
1717 wakelockTag = null;
1718 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001719 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001720 wakeReasonTag = localWakeReasonTag;
1721 wakeReasonTag.readFromParcel(src);
1722 } else {
1723 wakeReasonTag = null;
1724 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001725 if ((bat&0x40000000) != 0) {
1726 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001727 eventTag = localEventTag;
1728 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001729 } else {
1730 eventCode = EVENT_NONE;
1731 eventTag = null;
1732 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001733 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001734 currentTime = src.readLong();
1735 } else {
1736 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001737 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001738 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001739 }
1740
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001741 public void clear() {
1742 time = 0;
1743 cmd = CMD_NULL;
1744 batteryLevel = 0;
1745 batteryStatus = 0;
1746 batteryHealth = 0;
1747 batteryPlugType = 0;
1748 batteryTemperature = 0;
1749 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001750 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001751 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001752 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001753 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001754 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001755 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001756 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001757 }
Bookatzc8c44962017-05-11 12:12:54 -07001758
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001759 public void setTo(HistoryItem o) {
1760 time = o.time;
1761 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001762 setToCommon(o);
1763 }
1764
1765 public void setTo(long time, byte cmd, HistoryItem o) {
1766 this.time = time;
1767 this.cmd = cmd;
1768 setToCommon(o);
1769 }
1770
1771 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001772 batteryLevel = o.batteryLevel;
1773 batteryStatus = o.batteryStatus;
1774 batteryHealth = o.batteryHealth;
1775 batteryPlugType = o.batteryPlugType;
1776 batteryTemperature = o.batteryTemperature;
1777 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001778 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001779 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001780 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001781 if (o.wakelockTag != null) {
1782 wakelockTag = localWakelockTag;
1783 wakelockTag.setTo(o.wakelockTag);
1784 } else {
1785 wakelockTag = null;
1786 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001787 if (o.wakeReasonTag != null) {
1788 wakeReasonTag = localWakeReasonTag;
1789 wakeReasonTag.setTo(o.wakeReasonTag);
1790 } else {
1791 wakeReasonTag = null;
1792 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001793 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001794 if (o.eventTag != null) {
1795 eventTag = localEventTag;
1796 eventTag.setTo(o.eventTag);
1797 } else {
1798 eventTag = null;
1799 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001800 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001801 }
1802
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001803 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001804 return batteryLevel == o.batteryLevel
1805 && batteryStatus == o.batteryStatus
1806 && batteryHealth == o.batteryHealth
1807 && batteryPlugType == o.batteryPlugType
1808 && batteryTemperature == o.batteryTemperature
1809 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001810 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001811 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001812 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001813 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001814 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001815
1816 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001817 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001818 return false;
1819 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001820 if (wakelockTag != o.wakelockTag) {
1821 if (wakelockTag == null || o.wakelockTag == null) {
1822 return false;
1823 }
1824 if (!wakelockTag.equals(o.wakelockTag)) {
1825 return false;
1826 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001827 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001828 if (wakeReasonTag != o.wakeReasonTag) {
1829 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1830 return false;
1831 }
1832 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1833 return false;
1834 }
1835 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001836 if (eventTag != o.eventTag) {
1837 if (eventTag == null || o.eventTag == null) {
1838 return false;
1839 }
1840 if (!eventTag.equals(o.eventTag)) {
1841 return false;
1842 }
1843 }
1844 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001845 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001846 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001847
1848 public final static class HistoryEventTracker {
1849 private final HashMap<String, SparseIntArray>[] mActiveEvents
1850 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1851
1852 public boolean updateState(int code, String name, int uid, int poolIdx) {
1853 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1854 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1855 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1856 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001857 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001858 mActiveEvents[idx] = active;
1859 }
1860 SparseIntArray uids = active.get(name);
1861 if (uids == null) {
1862 uids = new SparseIntArray();
1863 active.put(name, uids);
1864 }
1865 if (uids.indexOfKey(uid) >= 0) {
1866 // Already set, nothing to do!
1867 return false;
1868 }
1869 uids.put(uid, poolIdx);
1870 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1871 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1872 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1873 if (active == null) {
1874 // not currently active, nothing to do.
1875 return false;
1876 }
1877 SparseIntArray uids = active.get(name);
1878 if (uids == null) {
1879 // not currently active, nothing to do.
1880 return false;
1881 }
1882 idx = uids.indexOfKey(uid);
1883 if (idx < 0) {
1884 // not currently active, nothing to do.
1885 return false;
1886 }
1887 uids.removeAt(idx);
1888 if (uids.size() <= 0) {
1889 active.remove(name);
1890 }
1891 }
1892 return true;
1893 }
1894
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001895 public void removeEvents(int code) {
1896 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1897 mActiveEvents[idx] = null;
1898 }
1899
Dianne Hackborn37de0982014-05-09 09:32:18 -07001900 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1901 return mActiveEvents[code];
1902 }
1903 }
1904
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001905 public static final class BitDescription {
1906 public final int mask;
1907 public final int shift;
1908 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001909 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001910 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001911 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07001912
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001913 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001914 this.mask = mask;
1915 this.shift = -1;
1916 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001917 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001918 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001919 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001920 }
Bookatzc8c44962017-05-11 12:12:54 -07001921
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001922 public BitDescription(int mask, int shift, String name, String shortName,
1923 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001924 this.mask = mask;
1925 this.shift = shift;
1926 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001927 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001928 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001929 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001930 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001931 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001932
Dianne Hackbornfc064132014-06-02 12:42:12 -07001933 /**
1934 * Don't allow any more batching in to the current history event. This
1935 * is called when printing partial histories, so to ensure that the next
1936 * history event will go in to a new batch after what was printed in the
1937 * last partial history.
1938 */
1939 public abstract void commitCurrentHistoryBatchLocked();
1940
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001941 public abstract int getHistoryTotalSize();
1942
1943 public abstract int getHistoryUsedSize();
1944
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001945 public abstract boolean startIteratingHistoryLocked();
1946
Dianne Hackborn099bc622014-01-22 13:39:16 -08001947 public abstract int getHistoryStringPoolSize();
1948
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001949 public abstract int getHistoryStringPoolBytes();
1950
1951 public abstract String getHistoryTagPoolString(int index);
1952
1953 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001954
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001955 public abstract boolean getNextHistoryLocked(HistoryItem out);
1956
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001957 public abstract void finishIteratingHistoryLocked();
1958
1959 public abstract boolean startIteratingOldHistoryLocked();
1960
1961 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1962
1963 public abstract void finishIteratingOldHistoryLocked();
1964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001966 * Return the base time offset for the battery history.
1967 */
1968 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07001969
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001970 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 * Returns the number of times the device has been started.
1972 */
1973 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07001974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001976 * 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 -08001977 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07001978 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 * {@hide}
1980 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001981 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07001982
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001983 /**
1984 * Returns the number of times the screen was turned on.
1985 *
1986 * {@hide}
1987 */
1988 public abstract int getScreenOnCount(int which);
1989
Mike Mac2f518a2017-09-19 16:06:03 -07001990 /**
1991 * Returns the time in microseconds that the screen has been dozing while the device was
1992 * running on battery.
1993 *
1994 * {@hide}
1995 */
1996 public abstract long getScreenDozeTime(long elapsedRealtimeUs, int which);
1997
1998 /**
1999 * Returns the number of times the screen was turned dozing.
2000 *
2001 * {@hide}
2002 */
2003 public abstract int getScreenDozeCount(int which);
2004
Jeff Browne95c3cd2014-05-02 16:59:26 -07002005 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
2006
Dianne Hackborn617f8772009-03-31 15:04:46 -07002007 public static final int SCREEN_BRIGHTNESS_DARK = 0;
2008 public static final int SCREEN_BRIGHTNESS_DIM = 1;
2009 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
2010 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
2011 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07002012
Dianne Hackborn617f8772009-03-31 15:04:46 -07002013 static final String[] SCREEN_BRIGHTNESS_NAMES = {
2014 "dark", "dim", "medium", "light", "bright"
2015 };
Bookatzc8c44962017-05-11 12:12:54 -07002016
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002017 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
2018 "0", "1", "2", "3", "4"
2019 };
2020
Dianne Hackborn617f8772009-03-31 15:04:46 -07002021 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07002022
Dianne Hackborn617f8772009-03-31 15:04:46 -07002023 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002024 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07002025 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07002026 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002027 * {@hide}
2028 */
2029 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002030 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002033 * Returns the {@link Timer} object that tracks the given screen brightness.
2034 *
2035 * {@hide}
2036 */
2037 public abstract Timer getScreenBrightnessTimer(int brightnessBin);
2038
2039 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002040 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002041 * running on battery.
2042 *
2043 * {@hide}
2044 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002045 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002046
2047 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002048 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002049 *
2050 * {@hide}
2051 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002052 public abstract int getPowerSaveModeEnabledCount(int which);
2053
2054 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002055 * Constant for device idle mode: not active.
2056 */
2057 public static final int DEVICE_IDLE_MODE_OFF = 0;
2058
2059 /**
2060 * Constant for device idle mode: active in lightweight mode.
2061 */
2062 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
2063
2064 /**
2065 * Constant for device idle mode: active in full mode.
2066 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002067 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002068
2069 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002070 * Returns the time in microseconds that device has been in idle mode while
2071 * running on battery.
2072 *
2073 * {@hide}
2074 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002075 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002076
2077 /**
2078 * Returns the number of times that the devie has gone in to idle mode.
2079 *
2080 * {@hide}
2081 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002082 public abstract int getDeviceIdleModeCount(int mode, int which);
2083
2084 /**
2085 * Return the longest duration we spent in a particular device idle mode (fully in the
2086 * mode, not in idle maintenance etc).
2087 */
2088 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002089
2090 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002091 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002092 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002093 * counts all of the time that we consider the device to be idle, whether or not
2094 * it is currently in the actual device idle mode.
2095 *
2096 * {@hide}
2097 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002098 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002099
2100 /**
Bookatz8c6571b2017-10-24 15:04:41 -07002101 * Returns the number of times that the device has started idling.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002102 *
2103 * {@hide}
2104 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002105 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002106
2107 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002108 * Returns the number of times that connectivity state changed.
2109 *
2110 * {@hide}
2111 */
2112 public abstract int getNumConnectivityChange(int which);
2113
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002114
2115 /**
2116 * Returns the time in microseconds that the phone has been running with
2117 * the given GPS signal quality level
2118 *
2119 * {@hide}
2120 */
2121 public abstract long getGpsSignalQualityTime(int strengthBin,
2122 long elapsedRealtimeUs, int which);
2123
2124 /**
2125 * Returns the GPS battery drain in mA-ms
2126 *
2127 * {@hide}
2128 */
2129 public abstract long getGpsBatteryDrainMaMs();
2130
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002131 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002132 * 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 -08002133 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002134 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 * {@hide}
2136 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002137 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002138
Dianne Hackborn627bba72009-03-24 22:32:56 -07002139 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002140 * Returns the number of times a phone call was activated.
2141 *
2142 * {@hide}
2143 */
2144 public abstract int getPhoneOnCount(int which);
2145
2146 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002147 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002148 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002149 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002150 * {@hide}
2151 */
2152 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002153 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002154
Dianne Hackborn617f8772009-03-31 15:04:46 -07002155 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07002156 * Returns the time in microseconds that the phone has been trying to
2157 * acquire a signal.
2158 *
2159 * {@hide}
2160 */
2161 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002162 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002163
2164 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002165 * Returns the {@link Timer} object that tracks how much the phone has been trying to
2166 * acquire a signal.
2167 *
2168 * {@hide}
2169 */
2170 public abstract Timer getPhoneSignalScanningTimer();
2171
2172 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002173 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002174 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002175 * {@hide}
2176 */
2177 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
2178
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002179 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002180 * Return the {@link Timer} object used to track the given signal strength's duration and
2181 * counts.
2182 */
2183 protected abstract Timer getPhoneSignalStrengthTimer(int strengthBin);
2184
2185 /**
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002186 * Returns the time in microseconds that the mobile network has been active
2187 * (in a high power state).
2188 *
2189 * {@hide}
2190 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002191 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002192
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002193 /**
2194 * Returns the number of times that the mobile network has transitioned to the
2195 * active state.
2196 *
2197 * {@hide}
2198 */
2199 public abstract int getMobileRadioActiveCount(int which);
2200
2201 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002202 * Returns the time in microseconds that is the difference between the mobile radio
2203 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
2204 * from the radio.
2205 *
2206 * {@hide}
2207 */
2208 public abstract long getMobileRadioActiveAdjustedTime(int which);
2209
2210 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002211 * Returns the time in microseconds that the mobile network has been active
2212 * (in a high power state) but not being able to blame on an app.
2213 *
2214 * {@hide}
2215 */
2216 public abstract long getMobileRadioActiveUnknownTime(int which);
2217
2218 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002219 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002220 *
2221 * {@hide}
2222 */
2223 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002224
Dianne Hackborn627bba72009-03-24 22:32:56 -07002225 public static final int DATA_CONNECTION_NONE = 0;
2226 public static final int DATA_CONNECTION_GPRS = 1;
2227 public static final int DATA_CONNECTION_EDGE = 2;
2228 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002229 public static final int DATA_CONNECTION_CDMA = 4;
2230 public static final int DATA_CONNECTION_EVDO_0 = 5;
2231 public static final int DATA_CONNECTION_EVDO_A = 6;
2232 public static final int DATA_CONNECTION_1xRTT = 7;
2233 public static final int DATA_CONNECTION_HSDPA = 8;
2234 public static final int DATA_CONNECTION_HSUPA = 9;
2235 public static final int DATA_CONNECTION_HSPA = 10;
2236 public static final int DATA_CONNECTION_IDEN = 11;
2237 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002238 public static final int DATA_CONNECTION_LTE = 13;
2239 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08002240 public static final int DATA_CONNECTION_HSPAP = 15;
2241 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002242
Dianne Hackborn627bba72009-03-24 22:32:56 -07002243 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002244 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002245 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08002246 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002247 };
Bookatzc8c44962017-05-11 12:12:54 -07002248
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002249 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002250
Dianne Hackborn627bba72009-03-24 22:32:56 -07002251 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002252 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002253 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002254 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002255 * {@hide}
2256 */
2257 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002258 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002261 * Returns the number of times the phone has entered the given data
2262 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002263 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002264 * {@hide}
2265 */
2266 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002267
Kweku Adams87b19ec2017-10-09 12:40:03 -07002268 /**
2269 * Returns the {@link Timer} object that tracks the phone's data connection type stats.
2270 */
2271 public abstract Timer getPhoneDataConnectionTimer(int dataType);
2272
Dianne Hackborn3251b902014-06-20 14:40:53 -07002273 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2274 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2275 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2276 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2277 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2278 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2279 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2280 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2281 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2282 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2283 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2284 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2285 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2286
2287 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2288
2289 static final String[] WIFI_SUPPL_STATE_NAMES = {
2290 "invalid", "disconn", "disabled", "inactive", "scanning",
2291 "authenticating", "associating", "associated", "4-way-handshake",
2292 "group-handshake", "completed", "dormant", "uninit"
2293 };
2294
2295 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2296 "inv", "dsc", "dis", "inact", "scan",
2297 "auth", "ascing", "asced", "4-way",
2298 "group", "compl", "dorm", "uninit"
2299 };
2300
Mike Mac2f518a2017-09-19 16:06:03 -07002301 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002302 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002303 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2304 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002305 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002306 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2307 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2308 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002309 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002310 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002311 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002312 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002313 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2314 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Mike Mac2f518a2017-09-19 16:06:03 -07002315 new BitDescription(HistoryItem.STATE_SCREEN_DOZE_FLAG, "screen_doze", "Sd"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002316 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2317 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2318 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2319 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2320 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2321 new String[] {"in", "out", "emergency", "off"},
2322 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002323 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2324 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2325 SignalStrength.SIGNAL_STRENGTH_NAMES,
2326 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002327 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2328 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2329 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002330 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002331
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002332 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2333 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002334 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002335 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002336 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002337 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002338 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002339 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2340 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2341 new String[] { "off", "light", "full", "???" },
2342 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002343 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2344 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2345 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002346 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2347 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2348 new String[] { "0", "1", "2", "3", "4" },
2349 new String[] { "0", "1", "2", "3", "4" }),
2350 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2351 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2352 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002353 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002354 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002355 new BitDescription(HistoryItem.STATE2_GPS_SIGNAL_QUALITY_MASK,
2356 HistoryItem.STATE2_GPS_SIGNAL_QUALITY_SHIFT, "gps_signal_quality", "Gss",
2357 new String[] { "poor", "good"}, new String[] { "poor", "good"}),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002358 };
2359
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002360 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002361 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002362 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2363 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002364 };
2365
2366 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002367 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002368 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002369 "Esw", "Ewa", "Elw", "Eec"
2370 };
2371
2372 @FunctionalInterface
2373 public interface IntToString {
2374 String applyAsString(int val);
2375 }
2376
2377 private static final IntToString sUidToString = UserHandle::formatUid;
2378 private static final IntToString sIntToString = Integer::toString;
2379
2380 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2381 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2382 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2383 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2384 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002385 };
2386
Dianne Hackborn617f8772009-03-31 15:04:46 -07002387 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08002388 * Returns total time for WiFi Multicast Wakelock timer.
2389 * Note that this may be different from the sum of per uid timer values.
2390 *
2391 * {@hide}
2392 */
2393 public abstract long getWifiMulticastWakelockTime(long elapsedRealtimeUs, int which);
2394
2395 /**
2396 * Returns total time for WiFi Multicast Wakelock timer
2397 * Note that this may be different from the sum of per uid timer values.
2398 *
2399 * {@hide}
2400 */
2401 public abstract int getWifiMulticastWakelockCount(int which);
2402
2403 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002404 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002405 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002406 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002407 * {@hide}
2408 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002409 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002410
2411 /**
Siddharth Rayb50a6842017-12-14 15:15:28 -08002412 * Returns the time in microseconds that wifi has been active while the device was
2413 * running on battery.
2414 *
2415 * {@hide}
2416 */
2417 public abstract long getWifiActiveTime(long elapsedRealtimeUs, int which);
2418
2419 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002420 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002421 * been in the running state while the device was running on battery.
2422 *
2423 * {@hide}
2424 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002425 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002426
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002427 public static final int WIFI_STATE_OFF = 0;
2428 public static final int WIFI_STATE_OFF_SCANNING = 1;
2429 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2430 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2431 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2432 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2433 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2434 public static final int WIFI_STATE_SOFT_AP = 7;
2435
2436 static final String[] WIFI_STATE_NAMES = {
2437 "off", "scanning", "no_net", "disconn",
2438 "sta", "p2p", "sta_p2p", "soft_ap"
2439 };
2440
2441 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2442
2443 /**
2444 * Returns the time in microseconds that WiFi has been running in the given state.
2445 *
2446 * {@hide}
2447 */
2448 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002449 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002450
2451 /**
2452 * Returns the number of times that WiFi has entered the given state.
2453 *
2454 * {@hide}
2455 */
2456 public abstract int getWifiStateCount(int wifiState, int which);
2457
The Android Open Source Project10592532009-03-18 17:39:46 -07002458 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002459 * Returns the {@link Timer} object that tracks the given WiFi state.
2460 *
2461 * {@hide}
2462 */
2463 public abstract Timer getWifiStateTimer(int wifiState);
2464
2465 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002466 * Returns the time in microseconds that the wifi supplicant has been
2467 * in a given state.
2468 *
2469 * {@hide}
2470 */
2471 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2472
2473 /**
2474 * Returns the number of times that the wifi supplicant has transitioned
2475 * to a given state.
2476 *
2477 * {@hide}
2478 */
2479 public abstract int getWifiSupplStateCount(int state, int which);
2480
Kweku Adams87b19ec2017-10-09 12:40:03 -07002481 /**
2482 * Returns the {@link Timer} object that tracks the given wifi supplicant state.
2483 *
2484 * {@hide}
2485 */
2486 public abstract Timer getWifiSupplStateTimer(int state);
2487
Dianne Hackborn3251b902014-06-20 14:40:53 -07002488 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2489
2490 /**
2491 * Returns the time in microseconds that WIFI has been running with
2492 * the given signal strength.
2493 *
2494 * {@hide}
2495 */
2496 public abstract long getWifiSignalStrengthTime(int strengthBin,
2497 long elapsedRealtimeUs, int which);
2498
2499 /**
2500 * Returns the number of times WIFI has entered the given signal strength.
2501 *
2502 * {@hide}
2503 */
2504 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2505
2506 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002507 * Returns the {@link Timer} object that tracks the given WIFI signal strength.
2508 *
2509 * {@hide}
2510 */
2511 public abstract Timer getWifiSignalStrengthTimer(int strengthBin);
2512
2513 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002514 * Returns the time in microseconds that the flashlight has been on while the device was
2515 * running on battery.
2516 *
2517 * {@hide}
2518 */
2519 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2520
2521 /**
2522 * Returns the number of times that the flashlight has been turned on while the device was
2523 * running on battery.
2524 *
2525 * {@hide}
2526 */
2527 public abstract long getFlashlightOnCount(int which);
2528
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002529 /**
2530 * Returns the time in microseconds that the camera has been on while the device was
2531 * running on battery.
2532 *
2533 * {@hide}
2534 */
2535 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2536
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002537 /**
2538 * Returns the time in microseconds that bluetooth scans were running while the device was
2539 * on battery.
2540 *
2541 * {@hide}
2542 */
2543 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002544
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002545 public static final int NETWORK_MOBILE_RX_DATA = 0;
2546 public static final int NETWORK_MOBILE_TX_DATA = 1;
2547 public static final int NETWORK_WIFI_RX_DATA = 2;
2548 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002549 public static final int NETWORK_BT_RX_DATA = 4;
2550 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002551 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2552 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2553 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2554 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2555 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002556
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002557 public abstract long getNetworkActivityBytes(int type, int which);
2558 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002559
Adam Lesinskie08af192015-03-25 16:42:59 -07002560 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002561 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002562 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002563 * actual power data.
2564 */
2565 public abstract boolean hasWifiActivityReporting();
2566
2567 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002568 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2569 * in various radio controller states, such as transmit, receive, and idle.
2570 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002571 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002572 public abstract ControllerActivityCounter getWifiControllerActivity();
2573
2574 /**
2575 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2576 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2577 * actual power data.
2578 */
2579 public abstract boolean hasBluetoothActivityReporting();
2580
2581 /**
2582 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2583 * in various radio controller states, such as transmit, receive, and idle.
2584 * @return non-null {@link ControllerActivityCounter}
2585 */
2586 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2587
2588 /**
2589 * Returns true if the BatteryStats object has detailed modem power reports.
2590 * When true, calling {@link #getModemControllerActivity()} will yield the
2591 * actual power data.
2592 */
2593 public abstract boolean hasModemActivityReporting();
2594
2595 /**
2596 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2597 * in various radio controller states, such as transmit, receive, and idle.
2598 * @return non-null {@link ControllerActivityCounter}
2599 */
2600 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002601
The Android Open Source Project10592532009-03-18 17:39:46 -07002602 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002603 * Return the wall clock time when battery stats data collection started.
2604 */
2605 public abstract long getStartClockTime();
2606
2607 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002608 * Return platform version tag that we were running in when the battery stats started.
2609 */
2610 public abstract String getStartPlatformVersion();
2611
2612 /**
2613 * Return platform version tag that we were running in when the battery stats ended.
2614 */
2615 public abstract String getEndPlatformVersion();
2616
2617 /**
2618 * Return the internal version code of the parcelled format.
2619 */
2620 public abstract int getParcelVersion();
2621
2622 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 * Return whether we are currently running on battery.
2624 */
2625 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 /**
2628 * Returns a SparseArray containing the statistics for each uid.
2629 */
2630 public abstract SparseArray<? extends Uid> getUidStats();
2631
2632 /**
2633 * Returns the current battery uptime in microseconds.
2634 *
2635 * @param curTime the amount of elapsed realtime in microseconds.
2636 */
2637 public abstract long getBatteryUptime(long curTime);
2638
2639 /**
2640 * Returns the current battery realtime in microseconds.
2641 *
2642 * @param curTime the amount of elapsed realtime in microseconds.
2643 */
2644 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002645
The Android Open Source Project10592532009-03-18 17:39:46 -07002646 /**
Evan Millar633a1742009-04-02 16:36:33 -07002647 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002648 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002649 */
Evan Millar633a1742009-04-02 16:36:33 -07002650 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002651
The Android Open Source Project10592532009-03-18 17:39:46 -07002652 /**
Evan Millar633a1742009-04-02 16:36:33 -07002653 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2654 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002655 */
Evan Millar633a1742009-04-02 16:36:33 -07002656 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657
2658 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002659 * Get the amount the battery has discharged since the stats were
2660 * last reset after charging, as a lower-end approximation.
2661 */
2662 public abstract int getLowDischargeAmountSinceCharge();
2663
2664 /**
2665 * Get the amount the battery has discharged since the stats were
2666 * last reset after charging, as an upper-end approximation.
2667 */
2668 public abstract int getHighDischargeAmountSinceCharge();
2669
2670 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002671 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2672 */
2673 public abstract int getDischargeAmount(int which);
2674
2675 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002676 * Get the amount the battery has discharged while the screen was on,
2677 * since the last time power was unplugged.
2678 */
2679 public abstract int getDischargeAmountScreenOn();
2680
2681 /**
2682 * Get the amount the battery has discharged while the screen was on,
2683 * since the last time the device was charged.
2684 */
2685 public abstract int getDischargeAmountScreenOnSinceCharge();
2686
2687 /**
2688 * Get the amount the battery has discharged while the screen was off,
2689 * since the last time power was unplugged.
2690 */
2691 public abstract int getDischargeAmountScreenOff();
2692
2693 /**
2694 * Get the amount the battery has discharged while the screen was off,
2695 * since the last time the device was charged.
2696 */
2697 public abstract int getDischargeAmountScreenOffSinceCharge();
2698
2699 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002700 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002701 * since the last time power was unplugged.
2702 */
2703 public abstract int getDischargeAmountScreenDoze();
2704
2705 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002706 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002707 * since the last time the device was charged.
2708 */
2709 public abstract int getDischargeAmountScreenDozeSinceCharge();
2710
2711 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 * Returns the total, last, or current battery uptime in microseconds.
2713 *
2714 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002715 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 */
2717 public abstract long computeBatteryUptime(long curTime, int which);
2718
2719 /**
2720 * Returns the total, last, or current battery realtime in microseconds.
2721 *
2722 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002723 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 */
2725 public abstract long computeBatteryRealtime(long curTime, int which);
2726
2727 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002728 * Returns the total, last, or current battery screen off/doze uptime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002729 *
2730 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002731 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002732 */
2733 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2734
2735 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002736 * Returns the total, last, or current battery screen off/doze realtime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002737 *
2738 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002739 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002740 */
2741 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2742
2743 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 * Returns the total, last, or current uptime in microseconds.
2745 *
2746 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002747 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 */
2749 public abstract long computeUptime(long curTime, int which);
2750
2751 /**
2752 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002753 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002755 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 */
2757 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002758
2759 /**
2760 * Compute an approximation for how much run time (in microseconds) is remaining on
2761 * the battery. Returns -1 if no time can be computed: either there is not
2762 * enough current data to make a decision, or the battery is currently
2763 * charging.
2764 *
2765 * @param curTime The current elepsed realtime in microseconds.
2766 */
2767 public abstract long computeBatteryTimeRemaining(long curTime);
2768
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002769 // The part of a step duration that is the actual time.
2770 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2771
2772 // Bits in a step duration that are the new battery level we are at.
2773 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002774 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002775
2776 // Bits in a step duration that are the initial mode we were in at that step.
2777 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002778 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002779
2780 // Bits in a step duration that indicate which modes changed during that step.
2781 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002782 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002783
2784 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2785 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2786
Santos Cordone94f0502017-02-24 12:31:20 -08002787 // The largest value for screen state that is tracked in battery states. Any values above
2788 // this should be mapped back to one of the tracked values before being tracked here.
2789 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2790
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002791 // Step duration mode: power save is on.
2792 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2793
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002794 // Step duration mode: device is currently in idle mode.
2795 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2796
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002797 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2798 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002799 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2800 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002801 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2802 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2803 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2804 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2805 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002806 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2807 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002808 };
2809 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2810 (Display.STATE_OFF-1),
2811 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002812 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002813 (Display.STATE_ON-1),
2814 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2815 (Display.STATE_DOZE-1),
2816 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2817 (Display.STATE_DOZE_SUSPEND-1),
2818 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002819 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002820 };
2821 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2822 "screen off",
2823 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002824 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002825 "screen on",
2826 "screen on power save",
2827 "screen doze",
2828 "screen doze power save",
2829 "screen doze-suspend",
2830 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002831 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002832 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002833
2834 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002835 * Return the amount of battery discharge while the screen was off, measured in
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002836 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2837 * a coulomb counter.
2838 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002839 public abstract long getUahDischargeScreenOff(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002840
2841 /**
2842 * Return the amount of battery discharge while the screen was in doze mode, measured in
2843 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2844 * a coulomb counter.
2845 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002846 public abstract long getUahDischargeScreenDoze(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002847
2848 /**
2849 * Return the amount of battery discharge measured in micro-Ampere-hours. This will be
2850 * non-zero only if the device's battery has a coulomb counter.
2851 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002852 public abstract long getUahDischarge(int which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002853
2854 /**
Mike Ma15313c92017-11-15 17:58:21 -08002855 * @return the amount of battery discharge while the device is in light idle mode, measured in
2856 * micro-Ampere-hours.
2857 */
2858 public abstract long getUahDischargeLightDoze(int which);
2859
2860 /**
2861 * @return the amount of battery discharge while the device is in deep idle mode, measured in
2862 * micro-Ampere-hours.
2863 */
2864 public abstract long getUahDischargeDeepDoze(int which);
2865
2866 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002867 * Returns the estimated real battery capacity, which may be less than the capacity
2868 * declared by the PowerProfile.
2869 * @return The estimated battery capacity in mAh.
2870 */
2871 public abstract int getEstimatedBatteryCapacity();
2872
2873 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002874 * @return The minimum learned battery capacity in uAh.
2875 */
2876 public abstract int getMinLearnedBatteryCapacity();
2877
2878 /**
2879 * @return The maximum learned battery capacity in uAh.
2880 */
2881 public abstract int getMaxLearnedBatteryCapacity() ;
2882
2883 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002884 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002885 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002886 public abstract LevelStepTracker getDischargeLevelStepTracker();
2887
2888 /**
2889 * Return the array of daily discharge step durations.
2890 */
2891 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002892
2893 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002894 * Compute an approximation for how much time (in microseconds) remains until the battery
2895 * is fully charged. Returns -1 if no time can be computed: either there is not
2896 * enough current data to make a decision, or the battery is currently
2897 * discharging.
2898 *
2899 * @param curTime The current elepsed realtime in microseconds.
2900 */
2901 public abstract long computeChargeTimeRemaining(long curTime);
2902
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002903 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002904 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002905 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002906 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002907
2908 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002909 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002910 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002911 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002912
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002913 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2914
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002915 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002916
Evan Millarc64edde2009-04-18 12:26:32 -07002917 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918
Bookatz50df7112017-08-04 14:53:26 -07002919 /**
2920 * Returns Timers tracking the total time of each Resource Power Manager state and voter.
2921 */
2922 public abstract Map<String, ? extends Timer> getRpmStats();
2923 /**
2924 * Returns Timers tracking the screen-off time of each Resource Power Manager state and voter.
2925 */
2926 public abstract Map<String, ? extends Timer> getScreenOffRpmStats();
2927
2928
James Carr2dd7e5e2016-07-20 18:48:39 -07002929 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2930
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002931 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2932
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002933 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934 long days = seconds / (60 * 60 * 24);
2935 if (days != 0) {
2936 out.append(days);
2937 out.append("d ");
2938 }
2939 long used = days * 60 * 60 * 24;
2940
2941 long hours = (seconds - used) / (60 * 60);
2942 if (hours != 0 || used != 0) {
2943 out.append(hours);
2944 out.append("h ");
2945 }
2946 used += hours * 60 * 60;
2947
2948 long mins = (seconds-used) / 60;
2949 if (mins != 0 || used != 0) {
2950 out.append(mins);
2951 out.append("m ");
2952 }
2953 used += mins * 60;
2954
2955 if (seconds != 0 || used != 0) {
2956 out.append(seconds-used);
2957 out.append("s ");
2958 }
2959 }
2960
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002961 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002963 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 sb.append(time - (sec * 1000));
2965 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 }
2967
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002968 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002969 long sec = time / 1000;
2970 formatTimeRaw(sb, sec);
2971 sb.append(time - (sec * 1000));
2972 sb.append("ms");
2973 }
2974
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002975 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002977 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 }
2979 float perc = ((float)num) / ((float)den) * 100;
2980 mFormatBuilder.setLength(0);
2981 mFormatter.format("%.1f%%", perc);
2982 return mFormatBuilder.toString();
2983 }
2984
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002985 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002986 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07002987
Evan Millar22ac0432009-03-31 11:33:18 -07002988 if (bytes < BYTES_PER_KB) {
2989 return bytes + "B";
2990 } else if (bytes < BYTES_PER_MB) {
2991 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2992 return mFormatBuilder.toString();
2993 } else if (bytes < BYTES_PER_GB){
2994 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2995 return mFormatBuilder.toString();
2996 } else {
2997 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2998 return mFormatBuilder.toString();
2999 }
3000 }
3001
Kweku Adams103351f2017-10-16 14:39:34 -07003002 private static long roundUsToMs(long timeUs) {
3003 return (timeUs + 500) / 1000;
3004 }
3005
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003006 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003007 if (timer != null) {
3008 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003009 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003010 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
3011 return totalTimeMillis;
3012 }
3013 return 0;
3014 }
3015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 /**
3017 *
3018 * @param sb a StringBuilder object.
3019 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003020 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003022 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 * @param linePrefix a String to be prepended to each line of output.
3024 * @return the line prefix
3025 */
3026 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003027 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003030 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003031
Evan Millarc64edde2009-04-18 12:26:32 -07003032 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 if (totalTimeMillis != 0) {
3034 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003035 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08003036 if (name != null) {
3037 sb.append(name);
3038 sb.append(' ');
3039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 sb.append('(');
3041 sb.append(count);
3042 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003043 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
3044 if (maxDurationMs >= 0) {
3045 sb.append(" max=");
3046 sb.append(maxDurationMs);
3047 }
Bookatz506a8182017-05-01 14:18:42 -07003048 // Put actual time if it is available and different from totalTimeMillis.
3049 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
3050 if (totalDurMs > totalTimeMillis) {
3051 sb.append(" actual=");
3052 sb.append(totalDurMs);
3053 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07003054 if (timer.isRunningLocked()) {
3055 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3056 if (currentMs >= 0) {
3057 sb.append(" (running for ");
3058 sb.append(currentMs);
3059 sb.append("ms)");
3060 } else {
3061 sb.append(" (running)");
3062 }
3063 }
3064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 return ", ";
3066 }
3067 }
3068 return linePrefix;
3069 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003070
3071 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07003072 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003073 *
3074 * @param pw a PrintWriter object to print to.
3075 * @param sb a StringBuilder object.
3076 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08003077 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003078 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
3079 * @param prefix a String to be prepended to each line of output.
3080 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07003081 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003082 */
3083 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07003084 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003085 if (timer != null) {
3086 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07003087 final long totalTimeMs = (timer.getTotalTimeLocked(
3088 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003089 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003090 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003091 sb.setLength(0);
3092 sb.append(prefix);
3093 sb.append(" ");
3094 sb.append(type);
3095 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003096 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003097 sb.append("realtime (");
3098 sb.append(count);
3099 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003100 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
3101 if (maxDurationMs >= 0) {
3102 sb.append(" max=");
3103 sb.append(maxDurationMs);
3104 }
3105 if (timer.isRunningLocked()) {
3106 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
3107 if (currentMs >= 0) {
3108 sb.append(" (running for ");
3109 sb.append(currentMs);
3110 sb.append("ms)");
3111 } else {
3112 sb.append(" (running)");
3113 }
3114 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003115 pw.println(sb.toString());
3116 return true;
3117 }
3118 }
3119 return false;
3120 }
Bookatzc8c44962017-05-11 12:12:54 -07003121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 /**
3123 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07003124 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 * @param sb a StringBuilder object.
3126 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003127 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003128 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003129 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130 * @param linePrefix a String to be prepended to each line of output.
3131 * @return the line prefix
3132 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003133 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
3134 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 long totalTimeMicros = 0;
3136 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07003137 long max = 0;
3138 long current = 0;
3139 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003140 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003141 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07003142 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003143 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3144 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07003145 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003146 }
3147 sb.append(linePrefix);
3148 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
3149 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07003150 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003152 sb.append(',');
3153 sb.append(current);
3154 sb.append(',');
3155 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07003156 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
3157 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
3158 // totalDuration independent of totalTimeMicros (since they are not pooled).
3159 if (name != null) {
3160 sb.append(',');
3161 sb.append(totalDuration);
3162 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 return ",";
3164 }
Bookatz506a8182017-05-01 14:18:42 -07003165
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003166 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
3167 String type) {
3168 pw.print(BATTERY_STATS_CHECKIN_VERSION);
3169 pw.print(',');
3170 pw.print(uid);
3171 pw.print(',');
3172 pw.print(category);
3173 pw.print(',');
3174 pw.print(type);
3175 }
3176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 /**
3178 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07003179 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 * @param pw the PageWriter to dump log to
3181 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3182 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3183 * @param args type-dependent data arguments
3184 */
Bookatzc8c44962017-05-11 12:12:54 -07003185 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003187 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003188 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003189 pw.print(',');
3190 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003192 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07003194
3195 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003196 * Dump a given timer stat for terse checkin mode.
3197 *
3198 * @param pw the PageWriter to dump log to
3199 * @param uid the UID to log
3200 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3201 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3202 * @param timer a {@link Timer} to dump stats for
3203 * @param rawRealtime the current elapsed realtime of the system in microseconds
3204 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3205 */
3206 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
3207 Timer timer, long rawRealtime, int which) {
3208 if (timer != null) {
3209 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003210 final long totalTime = roundUsToMs(timer.getTotalTimeLocked(rawRealtime, which));
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003211 final int count = timer.getCountLocked(which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003212 if (totalTime != 0 || count != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003213 dumpLine(pw, uid, category, type, totalTime, count);
3214 }
3215 }
3216 }
3217
3218 /**
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003219 * Dump a given timer stat to the proto stream.
3220 *
3221 * @param proto the ProtoOutputStream to log to
3222 * @param fieldId type of data, the field to save to (e.g. AggregatedBatteryStats.WAKELOCK)
3223 * @param timer a {@link Timer} to dump stats for
3224 * @param rawRealtimeUs the current elapsed realtime of the system in microseconds
3225 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3226 */
3227 private static void dumpTimer(ProtoOutputStream proto, long fieldId,
Kweku Adams87b19ec2017-10-09 12:40:03 -07003228 Timer timer, long rawRealtimeUs, int which) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003229 if (timer == null) {
3230 return;
3231 }
3232 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003233 final long timeMs = roundUsToMs(timer.getTotalTimeLocked(rawRealtimeUs, which));
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003234 final int count = timer.getCountLocked(which);
Kweku Adams103351f2017-10-16 14:39:34 -07003235 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs / 1000);
3236 final long curDurationMs = timer.getCurrentDurationMsLocked(rawRealtimeUs / 1000);
3237 final long totalDurationMs = timer.getTotalDurationMsLocked(rawRealtimeUs / 1000);
3238 if (timeMs != 0 || count != 0 || maxDurationMs != -1 || curDurationMs != -1
3239 || totalDurationMs != -1) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003240 final long token = proto.start(fieldId);
Kweku Adams103351f2017-10-16 14:39:34 -07003241 proto.write(TimerProto.DURATION_MS, timeMs);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003242 proto.write(TimerProto.COUNT, count);
Kweku Adams103351f2017-10-16 14:39:34 -07003243 // These values will be -1 for timers that don't implement the functionality.
3244 if (maxDurationMs != -1) {
3245 proto.write(TimerProto.MAX_DURATION_MS, maxDurationMs);
3246 }
3247 if (curDurationMs != -1) {
3248 proto.write(TimerProto.CURRENT_DURATION_MS, curDurationMs);
3249 }
3250 if (totalDurationMs != -1) {
3251 proto.write(TimerProto.TOTAL_DURATION_MS, totalDurationMs);
3252 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003253 proto.end(token);
3254 }
3255 }
3256
3257 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003258 * Checks if the ControllerActivityCounter has any data worth dumping.
3259 */
3260 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
3261 if (counter == null) {
3262 return false;
3263 }
3264
3265 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
3266 || counter.getRxTimeCounter().getCountLocked(which) != 0
3267 || counter.getPowerCounter().getCountLocked(which) != 0) {
3268 return true;
3269 }
3270
3271 for (LongCounter c : counter.getTxTimeCounters()) {
3272 if (c.getCountLocked(which) != 0) {
3273 return true;
3274 }
3275 }
3276 return false;
3277 }
3278
3279 /**
3280 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3281 * The order of the arguments in the final check in line is:
3282 *
3283 * idle, rx, power, tx...
3284 *
3285 * where tx... is one or more transmit level times.
3286 */
3287 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
3288 String type,
3289 ControllerActivityCounter counter,
3290 int which) {
3291 if (!controllerActivityHasData(counter, which)) {
3292 return;
3293 }
3294
3295 dumpLineHeader(pw, uid, category, type);
3296 pw.print(",");
3297 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
3298 pw.print(",");
3299 pw.print(counter.getRxTimeCounter().getCountLocked(which));
3300 pw.print(",");
3301 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3302 for (LongCounter c : counter.getTxTimeCounters()) {
3303 pw.print(",");
3304 pw.print(c.getCountLocked(which));
3305 }
3306 pw.println();
3307 }
3308
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003309 /**
3310 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3311 */
3312 private static void dumpControllerActivityProto(ProtoOutputStream proto, long fieldId,
3313 ControllerActivityCounter counter,
3314 int which) {
3315 if (!controllerActivityHasData(counter, which)) {
3316 return;
3317 }
3318
3319 final long cToken = proto.start(fieldId);
3320
3321 proto.write(ControllerActivityProto.IDLE_DURATION_MS,
3322 counter.getIdleTimeCounter().getCountLocked(which));
3323 proto.write(ControllerActivityProto.RX_DURATION_MS,
3324 counter.getRxTimeCounter().getCountLocked(which));
3325 proto.write(ControllerActivityProto.POWER_MAH,
3326 counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3327
3328 long tToken;
3329 LongCounter[] txCounters = counter.getTxTimeCounters();
3330 for (int i = 0; i < txCounters.length; ++i) {
3331 LongCounter c = txCounters[i];
3332 tToken = proto.start(ControllerActivityProto.TX);
3333 proto.write(ControllerActivityProto.TxLevel.LEVEL, i);
3334 proto.write(ControllerActivityProto.TxLevel.DURATION_MS, c.getCountLocked(which));
3335 proto.end(tToken);
3336 }
3337
3338 proto.end(cToken);
3339 }
3340
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003341 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
3342 String prefix, String controllerName,
3343 ControllerActivityCounter counter,
3344 int which) {
3345 if (controllerActivityHasData(counter, which)) {
3346 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
3347 }
3348 }
3349
3350 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
3351 String controllerName,
3352 ControllerActivityCounter counter, int which) {
3353 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
3354 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
3355 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003356 // Battery real time
3357 final long totalControllerActivityTimeMs
3358 = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003359 long totalTxTimeMs = 0;
3360 for (LongCounter txState : counter.getTxTimeCounters()) {
3361 totalTxTimeMs += txState.getCountLocked(which);
3362 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003363 final long sleepTimeMs
3364 = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + totalTxTimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003365
Siddharth Rayb50a6842017-12-14 15:15:28 -08003366 if (controllerName.equals(WIFI_CONTROLLER_NAME)) {
3367 final long scanTimeMs = counter.getScanTimeCounter().getCountLocked(which);
3368 sb.setLength(0);
3369 sb.append(prefix);
3370 sb.append(" ");
3371 sb.append(controllerName);
3372 sb.append(" Scan time: ");
3373 formatTimeMs(sb, scanTimeMs);
3374 sb.append("(");
3375 sb.append(formatRatioLocked(scanTimeMs, totalControllerActivityTimeMs));
3376 sb.append(")");
3377 pw.println(sb.toString());
3378 }
3379
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003380 sb.setLength(0);
3381 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003382 sb.append(" ");
3383 sb.append(controllerName);
3384 sb.append(" Sleep time: ");
3385 formatTimeMs(sb, sleepTimeMs);
3386 sb.append("(");
3387 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3388 sb.append(")");
3389 pw.println(sb.toString());
3390
3391 sb.setLength(0);
3392 sb.append(prefix);
3393 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003394 sb.append(controllerName);
3395 sb.append(" Idle time: ");
3396 formatTimeMs(sb, idleTimeMs);
3397 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003398 sb.append(formatRatioLocked(idleTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003399 sb.append(")");
3400 pw.println(sb.toString());
3401
3402 sb.setLength(0);
3403 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003404 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003405 sb.append(controllerName);
3406 sb.append(" Rx time: ");
3407 formatTimeMs(sb, rxTimeMs);
3408 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003409 sb.append(formatRatioLocked(rxTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003410 sb.append(")");
3411 pw.println(sb.toString());
3412
3413 sb.setLength(0);
3414 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003415 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003416 sb.append(controllerName);
3417 sb.append(" Tx time: ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003418
Siddharth Ray3c648c42017-10-02 17:30:58 -07003419 String [] powerLevel;
3420 switch(controllerName) {
Siddharth Rayb50a6842017-12-14 15:15:28 -08003421 case CELLULAR_CONTROLLER_NAME:
Siddharth Ray3c648c42017-10-02 17:30:58 -07003422 powerLevel = new String[] {
3423 " less than 0dBm: ",
3424 " 0dBm to 8dBm: ",
3425 " 8dBm to 15dBm: ",
3426 " 15dBm to 20dBm: ",
3427 " above 20dBm: "};
3428 break;
3429 default:
3430 powerLevel = new String[] {"[0]", "[1]", "[2]", "[3]", "[4]"};
3431 break;
3432 }
3433 final int numTxLvls = Math.min(counter.getTxTimeCounters().length, powerLevel.length);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003434 if (numTxLvls > 1) {
Siddharth Ray3c648c42017-10-02 17:30:58 -07003435 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003436 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3437 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3438 sb.setLength(0);
3439 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003440 sb.append(" ");
3441 sb.append(powerLevel[lvl]);
3442 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003443 formatTimeMs(sb, txLvlTimeMs);
3444 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003445 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003446 sb.append(")");
3447 pw.println(sb.toString());
3448 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003449 } else {
3450 final long txLvlTimeMs = counter.getTxTimeCounters()[0].getCountLocked(which);
3451 formatTimeMs(sb, txLvlTimeMs);
3452 sb.append("(");
3453 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
3454 sb.append(")");
3455 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003456 }
3457
Siddharth Ray3c648c42017-10-02 17:30:58 -07003458 if (powerDrainMaMs > 0) {
3459 sb.setLength(0);
3460 sb.append(prefix);
3461 sb.append(" ");
3462 sb.append(controllerName);
3463 sb.append(" Battery drain: ").append(
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003464 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
Siddharth Ray3c648c42017-10-02 17:30:58 -07003465 sb.append("mAh");
3466 pw.println(sb.toString());
3467 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003468 }
3469
3470 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003471 * Temporary for settings.
3472 */
3473 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3474 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3475 }
3476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 /**
3478 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003479 *
Kweku Adams87b19ec2017-10-09 12:40:03 -07003480 * NOTE: all times are expressed in microseconds, unless specified otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003482 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3483 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 final long rawUptime = SystemClock.uptimeMillis() * 1000;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003485 final long rawRealtimeMs = SystemClock.elapsedRealtime();
3486 final long rawRealtime = rawRealtimeMs * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3489 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003490 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3491 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3492 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 final long totalRealtime = computeRealtime(rawRealtime, which);
3494 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003495 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Mike Mac2f518a2017-09-19 16:06:03 -07003496 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003497 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003498 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003499 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3500 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003501 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003502 rawRealtime, which);
3503 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3504 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003505 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003506 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003507 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003508 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003509 final long dischargeCount = getUahDischarge(which);
3510 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
3511 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Ma15313c92017-11-15 17:58:21 -08003512 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
3513 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003514
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003515 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003516
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003517 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003518 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003519
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003520 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003522 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003523 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003524 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003525 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003526 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003527 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003528 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003529 getEstimatedBatteryCapacity(),
Mike Mac2f518a2017-09-19 16:06:03 -07003530 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity(),
3531 screenDozeTime / 1000);
Adam Lesinski67c134f2016-06-10 15:15:08 -07003532
Bookatzc8c44962017-05-11 12:12:54 -07003533
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003534 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003535 long fullWakeLockTimeTotal = 0;
3536 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003537
Evan Millar22ac0432009-03-31 11:33:18 -07003538 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003539 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003540
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003541 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3542 = u.getWakelockStats();
3543 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3544 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003545
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003546 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3547 if (fullWakeTimer != null) {
3548 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3549 which);
3550 }
3551
3552 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3553 if (partialWakeTimer != null) {
3554 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3555 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003556 }
3557 }
3558 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003559
3560 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003561 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3562 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3563 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3564 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3565 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3566 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3567 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3568 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003569 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3570 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003571 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3572 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003573 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3574 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003575
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003576 // Dump Modem controller stats
3577 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3578 getModemControllerActivity(), which);
3579
Adam Lesinskie283d332015-04-16 12:29:25 -07003580 // Dump Wifi controller stats
3581 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3582 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003583 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003584 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003585
3586 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3587 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003588
3589 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003590 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3591 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 // Dump misc stats
3594 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003595 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003596 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003597 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003598 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003599 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003600 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3601 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003602 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003603 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3604 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3605 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3606 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003607 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003608
Dianne Hackborn617f8772009-03-31 15:04:46 -07003609 // Dump screen brightness stats
3610 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3611 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003612 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003613 }
3614 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003615
Dianne Hackborn627bba72009-03-24 22:32:56 -07003616 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003617 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3618 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003619 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003620 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003621 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003622 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003623 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003624 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003625 args[i] = getPhoneSignalStrengthCount(i, which);
3626 }
3627 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003628
Dianne Hackborn627bba72009-03-24 22:32:56 -07003629 // Dump network type stats
3630 args = new Object[NUM_DATA_CONNECTION_TYPES];
3631 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003632 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003633 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003634 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3635 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3636 args[i] = getPhoneDataConnectionCount(i, which);
3637 }
3638 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003639
3640 // Dump wifi state stats
3641 args = new Object[NUM_WIFI_STATES];
3642 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003643 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003644 }
3645 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3646 for (int i=0; i<NUM_WIFI_STATES; i++) {
3647 args[i] = getWifiStateCount(i, which);
3648 }
3649 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3650
Dianne Hackborn3251b902014-06-20 14:40:53 -07003651 // Dump wifi suppl state stats
3652 args = new Object[NUM_WIFI_SUPPL_STATES];
3653 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3654 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3655 }
3656 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3657 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3658 args[i] = getWifiSupplStateCount(i, which);
3659 }
3660 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3661
3662 // Dump wifi signal strength stats
3663 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3664 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3665 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3666 }
3667 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3668 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3669 args[i] = getWifiSignalStrengthCount(i, which);
3670 }
3671 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3672
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003673 // Dump Multicast total stats
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003674 final long multicastWakeLockTimeTotalMicros =
3675 getWifiMulticastWakelockTime(rawRealtime, which);
3676 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003677 dumpLine(pw, 0 /* uid */, category, WIFI_MULTICAST_TOTAL_DATA,
3678 multicastWakeLockTimeTotalMicros / 1000,
3679 multicastWakeLockCountTotal);
3680
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003681 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003682 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003683 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003684 }
Bookatzc8c44962017-05-11 12:12:54 -07003685
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003686 if (which == STATS_SINCE_UNPLUGGED) {
3687 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3688 getDischargeStartLevel()-getDischargeCurrentLevel(),
3689 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003690 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
Mike Mac2f518a2017-09-19 16:06:03 -07003691 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003692 getDischargeAmountScreenDoze(), dischargeScreenDozeCount / 1000,
3693 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003694 } else {
3695 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3696 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003697 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003698 getDischargeAmountScreenOffSinceCharge(),
Mike Mac2f518a2017-09-19 16:06:03 -07003699 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003700 getDischargeAmountScreenDozeSinceCharge(), dischargeScreenDozeCount / 1000,
3701 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003702 }
Bookatzc8c44962017-05-11 12:12:54 -07003703
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003704 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003705 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003706 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003707 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003708 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003709 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003710 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3711 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003712 }
Evan Millarc64edde2009-04-18 12:26:32 -07003713 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003714 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003715 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003716 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3717 // Not doing the regular wake lock formatting to remain compatible
3718 // with the old checkin format.
3719 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3720 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003721 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003722 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003723 }
3724 }
Evan Millarc64edde2009-04-18 12:26:32 -07003725 }
Bookatzc8c44962017-05-11 12:12:54 -07003726
Bookatz50df7112017-08-04 14:53:26 -07003727 final Map<String, ? extends Timer> rpmStats = getRpmStats();
3728 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
3729 if (rpmStats.size() > 0) {
3730 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
3731 sb.setLength(0);
3732 Timer totalTimer = ent.getValue();
3733 long timeMs = (totalTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3734 int count = totalTimer.getCountLocked(which);
3735 Timer screenOffTimer = screenOffRpmStats.get(ent.getKey());
3736 long screenOffTimeMs = screenOffTimer != null
3737 ? (screenOffTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3738 int screenOffCount = screenOffTimer != null
3739 ? screenOffTimer.getCountLocked(which) : 0;
Bookatz82b341172017-09-07 19:06:08 -07003740 if (SCREEN_OFF_RPM_STATS_ENABLED) {
3741 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3742 "\"" + ent.getKey() + "\"", timeMs, count, screenOffTimeMs,
3743 screenOffCount);
3744 } else {
3745 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3746 "\"" + ent.getKey() + "\"", timeMs, count);
3747 }
Bookatz50df7112017-08-04 14:53:26 -07003748 }
3749 }
3750
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003751 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003752 helper.create(this);
3753 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003754 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003755 if (sippers != null && sippers.size() > 0) {
3756 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3757 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003758 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003759 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3760 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
Kweku Adams87b19ec2017-10-09 12:40:03 -07003761 int uid = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003762 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003763 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003764 String label;
3765 switch (bs.drainType) {
3766 case IDLE:
3767 label="idle";
3768 break;
3769 case CELL:
3770 label="cell";
3771 break;
3772 case PHONE:
3773 label="phone";
3774 break;
3775 case WIFI:
3776 label="wifi";
3777 break;
3778 case BLUETOOTH:
3779 label="blue";
3780 break;
3781 case SCREEN:
3782 label="scrn";
3783 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003784 case FLASHLIGHT:
3785 label="flashlight";
3786 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003787 case APP:
3788 uid = bs.uidObj.getUid();
3789 label = "uid";
3790 break;
3791 case USER:
3792 uid = UserHandle.getUid(bs.userId, 0);
3793 label = "user";
3794 break;
3795 case UNACCOUNTED:
3796 label = "unacc";
3797 break;
3798 case OVERCOUNTED:
3799 label = "over";
3800 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003801 case CAMERA:
3802 label = "camera";
3803 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003804 case MEMORY:
3805 label = "memory";
3806 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003807 default:
3808 label = "???";
3809 }
3810 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07003811 BatteryStatsHelper.makemAh(bs.totalPowerMah),
3812 bs.shouldHide ? 1 : 0,
3813 BatteryStatsHelper.makemAh(bs.screenPowerMah),
3814 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003815 }
3816 }
3817
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003818 final long[] cpuFreqs = getCpuFreqs();
3819 if (cpuFreqs != null) {
3820 sb.setLength(0);
3821 for (int i = 0; i < cpuFreqs.length; ++i) {
3822 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3823 }
3824 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3825 }
3826
Kweku Adams87b19ec2017-10-09 12:40:03 -07003827 // Dump stats per UID.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003828 for (int iu = 0; iu < NU; iu++) {
3829 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003830 if (reqUid >= 0 && uid != reqUid) {
3831 continue;
3832 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003833 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003835 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003836 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3837 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3838 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3839 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3840 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3841 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3842 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3843 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003844 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003845 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3846 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003847 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003848 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3849 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003850 // Background data transfers
3851 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3852 which);
3853 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3854 which);
3855 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3856 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3857 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3858 which);
3859 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3860 which);
3861 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3862 which);
3863 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3864 which);
3865
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003866 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3867 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003868 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003869 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3870 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3871 || wifiBytesBgTx > 0
3872 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3873 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003874 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3875 wifiBytesRx, wifiBytesTx,
3876 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003877 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003878 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003879 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3880 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3881 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3882 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003883 }
3884
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003885 // Dump modem controller data, per UID.
3886 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3887 u.getModemControllerActivity(), which);
3888
3889 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003890 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3891 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3892 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003893 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3894 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003895 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3896 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3897 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003898 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003899 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003900 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3901 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003902 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3903 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003904 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003905 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003906 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003907
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003908 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3909 u.getWifiControllerActivity(), which);
3910
Bookatz867c0d72017-03-07 18:23:42 -08003911 final Timer bleTimer = u.getBluetoothScanTimer();
3912 if (bleTimer != null) {
3913 // Convert from microseconds to milliseconds with rounding
3914 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3915 / 1000;
3916 if (totalTime != 0) {
3917 final int count = bleTimer.getCountLocked(which);
3918 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3919 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003920 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3921 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3922 final long actualTimeBg = bleTimerBg != null ?
3923 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003924 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003925 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3926 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003927 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3928 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3929 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3930 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3931 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
3932 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3933 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
3934 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3935 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
3936 final Timer unoptimizedScanTimerBg =
3937 u.getBluetoothUnoptimizedScanBackgroundTimer();
3938 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
3939 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3940 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
3941 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3942
Bookatz867c0d72017-03-07 18:23:42 -08003943 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07003944 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
3945 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
3946 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08003947 }
3948 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003949
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003950 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3951 u.getBluetoothControllerActivity(), which);
3952
Dianne Hackborn617f8772009-03-31 15:04:46 -07003953 if (u.hasUserActivity()) {
3954 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3955 boolean hasData = false;
3956 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3957 int val = u.getUserActivityCount(i, which);
3958 args[i] = val;
3959 if (val != 0) hasData = true;
3960 }
3961 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003962 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003963 }
3964 }
Bookatzc8c44962017-05-11 12:12:54 -07003965
3966 if (u.getAggregatedPartialWakelockTimer() != null) {
3967 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07003968 // Times are since reset (regardless of 'which')
3969 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07003970 final Timer bgTimer = timer.getSubTimer();
3971 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003972 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07003973 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
3974 }
3975
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003976 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3977 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3978 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3979 String linePrefix = "";
3980 sb.setLength(0);
3981 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3982 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003983 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3984 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003985 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003986 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
3987 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003988 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3989 rawRealtime, "w", which, linePrefix);
3990
Kweku Adams103351f2017-10-16 14:39:34 -07003991 // Only log if we had at least one wakelock...
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003992 if (sb.length() > 0) {
3993 String name = wakelocks.keyAt(iw);
3994 if (name.indexOf(',') >= 0) {
3995 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003996 }
Yi Jin02483362017-08-04 11:30:44 -07003997 if (name.indexOf('\n') >= 0) {
3998 name = name.replace('\n', '_');
3999 }
4000 if (name.indexOf('\r') >= 0) {
4001 name = name.replace('\r', '_');
4002 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004003 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004004 }
4005 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004006
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004007 // WiFi Multicast Wakelock Statistics
4008 final Timer mcTimer = u.getMulticastWakelockStats();
4009 if (mcTimer != null) {
4010 final long totalMcWakelockTimeMs =
4011 mcTimer.getTotalTimeLocked(rawRealtime, which) / 1000 ;
4012 final int countMcWakelock = mcTimer.getCountLocked(which);
4013 if(totalMcWakelockTimeMs > 0) {
4014 dumpLine(pw, uid, category, WIFI_MULTICAST_DATA,
4015 totalMcWakelockTimeMs, countMcWakelock);
4016 }
4017 }
4018
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004019 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4020 for (int isy=syncs.size()-1; isy>=0; isy--) {
4021 final Timer timer = syncs.valueAt(isy);
4022 // Convert from microseconds to milliseconds with rounding
4023 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4024 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07004025 final Timer bgTimer = timer.getSubTimer();
4026 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004027 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07004028 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004029 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004030 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07004031 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004032 }
4033 }
4034
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004035 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4036 for (int ij=jobs.size()-1; ij>=0; ij--) {
4037 final Timer timer = jobs.valueAt(ij);
4038 // Convert from microseconds to milliseconds with rounding
4039 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4040 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07004041 final Timer bgTimer = timer.getSubTimer();
4042 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004043 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07004044 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004045 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004046 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07004047 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004048 }
4049 }
4050
Dianne Hackborn94326cb2017-06-28 16:17:20 -07004051 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
4052 for (int ic=completions.size()-1; ic>=0; ic--) {
4053 SparseIntArray types = completions.valueAt(ic);
4054 if (types != null) {
4055 dumpLine(pw, uid, category, JOB_COMPLETION_DATA,
4056 "\"" + completions.keyAt(ic) + "\"",
4057 types.get(JobParameters.REASON_CANCELED, 0),
4058 types.get(JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, 0),
4059 types.get(JobParameters.REASON_PREEMPT, 0),
4060 types.get(JobParameters.REASON_TIMEOUT, 0),
4061 types.get(JobParameters.REASON_DEVICE_IDLE, 0));
4062 }
4063 }
4064
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004065 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
4066 rawRealtime, which);
4067 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
4068 rawRealtime, which);
4069 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
4070 rawRealtime, which);
4071 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
4072 rawRealtime, which);
4073
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004074 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4075 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004076 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004077 final Uid.Sensor se = sensors.valueAt(ise);
4078 final int sensorNumber = sensors.keyAt(ise);
4079 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004080 if (timer != null) {
4081 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004082 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4083 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004084 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004085 final int count = timer.getCountLocked(which);
4086 final Timer bgTimer = se.getSensorBackgroundTime();
4087 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004088 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4089 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4090 final long bgActualTime = bgTimer != null ?
4091 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4092 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
4093 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004095 }
4096 }
4097
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004098 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
4099 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004100
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07004101 dumpTimer(pw, uid, category, FOREGROUND_ACTIVITY_DATA, u.getForegroundActivityTimer(),
4102 rawRealtime, which);
4103
4104 dumpTimer(pw, uid, category, FOREGROUND_SERVICE_DATA, u.getForegroundServiceTimer(),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004105 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004106
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004107 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07004108 long totalStateTime = 0;
4109 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08004110 final long time = u.getProcessStateTime(ips, rawRealtime, which);
4111 totalStateTime += time;
4112 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004113 }
4114 if (totalStateTime > 0) {
4115 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
4116 }
4117
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004118 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4119 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004120 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004121 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004122 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004123 }
4124
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004125 // If the cpuFreqs is null, then don't bother checking for cpu freq times.
4126 if (cpuFreqs != null) {
4127 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
4128 // If total cpuFreqTimes is null, then we don't need to check for
4129 // screenOffCpuFreqTimes.
4130 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
4131 sb.setLength(0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004132 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004133 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004134 }
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004135 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
4136 if (screenOffCpuFreqTimeMs != null) {
4137 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
4138 sb.append("," + screenOffCpuFreqTimeMs[i]);
4139 }
4140 } else {
4141 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
4142 sb.append(",0");
4143 }
4144 }
4145 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
4146 cpuFreqTimeMs.length, sb.toString());
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004147 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -08004148
4149 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
4150 final long[] timesMs = u.getCpuFreqTimes(which, procState);
4151 if (timesMs != null && timesMs.length == cpuFreqs.length) {
4152 sb.setLength(0);
4153 for (int i = 0; i < timesMs.length; ++i) {
4154 sb.append((i == 0 ? "" : ",") + timesMs[i]);
4155 }
4156 final long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(
4157 which, procState);
4158 if (screenOffTimesMs != null) {
4159 for (int i = 0; i < screenOffTimesMs.length; ++i) {
4160 sb.append("," + screenOffTimesMs[i]);
4161 }
4162 } else {
4163 for (int i = 0; i < timesMs.length; ++i) {
4164 sb.append(",0");
4165 }
4166 }
4167 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA,
4168 Uid.UID_PROCESS_TYPES[procState], timesMs.length, sb.toString());
4169 }
4170 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004171 }
4172
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004173 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4174 = u.getProcessStats();
4175 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4176 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004177
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004178 final long userMillis = ps.getUserTime(which);
4179 final long systemMillis = ps.getSystemTime(which);
4180 final long foregroundMillis = ps.getForegroundTime(which);
4181 final int starts = ps.getStarts(which);
4182 final int numCrashes = ps.getNumCrashes(which);
4183 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004184
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004185 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
4186 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004187 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
4188 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004189 }
4190 }
4191
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004192 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4193 = u.getPackageStats();
4194 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4195 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4196 int wakeups = 0;
4197 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4198 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07004199 int count = alarms.valueAt(iwa).getCountLocked(which);
4200 wakeups += count;
4201 String name = alarms.keyAt(iwa).replace(',', '_');
4202 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004203 }
4204 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4205 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4206 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4207 final long startTime = ss.getStartTime(batteryUptime, which);
4208 final int starts = ss.getStarts(which);
4209 final int launches = ss.getLaunches(which);
4210 if (startTime != 0 || starts != 0 || launches != 0) {
4211 dumpLine(pw, uid, category, APK_DATA,
4212 wakeups, // wakeup alarms
4213 packageStats.keyAt(ipkg), // Apk
4214 serviceStats.keyAt(isvc), // service
4215 startTime / 1000, // time spent started, in ms
4216 starts,
4217 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 }
4219 }
4220 }
4221 }
4222 }
4223
Dianne Hackborn81038902012-11-26 17:04:09 -08004224 static final class TimerEntry {
4225 final String mName;
4226 final int mId;
4227 final BatteryStats.Timer mTimer;
4228 final long mTime;
4229 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
4230 mName = name;
4231 mId = id;
4232 mTimer = timer;
4233 mTime = time;
4234 }
4235 }
4236
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004237 private void printmAh(PrintWriter printer, double power) {
4238 printer.print(BatteryStatsHelper.makemAh(power));
4239 }
4240
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004241 private void printmAh(StringBuilder sb, double power) {
4242 sb.append(BatteryStatsHelper.makemAh(power));
4243 }
4244
Dianne Hackbornd953c532014-08-16 18:17:38 -07004245 /**
4246 * Temporary for settings.
4247 */
4248 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
4249 int reqUid) {
4250 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
4251 }
4252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004253 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004254 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07004255 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004256 final long rawUptime = SystemClock.uptimeMillis() * 1000;
4257 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07004258 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004259 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260
4261 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
4262 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
4263 final long totalRealtime = computeRealtime(rawRealtime, which);
4264 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004265 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
4266 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
4267 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004268 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
4269 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -07004270 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004271
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004272 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07004273
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004274 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07004275 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004276
Adam Lesinskif9b20a92016-06-17 17:30:01 -07004277 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
4278 if (estimatedBatteryCapacity > 0) {
4279 sb.setLength(0);
4280 sb.append(prefix);
4281 sb.append(" Estimated battery capacity: ");
4282 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
4283 sb.append(" mAh");
4284 pw.println(sb.toString());
4285 }
4286
Jocelyn Dangc627d102017-04-14 13:15:14 -07004287 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
4288 if (minLearnedBatteryCapacity > 0) {
4289 sb.setLength(0);
4290 sb.append(prefix);
4291 sb.append(" Min learned battery capacity: ");
4292 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
4293 sb.append(" mAh");
4294 pw.println(sb.toString());
4295 }
4296 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
4297 if (maxLearnedBatteryCapacity > 0) {
4298 sb.setLength(0);
4299 sb.append(prefix);
4300 sb.append(" Max learned battery capacity: ");
4301 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
4302 sb.append(" mAh");
4303 pw.println(sb.toString());
4304 }
4305
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004306 sb.setLength(0);
4307 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004308 sb.append(" Time on battery: ");
4309 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
4310 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
4311 sb.append(") realtime, ");
4312 formatTimeMs(sb, whichBatteryUptime / 1000);
4313 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, whichBatteryRealtime));
4314 sb.append(") uptime");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004315 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004316
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004317 sb.setLength(0);
4318 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004319 sb.append(" Time on battery screen off: ");
4320 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
4321 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, whichBatteryRealtime));
4322 sb.append(") realtime, ");
4323 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
4324 sb.append("(");
4325 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, whichBatteryRealtime));
4326 sb.append(") uptime");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004327 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004328
4329 sb.setLength(0);
4330 sb.append(prefix);
4331 sb.append(" Time on battery screen doze: ");
4332 formatTimeMs(sb, screenDozeTime / 1000); sb.append("(");
4333 sb.append(formatRatioLocked(screenDozeTime, whichBatteryRealtime));
4334 sb.append(")");
4335 pw.println(sb.toString());
4336
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004337 sb.setLength(0);
4338 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004339 sb.append(" Total run time: ");
4340 formatTimeMs(sb, totalRealtime / 1000);
4341 sb.append("realtime, ");
4342 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004343 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07004344 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004345 if (batteryTimeRemaining >= 0) {
4346 sb.setLength(0);
4347 sb.append(prefix);
4348 sb.append(" Battery time remaining: ");
4349 formatTimeMs(sb, batteryTimeRemaining / 1000);
4350 pw.println(sb.toString());
4351 }
4352 if (chargeTimeRemaining >= 0) {
4353 sb.setLength(0);
4354 sb.append(prefix);
4355 sb.append(" Charge time remaining: ");
4356 formatTimeMs(sb, chargeTimeRemaining / 1000);
4357 pw.println(sb.toString());
4358 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004359
Kweku Adams87b19ec2017-10-09 12:40:03 -07004360 final long dischargeCount = getUahDischarge(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004361 if (dischargeCount >= 0) {
4362 sb.setLength(0);
4363 sb.append(prefix);
4364 sb.append(" Discharge: ");
4365 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
4366 sb.append(" mAh");
4367 pw.println(sb.toString());
4368 }
4369
Kweku Adams87b19ec2017-10-09 12:40:03 -07004370 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004371 if (dischargeScreenOffCount >= 0) {
4372 sb.setLength(0);
4373 sb.append(prefix);
4374 sb.append(" Screen off discharge: ");
4375 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
4376 sb.append(" mAh");
4377 pw.println(sb.toString());
4378 }
4379
Kweku Adams87b19ec2017-10-09 12:40:03 -07004380 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Mac2f518a2017-09-19 16:06:03 -07004381 if (dischargeScreenDozeCount >= 0) {
4382 sb.setLength(0);
4383 sb.append(prefix);
4384 sb.append(" Screen doze discharge: ");
4385 sb.append(BatteryStatsHelper.makemAh(dischargeScreenDozeCount / 1000.0));
4386 sb.append(" mAh");
4387 pw.println(sb.toString());
4388 }
4389
4390 final long dischargeScreenOnCount =
4391 dischargeCount - dischargeScreenOffCount - dischargeScreenDozeCount;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004392 if (dischargeScreenOnCount >= 0) {
4393 sb.setLength(0);
4394 sb.append(prefix);
4395 sb.append(" Screen on discharge: ");
4396 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
4397 sb.append(" mAh");
4398 pw.println(sb.toString());
4399 }
4400
Mike Ma15313c92017-11-15 17:58:21 -08004401 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
4402 if (dischargeLightDozeCount >= 0) {
4403 sb.setLength(0);
4404 sb.append(prefix);
4405 sb.append(" Device light doze discharge: ");
4406 sb.append(BatteryStatsHelper.makemAh(dischargeLightDozeCount / 1000.0));
4407 sb.append(" mAh");
4408 pw.println(sb.toString());
4409 }
4410
4411 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
4412 if (dischargeDeepDozeCount >= 0) {
4413 sb.setLength(0);
4414 sb.append(prefix);
4415 sb.append(" Device deep doze discharge: ");
4416 sb.append(BatteryStatsHelper.makemAh(dischargeDeepDozeCount / 1000.0));
4417 sb.append(" mAh");
4418 pw.println(sb.toString());
4419 }
4420
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004421 pw.print(" Start clock time: ");
4422 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
4423
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004424 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004425 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004426 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004427 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
4428 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004429 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004430 rawRealtime, which);
4431 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
4432 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004433 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004434 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004435 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
4436 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
4437 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004438 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004439 sb.append(prefix);
4440 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
4441 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004442 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004443 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
4444 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004445 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004446 pw.println(sb.toString());
4447 sb.setLength(0);
4448 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004449 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004450 boolean didOne = false;
4451 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004452 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004453 if (time == 0) {
4454 continue;
4455 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004456 sb.append("\n ");
4457 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004458 didOne = true;
4459 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
4460 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004461 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004462 sb.append("(");
4463 sb.append(formatRatioLocked(time, screenOnTime));
4464 sb.append(")");
4465 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004466 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004467 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004468 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004469 sb.setLength(0);
4470 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004471 sb.append(" Power save mode enabled: ");
4472 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004473 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004474 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004475 sb.append(")");
4476 pw.println(sb.toString());
4477 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004478 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004479 sb.setLength(0);
4480 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004481 sb.append(" Device light idling: ");
4482 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004483 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004484 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
4485 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004486 sb.append("x");
4487 pw.println(sb.toString());
4488 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004489 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004490 sb.setLength(0);
4491 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004492 sb.append(" Idle mode light time: ");
4493 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004494 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004495 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
4496 sb.append(") ");
4497 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004498 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004499 sb.append(" -- longest ");
4500 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
4501 pw.println(sb.toString());
4502 }
4503 if (deviceIdlingTime != 0) {
4504 sb.setLength(0);
4505 sb.append(prefix);
4506 sb.append(" Device full idling: ");
4507 formatTimeMs(sb, deviceIdlingTime / 1000);
4508 sb.append("(");
4509 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004510 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004511 sb.append("x");
4512 pw.println(sb.toString());
4513 }
4514 if (deviceIdleModeFullTime != 0) {
4515 sb.setLength(0);
4516 sb.append(prefix);
4517 sb.append(" Idle mode full time: ");
4518 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
4519 sb.append("(");
4520 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
4521 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004522 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004523 sb.append("x");
4524 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004525 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004526 pw.println(sb.toString());
4527 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004528 if (phoneOnTime != 0) {
4529 sb.setLength(0);
4530 sb.append(prefix);
4531 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
4532 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004533 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004534 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004535 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004536 if (connChanges != 0) {
4537 pw.print(prefix);
4538 pw.print(" Connectivity changes: "); pw.println(connChanges);
4539 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004540
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004541 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07004542 long fullWakeLockTimeTotalMicros = 0;
4543 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08004544
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004545 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08004546
Evan Millar22ac0432009-03-31 11:33:18 -07004547 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004548 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004549
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004550 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4551 = u.getWakelockStats();
4552 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4553 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07004554
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004555 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
4556 if (fullWakeTimer != null) {
4557 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
4558 rawRealtime, which);
4559 }
4560
4561 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4562 if (partialWakeTimer != null) {
4563 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
4564 rawRealtime, which);
4565 if (totalTimeMicros > 0) {
4566 if (reqUid < 0) {
4567 // Only show the ordered list of all wake
4568 // locks if the caller is not asking for data
4569 // about a specific uid.
4570 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
4571 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08004572 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004573 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07004574 }
4575 }
4576 }
4577 }
Bookatzc8c44962017-05-11 12:12:54 -07004578
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004579 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4580 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4581 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4582 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4583 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4584 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4585 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4586 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004587 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4588 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004589
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004590 if (fullWakeLockTimeTotalMicros != 0) {
4591 sb.setLength(0);
4592 sb.append(prefix);
4593 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4594 (fullWakeLockTimeTotalMicros + 500) / 1000);
4595 pw.println(sb.toString());
4596 }
4597
4598 if (partialWakeLockTimeTotalMicros != 0) {
4599 sb.setLength(0);
4600 sb.append(prefix);
4601 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4602 (partialWakeLockTimeTotalMicros + 500) / 1000);
4603 pw.println(sb.toString());
4604 }
4605
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004606 final long multicastWakeLockTimeTotalMicros =
4607 getWifiMulticastWakelockTime(rawRealtime, which);
4608 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004609 if (multicastWakeLockTimeTotalMicros != 0) {
4610 sb.setLength(0);
4611 sb.append(prefix);
4612 sb.append(" Total WiFi Multicast wakelock Count: ");
4613 sb.append(multicastWakeLockCountTotal);
4614 pw.println(sb.toString());
4615
4616 sb.setLength(0);
4617 sb.append(prefix);
4618 sb.append(" Total WiFi Multicast wakelock time: ");
4619 formatTimeMsNoSpace(sb, (multicastWakeLockTimeTotalMicros + 500) / 1000);
4620 pw.println(sb.toString());
4621 }
4622
Siddharth Ray3c648c42017-10-02 17:30:58 -07004623 pw.println("");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004624 pw.print(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004625 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004626 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004627 sb.append(" CONNECTIVITY POWER SUMMARY START");
4628 pw.println(sb.toString());
4629
4630 pw.print(prefix);
4631 sb.setLength(0);
4632 sb.append(prefix);
4633 sb.append(" Logging duration for connectivity statistics: ");
4634 formatTimeMs(sb, whichBatteryRealtime / 1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004635 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004636
4637 sb.setLength(0);
4638 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004639 sb.append(" Cellular Statistics:");
Amith Yamasanif37447b2009-10-08 18:28:01 -07004640 pw.println(sb.toString());
4641
Siddharth Ray3c648c42017-10-02 17:30:58 -07004642 pw.print(prefix);
4643 sb.setLength(0);
4644 sb.append(prefix);
4645 sb.append(" Cellular kernel active time: ");
4646 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
4647 formatTimeMs(sb, mobileActiveTime / 1000);
4648 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4649 sb.append(")");
4650 pw.println(sb.toString());
4651
4652 pw.print(" Cellular data received: "); pw.println(formatBytesLocked(mobileRxTotalBytes));
4653 pw.print(" Cellular data sent: "); pw.println(formatBytesLocked(mobileTxTotalBytes));
4654 pw.print(" Cellular packets received: "); pw.println(mobileRxTotalPackets);
4655 pw.print(" Cellular packets sent: "); pw.println(mobileTxTotalPackets);
4656
Dianne Hackborn627bba72009-03-24 22:32:56 -07004657 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004658 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004659 sb.append(" Cellular Radio Access Technology:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004660 didOne = false;
4661 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004662 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004663 if (time == 0) {
4664 continue;
4665 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004666 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004667 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004668 didOne = true;
4669 sb.append(DATA_CONNECTION_NAMES[i]);
4670 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004671 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004672 sb.append("(");
4673 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004674 sb.append(") ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004675 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004676 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004677 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004678
4679 sb.setLength(0);
4680 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004681 sb.append(" Cellular Rx signal strength (RSRP):");
4682 final String[] cellularRxSignalStrengthDescription = new String[]{
4683 "very poor (less than -128dBm): ",
4684 "poor (-128dBm to -118dBm): ",
4685 "moderate (-118dBm to -108dBm): ",
4686 "good (-108dBm to -98dBm): ",
4687 "great (greater than -98dBm): "};
4688 didOne = false;
4689 final int numCellularRxBins = Math.min(SignalStrength.NUM_SIGNAL_STRENGTH_BINS,
4690 cellularRxSignalStrengthDescription.length);
4691 for (int i=0; i<numCellularRxBins; i++) {
4692 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
4693 if (time == 0) {
4694 continue;
4695 }
4696 sb.append("\n ");
4697 sb.append(prefix);
4698 didOne = true;
4699 sb.append(cellularRxSignalStrengthDescription[i]);
4700 sb.append(" ");
4701 formatTimeMs(sb, time/1000);
4702 sb.append("(");
4703 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4704 sb.append(") ");
4705 }
4706 if (!didOne) sb.append(" (no activity)");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004707 pw.println(sb.toString());
4708
Siddharth Rayb50a6842017-12-14 15:15:28 -08004709 printControllerActivity(pw, sb, prefix, CELLULAR_CONTROLLER_NAME,
Siddharth Ray3c648c42017-10-02 17:30:58 -07004710 getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004711
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004712 pw.print(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004713 sb.setLength(0);
4714 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004715 sb.append(" Wifi Statistics:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004716 pw.println(sb.toString());
4717
Siddharth Rayb50a6842017-12-14 15:15:28 -08004718 pw.print(prefix);
4719 sb.setLength(0);
4720 sb.append(prefix);
4721 sb.append(" Wifi kernel active time: ");
4722 final long wifiActiveTime = getWifiActiveTime(rawRealtime, which);
4723 formatTimeMs(sb, wifiActiveTime / 1000);
4724 sb.append("("); sb.append(formatRatioLocked(wifiActiveTime, whichBatteryRealtime));
4725 sb.append(")");
4726 pw.println(sb.toString());
4727
Siddharth Ray3c648c42017-10-02 17:30:58 -07004728 pw.print(" Wifi data received: "); pw.println(formatBytesLocked(wifiRxTotalBytes));
4729 pw.print(" Wifi data sent: "); pw.println(formatBytesLocked(wifiTxTotalBytes));
4730 pw.print(" Wifi packets received: "); pw.println(wifiRxTotalPackets);
4731 pw.print(" Wifi packets sent: "); pw.println(wifiTxTotalPackets);
4732
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004733 sb.setLength(0);
4734 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004735 sb.append(" Wifi states:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004736 didOne = false;
4737 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004738 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004739 if (time == 0) {
4740 continue;
4741 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004742 sb.append("\n ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004743 didOne = true;
4744 sb.append(WIFI_STATE_NAMES[i]);
4745 sb.append(" ");
4746 formatTimeMs(sb, time/1000);
4747 sb.append("(");
4748 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4749 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004750 }
4751 if (!didOne) sb.append(" (no activity)");
4752 pw.println(sb.toString());
4753
4754 sb.setLength(0);
4755 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004756 sb.append(" Wifi supplicant states:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004757 didOne = false;
4758 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4759 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4760 if (time == 0) {
4761 continue;
4762 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004763 sb.append("\n ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004764 didOne = true;
4765 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4766 sb.append(" ");
4767 formatTimeMs(sb, time/1000);
4768 sb.append("(");
4769 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4770 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004771 }
4772 if (!didOne) sb.append(" (no activity)");
4773 pw.println(sb.toString());
4774
4775 sb.setLength(0);
4776 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004777 sb.append(" Wifi Rx signal strength (RSSI):");
4778 final String[] wifiRxSignalStrengthDescription = new String[]{
4779 "very poor (less than -88.75dBm): ",
4780 "poor (-88.75 to -77.5dBm): ",
4781 "moderate (-77.5dBm to -66.25dBm): ",
4782 "good (-66.25dBm to -55dBm): ",
4783 "great (greater than -55dBm): "};
Dianne Hackborn3251b902014-06-20 14:40:53 -07004784 didOne = false;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004785 final int numWifiRxBins = Math.min(NUM_WIFI_SIGNAL_STRENGTH_BINS,
4786 wifiRxSignalStrengthDescription.length);
4787 for (int i=0; i<numWifiRxBins; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004788 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4789 if (time == 0) {
4790 continue;
4791 }
4792 sb.append("\n ");
4793 sb.append(prefix);
4794 didOne = true;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004795 sb.append(" ");
4796 sb.append(wifiRxSignalStrengthDescription[i]);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004797 formatTimeMs(sb, time/1000);
4798 sb.append("(");
4799 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4800 sb.append(") ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004801 }
4802 if (!didOne) sb.append(" (no activity)");
4803 pw.println(sb.toString());
4804
Siddharth Rayb50a6842017-12-14 15:15:28 -08004805 printControllerActivity(pw, sb, prefix, WIFI_CONTROLLER_NAME,
4806 getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004807
Adam Lesinski50e47602015-12-04 17:04:54 -08004808 pw.print(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004809 sb.setLength(0);
4810 sb.append(prefix);
Siddharth Ray78ccaf52017-12-23 16:16:21 -08004811 sb.append(" GPS Statistics:");
4812 pw.println(sb.toString());
4813
4814 sb.setLength(0);
4815 sb.append(prefix);
4816 sb.append(" GPS signal quality (Top 4 Average CN0):");
4817 final String[] gpsSignalQualityDescription = new String[]{
4818 "poor (less than 20 dBHz): ",
4819 "good (greater than 20 dBHz): "};
4820 final int numGpsSignalQualityBins = Math.min(GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS,
4821 gpsSignalQualityDescription.length);
4822 for (int i=0; i<numGpsSignalQualityBins; i++) {
4823 final long time = getGpsSignalQualityTime(i, rawRealtime, which);
4824 sb.append("\n ");
4825 sb.append(prefix);
4826 sb.append(" ");
4827 sb.append(gpsSignalQualityDescription[i]);
4828 formatTimeMs(sb, time/1000);
4829 sb.append("(");
4830 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4831 sb.append(") ");
4832 }
4833 pw.println(sb.toString());
4834
4835 final long gpsBatteryDrainMaMs = getGpsBatteryDrainMaMs();
4836 if (gpsBatteryDrainMaMs > 0) {
4837 pw.print(prefix);
4838 sb.setLength(0);
4839 sb.append(prefix);
4840 sb.append(" Battery Drain (mAh): ");
4841 sb.append(Double.toString(((double) gpsBatteryDrainMaMs)/(3600 * 1000)));
4842 pw.println(sb.toString());
4843 }
4844
4845 pw.print(prefix);
4846 sb.setLength(0);
4847 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004848 sb.append(" CONNECTIVITY POWER SUMMARY END");
4849 pw.println(sb.toString());
4850 pw.println("");
4851
4852 pw.print(prefix);
Adam Lesinski50e47602015-12-04 17:04:54 -08004853 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4854 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4855
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004856 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4857 sb.setLength(0);
4858 sb.append(prefix);
4859 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4860 pw.println(sb.toString());
4861
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004862 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4863 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004864
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004865 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004866
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004867 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004868 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004869 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004870 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004871 pw.println(getDischargeStartLevel());
4872 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4873 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004874 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004875 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004876 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004877 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004878 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004879 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004880 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004881 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004882 pw.println(getDischargeAmountScreenOn());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004883 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004884 pw.println(getDischargeAmountScreenOff());
4885 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4886 pw.println(getDischargeAmountScreenDoze());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004887 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004888 } else {
4889 pw.print(prefix); pw.println(" Device battery use since last full charge");
4890 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004891 pw.println(getLowDischargeAmountSinceCharge());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004892 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004893 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004894 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004895 pw.println(getDischargeAmountScreenOnSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004896 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004897 pw.println(getDischargeAmountScreenOffSinceCharge());
4898 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4899 pw.println(getDischargeAmountScreenDozeSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004900 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004901 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004902
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004903 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004904 helper.create(this);
4905 helper.refreshStats(which, UserHandle.USER_ALL);
4906 List<BatterySipper> sippers = helper.getUsageList();
4907 if (sippers != null && sippers.size() > 0) {
4908 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4909 pw.print(prefix); pw.print(" Capacity: ");
4910 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004911 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004912 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4913 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4914 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4915 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004916 pw.println();
4917 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004918 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004919 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004920 switch (bs.drainType) {
4921 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004922 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004923 break;
4924 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004925 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004926 break;
4927 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004928 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004929 break;
4930 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004931 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004932 break;
4933 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004934 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004935 break;
4936 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004937 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004938 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004939 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004940 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004941 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004942 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004943 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004944 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004945 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004946 break;
4947 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004948 pw.print(" User "); pw.print(bs.userId);
4949 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004950 break;
4951 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004952 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004953 break;
4954 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004955 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004956 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004957 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004958 pw.print(" Camera: ");
4959 break;
4960 default:
4961 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004962 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004963 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004964 printmAh(pw, bs.totalPowerMah);
4965
Adam Lesinski57123002015-06-12 16:12:07 -07004966 if (bs.usagePowerMah != bs.totalPowerMah) {
4967 // If the usage (generic power) isn't the whole amount, we list out
4968 // what components are involved in the calculation.
4969
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004970 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004971 if (bs.usagePowerMah != 0) {
4972 pw.print(" usage=");
4973 printmAh(pw, bs.usagePowerMah);
4974 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004975 if (bs.cpuPowerMah != 0) {
4976 pw.print(" cpu=");
4977 printmAh(pw, bs.cpuPowerMah);
4978 }
4979 if (bs.wakeLockPowerMah != 0) {
4980 pw.print(" wake=");
4981 printmAh(pw, bs.wakeLockPowerMah);
4982 }
4983 if (bs.mobileRadioPowerMah != 0) {
4984 pw.print(" radio=");
4985 printmAh(pw, bs.mobileRadioPowerMah);
4986 }
4987 if (bs.wifiPowerMah != 0) {
4988 pw.print(" wifi=");
4989 printmAh(pw, bs.wifiPowerMah);
4990 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004991 if (bs.bluetoothPowerMah != 0) {
4992 pw.print(" bt=");
4993 printmAh(pw, bs.bluetoothPowerMah);
4994 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004995 if (bs.gpsPowerMah != 0) {
4996 pw.print(" gps=");
4997 printmAh(pw, bs.gpsPowerMah);
4998 }
4999 if (bs.sensorPowerMah != 0) {
5000 pw.print(" sensor=");
5001 printmAh(pw, bs.sensorPowerMah);
5002 }
5003 if (bs.cameraPowerMah != 0) {
5004 pw.print(" camera=");
5005 printmAh(pw, bs.cameraPowerMah);
5006 }
5007 if (bs.flashlightPowerMah != 0) {
5008 pw.print(" flash=");
5009 printmAh(pw, bs.flashlightPowerMah);
5010 }
5011 pw.print(" )");
5012 }
Bookatz17d7d9d2017-06-08 14:50:46 -07005013
5014 // If there is additional smearing information, include it.
5015 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
5016 pw.print(" Including smearing: ");
5017 printmAh(pw, bs.totalSmearedPowerMah);
5018 pw.print(" (");
5019 if (bs.screenPowerMah != 0) {
5020 pw.print(" screen=");
5021 printmAh(pw, bs.screenPowerMah);
5022 }
5023 if (bs.proportionalSmearMah != 0) {
5024 pw.print(" proportional=");
5025 printmAh(pw, bs.proportionalSmearMah);
5026 }
5027 pw.print(" )");
5028 }
5029 if (bs.shouldHide) {
5030 pw.print(" Excluded from smearing");
5031 }
5032
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005033 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005034 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08005035 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005036 }
5037
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005038 sippers = helper.getMobilemsppList();
5039 if (sippers != null && sippers.size() > 0) {
5040 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005041 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005042 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005043 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005044 sb.setLength(0);
5045 sb.append(prefix); sb.append(" Uid ");
5046 UserHandle.formatUid(sb, bs.uidObj.getUid());
5047 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
5048 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
5049 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005050 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005051 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005052 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005053 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005054 sb.setLength(0);
5055 sb.append(prefix);
5056 sb.append(" TOTAL TIME: ");
5057 formatTimeMs(sb, totalTime);
5058 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
5059 sb.append(")");
5060 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005061 pw.println();
5062 }
5063
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005064 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
5065 @Override
5066 public int compare(TimerEntry lhs, TimerEntry rhs) {
5067 long lhsTime = lhs.mTime;
5068 long rhsTime = rhs.mTime;
5069 if (lhsTime < rhsTime) {
5070 return 1;
5071 }
5072 if (lhsTime > rhsTime) {
5073 return -1;
5074 }
5075 return 0;
5076 }
5077 };
5078
5079 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005080 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
5081 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005082 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005083 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
5084 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
5085 : kernelWakelocks.entrySet()) {
5086 final BatteryStats.Timer timer = ent.getValue();
5087 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005088 if (totalTimeMillis > 0) {
5089 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
5090 }
5091 }
5092 if (ktimers.size() > 0) {
5093 Collections.sort(ktimers, timerComparator);
5094 pw.print(prefix); pw.println(" All kernel wake locks:");
5095 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005096 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005097 String linePrefix = ": ";
5098 sb.setLength(0);
5099 sb.append(prefix);
5100 sb.append(" Kernel Wake lock ");
5101 sb.append(timer.mName);
5102 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
5103 which, linePrefix);
5104 if (!linePrefix.equals(": ")) {
5105 sb.append(" realtime");
5106 // Only print out wake locks that were held
5107 pw.println(sb.toString());
5108 }
5109 }
5110 pw.println();
5111 }
5112 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005113
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005114 if (timers.size() > 0) {
5115 Collections.sort(timers, timerComparator);
5116 pw.print(prefix); pw.println(" All partial wake locks:");
5117 for (int i=0; i<timers.size(); i++) {
5118 TimerEntry timer = timers.get(i);
5119 sb.setLength(0);
5120 sb.append(" Wake lock ");
5121 UserHandle.formatUid(sb, timer.mId);
5122 sb.append(" ");
5123 sb.append(timer.mName);
5124 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5125 sb.append(" realtime");
5126 pw.println(sb.toString());
5127 }
5128 timers.clear();
5129 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08005130 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005131
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005132 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005133 if (wakeupReasons.size() > 0) {
5134 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005135 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005136 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005137 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005138 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
5139 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005140 }
5141 Collections.sort(reasons, timerComparator);
5142 for (int i=0; i<reasons.size(); i++) {
5143 TimerEntry timer = reasons.get(i);
5144 String linePrefix = ": ";
5145 sb.setLength(0);
5146 sb.append(prefix);
5147 sb.append(" Wakeup reason ");
5148 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005149 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5150 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005151 pw.println(sb.toString());
5152 }
5153 pw.println();
5154 }
Dianne Hackborn81038902012-11-26 17:04:09 -08005155 }
Evan Millar22ac0432009-03-31 11:33:18 -07005156
James Carr2dd7e5e2016-07-20 18:48:39 -07005157 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
Bookatz50df7112017-08-04 14:53:26 -07005158 if (mMemoryStats.size() > 0) {
5159 pw.println(" Memory Stats");
5160 for (int i = 0; i < mMemoryStats.size(); i++) {
5161 sb.setLength(0);
5162 sb.append(" Bandwidth ");
5163 sb.append(mMemoryStats.keyAt(i));
5164 sb.append(" Time ");
5165 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
5166 pw.println(sb.toString());
5167 }
5168 pw.println();
5169 }
5170
5171 final Map<String, ? extends Timer> rpmStats = getRpmStats();
5172 if (rpmStats.size() > 0) {
5173 pw.print(prefix); pw.println(" Resource Power Manager Stats");
5174 if (rpmStats.size() > 0) {
5175 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
5176 final String timerName = ent.getKey();
5177 final Timer timer = ent.getValue();
5178 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5179 }
5180 }
5181 pw.println();
5182 }
Bookatz82b341172017-09-07 19:06:08 -07005183 if (SCREEN_OFF_RPM_STATS_ENABLED) {
5184 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
Bookatz50df7112017-08-04 14:53:26 -07005185 if (screenOffRpmStats.size() > 0) {
Bookatz82b341172017-09-07 19:06:08 -07005186 pw.print(prefix);
5187 pw.println(" Resource Power Manager Stats for when screen was off");
5188 if (screenOffRpmStats.size() > 0) {
5189 for (Map.Entry<String, ? extends Timer> ent : screenOffRpmStats.entrySet()) {
5190 final String timerName = ent.getKey();
5191 final Timer timer = ent.getValue();
5192 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5193 }
Bookatz50df7112017-08-04 14:53:26 -07005194 }
Bookatz82b341172017-09-07 19:06:08 -07005195 pw.println();
Bookatz50df7112017-08-04 14:53:26 -07005196 }
James Carr2dd7e5e2016-07-20 18:48:39 -07005197 }
5198
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005199 final long[] cpuFreqs = getCpuFreqs();
5200 if (cpuFreqs != null) {
5201 sb.setLength(0);
Bookatz50df7112017-08-04 14:53:26 -07005202 sb.append(" CPU freqs:");
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005203 for (int i = 0; i < cpuFreqs.length; ++i) {
5204 sb.append(" " + cpuFreqs[i]);
5205 }
5206 pw.println(sb.toString());
Bookatz50df7112017-08-04 14:53:26 -07005207 pw.println();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005208 }
5209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005210 for (int iu=0; iu<NU; iu++) {
5211 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005212 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005213 continue;
5214 }
Bookatzc8c44962017-05-11 12:12:54 -07005215
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005216 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07005217
5218 pw.print(prefix);
5219 pw.print(" ");
5220 UserHandle.formatUid(pw, uid);
5221 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005223
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005224 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
5225 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
5226 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
5227 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005228 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
5229 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
5230
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005231 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
5232 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005233 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
5234 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005235
5236 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
5237 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
5238
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005239 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
5240 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
5241 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08005242 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
5243 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5244 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
5245 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005246 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005247
Adam Lesinski5f056f62016-07-14 16:56:08 -07005248 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
5249 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
5250
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005251 if (mobileRxBytes > 0 || mobileTxBytes > 0
5252 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005253 pw.print(prefix); pw.print(" Mobile network: ");
5254 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005255 pw.print(formatBytesLocked(mobileTxBytes));
5256 pw.print(" sent (packets "); pw.print(mobileRxPackets);
5257 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005258 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005259 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
5260 sb.setLength(0);
5261 sb.append(prefix); sb.append(" Mobile radio active: ");
5262 formatTimeMs(sb, uidMobileActiveTime / 1000);
5263 sb.append("(");
5264 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
5265 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
5266 long packets = mobileRxPackets + mobileTxPackets;
5267 if (packets == 0) {
5268 packets = 1;
5269 }
5270 sb.append(" @ ");
5271 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
5272 sb.append(" mspp");
5273 pw.println(sb.toString());
5274 }
5275
Adam Lesinski5f056f62016-07-14 16:56:08 -07005276 if (mobileWakeup > 0) {
5277 sb.setLength(0);
5278 sb.append(prefix);
5279 sb.append(" Mobile radio AP wakeups: ");
5280 sb.append(mobileWakeup);
5281 pw.println(sb.toString());
5282 }
5283
Siddharth Rayb50a6842017-12-14 15:15:28 -08005284 printControllerActivityIfInteresting(pw, sb, prefix + " ",
5285 CELLULAR_CONTROLLER_NAME, u.getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005286
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005287 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005288 pw.print(prefix); pw.print(" Wi-Fi network: ");
5289 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005290 pw.print(formatBytesLocked(wifiTxBytes));
5291 pw.print(" sent (packets "); pw.print(wifiRxPackets);
5292 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005293 }
5294
Dianne Hackborn62793e42015-03-09 11:15:41 -07005295 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08005296 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005297 || uidWifiRunningTime != 0) {
5298 sb.setLength(0);
5299 sb.append(prefix); sb.append(" Wifi Running: ");
5300 formatTimeMs(sb, uidWifiRunningTime / 1000);
5301 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
5302 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07005303 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005304 formatTimeMs(sb, fullWifiLockOnTime / 1000);
5305 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
5306 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08005307 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005308 formatTimeMs(sb, wifiScanTime / 1000);
5309 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07005310 whichBatteryRealtime)); sb.append(") ");
5311 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08005312 sb.append("x\n");
5313 // actual and background times are unpooled and since reset (regardless of 'which')
5314 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
5315 formatTimeMs(sb, wifiScanActualTime / 1000);
5316 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
5317 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5318 sb.append(") ");
5319 sb.append(wifiScanCount);
5320 sb.append("x\n");
5321 sb.append(prefix); sb.append(" Background Wifi Scan: ");
5322 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
5323 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
5324 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5325 sb.append(") ");
5326 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07005327 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005328 pw.println(sb.toString());
5329 }
5330
Adam Lesinski5f056f62016-07-14 16:56:08 -07005331 if (wifiWakeup > 0) {
5332 sb.setLength(0);
5333 sb.append(prefix);
5334 sb.append(" WiFi AP wakeups: ");
5335 sb.append(wifiWakeup);
5336 pw.println(sb.toString());
5337 }
5338
Siddharth Rayb50a6842017-12-14 15:15:28 -08005339 printControllerActivityIfInteresting(pw, sb, prefix + " ", WIFI_CONTROLLER_NAME,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005340 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07005341
Adam Lesinski50e47602015-12-04 17:04:54 -08005342 if (btRxBytes > 0 || btTxBytes > 0) {
5343 pw.print(prefix); pw.print(" Bluetooth network: ");
5344 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
5345 pw.print(formatBytesLocked(btTxBytes));
5346 pw.println(" sent");
5347 }
5348
Bookatz867c0d72017-03-07 18:23:42 -08005349 final Timer bleTimer = u.getBluetoothScanTimer();
5350 if (bleTimer != null) {
5351 // Convert from microseconds to milliseconds with rounding
5352 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
5353 / 1000;
5354 if (totalTimeMs != 0) {
5355 final int count = bleTimer.getCountLocked(which);
5356 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
5357 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005358 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5359 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
5360 final long actualTimeMsBg = bleTimerBg != null ?
5361 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005362 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07005363 final int resultCount = u.getBluetoothScanResultCounter() != null ?
5364 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005365 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
5366 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
5367 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
5368 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
5369 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
5370 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5371 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
5372 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
5373 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
5374 final Timer unoptimizedScanTimerBg =
5375 u.getBluetoothUnoptimizedScanBackgroundTimer();
5376 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
5377 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5378 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
5379 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005380
5381 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08005382 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07005383 sb.append(prefix);
5384 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08005385 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07005386 sb.append(" (");
5387 sb.append(count);
5388 sb.append(" times)");
5389 if (bleTimer.isRunningLocked()) {
5390 sb.append(" (currently running)");
5391 }
5392 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005393 }
Bookatzb1f04f32017-05-19 13:57:32 -07005394
5395 sb.append(prefix);
5396 sb.append(" Bluetooth Scan (total actual realtime): ");
5397 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
5398 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005399 sb.append(count);
5400 sb.append(" times)");
5401 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07005402 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08005403 }
Bookatzb1f04f32017-05-19 13:57:32 -07005404 sb.append("\n");
5405 if (actualTimeMsBg > 0 || countBg > 0) {
5406 sb.append(prefix);
5407 sb.append(" Bluetooth Scan (background realtime): ");
5408 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
5409 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005410 sb.append(countBg);
5411 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07005412 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
5413 sb.append(" (currently running in background)");
5414 }
5415 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005416 }
Bookatzb1f04f32017-05-19 13:57:32 -07005417
5418 sb.append(prefix);
5419 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07005420 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07005421 sb.append(" (");
5422 sb.append(resultCountBg);
5423 sb.append(" in background)");
5424
5425 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
5426 sb.append("\n");
5427 sb.append(prefix);
5428 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
5429 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
5430 sb.append(" (max ");
5431 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
5432 sb.append(")");
5433 if (unoptimizedScanTimer != null
5434 && unoptimizedScanTimer.isRunningLocked()) {
5435 sb.append(" (currently running unoptimized)");
5436 }
5437 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
5438 sb.append("\n");
5439 sb.append(prefix);
5440 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
5441 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
5442 sb.append(" (max ");
5443 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
5444 sb.append(")");
5445 if (unoptimizedScanTimerBg.isRunningLocked()) {
5446 sb.append(" (currently running unoptimized in background)");
5447 }
5448 }
5449 }
Bookatz867c0d72017-03-07 18:23:42 -08005450 pw.println(sb.toString());
5451 uidActivity = true;
5452 }
5453 }
5454
5455
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005456
Dianne Hackborn617f8772009-03-31 15:04:46 -07005457 if (u.hasUserActivity()) {
5458 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07005459 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005460 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005461 if (val != 0) {
5462 if (!hasData) {
5463 sb.setLength(0);
5464 sb.append(" User activity: ");
5465 hasData = true;
5466 } else {
5467 sb.append(", ");
5468 }
5469 sb.append(val);
5470 sb.append(" ");
5471 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
5472 }
5473 }
5474 if (hasData) {
5475 pw.println(sb.toString());
5476 }
5477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005478
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005479 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
5480 = u.getWakelockStats();
5481 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005482 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005483 int countWakelock = 0;
5484 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
5485 final Uid.Wakelock wl = wakelocks.valueAt(iw);
5486 String linePrefix = ": ";
5487 sb.setLength(0);
5488 sb.append(prefix);
5489 sb.append(" Wake lock ");
5490 sb.append(wakelocks.keyAt(iw));
5491 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
5492 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005493 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
5494 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005495 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005496 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
5497 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005498 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
5499 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005500 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
5501 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07005502 sb.append(" realtime");
5503 pw.println(sb.toString());
5504 uidActivity = true;
5505 countWakelock++;
5506
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005507 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
5508 rawRealtime, which);
5509 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
5510 rawRealtime, which);
5511 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
5512 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005513 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07005514 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005515 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005516 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07005517 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
5518 // pooled and therefore just a lower bound)
5519 long actualTotalPartialWakelock = 0;
5520 long actualBgPartialWakelock = 0;
5521 if (u.getAggregatedPartialWakelockTimer() != null) {
5522 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
5523 // Convert from microseconds to milliseconds with rounding
5524 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07005525 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07005526 final Timer bgAggTimer = aggTimer.getSubTimer();
5527 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005528 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07005529 }
5530
5531 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
5532 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
5533 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005534 sb.setLength(0);
5535 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005536 sb.append(" TOTAL wake: ");
5537 boolean needComma = false;
5538 if (totalFullWakelock != 0) {
5539 needComma = true;
5540 formatTimeMs(sb, totalFullWakelock);
5541 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005542 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005543 if (totalPartialWakelock != 0) {
5544 if (needComma) {
5545 sb.append(", ");
5546 }
5547 needComma = true;
5548 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07005549 sb.append("blamed partial");
5550 }
5551 if (actualTotalPartialWakelock != 0) {
5552 if (needComma) {
5553 sb.append(", ");
5554 }
5555 needComma = true;
5556 formatTimeMs(sb, actualTotalPartialWakelock);
5557 sb.append("actual partial");
5558 }
5559 if (actualBgPartialWakelock != 0) {
5560 if (needComma) {
5561 sb.append(", ");
5562 }
5563 needComma = true;
5564 formatTimeMs(sb, actualBgPartialWakelock);
5565 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005566 }
5567 if (totalWindowWakelock != 0) {
5568 if (needComma) {
5569 sb.append(", ");
5570 }
5571 needComma = true;
5572 formatTimeMs(sb, totalWindowWakelock);
5573 sb.append("window");
5574 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005575 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07005576 if (needComma) {
5577 sb.append(",");
5578 }
5579 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005580 formatTimeMs(sb, totalDrawWakelock);
5581 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07005582 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005583 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005584 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005585 }
5586 }
5587
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07005588 // Calculate multicast wakelock stats
5589 final Timer mcTimer = u.getMulticastWakelockStats();
5590 if (mcTimer != null) {
5591 final long multicastWakeLockTimeMicros = mcTimer.getTotalTimeLocked(rawRealtime, which);
5592 final int multicastWakeLockCount = mcTimer.getCountLocked(which);
5593
5594 if (multicastWakeLockTimeMicros > 0) {
5595 sb.setLength(0);
5596 sb.append(prefix);
5597 sb.append(" WiFi Multicast Wakelock");
5598 sb.append(" count = ");
5599 sb.append(multicastWakeLockCount);
5600 sb.append(" time = ");
5601 formatTimeMsNoSpace(sb, (multicastWakeLockTimeMicros + 500) / 1000);
5602 pw.println(sb.toString());
5603 }
5604 }
5605
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005606 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
5607 for (int isy=syncs.size()-1; isy>=0; isy--) {
5608 final Timer timer = syncs.valueAt(isy);
5609 // Convert from microseconds to milliseconds with rounding
5610 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5611 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07005612 final Timer bgTimer = timer.getSubTimer();
5613 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005614 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07005615 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005616 sb.setLength(0);
5617 sb.append(prefix);
5618 sb.append(" Sync ");
5619 sb.append(syncs.keyAt(isy));
5620 sb.append(": ");
5621 if (totalTime != 0) {
5622 formatTimeMs(sb, totalTime);
5623 sb.append("realtime (");
5624 sb.append(count);
5625 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07005626 if (bgTime > 0) {
5627 sb.append(", ");
5628 formatTimeMs(sb, bgTime);
5629 sb.append("background (");
5630 sb.append(bgCount);
5631 sb.append(" times)");
5632 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005633 } else {
5634 sb.append("(not used)");
5635 }
5636 pw.println(sb.toString());
5637 uidActivity = true;
5638 }
5639
5640 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
5641 for (int ij=jobs.size()-1; ij>=0; ij--) {
5642 final Timer timer = jobs.valueAt(ij);
5643 // Convert from microseconds to milliseconds with rounding
5644 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5645 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07005646 final Timer bgTimer = timer.getSubTimer();
5647 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005648 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07005649 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005650 sb.setLength(0);
5651 sb.append(prefix);
5652 sb.append(" Job ");
5653 sb.append(jobs.keyAt(ij));
5654 sb.append(": ");
5655 if (totalTime != 0) {
5656 formatTimeMs(sb, totalTime);
5657 sb.append("realtime (");
5658 sb.append(count);
5659 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07005660 if (bgTime > 0) {
5661 sb.append(", ");
5662 formatTimeMs(sb, bgTime);
5663 sb.append("background (");
5664 sb.append(bgCount);
5665 sb.append(" times)");
5666 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005667 } else {
5668 sb.append("(not used)");
5669 }
5670 pw.println(sb.toString());
5671 uidActivity = true;
5672 }
5673
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005674 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
5675 for (int ic=completions.size()-1; ic>=0; ic--) {
5676 SparseIntArray types = completions.valueAt(ic);
5677 if (types != null) {
5678 pw.print(prefix);
5679 pw.print(" Job Completions ");
5680 pw.print(completions.keyAt(ic));
5681 pw.print(":");
5682 for (int it=0; it<types.size(); it++) {
5683 pw.print(" ");
5684 pw.print(JobParameters.getReasonName(types.keyAt(it)));
5685 pw.print("(");
5686 pw.print(types.valueAt(it));
5687 pw.print("x)");
5688 }
5689 pw.println();
5690 }
5691 }
5692
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005693 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
5694 prefix, "Flashlight");
5695 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
5696 prefix, "Camera");
5697 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
5698 prefix, "Video");
5699 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
5700 prefix, "Audio");
5701
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005702 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
5703 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005704 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005705 final Uid.Sensor se = sensors.valueAt(ise);
5706 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005707 sb.setLength(0);
5708 sb.append(prefix);
5709 sb.append(" Sensor ");
5710 int handle = se.getHandle();
5711 if (handle == Uid.Sensor.GPS) {
5712 sb.append("GPS");
5713 } else {
5714 sb.append(handle);
5715 }
5716 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005717
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005718 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005719 if (timer != null) {
5720 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005721 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5722 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005723 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005724 final Timer bgTimer = se.getSensorBackgroundTime();
5725 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005726 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5727 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5728 final long bgActualTime = bgTimer != null ?
5729 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5730
Dianne Hackborn61659e52014-07-09 16:13:01 -07005731 //timer.logState();
5732 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005733 if (actualTime != totalTime) {
5734 formatTimeMs(sb, totalTime);
5735 sb.append("blamed realtime, ");
5736 }
5737
5738 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005739 sb.append("realtime (");
5740 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005741 sb.append(" times)");
5742
5743 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005744 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005745 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5746 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005747 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005748 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005750 } else {
5751 sb.append("(not used)");
5752 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005753 } else {
5754 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005755 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005756
5757 pw.println(sb.toString());
5758 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005759 }
5760
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005761 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5762 "Vibrator");
5763 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5764 prefix, "Foreground activities");
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005765 uidActivity |= printTimer(pw, sb, u.getForegroundServiceTimer(), rawRealtime, which,
5766 prefix, "Foreground services");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005767
Dianne Hackborn61659e52014-07-09 16:13:01 -07005768 long totalStateTime = 0;
5769 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5770 long time = u.getProcessStateTime(ips, rawRealtime, which);
5771 if (time > 0) {
5772 totalStateTime += time;
5773 sb.setLength(0);
5774 sb.append(prefix);
5775 sb.append(" ");
5776 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5777 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005778 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005779 pw.println(sb.toString());
5780 uidActivity = true;
5781 }
5782 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005783 if (totalStateTime > 0) {
5784 sb.setLength(0);
5785 sb.append(prefix);
5786 sb.append(" Total running: ");
5787 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5788 pw.println(sb.toString());
5789 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005790
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005791 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5792 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005793 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005794 sb.setLength(0);
5795 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005796 sb.append(" Total cpu time: u=");
5797 formatTimeMs(sb, userCpuTimeUs / 1000);
5798 sb.append("s=");
5799 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005800 pw.println(sb.toString());
5801 }
5802
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005803 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5804 if (cpuFreqTimes != null) {
5805 sb.setLength(0);
5806 sb.append(" Total cpu time per freq:");
5807 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5808 sb.append(" " + cpuFreqTimes[i]);
5809 }
5810 pw.println(sb.toString());
5811 }
5812 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5813 if (screenOffCpuFreqTimes != null) {
5814 sb.setLength(0);
5815 sb.append(" Total screen-off cpu time per freq:");
5816 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5817 sb.append(" " + screenOffCpuFreqTimes[i]);
5818 }
5819 pw.println(sb.toString());
5820 }
5821
Sudheer Shankab2f83c12017-11-13 19:25:01 -08005822 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
5823 final long[] cpuTimes = u.getCpuFreqTimes(which, procState);
5824 if (cpuTimes != null) {
5825 sb.setLength(0);
5826 sb.append(" Cpu times per freq at state "
5827 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5828 for (int i = 0; i < cpuTimes.length; ++i) {
5829 sb.append(" " + cpuTimes[i]);
5830 }
5831 pw.println(sb.toString());
5832 }
5833
5834 final long[] screenOffCpuTimes = u.getScreenOffCpuFreqTimes(which, procState);
5835 if (screenOffCpuTimes != null) {
5836 sb.setLength(0);
5837 sb.append(" Screen-off cpu times per freq at state "
5838 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5839 for (int i = 0; i < screenOffCpuTimes.length; ++i) {
5840 sb.append(" " + screenOffCpuTimes[i]);
5841 }
5842 pw.println(sb.toString());
5843 }
5844 }
5845
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005846 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5847 = u.getProcessStats();
5848 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5849 final Uid.Proc ps = processStats.valueAt(ipr);
5850 long userTime;
5851 long systemTime;
5852 long foregroundTime;
5853 int starts;
5854 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005855
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005856 userTime = ps.getUserTime(which);
5857 systemTime = ps.getSystemTime(which);
5858 foregroundTime = ps.getForegroundTime(which);
5859 starts = ps.getStarts(which);
5860 final int numCrashes = ps.getNumCrashes(which);
5861 final int numAnrs = ps.getNumAnrs(which);
5862 numExcessive = which == STATS_SINCE_CHARGED
5863 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005864
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005865 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5866 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5867 sb.setLength(0);
5868 sb.append(prefix); sb.append(" Proc ");
5869 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5870 sb.append(prefix); sb.append(" CPU: ");
5871 formatTimeMs(sb, userTime); sb.append("usr + ");
5872 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5873 formatTimeMs(sb, foregroundTime); sb.append("fg");
5874 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5875 sb.append("\n"); sb.append(prefix); sb.append(" ");
5876 boolean hasOne = false;
5877 if (starts != 0) {
5878 hasOne = true;
5879 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005880 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005881 if (numCrashes != 0) {
5882 if (hasOne) {
5883 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005884 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005885 hasOne = true;
5886 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005887 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005888 if (numAnrs != 0) {
5889 if (hasOne) {
5890 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005891 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005892 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005893 }
5894 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005895 pw.println(sb.toString());
5896 for (int e=0; e<numExcessive; e++) {
5897 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5898 if (ew != null) {
5899 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07005900 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005901 pw.print("cpu");
5902 } else {
5903 pw.print("unknown");
5904 }
5905 pw.print(" use: ");
5906 TimeUtils.formatDuration(ew.usedTime, pw);
5907 pw.print(" over ");
5908 TimeUtils.formatDuration(ew.overTime, pw);
5909 if (ew.overTime != 0) {
5910 pw.print(" (");
5911 pw.print((ew.usedTime*100)/ew.overTime);
5912 pw.println("%)");
5913 }
5914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005915 }
5916 uidActivity = true;
5917 }
5918 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005919
5920 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5921 = u.getPackageStats();
5922 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
5923 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
5924 pw.println(":");
5925 boolean apkActivity = false;
5926 final Uid.Pkg ps = packageStats.valueAt(ipkg);
5927 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
5928 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
5929 pw.print(prefix); pw.print(" Wakeup alarm ");
5930 pw.print(alarms.keyAt(iwa)); pw.print(": ");
5931 pw.print(alarms.valueAt(iwa).getCountLocked(which));
5932 pw.println(" times");
5933 apkActivity = true;
5934 }
5935 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
5936 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
5937 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
5938 final long startTime = ss.getStartTime(batteryUptime, which);
5939 final int starts = ss.getStarts(which);
5940 final int launches = ss.getLaunches(which);
5941 if (startTime != 0 || starts != 0 || launches != 0) {
5942 sb.setLength(0);
5943 sb.append(prefix); sb.append(" Service ");
5944 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
5945 sb.append(prefix); sb.append(" Created for: ");
5946 formatTimeMs(sb, startTime / 1000);
5947 sb.append("uptime\n");
5948 sb.append(prefix); sb.append(" Starts: ");
5949 sb.append(starts);
5950 sb.append(", launches: "); sb.append(launches);
5951 pw.println(sb.toString());
5952 apkActivity = true;
5953 }
5954 }
5955 if (!apkActivity) {
5956 pw.print(prefix); pw.println(" (nothing executed)");
5957 }
5958 uidActivity = true;
5959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005960 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005961 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005962 }
5963 }
5964 }
5965
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005966 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005967 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005968 int diff = oldval ^ newval;
5969 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005970 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005971 for (int i=0; i<descriptions.length; i++) {
5972 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005973 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005974 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005975 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005976 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005977 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005978 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
5979 didWake = true;
5980 pw.print("=");
5981 if (longNames) {
5982 UserHandle.formatUid(pw, wakelockTag.uid);
5983 pw.print(":\"");
5984 pw.print(wakelockTag.string);
5985 pw.print("\"");
5986 } else {
5987 pw.print(wakelockTag.poolIdx);
5988 }
5989 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005990 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005991 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005992 pw.print("=");
5993 int val = (newval&bd.mask)>>bd.shift;
5994 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005995 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005996 } else {
5997 pw.print(val);
5998 }
5999 }
6000 }
6001 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006002 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07006003 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006004 if (longNames) {
6005 UserHandle.formatUid(pw, wakelockTag.uid);
6006 pw.print(":\"");
6007 pw.print(wakelockTag.string);
6008 pw.print("\"");
6009 } else {
6010 pw.print(wakelockTag.poolIdx);
6011 }
6012 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006013 }
Mike Mac2f518a2017-09-19 16:06:03 -07006014
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006015 public void prepareForDumpLocked() {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006016 // We don't need to require subclasses implement this.
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006017 }
6018
6019 public static class HistoryPrinter {
6020 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006021 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006022 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006023 int oldStatus = -1;
6024 int oldHealth = -1;
6025 int oldPlug = -1;
6026 int oldTemp = -1;
6027 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006028 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006029 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006030
Dianne Hackborn3251b902014-06-20 14:40:53 -07006031 void reset() {
6032 oldState = oldState2 = 0;
6033 oldLevel = -1;
6034 oldStatus = -1;
6035 oldHealth = -1;
6036 oldPlug = -1;
6037 oldTemp = -1;
6038 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006039 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07006040 }
6041
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006042 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006043 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006044 if (!checkin) {
6045 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006046 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006047 pw.print(" (");
6048 pw.print(rec.numReadInts);
6049 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006050 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006051 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6052 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006053 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006054 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006055 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006056 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006057 }
6058 lastTime = rec.time;
6059 }
6060 if (rec.cmd == HistoryItem.CMD_START) {
6061 if (checkin) {
6062 pw.print(":");
6063 }
6064 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006065 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006066 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
6067 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006068 if (checkin) {
6069 pw.print(":");
6070 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006071 if (rec.cmd == HistoryItem.CMD_RESET) {
6072 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006073 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006074 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006075 pw.print("TIME:");
6076 if (checkin) {
6077 pw.println(rec.currentTime);
6078 } else {
6079 pw.print(" ");
6080 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6081 rec.currentTime).toString());
6082 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006083 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
6084 if (checkin) {
6085 pw.print(":");
6086 }
6087 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006088 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
6089 if (checkin) {
6090 pw.print(":");
6091 }
6092 pw.println("*OVERFLOW*");
6093 } else {
6094 if (!checkin) {
6095 if (rec.batteryLevel < 10) pw.print("00");
6096 else if (rec.batteryLevel < 100) pw.print("0");
6097 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006098 if (verbose) {
6099 pw.print(" ");
6100 if (rec.states < 0) ;
6101 else if (rec.states < 0x10) pw.print("0000000");
6102 else if (rec.states < 0x100) pw.print("000000");
6103 else if (rec.states < 0x1000) pw.print("00000");
6104 else if (rec.states < 0x10000) pw.print("0000");
6105 else if (rec.states < 0x100000) pw.print("000");
6106 else if (rec.states < 0x1000000) pw.print("00");
6107 else if (rec.states < 0x10000000) pw.print("0");
6108 pw.print(Integer.toHexString(rec.states));
6109 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006110 } else {
6111 if (oldLevel != rec.batteryLevel) {
6112 oldLevel = rec.batteryLevel;
6113 pw.print(",Bl="); pw.print(rec.batteryLevel);
6114 }
6115 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006116 if (oldStatus != rec.batteryStatus) {
6117 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006118 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006119 switch (oldStatus) {
6120 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006121 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006122 break;
6123 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006124 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006125 break;
6126 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006127 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006128 break;
6129 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006130 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006131 break;
6132 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006133 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006134 break;
6135 default:
6136 pw.print(oldStatus);
6137 break;
6138 }
6139 }
6140 if (oldHealth != rec.batteryHealth) {
6141 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006142 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006143 switch (oldHealth) {
6144 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006145 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006146 break;
6147 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006148 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006149 break;
6150 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006151 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006152 break;
6153 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006154 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006155 break;
6156 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006157 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006158 break;
6159 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006160 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006161 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006162 case BatteryManager.BATTERY_HEALTH_COLD:
6163 pw.print(checkin ? "c" : "cold");
6164 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006165 default:
6166 pw.print(oldHealth);
6167 break;
6168 }
6169 }
6170 if (oldPlug != rec.batteryPlugType) {
6171 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006172 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006173 switch (oldPlug) {
6174 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006175 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006176 break;
6177 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006178 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006179 break;
6180 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006181 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006182 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006183 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006184 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006185 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006186 default:
6187 pw.print(oldPlug);
6188 break;
6189 }
6190 }
6191 if (oldTemp != rec.batteryTemperature) {
6192 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006193 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006194 pw.print(oldTemp);
6195 }
6196 if (oldVolt != rec.batteryVoltage) {
6197 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006198 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006199 pw.print(oldVolt);
6200 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006201 final int chargeMAh = rec.batteryChargeUAh / 1000;
6202 if (oldChargeMAh != chargeMAh) {
6203 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07006204 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006205 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07006206 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006207 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006208 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006209 printBitDescriptions(pw, oldState2, rec.states2, null,
6210 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006211 if (rec.wakeReasonTag != null) {
6212 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006213 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006214 pw.print(rec.wakeReasonTag.poolIdx);
6215 } else {
6216 pw.print(" wake_reason=");
6217 pw.print(rec.wakeReasonTag.uid);
6218 pw.print(":\"");
6219 pw.print(rec.wakeReasonTag.string);
6220 pw.print("\"");
6221 }
6222 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08006223 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006224 pw.print(checkin ? "," : " ");
6225 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
6226 pw.print("+");
6227 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
6228 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006229 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006230 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
6231 : HISTORY_EVENT_NAMES;
6232 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
6233 | HistoryItem.EVENT_FLAG_FINISH);
6234 if (idx >= 0 && idx < eventNames.length) {
6235 pw.print(eventNames[idx]);
6236 } else {
6237 pw.print(checkin ? "Ev" : "event");
6238 pw.print(idx);
6239 }
6240 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006241 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006242 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08006243 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08006244 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
6245 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006246 pw.print(":\"");
6247 pw.print(rec.eventTag.string);
6248 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006249 }
6250 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006251 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006252 if (rec.stepDetails != null) {
6253 if (!checkin) {
6254 pw.print(" Details: cpu=");
6255 pw.print(rec.stepDetails.userTime);
6256 pw.print("u+");
6257 pw.print(rec.stepDetails.systemTime);
6258 pw.print("s");
6259 if (rec.stepDetails.appCpuUid1 >= 0) {
6260 pw.print(" (");
6261 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
6262 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6263 if (rec.stepDetails.appCpuUid2 >= 0) {
6264 pw.print(", ");
6265 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
6266 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6267 }
6268 if (rec.stepDetails.appCpuUid3 >= 0) {
6269 pw.print(", ");
6270 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
6271 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6272 }
6273 pw.print(')');
6274 }
6275 pw.println();
6276 pw.print(" /proc/stat=");
6277 pw.print(rec.stepDetails.statUserTime);
6278 pw.print(" usr, ");
6279 pw.print(rec.stepDetails.statSystemTime);
6280 pw.print(" sys, ");
6281 pw.print(rec.stepDetails.statIOWaitTime);
6282 pw.print(" io, ");
6283 pw.print(rec.stepDetails.statIrqTime);
6284 pw.print(" irq, ");
6285 pw.print(rec.stepDetails.statSoftIrqTime);
6286 pw.print(" sirq, ");
6287 pw.print(rec.stepDetails.statIdlTime);
6288 pw.print(" idle");
6289 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
6290 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
6291 + rec.stepDetails.statSoftIrqTime;
6292 int total = totalRun + rec.stepDetails.statIdlTime;
6293 if (total > 0) {
6294 pw.print(" (");
6295 float perc = ((float)totalRun) / ((float)total) * 100;
6296 pw.print(String.format("%.1f%%", perc));
6297 pw.print(" of ");
6298 StringBuilder sb = new StringBuilder(64);
6299 formatTimeMsNoSpace(sb, total*10);
6300 pw.print(sb);
6301 pw.print(")");
6302 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006303 pw.print(", PlatformIdleStat ");
6304 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006305 pw.println();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006306
6307 pw.print(", SubsystemPowerState ");
6308 pw.print(rec.stepDetails.statSubsystemPowerState);
6309 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006310 } else {
6311 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6312 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
6313 pw.print(rec.stepDetails.userTime);
6314 pw.print(":");
6315 pw.print(rec.stepDetails.systemTime);
6316 if (rec.stepDetails.appCpuUid1 >= 0) {
6317 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
6318 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6319 if (rec.stepDetails.appCpuUid2 >= 0) {
6320 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
6321 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6322 }
6323 if (rec.stepDetails.appCpuUid3 >= 0) {
6324 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
6325 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6326 }
6327 }
6328 pw.println();
6329 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6330 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
6331 pw.print(rec.stepDetails.statUserTime);
6332 pw.print(',');
6333 pw.print(rec.stepDetails.statSystemTime);
6334 pw.print(',');
6335 pw.print(rec.stepDetails.statIOWaitTime);
6336 pw.print(',');
6337 pw.print(rec.stepDetails.statIrqTime);
6338 pw.print(',');
6339 pw.print(rec.stepDetails.statSoftIrqTime);
6340 pw.print(',');
6341 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006342 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006343 if (rec.stepDetails.statPlatformIdleState != null) {
6344 pw.print(rec.stepDetails.statPlatformIdleState);
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006345 if (rec.stepDetails.statSubsystemPowerState != null) {
6346 pw.print(',');
6347 }
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006348 }
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006349
6350 if (rec.stepDetails.statSubsystemPowerState != null) {
6351 pw.print(rec.stepDetails.statSubsystemPowerState);
6352 }
6353 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006354 }
6355 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006356 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006357 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006358 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006359 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006360
6361 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
6362 UserHandle.formatUid(pw, uid);
6363 pw.print("=");
6364 pw.print(utime);
6365 pw.print("u+");
6366 pw.print(stime);
6367 pw.print("s");
6368 }
6369
6370 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
6371 pw.print('/');
6372 pw.print(uid);
6373 pw.print(":");
6374 pw.print(utime);
6375 pw.print(":");
6376 pw.print(stime);
6377 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006378 }
6379
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006380 private void printSizeValue(PrintWriter pw, long size) {
6381 float result = size;
6382 String suffix = "";
6383 if (result >= 10*1024) {
6384 suffix = "KB";
6385 result = result / 1024;
6386 }
6387 if (result >= 10*1024) {
6388 suffix = "MB";
6389 result = result / 1024;
6390 }
6391 if (result >= 10*1024) {
6392 suffix = "GB";
6393 result = result / 1024;
6394 }
6395 if (result >= 10*1024) {
6396 suffix = "TB";
6397 result = result / 1024;
6398 }
6399 if (result >= 10*1024) {
6400 suffix = "PB";
6401 result = result / 1024;
6402 }
6403 pw.print((int)result);
6404 pw.print(suffix);
6405 }
6406
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006407 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
6408 String label3, long estimatedTime) {
6409 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006410 return false;
6411 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006412 pw.print(label1);
6413 pw.print(label2);
6414 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006415 StringBuilder sb = new StringBuilder(64);
6416 formatTimeMs(sb, estimatedTime);
6417 pw.print(sb);
6418 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006419 return true;
6420 }
6421
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006422 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
6423 LevelStepTracker steps, boolean checkin) {
6424 if (steps == null) {
6425 return false;
6426 }
6427 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006428 if (count <= 0) {
6429 return false;
6430 }
6431 if (!checkin) {
6432 pw.println(header);
6433 }
Kweku Adams030980a2015-04-01 16:07:48 -07006434 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006435 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006436 long duration = steps.getDurationAt(i);
6437 int level = steps.getLevelAt(i);
6438 long initMode = steps.getInitModeAt(i);
6439 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006440 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006441 lineArgs[0] = Long.toString(duration);
6442 lineArgs[1] = Integer.toString(level);
6443 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6444 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6445 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
6446 case Display.STATE_ON: lineArgs[2] = "s+"; break;
6447 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
6448 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07006449 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006450 }
6451 } else {
6452 lineArgs[2] = "";
6453 }
6454 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6455 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
6456 } else {
6457 lineArgs[3] = "";
6458 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006459 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07006460 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006461 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07006462 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006463 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006464 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
6465 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006466 pw.print(prefix);
6467 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006468 TimeUtils.formatDuration(duration, pw);
6469 pw.print(" to "); pw.print(level);
6470 boolean haveModes = false;
6471 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6472 pw.print(" (");
6473 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6474 case Display.STATE_OFF: pw.print("screen-off"); break;
6475 case Display.STATE_ON: pw.print("screen-on"); break;
6476 case Display.STATE_DOZE: pw.print("screen-doze"); break;
6477 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07006478 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006479 }
6480 haveModes = true;
6481 }
6482 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6483 pw.print(haveModes ? ", " : " (");
6484 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
6485 ? "power-save-on" : "power-save-off");
6486 haveModes = true;
6487 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006488 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6489 pw.print(haveModes ? ", " : " (");
6490 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6491 ? "device-idle-on" : "device-idle-off");
6492 haveModes = true;
6493 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006494 if (haveModes) {
6495 pw.print(")");
6496 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006497 pw.println();
6498 }
6499 }
6500 return true;
6501 }
6502
Kweku Adams87b19ec2017-10-09 12:40:03 -07006503 private static void dumpDurationSteps(ProtoOutputStream proto, long fieldId,
6504 LevelStepTracker steps) {
6505 if (steps == null) {
6506 return;
6507 }
6508 int count = steps.mNumStepDurations;
Kweku Adams87b19ec2017-10-09 12:40:03 -07006509 for (int i = 0; i < count; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07006510 long token = proto.start(fieldId);
Kweku Adams87b19ec2017-10-09 12:40:03 -07006511 proto.write(SystemProto.BatteryLevelStep.DURATION_MS, steps.getDurationAt(i));
6512 proto.write(SystemProto.BatteryLevelStep.LEVEL, steps.getLevelAt(i));
6513
6514 final long initMode = steps.getInitModeAt(i);
6515 final long modMode = steps.getModModeAt(i);
6516
6517 int ds = SystemProto.BatteryLevelStep.DS_MIXED;
6518 if ((modMode & STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6519 switch ((int) (initMode & STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6520 case Display.STATE_OFF:
6521 ds = SystemProto.BatteryLevelStep.DS_OFF;
6522 break;
6523 case Display.STATE_ON:
6524 ds = SystemProto.BatteryLevelStep.DS_ON;
6525 break;
6526 case Display.STATE_DOZE:
6527 ds = SystemProto.BatteryLevelStep.DS_DOZE;
6528 break;
6529 case Display.STATE_DOZE_SUSPEND:
6530 ds = SystemProto.BatteryLevelStep.DS_DOZE_SUSPEND;
6531 break;
6532 default:
6533 ds = SystemProto.BatteryLevelStep.DS_ERROR;
6534 break;
6535 }
6536 }
6537 proto.write(SystemProto.BatteryLevelStep.DISPLAY_STATE, ds);
6538
6539 int psm = SystemProto.BatteryLevelStep.PSM_MIXED;
6540 if ((modMode & STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6541 psm = (initMode & STEP_LEVEL_MODE_POWER_SAVE) != 0
6542 ? SystemProto.BatteryLevelStep.PSM_ON : SystemProto.BatteryLevelStep.PSM_OFF;
6543 }
6544 proto.write(SystemProto.BatteryLevelStep.POWER_SAVE_MODE, psm);
6545
6546 int im = SystemProto.BatteryLevelStep.IM_MIXED;
6547 if ((modMode & STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6548 im = (initMode & STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6549 ? SystemProto.BatteryLevelStep.IM_ON : SystemProto.BatteryLevelStep.IM_OFF;
6550 }
6551 proto.write(SystemProto.BatteryLevelStep.IDLE_MODE, im);
6552
6553 proto.end(token);
6554 }
6555 }
6556
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006557 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006558 public static final int DUMP_DAILY_ONLY = 1<<2;
6559 public static final int DUMP_HISTORY_ONLY = 1<<3;
6560 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
6561 public static final int DUMP_VERBOSE = 1<<5;
6562 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006563
Dianne Hackborn37de0982014-05-09 09:32:18 -07006564 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
6565 final HistoryPrinter hprinter = new HistoryPrinter();
6566 final HistoryItem rec = new HistoryItem();
6567 long lastTime = -1;
6568 long baseTime = -1;
6569 boolean printed = false;
6570 HistoryEventTracker tracker = null;
6571 while (getNextHistoryLocked(rec)) {
6572 lastTime = rec.time;
6573 if (baseTime < 0) {
6574 baseTime = lastTime;
6575 }
6576 if (rec.time >= histStart) {
6577 if (histStart >= 0 && !printed) {
6578 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07006579 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006580 || rec.cmd == HistoryItem.CMD_START
6581 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006582 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006583 hprinter.printNextItem(pw, rec, baseTime, checkin,
6584 (flags&DUMP_VERBOSE) != 0);
6585 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006586 } else if (rec.currentTime != 0) {
6587 printed = true;
6588 byte cmd = rec.cmd;
6589 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006590 hprinter.printNextItem(pw, rec, baseTime, checkin,
6591 (flags&DUMP_VERBOSE) != 0);
6592 rec.cmd = cmd;
6593 }
6594 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006595 if (rec.cmd != HistoryItem.CMD_UPDATE) {
6596 hprinter.printNextItem(pw, rec, baseTime, checkin,
6597 (flags&DUMP_VERBOSE) != 0);
6598 rec.cmd = HistoryItem.CMD_UPDATE;
6599 }
6600 int oldEventCode = rec.eventCode;
6601 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006602 rec.eventTag = new HistoryTag();
6603 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
6604 HashMap<String, SparseIntArray> active
6605 = tracker.getStateForEvent(i);
6606 if (active == null) {
6607 continue;
6608 }
6609 for (HashMap.Entry<String, SparseIntArray> ent
6610 : active.entrySet()) {
6611 SparseIntArray uids = ent.getValue();
6612 for (int j=0; j<uids.size(); j++) {
6613 rec.eventCode = i;
6614 rec.eventTag.string = ent.getKey();
6615 rec.eventTag.uid = uids.keyAt(j);
6616 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006617 hprinter.printNextItem(pw, rec, baseTime, checkin,
6618 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006619 rec.wakeReasonTag = null;
6620 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006621 }
6622 }
6623 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006624 rec.eventCode = oldEventCode;
6625 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006626 tracker = null;
6627 }
6628 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006629 hprinter.printNextItem(pw, rec, baseTime, checkin,
6630 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07006631 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
6632 // This is an attempt to aggregate the previous state and generate
6633 // fake events to reflect that state at the point where we start
6634 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07006635 if (tracker == null) {
6636 tracker = new HistoryEventTracker();
6637 }
6638 tracker.updateState(rec.eventCode, rec.eventTag.string,
6639 rec.eventTag.uid, rec.eventTag.poolIdx);
6640 }
6641 }
6642 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07006643 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006644 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
6645 }
6646 }
6647
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006648 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
6649 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
6650 if (steps == null) {
6651 return;
6652 }
6653 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
6654 if (timeRemaining >= 0) {
6655 pw.print(prefix); pw.print(label); pw.print(" total time: ");
6656 tmpSb.setLength(0);
6657 formatTimeMs(tmpSb, timeRemaining);
6658 pw.print(tmpSb);
6659 pw.print(" (from "); pw.print(tmpOutInt[0]);
6660 pw.println(" steps)");
6661 }
6662 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6663 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6664 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
6665 if (estimatedTime > 0) {
6666 pw.print(prefix); pw.print(label); pw.print(" ");
6667 pw.print(STEP_LEVEL_MODE_LABELS[i]);
6668 pw.print(" time: ");
6669 tmpSb.setLength(0);
6670 formatTimeMs(tmpSb, estimatedTime);
6671 pw.print(tmpSb);
6672 pw.print(" (from "); pw.print(tmpOutInt[0]);
6673 pw.println(" steps)");
6674 }
6675 }
6676 }
6677
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006678 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
6679 ArrayList<PackageChange> changes) {
6680 if (changes == null) {
6681 return;
6682 }
6683 pw.print(prefix); pw.println("Package changes:");
6684 for (int i=0; i<changes.size(); i++) {
6685 PackageChange pc = changes.get(i);
6686 if (pc.mUpdate) {
6687 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
6688 pw.print(" vers="); pw.println(pc.mVersionCode);
6689 } else {
6690 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
6691 }
6692 }
6693 }
6694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006695 /**
6696 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
6697 *
6698 * @param pw a Printer to receive the dump output.
6699 */
6700 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006701 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006702 prepareForDumpLocked();
6703
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006704 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006705 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006706
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006707 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006708 final long historyTotalSize = getHistoryTotalSize();
6709 final long historyUsedSize = getHistoryUsedSize();
6710 if (startIteratingHistoryLocked()) {
6711 try {
6712 pw.print("Battery History (");
6713 pw.print((100*historyUsedSize)/historyTotalSize);
6714 pw.print("% used, ");
6715 printSizeValue(pw, historyUsedSize);
6716 pw.print(" used of ");
6717 printSizeValue(pw, historyTotalSize);
6718 pw.print(", ");
6719 pw.print(getHistoryStringPoolSize());
6720 pw.print(" strings using ");
6721 printSizeValue(pw, getHistoryStringPoolBytes());
6722 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07006723 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006724 pw.println();
6725 } finally {
6726 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006727 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006728 }
6729
6730 if (startIteratingOldHistoryLocked()) {
6731 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006732 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006733 pw.println("Old battery History:");
6734 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006735 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006736 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006737 if (baseTime < 0) {
6738 baseTime = rec.time;
6739 }
6740 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006741 }
6742 pw.println();
6743 } finally {
6744 finishIteratingOldHistoryLocked();
6745 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006746 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006747 }
6748
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006749 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006750 return;
6751 }
6752
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006753 if (!filtering) {
6754 SparseArray<? extends Uid> uidStats = getUidStats();
6755 final int NU = uidStats.size();
6756 boolean didPid = false;
6757 long nowRealtime = SystemClock.elapsedRealtime();
6758 for (int i=0; i<NU; i++) {
6759 Uid uid = uidStats.valueAt(i);
6760 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
6761 if (pids != null) {
6762 for (int j=0; j<pids.size(); j++) {
6763 Uid.Pid pid = pids.valueAt(j);
6764 if (!didPid) {
6765 pw.println("Per-PID Stats:");
6766 didPid = true;
6767 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006768 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
6769 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006770 pw.print(" PID "); pw.print(pids.keyAt(j));
6771 pw.print(" wake time: ");
6772 TimeUtils.formatDuration(time, pw);
6773 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006774 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006775 }
6776 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006777 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006778 pw.println();
6779 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006780 }
6781
6782 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006783 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
6784 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006785 long timeRemaining = computeBatteryTimeRemaining(
6786 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006787 if (timeRemaining >= 0) {
6788 pw.print(" Estimated discharge time remaining: ");
6789 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6790 pw.println();
6791 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006792 final LevelStepTracker steps = getDischargeLevelStepTracker();
6793 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6794 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
6795 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6796 STEP_LEVEL_MODE_VALUES[i], null));
6797 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006798 pw.println();
6799 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006800 if (dumpDurationSteps(pw, " ", "Charge step durations:",
6801 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006802 long timeRemaining = computeChargeTimeRemaining(
6803 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006804 if (timeRemaining >= 0) {
6805 pw.print(" Estimated charge time remaining: ");
6806 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6807 pw.println();
6808 }
6809 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006810 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006811 }
Dianne Hackbornc81983a2017-10-20 16:16:32 -07006812 if (!filtering || (flags & DUMP_DAILY_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006813 pw.println("Daily stats:");
6814 pw.print(" Current start time: ");
6815 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6816 getCurrentDailyStartTime()).toString());
6817 pw.print(" Next min deadline: ");
6818 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6819 getNextMinDailyDeadline()).toString());
6820 pw.print(" Next max deadline: ");
6821 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6822 getNextMaxDailyDeadline()).toString());
6823 StringBuilder sb = new StringBuilder(64);
6824 int[] outInt = new int[1];
6825 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
6826 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006827 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
6828 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006829 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006830 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
6831 dsteps, false)) {
6832 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6833 sb, outInt);
6834 }
6835 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6836 csteps, false)) {
6837 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6838 sb, outInt);
6839 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006840 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006841 } else {
6842 pw.println(" Current daily steps:");
6843 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6844 sb, outInt);
6845 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6846 sb, outInt);
6847 }
6848 }
6849 DailyItem dit;
6850 int curIndex = 0;
6851 while ((dit=getDailyItemLocked(curIndex)) != null) {
6852 curIndex++;
6853 if ((flags&DUMP_DAILY_ONLY) != 0) {
6854 pw.println();
6855 }
6856 pw.print(" Daily from ");
6857 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6858 pw.print(" to ");
6859 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6860 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006861 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006862 if (dumpDurationSteps(pw, " ",
6863 " Discharge step durations:", dit.mDischargeSteps, false)) {
6864 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6865 sb, outInt);
6866 }
6867 if (dumpDurationSteps(pw, " ",
6868 " Charge step durations:", dit.mChargeSteps, false)) {
6869 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6870 sb, outInt);
6871 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006872 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006873 } else {
6874 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6875 sb, outInt);
6876 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6877 sb, outInt);
6878 }
6879 }
6880 pw.println();
6881 }
6882 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006883 pw.println("Statistics since last charge:");
6884 pw.println(" System starts: " + getStartCount()
6885 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006886 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6887 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006888 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006890 }
Mike Mac2f518a2017-09-19 16:06:03 -07006891
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006892 // This is called from BatteryStatsService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006893 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006894 public void dumpCheckinLocked(Context context, PrintWriter pw,
6895 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006896 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006897
6898 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006899 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6900 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006901
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006902 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6903
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006904 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07006905 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006906 try {
6907 for (int i=0; i<getHistoryStringPoolSize(); i++) {
6908 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6909 pw.print(HISTORY_STRING_POOL); pw.print(',');
6910 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006911 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006912 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006913 pw.print(",\"");
6914 String str = getHistoryTagPoolString(i);
6915 str = str.replace("\\", "\\\\");
6916 str = str.replace("\"", "\\\"");
6917 pw.print(str);
6918 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006919 pw.println();
6920 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006921 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006922 } finally {
6923 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08006924 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006925 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006926 }
6927
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006928 if ((flags & DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006929 return;
6930 }
6931
Dianne Hackborne4a59512010-12-07 11:08:07 -08006932 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006933 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08006934 for (int i=0; i<apps.size(); i++) {
6935 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006936 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
6937 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08006938 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006939 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
6940 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006941 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006942 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006943 }
6944 SparseArray<? extends Uid> uidStats = getUidStats();
6945 final int NU = uidStats.size();
6946 String[] lineArgs = new String[2];
6947 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006948 int uid = UserHandle.getAppId(uidStats.keyAt(i));
6949 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
6950 if (pkgs != null && !pkgs.second.value) {
6951 pkgs.second.value = true;
6952 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08006953 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006954 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006955 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
6956 (Object[])lineArgs);
6957 }
6958 }
6959 }
6960 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006961 if ((flags & DUMP_DAILY_ONLY) == 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006962 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006963 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07006964 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006965 if (timeRemaining >= 0) {
6966 lineArgs[0] = Long.toString(timeRemaining);
6967 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
6968 (Object[])lineArgs);
6969 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006970 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07006971 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006972 if (timeRemaining >= 0) {
6973 lineArgs[0] = Long.toString(timeRemaining);
6974 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
6975 (Object[])lineArgs);
6976 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006977 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
6978 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006980 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006981
Kweku Adams87b19ec2017-10-09 12:40:03 -07006982 /** Dump #STATS_SINCE_CHARGED batterystats data to a proto. @hide */
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006983 public void dumpProtoLocked(Context context, FileDescriptor fd, List<ApplicationInfo> apps,
Kweku Adams6ccebf22017-12-11 12:30:35 -08006984 int flags) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006985 final ProtoOutputStream proto = new ProtoOutputStream(fd);
6986 final long bToken = proto.start(BatteryStatsServiceDumpProto.BATTERYSTATS);
6987 prepareForDumpLocked();
6988
6989 proto.write(BatteryStatsProto.REPORT_VERSION, CHECKIN_VERSION);
6990 proto.write(BatteryStatsProto.PARCEL_VERSION, getParcelVersion());
6991 proto.write(BatteryStatsProto.START_PLATFORM_VERSION, getStartPlatformVersion());
6992 proto.write(BatteryStatsProto.END_PLATFORM_VERSION, getEndPlatformVersion());
6993
Kweku Adams6ccebf22017-12-11 12:30:35 -08006994 // History intentionally not included in proto dump.
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006995
6996 if ((flags & (DUMP_HISTORY_ONLY | DUMP_DAILY_ONLY)) == 0) {
Kweku Adams103351f2017-10-16 14:39:34 -07006997 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false,
6998 (flags & DUMP_DEVICE_WIFI_ONLY) != 0);
6999 helper.create(this);
7000 helper.refreshStats(STATS_SINCE_CHARGED, UserHandle.USER_ALL);
7001
7002 dumpProtoAppsLocked(proto, helper, apps);
7003 dumpProtoSystemLocked(proto, helper);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007004 }
7005
7006 proto.end(bToken);
7007 proto.flush();
7008 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07007009
Kweku Adams103351f2017-10-16 14:39:34 -07007010 private void dumpProtoAppsLocked(ProtoOutputStream proto, BatteryStatsHelper helper,
7011 List<ApplicationInfo> apps) {
7012 final int which = STATS_SINCE_CHARGED;
7013 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7014 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7015 final long rawRealtimeUs = rawRealtimeMs * 1000;
7016 final long batteryUptimeUs = getBatteryUptime(rawUptimeUs);
7017
7018 SparseArray<ArrayList<String>> aidToPackages = new SparseArray<>();
7019 if (apps != null) {
7020 for (int i = 0; i < apps.size(); ++i) {
7021 ApplicationInfo ai = apps.get(i);
7022 int aid = UserHandle.getAppId(ai.uid);
7023 ArrayList<String> pkgs = aidToPackages.get(aid);
7024 if (pkgs == null) {
7025 pkgs = new ArrayList<String>();
7026 aidToPackages.put(aid, pkgs);
7027 }
7028 pkgs.add(ai.packageName);
7029 }
7030 }
7031
7032 SparseArray<BatterySipper> uidToSipper = new SparseArray<>();
7033 final List<BatterySipper> sippers = helper.getUsageList();
7034 if (sippers != null) {
7035 for (int i = 0; i < sippers.size(); ++i) {
7036 final BatterySipper bs = sippers.get(i);
7037 if (bs.drainType != BatterySipper.DrainType.APP) {
7038 // Others are handled by dumpProtoSystemLocked()
7039 continue;
7040 }
7041 uidToSipper.put(bs.uidObj.getUid(), bs);
7042 }
7043 }
7044
7045 SparseArray<? extends Uid> uidStats = getUidStats();
7046 final int n = uidStats.size();
7047 for (int iu = 0; iu < n; ++iu) {
7048 final long uTkn = proto.start(BatteryStatsProto.UIDS);
7049 final Uid u = uidStats.valueAt(iu);
7050
7051 final int uid = uidStats.keyAt(iu);
7052 proto.write(UidProto.UID, uid);
7053
7054 // Print packages and apk stats (UID_DATA & APK_DATA)
7055 ArrayList<String> pkgs = aidToPackages.get(UserHandle.getAppId(uid));
7056 if (pkgs == null) {
7057 pkgs = new ArrayList<String>();
7058 }
7059 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats =
7060 u.getPackageStats();
7061 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7062 String pkg = packageStats.keyAt(ipkg);
7063 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats =
7064 packageStats.valueAt(ipkg).getServiceStats();
7065 if (serviceStats.size() == 0) {
7066 // Due to the way ActivityManagerService logs wakeup alarms, some packages (for
7067 // example, "android") may be included in the packageStats that aren't part of
7068 // the UID. If they don't have any services, then they shouldn't be listed here.
7069 // These packages won't be a part in the pkgs List.
7070 continue;
7071 }
7072
7073 final long pToken = proto.start(UidProto.PACKAGES);
7074 proto.write(UidProto.Package.NAME, pkg);
7075 // Remove from the packages list since we're logging it here.
7076 pkgs.remove(pkg);
7077
7078 for (int isvc = serviceStats.size() - 1; isvc >= 0; --isvc) {
7079 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
7080 long sToken = proto.start(UidProto.Package.SERVICES);
7081
7082 proto.write(UidProto.Package.Service.NAME, serviceStats.keyAt(isvc));
7083 proto.write(UidProto.Package.Service.START_DURATION_MS,
7084 roundUsToMs(ss.getStartTime(batteryUptimeUs, which)));
7085 proto.write(UidProto.Package.Service.START_COUNT, ss.getStarts(which));
7086 proto.write(UidProto.Package.Service.LAUNCH_COUNT, ss.getLaunches(which));
7087
7088 proto.end(sToken);
7089 }
7090 proto.end(pToken);
7091 }
7092 // Print any remaining packages that weren't in the packageStats map. pkgs is pulled
7093 // from PackageManager data. Packages are only included in packageStats if there was
7094 // specific data tracked for them (services and wakeup alarms, etc.).
7095 for (String p : pkgs) {
7096 final long pToken = proto.start(UidProto.PACKAGES);
7097 proto.write(UidProto.Package.NAME, p);
7098 proto.end(pToken);
7099 }
7100
7101 // Total wakelock data (AGGREGATED_WAKELOCK_DATA)
7102 if (u.getAggregatedPartialWakelockTimer() != null) {
7103 final Timer timer = u.getAggregatedPartialWakelockTimer();
7104 // Times are since reset (regardless of 'which')
7105 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
7106 final Timer bgTimer = timer.getSubTimer();
7107 final long bgTimeMs = bgTimer != null
7108 ? bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
7109 final long awToken = proto.start(UidProto.AGGREGATED_WAKELOCK);
7110 proto.write(UidProto.AggregatedWakelock.PARTIAL_DURATION_MS, totTimeMs);
7111 proto.write(UidProto.AggregatedWakelock.BACKGROUND_PARTIAL_DURATION_MS, bgTimeMs);
7112 proto.end(awToken);
7113 }
7114
7115 // Audio (AUDIO_DATA)
7116 dumpTimer(proto, UidProto.AUDIO, u.getAudioTurnedOnTimer(), rawRealtimeUs, which);
7117
7118 // Bluetooth Controller (BLUETOOTH_CONTROLLER_DATA)
7119 dumpControllerActivityProto(proto, UidProto.BLUETOOTH_CONTROLLER,
7120 u.getBluetoothControllerActivity(), which);
7121
7122 // BLE scans (BLUETOOTH_MISC_DATA) (uses totalDurationMsLocked and MaxDurationMsLocked)
7123 final Timer bleTimer = u.getBluetoothScanTimer();
7124 if (bleTimer != null) {
7125 final long bmToken = proto.start(UidProto.BLUETOOTH_MISC);
7126
7127 dumpTimer(proto, UidProto.BluetoothMisc.APPORTIONED_BLE_SCAN, bleTimer,
7128 rawRealtimeUs, which);
7129 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN,
7130 u.getBluetoothScanBackgroundTimer(), rawRealtimeUs, which);
7131 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
7132 dumpTimer(proto, UidProto.BluetoothMisc.UNOPTIMIZED_BLE_SCAN,
7133 u.getBluetoothUnoptimizedScanTimer(), rawRealtimeUs, which);
7134 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
7135 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_UNOPTIMIZED_BLE_SCAN,
7136 u.getBluetoothUnoptimizedScanBackgroundTimer(), rawRealtimeUs, which);
7137 // Result counters
7138 proto.write(UidProto.BluetoothMisc.BLE_SCAN_RESULT_COUNT,
7139 u.getBluetoothScanResultCounter() != null
7140 ? u.getBluetoothScanResultCounter().getCountLocked(which) : 0);
7141 proto.write(UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN_RESULT_COUNT,
7142 u.getBluetoothScanResultBgCounter() != null
7143 ? u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0);
7144
7145 proto.end(bmToken);
7146 }
7147
7148 // Camera (CAMERA_DATA)
7149 dumpTimer(proto, UidProto.CAMERA, u.getCameraTurnedOnTimer(), rawRealtimeUs, which);
7150
7151 // CPU stats (CPU_DATA & CPU_TIMES_AT_FREQ_DATA)
7152 final long cpuToken = proto.start(UidProto.CPU);
7153 proto.write(UidProto.Cpu.USER_DURATION_MS, roundUsToMs(u.getUserCpuTimeUs(which)));
7154 proto.write(UidProto.Cpu.SYSTEM_DURATION_MS, roundUsToMs(u.getSystemCpuTimeUs(which)));
7155
7156 final long[] cpuFreqs = getCpuFreqs();
7157 if (cpuFreqs != null) {
7158 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
7159 // If total cpuFreqTimes is null, then we don't need to check for
7160 // screenOffCpuFreqTimes.
7161 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
7162 long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
7163 if (screenOffCpuFreqTimeMs == null) {
7164 screenOffCpuFreqTimeMs = new long[cpuFreqTimeMs.length];
7165 }
7166 for (int ic = 0; ic < cpuFreqTimeMs.length; ++ic) {
7167 long cToken = proto.start(UidProto.Cpu.BY_FREQUENCY);
7168 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7169 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7170 cpuFreqTimeMs[ic]);
7171 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7172 screenOffCpuFreqTimeMs[ic]);
7173 proto.end(cToken);
7174 }
7175 }
7176 }
Sudheer Shanka6d658d72018-01-01 01:36:49 -08007177
7178 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
7179 final long[] timesMs = u.getCpuFreqTimes(which, procState);
7180 if (timesMs != null && timesMs.length == cpuFreqs.length) {
7181 long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(which, procState);
7182 if (screenOffTimesMs == null) {
7183 screenOffTimesMs = new long[timesMs.length];
7184 }
7185 final long procToken = proto.start(UidProto.Cpu.BY_PROCESS_STATE);
7186 proto.write(UidProto.Cpu.ByProcessState.PROCESS_STATE, procState);
7187 for (int ic = 0; ic < timesMs.length; ++ic) {
7188 long cToken = proto.start(UidProto.Cpu.ByProcessState.BY_FREQUENCY);
7189 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7190 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7191 timesMs[ic]);
7192 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7193 screenOffTimesMs[ic]);
7194 proto.end(cToken);
7195 }
7196 proto.end(procToken);
7197 }
7198 }
Kweku Adams103351f2017-10-16 14:39:34 -07007199 proto.end(cpuToken);
7200
7201 // Flashlight (FLASHLIGHT_DATA)
7202 dumpTimer(proto, UidProto.FLASHLIGHT, u.getFlashlightTurnedOnTimer(),
7203 rawRealtimeUs, which);
7204
7205 // Foreground activity (FOREGROUND_ACTIVITY_DATA)
7206 dumpTimer(proto, UidProto.FOREGROUND_ACTIVITY, u.getForegroundActivityTimer(),
7207 rawRealtimeUs, which);
7208
7209 // Foreground service (FOREGROUND_SERVICE_DATA)
7210 dumpTimer(proto, UidProto.FOREGROUND_SERVICE, u.getForegroundServiceTimer(),
7211 rawRealtimeUs, which);
7212
7213 // Job completion (JOB_COMPLETION_DATA)
7214 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
7215 final int[] reasons = new int[]{
7216 JobParameters.REASON_CANCELED,
7217 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED,
7218 JobParameters.REASON_PREEMPT,
7219 JobParameters.REASON_TIMEOUT,
7220 JobParameters.REASON_DEVICE_IDLE,
7221 };
7222 for (int ic = 0; ic < completions.size(); ++ic) {
7223 SparseIntArray types = completions.valueAt(ic);
7224 if (types != null) {
7225 final long jcToken = proto.start(UidProto.JOB_COMPLETION);
7226
7227 proto.write(UidProto.JobCompletion.NAME, completions.keyAt(ic));
7228
7229 for (int r : reasons) {
7230 long rToken = proto.start(UidProto.JobCompletion.REASON_COUNT);
7231 proto.write(UidProto.JobCompletion.ReasonCount.NAME, r);
7232 proto.write(UidProto.JobCompletion.ReasonCount.COUNT, types.get(r, 0));
7233 proto.end(rToken);
7234 }
7235
7236 proto.end(jcToken);
7237 }
7238 }
7239
7240 // Scheduled jobs (JOB_DATA)
7241 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
7242 for (int ij = jobs.size() - 1; ij >= 0; --ij) {
7243 final Timer timer = jobs.valueAt(ij);
7244 final Timer bgTimer = timer.getSubTimer();
7245 final long jToken = proto.start(UidProto.JOBS);
7246
7247 proto.write(UidProto.Job.NAME, jobs.keyAt(ij));
7248 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7249 dumpTimer(proto, UidProto.Job.TOTAL, timer, rawRealtimeUs, which);
7250 dumpTimer(proto, UidProto.Job.BACKGROUND, bgTimer, rawRealtimeUs, which);
7251
7252 proto.end(jToken);
7253 }
7254
7255 // Modem Controller (MODEM_CONTROLLER_DATA)
7256 dumpControllerActivityProto(proto, UidProto.MODEM_CONTROLLER,
7257 u.getModemControllerActivity(), which);
7258
7259 // Network stats (NETWORK_DATA)
7260 final long nToken = proto.start(UidProto.NETWORK);
7261 proto.write(UidProto.Network.MOBILE_BYTES_RX,
7262 u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7263 proto.write(UidProto.Network.MOBILE_BYTES_TX,
7264 u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7265 proto.write(UidProto.Network.WIFI_BYTES_RX,
7266 u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7267 proto.write(UidProto.Network.WIFI_BYTES_TX,
7268 u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7269 proto.write(UidProto.Network.BT_BYTES_RX,
7270 u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7271 proto.write(UidProto.Network.BT_BYTES_TX,
7272 u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
7273 proto.write(UidProto.Network.MOBILE_PACKETS_RX,
7274 u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7275 proto.write(UidProto.Network.MOBILE_PACKETS_TX,
7276 u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7277 proto.write(UidProto.Network.WIFI_PACKETS_RX,
7278 u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7279 proto.write(UidProto.Network.WIFI_PACKETS_TX,
7280 u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7281 proto.write(UidProto.Network.MOBILE_ACTIVE_DURATION_MS,
7282 roundUsToMs(u.getMobileRadioActiveTime(which)));
7283 proto.write(UidProto.Network.MOBILE_ACTIVE_COUNT,
7284 u.getMobileRadioActiveCount(which));
7285 proto.write(UidProto.Network.MOBILE_WAKEUP_COUNT,
7286 u.getMobileRadioApWakeupCount(which));
7287 proto.write(UidProto.Network.WIFI_WAKEUP_COUNT,
7288 u.getWifiRadioApWakeupCount(which));
7289 proto.write(UidProto.Network.MOBILE_BYTES_BG_RX,
7290 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA, which));
7291 proto.write(UidProto.Network.MOBILE_BYTES_BG_TX,
7292 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA, which));
7293 proto.write(UidProto.Network.WIFI_BYTES_BG_RX,
7294 u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which));
7295 proto.write(UidProto.Network.WIFI_BYTES_BG_TX,
7296 u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which));
7297 proto.write(UidProto.Network.MOBILE_PACKETS_BG_RX,
7298 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA, which));
7299 proto.write(UidProto.Network.MOBILE_PACKETS_BG_TX,
7300 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA, which));
7301 proto.write(UidProto.Network.WIFI_PACKETS_BG_RX,
7302 u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA, which));
7303 proto.write(UidProto.Network.WIFI_PACKETS_BG_TX,
7304 u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA, which));
7305 proto.end(nToken);
7306
7307 // Power use item (POWER_USE_ITEM_DATA)
7308 BatterySipper bs = uidToSipper.get(uid);
7309 if (bs != null) {
7310 final long bsToken = proto.start(UidProto.POWER_USE_ITEM);
7311 proto.write(UidProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7312 proto.write(UidProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7313 proto.write(UidProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7314 proto.write(UidProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7315 bs.proportionalSmearMah);
7316 proto.end(bsToken);
7317 }
7318
7319 // Processes (PROCESS_DATA)
7320 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats =
7321 u.getProcessStats();
7322 for (int ipr = processStats.size() - 1; ipr >= 0; --ipr) {
7323 final Uid.Proc ps = processStats.valueAt(ipr);
7324 final long prToken = proto.start(UidProto.PROCESS);
7325
7326 proto.write(UidProto.Process.NAME, processStats.keyAt(ipr));
7327 proto.write(UidProto.Process.USER_DURATION_MS, ps.getUserTime(which));
7328 proto.write(UidProto.Process.SYSTEM_DURATION_MS, ps.getSystemTime(which));
7329 proto.write(UidProto.Process.FOREGROUND_DURATION_MS, ps.getForegroundTime(which));
7330 proto.write(UidProto.Process.START_COUNT, ps.getStarts(which));
7331 proto.write(UidProto.Process.ANR_COUNT, ps.getNumAnrs(which));
7332 proto.write(UidProto.Process.CRASH_COUNT, ps.getNumCrashes(which));
7333
7334 proto.end(prToken);
7335 }
7336
7337 // Sensors (SENSOR_DATA)
7338 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
7339 for (int ise = 0; ise < sensors.size(); ++ise) {
7340 final Uid.Sensor se = sensors.valueAt(ise);
7341 final Timer timer = se.getSensorTime();
7342 if (timer == null) {
7343 continue;
7344 }
7345 final Timer bgTimer = se.getSensorBackgroundTime();
7346 final int sensorNumber = sensors.keyAt(ise);
7347 final long seToken = proto.start(UidProto.SENSORS);
7348
7349 proto.write(UidProto.Sensor.ID, sensorNumber);
7350 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7351 dumpTimer(proto, UidProto.Sensor.APPORTIONED, timer, rawRealtimeUs, which);
7352 dumpTimer(proto, UidProto.Sensor.BACKGROUND, bgTimer, rawRealtimeUs, which);
7353
7354 proto.end(seToken);
7355 }
7356
7357 // State times (STATE_TIME_DATA)
7358 for (int ips = 0; ips < Uid.NUM_PROCESS_STATE; ++ips) {
7359 long durMs = roundUsToMs(u.getProcessStateTime(ips, rawRealtimeUs, which));
7360 if (durMs == 0) {
7361 continue;
7362 }
7363 final long stToken = proto.start(UidProto.STATES);
7364 proto.write(UidProto.StateTime.STATE, ips);
7365 proto.write(UidProto.StateTime.DURATION_MS, durMs);
7366 proto.end(stToken);
7367 }
7368
7369 // Syncs (SYNC_DATA)
7370 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
7371 for (int isy = syncs.size() - 1; isy >= 0; --isy) {
7372 final Timer timer = syncs.valueAt(isy);
7373 final Timer bgTimer = timer.getSubTimer();
7374 final long syToken = proto.start(UidProto.SYNCS);
7375
7376 proto.write(UidProto.Sync.NAME, syncs.keyAt(isy));
7377 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7378 dumpTimer(proto, UidProto.Sync.TOTAL, timer, rawRealtimeUs, which);
7379 dumpTimer(proto, UidProto.Sync.BACKGROUND, bgTimer, rawRealtimeUs, which);
7380
7381 proto.end(syToken);
7382 }
7383
7384 // User activity (USER_ACTIVITY_DATA)
7385 if (u.hasUserActivity()) {
7386 for (int i = 0; i < Uid.NUM_USER_ACTIVITY_TYPES; ++i) {
7387 int val = u.getUserActivityCount(i, which);
7388 if (val != 0) {
7389 final long uaToken = proto.start(UidProto.USER_ACTIVITY);
7390 proto.write(UidProto.UserActivity.NAME, i);
7391 proto.write(UidProto.UserActivity.COUNT, val);
7392 proto.end(uaToken);
7393 }
7394 }
7395 }
7396
7397 // Vibrator (VIBRATOR_DATA)
7398 dumpTimer(proto, UidProto.VIBRATOR, u.getVibratorOnTimer(), rawRealtimeUs, which);
7399
7400 // Video (VIDEO_DATA)
7401 dumpTimer(proto, UidProto.VIDEO, u.getVideoTurnedOnTimer(), rawRealtimeUs, which);
7402
7403 // Wakelocks (WAKELOCK_DATA)
7404 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
7405 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7406 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7407 final long wToken = proto.start(UidProto.WAKELOCKS);
7408 proto.write(UidProto.Wakelock.NAME, wakelocks.keyAt(iw));
7409 dumpTimer(proto, UidProto.Wakelock.FULL, wl.getWakeTime(WAKE_TYPE_FULL),
7410 rawRealtimeUs, which);
7411 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7412 if (pTimer != null) {
7413 dumpTimer(proto, UidProto.Wakelock.PARTIAL, pTimer, rawRealtimeUs, which);
7414 dumpTimer(proto, UidProto.Wakelock.BACKGROUND_PARTIAL, pTimer.getSubTimer(),
7415 rawRealtimeUs, which);
7416 }
7417 dumpTimer(proto, UidProto.Wakelock.WINDOW, wl.getWakeTime(WAKE_TYPE_WINDOW),
7418 rawRealtimeUs, which);
7419 proto.end(wToken);
7420 }
7421
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007422 // Wifi Multicast Wakelock (WIFI_MULTICAST_WAKELOCK_DATA)
7423 dumpTimer(proto, UidProto.WIFI_MULTICAST_WAKELOCK, u.getMulticastWakelockStats(),
7424 rawRealtimeUs, which);
7425
Kweku Adams103351f2017-10-16 14:39:34 -07007426 // Wakeup alarms (WAKEUP_ALARM_DATA)
7427 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7428 final Uid.Pkg ps = packageStats.valueAt(ipkg);
7429 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
7430 for (int iwa = alarms.size() - 1; iwa >= 0; --iwa) {
7431 final long waToken = proto.start(UidProto.WAKEUP_ALARM);
7432 proto.write(UidProto.WakeupAlarm.NAME, alarms.keyAt(iwa));
7433 proto.write(UidProto.WakeupAlarm.COUNT,
7434 alarms.valueAt(iwa).getCountLocked(which));
7435 proto.end(waToken);
7436 }
7437 }
7438
7439 // Wifi Controller (WIFI_CONTROLLER_DATA)
7440 dumpControllerActivityProto(proto, UidProto.WIFI_CONTROLLER,
7441 u.getWifiControllerActivity(), which);
7442
7443 // Wifi data (WIFI_DATA)
7444 final long wToken = proto.start(UidProto.WIFI);
7445 proto.write(UidProto.Wifi.FULL_WIFI_LOCK_DURATION_MS,
7446 roundUsToMs(u.getFullWifiLockTime(rawRealtimeUs, which)));
7447 dumpTimer(proto, UidProto.Wifi.APPORTIONED_SCAN, u.getWifiScanTimer(),
7448 rawRealtimeUs, which);
7449 proto.write(UidProto.Wifi.RUNNING_DURATION_MS,
7450 roundUsToMs(u.getWifiRunningTime(rawRealtimeUs, which)));
7451 dumpTimer(proto, UidProto.Wifi.BACKGROUND_SCAN, u.getWifiScanBackgroundTimer(),
7452 rawRealtimeUs, which);
7453 proto.end(wToken);
7454
7455 proto.end(uTkn);
7456 }
7457 }
7458
7459 private void dumpProtoSystemLocked(ProtoOutputStream proto, BatteryStatsHelper helper) {
Kweku Adams87b19ec2017-10-09 12:40:03 -07007460 final long sToken = proto.start(BatteryStatsProto.SYSTEM);
7461 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7462 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7463 final long rawRealtimeUs = rawRealtimeMs * 1000;
7464 final int which = STATS_SINCE_CHARGED;
7465
7466 // Battery data (BATTERY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007467 final long bToken = proto.start(SystemProto.BATTERY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007468 proto.write(SystemProto.Battery.START_CLOCK_TIME_MS, getStartClockTime());
7469 proto.write(SystemProto.Battery.START_COUNT, getStartCount());
7470 proto.write(SystemProto.Battery.TOTAL_REALTIME_MS,
7471 computeRealtime(rawRealtimeUs, which) / 1000);
7472 proto.write(SystemProto.Battery.TOTAL_UPTIME_MS,
7473 computeUptime(rawUptimeUs, which) / 1000);
7474 proto.write(SystemProto.Battery.BATTERY_REALTIME_MS,
7475 computeBatteryRealtime(rawRealtimeUs, which) / 1000);
7476 proto.write(SystemProto.Battery.BATTERY_UPTIME_MS,
7477 computeBatteryUptime(rawUptimeUs, which) / 1000);
7478 proto.write(SystemProto.Battery.SCREEN_OFF_REALTIME_MS,
7479 computeBatteryScreenOffRealtime(rawRealtimeUs, which) / 1000);
7480 proto.write(SystemProto.Battery.SCREEN_OFF_UPTIME_MS,
7481 computeBatteryScreenOffUptime(rawUptimeUs, which) / 1000);
7482 proto.write(SystemProto.Battery.SCREEN_DOZE_DURATION_MS,
7483 getScreenDozeTime(rawRealtimeUs, which) / 1000);
7484 proto.write(SystemProto.Battery.ESTIMATED_BATTERY_CAPACITY_MAH,
7485 getEstimatedBatteryCapacity());
7486 proto.write(SystemProto.Battery.MIN_LEARNED_BATTERY_CAPACITY_UAH,
7487 getMinLearnedBatteryCapacity());
7488 proto.write(SystemProto.Battery.MAX_LEARNED_BATTERY_CAPACITY_UAH,
7489 getMaxLearnedBatteryCapacity());
Kweku Adams103351f2017-10-16 14:39:34 -07007490 proto.end(bToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007491
7492 // Battery discharge (BATTERY_DISCHARGE_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007493 final long bdToken = proto.start(SystemProto.BATTERY_DISCHARGE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007494 proto.write(SystemProto.BatteryDischarge.LOWER_BOUND_SINCE_CHARGE,
7495 getLowDischargeAmountSinceCharge());
7496 proto.write(SystemProto.BatteryDischarge.UPPER_BOUND_SINCE_CHARGE,
7497 getHighDischargeAmountSinceCharge());
7498 proto.write(SystemProto.BatteryDischarge.SCREEN_ON_SINCE_CHARGE,
7499 getDischargeAmountScreenOnSinceCharge());
7500 proto.write(SystemProto.BatteryDischarge.SCREEN_OFF_SINCE_CHARGE,
7501 getDischargeAmountScreenOffSinceCharge());
7502 proto.write(SystemProto.BatteryDischarge.SCREEN_DOZE_SINCE_CHARGE,
7503 getDischargeAmountScreenDozeSinceCharge());
7504 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH,
7505 getUahDischarge(which) / 1000);
7506 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_OFF,
7507 getUahDischargeScreenOff(which) / 1000);
7508 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_DOZE,
7509 getUahDischargeScreenDoze(which) / 1000);
Mike Ma15313c92017-11-15 17:58:21 -08007510 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_LIGHT_DOZE,
7511 getUahDischargeLightDoze(which) / 1000);
7512 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_DEEP_DOZE,
7513 getUahDischargeDeepDoze(which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007514 proto.end(bdToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007515
7516 // Time remaining
7517 long timeRemainingUs = computeChargeTimeRemaining(rawRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -07007518 // These are part of a oneof, so we should only set one of them.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007519 if (timeRemainingUs >= 0) {
7520 // Charge time remaining (CHARGE_TIME_REMAIN_DATA)
7521 proto.write(SystemProto.CHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7522 } else {
7523 timeRemainingUs = computeBatteryTimeRemaining(rawRealtimeUs);
7524 // Discharge time remaining (DISCHARGE_TIME_REMAIN_DATA)
7525 if (timeRemainingUs >= 0) {
7526 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7527 } else {
7528 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, -1);
7529 }
7530 }
7531
7532 // Charge step (CHARGE_STEP_DATA)
7533 dumpDurationSteps(proto, SystemProto.CHARGE_STEP, getChargeLevelStepTracker());
7534
7535 // Phone data connection (DATA_CONNECTION_TIME_DATA and DATA_CONNECTION_COUNT_DATA)
7536 for (int i = 0; i < NUM_DATA_CONNECTION_TYPES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007537 final long pdcToken = proto.start(SystemProto.DATA_CONNECTION);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007538 proto.write(SystemProto.DataConnection.NAME, i);
7539 dumpTimer(proto, SystemProto.DataConnection.TOTAL, getPhoneDataConnectionTimer(i),
7540 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007541 proto.end(pdcToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007542 }
7543
7544 // Discharge step (DISCHARGE_STEP_DATA)
7545 dumpDurationSteps(proto, SystemProto.DISCHARGE_STEP, getDischargeLevelStepTracker());
7546
7547 // CPU frequencies (GLOBAL_CPU_FREQ_DATA)
7548 final long[] cpuFreqs = getCpuFreqs();
7549 if (cpuFreqs != null) {
7550 for (long i : cpuFreqs) {
7551 proto.write(SystemProto.CPU_FREQUENCY, i);
7552 }
7553 }
7554
7555 // Bluetooth controller (GLOBAL_BLUETOOTH_CONTROLLER_DATA)
7556 dumpControllerActivityProto(proto, SystemProto.GLOBAL_BLUETOOTH_CONTROLLER,
7557 getBluetoothControllerActivity(), which);
7558
7559 // Modem controller (GLOBAL_MODEM_CONTROLLER_DATA)
7560 dumpControllerActivityProto(proto, SystemProto.GLOBAL_MODEM_CONTROLLER,
7561 getModemControllerActivity(), which);
7562
7563 // Global network data (GLOBAL_NETWORK_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007564 final long gnToken = proto.start(SystemProto.GLOBAL_NETWORK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007565 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_RX,
7566 getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7567 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_TX,
7568 getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7569 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_RX,
7570 getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7571 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_TX,
7572 getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7573 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_RX,
7574 getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7575 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_TX,
7576 getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7577 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_RX,
7578 getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7579 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_TX,
7580 getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7581 proto.write(SystemProto.GlobalNetwork.BT_BYTES_RX,
7582 getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7583 proto.write(SystemProto.GlobalNetwork.BT_BYTES_TX,
7584 getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
Kweku Adams103351f2017-10-16 14:39:34 -07007585 proto.end(gnToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007586
7587 // Wifi controller (GLOBAL_WIFI_CONTROLLER_DATA)
7588 dumpControllerActivityProto(proto, SystemProto.GLOBAL_WIFI_CONTROLLER,
7589 getWifiControllerActivity(), which);
7590
7591
7592 // Global wifi (GLOBAL_WIFI_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007593 final long gwToken = proto.start(SystemProto.GLOBAL_WIFI);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007594 proto.write(SystemProto.GlobalWifi.ON_DURATION_MS,
7595 getWifiOnTime(rawRealtimeUs, which) / 1000);
7596 proto.write(SystemProto.GlobalWifi.RUNNING_DURATION_MS,
7597 getGlobalWifiRunningTime(rawRealtimeUs, which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007598 proto.end(gwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007599
7600 // Kernel wakelock (KERNEL_WAKELOCK_DATA)
7601 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
7602 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007603 final long kwToken = proto.start(SystemProto.KERNEL_WAKELOCK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007604 proto.write(SystemProto.KernelWakelock.NAME, ent.getKey());
7605 dumpTimer(proto, SystemProto.KernelWakelock.TOTAL, ent.getValue(),
7606 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007607 proto.end(kwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007608 }
7609
7610 // Misc (MISC_DATA)
7611 // Calculate wakelock times across all uids.
7612 long fullWakeLockTimeTotalUs = 0;
7613 long partialWakeLockTimeTotalUs = 0;
7614
7615 final SparseArray<? extends Uid> uidStats = getUidStats();
7616 for (int iu = 0; iu < uidStats.size(); iu++) {
7617 final Uid u = uidStats.valueAt(iu);
7618
7619 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks =
7620 u.getWakelockStats();
7621 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7622 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7623
7624 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
7625 if (fullWakeTimer != null) {
7626 fullWakeLockTimeTotalUs += fullWakeTimer.getTotalTimeLocked(rawRealtimeUs,
7627 which);
7628 }
7629
7630 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7631 if (partialWakeTimer != null) {
7632 partialWakeLockTimeTotalUs += partialWakeTimer.getTotalTimeLocked(
7633 rawRealtimeUs, which);
7634 }
7635 }
7636 }
Kweku Adams103351f2017-10-16 14:39:34 -07007637 final long mToken = proto.start(SystemProto.MISC);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007638 proto.write(SystemProto.Misc.SCREEN_ON_DURATION_MS,
7639 getScreenOnTime(rawRealtimeUs, which) / 1000);
7640 proto.write(SystemProto.Misc.PHONE_ON_DURATION_MS,
7641 getPhoneOnTime(rawRealtimeUs, which) / 1000);
7642 proto.write(SystemProto.Misc.FULL_WAKELOCK_TOTAL_DURATION_MS,
7643 fullWakeLockTimeTotalUs / 1000);
7644 proto.write(SystemProto.Misc.PARTIAL_WAKELOCK_TOTAL_DURATION_MS,
7645 partialWakeLockTimeTotalUs / 1000);
7646 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_DURATION_MS,
7647 getMobileRadioActiveTime(rawRealtimeUs, which) / 1000);
7648 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_ADJUSTED_TIME_MS,
7649 getMobileRadioActiveAdjustedTime(which) / 1000);
7650 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_COUNT,
7651 getMobileRadioActiveCount(which));
7652 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_UNKNOWN_DURATION_MS,
7653 getMobileRadioActiveUnknownTime(which) / 1000);
7654 proto.write(SystemProto.Misc.INTERACTIVE_DURATION_MS,
7655 getInteractiveTime(rawRealtimeUs, which) / 1000);
7656 proto.write(SystemProto.Misc.BATTERY_SAVER_MODE_ENABLED_DURATION_MS,
7657 getPowerSaveModeEnabledTime(rawRealtimeUs, which) / 1000);
7658 proto.write(SystemProto.Misc.NUM_CONNECTIVITY_CHANGES,
7659 getNumConnectivityChange(which));
7660 proto.write(SystemProto.Misc.DEEP_DOZE_ENABLED_DURATION_MS,
7661 getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7662 proto.write(SystemProto.Misc.DEEP_DOZE_COUNT,
7663 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
7664 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_DURATION_MS,
7665 getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7666 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_COUNT,
7667 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
7668 proto.write(SystemProto.Misc.LONGEST_DEEP_DOZE_DURATION_MS,
7669 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
7670 proto.write(SystemProto.Misc.LIGHT_DOZE_ENABLED_DURATION_MS,
7671 getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7672 proto.write(SystemProto.Misc.LIGHT_DOZE_COUNT,
7673 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
7674 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_DURATION_MS,
7675 getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7676 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_COUNT,
7677 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
7678 proto.write(SystemProto.Misc.LONGEST_LIGHT_DOZE_DURATION_MS,
7679 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
Kweku Adams103351f2017-10-16 14:39:34 -07007680 proto.end(mToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007681
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007682 // Wifi multicast wakelock total stats (WIFI_MULTICAST_WAKELOCK_TOTAL_DATA)
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08007683 final long multicastWakeLockTimeTotalUs =
7684 getWifiMulticastWakelockTime(rawRealtimeUs, which);
7685 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007686 final long wmctToken = proto.start(SystemProto.WIFI_MULTICAST_WAKELOCK_TOTAL);
7687 proto.write(SystemProto.WifiMulticastWakelockTotal.DURATION_MS,
7688 multicastWakeLockTimeTotalUs / 1000);
7689 proto.write(SystemProto.WifiMulticastWakelockTotal.COUNT,
7690 multicastWakeLockCountTotal);
7691 proto.end(wmctToken);
7692
Kweku Adams87b19ec2017-10-09 12:40:03 -07007693 // Power use item (POWER_USE_ITEM_DATA)
7694 final List<BatterySipper> sippers = helper.getUsageList();
7695 if (sippers != null) {
7696 for (int i = 0; i < sippers.size(); ++i) {
7697 final BatterySipper bs = sippers.get(i);
7698 int n = SystemProto.PowerUseItem.UNKNOWN_SIPPER;
7699 int uid = 0;
7700 switch (bs.drainType) {
7701 case IDLE:
7702 n = SystemProto.PowerUseItem.IDLE;
7703 break;
7704 case CELL:
7705 n = SystemProto.PowerUseItem.CELL;
7706 break;
7707 case PHONE:
7708 n = SystemProto.PowerUseItem.PHONE;
7709 break;
7710 case WIFI:
7711 n = SystemProto.PowerUseItem.WIFI;
7712 break;
7713 case BLUETOOTH:
7714 n = SystemProto.PowerUseItem.BLUETOOTH;
7715 break;
7716 case SCREEN:
7717 n = SystemProto.PowerUseItem.SCREEN;
7718 break;
7719 case FLASHLIGHT:
7720 n = SystemProto.PowerUseItem.FLASHLIGHT;
7721 break;
7722 case APP:
Kweku Adams103351f2017-10-16 14:39:34 -07007723 // dumpProtoAppsLocked will handle this.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007724 continue;
7725 case USER:
7726 n = SystemProto.PowerUseItem.USER;
7727 uid = UserHandle.getUid(bs.userId, 0);
7728 break;
7729 case UNACCOUNTED:
7730 n = SystemProto.PowerUseItem.UNACCOUNTED;
7731 break;
7732 case OVERCOUNTED:
7733 n = SystemProto.PowerUseItem.OVERCOUNTED;
7734 break;
7735 case CAMERA:
7736 n = SystemProto.PowerUseItem.CAMERA;
7737 break;
7738 case MEMORY:
7739 n = SystemProto.PowerUseItem.MEMORY;
7740 break;
7741 }
Kweku Adams103351f2017-10-16 14:39:34 -07007742 final long puiToken = proto.start(SystemProto.POWER_USE_ITEM);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007743 proto.write(SystemProto.PowerUseItem.NAME, n);
7744 proto.write(SystemProto.PowerUseItem.UID, uid);
7745 proto.write(SystemProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7746 proto.write(SystemProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7747 proto.write(SystemProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7748 proto.write(SystemProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7749 bs.proportionalSmearMah);
Kweku Adams103351f2017-10-16 14:39:34 -07007750 proto.end(puiToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007751 }
7752 }
7753
7754 // Power use summary (POWER_USE_SUMMARY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007755 final long pusToken = proto.start(SystemProto.POWER_USE_SUMMARY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007756 proto.write(SystemProto.PowerUseSummary.BATTERY_CAPACITY_MAH,
7757 helper.getPowerProfile().getBatteryCapacity());
7758 proto.write(SystemProto.PowerUseSummary.COMPUTED_POWER_MAH, helper.getComputedPower());
7759 proto.write(SystemProto.PowerUseSummary.MIN_DRAINED_POWER_MAH, helper.getMinDrainedPower());
7760 proto.write(SystemProto.PowerUseSummary.MAX_DRAINED_POWER_MAH, helper.getMaxDrainedPower());
Kweku Adams103351f2017-10-16 14:39:34 -07007761 proto.end(pusToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007762
7763 // RPM stats (RESOURCE_POWER_MANAGER_DATA)
7764 final Map<String, ? extends Timer> rpmStats = getRpmStats();
7765 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
7766 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007767 final long rpmToken = proto.start(SystemProto.RESOURCE_POWER_MANAGER);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007768 proto.write(SystemProto.ResourcePowerManager.NAME, ent.getKey());
7769 dumpTimer(proto, SystemProto.ResourcePowerManager.TOTAL,
7770 ent.getValue(), rawRealtimeUs, which);
7771 dumpTimer(proto, SystemProto.ResourcePowerManager.SCREEN_OFF,
7772 screenOffRpmStats.get(ent.getKey()), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007773 proto.end(rpmToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007774 }
7775
7776 // Screen brightness (SCREEN_BRIGHTNESS_DATA)
7777 for (int i = 0; i < NUM_SCREEN_BRIGHTNESS_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007778 final long sbToken = proto.start(SystemProto.SCREEN_BRIGHTNESS);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007779 proto.write(SystemProto.ScreenBrightness.NAME, i);
7780 dumpTimer(proto, SystemProto.ScreenBrightness.TOTAL, getScreenBrightnessTimer(i),
7781 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007782 proto.end(sbToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007783 }
7784
7785 // Signal scanning time (SIGNAL_SCANNING_TIME_DATA)
7786 dumpTimer(proto, SystemProto.SIGNAL_SCANNING, getPhoneSignalScanningTimer(), rawRealtimeUs,
7787 which);
7788
7789 // Phone signal strength (SIGNAL_STRENGTH_TIME_DATA and SIGNAL_STRENGTH_COUNT_DATA)
7790 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007791 final long pssToken = proto.start(SystemProto.PHONE_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007792 proto.write(SystemProto.PhoneSignalStrength.NAME, i);
7793 dumpTimer(proto, SystemProto.PhoneSignalStrength.TOTAL, getPhoneSignalStrengthTimer(i),
7794 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007795 proto.end(pssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007796 }
7797
7798 // Wakeup reasons (WAKEUP_REASON_DATA)
7799 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
7800 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007801 final long wrToken = proto.start(SystemProto.WAKEUP_REASON);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007802 proto.write(SystemProto.WakeupReason.NAME, ent.getKey());
7803 dumpTimer(proto, SystemProto.WakeupReason.TOTAL, ent.getValue(), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007804 proto.end(wrToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007805 }
7806
7807 // Wifi signal strength (WIFI_SIGNAL_STRENGTH_TIME_DATA and WIFI_SIGNAL_STRENGTH_COUNT_DATA)
7808 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007809 final long wssToken = proto.start(SystemProto.WIFI_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007810 proto.write(SystemProto.WifiSignalStrength.NAME, i);
7811 dumpTimer(proto, SystemProto.WifiSignalStrength.TOTAL, getWifiSignalStrengthTimer(i),
7812 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007813 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007814 }
7815
7816 // Wifi state (WIFI_STATE_TIME_DATA and WIFI_STATE_COUNT_DATA)
7817 for (int i = 0; i < NUM_WIFI_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007818 final long wsToken = proto.start(SystemProto.WIFI_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007819 proto.write(SystemProto.WifiState.NAME, i);
7820 dumpTimer(proto, SystemProto.WifiState.TOTAL, getWifiStateTimer(i),
7821 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007822 proto.end(wsToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007823 }
7824
7825 // Wifi supplicant state (WIFI_SUPPL_STATE_TIME_DATA and WIFI_SUPPL_STATE_COUNT_DATA)
7826 for (int i = 0; i < NUM_WIFI_SUPPL_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007827 final long wssToken = proto.start(SystemProto.WIFI_SUPPLICANT_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007828 proto.write(SystemProto.WifiSupplicantState.NAME, i);
7829 dumpTimer(proto, SystemProto.WifiSupplicantState.TOTAL, getWifiSupplStateTimer(i),
7830 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007831 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007832 }
7833
7834 proto.end(sToken);
7835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007836}