blob: ab6dd7c07b42077365c707db20dcef3da7c6dc90 [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
Amith Yamasania0a30a12019-01-22 11:38:06 -080019import static android.app.ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE_LOCATION;
20
Sudheer Shankab2f83c12017-11-13 19:25:01 -080021import android.app.ActivityManager;
Dianne Hackborn94326cb2017-06-28 16:17:20 -070022import android.app.job.JobParameters;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080023import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080024import android.content.pm.ApplicationInfo;
Bookatz8bdae8d2018-01-16 11:24:30 -080025import android.server.ServerProtoEnums;
Kweku Adams71a95312018-04-16 16:54:24 -070026import android.service.batterystats.BatteryStatsServiceDumpHistoryProto;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070027import android.service.batterystats.BatteryStatsServiceDumpProto;
Wink Saville52840902011-02-18 12:40:47 -080028import android.telephony.SignalStrength;
Tej Singheee317b2018-03-07 19:28:05 -080029import android.telephony.TelephonyManager;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080030import android.text.format.DateFormat;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070031import android.util.ArrayMap;
James Carr2dd7e5e2016-07-20 18:48:39 -070032import android.util.LongSparseArray;
Dianne Hackborn9cfba352016-03-24 17:31:28 -070033import android.util.MutableBoolean;
34import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.util.Printer;
36import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070037import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070038import android.util.TimeUtils;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070039import android.util.proto.ProtoOutputStream;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070040import android.view.Display;
Amith Yamasaniab9ad192016-12-06 12:46:59 -080041
Sudheer Shankab2f83c12017-11-13 19:25:01 -080042import com.android.internal.annotations.VisibleForTesting;
Siddharth Ray78ccaf52017-12-23 16:16:21 -080043import com.android.internal.location.gnssmetrics.GnssMetrics;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080044import com.android.internal.os.BatterySipper;
45import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
Kweku Adams2f73ecd2017-09-27 16:59:19 -070047import java.io.FileDescriptor;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070048import java.io.PrintWriter;
49import java.util.ArrayList;
50import java.util.Collections;
51import java.util.Comparator;
52import java.util.Formatter;
53import java.util.HashMap;
54import java.util.List;
55import java.util.Map;
56
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057/**
58 * A class providing access to battery usage statistics, including information on
59 * wakelocks, processes, packages, and services. All times are represented in microseconds
60 * except where indicated otherwise.
61 * @hide
62 */
63public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070064 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
66 private static final boolean LOCAL_LOGV = false;
Bookatz82b341172017-09-07 19:06:08 -070067 /** Fetching RPM stats is too slow to do each time screen changes, so disable it. */
68 protected static final boolean SCREEN_OFF_RPM_STATS_ENABLED = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070069
70 /** @hide */
71 public static final String SERVICE_NAME = "batterystats";
72
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 /**
74 * A constant indicating a partial wake lock timer.
75 */
76 public static final int WAKE_TYPE_PARTIAL = 0;
77
78 /**
79 * A constant indicating a full wake lock timer.
80 */
81 public static final int WAKE_TYPE_FULL = 1;
82
83 /**
84 * A constant indicating a window wake lock timer.
85 */
86 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 /**
89 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 */
91 public static final int SENSOR = 3;
Mike Mac2f518a2017-09-19 16:06:03 -070092
The Android Open Source Project10592532009-03-18 17:39:46 -070093 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070094 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070095 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070096 public static final int WIFI_RUNNING = 4;
Mike Mac2f518a2017-09-19 16:06:03 -070097
Dianne Hackborn617f8772009-03-31 15:04:46 -070098 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070099 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -0700100 */
Dianne Hackborn617f8772009-03-31 15:04:46 -0700101 public static final int FULL_WIFI_LOCK = 5;
Mike Mac2f518a2017-09-19 16:06:03 -0700102
The Android Open Source Project10592532009-03-18 17:39:46 -0700103 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -0700104 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -0700105 */
Nick Pelly6ccaa542012-06-15 15:22:47 -0700106 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
Dianne Hackborn62793e42015-03-09 11:15:41 -0700108 /**
109 * A constant indicating a wifi multicast timer
110 */
111 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700114 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700115 */
116 public static final int VIDEO_TURNED_ON = 8;
117
118 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800119 * A constant indicating a vibrator on timer
120 */
121 public static final int VIBRATOR_ON = 9;
122
123 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700124 * A constant indicating a foreground activity timer
125 */
126 public static final int FOREGROUND_ACTIVITY = 10;
127
128 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700129 * A constant indicating a wifi batched scan is active
130 */
131 public static final int WIFI_BATCHED_SCAN = 11;
132
133 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700134 * A constant indicating a process state timer
135 */
136 public static final int PROCESS_STATE = 12;
137
138 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700139 * A constant indicating a sync timer
140 */
141 public static final int SYNC = 13;
142
143 /**
144 * A constant indicating a job timer
145 */
146 public static final int JOB = 14;
147
148 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800149 * A constant indicating an audio turn on timer
150 */
151 public static final int AUDIO_TURNED_ON = 15;
152
153 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700154 * A constant indicating a flashlight turn on timer
155 */
156 public static final int FLASHLIGHT_TURNED_ON = 16;
157
158 /**
159 * A constant indicating a camera turn on timer
160 */
161 public static final int CAMERA_TURNED_ON = 17;
162
163 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700164 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700165 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700166 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700167
168 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800169 * A constant indicating a bluetooth scan timer.
170 */
171 public static final int BLUETOOTH_SCAN_ON = 19;
172
173 /**
Bookatzc8c44962017-05-11 12:12:54 -0700174 * A constant indicating an aggregated partial wake lock timer.
175 */
176 public static final int AGGREGATED_WAKE_TYPE_PARTIAL = 20;
177
178 /**
Bookatzb1f04f32017-05-19 13:57:32 -0700179 * A constant indicating a bluetooth scan timer for unoptimized scans.
180 */
181 public static final int BLUETOOTH_UNOPTIMIZED_SCAN_ON = 21;
182
183 /**
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700184 * A constant indicating a foreground service timer
185 */
186 public static final int FOREGROUND_SERVICE = 22;
187
188 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -0800189 * A constant indicating an aggregate wifi multicast timer
190 */
191 public static final int WIFI_AGGREGATE_MULTICAST_ENABLED = 23;
192
193 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 * Include all of the data in the stats, including previously saved data.
195 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700196 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197
198 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 * Include only the current run in the stats.
200 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700201 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202
203 /**
204 * Include only the run since the last time the device was unplugged in the stats.
205 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700206 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700207
208 // NOTE: Update this list if you add/change any stats above.
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700209 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700210 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700211 private static final String[] STAT_NAMES = { "l", "c", "u" };
212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700214 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700215 *
216 * New in version 19:
217 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800218 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700219 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Bookatz506a8182017-05-01 14:18:42 -0700220 * New in version 21:
221 * - Actual (not just apportioned) Wakelock time is also recorded.
Bookatzc8c44962017-05-11 12:12:54 -0700222 * - Aggregated partial wakelock time (per uid, instead of per wakelock) is recorded.
Bookatzb1f04f32017-05-19 13:57:32 -0700223 * - BLE scan result count
224 * - CPU frequency time per uid
225 * New in version 22:
226 * - BLE scan result background count, BLE unoptimized scan time
Bookatz98d4d5c2017-08-01 19:07:54 -0700227 * - Background partial wakelock time & count
228 * New in version 23:
229 * - Logging smeared power model values
230 * New in version 24:
231 * - Fixed bugs in background timers and BLE scan time
232 * New in version 25:
233 * - Package wakeup alarms are now on screen-off timebase
Bookatz50df7112017-08-04 14:53:26 -0700234 * New in version 26:
Bookatz82b341172017-09-07 19:06:08 -0700235 * - Resource power manager (rpm) states [but screenOffRpm is disabled from working properly]
Mike Mac2f518a2017-09-19 16:06:03 -0700236 * New in version 27:
237 * - Always On Display (screen doze mode) time and power
Mike Ma15313c92017-11-15 17:58:21 -0800238 * New in version 28:
239 * - Light/Deep Doze power
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700240 * - WiFi Multicast Wakelock statistics (count & duration)
Kweku Adamsa8943cb2017-12-22 13:21:06 -0800241 * New in version 29:
242 * - Process states re-ordered. TOP_SLEEPING now below BACKGROUND. HEAVY_WEIGHT introduced.
243 * - CPU times per UID process state
zhouwenjie46712bc2018-01-11 15:21:27 -0800244 * New in version 30:
245 * - Uid.PROCESS_STATE_FOREGROUND_SERVICE only tracks
246 * ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE.
Kweku Adamsb78430e2018-02-20 18:06:09 -0800247 * New in version 31:
248 * - New cellular network types.
249 * - Deferred job metrics.
Kweku Adams6fe1a5a2018-05-22 15:02:50 -0700250 * New in version 32:
251 * - Ambient display properly output in data dump.
zhouwenjied707da92018-07-06 10:23:00 -0700252 * New in version 33:
253 * - Fixed bug in min learned capacity updating process.
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700254 */
zhouwenjied707da92018-07-06 10:23:00 -0700255 static final int CHECKIN_VERSION = 33;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700256
257 /**
258 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700260 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700261
Evan Millar22ac0432009-03-31 11:33:18 -0700262 private static final long BYTES_PER_KB = 1024;
263 private static final long BYTES_PER_MB = 1048576; // 1024^2
264 private static final long BYTES_PER_GB = 1073741824; //1024^3
Bookatz506a8182017-05-01 14:18:42 -0700265
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700266 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800267 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700268 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700270 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700271 private static final String CPU_DATA = "cpu";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700272 private static final String GLOBAL_CPU_FREQ_DATA = "gcf";
273 private static final String CPU_TIMES_AT_FREQ_DATA = "ctf";
Bookatz50df7112017-08-04 14:53:26 -0700274 // rpm line is:
275 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "rpm", state/voter name, total time, total count,
276 // screen-off time, screen-off count
277 private static final String RESOURCE_POWER_MANAGER_DATA = "rpm";
Evan Millare84de8d2009-04-02 22:16:12 -0700278 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800279 private static final String VIBRATOR_DATA = "vib";
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700280 private static final String FOREGROUND_ACTIVITY_DATA = "fg";
281 // fgs line is:
282 // BATTERY_STATS_CHECKIN_VERSION, uid, category, "fgs",
283 // foreground service time, count
284 private static final String FOREGROUND_SERVICE_DATA = "fgs";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700285 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700286 // wl line is:
287 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
Bookatz5b5ec322017-05-26 09:40:38 -0700288 // full totalTime, 'f', count, current duration, max duration, total duration,
289 // partial totalTime, 'p', count, current duration, max duration, total duration,
290 // bg partial totalTime, 'bp', count, current duration, max duration, total duration,
291 // window totalTime, 'w', count, current duration, max duration, total duration
Bookatz506a8182017-05-01 14:18:42 -0700292 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700293 private static final String WAKELOCK_DATA = "wl";
Bookatzc8c44962017-05-11 12:12:54 -0700294 // awl line is:
295 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "awl",
296 // cumulative partial wakelock duration, cumulative background partial wakelock duration
297 private static final String AGGREGATED_WAKELOCK_DATA = "awl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700298 private static final String SYNC_DATA = "sy";
299 private static final String JOB_DATA = "jb";
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700300 private static final String JOB_COMPLETION_DATA = "jbc";
Amith Yamasani977e11f2018-02-16 11:29:54 -0800301
302 /**
303 * jbd line is:
304 * BATTERY_STATS_CHECKIN_VERSION, uid, which, "jbd",
Amith Yamasani0ca706b2018-03-01 17:28:59 -0800305 * jobsDeferredEventCount, jobsDeferredCount, totalLatencyMillis,
306 * count at latency < 1 hr, count at latency 1 to 2 hrs, 2 to 4 hrs, 4 to 8 hrs, and past 8 hrs
Amith Yamasani977e11f2018-02-16 11:29:54 -0800307 * <p>
308 * @see #JOB_FRESHNESS_BUCKETS
309 */
310 private static final String JOBS_DEFERRED_DATA = "jbd";
Evan Millarc64edde2009-04-18 12:26:32 -0700311 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700312 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700313 private static final String NETWORK_DATA = "nt";
314 private static final String USER_ACTIVITY_DATA = "ua";
315 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800316 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700317 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700318 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700319 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800320 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
321 private static final String WIFI_CONTROLLER_DATA = "wfcd";
322 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
323 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700324 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700325 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800326 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800327 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
328 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800329 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700330 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700331 private static final String SCREEN_BRIGHTNESS_DATA = "br";
332 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700333 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700334 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
335 private static final String DATA_CONNECTION_TIME_DATA = "dct";
336 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800337 private static final String WIFI_STATE_TIME_DATA = "wst";
338 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700339 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
340 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
341 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
342 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800343 private static final String POWER_USE_SUMMARY_DATA = "pws";
344 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700345 private static final String DISCHARGE_STEP_DATA = "dsd";
346 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700347 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
348 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700349 private static final String FLASHLIGHT_DATA = "fla";
350 private static final String CAMERA_DATA = "cam";
351 private static final String VIDEO_DATA = "vid";
352 private static final String AUDIO_DATA = "aud";
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700353 private static final String WIFI_MULTICAST_TOTAL_DATA = "wmct";
354 private static final String WIFI_MULTICAST_DATA = "wmc";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355
Adam Lesinski010bf372016-04-11 12:18:18 -0700356 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
357
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700358 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 private final Formatter mFormatter = new Formatter(mFormatBuilder);
360
Siddharth Rayb50a6842017-12-14 15:15:28 -0800361 private static final String CELLULAR_CONTROLLER_NAME = "Cellular";
362 private static final String WIFI_CONTROLLER_NAME = "WiFi";
363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700365 * Indicates times spent by the uid at each cpu frequency in all process states.
366 *
367 * Other types might include times spent in foreground, background etc.
368 */
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800369 @VisibleForTesting
370 public static final String UID_TIMES_TYPE_ALL = "A";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700371
372 /**
Amith Yamasani977e11f2018-02-16 11:29:54 -0800373 * These are the thresholds for bucketing last time since a job was run for an app
374 * that just moved to ACTIVE due to a launch. So if the last time a job ran was less
Amith Yamasani0ca706b2018-03-01 17:28:59 -0800375 * than 1 hour ago, then it's reasonably fresh, 2 hours ago, not so fresh and so
Amith Yamasani977e11f2018-02-16 11:29:54 -0800376 * on.
377 */
378 public static final long[] JOB_FRESHNESS_BUCKETS = {
379 1 * 60 * 60 * 1000L,
380 2 * 60 * 60 * 1000L,
Amith Yamasani0ca706b2018-03-01 17:28:59 -0800381 4 * 60 * 60 * 1000L,
382 8 * 60 * 60 * 1000L,
Amith Yamasani977e11f2018-02-16 11:29:54 -0800383 Long.MAX_VALUE
384 };
385
386 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700387 * State for keeping track of counting information.
388 */
389 public static abstract class Counter {
390
391 /**
392 * Returns the count associated with this Counter for the
393 * selected type of statistics.
394 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700395 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700396 */
Evan Millarc64edde2009-04-18 12:26:32 -0700397 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700398
399 /**
400 * Temporary for debugging.
401 */
402 public abstract void logState(Printer pw, String prefix);
403 }
404
405 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700406 * State for keeping track of long counting information.
407 */
408 public static abstract class LongCounter {
409
410 /**
411 * Returns the count associated with this Counter for the
412 * selected type of statistics.
413 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700414 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700415 */
416 public abstract long getCountLocked(int which);
417
418 /**
419 * Temporary for debugging.
420 */
421 public abstract void logState(Printer pw, String prefix);
422 }
423
424 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700425 * State for keeping track of array of long counting information.
426 */
427 public static abstract class LongCounterArray {
428 /**
429 * Returns the counts associated with this Counter for the
430 * selected type of statistics.
431 *
432 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
433 */
434 public abstract long[] getCountsLocked(int which);
435
436 /**
437 * Temporary for debugging.
438 */
439 public abstract void logState(Printer pw, String prefix);
440 }
441
442 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800443 * Container class that aggregates counters for transmit, receive, and idle state of a
444 * radio controller.
445 */
446 public static abstract class ControllerActivityCounter {
447 /**
448 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
449 * idle state.
450 */
451 public abstract LongCounter getIdleTimeCounter();
452
453 /**
454 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
Siddharth Rayb50a6842017-12-14 15:15:28 -0800455 * scan state.
456 */
457 public abstract LongCounter getScanTimeCounter();
458
Siddharth Rayed754702018-02-15 12:44:37 -0800459 /**
460 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
461 * sleep state.
462 */
463 public abstract LongCounter getSleepTimeCounter();
Siddharth Rayb50a6842017-12-14 15:15:28 -0800464
465 /**
466 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800467 * receive state.
468 */
469 public abstract LongCounter getRxTimeCounter();
470
471 /**
472 * An array of {@link LongCounter}, representing various transmit levels, where each level
473 * may draw a different amount of power. The levels themselves are controller-specific.
474 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
475 * various transmit level states.
476 */
477 public abstract LongCounter[] getTxTimeCounters();
478
479 /**
480 * @return a non-null {@link LongCounter} representing the power consumed by the controller
481 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
482 * yield a value of 0 if the device doesn't support power calculations.
483 */
484 public abstract LongCounter getPowerCounter();
485 }
486
487 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 * State for keeping track of timing information.
489 */
490 public static abstract class Timer {
491
492 /**
493 * Returns the count associated with this Timer for the
494 * selected type of statistics.
495 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700496 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 */
Evan Millarc64edde2009-04-18 12:26:32 -0700498 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499
500 /**
501 * Returns the total time in microseconds associated with this Timer for the
502 * selected type of statistics.
503 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800504 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700505 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 * @return a time in microseconds
507 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800508 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700511 * Returns the total time in microseconds associated with this Timer since the
512 * 'mark' was last set.
513 *
514 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
515 * @return a time in microseconds
516 */
517 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
518
519 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700520 * Returns the max duration if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700521 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700522 */
523 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
524 return -1;
525 }
526
527 /**
528 * Returns the current time the timer has been active, if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700529 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700530 */
531 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
532 return -1;
533 }
534
535 /**
Kweku Adams103351f2017-10-16 14:39:34 -0700536 * Returns the total time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800537 *
538 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
539 * been on since reset.
540 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
541 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
542 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
543 * the actual total time.
Kweku Adams103351f2017-10-16 14:39:34 -0700544 * Not all Timer subclasses track the max, total, and current durations.
Bookatz867c0d72017-03-07 18:23:42 -0800545 */
546 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
547 return -1;
548 }
549
550 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700551 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
552 * used, for example, for tracking background usage. Secondary timers are never pooled.
553 *
554 * Not all Timer subclasses have a secondary timer; those that don't return null.
555 */
556 public Timer getSubTimer() {
557 return null;
558 }
559
560 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700561 * Returns whether the timer is currently running. Some types of timers
562 * (e.g. BatchTimers) don't know whether the event is currently active,
563 * and report false.
564 */
565 public boolean isRunningLocked() {
566 return false;
567 }
568
569 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 * Temporary for debugging.
571 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700572 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 }
574
575 /**
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800576 * Maps the ActivityManager procstate into corresponding BatteryStats procstate.
577 */
578 public static int mapToInternalProcessState(int procState) {
579 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
580 return ActivityManager.PROCESS_STATE_NONEXISTENT;
581 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
582 return Uid.PROCESS_STATE_TOP;
Amith Yamasania0a30a12019-01-22 11:38:06 -0800583 } else if (ActivityManager.isForegroundService(procState)) {
Dianne Hackborn10fc4fd2017-12-19 17:23:13 -0800584 // State when app has put itself in the foreground.
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800585 return Uid.PROCESS_STATE_FOREGROUND_SERVICE;
586 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
587 // Persistent and other foreground states go here.
588 return Uid.PROCESS_STATE_FOREGROUND;
589 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
590 return Uid.PROCESS_STATE_BACKGROUND;
591 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
592 return Uid.PROCESS_STATE_TOP_SLEEPING;
593 } else if (procState <= ActivityManager.PROCESS_STATE_HEAVY_WEIGHT) {
594 return Uid.PROCESS_STATE_HEAVY_WEIGHT;
595 } else {
596 return Uid.PROCESS_STATE_CACHED;
597 }
598 }
599
600 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 * The statistics associated with a particular uid.
602 */
603 public static abstract class Uid {
604
605 /**
606 * Returns a mapping containing wakelock statistics.
607 *
608 * @return a Map from Strings to Uid.Wakelock objects.
609 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700610 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611
612 /**
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700613 * Returns the WiFi Multicast Wakelock statistics.
614 *
615 * @return a Timer Object for the per uid Multicast statistics.
616 */
617 public abstract Timer getMulticastWakelockStats();
618
619 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700620 * Returns a mapping containing sync statistics.
621 *
622 * @return a Map from Strings to Timer objects.
623 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700624 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700625
626 /**
627 * Returns a mapping containing scheduled job statistics.
628 *
629 * @return a Map from Strings to Timer objects.
630 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700631 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700632
633 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700634 * Returns statistics about how jobs have completed.
635 *
636 * @return A Map of String job names to completion type -> count mapping.
637 */
638 public abstract ArrayMap<String, SparseIntArray> getJobCompletionStats();
639
640 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 * The statistics associated with a particular wake lock.
642 */
643 public static abstract class Wakelock {
644 public abstract Timer getWakeTime(int type);
645 }
646
647 /**
Bookatzc8c44962017-05-11 12:12:54 -0700648 * The cumulative time the uid spent holding any partial wakelocks. This will generally
649 * differ from summing over the Wakelocks in getWakelockStats since the latter may have
650 * wakelocks that overlap in time (and therefore over-counts).
651 */
652 public abstract Timer getAggregatedPartialWakelockTimer();
653
654 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 * Returns a mapping containing sensor statistics.
656 *
657 * @return a Map from Integer sensor ids to Uid.Sensor objects.
658 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700659 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660
661 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700662 * Returns a mapping containing active process data.
663 */
664 public abstract SparseArray<? extends Pid> getPidStats();
Bookatzc8c44962017-05-11 12:12:54 -0700665
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700666 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 * Returns a mapping containing process statistics.
668 *
669 * @return a Map from Strings to Uid.Proc objects.
670 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700671 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672
673 /**
674 * Returns a mapping containing package statistics.
675 *
676 * @return a Map from Strings to Uid.Pkg objects.
677 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700678 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700679
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800680 public abstract ControllerActivityCounter getWifiControllerActivity();
681 public abstract ControllerActivityCounter getBluetoothControllerActivity();
682 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800683
684 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 * {@hide}
686 */
687 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700688
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800689 public abstract void noteWifiRunningLocked(long elapsedRealtime);
690 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
691 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
692 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
693 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
694 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
695 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
696 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
697 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
698 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800699 public abstract void noteActivityResumedLocked(long elapsedRealtime);
700 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800701 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
702 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
703 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700704 public abstract int getWifiScanCount(int which);
Kweku Adams103351f2017-10-16 14:39:34 -0700705 /**
706 * Returns the timer keeping track of wifi scans.
707 */
708 public abstract Timer getWifiScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800709 public abstract int getWifiScanBackgroundCount(int which);
710 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
711 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -0700712 /**
713 * Returns the timer keeping track of background wifi scans.
714 */
715 public abstract Timer getWifiScanBackgroundTimer();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800716 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700717 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800718 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700719 public abstract Timer getAudioTurnedOnTimer();
720 public abstract Timer getVideoTurnedOnTimer();
721 public abstract Timer getFlashlightTurnedOnTimer();
722 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700723 public abstract Timer getForegroundActivityTimer();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700724
725 /**
726 * Returns the timer keeping track of Foreground Service time
727 */
728 public abstract Timer getForegroundServiceTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800729 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800730 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatzb1f04f32017-05-19 13:57:32 -0700731 public abstract Timer getBluetoothUnoptimizedScanTimer();
732 public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700733 public abstract Counter getBluetoothScanResultCounter();
Bookatzb1f04f32017-05-19 13:57:32 -0700734 public abstract Counter getBluetoothScanResultBgCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700735
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700736 public abstract long[] getCpuFreqTimes(int which);
737 public abstract long[] getScreenOffCpuFreqTimes(int which);
Mike Ma3d422c32017-10-25 11:08:57 -0700738 /**
739 * Returns cpu active time of an uid.
740 */
741 public abstract long getCpuActiveTime();
742 /**
743 * Returns cpu times of an uid on each cluster
744 */
745 public abstract long[] getCpuClusterTimes();
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700746
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800747 /**
748 * Returns cpu times of an uid at a particular process state.
749 */
750 public abstract long[] getCpuFreqTimes(int which, int procState);
751 /**
752 * Returns cpu times of an uid while the screen if off at a particular process state.
753 */
754 public abstract long[] getScreenOffCpuFreqTimes(int which, int procState);
755
Dianne Hackborna0200e32016-03-30 18:01:41 -0700756 // Note: the following times are disjoint. They can be added together to find the
757 // total time a uid has had any processes running at all.
758
759 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800760 * Time this uid has any processes in the top state.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700761 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800762 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700763 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800764 * Time this uid has any process with a started foreground service, but
Dianne Hackborna0200e32016-03-30 18:01:41 -0700765 * none in the "top" state.
766 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800767 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700768 /**
Dianne Hackborna0200e32016-03-30 18:01:41 -0700769 * Time this uid has any process in an active foreground state, but none in the
zhouwenjie46712bc2018-01-11 15:21:27 -0800770 * "foreground service" or better state. Persistent and other foreground states go here.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700771 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800772 public static final int PROCESS_STATE_FOREGROUND = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700773 /**
774 * Time this uid has any process in an active background state, but none in the
775 * "foreground" or better state.
776 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800777 public static final int PROCESS_STATE_BACKGROUND = 3;
778 /**
779 * Time this uid has any process that is top while the device is sleeping, but not
780 * active for any other reason. We kind-of consider it a kind of cached process
781 * for execution restrictions.
782 */
783 public static final int PROCESS_STATE_TOP_SLEEPING = 4;
784 /**
785 * Time this uid has any process that is in the background but it has an activity
786 * marked as "can't save state". This is essentially a cached process, though the
787 * system will try much harder than normal to avoid killing it.
788 */
789 public static final int PROCESS_STATE_HEAVY_WEIGHT = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700790 /**
791 * Time this uid has any processes that are sitting around cached, not in one of the
792 * other active states.
793 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800794 public static final int PROCESS_STATE_CACHED = 6;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700795 /**
796 * Total number of process states we track.
797 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800798 public static final int NUM_PROCESS_STATE = 7;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700799
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800800 // Used in dump
Dianne Hackborn61659e52014-07-09 16:13:01 -0700801 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800802 "Top", "Fg Service", "Foreground", "Background", "Top Sleeping", "Heavy Weight",
803 "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700804 };
805
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800806 // Used in checkin dump
807 @VisibleForTesting
808 public static final String[] UID_PROCESS_TYPES = {
809 "T", // TOP
810 "FS", // FOREGROUND_SERVICE
811 "F", // FOREGROUND
812 "B", // BACKGROUND
813 "TS", // TOP_SLEEPING
814 "HW", // HEAVY_WEIGHT
815 "C" // CACHED
816 };
817
818 /**
819 * When the process exits one of these states, we need to make sure cpu time in this state
820 * is not attributed to any non-critical process states.
821 */
822 public static final int[] CRITICAL_PROC_STATES = {
Amith Yamasania0a30a12019-01-22 11:38:06 -0800823 PROCESS_STATE_TOP,
824 PROCESS_STATE_FOREGROUND_SERVICE_LOCATION, PROCESS_STATE_FOREGROUND_SERVICE,
825 PROCESS_STATE_FOREGROUND
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800826 };
827
Dianne Hackborn61659e52014-07-09 16:13:01 -0700828 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800829 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700830
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800831 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832
Robert Greenwalta029ea12013-09-25 16:38:12 -0700833 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
834
Dianne Hackborn617f8772009-03-31 15:04:46 -0700835 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700836 * Note that these must match the constants in android.os.PowerManager.
837 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
838 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700839 */
840 static final String[] USER_ACTIVITY_TYPES = {
Lucas Dupin0a5d7972019-01-16 18:52:30 -0800841 "other", "button", "touch", "accessibility", "attention"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700842 };
Bookatzc8c44962017-05-11 12:12:54 -0700843
Lucas Dupin0a5d7972019-01-16 18:52:30 -0800844 public static final int NUM_USER_ACTIVITY_TYPES = USER_ACTIVITY_TYPES.length;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700845
Dianne Hackborn617f8772009-03-31 15:04:46 -0700846 public abstract void noteUserActivityLocked(int type);
847 public abstract boolean hasUserActivity();
848 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700849
850 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800851 public abstract long getNetworkActivityBytes(int type, int which);
852 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800853 public abstract long getMobileRadioActiveTime(int which);
854 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700855
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700856 /**
857 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
858 */
859 public abstract long getUserCpuTimeUs(int which);
860
861 /**
862 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
863 */
864 public abstract long getSystemCpuTimeUs(int which);
865
866 /**
Sudheer Shanka71f34b32017-07-21 00:14:24 -0700867 * Returns the approximate cpu time (in microseconds) spent at a certain CPU speed for a
Adam Lesinski6832f392015-09-05 18:05:40 -0700868 * given CPU cluster.
869 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700870 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700871 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700872 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
873 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700874 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700875 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700876
Adam Lesinski5f056f62016-07-14 16:56:08 -0700877 /**
878 * Returns the number of times this UID woke up the Application Processor to
879 * process a mobile radio packet.
880 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
881 */
882 public abstract long getMobileRadioApWakeupCount(int which);
883
884 /**
885 * Returns the number of times this UID woke up the Application Processor to
886 * process a WiFi packet.
887 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
888 */
889 public abstract long getWifiRadioApWakeupCount(int which);
890
Amith Yamasani977e11f2018-02-16 11:29:54 -0800891 /**
892 * Appends the deferred jobs data to the StringBuilder passed in, in checkin format
893 * @param sb StringBuilder that can be overwritten with the deferred jobs data
894 * @param which one of STATS_*
895 */
896 public abstract void getDeferredJobsCheckinLineLocked(StringBuilder sb, int which);
897
898 /**
899 * Appends the deferred jobs data to the StringBuilder passed in
900 * @param sb StringBuilder that can be overwritten with the deferred jobs data
901 * @param which one of STATS_*
902 */
903 public abstract void getDeferredJobsLineLocked(StringBuilder sb, int which);
904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800906 /*
907 * FIXME: it's not correct to use this magic value because it
908 * could clash with a sensor handle (which are defined by
909 * the sensor HAL, and therefore out of our control
910 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 // Magic sensor number for the GPS.
912 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800917
Bookatz867c0d72017-03-07 18:23:42 -0800918 /** Returns a Timer for sensor usage when app is in the background. */
919 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 }
921
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700922 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800923 public int mWakeNesting;
924 public long mWakeSumMs;
925 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700926 }
927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 /**
929 * The statistics associated with a particular process.
930 */
931 public static abstract class Proc {
932
Dianne Hackborn287952c2010-09-22 22:34:31 -0700933 public static class ExcessivePower {
934 public static final int TYPE_WAKE = 1;
935 public static final int TYPE_CPU = 2;
936
937 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700938 public long overTime;
939 public long usedTime;
940 }
941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800943 * Returns true if this process is still active in the battery stats.
944 */
945 public abstract boolean isActive();
946
947 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700948 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700950 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 */
952 public abstract long getUserTime(int which);
953
954 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700955 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700957 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 */
959 public abstract long getSystemTime(int which);
960
961 /**
962 * Returns the number of times the process has been started.
963 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700964 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 */
966 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700967
968 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800969 * Returns the number of times the process has crashed.
970 *
971 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
972 */
973 public abstract int getNumCrashes(int which);
974
975 /**
976 * Returns the number of times the process has ANRed.
977 *
978 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
979 */
980 public abstract int getNumAnrs(int which);
981
982 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700983 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700984 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700985 * @return foreground cpu time in microseconds
986 */
987 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700988
Dianne Hackborn287952c2010-09-22 22:34:31 -0700989 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700990
Dianne Hackborn287952c2010-09-22 22:34:31 -0700991 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 }
993
994 /**
995 * The statistics associated with a particular package.
996 */
997 public static abstract class Pkg {
998
999 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001000 * Returns information about all wakeup alarms that have been triggered for this
1001 * package. The mapping keys are tag names for the alarms, the counter contains
1002 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001004 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005
1006 /**
1007 * Returns a mapping containing service statistics.
1008 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001009 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010
1011 /**
1012 * The statistics associated with a particular service.
1013 */
Joe Onoratoabded112016-02-08 16:49:39 -08001014 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015
1016 /**
1017 * Returns the amount of time spent started.
1018 *
1019 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001020 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 * @return
1022 */
1023 public abstract long getStartTime(long batteryUptime, int which);
1024
1025 /**
1026 * Returns the total number of times startService() has been called.
1027 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001028 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 */
1030 public abstract int getStarts(int which);
1031
1032 /**
1033 * Returns the total number times the service has been launched.
1034 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001035 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 */
1037 public abstract int getLaunches(int which);
1038 }
1039 }
1040 }
1041
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001042 public static final class LevelStepTracker {
1043 public long mLastStepTime = -1;
1044 public int mNumStepDurations;
1045 public final long[] mStepDurations;
1046
1047 public LevelStepTracker(int maxLevelSteps) {
1048 mStepDurations = new long[maxLevelSteps];
1049 }
1050
1051 public LevelStepTracker(int numSteps, long[] steps) {
1052 mNumStepDurations = numSteps;
1053 mStepDurations = new long[numSteps];
1054 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
1055 }
1056
1057 public long getDurationAt(int index) {
1058 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
1059 }
1060
1061 public int getLevelAt(int index) {
1062 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
1063 >> STEP_LEVEL_LEVEL_SHIFT);
1064 }
1065
1066 public int getInitModeAt(int index) {
1067 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
1068 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1069 }
1070
1071 public int getModModeAt(int index) {
1072 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
1073 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1074 }
1075
1076 private void appendHex(long val, int topOffset, StringBuilder out) {
1077 boolean hasData = false;
1078 while (topOffset >= 0) {
1079 int digit = (int)( (val>>topOffset) & 0xf );
1080 topOffset -= 4;
1081 if (!hasData && digit == 0) {
1082 continue;
1083 }
1084 hasData = true;
1085 if (digit >= 0 && digit <= 9) {
1086 out.append((char)('0' + digit));
1087 } else {
1088 out.append((char)('a' + digit - 10));
1089 }
1090 }
1091 }
1092
1093 public void encodeEntryAt(int index, StringBuilder out) {
1094 long item = mStepDurations[index];
1095 long duration = item & STEP_LEVEL_TIME_MASK;
1096 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
1097 >> STEP_LEVEL_LEVEL_SHIFT);
1098 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
1099 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1100 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
1101 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1102 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1103 case Display.STATE_OFF: out.append('f'); break;
1104 case Display.STATE_ON: out.append('o'); break;
1105 case Display.STATE_DOZE: out.append('d'); break;
1106 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
1107 }
1108 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1109 out.append('p');
1110 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001111 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1112 out.append('i');
1113 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001114 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1115 case Display.STATE_OFF: out.append('F'); break;
1116 case Display.STATE_ON: out.append('O'); break;
1117 case Display.STATE_DOZE: out.append('D'); break;
1118 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
1119 }
1120 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1121 out.append('P');
1122 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001123 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1124 out.append('I');
1125 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001126 out.append('-');
1127 appendHex(level, 4, out);
1128 out.append('-');
1129 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
1130 }
1131
1132 public void decodeEntryAt(int index, String value) {
1133 final int N = value.length();
1134 int i = 0;
1135 char c;
1136 long out = 0;
1137 while (i < N && (c=value.charAt(i)) != '-') {
1138 i++;
1139 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001140 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001141 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001142 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001143 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001144 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001145 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001146 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1147 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1148 break;
1149 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1150 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1151 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001152 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1153 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1154 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001155 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1156 break;
1157 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1158 break;
1159 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1160 break;
1161 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1162 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1163 break;
1164 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1165 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001166 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001167 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1168 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1169 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001170 }
1171 }
1172 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001173 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001174 while (i < N && (c=value.charAt(i)) != '-') {
1175 i++;
1176 level <<= 4;
1177 if (c >= '0' && c <= '9') {
1178 level += c - '0';
1179 } else if (c >= 'a' && c <= 'f') {
1180 level += c - 'a' + 10;
1181 } else if (c >= 'A' && c <= 'F') {
1182 level += c - 'A' + 10;
1183 }
1184 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001185 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001186 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
1187 long duration = 0;
1188 while (i < N && (c=value.charAt(i)) != '-') {
1189 i++;
1190 duration <<= 4;
1191 if (c >= '0' && c <= '9') {
1192 duration += c - '0';
1193 } else if (c >= 'a' && c <= 'f') {
1194 duration += c - 'a' + 10;
1195 } else if (c >= 'A' && c <= 'F') {
1196 duration += c - 'A' + 10;
1197 }
1198 }
1199 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
1200 }
1201
1202 public void init() {
1203 mLastStepTime = -1;
1204 mNumStepDurations = 0;
1205 }
1206
1207 public void clearTime() {
1208 mLastStepTime = -1;
1209 }
1210
1211 public long computeTimePerLevel() {
1212 final long[] steps = mStepDurations;
1213 final int numSteps = mNumStepDurations;
1214
1215 // For now we'll do a simple average across all steps.
1216 if (numSteps <= 0) {
1217 return -1;
1218 }
1219 long total = 0;
1220 for (int i=0; i<numSteps; i++) {
1221 total += steps[i] & STEP_LEVEL_TIME_MASK;
1222 }
1223 return total / numSteps;
1224 /*
1225 long[] buckets = new long[numSteps];
1226 int numBuckets = 0;
1227 int numToAverage = 4;
1228 int i = 0;
1229 while (i < numSteps) {
1230 long totalTime = 0;
1231 int num = 0;
1232 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1233 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1234 num++;
1235 }
1236 buckets[numBuckets] = totalTime / num;
1237 numBuckets++;
1238 numToAverage *= 2;
1239 i += num;
1240 }
1241 if (numBuckets < 1) {
1242 return -1;
1243 }
1244 long averageTime = buckets[numBuckets-1];
1245 for (i=numBuckets-2; i>=0; i--) {
1246 averageTime = (averageTime + buckets[i]) / 2;
1247 }
1248 return averageTime;
1249 */
1250 }
1251
1252 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1253 int[] outNumOfInterest) {
1254 final long[] steps = mStepDurations;
1255 final int count = mNumStepDurations;
1256 if (count <= 0) {
1257 return -1;
1258 }
1259 long total = 0;
1260 int numOfInterest = 0;
1261 for (int i=0; i<count; i++) {
1262 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1263 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1264 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1265 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1266 // If the modes of interest didn't change during this step period...
1267 if ((modMode&modesOfInterest) == 0) {
1268 // And the mode values during this period match those we are measuring...
1269 if ((initMode&modesOfInterest) == modeValues) {
1270 // Then this can be used to estimate the total time!
1271 numOfInterest++;
1272 total += steps[i] & STEP_LEVEL_TIME_MASK;
1273 }
1274 }
1275 }
1276 if (numOfInterest <= 0) {
1277 return -1;
1278 }
1279
1280 if (outNumOfInterest != null) {
1281 outNumOfInterest[0] = numOfInterest;
1282 }
1283
1284 // The estimated time is the average time we spend in each level, multipled
1285 // by 100 -- the total number of battery levels
1286 return (total / numOfInterest) * 100;
1287 }
1288
1289 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1290 int stepCount = mNumStepDurations;
1291 final long lastStepTime = mLastStepTime;
1292 if (lastStepTime >= 0 && numStepLevels > 0) {
1293 final long[] steps = mStepDurations;
1294 long duration = elapsedRealtime - lastStepTime;
1295 for (int i=0; i<numStepLevels; i++) {
1296 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1297 long thisDuration = duration / (numStepLevels-i);
1298 duration -= thisDuration;
1299 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1300 thisDuration = STEP_LEVEL_TIME_MASK;
1301 }
1302 steps[0] = thisDuration | modeBits;
1303 }
1304 stepCount += numStepLevels;
1305 if (stepCount > steps.length) {
1306 stepCount = steps.length;
1307 }
1308 }
1309 mNumStepDurations = stepCount;
1310 mLastStepTime = elapsedRealtime;
1311 }
1312
1313 public void readFromParcel(Parcel in) {
1314 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001315 if (N > mStepDurations.length) {
1316 throw new ParcelFormatException("more step durations than available: " + N);
1317 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001318 mNumStepDurations = N;
1319 for (int i=0; i<N; i++) {
1320 mStepDurations[i] = in.readLong();
1321 }
1322 }
1323
1324 public void writeToParcel(Parcel out) {
1325 final int N = mNumStepDurations;
1326 out.writeInt(N);
1327 for (int i=0; i<N; i++) {
1328 out.writeLong(mStepDurations[i]);
1329 }
1330 }
1331 }
1332
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001333 public static final class PackageChange {
1334 public String mPackageName;
1335 public boolean mUpdate;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001336 public long mVersionCode;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001337 }
1338
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001339 public static final class DailyItem {
1340 public long mStartTime;
1341 public long mEndTime;
1342 public LevelStepTracker mDischargeSteps;
1343 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001344 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001345 }
1346
1347 public abstract DailyItem getDailyItemLocked(int daysAgo);
1348
1349 public abstract long getCurrentDailyStartTime();
1350
1351 public abstract long getNextMinDailyDeadline();
1352
1353 public abstract long getNextMaxDailyDeadline();
1354
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001355 public abstract long[] getCpuFreqs();
1356
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001357 public final static class HistoryTag {
1358 public String string;
1359 public int uid;
1360
1361 public int poolIdx;
1362
1363 public void setTo(HistoryTag o) {
1364 string = o.string;
1365 uid = o.uid;
1366 poolIdx = o.poolIdx;
1367 }
1368
1369 public void setTo(String _string, int _uid) {
1370 string = _string;
1371 uid = _uid;
1372 poolIdx = -1;
1373 }
1374
1375 public void writeToParcel(Parcel dest, int flags) {
1376 dest.writeString(string);
1377 dest.writeInt(uid);
1378 }
1379
1380 public void readFromParcel(Parcel src) {
1381 string = src.readString();
1382 uid = src.readInt();
1383 poolIdx = -1;
1384 }
1385
1386 @Override
1387 public boolean equals(Object o) {
1388 if (this == o) return true;
1389 if (o == null || getClass() != o.getClass()) return false;
1390
1391 HistoryTag that = (HistoryTag) o;
1392
1393 if (uid != that.uid) return false;
1394 if (!string.equals(that.string)) return false;
1395
1396 return true;
1397 }
1398
1399 @Override
1400 public int hashCode() {
1401 int result = string.hashCode();
1402 result = 31 * result + uid;
1403 return result;
1404 }
1405 }
1406
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001407 /**
1408 * Optional detailed information that can go into a history step. This is typically
1409 * generated each time the battery level changes.
1410 */
1411 public final static class HistoryStepDetails {
1412 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1413 public int userTime;
1414 public int systemTime;
1415
1416 // Top three apps using CPU in the last step, with times in 1/100 second.
1417 public int appCpuUid1;
1418 public int appCpuUTime1;
1419 public int appCpuSTime1;
1420 public int appCpuUid2;
1421 public int appCpuUTime2;
1422 public int appCpuSTime2;
1423 public int appCpuUid3;
1424 public int appCpuUTime3;
1425 public int appCpuSTime3;
1426
1427 // Information from /proc/stat
1428 public int statUserTime;
1429 public int statSystemTime;
1430 public int statIOWaitTime;
1431 public int statIrqTime;
1432 public int statSoftIrqTime;
1433 public int statIdlTime;
1434
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001435 // Platform-level low power state stats
1436 public String statPlatformIdleState;
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001437 public String statSubsystemPowerState;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001438
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001439 public HistoryStepDetails() {
1440 clear();
1441 }
1442
1443 public void clear() {
1444 userTime = systemTime = 0;
1445 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1446 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1447 = appCpuUTime3 = appCpuSTime3 = 0;
1448 }
1449
1450 public void writeToParcel(Parcel out) {
1451 out.writeInt(userTime);
1452 out.writeInt(systemTime);
1453 out.writeInt(appCpuUid1);
1454 out.writeInt(appCpuUTime1);
1455 out.writeInt(appCpuSTime1);
1456 out.writeInt(appCpuUid2);
1457 out.writeInt(appCpuUTime2);
1458 out.writeInt(appCpuSTime2);
1459 out.writeInt(appCpuUid3);
1460 out.writeInt(appCpuUTime3);
1461 out.writeInt(appCpuSTime3);
1462 out.writeInt(statUserTime);
1463 out.writeInt(statSystemTime);
1464 out.writeInt(statIOWaitTime);
1465 out.writeInt(statIrqTime);
1466 out.writeInt(statSoftIrqTime);
1467 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001468 out.writeString(statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001469 out.writeString(statSubsystemPowerState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001470 }
1471
1472 public void readFromParcel(Parcel in) {
1473 userTime = in.readInt();
1474 systemTime = in.readInt();
1475 appCpuUid1 = in.readInt();
1476 appCpuUTime1 = in.readInt();
1477 appCpuSTime1 = in.readInt();
1478 appCpuUid2 = in.readInt();
1479 appCpuUTime2 = in.readInt();
1480 appCpuSTime2 = in.readInt();
1481 appCpuUid3 = in.readInt();
1482 appCpuUTime3 = in.readInt();
1483 appCpuSTime3 = in.readInt();
1484 statUserTime = in.readInt();
1485 statSystemTime = in.readInt();
1486 statIOWaitTime = in.readInt();
1487 statIrqTime = in.readInt();
1488 statSoftIrqTime = in.readInt();
1489 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001490 statPlatformIdleState = in.readString();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001491 statSubsystemPowerState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001492 }
1493 }
1494
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001495 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001496 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001497
1498 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001499 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001500
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001501 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001502 public static final byte CMD_NULL = -1;
1503 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001504 public static final byte CMD_CURRENT_TIME = 5;
1505 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001506 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001507 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001508
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001509 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001510
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001511 /**
1512 * Return whether the command code is a delta data update.
1513 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001514 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001515 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001516 }
1517
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001518 public byte batteryLevel;
1519 public byte batteryStatus;
1520 public byte batteryHealth;
1521 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001522
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001523 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001524 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001525
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001526 // The charge of the battery in micro-Ampere-hours.
1527 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001528
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001529 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001530 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001531 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001532 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001533 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1534 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001535 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001536 public static final int STATE_PHONE_STATE_SHIFT = 6;
1537 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001538 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001539 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001540 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001541
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001542 // These states always appear directly in the first int token
1543 // of a delta change; they should be ones that change relatively
1544 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001545 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1546 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001547 public static final int STATE_GPS_ON_FLAG = 1<<29;
1548 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001549 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001550 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001551 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001552 // Do not use, this is used for coulomb delta count.
1553 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001554 // These are on the lower bits used for the command; if they change
1555 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001556 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001557 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001558 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001559 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1560 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
Mike Mac2f518a2017-09-19 16:06:03 -07001561 public static final int STATE_SCREEN_DOZE_FLAG = 1 << 18;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001562 // empty slot
1563 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001564
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001565 public static final int MOST_INTERESTING_STATES =
Mike Mac2f518a2017-09-19 16:06:03 -07001566 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG | STATE_SCREEN_DOZE_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001567
1568 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001569
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001570 public int states;
1571
Dianne Hackborn3251b902014-06-20 14:40:53 -07001572 // Constants from WIFI_SUPPL_STATE_*
1573 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1574 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1575 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1576 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1577 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1578 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
Siddharth Ray78ccaf52017-12-23 16:16:21 -08001579 // Values for NUM_GPS_SIGNAL_QUALITY_LEVELS
1580 public static final int STATE2_GPS_SIGNAL_QUALITY_SHIFT = 7;
1581 public static final int STATE2_GPS_SIGNAL_QUALITY_MASK =
1582 0x1 << STATE2_GPS_SIGNAL_QUALITY_SHIFT;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001583
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001584 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001585 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1586 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1587 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001588 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001589 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1590 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1591 public static final int STATE2_CHARGING_FLAG = 1<<24;
1592 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1593 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1594 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001595 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Siddharth Rayf5e796a2018-01-22 18:18:17 -08001596 public static final int STATE2_CELLULAR_HIGH_TX_POWER_FLAG = 1 << 19;
Mike Ma926a97c2018-03-25 02:32:35 -07001597 public static final int STATE2_USB_DATA_LINK_FLAG = 1 << 18;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001598
1599 public static final int MOST_INTERESTING_STATES2 =
Mike Mac2f518a2017-09-19 16:06:03 -07001600 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
1601 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001602
1603 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001604
Dianne Hackborn40c87252014-03-19 16:55:40 -07001605 public int states2;
1606
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001607 // The wake lock that was acquired at this point.
1608 public HistoryTag wakelockTag;
1609
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001610 // Kernel wakeup reason at this point.
1611 public HistoryTag wakeReasonTag;
1612
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001613 // Non-null when there is more detailed information at this step.
1614 public HistoryStepDetails stepDetails;
1615
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001616 public static final int EVENT_FLAG_START = 0x8000;
1617 public static final int EVENT_FLAG_FINISH = 0x4000;
1618
1619 // No event in this item.
1620 public static final int EVENT_NONE = 0x0000;
1621 // Event is about a process that is running.
1622 public static final int EVENT_PROC = 0x0001;
1623 // Event is about an application package that is in the foreground.
1624 public static final int EVENT_FOREGROUND = 0x0002;
1625 // Event is about an application package that is at the top of the screen.
1626 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001627 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001628 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001629 // Events for all additional wake locks aquired/release within a wake block.
1630 // These are not generated by default.
1631 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001632 // Event is about an application executing a scheduled job.
1633 public static final int EVENT_JOB = 0x0006;
1634 // Events for users running.
1635 public static final int EVENT_USER_RUNNING = 0x0007;
1636 // Events for foreground user.
1637 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001638 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001639 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001640 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001641 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001642 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001643 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001644 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001645 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001646 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001647 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001648 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001649 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001650 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001651 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001652 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001653 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001654 // Event for a package being on the temporary whitelist.
1655 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001656 // Event for the screen waking up.
1657 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001658 // Event for the UID that woke up the application processor.
1659 // Used for wakeups coming from WiFi, modem, etc.
1660 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001661 // Event for reporting that a specific partial wake lock has been held for a long duration.
1662 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001663
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001664 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001665 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001666 // Mask to extract out only the type part of the event.
1667 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001668
1669 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1670 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1671 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1672 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1673 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1674 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001675 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1676 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001677 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1678 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001679 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1680 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1681 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1682 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1683 public static final int EVENT_USER_FOREGROUND_START =
1684 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1685 public static final int EVENT_USER_FOREGROUND_FINISH =
1686 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001687 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1688 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001689 public static final int EVENT_TEMP_WHITELIST_START =
1690 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1691 public static final int EVENT_TEMP_WHITELIST_FINISH =
1692 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001693 public static final int EVENT_LONG_WAKE_LOCK_START =
1694 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1695 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1696 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001697
1698 // For CMD_EVENT.
1699 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001700 public HistoryTag eventTag;
1701
Dianne Hackborn9a755432014-05-15 17:05:22 -07001702 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001703 public long currentTime;
1704
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001705 // Meta-data when reading.
1706 public int numReadInts;
1707
1708 // Pre-allocated objects.
1709 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001710 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001711 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001712
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001713 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001714 }
Bookatzc8c44962017-05-11 12:12:54 -07001715
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001716 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001717 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001718 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001719 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001720 }
Bookatzc8c44962017-05-11 12:12:54 -07001721
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001722 public int describeContents() {
1723 return 0;
1724 }
1725
1726 public void writeToParcel(Parcel dest, int flags) {
1727 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001728 int bat = (((int)cmd)&0xff)
1729 | ((((int)batteryLevel)<<8)&0xff00)
1730 | ((((int)batteryStatus)<<16)&0xf0000)
1731 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001732 | ((((int)batteryPlugType)<<24)&0xf000000)
1733 | (wakelockTag != null ? 0x10000000 : 0)
1734 | (wakeReasonTag != null ? 0x20000000 : 0)
1735 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001736 dest.writeInt(bat);
1737 bat = (((int)batteryTemperature)&0xffff)
1738 | ((((int)batteryVoltage)<<16)&0xffff0000);
1739 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001740 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001741 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001742 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001743 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001744 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001745 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001746 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001747 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001748 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001749 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001750 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001751 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001752 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001753 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001754 dest.writeLong(currentTime);
1755 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001756 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001757
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001758 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001759 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001760 int bat = src.readInt();
1761 cmd = (byte)(bat&0xff);
1762 batteryLevel = (byte)((bat>>8)&0xff);
1763 batteryStatus = (byte)((bat>>16)&0xf);
1764 batteryHealth = (byte)((bat>>20)&0xf);
1765 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001766 int bat2 = src.readInt();
1767 batteryTemperature = (short)(bat2&0xffff);
1768 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001769 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001770 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001771 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001772 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001773 wakelockTag = localWakelockTag;
1774 wakelockTag.readFromParcel(src);
1775 } else {
1776 wakelockTag = null;
1777 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001778 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001779 wakeReasonTag = localWakeReasonTag;
1780 wakeReasonTag.readFromParcel(src);
1781 } else {
1782 wakeReasonTag = null;
1783 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001784 if ((bat&0x40000000) != 0) {
1785 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001786 eventTag = localEventTag;
1787 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001788 } else {
1789 eventCode = EVENT_NONE;
1790 eventTag = null;
1791 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001792 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001793 currentTime = src.readLong();
1794 } else {
1795 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001796 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001797 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001798 }
1799
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001800 public void clear() {
1801 time = 0;
1802 cmd = CMD_NULL;
1803 batteryLevel = 0;
1804 batteryStatus = 0;
1805 batteryHealth = 0;
1806 batteryPlugType = 0;
1807 batteryTemperature = 0;
1808 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001809 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001810 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001811 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001812 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001813 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001814 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001815 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001816 }
Bookatzc8c44962017-05-11 12:12:54 -07001817
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001818 public void setTo(HistoryItem o) {
1819 time = o.time;
1820 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001821 setToCommon(o);
1822 }
1823
1824 public void setTo(long time, byte cmd, HistoryItem o) {
1825 this.time = time;
1826 this.cmd = cmd;
1827 setToCommon(o);
1828 }
1829
1830 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001831 batteryLevel = o.batteryLevel;
1832 batteryStatus = o.batteryStatus;
1833 batteryHealth = o.batteryHealth;
1834 batteryPlugType = o.batteryPlugType;
1835 batteryTemperature = o.batteryTemperature;
1836 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001837 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001838 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001839 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001840 if (o.wakelockTag != null) {
1841 wakelockTag = localWakelockTag;
1842 wakelockTag.setTo(o.wakelockTag);
1843 } else {
1844 wakelockTag = null;
1845 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001846 if (o.wakeReasonTag != null) {
1847 wakeReasonTag = localWakeReasonTag;
1848 wakeReasonTag.setTo(o.wakeReasonTag);
1849 } else {
1850 wakeReasonTag = null;
1851 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001852 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001853 if (o.eventTag != null) {
1854 eventTag = localEventTag;
1855 eventTag.setTo(o.eventTag);
1856 } else {
1857 eventTag = null;
1858 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001859 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001860 }
1861
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001862 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001863 return batteryLevel == o.batteryLevel
1864 && batteryStatus == o.batteryStatus
1865 && batteryHealth == o.batteryHealth
1866 && batteryPlugType == o.batteryPlugType
1867 && batteryTemperature == o.batteryTemperature
1868 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001869 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001870 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001871 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001872 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001873 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001874
1875 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001876 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001877 return false;
1878 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001879 if (wakelockTag != o.wakelockTag) {
1880 if (wakelockTag == null || o.wakelockTag == null) {
1881 return false;
1882 }
1883 if (!wakelockTag.equals(o.wakelockTag)) {
1884 return false;
1885 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001886 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001887 if (wakeReasonTag != o.wakeReasonTag) {
1888 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1889 return false;
1890 }
1891 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1892 return false;
1893 }
1894 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001895 if (eventTag != o.eventTag) {
1896 if (eventTag == null || o.eventTag == null) {
1897 return false;
1898 }
1899 if (!eventTag.equals(o.eventTag)) {
1900 return false;
1901 }
1902 }
1903 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001904 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001905 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001906
1907 public final static class HistoryEventTracker {
1908 private final HashMap<String, SparseIntArray>[] mActiveEvents
1909 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1910
1911 public boolean updateState(int code, String name, int uid, int poolIdx) {
1912 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1913 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1914 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1915 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001916 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001917 mActiveEvents[idx] = active;
1918 }
1919 SparseIntArray uids = active.get(name);
1920 if (uids == null) {
1921 uids = new SparseIntArray();
1922 active.put(name, uids);
1923 }
1924 if (uids.indexOfKey(uid) >= 0) {
1925 // Already set, nothing to do!
1926 return false;
1927 }
1928 uids.put(uid, poolIdx);
1929 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1930 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1931 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1932 if (active == null) {
1933 // not currently active, nothing to do.
1934 return false;
1935 }
1936 SparseIntArray uids = active.get(name);
1937 if (uids == null) {
1938 // not currently active, nothing to do.
1939 return false;
1940 }
1941 idx = uids.indexOfKey(uid);
1942 if (idx < 0) {
1943 // not currently active, nothing to do.
1944 return false;
1945 }
1946 uids.removeAt(idx);
1947 if (uids.size() <= 0) {
1948 active.remove(name);
1949 }
1950 }
1951 return true;
1952 }
1953
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001954 public void removeEvents(int code) {
1955 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1956 mActiveEvents[idx] = null;
1957 }
1958
Dianne Hackborn37de0982014-05-09 09:32:18 -07001959 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1960 return mActiveEvents[code];
1961 }
1962 }
1963
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001964 public static final class BitDescription {
1965 public final int mask;
1966 public final int shift;
1967 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001968 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001969 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001970 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07001971
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001972 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001973 this.mask = mask;
1974 this.shift = -1;
1975 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001976 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001977 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001978 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001979 }
Bookatzc8c44962017-05-11 12:12:54 -07001980
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001981 public BitDescription(int mask, int shift, String name, String shortName,
1982 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001983 this.mask = mask;
1984 this.shift = shift;
1985 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001986 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001987 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001988 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001989 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001990 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001991
Dianne Hackbornfc064132014-06-02 12:42:12 -07001992 /**
1993 * Don't allow any more batching in to the current history event. This
1994 * is called when printing partial histories, so to ensure that the next
1995 * history event will go in to a new batch after what was printed in the
1996 * last partial history.
1997 */
1998 public abstract void commitCurrentHistoryBatchLocked();
1999
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002000 public abstract int getHistoryTotalSize();
2001
2002 public abstract int getHistoryUsedSize();
2003
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002004 public abstract boolean startIteratingHistoryLocked();
2005
Dianne Hackborn099bc622014-01-22 13:39:16 -08002006 public abstract int getHistoryStringPoolSize();
2007
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002008 public abstract int getHistoryStringPoolBytes();
2009
2010 public abstract String getHistoryTagPoolString(int index);
2011
2012 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002013
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002014 public abstract boolean getNextHistoryLocked(HistoryItem out);
2015
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002016 public abstract void finishIteratingHistoryLocked();
2017
2018 public abstract boolean startIteratingOldHistoryLocked();
2019
2020 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
2021
2022 public abstract void finishIteratingOldHistoryLocked();
2023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002025 * Return the base time offset for the battery history.
2026 */
2027 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07002028
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002029 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 * Returns the number of times the device has been started.
2031 */
2032 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07002033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002035 * 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 -08002036 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002037 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 * {@hide}
2039 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002040 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002041
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002042 /**
2043 * Returns the number of times the screen was turned on.
2044 *
2045 * {@hide}
2046 */
2047 public abstract int getScreenOnCount(int which);
2048
Mike Mac2f518a2017-09-19 16:06:03 -07002049 /**
2050 * Returns the time in microseconds that the screen has been dozing while the device was
2051 * running on battery.
2052 *
2053 * {@hide}
2054 */
2055 public abstract long getScreenDozeTime(long elapsedRealtimeUs, int which);
2056
2057 /**
2058 * Returns the number of times the screen was turned dozing.
2059 *
2060 * {@hide}
2061 */
2062 public abstract int getScreenDozeCount(int which);
2063
Jeff Browne95c3cd2014-05-02 16:59:26 -07002064 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
2065
Dianne Hackborn617f8772009-03-31 15:04:46 -07002066 public static final int SCREEN_BRIGHTNESS_DARK = 0;
2067 public static final int SCREEN_BRIGHTNESS_DIM = 1;
2068 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
2069 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
2070 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07002071
Dianne Hackborn617f8772009-03-31 15:04:46 -07002072 static final String[] SCREEN_BRIGHTNESS_NAMES = {
2073 "dark", "dim", "medium", "light", "bright"
2074 };
Bookatzc8c44962017-05-11 12:12:54 -07002075
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002076 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
2077 "0", "1", "2", "3", "4"
2078 };
2079
Dianne Hackborn617f8772009-03-31 15:04:46 -07002080 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07002081
Dianne Hackborn617f8772009-03-31 15:04:46 -07002082 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002083 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07002084 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07002085 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002086 * {@hide}
2087 */
2088 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002089 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002092 * Returns the {@link Timer} object that tracks the given screen brightness.
2093 *
2094 * {@hide}
2095 */
2096 public abstract Timer getScreenBrightnessTimer(int brightnessBin);
2097
2098 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002099 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002100 * running on battery.
2101 *
2102 * {@hide}
2103 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002104 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002105
2106 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002107 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002108 *
2109 * {@hide}
2110 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002111 public abstract int getPowerSaveModeEnabledCount(int which);
2112
2113 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002114 * Constant for device idle mode: not active.
2115 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002116 public static final int DEVICE_IDLE_MODE_OFF = ServerProtoEnums.DEVICE_IDLE_MODE_OFF; // 0
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002117
2118 /**
2119 * Constant for device idle mode: active in lightweight mode.
2120 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002121 public static final int DEVICE_IDLE_MODE_LIGHT = ServerProtoEnums.DEVICE_IDLE_MODE_LIGHT; // 1
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002122
2123 /**
2124 * Constant for device idle mode: active in full mode.
2125 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002126 public static final int DEVICE_IDLE_MODE_DEEP = ServerProtoEnums.DEVICE_IDLE_MODE_DEEP; // 2
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002127
2128 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002129 * Returns the time in microseconds that device has been in idle mode while
2130 * running on battery.
2131 *
2132 * {@hide}
2133 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002134 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002135
2136 /**
2137 * Returns the number of times that the devie has gone in to idle mode.
2138 *
2139 * {@hide}
2140 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002141 public abstract int getDeviceIdleModeCount(int mode, int which);
2142
2143 /**
2144 * Return the longest duration we spent in a particular device idle mode (fully in the
2145 * mode, not in idle maintenance etc).
2146 */
2147 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002148
2149 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002150 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002151 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002152 * counts all of the time that we consider the device to be idle, whether or not
2153 * it is currently in the actual device idle mode.
2154 *
2155 * {@hide}
2156 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002157 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002158
2159 /**
Bookatz8c6571b2017-10-24 15:04:41 -07002160 * Returns the number of times that the device has started idling.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002161 *
2162 * {@hide}
2163 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002164 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002165
2166 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002167 * Returns the number of times that connectivity state changed.
2168 *
2169 * {@hide}
2170 */
2171 public abstract int getNumConnectivityChange(int which);
2172
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002173
2174 /**
2175 * Returns the time in microseconds that the phone has been running with
2176 * the given GPS signal quality level
2177 *
2178 * {@hide}
2179 */
2180 public abstract long getGpsSignalQualityTime(int strengthBin,
2181 long elapsedRealtimeUs, int which);
2182
2183 /**
2184 * Returns the GPS battery drain in mA-ms
2185 *
2186 * {@hide}
2187 */
2188 public abstract long getGpsBatteryDrainMaMs();
2189
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002190 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002191 * 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 -08002192 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002193 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 * {@hide}
2195 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002196 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002197
Dianne Hackborn627bba72009-03-24 22:32:56 -07002198 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002199 * Returns the number of times a phone call was activated.
2200 *
2201 * {@hide}
2202 */
2203 public abstract int getPhoneOnCount(int which);
2204
2205 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002206 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002207 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002208 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002209 * {@hide}
2210 */
2211 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002212 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002213
Dianne Hackborn617f8772009-03-31 15:04:46 -07002214 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07002215 * Returns the time in microseconds that the phone has been trying to
2216 * acquire a signal.
2217 *
2218 * {@hide}
2219 */
2220 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002221 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002222
2223 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002224 * Returns the {@link Timer} object that tracks how much the phone has been trying to
2225 * acquire a signal.
2226 *
2227 * {@hide}
2228 */
2229 public abstract Timer getPhoneSignalScanningTimer();
2230
2231 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002232 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002233 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002234 * {@hide}
2235 */
2236 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
2237
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002238 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002239 * Return the {@link Timer} object used to track the given signal strength's duration and
2240 * counts.
2241 */
2242 protected abstract Timer getPhoneSignalStrengthTimer(int strengthBin);
2243
2244 /**
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002245 * Returns the time in microseconds that the mobile network has been active
2246 * (in a high power state).
2247 *
2248 * {@hide}
2249 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002250 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002251
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002252 /**
2253 * Returns the number of times that the mobile network has transitioned to the
2254 * active state.
2255 *
2256 * {@hide}
2257 */
2258 public abstract int getMobileRadioActiveCount(int which);
2259
2260 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002261 * Returns the time in microseconds that is the difference between the mobile radio
2262 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
2263 * from the radio.
2264 *
2265 * {@hide}
2266 */
2267 public abstract long getMobileRadioActiveAdjustedTime(int which);
2268
2269 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002270 * Returns the time in microseconds that the mobile network has been active
2271 * (in a high power state) but not being able to blame on an app.
2272 *
2273 * {@hide}
2274 */
2275 public abstract long getMobileRadioActiveUnknownTime(int which);
2276
2277 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002278 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002279 *
2280 * {@hide}
2281 */
2282 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002283
Tej Singheee317b2018-03-07 19:28:05 -08002284 public static final int DATA_CONNECTION_NONE = 0;
2285 public static final int DATA_CONNECTION_OTHER = TelephonyManager.MAX_NETWORK_TYPE + 1;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002286
Dianne Hackborn627bba72009-03-24 22:32:56 -07002287 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002288 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002289 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Bookatzdf6baff2018-12-12 19:38:11 -08002290 "ehrpd", "hspap", "gsm", "td_scdma", "iwlan", "lte_ca", "nr",
2291 "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002292 };
Bookatzc8c44962017-05-11 12:12:54 -07002293
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002294 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002295
Dianne Hackborn627bba72009-03-24 22:32:56 -07002296 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002297 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002298 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002299 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002300 * {@hide}
2301 */
2302 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002303 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002306 * Returns the number of times the phone has entered the given data
2307 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002308 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002309 * {@hide}
2310 */
2311 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002312
Kweku Adams87b19ec2017-10-09 12:40:03 -07002313 /**
2314 * Returns the {@link Timer} object that tracks the phone's data connection type stats.
2315 */
2316 public abstract Timer getPhoneDataConnectionTimer(int dataType);
2317
Dianne Hackborn3251b902014-06-20 14:40:53 -07002318 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2319 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2320 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2321 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2322 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2323 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2324 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2325 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2326 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2327 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2328 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2329 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2330 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2331
2332 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2333
2334 static final String[] WIFI_SUPPL_STATE_NAMES = {
2335 "invalid", "disconn", "disabled", "inactive", "scanning",
2336 "authenticating", "associating", "associated", "4-way-handshake",
2337 "group-handshake", "completed", "dormant", "uninit"
2338 };
2339
2340 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2341 "inv", "dsc", "dis", "inact", "scan",
2342 "auth", "ascing", "asced", "4-way",
2343 "group", "compl", "dorm", "uninit"
2344 };
2345
Mike Mac2f518a2017-09-19 16:06:03 -07002346 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002347 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002348 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2349 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002350 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002351 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2352 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2353 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002354 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002355 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002356 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002357 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002358 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2359 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Mike Mac2f518a2017-09-19 16:06:03 -07002360 new BitDescription(HistoryItem.STATE_SCREEN_DOZE_FLAG, "screen_doze", "Sd"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002361 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2362 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2363 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2364 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2365 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2366 new String[] {"in", "out", "emergency", "off"},
2367 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002368 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2369 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2370 SignalStrength.SIGNAL_STRENGTH_NAMES,
2371 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002372 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2373 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2374 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002375 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002376
Mike Ma926a97c2018-03-25 02:32:35 -07002377 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002378 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002379 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002380 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002381 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002382 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002383 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2384 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2385 new String[] { "off", "light", "full", "???" },
2386 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002387 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
Mike Ma926a97c2018-03-25 02:32:35 -07002388 new BitDescription(HistoryItem.STATE2_USB_DATA_LINK_FLAG, "usb_data", "Ud"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002389 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2390 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002391 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2392 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2393 new String[] { "0", "1", "2", "3", "4" },
2394 new String[] { "0", "1", "2", "3", "4" }),
2395 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2396 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2397 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002398 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002399 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Siddharth Rayf5e796a2018-01-22 18:18:17 -08002400 new BitDescription(HistoryItem.STATE2_CELLULAR_HIGH_TX_POWER_FLAG,
2401 "cellular_high_tx_power", "Chtp"),
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002402 new BitDescription(HistoryItem.STATE2_GPS_SIGNAL_QUALITY_MASK,
2403 HistoryItem.STATE2_GPS_SIGNAL_QUALITY_SHIFT, "gps_signal_quality", "Gss",
Siddharth Rayf5e796a2018-01-22 18:18:17 -08002404 new String[] { "poor", "good"}, new String[] { "poor", "good"})
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002405 };
2406
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002407 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002408 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002409 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2410 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002411 };
2412
2413 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002414 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002415 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002416 "Esw", "Ewa", "Elw", "Eec"
2417 };
2418
2419 @FunctionalInterface
2420 public interface IntToString {
2421 String applyAsString(int val);
2422 }
2423
2424 private static final IntToString sUidToString = UserHandle::formatUid;
2425 private static final IntToString sIntToString = Integer::toString;
2426
2427 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2428 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
Hirono Shiyama50ea4d02018-08-27 16:41:28 +09002429 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sIntToString,
Adam Lesinski041d9172016-12-12 12:03:56 -08002430 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2431 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002432 };
2433
Dianne Hackborn617f8772009-03-31 15:04:46 -07002434 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08002435 * Returns total time for WiFi Multicast Wakelock timer.
2436 * Note that this may be different from the sum of per uid timer values.
2437 *
2438 * {@hide}
2439 */
2440 public abstract long getWifiMulticastWakelockTime(long elapsedRealtimeUs, int which);
2441
2442 /**
2443 * Returns total time for WiFi Multicast Wakelock timer
2444 * Note that this may be different from the sum of per uid timer values.
2445 *
2446 * {@hide}
2447 */
2448 public abstract int getWifiMulticastWakelockCount(int which);
2449
2450 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002451 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002452 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002453 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002454 * {@hide}
2455 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002456 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002457
2458 /**
Siddharth Rayb50a6842017-12-14 15:15:28 -08002459 * Returns the time in microseconds that wifi has been active while the device was
2460 * running on battery.
2461 *
2462 * {@hide}
2463 */
2464 public abstract long getWifiActiveTime(long elapsedRealtimeUs, int which);
2465
2466 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002467 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002468 * been in the running state while the device was running on battery.
2469 *
2470 * {@hide}
2471 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002472 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002473
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002474 public static final int WIFI_STATE_OFF = 0;
2475 public static final int WIFI_STATE_OFF_SCANNING = 1;
2476 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2477 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2478 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2479 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2480 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2481 public static final int WIFI_STATE_SOFT_AP = 7;
2482
2483 static final String[] WIFI_STATE_NAMES = {
2484 "off", "scanning", "no_net", "disconn",
2485 "sta", "p2p", "sta_p2p", "soft_ap"
2486 };
2487
2488 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2489
2490 /**
2491 * Returns the time in microseconds that WiFi has been running in the given state.
2492 *
2493 * {@hide}
2494 */
2495 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002496 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002497
2498 /**
2499 * Returns the number of times that WiFi has entered the given state.
2500 *
2501 * {@hide}
2502 */
2503 public abstract int getWifiStateCount(int wifiState, int which);
2504
The Android Open Source Project10592532009-03-18 17:39:46 -07002505 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002506 * Returns the {@link Timer} object that tracks the given WiFi state.
2507 *
2508 * {@hide}
2509 */
2510 public abstract Timer getWifiStateTimer(int wifiState);
2511
2512 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002513 * Returns the time in microseconds that the wifi supplicant has been
2514 * in a given state.
2515 *
2516 * {@hide}
2517 */
2518 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2519
2520 /**
2521 * Returns the number of times that the wifi supplicant has transitioned
2522 * to a given state.
2523 *
2524 * {@hide}
2525 */
2526 public abstract int getWifiSupplStateCount(int state, int which);
2527
Kweku Adams87b19ec2017-10-09 12:40:03 -07002528 /**
2529 * Returns the {@link Timer} object that tracks the given wifi supplicant state.
2530 *
2531 * {@hide}
2532 */
2533 public abstract Timer getWifiSupplStateTimer(int state);
2534
Dianne Hackborn3251b902014-06-20 14:40:53 -07002535 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2536
2537 /**
2538 * Returns the time in microseconds that WIFI has been running with
2539 * the given signal strength.
2540 *
2541 * {@hide}
2542 */
2543 public abstract long getWifiSignalStrengthTime(int strengthBin,
2544 long elapsedRealtimeUs, int which);
2545
2546 /**
2547 * Returns the number of times WIFI has entered the given signal strength.
2548 *
2549 * {@hide}
2550 */
2551 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2552
2553 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002554 * Returns the {@link Timer} object that tracks the given WIFI signal strength.
2555 *
2556 * {@hide}
2557 */
2558 public abstract Timer getWifiSignalStrengthTimer(int strengthBin);
2559
2560 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002561 * Returns the time in microseconds that the flashlight has been on while the device was
2562 * running on battery.
2563 *
2564 * {@hide}
2565 */
2566 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2567
2568 /**
2569 * Returns the number of times that the flashlight has been turned on while the device was
2570 * running on battery.
2571 *
2572 * {@hide}
2573 */
2574 public abstract long getFlashlightOnCount(int which);
2575
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002576 /**
2577 * Returns the time in microseconds that the camera has been on while the device was
2578 * running on battery.
2579 *
2580 * {@hide}
2581 */
2582 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2583
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002584 /**
2585 * Returns the time in microseconds that bluetooth scans were running while the device was
2586 * on battery.
2587 *
2588 * {@hide}
2589 */
2590 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002591
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002592 public static final int NETWORK_MOBILE_RX_DATA = 0;
2593 public static final int NETWORK_MOBILE_TX_DATA = 1;
2594 public static final int NETWORK_WIFI_RX_DATA = 2;
2595 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002596 public static final int NETWORK_BT_RX_DATA = 4;
2597 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002598 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2599 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2600 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2601 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2602 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002603
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002604 public abstract long getNetworkActivityBytes(int type, int which);
2605 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002606
Adam Lesinskie08af192015-03-25 16:42:59 -07002607 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002608 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002609 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002610 * actual power data.
2611 */
2612 public abstract boolean hasWifiActivityReporting();
2613
2614 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002615 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2616 * in various radio controller states, such as transmit, receive, and idle.
2617 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002618 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002619 public abstract ControllerActivityCounter getWifiControllerActivity();
2620
2621 /**
2622 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2623 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2624 * actual power data.
2625 */
2626 public abstract boolean hasBluetoothActivityReporting();
2627
2628 /**
2629 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2630 * in various radio controller states, such as transmit, receive, and idle.
2631 * @return non-null {@link ControllerActivityCounter}
2632 */
2633 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2634
2635 /**
2636 * Returns true if the BatteryStats object has detailed modem power reports.
2637 * When true, calling {@link #getModemControllerActivity()} will yield the
2638 * actual power data.
2639 */
2640 public abstract boolean hasModemActivityReporting();
2641
2642 /**
2643 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2644 * in various radio controller states, such as transmit, receive, and idle.
2645 * @return non-null {@link ControllerActivityCounter}
2646 */
2647 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002648
The Android Open Source Project10592532009-03-18 17:39:46 -07002649 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002650 * Return the wall clock time when battery stats data collection started.
2651 */
2652 public abstract long getStartClockTime();
2653
2654 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002655 * Return platform version tag that we were running in when the battery stats started.
2656 */
2657 public abstract String getStartPlatformVersion();
2658
2659 /**
2660 * Return platform version tag that we were running in when the battery stats ended.
2661 */
2662 public abstract String getEndPlatformVersion();
2663
2664 /**
2665 * Return the internal version code of the parcelled format.
2666 */
2667 public abstract int getParcelVersion();
2668
2669 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 * Return whether we are currently running on battery.
2671 */
2672 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 /**
2675 * Returns a SparseArray containing the statistics for each uid.
2676 */
2677 public abstract SparseArray<? extends Uid> getUidStats();
2678
2679 /**
2680 * Returns the current battery uptime in microseconds.
2681 *
2682 * @param curTime the amount of elapsed realtime in microseconds.
2683 */
2684 public abstract long getBatteryUptime(long curTime);
2685
2686 /**
2687 * Returns the current battery realtime in microseconds.
2688 *
2689 * @param curTime the amount of elapsed realtime in microseconds.
2690 */
2691 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002692
The Android Open Source Project10592532009-03-18 17:39:46 -07002693 /**
Evan Millar633a1742009-04-02 16:36:33 -07002694 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002695 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002696 */
Evan Millar633a1742009-04-02 16:36:33 -07002697 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002698
The Android Open Source Project10592532009-03-18 17:39:46 -07002699 /**
Evan Millar633a1742009-04-02 16:36:33 -07002700 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2701 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002702 */
Evan Millar633a1742009-04-02 16:36:33 -07002703 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704
2705 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002706 * Get the amount the battery has discharged since the stats were
2707 * last reset after charging, as a lower-end approximation.
2708 */
2709 public abstract int getLowDischargeAmountSinceCharge();
2710
2711 /**
2712 * Get the amount the battery has discharged since the stats were
2713 * last reset after charging, as an upper-end approximation.
2714 */
2715 public abstract int getHighDischargeAmountSinceCharge();
2716
2717 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002718 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2719 */
2720 public abstract int getDischargeAmount(int which);
2721
2722 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002723 * Get the amount the battery has discharged while the screen was on,
2724 * since the last time power was unplugged.
2725 */
2726 public abstract int getDischargeAmountScreenOn();
2727
2728 /**
2729 * Get the amount the battery has discharged while the screen was on,
2730 * since the last time the device was charged.
2731 */
2732 public abstract int getDischargeAmountScreenOnSinceCharge();
2733
2734 /**
2735 * Get the amount the battery has discharged while the screen was off,
2736 * since the last time power was unplugged.
2737 */
2738 public abstract int getDischargeAmountScreenOff();
2739
2740 /**
2741 * Get the amount the battery has discharged while the screen was off,
2742 * since the last time the device was charged.
2743 */
2744 public abstract int getDischargeAmountScreenOffSinceCharge();
2745
2746 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002747 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002748 * since the last time power was unplugged.
2749 */
2750 public abstract int getDischargeAmountScreenDoze();
2751
2752 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002753 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002754 * since the last time the device was charged.
2755 */
2756 public abstract int getDischargeAmountScreenDozeSinceCharge();
2757
2758 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 * Returns the total, last, or current battery uptime in microseconds.
2760 *
2761 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002762 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 */
2764 public abstract long computeBatteryUptime(long curTime, int which);
2765
2766 /**
2767 * Returns the total, last, or current battery realtime in microseconds.
2768 *
2769 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002770 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 */
2772 public abstract long computeBatteryRealtime(long curTime, int which);
2773
2774 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002775 * Returns the total, last, or current battery screen off/doze uptime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002776 *
2777 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002778 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002779 */
2780 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2781
2782 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002783 * Returns the total, last, or current battery screen off/doze realtime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002784 *
2785 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002786 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002787 */
2788 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2789
2790 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 * Returns the total, last, or current uptime in microseconds.
2792 *
2793 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002794 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 */
2796 public abstract long computeUptime(long curTime, int which);
2797
2798 /**
2799 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002800 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002802 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 */
2804 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002805
2806 /**
2807 * Compute an approximation for how much run time (in microseconds) is remaining on
2808 * the battery. Returns -1 if no time can be computed: either there is not
2809 * enough current data to make a decision, or the battery is currently
2810 * charging.
2811 *
2812 * @param curTime The current elepsed realtime in microseconds.
2813 */
2814 public abstract long computeBatteryTimeRemaining(long curTime);
2815
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002816 // The part of a step duration that is the actual time.
2817 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2818
2819 // Bits in a step duration that are the new battery level we are at.
2820 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002821 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002822
2823 // Bits in a step duration that are the initial mode we were in at that step.
2824 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002825 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002826
2827 // Bits in a step duration that indicate which modes changed during that step.
2828 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002829 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002830
2831 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2832 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2833
Santos Cordone94f0502017-02-24 12:31:20 -08002834 // The largest value for screen state that is tracked in battery states. Any values above
2835 // this should be mapped back to one of the tracked values before being tracked here.
2836 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2837
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002838 // Step duration mode: power save is on.
2839 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2840
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002841 // Step duration mode: device is currently in idle mode.
2842 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2843
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002844 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2845 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002846 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2847 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002848 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2849 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2850 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2851 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2852 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002853 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2854 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002855 };
2856 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2857 (Display.STATE_OFF-1),
2858 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002859 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002860 (Display.STATE_ON-1),
2861 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2862 (Display.STATE_DOZE-1),
2863 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2864 (Display.STATE_DOZE_SUSPEND-1),
2865 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002866 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002867 };
2868 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2869 "screen off",
2870 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002871 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002872 "screen on",
2873 "screen on power save",
2874 "screen doze",
2875 "screen doze power save",
2876 "screen doze-suspend",
2877 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002878 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002879 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002880
2881 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002882 * Return the amount of battery discharge while the screen was off, measured in
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002883 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2884 * a coulomb counter.
2885 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002886 public abstract long getUahDischargeScreenOff(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002887
2888 /**
2889 * Return the amount of battery discharge while the screen was in doze mode, measured in
2890 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2891 * a coulomb counter.
2892 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002893 public abstract long getUahDischargeScreenDoze(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002894
2895 /**
2896 * Return the amount of battery discharge measured in micro-Ampere-hours. This will be
2897 * non-zero only if the device's battery has a coulomb counter.
2898 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002899 public abstract long getUahDischarge(int which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002900
2901 /**
Mike Ma15313c92017-11-15 17:58:21 -08002902 * @return the amount of battery discharge while the device is in light idle mode, measured in
2903 * micro-Ampere-hours.
2904 */
2905 public abstract long getUahDischargeLightDoze(int which);
2906
2907 /**
2908 * @return the amount of battery discharge while the device is in deep idle mode, measured in
2909 * micro-Ampere-hours.
2910 */
2911 public abstract long getUahDischargeDeepDoze(int which);
2912
2913 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002914 * Returns the estimated real battery capacity, which may be less than the capacity
2915 * declared by the PowerProfile.
2916 * @return The estimated battery capacity in mAh.
2917 */
2918 public abstract int getEstimatedBatteryCapacity();
2919
2920 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002921 * @return The minimum learned battery capacity in uAh.
2922 */
2923 public abstract int getMinLearnedBatteryCapacity();
2924
2925 /**
2926 * @return The maximum learned battery capacity in uAh.
2927 */
2928 public abstract int getMaxLearnedBatteryCapacity() ;
2929
2930 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002931 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002932 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002933 public abstract LevelStepTracker getDischargeLevelStepTracker();
2934
2935 /**
2936 * Return the array of daily discharge step durations.
2937 */
2938 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002939
2940 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002941 * Compute an approximation for how much time (in microseconds) remains until the battery
2942 * is fully charged. Returns -1 if no time can be computed: either there is not
2943 * enough current data to make a decision, or the battery is currently
2944 * discharging.
2945 *
2946 * @param curTime The current elepsed realtime in microseconds.
2947 */
2948 public abstract long computeChargeTimeRemaining(long curTime);
2949
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002950 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002951 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002952 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002953 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002954
2955 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002956 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002957 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002958 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002959
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002960 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2961
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002962 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002963
Evan Millarc64edde2009-04-18 12:26:32 -07002964 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965
Bookatz50df7112017-08-04 14:53:26 -07002966 /**
2967 * Returns Timers tracking the total time of each Resource Power Manager state and voter.
2968 */
2969 public abstract Map<String, ? extends Timer> getRpmStats();
2970 /**
2971 * Returns Timers tracking the screen-off time of each Resource Power Manager state and voter.
2972 */
2973 public abstract Map<String, ? extends Timer> getScreenOffRpmStats();
2974
2975
James Carr2dd7e5e2016-07-20 18:48:39 -07002976 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2977
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002978 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2979
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002980 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 long days = seconds / (60 * 60 * 24);
2982 if (days != 0) {
2983 out.append(days);
2984 out.append("d ");
2985 }
2986 long used = days * 60 * 60 * 24;
2987
2988 long hours = (seconds - used) / (60 * 60);
2989 if (hours != 0 || used != 0) {
2990 out.append(hours);
2991 out.append("h ");
2992 }
2993 used += hours * 60 * 60;
2994
2995 long mins = (seconds-used) / 60;
2996 if (mins != 0 || used != 0) {
2997 out.append(mins);
2998 out.append("m ");
2999 }
3000 used += mins * 60;
3001
3002 if (seconds != 0 || used != 0) {
3003 out.append(seconds-used);
3004 out.append("s ");
3005 }
3006 }
3007
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003008 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003010 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 sb.append(time - (sec * 1000));
3012 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 }
3014
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003015 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003016 long sec = time / 1000;
3017 formatTimeRaw(sb, sec);
3018 sb.append(time - (sec * 1000));
3019 sb.append("ms");
3020 }
3021
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003022 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003024 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 }
3026 float perc = ((float)num) / ((float)den) * 100;
3027 mFormatBuilder.setLength(0);
3028 mFormatter.format("%.1f%%", perc);
3029 return mFormatBuilder.toString();
3030 }
3031
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003032 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07003033 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07003034
Evan Millar22ac0432009-03-31 11:33:18 -07003035 if (bytes < BYTES_PER_KB) {
3036 return bytes + "B";
3037 } else if (bytes < BYTES_PER_MB) {
3038 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
3039 return mFormatBuilder.toString();
3040 } else if (bytes < BYTES_PER_GB){
3041 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
3042 return mFormatBuilder.toString();
3043 } else {
3044 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
3045 return mFormatBuilder.toString();
3046 }
3047 }
3048
Kweku Adams103351f2017-10-16 14:39:34 -07003049 private static long roundUsToMs(long timeUs) {
3050 return (timeUs + 500) / 1000;
3051 }
3052
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003053 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003054 if (timer != null) {
3055 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003056 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003057 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
3058 return totalTimeMillis;
3059 }
3060 return 0;
3061 }
3062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 /**
3064 *
3065 * @param sb a StringBuilder object.
3066 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003067 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003068 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003069 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003070 * @param linePrefix a String to be prepended to each line of output.
3071 * @return the line prefix
3072 */
3073 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003074 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003077 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003078
Evan Millarc64edde2009-04-18 12:26:32 -07003079 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003080 if (totalTimeMillis != 0) {
3081 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003082 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08003083 if (name != null) {
3084 sb.append(name);
3085 sb.append(' ');
3086 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 sb.append('(');
3088 sb.append(count);
3089 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003090 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
3091 if (maxDurationMs >= 0) {
3092 sb.append(" max=");
3093 sb.append(maxDurationMs);
3094 }
Bookatz506a8182017-05-01 14:18:42 -07003095 // Put actual time if it is available and different from totalTimeMillis.
3096 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
3097 if (totalDurMs > totalTimeMillis) {
3098 sb.append(" actual=");
3099 sb.append(totalDurMs);
3100 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07003101 if (timer.isRunningLocked()) {
3102 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3103 if (currentMs >= 0) {
3104 sb.append(" (running for ");
3105 sb.append(currentMs);
3106 sb.append("ms)");
3107 } else {
3108 sb.append(" (running)");
3109 }
3110 }
3111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 return ", ";
3113 }
3114 }
3115 return linePrefix;
3116 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003117
3118 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07003119 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003120 *
3121 * @param pw a PrintWriter object to print to.
3122 * @param sb a StringBuilder object.
3123 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08003124 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003125 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
3126 * @param prefix a String to be prepended to each line of output.
3127 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07003128 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003129 */
3130 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07003131 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003132 if (timer != null) {
3133 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07003134 final long totalTimeMs = (timer.getTotalTimeLocked(
3135 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003136 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003137 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003138 sb.setLength(0);
3139 sb.append(prefix);
3140 sb.append(" ");
3141 sb.append(type);
3142 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003143 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003144 sb.append("realtime (");
3145 sb.append(count);
3146 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003147 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
3148 if (maxDurationMs >= 0) {
3149 sb.append(" max=");
3150 sb.append(maxDurationMs);
3151 }
3152 if (timer.isRunningLocked()) {
3153 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
3154 if (currentMs >= 0) {
3155 sb.append(" (running for ");
3156 sb.append(currentMs);
3157 sb.append("ms)");
3158 } else {
3159 sb.append(" (running)");
3160 }
3161 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003162 pw.println(sb.toString());
3163 return true;
3164 }
3165 }
3166 return false;
3167 }
Bookatzc8c44962017-05-11 12:12:54 -07003168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 /**
3170 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07003171 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 * @param sb a StringBuilder object.
3173 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003174 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003176 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 * @param linePrefix a String to be prepended to each line of output.
3178 * @return the line prefix
3179 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003180 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
3181 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 long totalTimeMicros = 0;
3183 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07003184 long max = 0;
3185 long current = 0;
3186 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003187 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003188 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07003189 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003190 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3191 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07003192 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 }
3194 sb.append(linePrefix);
3195 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
3196 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07003197 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003199 sb.append(',');
3200 sb.append(current);
3201 sb.append(',');
3202 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07003203 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
3204 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
3205 // totalDuration independent of totalTimeMicros (since they are not pooled).
3206 if (name != null) {
3207 sb.append(',');
3208 sb.append(totalDuration);
3209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 return ",";
3211 }
Bookatz506a8182017-05-01 14:18:42 -07003212
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003213 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
3214 String type) {
3215 pw.print(BATTERY_STATS_CHECKIN_VERSION);
3216 pw.print(',');
3217 pw.print(uid);
3218 pw.print(',');
3219 pw.print(category);
3220 pw.print(',');
3221 pw.print(type);
3222 }
3223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 /**
3225 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07003226 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 * @param pw the PageWriter to dump log to
3228 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3229 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3230 * @param args type-dependent data arguments
3231 */
Bookatzc8c44962017-05-11 12:12:54 -07003232 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003234 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003235 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003236 pw.print(',');
3237 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003239 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07003241
3242 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003243 * Dump a given timer stat for terse checkin mode.
3244 *
3245 * @param pw the PageWriter to dump log to
3246 * @param uid the UID to log
3247 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3248 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3249 * @param timer a {@link Timer} to dump stats for
3250 * @param rawRealtime the current elapsed realtime of the system in microseconds
3251 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3252 */
3253 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
3254 Timer timer, long rawRealtime, int which) {
3255 if (timer != null) {
3256 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003257 final long totalTime = roundUsToMs(timer.getTotalTimeLocked(rawRealtime, which));
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003258 final int count = timer.getCountLocked(which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003259 if (totalTime != 0 || count != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003260 dumpLine(pw, uid, category, type, totalTime, count);
3261 }
3262 }
3263 }
3264
3265 /**
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003266 * Dump a given timer stat to the proto stream.
3267 *
3268 * @param proto the ProtoOutputStream to log to
3269 * @param fieldId type of data, the field to save to (e.g. AggregatedBatteryStats.WAKELOCK)
3270 * @param timer a {@link Timer} to dump stats for
3271 * @param rawRealtimeUs the current elapsed realtime of the system in microseconds
3272 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3273 */
3274 private static void dumpTimer(ProtoOutputStream proto, long fieldId,
Kweku Adams87b19ec2017-10-09 12:40:03 -07003275 Timer timer, long rawRealtimeUs, int which) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003276 if (timer == null) {
3277 return;
3278 }
3279 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003280 final long timeMs = roundUsToMs(timer.getTotalTimeLocked(rawRealtimeUs, which));
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003281 final int count = timer.getCountLocked(which);
Kweku Adams103351f2017-10-16 14:39:34 -07003282 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs / 1000);
3283 final long curDurationMs = timer.getCurrentDurationMsLocked(rawRealtimeUs / 1000);
3284 final long totalDurationMs = timer.getTotalDurationMsLocked(rawRealtimeUs / 1000);
3285 if (timeMs != 0 || count != 0 || maxDurationMs != -1 || curDurationMs != -1
3286 || totalDurationMs != -1) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003287 final long token = proto.start(fieldId);
Kweku Adams103351f2017-10-16 14:39:34 -07003288 proto.write(TimerProto.DURATION_MS, timeMs);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003289 proto.write(TimerProto.COUNT, count);
Kweku Adams103351f2017-10-16 14:39:34 -07003290 // These values will be -1 for timers that don't implement the functionality.
3291 if (maxDurationMs != -1) {
3292 proto.write(TimerProto.MAX_DURATION_MS, maxDurationMs);
3293 }
3294 if (curDurationMs != -1) {
3295 proto.write(TimerProto.CURRENT_DURATION_MS, curDurationMs);
3296 }
3297 if (totalDurationMs != -1) {
3298 proto.write(TimerProto.TOTAL_DURATION_MS, totalDurationMs);
3299 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003300 proto.end(token);
3301 }
3302 }
3303
3304 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003305 * Checks if the ControllerActivityCounter has any data worth dumping.
3306 */
3307 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
3308 if (counter == null) {
3309 return false;
3310 }
3311
3312 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
3313 || counter.getRxTimeCounter().getCountLocked(which) != 0
3314 || counter.getPowerCounter().getCountLocked(which) != 0) {
3315 return true;
3316 }
3317
3318 for (LongCounter c : counter.getTxTimeCounters()) {
3319 if (c.getCountLocked(which) != 0) {
3320 return true;
3321 }
3322 }
3323 return false;
3324 }
3325
3326 /**
3327 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3328 * The order of the arguments in the final check in line is:
3329 *
3330 * idle, rx, power, tx...
3331 *
3332 * where tx... is one or more transmit level times.
3333 */
3334 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
3335 String type,
3336 ControllerActivityCounter counter,
3337 int which) {
3338 if (!controllerActivityHasData(counter, which)) {
3339 return;
3340 }
3341
3342 dumpLineHeader(pw, uid, category, type);
3343 pw.print(",");
3344 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
3345 pw.print(",");
3346 pw.print(counter.getRxTimeCounter().getCountLocked(which));
3347 pw.print(",");
3348 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3349 for (LongCounter c : counter.getTxTimeCounters()) {
3350 pw.print(",");
3351 pw.print(c.getCountLocked(which));
3352 }
3353 pw.println();
3354 }
3355
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003356 /**
3357 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3358 */
3359 private static void dumpControllerActivityProto(ProtoOutputStream proto, long fieldId,
3360 ControllerActivityCounter counter,
3361 int which) {
3362 if (!controllerActivityHasData(counter, which)) {
3363 return;
3364 }
3365
3366 final long cToken = proto.start(fieldId);
3367
3368 proto.write(ControllerActivityProto.IDLE_DURATION_MS,
3369 counter.getIdleTimeCounter().getCountLocked(which));
3370 proto.write(ControllerActivityProto.RX_DURATION_MS,
3371 counter.getRxTimeCounter().getCountLocked(which));
3372 proto.write(ControllerActivityProto.POWER_MAH,
3373 counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3374
3375 long tToken;
3376 LongCounter[] txCounters = counter.getTxTimeCounters();
3377 for (int i = 0; i < txCounters.length; ++i) {
3378 LongCounter c = txCounters[i];
3379 tToken = proto.start(ControllerActivityProto.TX);
3380 proto.write(ControllerActivityProto.TxLevel.LEVEL, i);
3381 proto.write(ControllerActivityProto.TxLevel.DURATION_MS, c.getCountLocked(which));
3382 proto.end(tToken);
3383 }
3384
3385 proto.end(cToken);
3386 }
3387
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003388 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
3389 String prefix, String controllerName,
3390 ControllerActivityCounter counter,
3391 int which) {
3392 if (controllerActivityHasData(counter, which)) {
3393 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
3394 }
3395 }
3396
3397 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
3398 String controllerName,
3399 ControllerActivityCounter counter, int which) {
3400 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
3401 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
3402 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003403 // Battery real time
3404 final long totalControllerActivityTimeMs
3405 = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003406 long totalTxTimeMs = 0;
3407 for (LongCounter txState : counter.getTxTimeCounters()) {
3408 totalTxTimeMs += txState.getCountLocked(which);
3409 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003410
Siddharth Rayb50a6842017-12-14 15:15:28 -08003411 if (controllerName.equals(WIFI_CONTROLLER_NAME)) {
3412 final long scanTimeMs = counter.getScanTimeCounter().getCountLocked(which);
3413 sb.setLength(0);
3414 sb.append(prefix);
3415 sb.append(" ");
3416 sb.append(controllerName);
3417 sb.append(" Scan time: ");
3418 formatTimeMs(sb, scanTimeMs);
3419 sb.append("(");
3420 sb.append(formatRatioLocked(scanTimeMs, totalControllerActivityTimeMs));
3421 sb.append(")");
3422 pw.println(sb.toString());
Siddharth Rayed754702018-02-15 12:44:37 -08003423
3424 final long sleepTimeMs
3425 = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + totalTxTimeMs);
3426 sb.setLength(0);
3427 sb.append(prefix);
3428 sb.append(" ");
3429 sb.append(controllerName);
3430 sb.append(" Sleep time: ");
3431 formatTimeMs(sb, sleepTimeMs);
3432 sb.append("(");
3433 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3434 sb.append(")");
3435 pw.println(sb.toString());
Siddharth Rayb50a6842017-12-14 15:15:28 -08003436 }
3437
Siddharth Rayed754702018-02-15 12:44:37 -08003438 if (controllerName.equals(CELLULAR_CONTROLLER_NAME)) {
3439 final long sleepTimeMs = counter.getSleepTimeCounter().getCountLocked(which);
3440 sb.setLength(0);
3441 sb.append(prefix);
3442 sb.append(" ");
3443 sb.append(controllerName);
3444 sb.append(" Sleep time: ");
3445 formatTimeMs(sb, sleepTimeMs);
3446 sb.append("(");
3447 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3448 sb.append(")");
3449 pw.println(sb.toString());
3450 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003451
3452 sb.setLength(0);
3453 sb.append(prefix);
3454 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003455 sb.append(controllerName);
3456 sb.append(" Idle time: ");
3457 formatTimeMs(sb, idleTimeMs);
3458 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003459 sb.append(formatRatioLocked(idleTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003460 sb.append(")");
3461 pw.println(sb.toString());
3462
3463 sb.setLength(0);
3464 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003465 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003466 sb.append(controllerName);
3467 sb.append(" Rx time: ");
3468 formatTimeMs(sb, rxTimeMs);
3469 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003470 sb.append(formatRatioLocked(rxTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003471 sb.append(")");
3472 pw.println(sb.toString());
3473
3474 sb.setLength(0);
3475 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003476 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003477 sb.append(controllerName);
3478 sb.append(" Tx time: ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003479
Siddharth Ray3c648c42017-10-02 17:30:58 -07003480 String [] powerLevel;
3481 switch(controllerName) {
Siddharth Rayb50a6842017-12-14 15:15:28 -08003482 case CELLULAR_CONTROLLER_NAME:
Siddharth Ray3c648c42017-10-02 17:30:58 -07003483 powerLevel = new String[] {
3484 " less than 0dBm: ",
3485 " 0dBm to 8dBm: ",
3486 " 8dBm to 15dBm: ",
3487 " 15dBm to 20dBm: ",
3488 " above 20dBm: "};
3489 break;
3490 default:
3491 powerLevel = new String[] {"[0]", "[1]", "[2]", "[3]", "[4]"};
3492 break;
3493 }
3494 final int numTxLvls = Math.min(counter.getTxTimeCounters().length, powerLevel.length);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003495 if (numTxLvls > 1) {
Siddharth Ray3c648c42017-10-02 17:30:58 -07003496 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003497 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3498 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3499 sb.setLength(0);
3500 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003501 sb.append(" ");
3502 sb.append(powerLevel[lvl]);
3503 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003504 formatTimeMs(sb, txLvlTimeMs);
3505 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003506 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003507 sb.append(")");
3508 pw.println(sb.toString());
3509 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003510 } else {
3511 final long txLvlTimeMs = counter.getTxTimeCounters()[0].getCountLocked(which);
3512 formatTimeMs(sb, txLvlTimeMs);
3513 sb.append("(");
3514 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
3515 sb.append(")");
3516 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003517 }
3518
Siddharth Ray3c648c42017-10-02 17:30:58 -07003519 if (powerDrainMaMs > 0) {
3520 sb.setLength(0);
3521 sb.append(prefix);
3522 sb.append(" ");
3523 sb.append(controllerName);
3524 sb.append(" Battery drain: ").append(
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003525 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
Siddharth Ray3c648c42017-10-02 17:30:58 -07003526 sb.append("mAh");
3527 pw.println(sb.toString());
3528 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003529 }
3530
3531 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003532 * Temporary for settings.
3533 */
3534 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3535 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3536 }
3537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 /**
3539 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003540 *
Kweku Adams87b19ec2017-10-09 12:40:03 -07003541 * NOTE: all times are expressed in microseconds, unless specified otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003543 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3544 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003545 final long rawUptime = SystemClock.uptimeMillis() * 1000;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003546 final long rawRealtimeMs = SystemClock.elapsedRealtime();
3547 final long rawRealtime = rawRealtimeMs * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3550 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003551 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3552 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3553 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 final long totalRealtime = computeRealtime(rawRealtime, which);
3555 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003556 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Mike Mac2f518a2017-09-19 16:06:03 -07003557 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003558 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003559 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003560 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3561 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003562 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003563 rawRealtime, which);
3564 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3565 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003566 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003567 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003568 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003569 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003570 final long dischargeCount = getUahDischarge(which);
3571 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
3572 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Ma15313c92017-11-15 17:58:21 -08003573 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
3574 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003575
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003576 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003577
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003578 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003579 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003580
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003581 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003583 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003584 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003585 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003586 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003587 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003588 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003589 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003590 getEstimatedBatteryCapacity(),
Mike Mac2f518a2017-09-19 16:06:03 -07003591 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity(),
3592 screenDozeTime / 1000);
Adam Lesinski67c134f2016-06-10 15:15:08 -07003593
Bookatzc8c44962017-05-11 12:12:54 -07003594
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003595 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003596 long fullWakeLockTimeTotal = 0;
3597 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003598
Evan Millar22ac0432009-03-31 11:33:18 -07003599 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003600 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003601
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003602 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3603 = u.getWakelockStats();
3604 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3605 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003606
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003607 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3608 if (fullWakeTimer != null) {
3609 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3610 which);
3611 }
3612
3613 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3614 if (partialWakeTimer != null) {
3615 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3616 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003617 }
3618 }
3619 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003620
3621 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003622 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3623 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3624 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3625 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3626 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3627 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3628 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3629 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003630 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3631 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003632 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3633 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003634 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3635 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003636
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003637 // Dump Modem controller stats
3638 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3639 getModemControllerActivity(), which);
3640
Adam Lesinskie283d332015-04-16 12:29:25 -07003641 // Dump Wifi controller stats
3642 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3643 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003644 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003645 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003646
3647 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3648 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003649
3650 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003651 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3652 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003654 // Dump misc stats
3655 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003656 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003657 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003658 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003659 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003660 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003661 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3662 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003663 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003664 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3665 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3666 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3667 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003668 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003669
Dianne Hackborn617f8772009-03-31 15:04:46 -07003670 // Dump screen brightness stats
3671 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3672 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003673 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003674 }
3675 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003676
Dianne Hackborn627bba72009-03-24 22:32:56 -07003677 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003678 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3679 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003680 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003681 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003682 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003683 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003684 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003685 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003686 args[i] = getPhoneSignalStrengthCount(i, which);
3687 }
3688 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003689
Dianne Hackborn627bba72009-03-24 22:32:56 -07003690 // Dump network type stats
3691 args = new Object[NUM_DATA_CONNECTION_TYPES];
3692 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003693 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003694 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003695 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3696 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3697 args[i] = getPhoneDataConnectionCount(i, which);
3698 }
3699 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003700
3701 // Dump wifi state stats
3702 args = new Object[NUM_WIFI_STATES];
3703 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003704 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003705 }
3706 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3707 for (int i=0; i<NUM_WIFI_STATES; i++) {
3708 args[i] = getWifiStateCount(i, which);
3709 }
3710 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3711
Dianne Hackborn3251b902014-06-20 14:40:53 -07003712 // Dump wifi suppl state stats
3713 args = new Object[NUM_WIFI_SUPPL_STATES];
3714 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3715 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3716 }
3717 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3718 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3719 args[i] = getWifiSupplStateCount(i, which);
3720 }
3721 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3722
3723 // Dump wifi signal strength stats
3724 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3725 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3726 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3727 }
3728 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3729 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3730 args[i] = getWifiSignalStrengthCount(i, which);
3731 }
3732 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3733
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003734 // Dump Multicast total stats
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003735 final long multicastWakeLockTimeTotalMicros =
3736 getWifiMulticastWakelockTime(rawRealtime, which);
3737 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003738 dumpLine(pw, 0 /* uid */, category, WIFI_MULTICAST_TOTAL_DATA,
3739 multicastWakeLockTimeTotalMicros / 1000,
3740 multicastWakeLockCountTotal);
3741
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003742 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003743 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003744 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003745 }
Bookatzc8c44962017-05-11 12:12:54 -07003746
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003747 if (which == STATS_SINCE_UNPLUGGED) {
3748 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3749 getDischargeStartLevel()-getDischargeCurrentLevel(),
3750 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003751 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
Mike Mac2f518a2017-09-19 16:06:03 -07003752 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003753 getDischargeAmountScreenDoze(), dischargeScreenDozeCount / 1000,
3754 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003755 } else {
3756 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3757 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003758 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003759 getDischargeAmountScreenOffSinceCharge(),
Mike Mac2f518a2017-09-19 16:06:03 -07003760 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003761 getDischargeAmountScreenDozeSinceCharge(), dischargeScreenDozeCount / 1000,
3762 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003763 }
Bookatzc8c44962017-05-11 12:12:54 -07003764
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003765 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003766 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003767 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003768 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003769 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003770 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003771 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3772 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003773 }
Evan Millarc64edde2009-04-18 12:26:32 -07003774 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003775 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003776 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003777 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3778 // Not doing the regular wake lock formatting to remain compatible
3779 // with the old checkin format.
3780 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3781 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003782 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003783 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003784 }
3785 }
Evan Millarc64edde2009-04-18 12:26:32 -07003786 }
Bookatzc8c44962017-05-11 12:12:54 -07003787
Bookatz50df7112017-08-04 14:53:26 -07003788 final Map<String, ? extends Timer> rpmStats = getRpmStats();
3789 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
3790 if (rpmStats.size() > 0) {
3791 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
3792 sb.setLength(0);
3793 Timer totalTimer = ent.getValue();
3794 long timeMs = (totalTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3795 int count = totalTimer.getCountLocked(which);
3796 Timer screenOffTimer = screenOffRpmStats.get(ent.getKey());
3797 long screenOffTimeMs = screenOffTimer != null
3798 ? (screenOffTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3799 int screenOffCount = screenOffTimer != null
3800 ? screenOffTimer.getCountLocked(which) : 0;
Bookatz82b341172017-09-07 19:06:08 -07003801 if (SCREEN_OFF_RPM_STATS_ENABLED) {
3802 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3803 "\"" + ent.getKey() + "\"", timeMs, count, screenOffTimeMs,
3804 screenOffCount);
3805 } else {
3806 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3807 "\"" + ent.getKey() + "\"", timeMs, count);
3808 }
Bookatz50df7112017-08-04 14:53:26 -07003809 }
3810 }
3811
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003812 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003813 helper.create(this);
3814 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003815 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003816 if (sippers != null && sippers.size() > 0) {
3817 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3818 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003819 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003820 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3821 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
Kweku Adams87b19ec2017-10-09 12:40:03 -07003822 int uid = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003823 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003824 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003825 String label;
3826 switch (bs.drainType) {
Kweku Adamsef728952018-04-10 17:56:16 -07003827 case AMBIENT_DISPLAY:
3828 label = "ambi";
3829 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003830 case IDLE:
3831 label="idle";
3832 break;
3833 case CELL:
3834 label="cell";
3835 break;
3836 case PHONE:
3837 label="phone";
3838 break;
3839 case WIFI:
3840 label="wifi";
3841 break;
3842 case BLUETOOTH:
3843 label="blue";
3844 break;
3845 case SCREEN:
3846 label="scrn";
3847 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003848 case FLASHLIGHT:
3849 label="flashlight";
3850 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003851 case APP:
3852 uid = bs.uidObj.getUid();
3853 label = "uid";
3854 break;
3855 case USER:
3856 uid = UserHandle.getUid(bs.userId, 0);
3857 label = "user";
3858 break;
3859 case UNACCOUNTED:
3860 label = "unacc";
3861 break;
3862 case OVERCOUNTED:
3863 label = "over";
3864 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003865 case CAMERA:
3866 label = "camera";
3867 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003868 case MEMORY:
3869 label = "memory";
3870 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003871 default:
3872 label = "???";
3873 }
3874 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07003875 BatteryStatsHelper.makemAh(bs.totalPowerMah),
3876 bs.shouldHide ? 1 : 0,
3877 BatteryStatsHelper.makemAh(bs.screenPowerMah),
3878 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003879 }
3880 }
3881
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003882 final long[] cpuFreqs = getCpuFreqs();
3883 if (cpuFreqs != null) {
3884 sb.setLength(0);
3885 for (int i = 0; i < cpuFreqs.length; ++i) {
3886 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3887 }
3888 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3889 }
3890
Kweku Adams87b19ec2017-10-09 12:40:03 -07003891 // Dump stats per UID.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 for (int iu = 0; iu < NU; iu++) {
3893 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003894 if (reqUid >= 0 && uid != reqUid) {
3895 continue;
3896 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003897 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003900 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3901 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3902 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3903 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3904 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3905 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3906 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3907 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003908 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003909 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3910 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003911 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003912 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3913 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003914 // Background data transfers
3915 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3916 which);
3917 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3918 which);
3919 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3920 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3921 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3922 which);
3923 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3924 which);
3925 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3926 which);
3927 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3928 which);
3929
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003930 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3931 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003932 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003933 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3934 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3935 || wifiBytesBgTx > 0
3936 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3937 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003938 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3939 wifiBytesRx, wifiBytesTx,
3940 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003941 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003942 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003943 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3944 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3945 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3946 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003947 }
3948
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003949 // Dump modem controller data, per UID.
3950 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3951 u.getModemControllerActivity(), which);
3952
3953 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003954 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3955 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3956 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003957 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3958 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003959 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3960 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3961 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003962 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003963 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003964 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3965 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003966 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3967 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003968 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003969 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003972 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3973 u.getWifiControllerActivity(), which);
3974
Bookatz867c0d72017-03-07 18:23:42 -08003975 final Timer bleTimer = u.getBluetoothScanTimer();
3976 if (bleTimer != null) {
3977 // Convert from microseconds to milliseconds with rounding
3978 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3979 / 1000;
3980 if (totalTime != 0) {
3981 final int count = bleTimer.getCountLocked(which);
3982 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3983 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003984 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3985 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3986 final long actualTimeBg = bleTimerBg != null ?
3987 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003988 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003989 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3990 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003991 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3992 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3993 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3994 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3995 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
3996 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3997 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
3998 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3999 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
4000 final Timer unoptimizedScanTimerBg =
4001 u.getBluetoothUnoptimizedScanBackgroundTimer();
4002 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
4003 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4004 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
4005 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
4006
Bookatz867c0d72017-03-07 18:23:42 -08004007 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07004008 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
4009 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
4010 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08004011 }
4012 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004013
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004014 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
4015 u.getBluetoothControllerActivity(), which);
4016
Dianne Hackborn617f8772009-03-31 15:04:46 -07004017 if (u.hasUserActivity()) {
4018 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
4019 boolean hasData = false;
4020 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
4021 int val = u.getUserActivityCount(i, which);
4022 args[i] = val;
4023 if (val != 0) hasData = true;
4024 }
4025 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07004026 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004027 }
4028 }
Bookatzc8c44962017-05-11 12:12:54 -07004029
4030 if (u.getAggregatedPartialWakelockTimer() != null) {
4031 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07004032 // Times are since reset (regardless of 'which')
4033 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07004034 final Timer bgTimer = timer.getSubTimer();
4035 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004036 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07004037 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
4038 }
4039
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004040 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
4041 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4042 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4043 String linePrefix = "";
4044 sb.setLength(0);
4045 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
4046 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004047 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4048 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004049 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004050 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
4051 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004052 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
4053 rawRealtime, "w", which, linePrefix);
4054
Kweku Adams103351f2017-10-16 14:39:34 -07004055 // Only log if we had at least one wakelock...
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004056 if (sb.length() > 0) {
4057 String name = wakelocks.keyAt(iw);
4058 if (name.indexOf(',') >= 0) {
4059 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004060 }
Yi Jin02483362017-08-04 11:30:44 -07004061 if (name.indexOf('\n') >= 0) {
4062 name = name.replace('\n', '_');
4063 }
4064 if (name.indexOf('\r') >= 0) {
4065 name = name.replace('\r', '_');
4066 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004067 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004068 }
4069 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004070
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004071 // WiFi Multicast Wakelock Statistics
4072 final Timer mcTimer = u.getMulticastWakelockStats();
4073 if (mcTimer != null) {
4074 final long totalMcWakelockTimeMs =
4075 mcTimer.getTotalTimeLocked(rawRealtime, which) / 1000 ;
4076 final int countMcWakelock = mcTimer.getCountLocked(which);
4077 if(totalMcWakelockTimeMs > 0) {
4078 dumpLine(pw, uid, category, WIFI_MULTICAST_DATA,
4079 totalMcWakelockTimeMs, countMcWakelock);
4080 }
4081 }
4082
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004083 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4084 for (int isy=syncs.size()-1; isy>=0; isy--) {
4085 final Timer timer = syncs.valueAt(isy);
4086 // Convert from microseconds to milliseconds with rounding
4087 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4088 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07004089 final Timer bgTimer = timer.getSubTimer();
4090 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004091 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07004092 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004093 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004094 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07004095 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004096 }
4097 }
4098
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004099 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4100 for (int ij=jobs.size()-1; ij>=0; ij--) {
4101 final Timer timer = jobs.valueAt(ij);
4102 // Convert from microseconds to milliseconds with rounding
4103 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4104 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07004105 final Timer bgTimer = timer.getSubTimer();
4106 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004107 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07004108 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004109 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004110 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07004111 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004112 }
4113 }
4114
Dianne Hackborn94326cb2017-06-28 16:17:20 -07004115 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
4116 for (int ic=completions.size()-1; ic>=0; ic--) {
4117 SparseIntArray types = completions.valueAt(ic);
4118 if (types != null) {
4119 dumpLine(pw, uid, category, JOB_COMPLETION_DATA,
4120 "\"" + completions.keyAt(ic) + "\"",
4121 types.get(JobParameters.REASON_CANCELED, 0),
4122 types.get(JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, 0),
4123 types.get(JobParameters.REASON_PREEMPT, 0),
4124 types.get(JobParameters.REASON_TIMEOUT, 0),
4125 types.get(JobParameters.REASON_DEVICE_IDLE, 0));
4126 }
4127 }
4128
Amith Yamasani977e11f2018-02-16 11:29:54 -08004129 // Dump deferred jobs stats
4130 u.getDeferredJobsCheckinLineLocked(sb, which);
4131 if (sb.length() > 0) {
4132 dumpLine(pw, uid, category, JOBS_DEFERRED_DATA, sb.toString());
4133 }
4134
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004135 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
4136 rawRealtime, which);
4137 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
4138 rawRealtime, which);
4139 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
4140 rawRealtime, which);
4141 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
4142 rawRealtime, which);
4143
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004144 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4145 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004146 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004147 final Uid.Sensor se = sensors.valueAt(ise);
4148 final int sensorNumber = sensors.keyAt(ise);
4149 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004150 if (timer != null) {
4151 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004152 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4153 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004154 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004155 final int count = timer.getCountLocked(which);
4156 final Timer bgTimer = se.getSensorBackgroundTime();
4157 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004158 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4159 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4160 final long bgActualTime = bgTimer != null ?
4161 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4162 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
4163 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004165 }
4166 }
4167
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004168 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
4169 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004170
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07004171 dumpTimer(pw, uid, category, FOREGROUND_ACTIVITY_DATA, u.getForegroundActivityTimer(),
4172 rawRealtime, which);
4173
4174 dumpTimer(pw, uid, category, FOREGROUND_SERVICE_DATA, u.getForegroundServiceTimer(),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004175 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004176
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004177 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07004178 long totalStateTime = 0;
4179 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08004180 final long time = u.getProcessStateTime(ips, rawRealtime, which);
4181 totalStateTime += time;
4182 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004183 }
4184 if (totalStateTime > 0) {
4185 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
4186 }
4187
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004188 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4189 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004190 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004191 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004192 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004193 }
4194
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004195 // If the cpuFreqs is null, then don't bother checking for cpu freq times.
4196 if (cpuFreqs != null) {
4197 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
4198 // If total cpuFreqTimes is null, then we don't need to check for
4199 // screenOffCpuFreqTimes.
4200 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
4201 sb.setLength(0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004202 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004203 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004204 }
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004205 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
4206 if (screenOffCpuFreqTimeMs != null) {
4207 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
4208 sb.append("," + screenOffCpuFreqTimeMs[i]);
4209 }
4210 } else {
4211 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
4212 sb.append(",0");
4213 }
4214 }
4215 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
4216 cpuFreqTimeMs.length, sb.toString());
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004217 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -08004218
4219 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
4220 final long[] timesMs = u.getCpuFreqTimes(which, procState);
4221 if (timesMs != null && timesMs.length == cpuFreqs.length) {
4222 sb.setLength(0);
4223 for (int i = 0; i < timesMs.length; ++i) {
4224 sb.append((i == 0 ? "" : ",") + timesMs[i]);
4225 }
4226 final long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(
4227 which, procState);
4228 if (screenOffTimesMs != null) {
4229 for (int i = 0; i < screenOffTimesMs.length; ++i) {
4230 sb.append("," + screenOffTimesMs[i]);
4231 }
4232 } else {
4233 for (int i = 0; i < timesMs.length; ++i) {
4234 sb.append(",0");
4235 }
4236 }
4237 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA,
4238 Uid.UID_PROCESS_TYPES[procState], timesMs.length, sb.toString());
4239 }
4240 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004241 }
4242
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004243 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4244 = u.getProcessStats();
4245 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4246 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004247
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004248 final long userMillis = ps.getUserTime(which);
4249 final long systemMillis = ps.getSystemTime(which);
4250 final long foregroundMillis = ps.getForegroundTime(which);
4251 final int starts = ps.getStarts(which);
4252 final int numCrashes = ps.getNumCrashes(which);
4253 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004254
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004255 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
4256 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004257 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
4258 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004259 }
4260 }
4261
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004262 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4263 = u.getPackageStats();
4264 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4265 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4266 int wakeups = 0;
4267 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4268 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07004269 int count = alarms.valueAt(iwa).getCountLocked(which);
4270 wakeups += count;
4271 String name = alarms.keyAt(iwa).replace(',', '_');
4272 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004273 }
4274 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4275 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4276 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4277 final long startTime = ss.getStartTime(batteryUptime, which);
4278 final int starts = ss.getStarts(which);
4279 final int launches = ss.getLaunches(which);
4280 if (startTime != 0 || starts != 0 || launches != 0) {
4281 dumpLine(pw, uid, category, APK_DATA,
4282 wakeups, // wakeup alarms
4283 packageStats.keyAt(ipkg), // Apk
4284 serviceStats.keyAt(isvc), // service
4285 startTime / 1000, // time spent started, in ms
4286 starts,
4287 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004288 }
4289 }
4290 }
4291 }
4292 }
4293
Dianne Hackborn81038902012-11-26 17:04:09 -08004294 static final class TimerEntry {
4295 final String mName;
4296 final int mId;
4297 final BatteryStats.Timer mTimer;
4298 final long mTime;
4299 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
4300 mName = name;
4301 mId = id;
4302 mTimer = timer;
4303 mTime = time;
4304 }
4305 }
4306
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004307 private void printmAh(PrintWriter printer, double power) {
4308 printer.print(BatteryStatsHelper.makemAh(power));
4309 }
4310
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004311 private void printmAh(StringBuilder sb, double power) {
4312 sb.append(BatteryStatsHelper.makemAh(power));
4313 }
4314
Dianne Hackbornd953c532014-08-16 18:17:38 -07004315 /**
4316 * Temporary for settings.
4317 */
4318 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
4319 int reqUid) {
4320 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
4321 }
4322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004324 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07004325 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004326 final long rawUptime = SystemClock.uptimeMillis() * 1000;
4327 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07004328 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004330
4331 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
4332 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
4333 final long totalRealtime = computeRealtime(rawRealtime, which);
4334 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004335 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
4336 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
4337 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004338 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
4339 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -07004340 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004341
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004342 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07004343
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004344 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07004345 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004346
Adam Lesinskif9b20a92016-06-17 17:30:01 -07004347 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
4348 if (estimatedBatteryCapacity > 0) {
4349 sb.setLength(0);
4350 sb.append(prefix);
4351 sb.append(" Estimated battery capacity: ");
4352 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
4353 sb.append(" mAh");
4354 pw.println(sb.toString());
4355 }
4356
Jocelyn Dangc627d102017-04-14 13:15:14 -07004357 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
4358 if (minLearnedBatteryCapacity > 0) {
4359 sb.setLength(0);
4360 sb.append(prefix);
4361 sb.append(" Min learned battery capacity: ");
4362 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
4363 sb.append(" mAh");
4364 pw.println(sb.toString());
4365 }
4366 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
4367 if (maxLearnedBatteryCapacity > 0) {
4368 sb.setLength(0);
4369 sb.append(prefix);
4370 sb.append(" Max learned battery capacity: ");
4371 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
4372 sb.append(" mAh");
4373 pw.println(sb.toString());
4374 }
4375
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004376 sb.setLength(0);
4377 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004378 sb.append(" Time on battery: ");
4379 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
4380 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
4381 sb.append(") realtime, ");
4382 formatTimeMs(sb, whichBatteryUptime / 1000);
4383 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, whichBatteryRealtime));
4384 sb.append(") uptime");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004385 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004386
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004387 sb.setLength(0);
4388 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004389 sb.append(" Time on battery screen off: ");
4390 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
4391 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, whichBatteryRealtime));
4392 sb.append(") realtime, ");
4393 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
4394 sb.append("(");
4395 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, whichBatteryRealtime));
4396 sb.append(") uptime");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004397 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004398
4399 sb.setLength(0);
4400 sb.append(prefix);
4401 sb.append(" Time on battery screen doze: ");
4402 formatTimeMs(sb, screenDozeTime / 1000); sb.append("(");
4403 sb.append(formatRatioLocked(screenDozeTime, whichBatteryRealtime));
4404 sb.append(")");
4405 pw.println(sb.toString());
4406
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004407 sb.setLength(0);
4408 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004409 sb.append(" Total run time: ");
4410 formatTimeMs(sb, totalRealtime / 1000);
4411 sb.append("realtime, ");
4412 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004413 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07004414 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004415 if (batteryTimeRemaining >= 0) {
4416 sb.setLength(0);
4417 sb.append(prefix);
4418 sb.append(" Battery time remaining: ");
4419 formatTimeMs(sb, batteryTimeRemaining / 1000);
4420 pw.println(sb.toString());
4421 }
4422 if (chargeTimeRemaining >= 0) {
4423 sb.setLength(0);
4424 sb.append(prefix);
4425 sb.append(" Charge time remaining: ");
4426 formatTimeMs(sb, chargeTimeRemaining / 1000);
4427 pw.println(sb.toString());
4428 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004429
Kweku Adams87b19ec2017-10-09 12:40:03 -07004430 final long dischargeCount = getUahDischarge(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004431 if (dischargeCount >= 0) {
4432 sb.setLength(0);
4433 sb.append(prefix);
4434 sb.append(" Discharge: ");
4435 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
4436 sb.append(" mAh");
4437 pw.println(sb.toString());
4438 }
4439
Kweku Adams87b19ec2017-10-09 12:40:03 -07004440 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004441 if (dischargeScreenOffCount >= 0) {
4442 sb.setLength(0);
4443 sb.append(prefix);
4444 sb.append(" Screen off discharge: ");
4445 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
4446 sb.append(" mAh");
4447 pw.println(sb.toString());
4448 }
4449
Kweku Adams87b19ec2017-10-09 12:40:03 -07004450 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Mac2f518a2017-09-19 16:06:03 -07004451 if (dischargeScreenDozeCount >= 0) {
4452 sb.setLength(0);
4453 sb.append(prefix);
4454 sb.append(" Screen doze discharge: ");
4455 sb.append(BatteryStatsHelper.makemAh(dischargeScreenDozeCount / 1000.0));
4456 sb.append(" mAh");
4457 pw.println(sb.toString());
4458 }
4459
Mike Ma90902652018-04-17 14:07:24 -07004460 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004461 if (dischargeScreenOnCount >= 0) {
4462 sb.setLength(0);
4463 sb.append(prefix);
4464 sb.append(" Screen on discharge: ");
4465 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
4466 sb.append(" mAh");
4467 pw.println(sb.toString());
4468 }
4469
Mike Ma15313c92017-11-15 17:58:21 -08004470 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
4471 if (dischargeLightDozeCount >= 0) {
4472 sb.setLength(0);
4473 sb.append(prefix);
4474 sb.append(" Device light doze discharge: ");
4475 sb.append(BatteryStatsHelper.makemAh(dischargeLightDozeCount / 1000.0));
4476 sb.append(" mAh");
4477 pw.println(sb.toString());
4478 }
4479
4480 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
4481 if (dischargeDeepDozeCount >= 0) {
4482 sb.setLength(0);
4483 sb.append(prefix);
4484 sb.append(" Device deep doze discharge: ");
4485 sb.append(BatteryStatsHelper.makemAh(dischargeDeepDozeCount / 1000.0));
4486 sb.append(" mAh");
4487 pw.println(sb.toString());
4488 }
4489
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004490 pw.print(" Start clock time: ");
4491 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
4492
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004493 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004494 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004495 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004496 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
4497 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004498 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004499 rawRealtime, which);
4500 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
4501 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004502 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004503 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004504 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
4505 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
4506 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004507 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004508 sb.append(prefix);
4509 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
4510 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004511 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004512 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
4513 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004514 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004515 pw.println(sb.toString());
4516 sb.setLength(0);
4517 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004518 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004519 boolean didOne = false;
4520 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004521 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004522 if (time == 0) {
4523 continue;
4524 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004525 sb.append("\n ");
4526 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004527 didOne = true;
4528 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
4529 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004530 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004531 sb.append("(");
4532 sb.append(formatRatioLocked(time, screenOnTime));
4533 sb.append(")");
4534 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004535 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004536 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004537 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004538 sb.setLength(0);
4539 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004540 sb.append(" Power save mode enabled: ");
4541 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004542 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004543 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004544 sb.append(")");
4545 pw.println(sb.toString());
4546 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004547 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004548 sb.setLength(0);
4549 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004550 sb.append(" Device light idling: ");
4551 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004552 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004553 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
4554 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004555 sb.append("x");
4556 pw.println(sb.toString());
4557 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004558 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004559 sb.setLength(0);
4560 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004561 sb.append(" Idle mode light time: ");
4562 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004563 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004564 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
4565 sb.append(") ");
4566 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004567 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004568 sb.append(" -- longest ");
4569 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
4570 pw.println(sb.toString());
4571 }
4572 if (deviceIdlingTime != 0) {
4573 sb.setLength(0);
4574 sb.append(prefix);
4575 sb.append(" Device full idling: ");
4576 formatTimeMs(sb, deviceIdlingTime / 1000);
4577 sb.append("(");
4578 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004579 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004580 sb.append("x");
4581 pw.println(sb.toString());
4582 }
4583 if (deviceIdleModeFullTime != 0) {
4584 sb.setLength(0);
4585 sb.append(prefix);
4586 sb.append(" Idle mode full time: ");
4587 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
4588 sb.append("(");
4589 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
4590 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004591 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004592 sb.append("x");
4593 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004594 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004595 pw.println(sb.toString());
4596 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004597 if (phoneOnTime != 0) {
4598 sb.setLength(0);
4599 sb.append(prefix);
4600 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
4601 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004602 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004603 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004604 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004605 if (connChanges != 0) {
4606 pw.print(prefix);
4607 pw.print(" Connectivity changes: "); pw.println(connChanges);
4608 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004609
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004610 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07004611 long fullWakeLockTimeTotalMicros = 0;
4612 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08004613
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004614 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08004615
Evan Millar22ac0432009-03-31 11:33:18 -07004616 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004617 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004618
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004619 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4620 = u.getWakelockStats();
4621 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4622 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07004623
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004624 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
4625 if (fullWakeTimer != null) {
4626 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
4627 rawRealtime, which);
4628 }
4629
4630 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4631 if (partialWakeTimer != null) {
4632 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
4633 rawRealtime, which);
4634 if (totalTimeMicros > 0) {
4635 if (reqUid < 0) {
4636 // Only show the ordered list of all wake
4637 // locks if the caller is not asking for data
4638 // about a specific uid.
4639 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
4640 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08004641 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004642 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07004643 }
4644 }
4645 }
4646 }
Bookatzc8c44962017-05-11 12:12:54 -07004647
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004648 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4649 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4650 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4651 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4652 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4653 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4654 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4655 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004656 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4657 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004658
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004659 if (fullWakeLockTimeTotalMicros != 0) {
4660 sb.setLength(0);
4661 sb.append(prefix);
4662 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4663 (fullWakeLockTimeTotalMicros + 500) / 1000);
4664 pw.println(sb.toString());
4665 }
4666
4667 if (partialWakeLockTimeTotalMicros != 0) {
4668 sb.setLength(0);
4669 sb.append(prefix);
4670 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4671 (partialWakeLockTimeTotalMicros + 500) / 1000);
4672 pw.println(sb.toString());
4673 }
4674
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004675 final long multicastWakeLockTimeTotalMicros =
4676 getWifiMulticastWakelockTime(rawRealtime, which);
4677 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004678 if (multicastWakeLockTimeTotalMicros != 0) {
4679 sb.setLength(0);
4680 sb.append(prefix);
4681 sb.append(" Total WiFi Multicast wakelock Count: ");
4682 sb.append(multicastWakeLockCountTotal);
4683 pw.println(sb.toString());
4684
4685 sb.setLength(0);
4686 sb.append(prefix);
4687 sb.append(" Total WiFi Multicast wakelock time: ");
4688 formatTimeMsNoSpace(sb, (multicastWakeLockTimeTotalMicros + 500) / 1000);
4689 pw.println(sb.toString());
4690 }
4691
Siddharth Ray3c648c42017-10-02 17:30:58 -07004692 pw.println("");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004693 pw.print(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004694 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004695 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004696 sb.append(" CONNECTIVITY POWER SUMMARY START");
4697 pw.println(sb.toString());
4698
4699 pw.print(prefix);
4700 sb.setLength(0);
4701 sb.append(prefix);
4702 sb.append(" Logging duration for connectivity statistics: ");
4703 formatTimeMs(sb, whichBatteryRealtime / 1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004704 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004705
4706 sb.setLength(0);
4707 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004708 sb.append(" Cellular Statistics:");
Amith Yamasanif37447b2009-10-08 18:28:01 -07004709 pw.println(sb.toString());
4710
Siddharth Ray3c648c42017-10-02 17:30:58 -07004711 pw.print(prefix);
4712 sb.setLength(0);
4713 sb.append(prefix);
4714 sb.append(" Cellular kernel active time: ");
4715 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
4716 formatTimeMs(sb, mobileActiveTime / 1000);
4717 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4718 sb.append(")");
4719 pw.println(sb.toString());
4720
4721 pw.print(" Cellular data received: "); pw.println(formatBytesLocked(mobileRxTotalBytes));
4722 pw.print(" Cellular data sent: "); pw.println(formatBytesLocked(mobileTxTotalBytes));
4723 pw.print(" Cellular packets received: "); pw.println(mobileRxTotalPackets);
4724 pw.print(" Cellular packets sent: "); pw.println(mobileTxTotalPackets);
4725
Dianne Hackborn627bba72009-03-24 22:32:56 -07004726 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004727 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004728 sb.append(" Cellular Radio Access Technology:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004729 didOne = false;
4730 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004731 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004732 if (time == 0) {
4733 continue;
4734 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004735 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004736 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004737 didOne = true;
Bookatzdf6baff2018-12-12 19:38:11 -08004738 sb.append(i < DATA_CONNECTION_NAMES.length ? DATA_CONNECTION_NAMES[i] : "ERROR");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004739 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004740 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004741 sb.append("(");
4742 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004743 sb.append(") ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004744 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004745 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004746 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004747
4748 sb.setLength(0);
4749 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004750 sb.append(" Cellular Rx signal strength (RSRP):");
4751 final String[] cellularRxSignalStrengthDescription = new String[]{
4752 "very poor (less than -128dBm): ",
4753 "poor (-128dBm to -118dBm): ",
4754 "moderate (-118dBm to -108dBm): ",
4755 "good (-108dBm to -98dBm): ",
4756 "great (greater than -98dBm): "};
4757 didOne = false;
4758 final int numCellularRxBins = Math.min(SignalStrength.NUM_SIGNAL_STRENGTH_BINS,
4759 cellularRxSignalStrengthDescription.length);
4760 for (int i=0; i<numCellularRxBins; i++) {
4761 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
4762 if (time == 0) {
4763 continue;
4764 }
4765 sb.append("\n ");
4766 sb.append(prefix);
4767 didOne = true;
4768 sb.append(cellularRxSignalStrengthDescription[i]);
4769 sb.append(" ");
4770 formatTimeMs(sb, time/1000);
4771 sb.append("(");
4772 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4773 sb.append(") ");
4774 }
4775 if (!didOne) sb.append(" (no activity)");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004776 pw.println(sb.toString());
4777
Siddharth Rayb50a6842017-12-14 15:15:28 -08004778 printControllerActivity(pw, sb, prefix, CELLULAR_CONTROLLER_NAME,
Siddharth Ray3c648c42017-10-02 17:30:58 -07004779 getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004780
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004781 pw.print(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004782 sb.setLength(0);
4783 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004784 sb.append(" Wifi Statistics:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004785 pw.println(sb.toString());
4786
Siddharth Rayb50a6842017-12-14 15:15:28 -08004787 pw.print(prefix);
4788 sb.setLength(0);
4789 sb.append(prefix);
4790 sb.append(" Wifi kernel active time: ");
4791 final long wifiActiveTime = getWifiActiveTime(rawRealtime, which);
4792 formatTimeMs(sb, wifiActiveTime / 1000);
4793 sb.append("("); sb.append(formatRatioLocked(wifiActiveTime, whichBatteryRealtime));
4794 sb.append(")");
4795 pw.println(sb.toString());
4796
Siddharth Ray3c648c42017-10-02 17:30:58 -07004797 pw.print(" Wifi data received: "); pw.println(formatBytesLocked(wifiRxTotalBytes));
4798 pw.print(" Wifi data sent: "); pw.println(formatBytesLocked(wifiTxTotalBytes));
4799 pw.print(" Wifi packets received: "); pw.println(wifiRxTotalPackets);
4800 pw.print(" Wifi packets sent: "); pw.println(wifiTxTotalPackets);
4801
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004802 sb.setLength(0);
4803 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004804 sb.append(" Wifi states:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004805 didOne = false;
4806 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004807 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004808 if (time == 0) {
4809 continue;
4810 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004811 sb.append("\n ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004812 didOne = true;
4813 sb.append(WIFI_STATE_NAMES[i]);
4814 sb.append(" ");
4815 formatTimeMs(sb, time/1000);
4816 sb.append("(");
4817 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4818 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004819 }
4820 if (!didOne) sb.append(" (no activity)");
4821 pw.println(sb.toString());
4822
4823 sb.setLength(0);
4824 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004825 sb.append(" Wifi supplicant states:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004826 didOne = false;
4827 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4828 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4829 if (time == 0) {
4830 continue;
4831 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004832 sb.append("\n ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004833 didOne = true;
4834 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4835 sb.append(" ");
4836 formatTimeMs(sb, time/1000);
4837 sb.append("(");
4838 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4839 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004840 }
4841 if (!didOne) sb.append(" (no activity)");
4842 pw.println(sb.toString());
4843
4844 sb.setLength(0);
4845 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004846 sb.append(" Wifi Rx signal strength (RSSI):");
4847 final String[] wifiRxSignalStrengthDescription = new String[]{
4848 "very poor (less than -88.75dBm): ",
4849 "poor (-88.75 to -77.5dBm): ",
4850 "moderate (-77.5dBm to -66.25dBm): ",
4851 "good (-66.25dBm to -55dBm): ",
4852 "great (greater than -55dBm): "};
Dianne Hackborn3251b902014-06-20 14:40:53 -07004853 didOne = false;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004854 final int numWifiRxBins = Math.min(NUM_WIFI_SIGNAL_STRENGTH_BINS,
4855 wifiRxSignalStrengthDescription.length);
4856 for (int i=0; i<numWifiRxBins; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004857 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4858 if (time == 0) {
4859 continue;
4860 }
4861 sb.append("\n ");
4862 sb.append(prefix);
4863 didOne = true;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004864 sb.append(" ");
4865 sb.append(wifiRxSignalStrengthDescription[i]);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004866 formatTimeMs(sb, time/1000);
4867 sb.append("(");
4868 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4869 sb.append(") ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004870 }
4871 if (!didOne) sb.append(" (no activity)");
4872 pw.println(sb.toString());
4873
Siddharth Rayb50a6842017-12-14 15:15:28 -08004874 printControllerActivity(pw, sb, prefix, WIFI_CONTROLLER_NAME,
4875 getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004876
Adam Lesinski50e47602015-12-04 17:04:54 -08004877 pw.print(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004878 sb.setLength(0);
4879 sb.append(prefix);
Siddharth Ray78ccaf52017-12-23 16:16:21 -08004880 sb.append(" GPS Statistics:");
4881 pw.println(sb.toString());
4882
4883 sb.setLength(0);
4884 sb.append(prefix);
4885 sb.append(" GPS signal quality (Top 4 Average CN0):");
4886 final String[] gpsSignalQualityDescription = new String[]{
4887 "poor (less than 20 dBHz): ",
4888 "good (greater than 20 dBHz): "};
4889 final int numGpsSignalQualityBins = Math.min(GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS,
4890 gpsSignalQualityDescription.length);
4891 for (int i=0; i<numGpsSignalQualityBins; i++) {
4892 final long time = getGpsSignalQualityTime(i, rawRealtime, which);
4893 sb.append("\n ");
4894 sb.append(prefix);
4895 sb.append(" ");
4896 sb.append(gpsSignalQualityDescription[i]);
4897 formatTimeMs(sb, time/1000);
4898 sb.append("(");
4899 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4900 sb.append(") ");
4901 }
4902 pw.println(sb.toString());
4903
4904 final long gpsBatteryDrainMaMs = getGpsBatteryDrainMaMs();
4905 if (gpsBatteryDrainMaMs > 0) {
4906 pw.print(prefix);
4907 sb.setLength(0);
4908 sb.append(prefix);
4909 sb.append(" Battery Drain (mAh): ");
4910 sb.append(Double.toString(((double) gpsBatteryDrainMaMs)/(3600 * 1000)));
4911 pw.println(sb.toString());
4912 }
4913
4914 pw.print(prefix);
4915 sb.setLength(0);
4916 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004917 sb.append(" CONNECTIVITY POWER SUMMARY END");
4918 pw.println(sb.toString());
4919 pw.println("");
4920
4921 pw.print(prefix);
Adam Lesinski50e47602015-12-04 17:04:54 -08004922 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4923 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4924
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004925 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4926 sb.setLength(0);
4927 sb.append(prefix);
4928 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4929 pw.println(sb.toString());
4930
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004931 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4932 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004933
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004934 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004935
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004936 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004937 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004938 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004939 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004940 pw.println(getDischargeStartLevel());
4941 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4942 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004943 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004944 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004945 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004946 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004947 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004948 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004949 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004950 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004951 pw.println(getDischargeAmountScreenOn());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004952 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004953 pw.println(getDischargeAmountScreenOff());
4954 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4955 pw.println(getDischargeAmountScreenDoze());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004956 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004957 } else {
4958 pw.print(prefix); pw.println(" Device battery use since last full charge");
4959 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004960 pw.println(getLowDischargeAmountSinceCharge());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004961 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004962 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004963 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004964 pw.println(getDischargeAmountScreenOnSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004965 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004966 pw.println(getDischargeAmountScreenOffSinceCharge());
4967 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4968 pw.println(getDischargeAmountScreenDozeSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004969 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004970 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004971
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004972 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004973 helper.create(this);
4974 helper.refreshStats(which, UserHandle.USER_ALL);
4975 List<BatterySipper> sippers = helper.getUsageList();
4976 if (sippers != null && sippers.size() > 0) {
4977 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4978 pw.print(prefix); pw.print(" Capacity: ");
4979 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004980 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004981 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4982 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4983 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4984 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004985 pw.println();
4986 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004987 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004988 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004989 switch (bs.drainType) {
Kweku Adamsef728952018-04-10 17:56:16 -07004990 case AMBIENT_DISPLAY:
4991 pw.print(" Ambient display: ");
4992 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004993 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004994 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004995 break;
4996 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004997 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004998 break;
4999 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005000 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005001 break;
5002 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005003 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005004 break;
5005 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005006 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005007 break;
5008 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005009 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005010 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005011 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005012 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005013 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005014 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005015 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005016 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005017 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005018 break;
5019 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005020 pw.print(" User "); pw.print(bs.userId);
5021 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005022 break;
5023 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005024 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005025 break;
5026 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005027 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005028 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005029 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005030 pw.print(" Camera: ");
5031 break;
5032 default:
5033 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005034 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005035 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005036 printmAh(pw, bs.totalPowerMah);
5037
Adam Lesinski57123002015-06-12 16:12:07 -07005038 if (bs.usagePowerMah != bs.totalPowerMah) {
5039 // If the usage (generic power) isn't the whole amount, we list out
5040 // what components are involved in the calculation.
5041
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005042 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07005043 if (bs.usagePowerMah != 0) {
5044 pw.print(" usage=");
5045 printmAh(pw, bs.usagePowerMah);
5046 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005047 if (bs.cpuPowerMah != 0) {
5048 pw.print(" cpu=");
5049 printmAh(pw, bs.cpuPowerMah);
5050 }
5051 if (bs.wakeLockPowerMah != 0) {
5052 pw.print(" wake=");
5053 printmAh(pw, bs.wakeLockPowerMah);
5054 }
5055 if (bs.mobileRadioPowerMah != 0) {
5056 pw.print(" radio=");
5057 printmAh(pw, bs.mobileRadioPowerMah);
5058 }
5059 if (bs.wifiPowerMah != 0) {
5060 pw.print(" wifi=");
5061 printmAh(pw, bs.wifiPowerMah);
5062 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005063 if (bs.bluetoothPowerMah != 0) {
5064 pw.print(" bt=");
5065 printmAh(pw, bs.bluetoothPowerMah);
5066 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005067 if (bs.gpsPowerMah != 0) {
5068 pw.print(" gps=");
5069 printmAh(pw, bs.gpsPowerMah);
5070 }
5071 if (bs.sensorPowerMah != 0) {
5072 pw.print(" sensor=");
5073 printmAh(pw, bs.sensorPowerMah);
5074 }
5075 if (bs.cameraPowerMah != 0) {
5076 pw.print(" camera=");
5077 printmAh(pw, bs.cameraPowerMah);
5078 }
5079 if (bs.flashlightPowerMah != 0) {
5080 pw.print(" flash=");
5081 printmAh(pw, bs.flashlightPowerMah);
5082 }
5083 pw.print(" )");
5084 }
Bookatz17d7d9d2017-06-08 14:50:46 -07005085
5086 // If there is additional smearing information, include it.
5087 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
5088 pw.print(" Including smearing: ");
5089 printmAh(pw, bs.totalSmearedPowerMah);
5090 pw.print(" (");
5091 if (bs.screenPowerMah != 0) {
5092 pw.print(" screen=");
5093 printmAh(pw, bs.screenPowerMah);
5094 }
5095 if (bs.proportionalSmearMah != 0) {
5096 pw.print(" proportional=");
5097 printmAh(pw, bs.proportionalSmearMah);
5098 }
5099 pw.print(" )");
5100 }
5101 if (bs.shouldHide) {
5102 pw.print(" Excluded from smearing");
5103 }
5104
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005105 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005106 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08005107 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005108 }
5109
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005110 sippers = helper.getMobilemsppList();
5111 if (sippers != null && sippers.size() > 0) {
5112 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005113 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005114 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005115 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005116 sb.setLength(0);
5117 sb.append(prefix); sb.append(" Uid ");
5118 UserHandle.formatUid(sb, bs.uidObj.getUid());
5119 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
5120 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
5121 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005122 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005123 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005124 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005125 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005126 sb.setLength(0);
5127 sb.append(prefix);
5128 sb.append(" TOTAL TIME: ");
5129 formatTimeMs(sb, totalTime);
5130 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
5131 sb.append(")");
5132 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005133 pw.println();
5134 }
5135
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005136 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
5137 @Override
5138 public int compare(TimerEntry lhs, TimerEntry rhs) {
5139 long lhsTime = lhs.mTime;
5140 long rhsTime = rhs.mTime;
5141 if (lhsTime < rhsTime) {
5142 return 1;
5143 }
5144 if (lhsTime > rhsTime) {
5145 return -1;
5146 }
5147 return 0;
5148 }
5149 };
5150
5151 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005152 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
5153 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005154 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005155 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
5156 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
5157 : kernelWakelocks.entrySet()) {
5158 final BatteryStats.Timer timer = ent.getValue();
5159 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005160 if (totalTimeMillis > 0) {
5161 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
5162 }
5163 }
5164 if (ktimers.size() > 0) {
5165 Collections.sort(ktimers, timerComparator);
5166 pw.print(prefix); pw.println(" All kernel wake locks:");
5167 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005168 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005169 String linePrefix = ": ";
5170 sb.setLength(0);
5171 sb.append(prefix);
5172 sb.append(" Kernel Wake lock ");
5173 sb.append(timer.mName);
5174 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
5175 which, linePrefix);
5176 if (!linePrefix.equals(": ")) {
5177 sb.append(" realtime");
5178 // Only print out wake locks that were held
5179 pw.println(sb.toString());
5180 }
5181 }
5182 pw.println();
5183 }
5184 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005185
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005186 if (timers.size() > 0) {
5187 Collections.sort(timers, timerComparator);
5188 pw.print(prefix); pw.println(" All partial wake locks:");
5189 for (int i=0; i<timers.size(); i++) {
5190 TimerEntry timer = timers.get(i);
5191 sb.setLength(0);
5192 sb.append(" Wake lock ");
5193 UserHandle.formatUid(sb, timer.mId);
5194 sb.append(" ");
5195 sb.append(timer.mName);
5196 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5197 sb.append(" realtime");
5198 pw.println(sb.toString());
5199 }
5200 timers.clear();
5201 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08005202 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005203
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005204 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005205 if (wakeupReasons.size() > 0) {
5206 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005207 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005208 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005209 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005210 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
5211 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005212 }
5213 Collections.sort(reasons, timerComparator);
5214 for (int i=0; i<reasons.size(); i++) {
5215 TimerEntry timer = reasons.get(i);
5216 String linePrefix = ": ";
5217 sb.setLength(0);
5218 sb.append(prefix);
5219 sb.append(" Wakeup reason ");
5220 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005221 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5222 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005223 pw.println(sb.toString());
5224 }
5225 pw.println();
5226 }
Dianne Hackborn81038902012-11-26 17:04:09 -08005227 }
Evan Millar22ac0432009-03-31 11:33:18 -07005228
James Carr2dd7e5e2016-07-20 18:48:39 -07005229 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
Bookatz50df7112017-08-04 14:53:26 -07005230 if (mMemoryStats.size() > 0) {
5231 pw.println(" Memory Stats");
5232 for (int i = 0; i < mMemoryStats.size(); i++) {
5233 sb.setLength(0);
5234 sb.append(" Bandwidth ");
5235 sb.append(mMemoryStats.keyAt(i));
5236 sb.append(" Time ");
5237 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
5238 pw.println(sb.toString());
5239 }
5240 pw.println();
5241 }
5242
5243 final Map<String, ? extends Timer> rpmStats = getRpmStats();
5244 if (rpmStats.size() > 0) {
5245 pw.print(prefix); pw.println(" Resource Power Manager Stats");
5246 if (rpmStats.size() > 0) {
5247 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
5248 final String timerName = ent.getKey();
5249 final Timer timer = ent.getValue();
5250 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5251 }
5252 }
5253 pw.println();
5254 }
Bookatz82b341172017-09-07 19:06:08 -07005255 if (SCREEN_OFF_RPM_STATS_ENABLED) {
5256 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
Bookatz50df7112017-08-04 14:53:26 -07005257 if (screenOffRpmStats.size() > 0) {
Bookatz82b341172017-09-07 19:06:08 -07005258 pw.print(prefix);
5259 pw.println(" Resource Power Manager Stats for when screen was off");
5260 if (screenOffRpmStats.size() > 0) {
5261 for (Map.Entry<String, ? extends Timer> ent : screenOffRpmStats.entrySet()) {
5262 final String timerName = ent.getKey();
5263 final Timer timer = ent.getValue();
5264 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5265 }
Bookatz50df7112017-08-04 14:53:26 -07005266 }
Bookatz82b341172017-09-07 19:06:08 -07005267 pw.println();
Bookatz50df7112017-08-04 14:53:26 -07005268 }
James Carr2dd7e5e2016-07-20 18:48:39 -07005269 }
5270
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005271 final long[] cpuFreqs = getCpuFreqs();
5272 if (cpuFreqs != null) {
5273 sb.setLength(0);
Bookatz50df7112017-08-04 14:53:26 -07005274 sb.append(" CPU freqs:");
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005275 for (int i = 0; i < cpuFreqs.length; ++i) {
5276 sb.append(" " + cpuFreqs[i]);
5277 }
5278 pw.println(sb.toString());
Bookatz50df7112017-08-04 14:53:26 -07005279 pw.println();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005280 }
5281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005282 for (int iu=0; iu<NU; iu++) {
5283 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005284 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005285 continue;
5286 }
Bookatzc8c44962017-05-11 12:12:54 -07005287
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005288 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07005289
5290 pw.print(prefix);
5291 pw.print(" ");
5292 UserHandle.formatUid(pw, uid);
5293 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005294 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005295
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005296 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
5297 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
5298 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
5299 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005300 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
5301 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
5302
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005303 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
5304 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005305 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
5306 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005307
5308 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
5309 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
5310
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005311 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
5312 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
5313 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08005314 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
5315 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5316 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
5317 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005318 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005319
Adam Lesinski5f056f62016-07-14 16:56:08 -07005320 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
5321 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
5322
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005323 if (mobileRxBytes > 0 || mobileTxBytes > 0
5324 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005325 pw.print(prefix); pw.print(" Mobile network: ");
5326 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005327 pw.print(formatBytesLocked(mobileTxBytes));
5328 pw.print(" sent (packets "); pw.print(mobileRxPackets);
5329 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005330 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005331 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
5332 sb.setLength(0);
5333 sb.append(prefix); sb.append(" Mobile radio active: ");
5334 formatTimeMs(sb, uidMobileActiveTime / 1000);
5335 sb.append("(");
5336 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
5337 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
5338 long packets = mobileRxPackets + mobileTxPackets;
5339 if (packets == 0) {
5340 packets = 1;
5341 }
5342 sb.append(" @ ");
5343 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
5344 sb.append(" mspp");
5345 pw.println(sb.toString());
5346 }
5347
Adam Lesinski5f056f62016-07-14 16:56:08 -07005348 if (mobileWakeup > 0) {
5349 sb.setLength(0);
5350 sb.append(prefix);
5351 sb.append(" Mobile radio AP wakeups: ");
5352 sb.append(mobileWakeup);
5353 pw.println(sb.toString());
5354 }
5355
Siddharth Rayb50a6842017-12-14 15:15:28 -08005356 printControllerActivityIfInteresting(pw, sb, prefix + " ",
5357 CELLULAR_CONTROLLER_NAME, u.getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005358
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005359 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005360 pw.print(prefix); pw.print(" Wi-Fi network: ");
5361 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005362 pw.print(formatBytesLocked(wifiTxBytes));
5363 pw.print(" sent (packets "); pw.print(wifiRxPackets);
5364 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005365 }
5366
Dianne Hackborn62793e42015-03-09 11:15:41 -07005367 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08005368 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005369 || uidWifiRunningTime != 0) {
5370 sb.setLength(0);
5371 sb.append(prefix); sb.append(" Wifi Running: ");
5372 formatTimeMs(sb, uidWifiRunningTime / 1000);
5373 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
5374 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07005375 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005376 formatTimeMs(sb, fullWifiLockOnTime / 1000);
5377 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
5378 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08005379 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005380 formatTimeMs(sb, wifiScanTime / 1000);
5381 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07005382 whichBatteryRealtime)); sb.append(") ");
5383 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08005384 sb.append("x\n");
5385 // actual and background times are unpooled and since reset (regardless of 'which')
5386 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
5387 formatTimeMs(sb, wifiScanActualTime / 1000);
5388 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
5389 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5390 sb.append(") ");
5391 sb.append(wifiScanCount);
5392 sb.append("x\n");
5393 sb.append(prefix); sb.append(" Background Wifi Scan: ");
5394 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
5395 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
5396 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5397 sb.append(") ");
5398 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07005399 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005400 pw.println(sb.toString());
5401 }
5402
Adam Lesinski5f056f62016-07-14 16:56:08 -07005403 if (wifiWakeup > 0) {
5404 sb.setLength(0);
5405 sb.append(prefix);
5406 sb.append(" WiFi AP wakeups: ");
5407 sb.append(wifiWakeup);
5408 pw.println(sb.toString());
5409 }
5410
Siddharth Rayb50a6842017-12-14 15:15:28 -08005411 printControllerActivityIfInteresting(pw, sb, prefix + " ", WIFI_CONTROLLER_NAME,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005412 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07005413
Adam Lesinski50e47602015-12-04 17:04:54 -08005414 if (btRxBytes > 0 || btTxBytes > 0) {
5415 pw.print(prefix); pw.print(" Bluetooth network: ");
5416 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
5417 pw.print(formatBytesLocked(btTxBytes));
5418 pw.println(" sent");
5419 }
5420
Bookatz867c0d72017-03-07 18:23:42 -08005421 final Timer bleTimer = u.getBluetoothScanTimer();
5422 if (bleTimer != null) {
5423 // Convert from microseconds to milliseconds with rounding
5424 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
5425 / 1000;
5426 if (totalTimeMs != 0) {
5427 final int count = bleTimer.getCountLocked(which);
5428 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
5429 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005430 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5431 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
5432 final long actualTimeMsBg = bleTimerBg != null ?
5433 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005434 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07005435 final int resultCount = u.getBluetoothScanResultCounter() != null ?
5436 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005437 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
5438 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
5439 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
5440 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
5441 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
5442 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5443 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
5444 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
5445 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
5446 final Timer unoptimizedScanTimerBg =
5447 u.getBluetoothUnoptimizedScanBackgroundTimer();
5448 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
5449 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5450 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
5451 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005452
5453 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08005454 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07005455 sb.append(prefix);
5456 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08005457 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07005458 sb.append(" (");
5459 sb.append(count);
5460 sb.append(" times)");
5461 if (bleTimer.isRunningLocked()) {
5462 sb.append(" (currently running)");
5463 }
5464 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005465 }
Bookatzb1f04f32017-05-19 13:57:32 -07005466
5467 sb.append(prefix);
5468 sb.append(" Bluetooth Scan (total actual realtime): ");
5469 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
5470 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005471 sb.append(count);
5472 sb.append(" times)");
5473 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07005474 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08005475 }
Bookatzb1f04f32017-05-19 13:57:32 -07005476 sb.append("\n");
5477 if (actualTimeMsBg > 0 || countBg > 0) {
5478 sb.append(prefix);
5479 sb.append(" Bluetooth Scan (background realtime): ");
5480 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
5481 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005482 sb.append(countBg);
5483 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07005484 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
5485 sb.append(" (currently running in background)");
5486 }
5487 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005488 }
Bookatzb1f04f32017-05-19 13:57:32 -07005489
5490 sb.append(prefix);
5491 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07005492 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07005493 sb.append(" (");
5494 sb.append(resultCountBg);
5495 sb.append(" in background)");
5496
5497 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
5498 sb.append("\n");
5499 sb.append(prefix);
5500 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
5501 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
5502 sb.append(" (max ");
5503 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
5504 sb.append(")");
5505 if (unoptimizedScanTimer != null
5506 && unoptimizedScanTimer.isRunningLocked()) {
5507 sb.append(" (currently running unoptimized)");
5508 }
5509 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
5510 sb.append("\n");
5511 sb.append(prefix);
5512 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
5513 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
5514 sb.append(" (max ");
5515 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
5516 sb.append(")");
5517 if (unoptimizedScanTimerBg.isRunningLocked()) {
5518 sb.append(" (currently running unoptimized in background)");
5519 }
5520 }
5521 }
Bookatz867c0d72017-03-07 18:23:42 -08005522 pw.println(sb.toString());
5523 uidActivity = true;
5524 }
5525 }
5526
5527
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005528
Dianne Hackborn617f8772009-03-31 15:04:46 -07005529 if (u.hasUserActivity()) {
5530 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07005531 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005532 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005533 if (val != 0) {
5534 if (!hasData) {
5535 sb.setLength(0);
5536 sb.append(" User activity: ");
5537 hasData = true;
5538 } else {
5539 sb.append(", ");
5540 }
5541 sb.append(val);
5542 sb.append(" ");
5543 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
5544 }
5545 }
5546 if (hasData) {
5547 pw.println(sb.toString());
5548 }
5549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005550
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005551 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
5552 = u.getWakelockStats();
5553 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005554 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005555 int countWakelock = 0;
5556 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
5557 final Uid.Wakelock wl = wakelocks.valueAt(iw);
5558 String linePrefix = ": ";
5559 sb.setLength(0);
5560 sb.append(prefix);
5561 sb.append(" Wake lock ");
5562 sb.append(wakelocks.keyAt(iw));
5563 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
5564 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005565 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
5566 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005567 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005568 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
5569 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005570 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
5571 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005572 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
5573 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07005574 sb.append(" realtime");
5575 pw.println(sb.toString());
5576 uidActivity = true;
5577 countWakelock++;
5578
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005579 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
5580 rawRealtime, which);
5581 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
5582 rawRealtime, which);
5583 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
5584 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005585 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07005586 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005587 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005588 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07005589 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
5590 // pooled and therefore just a lower bound)
5591 long actualTotalPartialWakelock = 0;
5592 long actualBgPartialWakelock = 0;
5593 if (u.getAggregatedPartialWakelockTimer() != null) {
5594 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
5595 // Convert from microseconds to milliseconds with rounding
5596 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07005597 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07005598 final Timer bgAggTimer = aggTimer.getSubTimer();
5599 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005600 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07005601 }
5602
5603 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
5604 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
5605 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005606 sb.setLength(0);
5607 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005608 sb.append(" TOTAL wake: ");
5609 boolean needComma = false;
5610 if (totalFullWakelock != 0) {
5611 needComma = true;
5612 formatTimeMs(sb, totalFullWakelock);
5613 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005614 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005615 if (totalPartialWakelock != 0) {
5616 if (needComma) {
5617 sb.append(", ");
5618 }
5619 needComma = true;
5620 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07005621 sb.append("blamed partial");
5622 }
5623 if (actualTotalPartialWakelock != 0) {
5624 if (needComma) {
5625 sb.append(", ");
5626 }
5627 needComma = true;
5628 formatTimeMs(sb, actualTotalPartialWakelock);
5629 sb.append("actual partial");
5630 }
5631 if (actualBgPartialWakelock != 0) {
5632 if (needComma) {
5633 sb.append(", ");
5634 }
5635 needComma = true;
5636 formatTimeMs(sb, actualBgPartialWakelock);
5637 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005638 }
5639 if (totalWindowWakelock != 0) {
5640 if (needComma) {
5641 sb.append(", ");
5642 }
5643 needComma = true;
5644 formatTimeMs(sb, totalWindowWakelock);
5645 sb.append("window");
5646 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005647 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07005648 if (needComma) {
5649 sb.append(",");
5650 }
5651 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005652 formatTimeMs(sb, totalDrawWakelock);
5653 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07005654 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005655 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005656 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005657 }
5658 }
5659
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07005660 // Calculate multicast wakelock stats
5661 final Timer mcTimer = u.getMulticastWakelockStats();
5662 if (mcTimer != null) {
5663 final long multicastWakeLockTimeMicros = mcTimer.getTotalTimeLocked(rawRealtime, which);
5664 final int multicastWakeLockCount = mcTimer.getCountLocked(which);
5665
5666 if (multicastWakeLockTimeMicros > 0) {
5667 sb.setLength(0);
5668 sb.append(prefix);
5669 sb.append(" WiFi Multicast Wakelock");
5670 sb.append(" count = ");
5671 sb.append(multicastWakeLockCount);
5672 sb.append(" time = ");
5673 formatTimeMsNoSpace(sb, (multicastWakeLockTimeMicros + 500) / 1000);
5674 pw.println(sb.toString());
5675 }
5676 }
5677
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005678 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
5679 for (int isy=syncs.size()-1; isy>=0; isy--) {
5680 final Timer timer = syncs.valueAt(isy);
5681 // Convert from microseconds to milliseconds with rounding
5682 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5683 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07005684 final Timer bgTimer = timer.getSubTimer();
5685 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005686 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07005687 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005688 sb.setLength(0);
5689 sb.append(prefix);
5690 sb.append(" Sync ");
5691 sb.append(syncs.keyAt(isy));
5692 sb.append(": ");
5693 if (totalTime != 0) {
5694 formatTimeMs(sb, totalTime);
5695 sb.append("realtime (");
5696 sb.append(count);
5697 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07005698 if (bgTime > 0) {
5699 sb.append(", ");
5700 formatTimeMs(sb, bgTime);
5701 sb.append("background (");
5702 sb.append(bgCount);
5703 sb.append(" times)");
5704 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005705 } else {
5706 sb.append("(not used)");
5707 }
5708 pw.println(sb.toString());
5709 uidActivity = true;
5710 }
5711
5712 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
5713 for (int ij=jobs.size()-1; ij>=0; ij--) {
5714 final Timer timer = jobs.valueAt(ij);
5715 // Convert from microseconds to milliseconds with rounding
5716 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5717 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07005718 final Timer bgTimer = timer.getSubTimer();
5719 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005720 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07005721 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005722 sb.setLength(0);
5723 sb.append(prefix);
5724 sb.append(" Job ");
5725 sb.append(jobs.keyAt(ij));
5726 sb.append(": ");
5727 if (totalTime != 0) {
5728 formatTimeMs(sb, totalTime);
5729 sb.append("realtime (");
5730 sb.append(count);
5731 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07005732 if (bgTime > 0) {
5733 sb.append(", ");
5734 formatTimeMs(sb, bgTime);
5735 sb.append("background (");
5736 sb.append(bgCount);
5737 sb.append(" times)");
5738 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005739 } else {
5740 sb.append("(not used)");
5741 }
5742 pw.println(sb.toString());
5743 uidActivity = true;
5744 }
5745
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005746 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
5747 for (int ic=completions.size()-1; ic>=0; ic--) {
5748 SparseIntArray types = completions.valueAt(ic);
5749 if (types != null) {
5750 pw.print(prefix);
5751 pw.print(" Job Completions ");
5752 pw.print(completions.keyAt(ic));
5753 pw.print(":");
5754 for (int it=0; it<types.size(); it++) {
5755 pw.print(" ");
5756 pw.print(JobParameters.getReasonName(types.keyAt(it)));
5757 pw.print("(");
5758 pw.print(types.valueAt(it));
5759 pw.print("x)");
5760 }
5761 pw.println();
5762 }
5763 }
5764
Amith Yamasani977e11f2018-02-16 11:29:54 -08005765 u.getDeferredJobsLineLocked(sb, which);
5766 if (sb.length() > 0) {
5767 pw.print(" Jobs deferred on launch "); pw.println(sb.toString());
5768 }
5769
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005770 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
5771 prefix, "Flashlight");
5772 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
5773 prefix, "Camera");
5774 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
5775 prefix, "Video");
5776 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
5777 prefix, "Audio");
5778
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005779 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
5780 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005781 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005782 final Uid.Sensor se = sensors.valueAt(ise);
5783 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005784 sb.setLength(0);
5785 sb.append(prefix);
5786 sb.append(" Sensor ");
5787 int handle = se.getHandle();
5788 if (handle == Uid.Sensor.GPS) {
5789 sb.append("GPS");
5790 } else {
5791 sb.append(handle);
5792 }
5793 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005794
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005795 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005796 if (timer != null) {
5797 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005798 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5799 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005800 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005801 final Timer bgTimer = se.getSensorBackgroundTime();
5802 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005803 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5804 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5805 final long bgActualTime = bgTimer != null ?
5806 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5807
Dianne Hackborn61659e52014-07-09 16:13:01 -07005808 //timer.logState();
5809 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005810 if (actualTime != totalTime) {
5811 formatTimeMs(sb, totalTime);
5812 sb.append("blamed realtime, ");
5813 }
5814
5815 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005816 sb.append("realtime (");
5817 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005818 sb.append(" times)");
5819
5820 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005821 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005822 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5823 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005824 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005825 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005826 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005827 } else {
5828 sb.append("(not used)");
5829 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005830 } else {
5831 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005832 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005833
5834 pw.println(sb.toString());
5835 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005836 }
5837
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005838 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5839 "Vibrator");
5840 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5841 prefix, "Foreground activities");
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005842 uidActivity |= printTimer(pw, sb, u.getForegroundServiceTimer(), rawRealtime, which,
5843 prefix, "Foreground services");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005844
Dianne Hackborn61659e52014-07-09 16:13:01 -07005845 long totalStateTime = 0;
5846 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5847 long time = u.getProcessStateTime(ips, rawRealtime, which);
5848 if (time > 0) {
5849 totalStateTime += time;
5850 sb.setLength(0);
5851 sb.append(prefix);
5852 sb.append(" ");
5853 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5854 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005855 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005856 pw.println(sb.toString());
5857 uidActivity = true;
5858 }
5859 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005860 if (totalStateTime > 0) {
5861 sb.setLength(0);
5862 sb.append(prefix);
5863 sb.append(" Total running: ");
5864 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5865 pw.println(sb.toString());
5866 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005867
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005868 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5869 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005870 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005871 sb.setLength(0);
5872 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005873 sb.append(" Total cpu time: u=");
5874 formatTimeMs(sb, userCpuTimeUs / 1000);
5875 sb.append("s=");
5876 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005877 pw.println(sb.toString());
5878 }
5879
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005880 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5881 if (cpuFreqTimes != null) {
5882 sb.setLength(0);
5883 sb.append(" Total cpu time per freq:");
5884 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5885 sb.append(" " + cpuFreqTimes[i]);
5886 }
5887 pw.println(sb.toString());
5888 }
5889 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5890 if (screenOffCpuFreqTimes != null) {
5891 sb.setLength(0);
5892 sb.append(" Total screen-off cpu time per freq:");
5893 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5894 sb.append(" " + screenOffCpuFreqTimes[i]);
5895 }
5896 pw.println(sb.toString());
5897 }
5898
Sudheer Shankab2f83c12017-11-13 19:25:01 -08005899 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
5900 final long[] cpuTimes = u.getCpuFreqTimes(which, procState);
5901 if (cpuTimes != null) {
5902 sb.setLength(0);
5903 sb.append(" Cpu times per freq at state "
5904 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5905 for (int i = 0; i < cpuTimes.length; ++i) {
5906 sb.append(" " + cpuTimes[i]);
5907 }
5908 pw.println(sb.toString());
5909 }
5910
5911 final long[] screenOffCpuTimes = u.getScreenOffCpuFreqTimes(which, procState);
5912 if (screenOffCpuTimes != null) {
5913 sb.setLength(0);
5914 sb.append(" Screen-off cpu times per freq at state "
5915 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5916 for (int i = 0; i < screenOffCpuTimes.length; ++i) {
5917 sb.append(" " + screenOffCpuTimes[i]);
5918 }
5919 pw.println(sb.toString());
5920 }
5921 }
5922
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005923 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5924 = u.getProcessStats();
5925 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5926 final Uid.Proc ps = processStats.valueAt(ipr);
5927 long userTime;
5928 long systemTime;
5929 long foregroundTime;
5930 int starts;
5931 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005932
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005933 userTime = ps.getUserTime(which);
5934 systemTime = ps.getSystemTime(which);
5935 foregroundTime = ps.getForegroundTime(which);
5936 starts = ps.getStarts(which);
5937 final int numCrashes = ps.getNumCrashes(which);
5938 final int numAnrs = ps.getNumAnrs(which);
5939 numExcessive = which == STATS_SINCE_CHARGED
5940 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005941
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005942 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5943 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5944 sb.setLength(0);
5945 sb.append(prefix); sb.append(" Proc ");
5946 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5947 sb.append(prefix); sb.append(" CPU: ");
5948 formatTimeMs(sb, userTime); sb.append("usr + ");
5949 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5950 formatTimeMs(sb, foregroundTime); sb.append("fg");
5951 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5952 sb.append("\n"); sb.append(prefix); sb.append(" ");
5953 boolean hasOne = false;
5954 if (starts != 0) {
5955 hasOne = true;
5956 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005957 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005958 if (numCrashes != 0) {
5959 if (hasOne) {
5960 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005961 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005962 hasOne = true;
5963 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005964 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005965 if (numAnrs != 0) {
5966 if (hasOne) {
5967 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005968 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005969 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005970 }
5971 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005972 pw.println(sb.toString());
5973 for (int e=0; e<numExcessive; e++) {
5974 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5975 if (ew != null) {
5976 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07005977 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005978 pw.print("cpu");
5979 } else {
5980 pw.print("unknown");
5981 }
5982 pw.print(" use: ");
5983 TimeUtils.formatDuration(ew.usedTime, pw);
5984 pw.print(" over ");
5985 TimeUtils.formatDuration(ew.overTime, pw);
5986 if (ew.overTime != 0) {
5987 pw.print(" (");
5988 pw.print((ew.usedTime*100)/ew.overTime);
5989 pw.println("%)");
5990 }
5991 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005992 }
5993 uidActivity = true;
5994 }
5995 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005996
5997 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5998 = u.getPackageStats();
5999 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
6000 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
6001 pw.println(":");
6002 boolean apkActivity = false;
6003 final Uid.Pkg ps = packageStats.valueAt(ipkg);
6004 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
6005 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
6006 pw.print(prefix); pw.print(" Wakeup alarm ");
6007 pw.print(alarms.keyAt(iwa)); pw.print(": ");
6008 pw.print(alarms.valueAt(iwa).getCountLocked(which));
6009 pw.println(" times");
6010 apkActivity = true;
6011 }
6012 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
6013 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
6014 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
6015 final long startTime = ss.getStartTime(batteryUptime, which);
6016 final int starts = ss.getStarts(which);
6017 final int launches = ss.getLaunches(which);
6018 if (startTime != 0 || starts != 0 || launches != 0) {
6019 sb.setLength(0);
6020 sb.append(prefix); sb.append(" Service ");
6021 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
6022 sb.append(prefix); sb.append(" Created for: ");
6023 formatTimeMs(sb, startTime / 1000);
6024 sb.append("uptime\n");
6025 sb.append(prefix); sb.append(" Starts: ");
6026 sb.append(starts);
6027 sb.append(", launches: "); sb.append(launches);
6028 pw.println(sb.toString());
6029 apkActivity = true;
6030 }
6031 }
6032 if (!apkActivity) {
6033 pw.print(prefix); pw.println(" (nothing executed)");
6034 }
6035 uidActivity = true;
6036 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006037 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006038 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 }
6040 }
6041 }
6042
Kweku Adams71a95312018-04-16 16:54:24 -07006043 static void printBitDescriptions(StringBuilder sb, int oldval, int newval,
6044 HistoryTag wakelockTag, BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006045 int diff = oldval ^ newval;
6046 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006047 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006048 for (int i=0; i<descriptions.length; i++) {
6049 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006050 if ((diff&bd.mask) != 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006051 sb.append(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006052 if (bd.shift < 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006053 sb.append((newval & bd.mask) != 0 ? "+" : "-");
6054 sb.append(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006055 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
6056 didWake = true;
Kweku Adams71a95312018-04-16 16:54:24 -07006057 sb.append("=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006058 if (longNames) {
Kweku Adams71a95312018-04-16 16:54:24 -07006059 UserHandle.formatUid(sb, wakelockTag.uid);
6060 sb.append(":\"");
6061 sb.append(wakelockTag.string);
6062 sb.append("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006063 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006064 sb.append(wakelockTag.poolIdx);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006065 }
6066 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006067 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006068 sb.append(longNames ? bd.name : bd.shortName);
6069 sb.append("=");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006070 int val = (newval&bd.mask)>>bd.shift;
6071 if (bd.values != null && val >= 0 && val < bd.values.length) {
Kweku Adams71a95312018-04-16 16:54:24 -07006072 sb.append(longNames ? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006073 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006074 sb.append(val);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006075 }
6076 }
6077 }
6078 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006079 if (!didWake && wakelockTag != null) {
Kweku Adams71a95312018-04-16 16:54:24 -07006080 sb.append(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006081 if (longNames) {
Kweku Adams71a95312018-04-16 16:54:24 -07006082 UserHandle.formatUid(sb, wakelockTag.uid);
6083 sb.append(":\"");
6084 sb.append(wakelockTag.string);
6085 sb.append("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006086 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006087 sb.append(wakelockTag.poolIdx);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006088 }
6089 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006090 }
Mike Mac2f518a2017-09-19 16:06:03 -07006091
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006092 public void prepareForDumpLocked() {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006093 // We don't need to require subclasses implement this.
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006094 }
6095
6096 public static class HistoryPrinter {
6097 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006098 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006099 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006100 int oldStatus = -1;
6101 int oldHealth = -1;
6102 int oldPlug = -1;
6103 int oldTemp = -1;
6104 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006105 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006106 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006107
Dianne Hackborn3251b902014-06-20 14:40:53 -07006108 void reset() {
6109 oldState = oldState2 = 0;
6110 oldLevel = -1;
6111 oldStatus = -1;
6112 oldHealth = -1;
6113 oldPlug = -1;
6114 oldTemp = -1;
6115 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006116 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07006117 }
6118
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006119 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006120 boolean verbose) {
Kweku Adams71a95312018-04-16 16:54:24 -07006121 pw.print(printNextItem(rec, baseTime, checkin, verbose));
6122 }
6123
6124 /** Print the next history item to proto. */
6125 public void printNextItem(ProtoOutputStream proto, HistoryItem rec, long baseTime,
6126 boolean verbose) {
6127 String item = printNextItem(rec, baseTime, true, verbose);
6128 for (String line : item.split("\n")) {
6129 proto.write(BatteryStatsServiceDumpHistoryProto.CSV_LINES, line);
6130 }
6131 }
6132
6133 private String printNextItem(HistoryItem rec, long baseTime, boolean checkin,
6134 boolean verbose) {
6135 StringBuilder item = new StringBuilder();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006136 if (!checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006137 item.append(" ");
6138 TimeUtils.formatDuration(
6139 rec.time - baseTime, item, TimeUtils.HUNDRED_DAY_FIELD_LEN);
6140 item.append(" (");
6141 item.append(rec.numReadInts);
6142 item.append(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006143 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006144 item.append(BATTERY_STATS_CHECKIN_VERSION); item.append(',');
6145 item.append(HISTORY_DATA); item.append(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006146 if (lastTime < 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006147 item.append(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006148 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006149 item.append(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006150 }
6151 lastTime = rec.time;
6152 }
6153 if (rec.cmd == HistoryItem.CMD_START) {
6154 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006155 item.append(":");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006156 }
Kweku Adams71a95312018-04-16 16:54:24 -07006157 item.append("START\n");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006158 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006159 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
6160 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006161 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006162 item.append(":");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006163 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006164 if (rec.cmd == HistoryItem.CMD_RESET) {
Kweku Adams71a95312018-04-16 16:54:24 -07006165 item.append("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006166 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006167 }
Kweku Adams71a95312018-04-16 16:54:24 -07006168 item.append("TIME:");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006169 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006170 item.append(rec.currentTime);
6171 item.append("\n");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006172 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006173 item.append(" ");
6174 item.append(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006175 rec.currentTime).toString());
Kweku Adams71a95312018-04-16 16:54:24 -07006176 item.append("\n");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006177 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006178 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
6179 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006180 item.append(":");
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006181 }
Kweku Adams71a95312018-04-16 16:54:24 -07006182 item.append("SHUTDOWN\n");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006183 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
6184 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006185 item.append(":");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006186 }
Kweku Adams71a95312018-04-16 16:54:24 -07006187 item.append("*OVERFLOW*\n");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006188 } else {
6189 if (!checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006190 if (rec.batteryLevel < 10) item.append("00");
6191 else if (rec.batteryLevel < 100) item.append("0");
6192 item.append(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006193 if (verbose) {
Kweku Adams71a95312018-04-16 16:54:24 -07006194 item.append(" ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006195 if (rec.states < 0) ;
Kweku Adams71a95312018-04-16 16:54:24 -07006196 else if (rec.states < 0x10) item.append("0000000");
6197 else if (rec.states < 0x100) item.append("000000");
6198 else if (rec.states < 0x1000) item.append("00000");
6199 else if (rec.states < 0x10000) item.append("0000");
6200 else if (rec.states < 0x100000) item.append("000");
6201 else if (rec.states < 0x1000000) item.append("00");
6202 else if (rec.states < 0x10000000) item.append("0");
6203 item.append(Integer.toHexString(rec.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006204 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006205 } else {
6206 if (oldLevel != rec.batteryLevel) {
6207 oldLevel = rec.batteryLevel;
Kweku Adams71a95312018-04-16 16:54:24 -07006208 item.append(",Bl="); item.append(rec.batteryLevel);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006209 }
6210 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006211 if (oldStatus != rec.batteryStatus) {
6212 oldStatus = rec.batteryStatus;
Kweku Adams71a95312018-04-16 16:54:24 -07006213 item.append(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006214 switch (oldStatus) {
6215 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Kweku Adams71a95312018-04-16 16:54:24 -07006216 item.append(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006217 break;
6218 case BatteryManager.BATTERY_STATUS_CHARGING:
Kweku Adams71a95312018-04-16 16:54:24 -07006219 item.append(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006220 break;
6221 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Kweku Adams71a95312018-04-16 16:54:24 -07006222 item.append(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006223 break;
6224 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Kweku Adams71a95312018-04-16 16:54:24 -07006225 item.append(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006226 break;
6227 case BatteryManager.BATTERY_STATUS_FULL:
Kweku Adams71a95312018-04-16 16:54:24 -07006228 item.append(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006229 break;
6230 default:
Kweku Adams71a95312018-04-16 16:54:24 -07006231 item.append(oldStatus);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006232 break;
6233 }
6234 }
6235 if (oldHealth != rec.batteryHealth) {
6236 oldHealth = rec.batteryHealth;
Kweku Adams71a95312018-04-16 16:54:24 -07006237 item.append(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006238 switch (oldHealth) {
6239 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Kweku Adams71a95312018-04-16 16:54:24 -07006240 item.append(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006241 break;
6242 case BatteryManager.BATTERY_HEALTH_GOOD:
Kweku Adams71a95312018-04-16 16:54:24 -07006243 item.append(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006244 break;
6245 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Kweku Adams71a95312018-04-16 16:54:24 -07006246 item.append(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006247 break;
6248 case BatteryManager.BATTERY_HEALTH_DEAD:
Kweku Adams71a95312018-04-16 16:54:24 -07006249 item.append(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006250 break;
6251 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Kweku Adams71a95312018-04-16 16:54:24 -07006252 item.append(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006253 break;
6254 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Kweku Adams71a95312018-04-16 16:54:24 -07006255 item.append(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006256 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006257 case BatteryManager.BATTERY_HEALTH_COLD:
Kweku Adams71a95312018-04-16 16:54:24 -07006258 item.append(checkin ? "c" : "cold");
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006259 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006260 default:
Kweku Adams71a95312018-04-16 16:54:24 -07006261 item.append(oldHealth);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006262 break;
6263 }
6264 }
6265 if (oldPlug != rec.batteryPlugType) {
6266 oldPlug = rec.batteryPlugType;
Kweku Adams71a95312018-04-16 16:54:24 -07006267 item.append(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006268 switch (oldPlug) {
6269 case 0:
Kweku Adams71a95312018-04-16 16:54:24 -07006270 item.append(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006271 break;
6272 case BatteryManager.BATTERY_PLUGGED_AC:
Kweku Adams71a95312018-04-16 16:54:24 -07006273 item.append(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006274 break;
6275 case BatteryManager.BATTERY_PLUGGED_USB:
Kweku Adams71a95312018-04-16 16:54:24 -07006276 item.append(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006277 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006278 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Kweku Adams71a95312018-04-16 16:54:24 -07006279 item.append(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006280 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006281 default:
Kweku Adams71a95312018-04-16 16:54:24 -07006282 item.append(oldPlug);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006283 break;
6284 }
6285 }
6286 if (oldTemp != rec.batteryTemperature) {
6287 oldTemp = rec.batteryTemperature;
Kweku Adams71a95312018-04-16 16:54:24 -07006288 item.append(checkin ? ",Bt=" : " temp=");
6289 item.append(oldTemp);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006290 }
6291 if (oldVolt != rec.batteryVoltage) {
6292 oldVolt = rec.batteryVoltage;
Kweku Adams71a95312018-04-16 16:54:24 -07006293 item.append(checkin ? ",Bv=" : " volt=");
6294 item.append(oldVolt);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006295 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006296 final int chargeMAh = rec.batteryChargeUAh / 1000;
6297 if (oldChargeMAh != chargeMAh) {
6298 oldChargeMAh = chargeMAh;
Kweku Adams71a95312018-04-16 16:54:24 -07006299 item.append(checkin ? ",Bcc=" : " charge=");
6300 item.append(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07006301 }
Kweku Adams71a95312018-04-16 16:54:24 -07006302 printBitDescriptions(item, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006303 HISTORY_STATE_DESCRIPTIONS, !checkin);
Kweku Adams71a95312018-04-16 16:54:24 -07006304 printBitDescriptions(item, oldState2, rec.states2, null,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006305 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006306 if (rec.wakeReasonTag != null) {
6307 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006308 item.append(",wr=");
6309 item.append(rec.wakeReasonTag.poolIdx);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006310 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006311 item.append(" wake_reason=");
6312 item.append(rec.wakeReasonTag.uid);
6313 item.append(":\"");
6314 item.append(rec.wakeReasonTag.string);
6315 item.append("\"");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006316 }
6317 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08006318 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Kweku Adams71a95312018-04-16 16:54:24 -07006319 item.append(checkin ? "," : " ");
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006320 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006321 item.append("+");
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006322 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006323 item.append("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006324 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006325 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
6326 : HISTORY_EVENT_NAMES;
6327 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
6328 | HistoryItem.EVENT_FLAG_FINISH);
6329 if (idx >= 0 && idx < eventNames.length) {
Kweku Adams71a95312018-04-16 16:54:24 -07006330 item.append(eventNames[idx]);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006331 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006332 item.append(checkin ? "Ev" : "event");
6333 item.append(idx);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006334 }
Kweku Adams71a95312018-04-16 16:54:24 -07006335 item.append("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006336 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006337 item.append(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08006338 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006339 item.append(HISTORY_EVENT_INT_FORMATTERS[idx]
Adam Lesinski041d9172016-12-12 12:03:56 -08006340 .applyAsString(rec.eventTag.uid));
Kweku Adams71a95312018-04-16 16:54:24 -07006341 item.append(":\"");
6342 item.append(rec.eventTag.string);
6343 item.append("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006344 }
6345 }
Kweku Adams71a95312018-04-16 16:54:24 -07006346 item.append("\n");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006347 if (rec.stepDetails != null) {
6348 if (!checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006349 item.append(" Details: cpu=");
6350 item.append(rec.stepDetails.userTime);
6351 item.append("u+");
6352 item.append(rec.stepDetails.systemTime);
6353 item.append("s");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006354 if (rec.stepDetails.appCpuUid1 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006355 item.append(" (");
6356 printStepCpuUidDetails(item, rec.stepDetails.appCpuUid1,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006357 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6358 if (rec.stepDetails.appCpuUid2 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006359 item.append(", ");
6360 printStepCpuUidDetails(item, rec.stepDetails.appCpuUid2,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006361 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6362 }
6363 if (rec.stepDetails.appCpuUid3 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006364 item.append(", ");
6365 printStepCpuUidDetails(item, rec.stepDetails.appCpuUid3,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006366 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6367 }
Kweku Adams71a95312018-04-16 16:54:24 -07006368 item.append(')');
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006369 }
Kweku Adams71a95312018-04-16 16:54:24 -07006370 item.append("\n");
6371 item.append(" /proc/stat=");
6372 item.append(rec.stepDetails.statUserTime);
6373 item.append(" usr, ");
6374 item.append(rec.stepDetails.statSystemTime);
6375 item.append(" sys, ");
6376 item.append(rec.stepDetails.statIOWaitTime);
6377 item.append(" io, ");
6378 item.append(rec.stepDetails.statIrqTime);
6379 item.append(" irq, ");
6380 item.append(rec.stepDetails.statSoftIrqTime);
6381 item.append(" sirq, ");
6382 item.append(rec.stepDetails.statIdlTime);
6383 item.append(" idle");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006384 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
6385 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
6386 + rec.stepDetails.statSoftIrqTime;
6387 int total = totalRun + rec.stepDetails.statIdlTime;
6388 if (total > 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006389 item.append(" (");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006390 float perc = ((float)totalRun) / ((float)total) * 100;
Kweku Adams71a95312018-04-16 16:54:24 -07006391 item.append(String.format("%.1f%%", perc));
6392 item.append(" of ");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006393 StringBuilder sb = new StringBuilder(64);
6394 formatTimeMsNoSpace(sb, total*10);
Kweku Adams71a95312018-04-16 16:54:24 -07006395 item.append(sb);
6396 item.append(")");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006397 }
Kweku Adams71a95312018-04-16 16:54:24 -07006398 item.append(", PlatformIdleStat ");
6399 item.append(rec.stepDetails.statPlatformIdleState);
6400 item.append("\n");
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006401
Kweku Adams71a95312018-04-16 16:54:24 -07006402 item.append(", SubsystemPowerState ");
6403 item.append(rec.stepDetails.statSubsystemPowerState);
6404 item.append("\n");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006405 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006406 item.append(BATTERY_STATS_CHECKIN_VERSION); item.append(',');
6407 item.append(HISTORY_DATA); item.append(",0,Dcpu=");
6408 item.append(rec.stepDetails.userTime);
6409 item.append(":");
6410 item.append(rec.stepDetails.systemTime);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006411 if (rec.stepDetails.appCpuUid1 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006412 printStepCpuUidCheckinDetails(item, rec.stepDetails.appCpuUid1,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006413 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6414 if (rec.stepDetails.appCpuUid2 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006415 printStepCpuUidCheckinDetails(item, rec.stepDetails.appCpuUid2,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006416 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6417 }
6418 if (rec.stepDetails.appCpuUid3 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006419 printStepCpuUidCheckinDetails(item, rec.stepDetails.appCpuUid3,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006420 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6421 }
6422 }
Kweku Adams71a95312018-04-16 16:54:24 -07006423 item.append("\n");
6424 item.append(BATTERY_STATS_CHECKIN_VERSION); item.append(',');
6425 item.append(HISTORY_DATA); item.append(",0,Dpst=");
6426 item.append(rec.stepDetails.statUserTime);
6427 item.append(',');
6428 item.append(rec.stepDetails.statSystemTime);
6429 item.append(',');
6430 item.append(rec.stepDetails.statIOWaitTime);
6431 item.append(',');
6432 item.append(rec.stepDetails.statIrqTime);
6433 item.append(',');
6434 item.append(rec.stepDetails.statSoftIrqTime);
6435 item.append(',');
6436 item.append(rec.stepDetails.statIdlTime);
6437 item.append(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006438 if (rec.stepDetails.statPlatformIdleState != null) {
Kweku Adams71a95312018-04-16 16:54:24 -07006439 item.append(rec.stepDetails.statPlatformIdleState);
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006440 if (rec.stepDetails.statSubsystemPowerState != null) {
Kweku Adams71a95312018-04-16 16:54:24 -07006441 item.append(',');
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006442 }
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006443 }
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006444
6445 if (rec.stepDetails.statSubsystemPowerState != null) {
Kweku Adams71a95312018-04-16 16:54:24 -07006446 item.append(rec.stepDetails.statSubsystemPowerState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006447 }
Kweku Adams71a95312018-04-16 16:54:24 -07006448 item.append("\n");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006449 }
6450 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006451 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006452 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006453 }
Kweku Adams71a95312018-04-16 16:54:24 -07006454
6455 return item.toString();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006456 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006457
Kweku Adams71a95312018-04-16 16:54:24 -07006458 private void printStepCpuUidDetails(StringBuilder sb, int uid, int utime, int stime) {
6459 UserHandle.formatUid(sb, uid);
6460 sb.append("=");
6461 sb.append(utime);
6462 sb.append("u+");
6463 sb.append(stime);
6464 sb.append("s");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006465 }
6466
Kweku Adams71a95312018-04-16 16:54:24 -07006467 private void printStepCpuUidCheckinDetails(StringBuilder sb, int uid, int utime,
6468 int stime) {
6469 sb.append('/');
6470 sb.append(uid);
6471 sb.append(":");
6472 sb.append(utime);
6473 sb.append(":");
6474 sb.append(stime);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006475 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006476 }
6477
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006478 private void printSizeValue(PrintWriter pw, long size) {
6479 float result = size;
6480 String suffix = "";
6481 if (result >= 10*1024) {
6482 suffix = "KB";
6483 result = result / 1024;
6484 }
6485 if (result >= 10*1024) {
6486 suffix = "MB";
6487 result = result / 1024;
6488 }
6489 if (result >= 10*1024) {
6490 suffix = "GB";
6491 result = result / 1024;
6492 }
6493 if (result >= 10*1024) {
6494 suffix = "TB";
6495 result = result / 1024;
6496 }
6497 if (result >= 10*1024) {
6498 suffix = "PB";
6499 result = result / 1024;
6500 }
6501 pw.print((int)result);
6502 pw.print(suffix);
6503 }
6504
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006505 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
6506 String label3, long estimatedTime) {
6507 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006508 return false;
6509 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006510 pw.print(label1);
6511 pw.print(label2);
6512 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006513 StringBuilder sb = new StringBuilder(64);
6514 formatTimeMs(sb, estimatedTime);
6515 pw.print(sb);
6516 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006517 return true;
6518 }
6519
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006520 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
6521 LevelStepTracker steps, boolean checkin) {
6522 if (steps == null) {
6523 return false;
6524 }
6525 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006526 if (count <= 0) {
6527 return false;
6528 }
6529 if (!checkin) {
6530 pw.println(header);
6531 }
Kweku Adams030980a2015-04-01 16:07:48 -07006532 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006533 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006534 long duration = steps.getDurationAt(i);
6535 int level = steps.getLevelAt(i);
6536 long initMode = steps.getInitModeAt(i);
6537 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006538 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006539 lineArgs[0] = Long.toString(duration);
6540 lineArgs[1] = Integer.toString(level);
6541 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6542 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6543 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
6544 case Display.STATE_ON: lineArgs[2] = "s+"; break;
6545 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
6546 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07006547 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006548 }
6549 } else {
6550 lineArgs[2] = "";
6551 }
6552 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6553 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
6554 } else {
6555 lineArgs[3] = "";
6556 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006557 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07006558 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006559 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07006560 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006561 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006562 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
6563 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006564 pw.print(prefix);
6565 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006566 TimeUtils.formatDuration(duration, pw);
6567 pw.print(" to "); pw.print(level);
6568 boolean haveModes = false;
6569 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6570 pw.print(" (");
6571 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6572 case Display.STATE_OFF: pw.print("screen-off"); break;
6573 case Display.STATE_ON: pw.print("screen-on"); break;
6574 case Display.STATE_DOZE: pw.print("screen-doze"); break;
6575 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07006576 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006577 }
6578 haveModes = true;
6579 }
6580 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6581 pw.print(haveModes ? ", " : " (");
6582 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
6583 ? "power-save-on" : "power-save-off");
6584 haveModes = true;
6585 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006586 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6587 pw.print(haveModes ? ", " : " (");
6588 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6589 ? "device-idle-on" : "device-idle-off");
6590 haveModes = true;
6591 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006592 if (haveModes) {
6593 pw.print(")");
6594 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006595 pw.println();
6596 }
6597 }
6598 return true;
6599 }
6600
Kweku Adams87b19ec2017-10-09 12:40:03 -07006601 private static void dumpDurationSteps(ProtoOutputStream proto, long fieldId,
6602 LevelStepTracker steps) {
6603 if (steps == null) {
6604 return;
6605 }
6606 int count = steps.mNumStepDurations;
Kweku Adams87b19ec2017-10-09 12:40:03 -07006607 for (int i = 0; i < count; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07006608 long token = proto.start(fieldId);
Kweku Adams87b19ec2017-10-09 12:40:03 -07006609 proto.write(SystemProto.BatteryLevelStep.DURATION_MS, steps.getDurationAt(i));
6610 proto.write(SystemProto.BatteryLevelStep.LEVEL, steps.getLevelAt(i));
6611
6612 final long initMode = steps.getInitModeAt(i);
6613 final long modMode = steps.getModModeAt(i);
6614
6615 int ds = SystemProto.BatteryLevelStep.DS_MIXED;
6616 if ((modMode & STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6617 switch ((int) (initMode & STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6618 case Display.STATE_OFF:
6619 ds = SystemProto.BatteryLevelStep.DS_OFF;
6620 break;
6621 case Display.STATE_ON:
6622 ds = SystemProto.BatteryLevelStep.DS_ON;
6623 break;
6624 case Display.STATE_DOZE:
6625 ds = SystemProto.BatteryLevelStep.DS_DOZE;
6626 break;
6627 case Display.STATE_DOZE_SUSPEND:
6628 ds = SystemProto.BatteryLevelStep.DS_DOZE_SUSPEND;
6629 break;
6630 default:
6631 ds = SystemProto.BatteryLevelStep.DS_ERROR;
6632 break;
6633 }
6634 }
6635 proto.write(SystemProto.BatteryLevelStep.DISPLAY_STATE, ds);
6636
6637 int psm = SystemProto.BatteryLevelStep.PSM_MIXED;
6638 if ((modMode & STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6639 psm = (initMode & STEP_LEVEL_MODE_POWER_SAVE) != 0
6640 ? SystemProto.BatteryLevelStep.PSM_ON : SystemProto.BatteryLevelStep.PSM_OFF;
6641 }
6642 proto.write(SystemProto.BatteryLevelStep.POWER_SAVE_MODE, psm);
6643
6644 int im = SystemProto.BatteryLevelStep.IM_MIXED;
6645 if ((modMode & STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6646 im = (initMode & STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6647 ? SystemProto.BatteryLevelStep.IM_ON : SystemProto.BatteryLevelStep.IM_OFF;
6648 }
6649 proto.write(SystemProto.BatteryLevelStep.IDLE_MODE, im);
6650
6651 proto.end(token);
6652 }
6653 }
6654
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006655 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006656 public static final int DUMP_DAILY_ONLY = 1<<2;
6657 public static final int DUMP_HISTORY_ONLY = 1<<3;
6658 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
6659 public static final int DUMP_VERBOSE = 1<<5;
6660 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006661
Dianne Hackborn37de0982014-05-09 09:32:18 -07006662 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
6663 final HistoryPrinter hprinter = new HistoryPrinter();
6664 final HistoryItem rec = new HistoryItem();
6665 long lastTime = -1;
6666 long baseTime = -1;
6667 boolean printed = false;
6668 HistoryEventTracker tracker = null;
6669 while (getNextHistoryLocked(rec)) {
6670 lastTime = rec.time;
6671 if (baseTime < 0) {
6672 baseTime = lastTime;
6673 }
6674 if (rec.time >= histStart) {
6675 if (histStart >= 0 && !printed) {
6676 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07006677 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006678 || rec.cmd == HistoryItem.CMD_START
6679 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006680 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006681 hprinter.printNextItem(pw, rec, baseTime, checkin,
6682 (flags&DUMP_VERBOSE) != 0);
6683 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006684 } else if (rec.currentTime != 0) {
6685 printed = true;
6686 byte cmd = rec.cmd;
6687 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006688 hprinter.printNextItem(pw, rec, baseTime, checkin,
6689 (flags&DUMP_VERBOSE) != 0);
6690 rec.cmd = cmd;
6691 }
6692 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006693 if (rec.cmd != HistoryItem.CMD_UPDATE) {
6694 hprinter.printNextItem(pw, rec, baseTime, checkin,
6695 (flags&DUMP_VERBOSE) != 0);
6696 rec.cmd = HistoryItem.CMD_UPDATE;
6697 }
6698 int oldEventCode = rec.eventCode;
6699 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006700 rec.eventTag = new HistoryTag();
6701 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
6702 HashMap<String, SparseIntArray> active
6703 = tracker.getStateForEvent(i);
6704 if (active == null) {
6705 continue;
6706 }
6707 for (HashMap.Entry<String, SparseIntArray> ent
6708 : active.entrySet()) {
6709 SparseIntArray uids = ent.getValue();
6710 for (int j=0; j<uids.size(); j++) {
6711 rec.eventCode = i;
6712 rec.eventTag.string = ent.getKey();
6713 rec.eventTag.uid = uids.keyAt(j);
6714 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006715 hprinter.printNextItem(pw, rec, baseTime, checkin,
6716 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006717 rec.wakeReasonTag = null;
6718 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006719 }
6720 }
6721 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006722 rec.eventCode = oldEventCode;
6723 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006724 tracker = null;
6725 }
6726 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006727 hprinter.printNextItem(pw, rec, baseTime, checkin,
6728 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07006729 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
6730 // This is an attempt to aggregate the previous state and generate
6731 // fake events to reflect that state at the point where we start
6732 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07006733 if (tracker == null) {
6734 tracker = new HistoryEventTracker();
6735 }
6736 tracker.updateState(rec.eventCode, rec.eventTag.string,
6737 rec.eventTag.uid, rec.eventTag.poolIdx);
6738 }
6739 }
6740 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07006741 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006742 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
6743 }
6744 }
6745
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006746 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
6747 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
6748 if (steps == null) {
6749 return;
6750 }
6751 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
6752 if (timeRemaining >= 0) {
6753 pw.print(prefix); pw.print(label); pw.print(" total time: ");
6754 tmpSb.setLength(0);
6755 formatTimeMs(tmpSb, timeRemaining);
6756 pw.print(tmpSb);
6757 pw.print(" (from "); pw.print(tmpOutInt[0]);
6758 pw.println(" steps)");
6759 }
6760 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6761 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6762 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
6763 if (estimatedTime > 0) {
6764 pw.print(prefix); pw.print(label); pw.print(" ");
6765 pw.print(STEP_LEVEL_MODE_LABELS[i]);
6766 pw.print(" time: ");
6767 tmpSb.setLength(0);
6768 formatTimeMs(tmpSb, estimatedTime);
6769 pw.print(tmpSb);
6770 pw.print(" (from "); pw.print(tmpOutInt[0]);
6771 pw.println(" steps)");
6772 }
6773 }
6774 }
6775
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006776 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
6777 ArrayList<PackageChange> changes) {
6778 if (changes == null) {
6779 return;
6780 }
6781 pw.print(prefix); pw.println("Package changes:");
6782 for (int i=0; i<changes.size(); i++) {
6783 PackageChange pc = changes.get(i);
6784 if (pc.mUpdate) {
6785 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
6786 pw.print(" vers="); pw.println(pc.mVersionCode);
6787 } else {
6788 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
6789 }
6790 }
6791 }
6792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006793 /**
6794 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
6795 *
6796 * @param pw a Printer to receive the dump output.
6797 */
6798 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006799 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006800 prepareForDumpLocked();
6801
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006802 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006803 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006804
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006805 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006806 final long historyTotalSize = getHistoryTotalSize();
6807 final long historyUsedSize = getHistoryUsedSize();
6808 if (startIteratingHistoryLocked()) {
6809 try {
6810 pw.print("Battery History (");
6811 pw.print((100*historyUsedSize)/historyTotalSize);
6812 pw.print("% used, ");
6813 printSizeValue(pw, historyUsedSize);
6814 pw.print(" used of ");
6815 printSizeValue(pw, historyTotalSize);
6816 pw.print(", ");
6817 pw.print(getHistoryStringPoolSize());
6818 pw.print(" strings using ");
6819 printSizeValue(pw, getHistoryStringPoolBytes());
6820 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07006821 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006822 pw.println();
6823 } finally {
6824 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006825 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006826 }
6827
6828 if (startIteratingOldHistoryLocked()) {
6829 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006830 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006831 pw.println("Old battery History:");
6832 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006833 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006834 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006835 if (baseTime < 0) {
6836 baseTime = rec.time;
6837 }
6838 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006839 }
6840 pw.println();
6841 } finally {
6842 finishIteratingOldHistoryLocked();
6843 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006844 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006845 }
6846
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006847 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006848 return;
6849 }
6850
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006851 if (!filtering) {
6852 SparseArray<? extends Uid> uidStats = getUidStats();
6853 final int NU = uidStats.size();
6854 boolean didPid = false;
6855 long nowRealtime = SystemClock.elapsedRealtime();
6856 for (int i=0; i<NU; i++) {
6857 Uid uid = uidStats.valueAt(i);
6858 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
6859 if (pids != null) {
6860 for (int j=0; j<pids.size(); j++) {
6861 Uid.Pid pid = pids.valueAt(j);
6862 if (!didPid) {
6863 pw.println("Per-PID Stats:");
6864 didPid = true;
6865 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006866 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
6867 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006868 pw.print(" PID "); pw.print(pids.keyAt(j));
6869 pw.print(" wake time: ");
6870 TimeUtils.formatDuration(time, pw);
6871 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006872 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006873 }
6874 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006875 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006876 pw.println();
6877 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006878 }
6879
6880 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006881 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
6882 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006883 long timeRemaining = computeBatteryTimeRemaining(
6884 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006885 if (timeRemaining >= 0) {
6886 pw.print(" Estimated discharge time remaining: ");
6887 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6888 pw.println();
6889 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006890 final LevelStepTracker steps = getDischargeLevelStepTracker();
6891 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6892 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
6893 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6894 STEP_LEVEL_MODE_VALUES[i], null));
6895 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006896 pw.println();
6897 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006898 if (dumpDurationSteps(pw, " ", "Charge step durations:",
6899 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006900 long timeRemaining = computeChargeTimeRemaining(
6901 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006902 if (timeRemaining >= 0) {
6903 pw.print(" Estimated charge time remaining: ");
6904 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6905 pw.println();
6906 }
6907 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006908 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006909 }
Dianne Hackbornc81983a2017-10-20 16:16:32 -07006910 if (!filtering || (flags & DUMP_DAILY_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006911 pw.println("Daily stats:");
6912 pw.print(" Current start time: ");
6913 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6914 getCurrentDailyStartTime()).toString());
6915 pw.print(" Next min deadline: ");
6916 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6917 getNextMinDailyDeadline()).toString());
6918 pw.print(" Next max deadline: ");
6919 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6920 getNextMaxDailyDeadline()).toString());
6921 StringBuilder sb = new StringBuilder(64);
6922 int[] outInt = new int[1];
6923 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
6924 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006925 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
6926 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006927 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006928 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
6929 dsteps, false)) {
6930 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6931 sb, outInt);
6932 }
6933 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6934 csteps, false)) {
6935 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6936 sb, outInt);
6937 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006938 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006939 } else {
6940 pw.println(" Current daily steps:");
6941 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6942 sb, outInt);
6943 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6944 sb, outInt);
6945 }
6946 }
6947 DailyItem dit;
6948 int curIndex = 0;
6949 while ((dit=getDailyItemLocked(curIndex)) != null) {
6950 curIndex++;
6951 if ((flags&DUMP_DAILY_ONLY) != 0) {
6952 pw.println();
6953 }
6954 pw.print(" Daily from ");
6955 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6956 pw.print(" to ");
6957 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6958 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006959 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006960 if (dumpDurationSteps(pw, " ",
6961 " Discharge step durations:", dit.mDischargeSteps, false)) {
6962 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6963 sb, outInt);
6964 }
6965 if (dumpDurationSteps(pw, " ",
6966 " Charge step durations:", dit.mChargeSteps, false)) {
6967 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6968 sb, outInt);
6969 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006970 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006971 } else {
6972 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6973 sb, outInt);
6974 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6975 sb, outInt);
6976 }
6977 }
6978 pw.println();
6979 }
6980 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006981 pw.println("Statistics since last charge:");
6982 pw.println(" System starts: " + getStartCount()
6983 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006984 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6985 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006986 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006987 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006988 }
Mike Mac2f518a2017-09-19 16:06:03 -07006989
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006990 // This is called from BatteryStatsService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006991 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006992 public void dumpCheckinLocked(Context context, PrintWriter pw,
6993 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006994 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006995
6996 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006997 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6998 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006999
Dianne Hackborn13ac0412013-06-25 19:34:49 -07007000 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
7001
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007002 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07007003 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007004 try {
7005 for (int i=0; i<getHistoryStringPoolSize(); i++) {
7006 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
7007 pw.print(HISTORY_STRING_POOL); pw.print(',');
7008 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007009 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007010 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007011 pw.print(",\"");
7012 String str = getHistoryTagPoolString(i);
7013 str = str.replace("\\", "\\\\");
7014 str = str.replace("\"", "\\\"");
7015 pw.print(str);
7016 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007017 pw.println();
7018 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07007019 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007020 } finally {
7021 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08007022 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07007023 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07007024 }
7025
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007026 if ((flags & DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007027 return;
7028 }
7029
Dianne Hackborne4a59512010-12-07 11:08:07 -08007030 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007031 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08007032 for (int i=0; i<apps.size(); i++) {
7033 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007034 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
7035 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08007036 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007037 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
7038 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007039 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007040 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007041 }
7042 SparseArray<? extends Uid> uidStats = getUidStats();
7043 final int NU = uidStats.size();
7044 String[] lineArgs = new String[2];
7045 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007046 int uid = UserHandle.getAppId(uidStats.keyAt(i));
7047 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
7048 if (pkgs != null && !pkgs.second.value) {
7049 pkgs.second.value = true;
7050 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08007051 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007052 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007053 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
7054 (Object[])lineArgs);
7055 }
7056 }
7057 }
7058 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007059 if ((flags & DUMP_DAILY_ONLY) == 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007060 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007061 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07007062 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007063 if (timeRemaining >= 0) {
7064 lineArgs[0] = Long.toString(timeRemaining);
7065 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
7066 (Object[])lineArgs);
7067 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007068 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07007069 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007070 if (timeRemaining >= 0) {
7071 lineArgs[0] = Long.toString(timeRemaining);
7072 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
7073 (Object[])lineArgs);
7074 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007075 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
7076 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08007077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007078 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007079
Kweku Adams71a95312018-04-16 16:54:24 -07007080 /**
7081 * Dump #STATS_SINCE_CHARGED batterystats data to a proto. If the flags include
7082 * DUMP_INCLUDE_HISTORY or DUMP_HISTORY_ONLY, only the history will be dumped.
7083 * @hide
7084 */
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007085 public void dumpProtoLocked(Context context, FileDescriptor fd, List<ApplicationInfo> apps,
Kweku Adams71a95312018-04-16 16:54:24 -07007086 int flags, long histStart) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007087 final ProtoOutputStream proto = new ProtoOutputStream(fd);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007088 prepareForDumpLocked();
7089
Kweku Adams71a95312018-04-16 16:54:24 -07007090 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
7091 dumpProtoHistoryLocked(proto, flags, histStart);
7092 proto.flush();
7093 return;
7094 }
7095
7096 final long bToken = proto.start(BatteryStatsServiceDumpProto.BATTERYSTATS);
7097
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007098 proto.write(BatteryStatsProto.REPORT_VERSION, CHECKIN_VERSION);
7099 proto.write(BatteryStatsProto.PARCEL_VERSION, getParcelVersion());
7100 proto.write(BatteryStatsProto.START_PLATFORM_VERSION, getStartPlatformVersion());
7101 proto.write(BatteryStatsProto.END_PLATFORM_VERSION, getEndPlatformVersion());
7102
Kweku Adams71a95312018-04-16 16:54:24 -07007103 if ((flags & DUMP_DAILY_ONLY) == 0) {
Kweku Adams103351f2017-10-16 14:39:34 -07007104 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false,
7105 (flags & DUMP_DEVICE_WIFI_ONLY) != 0);
7106 helper.create(this);
7107 helper.refreshStats(STATS_SINCE_CHARGED, UserHandle.USER_ALL);
7108
7109 dumpProtoAppsLocked(proto, helper, apps);
7110 dumpProtoSystemLocked(proto, helper);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007111 }
7112
7113 proto.end(bToken);
7114 proto.flush();
7115 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07007116
Kweku Adams103351f2017-10-16 14:39:34 -07007117 private void dumpProtoAppsLocked(ProtoOutputStream proto, BatteryStatsHelper helper,
7118 List<ApplicationInfo> apps) {
7119 final int which = STATS_SINCE_CHARGED;
7120 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7121 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7122 final long rawRealtimeUs = rawRealtimeMs * 1000;
7123 final long batteryUptimeUs = getBatteryUptime(rawUptimeUs);
7124
7125 SparseArray<ArrayList<String>> aidToPackages = new SparseArray<>();
7126 if (apps != null) {
7127 for (int i = 0; i < apps.size(); ++i) {
7128 ApplicationInfo ai = apps.get(i);
7129 int aid = UserHandle.getAppId(ai.uid);
7130 ArrayList<String> pkgs = aidToPackages.get(aid);
7131 if (pkgs == null) {
7132 pkgs = new ArrayList<String>();
7133 aidToPackages.put(aid, pkgs);
7134 }
7135 pkgs.add(ai.packageName);
7136 }
7137 }
7138
7139 SparseArray<BatterySipper> uidToSipper = new SparseArray<>();
7140 final List<BatterySipper> sippers = helper.getUsageList();
7141 if (sippers != null) {
7142 for (int i = 0; i < sippers.size(); ++i) {
7143 final BatterySipper bs = sippers.get(i);
7144 if (bs.drainType != BatterySipper.DrainType.APP) {
7145 // Others are handled by dumpProtoSystemLocked()
7146 continue;
7147 }
7148 uidToSipper.put(bs.uidObj.getUid(), bs);
7149 }
7150 }
7151
7152 SparseArray<? extends Uid> uidStats = getUidStats();
7153 final int n = uidStats.size();
7154 for (int iu = 0; iu < n; ++iu) {
7155 final long uTkn = proto.start(BatteryStatsProto.UIDS);
7156 final Uid u = uidStats.valueAt(iu);
7157
7158 final int uid = uidStats.keyAt(iu);
7159 proto.write(UidProto.UID, uid);
7160
7161 // Print packages and apk stats (UID_DATA & APK_DATA)
7162 ArrayList<String> pkgs = aidToPackages.get(UserHandle.getAppId(uid));
7163 if (pkgs == null) {
7164 pkgs = new ArrayList<String>();
7165 }
7166 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats =
7167 u.getPackageStats();
7168 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7169 String pkg = packageStats.keyAt(ipkg);
7170 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats =
7171 packageStats.valueAt(ipkg).getServiceStats();
7172 if (serviceStats.size() == 0) {
7173 // Due to the way ActivityManagerService logs wakeup alarms, some packages (for
7174 // example, "android") may be included in the packageStats that aren't part of
7175 // the UID. If they don't have any services, then they shouldn't be listed here.
7176 // These packages won't be a part in the pkgs List.
7177 continue;
7178 }
7179
7180 final long pToken = proto.start(UidProto.PACKAGES);
7181 proto.write(UidProto.Package.NAME, pkg);
7182 // Remove from the packages list since we're logging it here.
7183 pkgs.remove(pkg);
7184
7185 for (int isvc = serviceStats.size() - 1; isvc >= 0; --isvc) {
7186 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
Kweku Adams14f3d222018-03-22 14:12:55 -07007187
7188 final long startTimeMs = roundUsToMs(ss.getStartTime(batteryUptimeUs, which));
7189 final int starts = ss.getStarts(which);
7190 final int launches = ss.getLaunches(which);
7191 if (startTimeMs == 0 && starts == 0 && launches == 0) {
7192 continue;
7193 }
7194
Kweku Adams103351f2017-10-16 14:39:34 -07007195 long sToken = proto.start(UidProto.Package.SERVICES);
7196
7197 proto.write(UidProto.Package.Service.NAME, serviceStats.keyAt(isvc));
Kweku Adams14f3d222018-03-22 14:12:55 -07007198 proto.write(UidProto.Package.Service.START_DURATION_MS, startTimeMs);
7199 proto.write(UidProto.Package.Service.START_COUNT, starts);
7200 proto.write(UidProto.Package.Service.LAUNCH_COUNT, launches);
Kweku Adams103351f2017-10-16 14:39:34 -07007201
7202 proto.end(sToken);
7203 }
7204 proto.end(pToken);
7205 }
7206 // Print any remaining packages that weren't in the packageStats map. pkgs is pulled
7207 // from PackageManager data. Packages are only included in packageStats if there was
7208 // specific data tracked for them (services and wakeup alarms, etc.).
7209 for (String p : pkgs) {
7210 final long pToken = proto.start(UidProto.PACKAGES);
7211 proto.write(UidProto.Package.NAME, p);
7212 proto.end(pToken);
7213 }
7214
7215 // Total wakelock data (AGGREGATED_WAKELOCK_DATA)
7216 if (u.getAggregatedPartialWakelockTimer() != null) {
7217 final Timer timer = u.getAggregatedPartialWakelockTimer();
7218 // Times are since reset (regardless of 'which')
7219 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
7220 final Timer bgTimer = timer.getSubTimer();
7221 final long bgTimeMs = bgTimer != null
7222 ? bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
7223 final long awToken = proto.start(UidProto.AGGREGATED_WAKELOCK);
7224 proto.write(UidProto.AggregatedWakelock.PARTIAL_DURATION_MS, totTimeMs);
7225 proto.write(UidProto.AggregatedWakelock.BACKGROUND_PARTIAL_DURATION_MS, bgTimeMs);
7226 proto.end(awToken);
7227 }
7228
7229 // Audio (AUDIO_DATA)
7230 dumpTimer(proto, UidProto.AUDIO, u.getAudioTurnedOnTimer(), rawRealtimeUs, which);
7231
7232 // Bluetooth Controller (BLUETOOTH_CONTROLLER_DATA)
7233 dumpControllerActivityProto(proto, UidProto.BLUETOOTH_CONTROLLER,
7234 u.getBluetoothControllerActivity(), which);
7235
7236 // BLE scans (BLUETOOTH_MISC_DATA) (uses totalDurationMsLocked and MaxDurationMsLocked)
7237 final Timer bleTimer = u.getBluetoothScanTimer();
7238 if (bleTimer != null) {
7239 final long bmToken = proto.start(UidProto.BLUETOOTH_MISC);
7240
7241 dumpTimer(proto, UidProto.BluetoothMisc.APPORTIONED_BLE_SCAN, bleTimer,
7242 rawRealtimeUs, which);
7243 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN,
7244 u.getBluetoothScanBackgroundTimer(), rawRealtimeUs, which);
7245 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
7246 dumpTimer(proto, UidProto.BluetoothMisc.UNOPTIMIZED_BLE_SCAN,
7247 u.getBluetoothUnoptimizedScanTimer(), rawRealtimeUs, which);
7248 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
7249 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_UNOPTIMIZED_BLE_SCAN,
7250 u.getBluetoothUnoptimizedScanBackgroundTimer(), rawRealtimeUs, which);
7251 // Result counters
7252 proto.write(UidProto.BluetoothMisc.BLE_SCAN_RESULT_COUNT,
7253 u.getBluetoothScanResultCounter() != null
7254 ? u.getBluetoothScanResultCounter().getCountLocked(which) : 0);
7255 proto.write(UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN_RESULT_COUNT,
7256 u.getBluetoothScanResultBgCounter() != null
7257 ? u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0);
7258
7259 proto.end(bmToken);
7260 }
7261
7262 // Camera (CAMERA_DATA)
7263 dumpTimer(proto, UidProto.CAMERA, u.getCameraTurnedOnTimer(), rawRealtimeUs, which);
7264
7265 // CPU stats (CPU_DATA & CPU_TIMES_AT_FREQ_DATA)
7266 final long cpuToken = proto.start(UidProto.CPU);
7267 proto.write(UidProto.Cpu.USER_DURATION_MS, roundUsToMs(u.getUserCpuTimeUs(which)));
7268 proto.write(UidProto.Cpu.SYSTEM_DURATION_MS, roundUsToMs(u.getSystemCpuTimeUs(which)));
7269
7270 final long[] cpuFreqs = getCpuFreqs();
7271 if (cpuFreqs != null) {
7272 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
7273 // If total cpuFreqTimes is null, then we don't need to check for
7274 // screenOffCpuFreqTimes.
7275 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
7276 long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
7277 if (screenOffCpuFreqTimeMs == null) {
7278 screenOffCpuFreqTimeMs = new long[cpuFreqTimeMs.length];
7279 }
7280 for (int ic = 0; ic < cpuFreqTimeMs.length; ++ic) {
7281 long cToken = proto.start(UidProto.Cpu.BY_FREQUENCY);
7282 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7283 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7284 cpuFreqTimeMs[ic]);
7285 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7286 screenOffCpuFreqTimeMs[ic]);
7287 proto.end(cToken);
7288 }
7289 }
7290 }
Sudheer Shanka6d658d72018-01-01 01:36:49 -08007291
7292 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
7293 final long[] timesMs = u.getCpuFreqTimes(which, procState);
7294 if (timesMs != null && timesMs.length == cpuFreqs.length) {
7295 long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(which, procState);
7296 if (screenOffTimesMs == null) {
7297 screenOffTimesMs = new long[timesMs.length];
7298 }
7299 final long procToken = proto.start(UidProto.Cpu.BY_PROCESS_STATE);
7300 proto.write(UidProto.Cpu.ByProcessState.PROCESS_STATE, procState);
7301 for (int ic = 0; ic < timesMs.length; ++ic) {
7302 long cToken = proto.start(UidProto.Cpu.ByProcessState.BY_FREQUENCY);
7303 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7304 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7305 timesMs[ic]);
7306 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7307 screenOffTimesMs[ic]);
7308 proto.end(cToken);
7309 }
7310 proto.end(procToken);
7311 }
7312 }
Kweku Adams103351f2017-10-16 14:39:34 -07007313 proto.end(cpuToken);
7314
7315 // Flashlight (FLASHLIGHT_DATA)
7316 dumpTimer(proto, UidProto.FLASHLIGHT, u.getFlashlightTurnedOnTimer(),
7317 rawRealtimeUs, which);
7318
7319 // Foreground activity (FOREGROUND_ACTIVITY_DATA)
7320 dumpTimer(proto, UidProto.FOREGROUND_ACTIVITY, u.getForegroundActivityTimer(),
7321 rawRealtimeUs, which);
7322
7323 // Foreground service (FOREGROUND_SERVICE_DATA)
7324 dumpTimer(proto, UidProto.FOREGROUND_SERVICE, u.getForegroundServiceTimer(),
7325 rawRealtimeUs, which);
7326
7327 // Job completion (JOB_COMPLETION_DATA)
7328 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
7329 final int[] reasons = new int[]{
7330 JobParameters.REASON_CANCELED,
7331 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED,
7332 JobParameters.REASON_PREEMPT,
7333 JobParameters.REASON_TIMEOUT,
7334 JobParameters.REASON_DEVICE_IDLE,
7335 };
7336 for (int ic = 0; ic < completions.size(); ++ic) {
7337 SparseIntArray types = completions.valueAt(ic);
7338 if (types != null) {
7339 final long jcToken = proto.start(UidProto.JOB_COMPLETION);
7340
7341 proto.write(UidProto.JobCompletion.NAME, completions.keyAt(ic));
7342
7343 for (int r : reasons) {
7344 long rToken = proto.start(UidProto.JobCompletion.REASON_COUNT);
7345 proto.write(UidProto.JobCompletion.ReasonCount.NAME, r);
7346 proto.write(UidProto.JobCompletion.ReasonCount.COUNT, types.get(r, 0));
7347 proto.end(rToken);
7348 }
7349
7350 proto.end(jcToken);
7351 }
7352 }
7353
7354 // Scheduled jobs (JOB_DATA)
7355 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
7356 for (int ij = jobs.size() - 1; ij >= 0; --ij) {
7357 final Timer timer = jobs.valueAt(ij);
7358 final Timer bgTimer = timer.getSubTimer();
7359 final long jToken = proto.start(UidProto.JOBS);
7360
7361 proto.write(UidProto.Job.NAME, jobs.keyAt(ij));
7362 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7363 dumpTimer(proto, UidProto.Job.TOTAL, timer, rawRealtimeUs, which);
7364 dumpTimer(proto, UidProto.Job.BACKGROUND, bgTimer, rawRealtimeUs, which);
7365
7366 proto.end(jToken);
7367 }
7368
7369 // Modem Controller (MODEM_CONTROLLER_DATA)
7370 dumpControllerActivityProto(proto, UidProto.MODEM_CONTROLLER,
7371 u.getModemControllerActivity(), which);
7372
7373 // Network stats (NETWORK_DATA)
7374 final long nToken = proto.start(UidProto.NETWORK);
7375 proto.write(UidProto.Network.MOBILE_BYTES_RX,
7376 u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7377 proto.write(UidProto.Network.MOBILE_BYTES_TX,
7378 u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7379 proto.write(UidProto.Network.WIFI_BYTES_RX,
7380 u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7381 proto.write(UidProto.Network.WIFI_BYTES_TX,
7382 u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7383 proto.write(UidProto.Network.BT_BYTES_RX,
7384 u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7385 proto.write(UidProto.Network.BT_BYTES_TX,
7386 u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
7387 proto.write(UidProto.Network.MOBILE_PACKETS_RX,
7388 u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7389 proto.write(UidProto.Network.MOBILE_PACKETS_TX,
7390 u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7391 proto.write(UidProto.Network.WIFI_PACKETS_RX,
7392 u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7393 proto.write(UidProto.Network.WIFI_PACKETS_TX,
7394 u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7395 proto.write(UidProto.Network.MOBILE_ACTIVE_DURATION_MS,
7396 roundUsToMs(u.getMobileRadioActiveTime(which)));
7397 proto.write(UidProto.Network.MOBILE_ACTIVE_COUNT,
7398 u.getMobileRadioActiveCount(which));
7399 proto.write(UidProto.Network.MOBILE_WAKEUP_COUNT,
7400 u.getMobileRadioApWakeupCount(which));
7401 proto.write(UidProto.Network.WIFI_WAKEUP_COUNT,
7402 u.getWifiRadioApWakeupCount(which));
7403 proto.write(UidProto.Network.MOBILE_BYTES_BG_RX,
7404 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA, which));
7405 proto.write(UidProto.Network.MOBILE_BYTES_BG_TX,
7406 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA, which));
7407 proto.write(UidProto.Network.WIFI_BYTES_BG_RX,
7408 u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which));
7409 proto.write(UidProto.Network.WIFI_BYTES_BG_TX,
7410 u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which));
7411 proto.write(UidProto.Network.MOBILE_PACKETS_BG_RX,
7412 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA, which));
7413 proto.write(UidProto.Network.MOBILE_PACKETS_BG_TX,
7414 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA, which));
7415 proto.write(UidProto.Network.WIFI_PACKETS_BG_RX,
7416 u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA, which));
7417 proto.write(UidProto.Network.WIFI_PACKETS_BG_TX,
7418 u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA, which));
7419 proto.end(nToken);
7420
7421 // Power use item (POWER_USE_ITEM_DATA)
7422 BatterySipper bs = uidToSipper.get(uid);
7423 if (bs != null) {
7424 final long bsToken = proto.start(UidProto.POWER_USE_ITEM);
7425 proto.write(UidProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7426 proto.write(UidProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7427 proto.write(UidProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7428 proto.write(UidProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7429 bs.proportionalSmearMah);
7430 proto.end(bsToken);
7431 }
7432
7433 // Processes (PROCESS_DATA)
7434 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats =
7435 u.getProcessStats();
7436 for (int ipr = processStats.size() - 1; ipr >= 0; --ipr) {
7437 final Uid.Proc ps = processStats.valueAt(ipr);
7438 final long prToken = proto.start(UidProto.PROCESS);
7439
7440 proto.write(UidProto.Process.NAME, processStats.keyAt(ipr));
7441 proto.write(UidProto.Process.USER_DURATION_MS, ps.getUserTime(which));
7442 proto.write(UidProto.Process.SYSTEM_DURATION_MS, ps.getSystemTime(which));
7443 proto.write(UidProto.Process.FOREGROUND_DURATION_MS, ps.getForegroundTime(which));
7444 proto.write(UidProto.Process.START_COUNT, ps.getStarts(which));
7445 proto.write(UidProto.Process.ANR_COUNT, ps.getNumAnrs(which));
7446 proto.write(UidProto.Process.CRASH_COUNT, ps.getNumCrashes(which));
7447
7448 proto.end(prToken);
7449 }
7450
7451 // Sensors (SENSOR_DATA)
7452 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
7453 for (int ise = 0; ise < sensors.size(); ++ise) {
7454 final Uid.Sensor se = sensors.valueAt(ise);
7455 final Timer timer = se.getSensorTime();
7456 if (timer == null) {
7457 continue;
7458 }
7459 final Timer bgTimer = se.getSensorBackgroundTime();
7460 final int sensorNumber = sensors.keyAt(ise);
7461 final long seToken = proto.start(UidProto.SENSORS);
7462
7463 proto.write(UidProto.Sensor.ID, sensorNumber);
7464 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7465 dumpTimer(proto, UidProto.Sensor.APPORTIONED, timer, rawRealtimeUs, which);
7466 dumpTimer(proto, UidProto.Sensor.BACKGROUND, bgTimer, rawRealtimeUs, which);
7467
7468 proto.end(seToken);
7469 }
7470
7471 // State times (STATE_TIME_DATA)
7472 for (int ips = 0; ips < Uid.NUM_PROCESS_STATE; ++ips) {
7473 long durMs = roundUsToMs(u.getProcessStateTime(ips, rawRealtimeUs, which));
7474 if (durMs == 0) {
7475 continue;
7476 }
7477 final long stToken = proto.start(UidProto.STATES);
7478 proto.write(UidProto.StateTime.STATE, ips);
7479 proto.write(UidProto.StateTime.DURATION_MS, durMs);
7480 proto.end(stToken);
7481 }
7482
7483 // Syncs (SYNC_DATA)
7484 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
7485 for (int isy = syncs.size() - 1; isy >= 0; --isy) {
7486 final Timer timer = syncs.valueAt(isy);
7487 final Timer bgTimer = timer.getSubTimer();
7488 final long syToken = proto.start(UidProto.SYNCS);
7489
7490 proto.write(UidProto.Sync.NAME, syncs.keyAt(isy));
7491 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7492 dumpTimer(proto, UidProto.Sync.TOTAL, timer, rawRealtimeUs, which);
7493 dumpTimer(proto, UidProto.Sync.BACKGROUND, bgTimer, rawRealtimeUs, which);
7494
7495 proto.end(syToken);
7496 }
7497
7498 // User activity (USER_ACTIVITY_DATA)
7499 if (u.hasUserActivity()) {
7500 for (int i = 0; i < Uid.NUM_USER_ACTIVITY_TYPES; ++i) {
7501 int val = u.getUserActivityCount(i, which);
7502 if (val != 0) {
7503 final long uaToken = proto.start(UidProto.USER_ACTIVITY);
7504 proto.write(UidProto.UserActivity.NAME, i);
7505 proto.write(UidProto.UserActivity.COUNT, val);
7506 proto.end(uaToken);
7507 }
7508 }
7509 }
7510
7511 // Vibrator (VIBRATOR_DATA)
7512 dumpTimer(proto, UidProto.VIBRATOR, u.getVibratorOnTimer(), rawRealtimeUs, which);
7513
7514 // Video (VIDEO_DATA)
7515 dumpTimer(proto, UidProto.VIDEO, u.getVideoTurnedOnTimer(), rawRealtimeUs, which);
7516
7517 // Wakelocks (WAKELOCK_DATA)
7518 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
7519 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7520 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7521 final long wToken = proto.start(UidProto.WAKELOCKS);
7522 proto.write(UidProto.Wakelock.NAME, wakelocks.keyAt(iw));
7523 dumpTimer(proto, UidProto.Wakelock.FULL, wl.getWakeTime(WAKE_TYPE_FULL),
7524 rawRealtimeUs, which);
7525 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7526 if (pTimer != null) {
7527 dumpTimer(proto, UidProto.Wakelock.PARTIAL, pTimer, rawRealtimeUs, which);
7528 dumpTimer(proto, UidProto.Wakelock.BACKGROUND_PARTIAL, pTimer.getSubTimer(),
7529 rawRealtimeUs, which);
7530 }
7531 dumpTimer(proto, UidProto.Wakelock.WINDOW, wl.getWakeTime(WAKE_TYPE_WINDOW),
7532 rawRealtimeUs, which);
7533 proto.end(wToken);
7534 }
7535
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007536 // Wifi Multicast Wakelock (WIFI_MULTICAST_WAKELOCK_DATA)
7537 dumpTimer(proto, UidProto.WIFI_MULTICAST_WAKELOCK, u.getMulticastWakelockStats(),
7538 rawRealtimeUs, which);
7539
Kweku Adams103351f2017-10-16 14:39:34 -07007540 // Wakeup alarms (WAKEUP_ALARM_DATA)
7541 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7542 final Uid.Pkg ps = packageStats.valueAt(ipkg);
7543 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
7544 for (int iwa = alarms.size() - 1; iwa >= 0; --iwa) {
7545 final long waToken = proto.start(UidProto.WAKEUP_ALARM);
7546 proto.write(UidProto.WakeupAlarm.NAME, alarms.keyAt(iwa));
7547 proto.write(UidProto.WakeupAlarm.COUNT,
7548 alarms.valueAt(iwa).getCountLocked(which));
7549 proto.end(waToken);
7550 }
7551 }
7552
7553 // Wifi Controller (WIFI_CONTROLLER_DATA)
7554 dumpControllerActivityProto(proto, UidProto.WIFI_CONTROLLER,
7555 u.getWifiControllerActivity(), which);
7556
7557 // Wifi data (WIFI_DATA)
7558 final long wToken = proto.start(UidProto.WIFI);
7559 proto.write(UidProto.Wifi.FULL_WIFI_LOCK_DURATION_MS,
7560 roundUsToMs(u.getFullWifiLockTime(rawRealtimeUs, which)));
7561 dumpTimer(proto, UidProto.Wifi.APPORTIONED_SCAN, u.getWifiScanTimer(),
7562 rawRealtimeUs, which);
7563 proto.write(UidProto.Wifi.RUNNING_DURATION_MS,
7564 roundUsToMs(u.getWifiRunningTime(rawRealtimeUs, which)));
7565 dumpTimer(proto, UidProto.Wifi.BACKGROUND_SCAN, u.getWifiScanBackgroundTimer(),
7566 rawRealtimeUs, which);
7567 proto.end(wToken);
7568
7569 proto.end(uTkn);
7570 }
7571 }
7572
Kweku Adams71a95312018-04-16 16:54:24 -07007573 private void dumpProtoHistoryLocked(ProtoOutputStream proto, int flags, long histStart) {
7574 if (!startIteratingHistoryLocked()) {
7575 return;
7576 }
7577
7578 proto.write(BatteryStatsServiceDumpHistoryProto.REPORT_VERSION, CHECKIN_VERSION);
7579 proto.write(BatteryStatsServiceDumpHistoryProto.PARCEL_VERSION, getParcelVersion());
7580 proto.write(BatteryStatsServiceDumpHistoryProto.START_PLATFORM_VERSION,
7581 getStartPlatformVersion());
7582 proto.write(BatteryStatsServiceDumpHistoryProto.END_PLATFORM_VERSION,
7583 getEndPlatformVersion());
7584 try {
7585 long token;
7586 // History string pool (HISTORY_STRING_POOL)
7587 for (int i = 0; i < getHistoryStringPoolSize(); ++i) {
7588 token = proto.start(BatteryStatsServiceDumpHistoryProto.KEYS);
7589 proto.write(BatteryStatsServiceDumpHistoryProto.Key.INDEX, i);
7590 proto.write(BatteryStatsServiceDumpHistoryProto.Key.UID, getHistoryTagPoolUid(i));
7591 proto.write(BatteryStatsServiceDumpHistoryProto.Key.TAG,
7592 getHistoryTagPoolString(i));
7593 proto.end(token);
7594 }
7595
7596 // History data (HISTORY_DATA)
7597 final HistoryPrinter hprinter = new HistoryPrinter();
7598 final HistoryItem rec = new HistoryItem();
7599 long lastTime = -1;
7600 long baseTime = -1;
7601 boolean printed = false;
7602 HistoryEventTracker tracker = null;
7603 while (getNextHistoryLocked(rec)) {
7604 lastTime = rec.time;
7605 if (baseTime < 0) {
7606 baseTime = lastTime;
7607 }
7608 if (rec.time >= histStart) {
7609 if (histStart >= 0 && !printed) {
7610 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
7611 || rec.cmd == HistoryItem.CMD_RESET
7612 || rec.cmd == HistoryItem.CMD_START
7613 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
7614 printed = true;
7615 hprinter.printNextItem(proto, rec, baseTime,
7616 (flags & DUMP_VERBOSE) != 0);
7617 rec.cmd = HistoryItem.CMD_UPDATE;
7618 } else if (rec.currentTime != 0) {
7619 printed = true;
7620 byte cmd = rec.cmd;
7621 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
7622 hprinter.printNextItem(proto, rec, baseTime,
7623 (flags & DUMP_VERBOSE) != 0);
7624 rec.cmd = cmd;
7625 }
7626 if (tracker != null) {
7627 if (rec.cmd != HistoryItem.CMD_UPDATE) {
7628 hprinter.printNextItem(proto, rec, baseTime,
7629 (flags & DUMP_VERBOSE) != 0);
7630 rec.cmd = HistoryItem.CMD_UPDATE;
7631 }
7632 int oldEventCode = rec.eventCode;
7633 HistoryTag oldEventTag = rec.eventTag;
7634 rec.eventTag = new HistoryTag();
7635 for (int i = 0; i < HistoryItem.EVENT_COUNT; i++) {
7636 HashMap<String, SparseIntArray> active =
7637 tracker.getStateForEvent(i);
7638 if (active == null) {
7639 continue;
7640 }
7641 for (HashMap.Entry<String, SparseIntArray> ent
7642 : active.entrySet()) {
7643 SparseIntArray uids = ent.getValue();
7644 for (int j = 0; j < uids.size(); j++) {
7645 rec.eventCode = i;
7646 rec.eventTag.string = ent.getKey();
7647 rec.eventTag.uid = uids.keyAt(j);
7648 rec.eventTag.poolIdx = uids.valueAt(j);
7649 hprinter.printNextItem(proto, rec, baseTime,
7650 (flags & DUMP_VERBOSE) != 0);
7651 rec.wakeReasonTag = null;
7652 rec.wakelockTag = null;
7653 }
7654 }
7655 }
7656 rec.eventCode = oldEventCode;
7657 rec.eventTag = oldEventTag;
7658 tracker = null;
7659 }
7660 }
7661 hprinter.printNextItem(proto, rec, baseTime,
7662 (flags & DUMP_VERBOSE) != 0);
7663 }
7664 }
7665 if (histStart >= 0) {
7666 commitCurrentHistoryBatchLocked();
7667 proto.write(BatteryStatsServiceDumpHistoryProto.CSV_LINES,
7668 "NEXT: " + (lastTime + 1));
7669 }
7670 } finally {
7671 finishIteratingHistoryLocked();
7672 }
7673 }
7674
Kweku Adams103351f2017-10-16 14:39:34 -07007675 private void dumpProtoSystemLocked(ProtoOutputStream proto, BatteryStatsHelper helper) {
Kweku Adams87b19ec2017-10-09 12:40:03 -07007676 final long sToken = proto.start(BatteryStatsProto.SYSTEM);
7677 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7678 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7679 final long rawRealtimeUs = rawRealtimeMs * 1000;
7680 final int which = STATS_SINCE_CHARGED;
7681
7682 // Battery data (BATTERY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007683 final long bToken = proto.start(SystemProto.BATTERY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007684 proto.write(SystemProto.Battery.START_CLOCK_TIME_MS, getStartClockTime());
7685 proto.write(SystemProto.Battery.START_COUNT, getStartCount());
7686 proto.write(SystemProto.Battery.TOTAL_REALTIME_MS,
7687 computeRealtime(rawRealtimeUs, which) / 1000);
7688 proto.write(SystemProto.Battery.TOTAL_UPTIME_MS,
7689 computeUptime(rawUptimeUs, which) / 1000);
7690 proto.write(SystemProto.Battery.BATTERY_REALTIME_MS,
7691 computeBatteryRealtime(rawRealtimeUs, which) / 1000);
7692 proto.write(SystemProto.Battery.BATTERY_UPTIME_MS,
7693 computeBatteryUptime(rawUptimeUs, which) / 1000);
7694 proto.write(SystemProto.Battery.SCREEN_OFF_REALTIME_MS,
7695 computeBatteryScreenOffRealtime(rawRealtimeUs, which) / 1000);
7696 proto.write(SystemProto.Battery.SCREEN_OFF_UPTIME_MS,
7697 computeBatteryScreenOffUptime(rawUptimeUs, which) / 1000);
7698 proto.write(SystemProto.Battery.SCREEN_DOZE_DURATION_MS,
7699 getScreenDozeTime(rawRealtimeUs, which) / 1000);
7700 proto.write(SystemProto.Battery.ESTIMATED_BATTERY_CAPACITY_MAH,
7701 getEstimatedBatteryCapacity());
7702 proto.write(SystemProto.Battery.MIN_LEARNED_BATTERY_CAPACITY_UAH,
7703 getMinLearnedBatteryCapacity());
7704 proto.write(SystemProto.Battery.MAX_LEARNED_BATTERY_CAPACITY_UAH,
7705 getMaxLearnedBatteryCapacity());
Kweku Adams103351f2017-10-16 14:39:34 -07007706 proto.end(bToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007707
7708 // Battery discharge (BATTERY_DISCHARGE_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007709 final long bdToken = proto.start(SystemProto.BATTERY_DISCHARGE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007710 proto.write(SystemProto.BatteryDischarge.LOWER_BOUND_SINCE_CHARGE,
7711 getLowDischargeAmountSinceCharge());
7712 proto.write(SystemProto.BatteryDischarge.UPPER_BOUND_SINCE_CHARGE,
7713 getHighDischargeAmountSinceCharge());
7714 proto.write(SystemProto.BatteryDischarge.SCREEN_ON_SINCE_CHARGE,
7715 getDischargeAmountScreenOnSinceCharge());
7716 proto.write(SystemProto.BatteryDischarge.SCREEN_OFF_SINCE_CHARGE,
7717 getDischargeAmountScreenOffSinceCharge());
7718 proto.write(SystemProto.BatteryDischarge.SCREEN_DOZE_SINCE_CHARGE,
7719 getDischargeAmountScreenDozeSinceCharge());
7720 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH,
7721 getUahDischarge(which) / 1000);
7722 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_OFF,
7723 getUahDischargeScreenOff(which) / 1000);
7724 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_DOZE,
7725 getUahDischargeScreenDoze(which) / 1000);
Mike Ma15313c92017-11-15 17:58:21 -08007726 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_LIGHT_DOZE,
7727 getUahDischargeLightDoze(which) / 1000);
7728 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_DEEP_DOZE,
7729 getUahDischargeDeepDoze(which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007730 proto.end(bdToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007731
7732 // Time remaining
7733 long timeRemainingUs = computeChargeTimeRemaining(rawRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -07007734 // These are part of a oneof, so we should only set one of them.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007735 if (timeRemainingUs >= 0) {
7736 // Charge time remaining (CHARGE_TIME_REMAIN_DATA)
7737 proto.write(SystemProto.CHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7738 } else {
7739 timeRemainingUs = computeBatteryTimeRemaining(rawRealtimeUs);
7740 // Discharge time remaining (DISCHARGE_TIME_REMAIN_DATA)
7741 if (timeRemainingUs >= 0) {
7742 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7743 } else {
7744 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, -1);
7745 }
7746 }
7747
7748 // Charge step (CHARGE_STEP_DATA)
7749 dumpDurationSteps(proto, SystemProto.CHARGE_STEP, getChargeLevelStepTracker());
7750
7751 // Phone data connection (DATA_CONNECTION_TIME_DATA and DATA_CONNECTION_COUNT_DATA)
7752 for (int i = 0; i < NUM_DATA_CONNECTION_TYPES; ++i) {
Tej Singheee317b2018-03-07 19:28:05 -08007753 // Map OTHER to TelephonyManager.NETWORK_TYPE_UNKNOWN and mark NONE as a boolean.
7754 boolean isNone = (i == DATA_CONNECTION_NONE);
7755 int telephonyNetworkType = i;
7756 if (i == DATA_CONNECTION_OTHER) {
7757 telephonyNetworkType = TelephonyManager.NETWORK_TYPE_UNKNOWN;
7758 }
Kweku Adams103351f2017-10-16 14:39:34 -07007759 final long pdcToken = proto.start(SystemProto.DATA_CONNECTION);
Tej Singheee317b2018-03-07 19:28:05 -08007760 if (isNone) {
7761 proto.write(SystemProto.DataConnection.IS_NONE, isNone);
7762 } else {
7763 proto.write(SystemProto.DataConnection.NAME, telephonyNetworkType);
7764 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07007765 dumpTimer(proto, SystemProto.DataConnection.TOTAL, getPhoneDataConnectionTimer(i),
7766 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007767 proto.end(pdcToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007768 }
7769
7770 // Discharge step (DISCHARGE_STEP_DATA)
7771 dumpDurationSteps(proto, SystemProto.DISCHARGE_STEP, getDischargeLevelStepTracker());
7772
7773 // CPU frequencies (GLOBAL_CPU_FREQ_DATA)
7774 final long[] cpuFreqs = getCpuFreqs();
7775 if (cpuFreqs != null) {
7776 for (long i : cpuFreqs) {
7777 proto.write(SystemProto.CPU_FREQUENCY, i);
7778 }
7779 }
7780
7781 // Bluetooth controller (GLOBAL_BLUETOOTH_CONTROLLER_DATA)
7782 dumpControllerActivityProto(proto, SystemProto.GLOBAL_BLUETOOTH_CONTROLLER,
7783 getBluetoothControllerActivity(), which);
7784
7785 // Modem controller (GLOBAL_MODEM_CONTROLLER_DATA)
7786 dumpControllerActivityProto(proto, SystemProto.GLOBAL_MODEM_CONTROLLER,
7787 getModemControllerActivity(), which);
7788
7789 // Global network data (GLOBAL_NETWORK_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007790 final long gnToken = proto.start(SystemProto.GLOBAL_NETWORK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007791 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_RX,
7792 getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7793 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_TX,
7794 getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7795 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_RX,
7796 getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7797 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_TX,
7798 getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7799 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_RX,
7800 getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7801 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_TX,
7802 getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7803 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_RX,
7804 getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7805 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_TX,
7806 getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7807 proto.write(SystemProto.GlobalNetwork.BT_BYTES_RX,
7808 getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7809 proto.write(SystemProto.GlobalNetwork.BT_BYTES_TX,
7810 getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
Kweku Adams103351f2017-10-16 14:39:34 -07007811 proto.end(gnToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007812
7813 // Wifi controller (GLOBAL_WIFI_CONTROLLER_DATA)
7814 dumpControllerActivityProto(proto, SystemProto.GLOBAL_WIFI_CONTROLLER,
7815 getWifiControllerActivity(), which);
7816
7817
7818 // Global wifi (GLOBAL_WIFI_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007819 final long gwToken = proto.start(SystemProto.GLOBAL_WIFI);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007820 proto.write(SystemProto.GlobalWifi.ON_DURATION_MS,
7821 getWifiOnTime(rawRealtimeUs, which) / 1000);
7822 proto.write(SystemProto.GlobalWifi.RUNNING_DURATION_MS,
7823 getGlobalWifiRunningTime(rawRealtimeUs, which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007824 proto.end(gwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007825
7826 // Kernel wakelock (KERNEL_WAKELOCK_DATA)
7827 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
7828 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007829 final long kwToken = proto.start(SystemProto.KERNEL_WAKELOCK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007830 proto.write(SystemProto.KernelWakelock.NAME, ent.getKey());
7831 dumpTimer(proto, SystemProto.KernelWakelock.TOTAL, ent.getValue(),
7832 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007833 proto.end(kwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007834 }
7835
7836 // Misc (MISC_DATA)
7837 // Calculate wakelock times across all uids.
7838 long fullWakeLockTimeTotalUs = 0;
7839 long partialWakeLockTimeTotalUs = 0;
7840
7841 final SparseArray<? extends Uid> uidStats = getUidStats();
7842 for (int iu = 0; iu < uidStats.size(); iu++) {
7843 final Uid u = uidStats.valueAt(iu);
7844
7845 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks =
7846 u.getWakelockStats();
7847 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7848 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7849
7850 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
7851 if (fullWakeTimer != null) {
7852 fullWakeLockTimeTotalUs += fullWakeTimer.getTotalTimeLocked(rawRealtimeUs,
7853 which);
7854 }
7855
7856 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7857 if (partialWakeTimer != null) {
7858 partialWakeLockTimeTotalUs += partialWakeTimer.getTotalTimeLocked(
7859 rawRealtimeUs, which);
7860 }
7861 }
7862 }
Kweku Adams103351f2017-10-16 14:39:34 -07007863 final long mToken = proto.start(SystemProto.MISC);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007864 proto.write(SystemProto.Misc.SCREEN_ON_DURATION_MS,
7865 getScreenOnTime(rawRealtimeUs, which) / 1000);
7866 proto.write(SystemProto.Misc.PHONE_ON_DURATION_MS,
7867 getPhoneOnTime(rawRealtimeUs, which) / 1000);
7868 proto.write(SystemProto.Misc.FULL_WAKELOCK_TOTAL_DURATION_MS,
7869 fullWakeLockTimeTotalUs / 1000);
7870 proto.write(SystemProto.Misc.PARTIAL_WAKELOCK_TOTAL_DURATION_MS,
7871 partialWakeLockTimeTotalUs / 1000);
7872 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_DURATION_MS,
7873 getMobileRadioActiveTime(rawRealtimeUs, which) / 1000);
7874 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_ADJUSTED_TIME_MS,
7875 getMobileRadioActiveAdjustedTime(which) / 1000);
7876 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_COUNT,
7877 getMobileRadioActiveCount(which));
7878 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_UNKNOWN_DURATION_MS,
7879 getMobileRadioActiveUnknownTime(which) / 1000);
7880 proto.write(SystemProto.Misc.INTERACTIVE_DURATION_MS,
7881 getInteractiveTime(rawRealtimeUs, which) / 1000);
7882 proto.write(SystemProto.Misc.BATTERY_SAVER_MODE_ENABLED_DURATION_MS,
7883 getPowerSaveModeEnabledTime(rawRealtimeUs, which) / 1000);
7884 proto.write(SystemProto.Misc.NUM_CONNECTIVITY_CHANGES,
7885 getNumConnectivityChange(which));
7886 proto.write(SystemProto.Misc.DEEP_DOZE_ENABLED_DURATION_MS,
7887 getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7888 proto.write(SystemProto.Misc.DEEP_DOZE_COUNT,
7889 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
7890 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_DURATION_MS,
7891 getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7892 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_COUNT,
7893 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
7894 proto.write(SystemProto.Misc.LONGEST_DEEP_DOZE_DURATION_MS,
7895 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
7896 proto.write(SystemProto.Misc.LIGHT_DOZE_ENABLED_DURATION_MS,
7897 getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7898 proto.write(SystemProto.Misc.LIGHT_DOZE_COUNT,
7899 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
7900 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_DURATION_MS,
7901 getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7902 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_COUNT,
7903 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
7904 proto.write(SystemProto.Misc.LONGEST_LIGHT_DOZE_DURATION_MS,
7905 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
Kweku Adams103351f2017-10-16 14:39:34 -07007906 proto.end(mToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007907
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007908 // Wifi multicast wakelock total stats (WIFI_MULTICAST_WAKELOCK_TOTAL_DATA)
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08007909 final long multicastWakeLockTimeTotalUs =
7910 getWifiMulticastWakelockTime(rawRealtimeUs, which);
7911 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007912 final long wmctToken = proto.start(SystemProto.WIFI_MULTICAST_WAKELOCK_TOTAL);
7913 proto.write(SystemProto.WifiMulticastWakelockTotal.DURATION_MS,
7914 multicastWakeLockTimeTotalUs / 1000);
7915 proto.write(SystemProto.WifiMulticastWakelockTotal.COUNT,
7916 multicastWakeLockCountTotal);
7917 proto.end(wmctToken);
7918
Kweku Adams87b19ec2017-10-09 12:40:03 -07007919 // Power use item (POWER_USE_ITEM_DATA)
7920 final List<BatterySipper> sippers = helper.getUsageList();
7921 if (sippers != null) {
7922 for (int i = 0; i < sippers.size(); ++i) {
7923 final BatterySipper bs = sippers.get(i);
7924 int n = SystemProto.PowerUseItem.UNKNOWN_SIPPER;
7925 int uid = 0;
7926 switch (bs.drainType) {
Kweku Adamsef728952018-04-10 17:56:16 -07007927 case AMBIENT_DISPLAY:
7928 n = SystemProto.PowerUseItem.AMBIENT_DISPLAY;
7929 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07007930 case IDLE:
7931 n = SystemProto.PowerUseItem.IDLE;
7932 break;
7933 case CELL:
7934 n = SystemProto.PowerUseItem.CELL;
7935 break;
7936 case PHONE:
7937 n = SystemProto.PowerUseItem.PHONE;
7938 break;
7939 case WIFI:
7940 n = SystemProto.PowerUseItem.WIFI;
7941 break;
7942 case BLUETOOTH:
7943 n = SystemProto.PowerUseItem.BLUETOOTH;
7944 break;
7945 case SCREEN:
7946 n = SystemProto.PowerUseItem.SCREEN;
7947 break;
7948 case FLASHLIGHT:
7949 n = SystemProto.PowerUseItem.FLASHLIGHT;
7950 break;
7951 case APP:
Kweku Adams103351f2017-10-16 14:39:34 -07007952 // dumpProtoAppsLocked will handle this.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007953 continue;
7954 case USER:
7955 n = SystemProto.PowerUseItem.USER;
7956 uid = UserHandle.getUid(bs.userId, 0);
7957 break;
7958 case UNACCOUNTED:
7959 n = SystemProto.PowerUseItem.UNACCOUNTED;
7960 break;
7961 case OVERCOUNTED:
7962 n = SystemProto.PowerUseItem.OVERCOUNTED;
7963 break;
7964 case CAMERA:
7965 n = SystemProto.PowerUseItem.CAMERA;
7966 break;
7967 case MEMORY:
7968 n = SystemProto.PowerUseItem.MEMORY;
7969 break;
7970 }
Kweku Adams103351f2017-10-16 14:39:34 -07007971 final long puiToken = proto.start(SystemProto.POWER_USE_ITEM);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007972 proto.write(SystemProto.PowerUseItem.NAME, n);
7973 proto.write(SystemProto.PowerUseItem.UID, uid);
7974 proto.write(SystemProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7975 proto.write(SystemProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7976 proto.write(SystemProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7977 proto.write(SystemProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7978 bs.proportionalSmearMah);
Kweku Adams103351f2017-10-16 14:39:34 -07007979 proto.end(puiToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007980 }
7981 }
7982
7983 // Power use summary (POWER_USE_SUMMARY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007984 final long pusToken = proto.start(SystemProto.POWER_USE_SUMMARY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007985 proto.write(SystemProto.PowerUseSummary.BATTERY_CAPACITY_MAH,
7986 helper.getPowerProfile().getBatteryCapacity());
7987 proto.write(SystemProto.PowerUseSummary.COMPUTED_POWER_MAH, helper.getComputedPower());
7988 proto.write(SystemProto.PowerUseSummary.MIN_DRAINED_POWER_MAH, helper.getMinDrainedPower());
7989 proto.write(SystemProto.PowerUseSummary.MAX_DRAINED_POWER_MAH, helper.getMaxDrainedPower());
Kweku Adams103351f2017-10-16 14:39:34 -07007990 proto.end(pusToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007991
7992 // RPM stats (RESOURCE_POWER_MANAGER_DATA)
7993 final Map<String, ? extends Timer> rpmStats = getRpmStats();
7994 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
7995 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007996 final long rpmToken = proto.start(SystemProto.RESOURCE_POWER_MANAGER);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007997 proto.write(SystemProto.ResourcePowerManager.NAME, ent.getKey());
7998 dumpTimer(proto, SystemProto.ResourcePowerManager.TOTAL,
7999 ent.getValue(), rawRealtimeUs, which);
8000 dumpTimer(proto, SystemProto.ResourcePowerManager.SCREEN_OFF,
8001 screenOffRpmStats.get(ent.getKey()), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008002 proto.end(rpmToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008003 }
8004
8005 // Screen brightness (SCREEN_BRIGHTNESS_DATA)
8006 for (int i = 0; i < NUM_SCREEN_BRIGHTNESS_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008007 final long sbToken = proto.start(SystemProto.SCREEN_BRIGHTNESS);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008008 proto.write(SystemProto.ScreenBrightness.NAME, i);
8009 dumpTimer(proto, SystemProto.ScreenBrightness.TOTAL, getScreenBrightnessTimer(i),
8010 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008011 proto.end(sbToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008012 }
8013
8014 // Signal scanning time (SIGNAL_SCANNING_TIME_DATA)
8015 dumpTimer(proto, SystemProto.SIGNAL_SCANNING, getPhoneSignalScanningTimer(), rawRealtimeUs,
8016 which);
8017
8018 // Phone signal strength (SIGNAL_STRENGTH_TIME_DATA and SIGNAL_STRENGTH_COUNT_DATA)
8019 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008020 final long pssToken = proto.start(SystemProto.PHONE_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008021 proto.write(SystemProto.PhoneSignalStrength.NAME, i);
8022 dumpTimer(proto, SystemProto.PhoneSignalStrength.TOTAL, getPhoneSignalStrengthTimer(i),
8023 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008024 proto.end(pssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008025 }
8026
8027 // Wakeup reasons (WAKEUP_REASON_DATA)
8028 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
8029 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07008030 final long wrToken = proto.start(SystemProto.WAKEUP_REASON);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008031 proto.write(SystemProto.WakeupReason.NAME, ent.getKey());
8032 dumpTimer(proto, SystemProto.WakeupReason.TOTAL, ent.getValue(), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008033 proto.end(wrToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008034 }
8035
8036 // Wifi signal strength (WIFI_SIGNAL_STRENGTH_TIME_DATA and WIFI_SIGNAL_STRENGTH_COUNT_DATA)
8037 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008038 final long wssToken = proto.start(SystemProto.WIFI_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008039 proto.write(SystemProto.WifiSignalStrength.NAME, i);
8040 dumpTimer(proto, SystemProto.WifiSignalStrength.TOTAL, getWifiSignalStrengthTimer(i),
8041 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008042 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008043 }
8044
8045 // Wifi state (WIFI_STATE_TIME_DATA and WIFI_STATE_COUNT_DATA)
8046 for (int i = 0; i < NUM_WIFI_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008047 final long wsToken = proto.start(SystemProto.WIFI_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008048 proto.write(SystemProto.WifiState.NAME, i);
8049 dumpTimer(proto, SystemProto.WifiState.TOTAL, getWifiStateTimer(i),
8050 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008051 proto.end(wsToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008052 }
8053
8054 // Wifi supplicant state (WIFI_SUPPL_STATE_TIME_DATA and WIFI_SUPPL_STATE_COUNT_DATA)
8055 for (int i = 0; i < NUM_WIFI_SUPPL_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008056 final long wssToken = proto.start(SystemProto.WIFI_SUPPLICANT_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008057 proto.write(SystemProto.WifiSupplicantState.NAME, i);
8058 dumpTimer(proto, SystemProto.WifiSupplicantState.TOTAL, getWifiSupplStateTimer(i),
8059 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008060 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008061 }
8062
8063 proto.end(sToken);
8064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008065}