blob: 619ec235913ea52f871772c3331df92f1853057e [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.os;
18
Sudheer Shankab2f83c12017-11-13 19:25:01 -080019import android.app.ActivityManager;
Dianne Hackborn94326cb2017-06-28 16:17:20 -070020import android.app.job.JobParameters;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080021import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080022import android.content.pm.ApplicationInfo;
Bookatz8bdae8d2018-01-16 11:24:30 -080023import android.server.ServerProtoEnums;
Kweku Adams71a95312018-04-16 16:54:24 -070024import android.service.batterystats.BatteryStatsServiceDumpHistoryProto;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070025import android.service.batterystats.BatteryStatsServiceDumpProto;
Wink Saville52840902011-02-18 12:40:47 -080026import android.telephony.SignalStrength;
Tej Singheee317b2018-03-07 19:28:05 -080027import android.telephony.TelephonyManager;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080028import android.text.format.DateFormat;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070029import android.util.ArrayMap;
James Carr2dd7e5e2016-07-20 18:48:39 -070030import android.util.LongSparseArray;
Dianne Hackborn9cfba352016-03-24 17:31:28 -070031import android.util.MutableBoolean;
32import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.util.Printer;
34import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070035import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070036import android.util.TimeUtils;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070037import android.util.proto.ProtoOutputStream;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070038import android.view.Display;
Amith Yamasaniab9ad192016-12-06 12:46:59 -080039
Sudheer Shankab2f83c12017-11-13 19:25:01 -080040import com.android.internal.annotations.VisibleForTesting;
Siddharth Ray78ccaf52017-12-23 16:16:21 -080041import com.android.internal.location.gnssmetrics.GnssMetrics;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080042import com.android.internal.os.BatterySipper;
43import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
Kweku Adams2f73ecd2017-09-27 16:59:19 -070045import java.io.FileDescriptor;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070046import java.io.PrintWriter;
47import java.util.ArrayList;
48import java.util.Collections;
49import java.util.Comparator;
50import java.util.Formatter;
51import java.util.HashMap;
52import java.util.List;
53import java.util.Map;
54
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055/**
56 * A class providing access to battery usage statistics, including information on
57 * wakelocks, processes, packages, and services. All times are represented in microseconds
58 * except where indicated otherwise.
59 * @hide
60 */
61public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070062 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063
64 private static final boolean LOCAL_LOGV = false;
Bookatz82b341172017-09-07 19:06:08 -070065 /** Fetching RPM stats is too slow to do each time screen changes, so disable it. */
66 protected static final boolean SCREEN_OFF_RPM_STATS_ENABLED = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070067
68 /** @hide */
69 public static final String SERVICE_NAME = "batterystats";
70
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 /**
72 * A constant indicating a partial wake lock timer.
73 */
74 public static final int WAKE_TYPE_PARTIAL = 0;
75
76 /**
77 * A constant indicating a full wake lock timer.
78 */
79 public static final int WAKE_TYPE_FULL = 1;
80
81 /**
82 * A constant indicating a window wake lock timer.
83 */
84 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 /**
87 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 */
89 public static final int SENSOR = 3;
Mike Mac2f518a2017-09-19 16:06:03 -070090
The Android Open Source Project10592532009-03-18 17:39:46 -070091 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070092 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070093 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070094 public static final int WIFI_RUNNING = 4;
Mike Mac2f518a2017-09-19 16:06:03 -070095
Dianne Hackborn617f8772009-03-31 15:04:46 -070096 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070097 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070098 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070099 public static final int FULL_WIFI_LOCK = 5;
Mike Mac2f518a2017-09-19 16:06:03 -0700100
The Android Open Source Project10592532009-03-18 17:39:46 -0700101 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -0700102 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -0700103 */
Nick Pelly6ccaa542012-06-15 15:22:47 -0700104 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
Dianne Hackborn62793e42015-03-09 11:15:41 -0700106 /**
107 * A constant indicating a wifi multicast timer
108 */
109 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700112 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700113 */
114 public static final int VIDEO_TURNED_ON = 8;
115
116 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800117 * A constant indicating a vibrator on timer
118 */
119 public static final int VIBRATOR_ON = 9;
120
121 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700122 * A constant indicating a foreground activity timer
123 */
124 public static final int FOREGROUND_ACTIVITY = 10;
125
126 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700127 * A constant indicating a wifi batched scan is active
128 */
129 public static final int WIFI_BATCHED_SCAN = 11;
130
131 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700132 * A constant indicating a process state timer
133 */
134 public static final int PROCESS_STATE = 12;
135
136 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700137 * A constant indicating a sync timer
138 */
139 public static final int SYNC = 13;
140
141 /**
142 * A constant indicating a job timer
143 */
144 public static final int JOB = 14;
145
146 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800147 * A constant indicating an audio turn on timer
148 */
149 public static final int AUDIO_TURNED_ON = 15;
150
151 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700152 * A constant indicating a flashlight turn on timer
153 */
154 public static final int FLASHLIGHT_TURNED_ON = 16;
155
156 /**
157 * A constant indicating a camera turn on timer
158 */
159 public static final int CAMERA_TURNED_ON = 17;
160
161 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700162 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700163 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700164 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700165
166 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800167 * A constant indicating a bluetooth scan timer.
168 */
169 public static final int BLUETOOTH_SCAN_ON = 19;
170
171 /**
Bookatzc8c44962017-05-11 12:12:54 -0700172 * A constant indicating an aggregated partial wake lock timer.
173 */
174 public static final int AGGREGATED_WAKE_TYPE_PARTIAL = 20;
175
176 /**
Bookatzb1f04f32017-05-19 13:57:32 -0700177 * A constant indicating a bluetooth scan timer for unoptimized scans.
178 */
179 public static final int BLUETOOTH_UNOPTIMIZED_SCAN_ON = 21;
180
181 /**
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700182 * A constant indicating a foreground service timer
183 */
184 public static final int FOREGROUND_SERVICE = 22;
185
186 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -0800187 * A constant indicating an aggregate wifi multicast timer
188 */
189 public static final int WIFI_AGGREGATE_MULTICAST_ENABLED = 23;
190
191 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 * Include all of the data in the stats, including previously saved data.
193 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700194 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195
196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 * Include only the current run in the stats.
198 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700199 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200
201 /**
202 * Include only the run since the last time the device was unplugged in the stats.
203 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700204 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700205
206 // NOTE: Update this list if you add/change any stats above.
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700207 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700208 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700209 private static final String[] STAT_NAMES = { "l", "c", "u" };
210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700212 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700213 *
214 * New in version 19:
215 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800216 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700217 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Bookatz506a8182017-05-01 14:18:42 -0700218 * New in version 21:
219 * - Actual (not just apportioned) Wakelock time is also recorded.
Bookatzc8c44962017-05-11 12:12:54 -0700220 * - Aggregated partial wakelock time (per uid, instead of per wakelock) is recorded.
Bookatzb1f04f32017-05-19 13:57:32 -0700221 * - BLE scan result count
222 * - CPU frequency time per uid
223 * New in version 22:
224 * - BLE scan result background count, BLE unoptimized scan time
Bookatz98d4d5c2017-08-01 19:07:54 -0700225 * - Background partial wakelock time & count
226 * New in version 23:
227 * - Logging smeared power model values
228 * New in version 24:
229 * - Fixed bugs in background timers and BLE scan time
230 * New in version 25:
231 * - Package wakeup alarms are now on screen-off timebase
Bookatz50df7112017-08-04 14:53:26 -0700232 * New in version 26:
Bookatz82b341172017-09-07 19:06:08 -0700233 * - Resource power manager (rpm) states [but screenOffRpm is disabled from working properly]
Mike Mac2f518a2017-09-19 16:06:03 -0700234 * New in version 27:
235 * - Always On Display (screen doze mode) time and power
Mike Ma15313c92017-11-15 17:58:21 -0800236 * New in version 28:
237 * - Light/Deep Doze power
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700238 * - WiFi Multicast Wakelock statistics (count & duration)
Kweku Adamsa8943cb2017-12-22 13:21:06 -0800239 * New in version 29:
240 * - Process states re-ordered. TOP_SLEEPING now below BACKGROUND. HEAVY_WEIGHT introduced.
241 * - CPU times per UID process state
zhouwenjie46712bc2018-01-11 15:21:27 -0800242 * New in version 30:
243 * - Uid.PROCESS_STATE_FOREGROUND_SERVICE only tracks
244 * ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE.
Kweku Adamsb78430e2018-02-20 18:06:09 -0800245 * New in version 31:
246 * - New cellular network types.
247 * - Deferred job metrics.
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700248 */
Kweku Adamsb78430e2018-02-20 18:06:09 -0800249 static final int CHECKIN_VERSION = 31;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700250
251 /**
252 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700254 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700255
Evan Millar22ac0432009-03-31 11:33:18 -0700256 private static final long BYTES_PER_KB = 1024;
257 private static final long BYTES_PER_MB = 1048576; // 1024^2
258 private static final long BYTES_PER_GB = 1073741824; //1024^3
Bookatz506a8182017-05-01 14:18:42 -0700259
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700260 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800261 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700262 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700264 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700265 private static final String CPU_DATA = "cpu";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700266 private static final String GLOBAL_CPU_FREQ_DATA = "gcf";
267 private static final String CPU_TIMES_AT_FREQ_DATA = "ctf";
Bookatz50df7112017-08-04 14:53:26 -0700268 // rpm line is:
269 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "rpm", state/voter name, total time, total count,
270 // screen-off time, screen-off count
271 private static final String RESOURCE_POWER_MANAGER_DATA = "rpm";
Evan Millare84de8d2009-04-02 22:16:12 -0700272 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800273 private static final String VIBRATOR_DATA = "vib";
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700274 private static final String FOREGROUND_ACTIVITY_DATA = "fg";
275 // fgs line is:
276 // BATTERY_STATS_CHECKIN_VERSION, uid, category, "fgs",
277 // foreground service time, count
278 private static final String FOREGROUND_SERVICE_DATA = "fgs";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700279 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700280 // wl line is:
281 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
Bookatz5b5ec322017-05-26 09:40:38 -0700282 // full totalTime, 'f', count, current duration, max duration, total duration,
283 // partial totalTime, 'p', count, current duration, max duration, total duration,
284 // bg partial totalTime, 'bp', count, current duration, max duration, total duration,
285 // window totalTime, 'w', count, current duration, max duration, total duration
Bookatz506a8182017-05-01 14:18:42 -0700286 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700287 private static final String WAKELOCK_DATA = "wl";
Bookatzc8c44962017-05-11 12:12:54 -0700288 // awl line is:
289 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "awl",
290 // cumulative partial wakelock duration, cumulative background partial wakelock duration
291 private static final String AGGREGATED_WAKELOCK_DATA = "awl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700292 private static final String SYNC_DATA = "sy";
293 private static final String JOB_DATA = "jb";
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700294 private static final String JOB_COMPLETION_DATA = "jbc";
Amith Yamasani977e11f2018-02-16 11:29:54 -0800295
296 /**
297 * jbd line is:
298 * BATTERY_STATS_CHECKIN_VERSION, uid, which, "jbd",
Amith Yamasani0ca706b2018-03-01 17:28:59 -0800299 * jobsDeferredEventCount, jobsDeferredCount, totalLatencyMillis,
300 * 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 -0800301 * <p>
302 * @see #JOB_FRESHNESS_BUCKETS
303 */
304 private static final String JOBS_DEFERRED_DATA = "jbd";
Evan Millarc64edde2009-04-18 12:26:32 -0700305 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700306 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700307 private static final String NETWORK_DATA = "nt";
308 private static final String USER_ACTIVITY_DATA = "ua";
309 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800310 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700311 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700312 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700313 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800314 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
315 private static final String WIFI_CONTROLLER_DATA = "wfcd";
316 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
317 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700318 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700319 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800320 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800321 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
322 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800323 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700324 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700325 private static final String SCREEN_BRIGHTNESS_DATA = "br";
326 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700327 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700328 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
329 private static final String DATA_CONNECTION_TIME_DATA = "dct";
330 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800331 private static final String WIFI_STATE_TIME_DATA = "wst";
332 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700333 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
334 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
335 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
336 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800337 private static final String POWER_USE_SUMMARY_DATA = "pws";
338 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700339 private static final String DISCHARGE_STEP_DATA = "dsd";
340 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700341 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
342 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700343 private static final String FLASHLIGHT_DATA = "fla";
344 private static final String CAMERA_DATA = "cam";
345 private static final String VIDEO_DATA = "vid";
346 private static final String AUDIO_DATA = "aud";
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700347 private static final String WIFI_MULTICAST_TOTAL_DATA = "wmct";
348 private static final String WIFI_MULTICAST_DATA = "wmc";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349
Adam Lesinski010bf372016-04-11 12:18:18 -0700350 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
351
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700352 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 private final Formatter mFormatter = new Formatter(mFormatBuilder);
354
Siddharth Rayb50a6842017-12-14 15:15:28 -0800355 private static final String CELLULAR_CONTROLLER_NAME = "Cellular";
356 private static final String WIFI_CONTROLLER_NAME = "WiFi";
357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700359 * Indicates times spent by the uid at each cpu frequency in all process states.
360 *
361 * Other types might include times spent in foreground, background etc.
362 */
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800363 @VisibleForTesting
364 public static final String UID_TIMES_TYPE_ALL = "A";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700365
366 /**
Amith Yamasani977e11f2018-02-16 11:29:54 -0800367 * These are the thresholds for bucketing last time since a job was run for an app
368 * 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 -0800369 * than 1 hour ago, then it's reasonably fresh, 2 hours ago, not so fresh and so
Amith Yamasani977e11f2018-02-16 11:29:54 -0800370 * on.
371 */
372 public static final long[] JOB_FRESHNESS_BUCKETS = {
373 1 * 60 * 60 * 1000L,
374 2 * 60 * 60 * 1000L,
Amith Yamasani0ca706b2018-03-01 17:28:59 -0800375 4 * 60 * 60 * 1000L,
376 8 * 60 * 60 * 1000L,
Amith Yamasani977e11f2018-02-16 11:29:54 -0800377 Long.MAX_VALUE
378 };
379
380 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700381 * State for keeping track of counting information.
382 */
383 public static abstract class Counter {
384
385 /**
386 * Returns the count associated with this Counter for the
387 * selected type of statistics.
388 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700389 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700390 */
Evan Millarc64edde2009-04-18 12:26:32 -0700391 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700392
393 /**
394 * Temporary for debugging.
395 */
396 public abstract void logState(Printer pw, String prefix);
397 }
398
399 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700400 * State for keeping track of long counting information.
401 */
402 public static abstract class LongCounter {
403
404 /**
405 * Returns the count associated with this Counter for the
406 * selected type of statistics.
407 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700408 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700409 */
410 public abstract long getCountLocked(int which);
411
412 /**
413 * Temporary for debugging.
414 */
415 public abstract void logState(Printer pw, String prefix);
416 }
417
418 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700419 * State for keeping track of array of long counting information.
420 */
421 public static abstract class LongCounterArray {
422 /**
423 * Returns the counts associated with this Counter for the
424 * selected type of statistics.
425 *
426 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
427 */
428 public abstract long[] getCountsLocked(int which);
429
430 /**
431 * Temporary for debugging.
432 */
433 public abstract void logState(Printer pw, String prefix);
434 }
435
436 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800437 * Container class that aggregates counters for transmit, receive, and idle state of a
438 * radio controller.
439 */
440 public static abstract class ControllerActivityCounter {
441 /**
442 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
443 * idle state.
444 */
445 public abstract LongCounter getIdleTimeCounter();
446
447 /**
448 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
Siddharth Rayb50a6842017-12-14 15:15:28 -0800449 * scan state.
450 */
451 public abstract LongCounter getScanTimeCounter();
452
Siddharth Rayed754702018-02-15 12:44:37 -0800453 /**
454 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
455 * sleep state.
456 */
457 public abstract LongCounter getSleepTimeCounter();
Siddharth Rayb50a6842017-12-14 15:15:28 -0800458
459 /**
460 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800461 * receive state.
462 */
463 public abstract LongCounter getRxTimeCounter();
464
465 /**
466 * An array of {@link LongCounter}, representing various transmit levels, where each level
467 * may draw a different amount of power. The levels themselves are controller-specific.
468 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
469 * various transmit level states.
470 */
471 public abstract LongCounter[] getTxTimeCounters();
472
473 /**
474 * @return a non-null {@link LongCounter} representing the power consumed by the controller
475 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
476 * yield a value of 0 if the device doesn't support power calculations.
477 */
478 public abstract LongCounter getPowerCounter();
479 }
480
481 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 * State for keeping track of timing information.
483 */
484 public static abstract class Timer {
485
486 /**
487 * Returns the count associated with this Timer for the
488 * selected type of statistics.
489 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700490 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 */
Evan Millarc64edde2009-04-18 12:26:32 -0700492 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493
494 /**
495 * Returns the total time in microseconds associated with this Timer for the
496 * selected type of statistics.
497 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800498 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700499 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 * @return a time in microseconds
501 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800502 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700505 * Returns the total time in microseconds associated with this Timer since the
506 * 'mark' was last set.
507 *
508 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
509 * @return a time in microseconds
510 */
511 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
512
513 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700514 * Returns the max duration if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700515 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700516 */
517 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
518 return -1;
519 }
520
521 /**
522 * Returns the current time the timer has been active, if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700523 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700524 */
525 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
526 return -1;
527 }
528
529 /**
Kweku Adams103351f2017-10-16 14:39:34 -0700530 * Returns the total time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800531 *
532 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
533 * been on since reset.
534 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
535 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
536 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
537 * the actual total time.
Kweku Adams103351f2017-10-16 14:39:34 -0700538 * Not all Timer subclasses track the max, total, and current durations.
Bookatz867c0d72017-03-07 18:23:42 -0800539 */
540 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
541 return -1;
542 }
543
544 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700545 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
546 * used, for example, for tracking background usage. Secondary timers are never pooled.
547 *
548 * Not all Timer subclasses have a secondary timer; those that don't return null.
549 */
550 public Timer getSubTimer() {
551 return null;
552 }
553
554 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700555 * Returns whether the timer is currently running. Some types of timers
556 * (e.g. BatchTimers) don't know whether the event is currently active,
557 * and report false.
558 */
559 public boolean isRunningLocked() {
560 return false;
561 }
562
563 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 * Temporary for debugging.
565 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700566 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 }
568
569 /**
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800570 * Maps the ActivityManager procstate into corresponding BatteryStats procstate.
571 */
572 public static int mapToInternalProcessState(int procState) {
573 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
574 return ActivityManager.PROCESS_STATE_NONEXISTENT;
575 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
576 return Uid.PROCESS_STATE_TOP;
Dianne Hackborn10fc4fd2017-12-19 17:23:13 -0800577 } else if (procState == ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
578 // State when app has put itself in the foreground.
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800579 return Uid.PROCESS_STATE_FOREGROUND_SERVICE;
580 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
581 // Persistent and other foreground states go here.
582 return Uid.PROCESS_STATE_FOREGROUND;
583 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
584 return Uid.PROCESS_STATE_BACKGROUND;
585 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
586 return Uid.PROCESS_STATE_TOP_SLEEPING;
587 } else if (procState <= ActivityManager.PROCESS_STATE_HEAVY_WEIGHT) {
588 return Uid.PROCESS_STATE_HEAVY_WEIGHT;
589 } else {
590 return Uid.PROCESS_STATE_CACHED;
591 }
592 }
593
594 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 * The statistics associated with a particular uid.
596 */
597 public static abstract class Uid {
598
599 /**
600 * Returns a mapping containing wakelock statistics.
601 *
602 * @return a Map from Strings to Uid.Wakelock objects.
603 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700604 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605
606 /**
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700607 * Returns the WiFi Multicast Wakelock statistics.
608 *
609 * @return a Timer Object for the per uid Multicast statistics.
610 */
611 public abstract Timer getMulticastWakelockStats();
612
613 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700614 * Returns a mapping containing sync statistics.
615 *
616 * @return a Map from Strings to Timer objects.
617 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700618 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700619
620 /**
621 * Returns a mapping containing scheduled job statistics.
622 *
623 * @return a Map from Strings to Timer objects.
624 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700625 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700626
627 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700628 * Returns statistics about how jobs have completed.
629 *
630 * @return A Map of String job names to completion type -> count mapping.
631 */
632 public abstract ArrayMap<String, SparseIntArray> getJobCompletionStats();
633
634 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 * The statistics associated with a particular wake lock.
636 */
637 public static abstract class Wakelock {
638 public abstract Timer getWakeTime(int type);
639 }
640
641 /**
Bookatzc8c44962017-05-11 12:12:54 -0700642 * The cumulative time the uid spent holding any partial wakelocks. This will generally
643 * differ from summing over the Wakelocks in getWakelockStats since the latter may have
644 * wakelocks that overlap in time (and therefore over-counts).
645 */
646 public abstract Timer getAggregatedPartialWakelockTimer();
647
648 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 * Returns a mapping containing sensor statistics.
650 *
651 * @return a Map from Integer sensor ids to Uid.Sensor objects.
652 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700653 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654
655 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700656 * Returns a mapping containing active process data.
657 */
658 public abstract SparseArray<? extends Pid> getPidStats();
Bookatzc8c44962017-05-11 12:12:54 -0700659
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700660 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 * Returns a mapping containing process statistics.
662 *
663 * @return a Map from Strings to Uid.Proc objects.
664 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700665 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666
667 /**
668 * Returns a mapping containing package statistics.
669 *
670 * @return a Map from Strings to Uid.Pkg objects.
671 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700672 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700673
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800674 public abstract ControllerActivityCounter getWifiControllerActivity();
675 public abstract ControllerActivityCounter getBluetoothControllerActivity();
676 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800677
678 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 * {@hide}
680 */
681 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700682
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800683 public abstract void noteWifiRunningLocked(long elapsedRealtime);
684 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
685 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
686 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
687 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
688 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
689 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
690 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
691 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
692 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800693 public abstract void noteActivityResumedLocked(long elapsedRealtime);
694 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800695 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
696 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
697 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700698 public abstract int getWifiScanCount(int which);
Kweku Adams103351f2017-10-16 14:39:34 -0700699 /**
700 * Returns the timer keeping track of wifi scans.
701 */
702 public abstract Timer getWifiScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800703 public abstract int getWifiScanBackgroundCount(int which);
704 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
705 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -0700706 /**
707 * Returns the timer keeping track of background wifi scans.
708 */
709 public abstract Timer getWifiScanBackgroundTimer();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800710 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700711 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800712 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700713 public abstract Timer getAudioTurnedOnTimer();
714 public abstract Timer getVideoTurnedOnTimer();
715 public abstract Timer getFlashlightTurnedOnTimer();
716 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700717 public abstract Timer getForegroundActivityTimer();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700718
719 /**
720 * Returns the timer keeping track of Foreground Service time
721 */
722 public abstract Timer getForegroundServiceTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800723 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800724 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatzb1f04f32017-05-19 13:57:32 -0700725 public abstract Timer getBluetoothUnoptimizedScanTimer();
726 public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700727 public abstract Counter getBluetoothScanResultCounter();
Bookatzb1f04f32017-05-19 13:57:32 -0700728 public abstract Counter getBluetoothScanResultBgCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700729
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700730 public abstract long[] getCpuFreqTimes(int which);
731 public abstract long[] getScreenOffCpuFreqTimes(int which);
Mike Ma3d422c32017-10-25 11:08:57 -0700732 /**
733 * Returns cpu active time of an uid.
734 */
735 public abstract long getCpuActiveTime();
736 /**
737 * Returns cpu times of an uid on each cluster
738 */
739 public abstract long[] getCpuClusterTimes();
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700740
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800741 /**
742 * Returns cpu times of an uid at a particular process state.
743 */
744 public abstract long[] getCpuFreqTimes(int which, int procState);
745 /**
746 * Returns cpu times of an uid while the screen if off at a particular process state.
747 */
748 public abstract long[] getScreenOffCpuFreqTimes(int which, int procState);
749
Dianne Hackborna0200e32016-03-30 18:01:41 -0700750 // Note: the following times are disjoint. They can be added together to find the
751 // total time a uid has had any processes running at all.
752
753 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800754 * Time this uid has any processes in the top state.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700755 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800756 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700757 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800758 * Time this uid has any process with a started foreground service, but
Dianne Hackborna0200e32016-03-30 18:01:41 -0700759 * none in the "top" state.
760 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800761 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700762 /**
Dianne Hackborna0200e32016-03-30 18:01:41 -0700763 * Time this uid has any process in an active foreground state, but none in the
zhouwenjie46712bc2018-01-11 15:21:27 -0800764 * "foreground service" or better state. Persistent and other foreground states go here.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700765 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800766 public static final int PROCESS_STATE_FOREGROUND = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700767 /**
768 * Time this uid has any process in an active background state, but none in the
769 * "foreground" or better state.
770 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800771 public static final int PROCESS_STATE_BACKGROUND = 3;
772 /**
773 * Time this uid has any process that is top while the device is sleeping, but not
774 * active for any other reason. We kind-of consider it a kind of cached process
775 * for execution restrictions.
776 */
777 public static final int PROCESS_STATE_TOP_SLEEPING = 4;
778 /**
779 * Time this uid has any process that is in the background but it has an activity
780 * marked as "can't save state". This is essentially a cached process, though the
781 * system will try much harder than normal to avoid killing it.
782 */
783 public static final int PROCESS_STATE_HEAVY_WEIGHT = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700784 /**
785 * Time this uid has any processes that are sitting around cached, not in one of the
786 * other active states.
787 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800788 public static final int PROCESS_STATE_CACHED = 6;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700789 /**
790 * Total number of process states we track.
791 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800792 public static final int NUM_PROCESS_STATE = 7;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700793
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800794 // Used in dump
Dianne Hackborn61659e52014-07-09 16:13:01 -0700795 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800796 "Top", "Fg Service", "Foreground", "Background", "Top Sleeping", "Heavy Weight",
797 "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700798 };
799
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800800 // Used in checkin dump
801 @VisibleForTesting
802 public static final String[] UID_PROCESS_TYPES = {
803 "T", // TOP
804 "FS", // FOREGROUND_SERVICE
805 "F", // FOREGROUND
806 "B", // BACKGROUND
807 "TS", // TOP_SLEEPING
808 "HW", // HEAVY_WEIGHT
809 "C" // CACHED
810 };
811
812 /**
813 * When the process exits one of these states, we need to make sure cpu time in this state
814 * is not attributed to any non-critical process states.
815 */
816 public static final int[] CRITICAL_PROC_STATES = {
817 PROCESS_STATE_TOP, PROCESS_STATE_FOREGROUND_SERVICE, PROCESS_STATE_FOREGROUND
818 };
819
Dianne Hackborn61659e52014-07-09 16:13:01 -0700820 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800821 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700822
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800823 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824
Robert Greenwalta029ea12013-09-25 16:38:12 -0700825 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
826
Dianne Hackborn617f8772009-03-31 15:04:46 -0700827 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700828 * Note that these must match the constants in android.os.PowerManager.
829 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
830 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700831 */
832 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700833 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700834 };
Bookatzc8c44962017-05-11 12:12:54 -0700835
Phil Weaverda80d672016-03-15 16:25:46 -0700836 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700837
Dianne Hackborn617f8772009-03-31 15:04:46 -0700838 public abstract void noteUserActivityLocked(int type);
839 public abstract boolean hasUserActivity();
840 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700841
842 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800843 public abstract long getNetworkActivityBytes(int type, int which);
844 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800845 public abstract long getMobileRadioActiveTime(int which);
846 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700847
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700848 /**
849 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
850 */
851 public abstract long getUserCpuTimeUs(int which);
852
853 /**
854 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
855 */
856 public abstract long getSystemCpuTimeUs(int which);
857
858 /**
Sudheer Shanka71f34b32017-07-21 00:14:24 -0700859 * Returns the approximate cpu time (in microseconds) spent at a certain CPU speed for a
Adam Lesinski6832f392015-09-05 18:05:40 -0700860 * given CPU cluster.
861 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700862 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700863 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700864 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
865 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700866 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700867 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700868
Adam Lesinski5f056f62016-07-14 16:56:08 -0700869 /**
870 * Returns the number of times this UID woke up the Application Processor to
871 * process a mobile radio packet.
872 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
873 */
874 public abstract long getMobileRadioApWakeupCount(int which);
875
876 /**
877 * Returns the number of times this UID woke up the Application Processor to
878 * process a WiFi packet.
879 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
880 */
881 public abstract long getWifiRadioApWakeupCount(int which);
882
Amith Yamasani977e11f2018-02-16 11:29:54 -0800883 /**
884 * Appends the deferred jobs data to the StringBuilder passed in, in checkin format
885 * @param sb StringBuilder that can be overwritten with the deferred jobs data
886 * @param which one of STATS_*
887 */
888 public abstract void getDeferredJobsCheckinLineLocked(StringBuilder sb, int which);
889
890 /**
891 * Appends the deferred jobs data to the StringBuilder passed in
892 * @param sb StringBuilder that can be overwritten with the deferred jobs data
893 * @param which one of STATS_*
894 */
895 public abstract void getDeferredJobsLineLocked(StringBuilder sb, int which);
896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800898 /*
899 * FIXME: it's not correct to use this magic value because it
900 * could clash with a sensor handle (which are defined by
901 * the sensor HAL, and therefore out of our control
902 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 // Magic sensor number for the GPS.
904 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800909
Bookatz867c0d72017-03-07 18:23:42 -0800910 /** Returns a Timer for sensor usage when app is in the background. */
911 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 }
913
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700914 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800915 public int mWakeNesting;
916 public long mWakeSumMs;
917 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700918 }
919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 /**
921 * The statistics associated with a particular process.
922 */
923 public static abstract class Proc {
924
Dianne Hackborn287952c2010-09-22 22:34:31 -0700925 public static class ExcessivePower {
926 public static final int TYPE_WAKE = 1;
927 public static final int TYPE_CPU = 2;
928
929 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700930 public long overTime;
931 public long usedTime;
932 }
933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800935 * Returns true if this process is still active in the battery stats.
936 */
937 public abstract boolean isActive();
938
939 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700940 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700942 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 */
944 public abstract long getUserTime(int which);
945
946 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700947 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700949 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 */
951 public abstract long getSystemTime(int which);
952
953 /**
954 * Returns the number of times the process has been started.
955 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700956 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 */
958 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700959
960 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800961 * Returns the number of times the process has crashed.
962 *
963 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
964 */
965 public abstract int getNumCrashes(int which);
966
967 /**
968 * Returns the number of times the process has ANRed.
969 *
970 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
971 */
972 public abstract int getNumAnrs(int which);
973
974 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700975 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700976 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700977 * @return foreground cpu time in microseconds
978 */
979 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700980
Dianne Hackborn287952c2010-09-22 22:34:31 -0700981 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700982
Dianne Hackborn287952c2010-09-22 22:34:31 -0700983 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 }
985
986 /**
987 * The statistics associated with a particular package.
988 */
989 public static abstract class Pkg {
990
991 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700992 * Returns information about all wakeup alarms that have been triggered for this
993 * package. The mapping keys are tag names for the alarms, the counter contains
994 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700996 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997
998 /**
999 * Returns a mapping containing service statistics.
1000 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001001 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002
1003 /**
1004 * The statistics associated with a particular service.
1005 */
Joe Onoratoabded112016-02-08 16:49:39 -08001006 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007
1008 /**
1009 * Returns the amount of time spent started.
1010 *
1011 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001012 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 * @return
1014 */
1015 public abstract long getStartTime(long batteryUptime, int which);
1016
1017 /**
1018 * Returns the total number of times startService() has been called.
1019 *
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 */
1022 public abstract int getStarts(int which);
1023
1024 /**
1025 * Returns the total number times the service has been launched.
1026 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001027 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 */
1029 public abstract int getLaunches(int which);
1030 }
1031 }
1032 }
1033
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001034 public static final class LevelStepTracker {
1035 public long mLastStepTime = -1;
1036 public int mNumStepDurations;
1037 public final long[] mStepDurations;
1038
1039 public LevelStepTracker(int maxLevelSteps) {
1040 mStepDurations = new long[maxLevelSteps];
1041 }
1042
1043 public LevelStepTracker(int numSteps, long[] steps) {
1044 mNumStepDurations = numSteps;
1045 mStepDurations = new long[numSteps];
1046 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
1047 }
1048
1049 public long getDurationAt(int index) {
1050 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
1051 }
1052
1053 public int getLevelAt(int index) {
1054 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
1055 >> STEP_LEVEL_LEVEL_SHIFT);
1056 }
1057
1058 public int getInitModeAt(int index) {
1059 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
1060 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1061 }
1062
1063 public int getModModeAt(int index) {
1064 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
1065 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1066 }
1067
1068 private void appendHex(long val, int topOffset, StringBuilder out) {
1069 boolean hasData = false;
1070 while (topOffset >= 0) {
1071 int digit = (int)( (val>>topOffset) & 0xf );
1072 topOffset -= 4;
1073 if (!hasData && digit == 0) {
1074 continue;
1075 }
1076 hasData = true;
1077 if (digit >= 0 && digit <= 9) {
1078 out.append((char)('0' + digit));
1079 } else {
1080 out.append((char)('a' + digit - 10));
1081 }
1082 }
1083 }
1084
1085 public void encodeEntryAt(int index, StringBuilder out) {
1086 long item = mStepDurations[index];
1087 long duration = item & STEP_LEVEL_TIME_MASK;
1088 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
1089 >> STEP_LEVEL_LEVEL_SHIFT);
1090 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
1091 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1092 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
1093 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1094 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1095 case Display.STATE_OFF: out.append('f'); break;
1096 case Display.STATE_ON: out.append('o'); break;
1097 case Display.STATE_DOZE: out.append('d'); break;
1098 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
1099 }
1100 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1101 out.append('p');
1102 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001103 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1104 out.append('i');
1105 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001106 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1107 case Display.STATE_OFF: out.append('F'); break;
1108 case Display.STATE_ON: out.append('O'); break;
1109 case Display.STATE_DOZE: out.append('D'); break;
1110 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
1111 }
1112 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1113 out.append('P');
1114 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001115 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1116 out.append('I');
1117 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001118 out.append('-');
1119 appendHex(level, 4, out);
1120 out.append('-');
1121 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
1122 }
1123
1124 public void decodeEntryAt(int index, String value) {
1125 final int N = value.length();
1126 int i = 0;
1127 char c;
1128 long out = 0;
1129 while (i < N && (c=value.charAt(i)) != '-') {
1130 i++;
1131 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001132 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001133 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001134 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001135 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001136 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001137 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001138 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1139 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1140 break;
1141 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1142 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1143 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001144 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1145 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1146 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001147 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1148 break;
1149 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1150 break;
1151 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1152 break;
1153 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1154 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1155 break;
1156 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1157 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001158 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001159 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1160 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1161 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001162 }
1163 }
1164 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001165 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001166 while (i < N && (c=value.charAt(i)) != '-') {
1167 i++;
1168 level <<= 4;
1169 if (c >= '0' && c <= '9') {
1170 level += c - '0';
1171 } else if (c >= 'a' && c <= 'f') {
1172 level += c - 'a' + 10;
1173 } else if (c >= 'A' && c <= 'F') {
1174 level += c - 'A' + 10;
1175 }
1176 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001177 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001178 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
1179 long duration = 0;
1180 while (i < N && (c=value.charAt(i)) != '-') {
1181 i++;
1182 duration <<= 4;
1183 if (c >= '0' && c <= '9') {
1184 duration += c - '0';
1185 } else if (c >= 'a' && c <= 'f') {
1186 duration += c - 'a' + 10;
1187 } else if (c >= 'A' && c <= 'F') {
1188 duration += c - 'A' + 10;
1189 }
1190 }
1191 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
1192 }
1193
1194 public void init() {
1195 mLastStepTime = -1;
1196 mNumStepDurations = 0;
1197 }
1198
1199 public void clearTime() {
1200 mLastStepTime = -1;
1201 }
1202
1203 public long computeTimePerLevel() {
1204 final long[] steps = mStepDurations;
1205 final int numSteps = mNumStepDurations;
1206
1207 // For now we'll do a simple average across all steps.
1208 if (numSteps <= 0) {
1209 return -1;
1210 }
1211 long total = 0;
1212 for (int i=0; i<numSteps; i++) {
1213 total += steps[i] & STEP_LEVEL_TIME_MASK;
1214 }
1215 return total / numSteps;
1216 /*
1217 long[] buckets = new long[numSteps];
1218 int numBuckets = 0;
1219 int numToAverage = 4;
1220 int i = 0;
1221 while (i < numSteps) {
1222 long totalTime = 0;
1223 int num = 0;
1224 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1225 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1226 num++;
1227 }
1228 buckets[numBuckets] = totalTime / num;
1229 numBuckets++;
1230 numToAverage *= 2;
1231 i += num;
1232 }
1233 if (numBuckets < 1) {
1234 return -1;
1235 }
1236 long averageTime = buckets[numBuckets-1];
1237 for (i=numBuckets-2; i>=0; i--) {
1238 averageTime = (averageTime + buckets[i]) / 2;
1239 }
1240 return averageTime;
1241 */
1242 }
1243
1244 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1245 int[] outNumOfInterest) {
1246 final long[] steps = mStepDurations;
1247 final int count = mNumStepDurations;
1248 if (count <= 0) {
1249 return -1;
1250 }
1251 long total = 0;
1252 int numOfInterest = 0;
1253 for (int i=0; i<count; i++) {
1254 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1255 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1256 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1257 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1258 // If the modes of interest didn't change during this step period...
1259 if ((modMode&modesOfInterest) == 0) {
1260 // And the mode values during this period match those we are measuring...
1261 if ((initMode&modesOfInterest) == modeValues) {
1262 // Then this can be used to estimate the total time!
1263 numOfInterest++;
1264 total += steps[i] & STEP_LEVEL_TIME_MASK;
1265 }
1266 }
1267 }
1268 if (numOfInterest <= 0) {
1269 return -1;
1270 }
1271
1272 if (outNumOfInterest != null) {
1273 outNumOfInterest[0] = numOfInterest;
1274 }
1275
1276 // The estimated time is the average time we spend in each level, multipled
1277 // by 100 -- the total number of battery levels
1278 return (total / numOfInterest) * 100;
1279 }
1280
1281 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1282 int stepCount = mNumStepDurations;
1283 final long lastStepTime = mLastStepTime;
1284 if (lastStepTime >= 0 && numStepLevels > 0) {
1285 final long[] steps = mStepDurations;
1286 long duration = elapsedRealtime - lastStepTime;
1287 for (int i=0; i<numStepLevels; i++) {
1288 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1289 long thisDuration = duration / (numStepLevels-i);
1290 duration -= thisDuration;
1291 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1292 thisDuration = STEP_LEVEL_TIME_MASK;
1293 }
1294 steps[0] = thisDuration | modeBits;
1295 }
1296 stepCount += numStepLevels;
1297 if (stepCount > steps.length) {
1298 stepCount = steps.length;
1299 }
1300 }
1301 mNumStepDurations = stepCount;
1302 mLastStepTime = elapsedRealtime;
1303 }
1304
1305 public void readFromParcel(Parcel in) {
1306 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001307 if (N > mStepDurations.length) {
1308 throw new ParcelFormatException("more step durations than available: " + N);
1309 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001310 mNumStepDurations = N;
1311 for (int i=0; i<N; i++) {
1312 mStepDurations[i] = in.readLong();
1313 }
1314 }
1315
1316 public void writeToParcel(Parcel out) {
1317 final int N = mNumStepDurations;
1318 out.writeInt(N);
1319 for (int i=0; i<N; i++) {
1320 out.writeLong(mStepDurations[i]);
1321 }
1322 }
1323 }
1324
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001325 public static final class PackageChange {
1326 public String mPackageName;
1327 public boolean mUpdate;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001328 public long mVersionCode;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001329 }
1330
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001331 public static final class DailyItem {
1332 public long mStartTime;
1333 public long mEndTime;
1334 public LevelStepTracker mDischargeSteps;
1335 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001336 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001337 }
1338
1339 public abstract DailyItem getDailyItemLocked(int daysAgo);
1340
1341 public abstract long getCurrentDailyStartTime();
1342
1343 public abstract long getNextMinDailyDeadline();
1344
1345 public abstract long getNextMaxDailyDeadline();
1346
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001347 public abstract long[] getCpuFreqs();
1348
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001349 public final static class HistoryTag {
1350 public String string;
1351 public int uid;
1352
1353 public int poolIdx;
1354
1355 public void setTo(HistoryTag o) {
1356 string = o.string;
1357 uid = o.uid;
1358 poolIdx = o.poolIdx;
1359 }
1360
1361 public void setTo(String _string, int _uid) {
1362 string = _string;
1363 uid = _uid;
1364 poolIdx = -1;
1365 }
1366
1367 public void writeToParcel(Parcel dest, int flags) {
1368 dest.writeString(string);
1369 dest.writeInt(uid);
1370 }
1371
1372 public void readFromParcel(Parcel src) {
1373 string = src.readString();
1374 uid = src.readInt();
1375 poolIdx = -1;
1376 }
1377
1378 @Override
1379 public boolean equals(Object o) {
1380 if (this == o) return true;
1381 if (o == null || getClass() != o.getClass()) return false;
1382
1383 HistoryTag that = (HistoryTag) o;
1384
1385 if (uid != that.uid) return false;
1386 if (!string.equals(that.string)) return false;
1387
1388 return true;
1389 }
1390
1391 @Override
1392 public int hashCode() {
1393 int result = string.hashCode();
1394 result = 31 * result + uid;
1395 return result;
1396 }
1397 }
1398
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001399 /**
1400 * Optional detailed information that can go into a history step. This is typically
1401 * generated each time the battery level changes.
1402 */
1403 public final static class HistoryStepDetails {
1404 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1405 public int userTime;
1406 public int systemTime;
1407
1408 // Top three apps using CPU in the last step, with times in 1/100 second.
1409 public int appCpuUid1;
1410 public int appCpuUTime1;
1411 public int appCpuSTime1;
1412 public int appCpuUid2;
1413 public int appCpuUTime2;
1414 public int appCpuSTime2;
1415 public int appCpuUid3;
1416 public int appCpuUTime3;
1417 public int appCpuSTime3;
1418
1419 // Information from /proc/stat
1420 public int statUserTime;
1421 public int statSystemTime;
1422 public int statIOWaitTime;
1423 public int statIrqTime;
1424 public int statSoftIrqTime;
1425 public int statIdlTime;
1426
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001427 // Platform-level low power state stats
1428 public String statPlatformIdleState;
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001429 public String statSubsystemPowerState;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001430
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001431 public HistoryStepDetails() {
1432 clear();
1433 }
1434
1435 public void clear() {
1436 userTime = systemTime = 0;
1437 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1438 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1439 = appCpuUTime3 = appCpuSTime3 = 0;
1440 }
1441
1442 public void writeToParcel(Parcel out) {
1443 out.writeInt(userTime);
1444 out.writeInt(systemTime);
1445 out.writeInt(appCpuUid1);
1446 out.writeInt(appCpuUTime1);
1447 out.writeInt(appCpuSTime1);
1448 out.writeInt(appCpuUid2);
1449 out.writeInt(appCpuUTime2);
1450 out.writeInt(appCpuSTime2);
1451 out.writeInt(appCpuUid3);
1452 out.writeInt(appCpuUTime3);
1453 out.writeInt(appCpuSTime3);
1454 out.writeInt(statUserTime);
1455 out.writeInt(statSystemTime);
1456 out.writeInt(statIOWaitTime);
1457 out.writeInt(statIrqTime);
1458 out.writeInt(statSoftIrqTime);
1459 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001460 out.writeString(statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001461 out.writeString(statSubsystemPowerState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001462 }
1463
1464 public void readFromParcel(Parcel in) {
1465 userTime = in.readInt();
1466 systemTime = in.readInt();
1467 appCpuUid1 = in.readInt();
1468 appCpuUTime1 = in.readInt();
1469 appCpuSTime1 = in.readInt();
1470 appCpuUid2 = in.readInt();
1471 appCpuUTime2 = in.readInt();
1472 appCpuSTime2 = in.readInt();
1473 appCpuUid3 = in.readInt();
1474 appCpuUTime3 = in.readInt();
1475 appCpuSTime3 = in.readInt();
1476 statUserTime = in.readInt();
1477 statSystemTime = in.readInt();
1478 statIOWaitTime = in.readInt();
1479 statIrqTime = in.readInt();
1480 statSoftIrqTime = in.readInt();
1481 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001482 statPlatformIdleState = in.readString();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001483 statSubsystemPowerState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001484 }
1485 }
1486
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001487 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001488 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001489
1490 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001491 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001492
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001493 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001494 public static final byte CMD_NULL = -1;
1495 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001496 public static final byte CMD_CURRENT_TIME = 5;
1497 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001498 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001499 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001500
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001501 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001502
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001503 /**
1504 * Return whether the command code is a delta data update.
1505 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001506 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001507 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001508 }
1509
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001510 public byte batteryLevel;
1511 public byte batteryStatus;
1512 public byte batteryHealth;
1513 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001514
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001515 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001516 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001517
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001518 // The charge of the battery in micro-Ampere-hours.
1519 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001520
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001521 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001522 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001523 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001524 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001525 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1526 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001527 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001528 public static final int STATE_PHONE_STATE_SHIFT = 6;
1529 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001530 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001531 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001532 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001533
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001534 // These states always appear directly in the first int token
1535 // of a delta change; they should be ones that change relatively
1536 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001537 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1538 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001539 public static final int STATE_GPS_ON_FLAG = 1<<29;
1540 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001541 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001542 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001543 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001544 // Do not use, this is used for coulomb delta count.
1545 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001546 // These are on the lower bits used for the command; if they change
1547 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001548 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001549 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001550 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001551 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1552 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
Mike Mac2f518a2017-09-19 16:06:03 -07001553 public static final int STATE_SCREEN_DOZE_FLAG = 1 << 18;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001554 // empty slot
1555 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001556
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001557 public static final int MOST_INTERESTING_STATES =
Mike Mac2f518a2017-09-19 16:06:03 -07001558 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG | STATE_SCREEN_DOZE_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001559
1560 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001561
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001562 public int states;
1563
Dianne Hackborn3251b902014-06-20 14:40:53 -07001564 // Constants from WIFI_SUPPL_STATE_*
1565 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1566 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1567 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1568 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1569 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1570 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
Siddharth Ray78ccaf52017-12-23 16:16:21 -08001571 // Values for NUM_GPS_SIGNAL_QUALITY_LEVELS
1572 public static final int STATE2_GPS_SIGNAL_QUALITY_SHIFT = 7;
1573 public static final int STATE2_GPS_SIGNAL_QUALITY_MASK =
1574 0x1 << STATE2_GPS_SIGNAL_QUALITY_SHIFT;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001575
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001576 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001577 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1578 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1579 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001580 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001581 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1582 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1583 public static final int STATE2_CHARGING_FLAG = 1<<24;
1584 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1585 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1586 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001587 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Siddharth Rayf5e796a2018-01-22 18:18:17 -08001588 public static final int STATE2_CELLULAR_HIGH_TX_POWER_FLAG = 1 << 19;
Mike Ma926a97c2018-03-25 02:32:35 -07001589 public static final int STATE2_USB_DATA_LINK_FLAG = 1 << 18;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001590
1591 public static final int MOST_INTERESTING_STATES2 =
Mike Mac2f518a2017-09-19 16:06:03 -07001592 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
1593 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001594
1595 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001596
Dianne Hackborn40c87252014-03-19 16:55:40 -07001597 public int states2;
1598
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001599 // The wake lock that was acquired at this point.
1600 public HistoryTag wakelockTag;
1601
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001602 // Kernel wakeup reason at this point.
1603 public HistoryTag wakeReasonTag;
1604
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001605 // Non-null when there is more detailed information at this step.
1606 public HistoryStepDetails stepDetails;
1607
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001608 public static final int EVENT_FLAG_START = 0x8000;
1609 public static final int EVENT_FLAG_FINISH = 0x4000;
1610
1611 // No event in this item.
1612 public static final int EVENT_NONE = 0x0000;
1613 // Event is about a process that is running.
1614 public static final int EVENT_PROC = 0x0001;
1615 // Event is about an application package that is in the foreground.
1616 public static final int EVENT_FOREGROUND = 0x0002;
1617 // Event is about an application package that is at the top of the screen.
1618 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001619 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001620 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001621 // Events for all additional wake locks aquired/release within a wake block.
1622 // These are not generated by default.
1623 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001624 // Event is about an application executing a scheduled job.
1625 public static final int EVENT_JOB = 0x0006;
1626 // Events for users running.
1627 public static final int EVENT_USER_RUNNING = 0x0007;
1628 // Events for foreground user.
1629 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001630 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001631 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001632 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001633 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001634 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001635 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001636 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001637 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001638 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001639 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001640 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001641 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001642 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001643 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001644 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001645 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001646 // Event for a package being on the temporary whitelist.
1647 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001648 // Event for the screen waking up.
1649 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001650 // Event for the UID that woke up the application processor.
1651 // Used for wakeups coming from WiFi, modem, etc.
1652 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001653 // Event for reporting that a specific partial wake lock has been held for a long duration.
1654 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001655
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001656 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001657 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001658 // Mask to extract out only the type part of the event.
1659 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001660
1661 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1662 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1663 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1664 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1665 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1666 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001667 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1668 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001669 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1670 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001671 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1672 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1673 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1674 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1675 public static final int EVENT_USER_FOREGROUND_START =
1676 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1677 public static final int EVENT_USER_FOREGROUND_FINISH =
1678 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001679 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1680 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001681 public static final int EVENT_TEMP_WHITELIST_START =
1682 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1683 public static final int EVENT_TEMP_WHITELIST_FINISH =
1684 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001685 public static final int EVENT_LONG_WAKE_LOCK_START =
1686 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1687 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1688 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001689
1690 // For CMD_EVENT.
1691 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001692 public HistoryTag eventTag;
1693
Dianne Hackborn9a755432014-05-15 17:05:22 -07001694 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001695 public long currentTime;
1696
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001697 // Meta-data when reading.
1698 public int numReadInts;
1699
1700 // Pre-allocated objects.
1701 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001702 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001703 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001704
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001705 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001706 }
Bookatzc8c44962017-05-11 12:12:54 -07001707
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001708 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001709 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001710 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001711 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001712 }
Bookatzc8c44962017-05-11 12:12:54 -07001713
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001714 public int describeContents() {
1715 return 0;
1716 }
1717
1718 public void writeToParcel(Parcel dest, int flags) {
1719 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001720 int bat = (((int)cmd)&0xff)
1721 | ((((int)batteryLevel)<<8)&0xff00)
1722 | ((((int)batteryStatus)<<16)&0xf0000)
1723 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001724 | ((((int)batteryPlugType)<<24)&0xf000000)
1725 | (wakelockTag != null ? 0x10000000 : 0)
1726 | (wakeReasonTag != null ? 0x20000000 : 0)
1727 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001728 dest.writeInt(bat);
1729 bat = (((int)batteryTemperature)&0xffff)
1730 | ((((int)batteryVoltage)<<16)&0xffff0000);
1731 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001732 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001733 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001734 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001735 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001736 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001737 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001738 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001739 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001740 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001741 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001742 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001743 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001744 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001745 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001746 dest.writeLong(currentTime);
1747 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001748 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001749
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001750 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001751 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001752 int bat = src.readInt();
1753 cmd = (byte)(bat&0xff);
1754 batteryLevel = (byte)((bat>>8)&0xff);
1755 batteryStatus = (byte)((bat>>16)&0xf);
1756 batteryHealth = (byte)((bat>>20)&0xf);
1757 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001758 int bat2 = src.readInt();
1759 batteryTemperature = (short)(bat2&0xffff);
1760 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001761 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001762 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001763 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001764 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001765 wakelockTag = localWakelockTag;
1766 wakelockTag.readFromParcel(src);
1767 } else {
1768 wakelockTag = null;
1769 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001770 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001771 wakeReasonTag = localWakeReasonTag;
1772 wakeReasonTag.readFromParcel(src);
1773 } else {
1774 wakeReasonTag = null;
1775 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001776 if ((bat&0x40000000) != 0) {
1777 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001778 eventTag = localEventTag;
1779 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001780 } else {
1781 eventCode = EVENT_NONE;
1782 eventTag = null;
1783 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001784 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001785 currentTime = src.readLong();
1786 } else {
1787 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001788 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001789 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001790 }
1791
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001792 public void clear() {
1793 time = 0;
1794 cmd = CMD_NULL;
1795 batteryLevel = 0;
1796 batteryStatus = 0;
1797 batteryHealth = 0;
1798 batteryPlugType = 0;
1799 batteryTemperature = 0;
1800 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001801 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001802 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001803 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001804 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001805 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001806 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001807 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001808 }
Bookatzc8c44962017-05-11 12:12:54 -07001809
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001810 public void setTo(HistoryItem o) {
1811 time = o.time;
1812 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001813 setToCommon(o);
1814 }
1815
1816 public void setTo(long time, byte cmd, HistoryItem o) {
1817 this.time = time;
1818 this.cmd = cmd;
1819 setToCommon(o);
1820 }
1821
1822 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001823 batteryLevel = o.batteryLevel;
1824 batteryStatus = o.batteryStatus;
1825 batteryHealth = o.batteryHealth;
1826 batteryPlugType = o.batteryPlugType;
1827 batteryTemperature = o.batteryTemperature;
1828 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001829 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001830 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001831 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001832 if (o.wakelockTag != null) {
1833 wakelockTag = localWakelockTag;
1834 wakelockTag.setTo(o.wakelockTag);
1835 } else {
1836 wakelockTag = null;
1837 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001838 if (o.wakeReasonTag != null) {
1839 wakeReasonTag = localWakeReasonTag;
1840 wakeReasonTag.setTo(o.wakeReasonTag);
1841 } else {
1842 wakeReasonTag = null;
1843 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001844 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001845 if (o.eventTag != null) {
1846 eventTag = localEventTag;
1847 eventTag.setTo(o.eventTag);
1848 } else {
1849 eventTag = null;
1850 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001851 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001852 }
1853
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001854 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001855 return batteryLevel == o.batteryLevel
1856 && batteryStatus == o.batteryStatus
1857 && batteryHealth == o.batteryHealth
1858 && batteryPlugType == o.batteryPlugType
1859 && batteryTemperature == o.batteryTemperature
1860 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001861 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001862 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001863 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001864 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001865 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001866
1867 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001868 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001869 return false;
1870 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001871 if (wakelockTag != o.wakelockTag) {
1872 if (wakelockTag == null || o.wakelockTag == null) {
1873 return false;
1874 }
1875 if (!wakelockTag.equals(o.wakelockTag)) {
1876 return false;
1877 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001878 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001879 if (wakeReasonTag != o.wakeReasonTag) {
1880 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1881 return false;
1882 }
1883 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1884 return false;
1885 }
1886 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001887 if (eventTag != o.eventTag) {
1888 if (eventTag == null || o.eventTag == null) {
1889 return false;
1890 }
1891 if (!eventTag.equals(o.eventTag)) {
1892 return false;
1893 }
1894 }
1895 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001896 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001897 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001898
1899 public final static class HistoryEventTracker {
1900 private final HashMap<String, SparseIntArray>[] mActiveEvents
1901 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1902
1903 public boolean updateState(int code, String name, int uid, int poolIdx) {
1904 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1905 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1906 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1907 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001908 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001909 mActiveEvents[idx] = active;
1910 }
1911 SparseIntArray uids = active.get(name);
1912 if (uids == null) {
1913 uids = new SparseIntArray();
1914 active.put(name, uids);
1915 }
1916 if (uids.indexOfKey(uid) >= 0) {
1917 // Already set, nothing to do!
1918 return false;
1919 }
1920 uids.put(uid, poolIdx);
1921 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1922 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1923 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1924 if (active == null) {
1925 // not currently active, nothing to do.
1926 return false;
1927 }
1928 SparseIntArray uids = active.get(name);
1929 if (uids == null) {
1930 // not currently active, nothing to do.
1931 return false;
1932 }
1933 idx = uids.indexOfKey(uid);
1934 if (idx < 0) {
1935 // not currently active, nothing to do.
1936 return false;
1937 }
1938 uids.removeAt(idx);
1939 if (uids.size() <= 0) {
1940 active.remove(name);
1941 }
1942 }
1943 return true;
1944 }
1945
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001946 public void removeEvents(int code) {
1947 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1948 mActiveEvents[idx] = null;
1949 }
1950
Dianne Hackborn37de0982014-05-09 09:32:18 -07001951 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1952 return mActiveEvents[code];
1953 }
1954 }
1955
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001956 public static final class BitDescription {
1957 public final int mask;
1958 public final int shift;
1959 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001960 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001961 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001962 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07001963
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001964 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001965 this.mask = mask;
1966 this.shift = -1;
1967 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001968 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001969 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001970 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001971 }
Bookatzc8c44962017-05-11 12:12:54 -07001972
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001973 public BitDescription(int mask, int shift, String name, String shortName,
1974 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001975 this.mask = mask;
1976 this.shift = shift;
1977 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001978 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001979 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001980 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001981 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001982 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001983
Dianne Hackbornfc064132014-06-02 12:42:12 -07001984 /**
1985 * Don't allow any more batching in to the current history event. This
1986 * is called when printing partial histories, so to ensure that the next
1987 * history event will go in to a new batch after what was printed in the
1988 * last partial history.
1989 */
1990 public abstract void commitCurrentHistoryBatchLocked();
1991
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001992 public abstract int getHistoryTotalSize();
1993
1994 public abstract int getHistoryUsedSize();
1995
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001996 public abstract boolean startIteratingHistoryLocked();
1997
Dianne Hackborn099bc622014-01-22 13:39:16 -08001998 public abstract int getHistoryStringPoolSize();
1999
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002000 public abstract int getHistoryStringPoolBytes();
2001
2002 public abstract String getHistoryTagPoolString(int index);
2003
2004 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002005
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002006 public abstract boolean getNextHistoryLocked(HistoryItem out);
2007
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002008 public abstract void finishIteratingHistoryLocked();
2009
2010 public abstract boolean startIteratingOldHistoryLocked();
2011
2012 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
2013
2014 public abstract void finishIteratingOldHistoryLocked();
2015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002017 * Return the base time offset for the battery history.
2018 */
2019 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07002020
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002021 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 * Returns the number of times the device has been started.
2023 */
2024 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07002025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002027 * 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 -08002028 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002029 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 * {@hide}
2031 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002032 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002033
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002034 /**
2035 * Returns the number of times the screen was turned on.
2036 *
2037 * {@hide}
2038 */
2039 public abstract int getScreenOnCount(int which);
2040
Mike Mac2f518a2017-09-19 16:06:03 -07002041 /**
2042 * Returns the time in microseconds that the screen has been dozing while the device was
2043 * running on battery.
2044 *
2045 * {@hide}
2046 */
2047 public abstract long getScreenDozeTime(long elapsedRealtimeUs, int which);
2048
2049 /**
2050 * Returns the number of times the screen was turned dozing.
2051 *
2052 * {@hide}
2053 */
2054 public abstract int getScreenDozeCount(int which);
2055
Jeff Browne95c3cd2014-05-02 16:59:26 -07002056 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
2057
Dianne Hackborn617f8772009-03-31 15:04:46 -07002058 public static final int SCREEN_BRIGHTNESS_DARK = 0;
2059 public static final int SCREEN_BRIGHTNESS_DIM = 1;
2060 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
2061 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
2062 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07002063
Dianne Hackborn617f8772009-03-31 15:04:46 -07002064 static final String[] SCREEN_BRIGHTNESS_NAMES = {
2065 "dark", "dim", "medium", "light", "bright"
2066 };
Bookatzc8c44962017-05-11 12:12:54 -07002067
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002068 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
2069 "0", "1", "2", "3", "4"
2070 };
2071
Dianne Hackborn617f8772009-03-31 15:04:46 -07002072 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07002073
Dianne Hackborn617f8772009-03-31 15:04:46 -07002074 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002075 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07002076 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07002077 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002078 * {@hide}
2079 */
2080 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002081 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002084 * Returns the {@link Timer} object that tracks the given screen brightness.
2085 *
2086 * {@hide}
2087 */
2088 public abstract Timer getScreenBrightnessTimer(int brightnessBin);
2089
2090 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002091 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002092 * running on battery.
2093 *
2094 * {@hide}
2095 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002096 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002097
2098 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002099 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002100 *
2101 * {@hide}
2102 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002103 public abstract int getPowerSaveModeEnabledCount(int which);
2104
2105 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002106 * Constant for device idle mode: not active.
2107 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002108 public static final int DEVICE_IDLE_MODE_OFF = ServerProtoEnums.DEVICE_IDLE_MODE_OFF; // 0
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002109
2110 /**
2111 * Constant for device idle mode: active in lightweight mode.
2112 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002113 public static final int DEVICE_IDLE_MODE_LIGHT = ServerProtoEnums.DEVICE_IDLE_MODE_LIGHT; // 1
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002114
2115 /**
2116 * Constant for device idle mode: active in full mode.
2117 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002118 public static final int DEVICE_IDLE_MODE_DEEP = ServerProtoEnums.DEVICE_IDLE_MODE_DEEP; // 2
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002119
2120 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002121 * Returns the time in microseconds that device has been in idle mode while
2122 * running on battery.
2123 *
2124 * {@hide}
2125 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002126 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002127
2128 /**
2129 * Returns the number of times that the devie has gone in to idle mode.
2130 *
2131 * {@hide}
2132 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002133 public abstract int getDeviceIdleModeCount(int mode, int which);
2134
2135 /**
2136 * Return the longest duration we spent in a particular device idle mode (fully in the
2137 * mode, not in idle maintenance etc).
2138 */
2139 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002140
2141 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002142 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002143 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002144 * counts all of the time that we consider the device to be idle, whether or not
2145 * it is currently in the actual device idle mode.
2146 *
2147 * {@hide}
2148 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002149 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002150
2151 /**
Bookatz8c6571b2017-10-24 15:04:41 -07002152 * Returns the number of times that the device has started idling.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002153 *
2154 * {@hide}
2155 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002156 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002157
2158 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002159 * Returns the number of times that connectivity state changed.
2160 *
2161 * {@hide}
2162 */
2163 public abstract int getNumConnectivityChange(int which);
2164
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002165
2166 /**
2167 * Returns the time in microseconds that the phone has been running with
2168 * the given GPS signal quality level
2169 *
2170 * {@hide}
2171 */
2172 public abstract long getGpsSignalQualityTime(int strengthBin,
2173 long elapsedRealtimeUs, int which);
2174
2175 /**
2176 * Returns the GPS battery drain in mA-ms
2177 *
2178 * {@hide}
2179 */
2180 public abstract long getGpsBatteryDrainMaMs();
2181
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002182 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002183 * 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 -08002184 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002185 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 * {@hide}
2187 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002188 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002189
Dianne Hackborn627bba72009-03-24 22:32:56 -07002190 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002191 * Returns the number of times a phone call was activated.
2192 *
2193 * {@hide}
2194 */
2195 public abstract int getPhoneOnCount(int which);
2196
2197 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002198 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002199 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002200 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002201 * {@hide}
2202 */
2203 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002204 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002205
Dianne Hackborn617f8772009-03-31 15:04:46 -07002206 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07002207 * Returns the time in microseconds that the phone has been trying to
2208 * acquire a signal.
2209 *
2210 * {@hide}
2211 */
2212 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002213 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002214
2215 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002216 * Returns the {@link Timer} object that tracks how much the phone has been trying to
2217 * acquire a signal.
2218 *
2219 * {@hide}
2220 */
2221 public abstract Timer getPhoneSignalScanningTimer();
2222
2223 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002224 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002225 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002226 * {@hide}
2227 */
2228 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
2229
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002230 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002231 * Return the {@link Timer} object used to track the given signal strength's duration and
2232 * counts.
2233 */
2234 protected abstract Timer getPhoneSignalStrengthTimer(int strengthBin);
2235
2236 /**
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002237 * Returns the time in microseconds that the mobile network has been active
2238 * (in a high power state).
2239 *
2240 * {@hide}
2241 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002242 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002243
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002244 /**
2245 * Returns the number of times that the mobile network has transitioned to the
2246 * active state.
2247 *
2248 * {@hide}
2249 */
2250 public abstract int getMobileRadioActiveCount(int which);
2251
2252 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002253 * Returns the time in microseconds that is the difference between the mobile radio
2254 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
2255 * from the radio.
2256 *
2257 * {@hide}
2258 */
2259 public abstract long getMobileRadioActiveAdjustedTime(int which);
2260
2261 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002262 * Returns the time in microseconds that the mobile network has been active
2263 * (in a high power state) but not being able to blame on an app.
2264 *
2265 * {@hide}
2266 */
2267 public abstract long getMobileRadioActiveUnknownTime(int which);
2268
2269 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002270 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002271 *
2272 * {@hide}
2273 */
2274 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002275
Tej Singheee317b2018-03-07 19:28:05 -08002276 public static final int DATA_CONNECTION_NONE = 0;
2277 public static final int DATA_CONNECTION_OTHER = TelephonyManager.MAX_NETWORK_TYPE + 1;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002278
Dianne Hackborn627bba72009-03-24 22:32:56 -07002279 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002280 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002281 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Siddharth Rayc72081d2018-02-13 11:31:54 -08002282 "ehrpd", "hspap", "gsm", "td_scdma", "iwlan", "lte_ca", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002283 };
Bookatzc8c44962017-05-11 12:12:54 -07002284
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002285 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002286
Dianne Hackborn627bba72009-03-24 22:32:56 -07002287 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002288 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002289 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002290 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002291 * {@hide}
2292 */
2293 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002294 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002297 * Returns the number of times the phone has entered the given data
2298 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002299 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002300 * {@hide}
2301 */
2302 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002303
Kweku Adams87b19ec2017-10-09 12:40:03 -07002304 /**
2305 * Returns the {@link Timer} object that tracks the phone's data connection type stats.
2306 */
2307 public abstract Timer getPhoneDataConnectionTimer(int dataType);
2308
Dianne Hackborn3251b902014-06-20 14:40:53 -07002309 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2310 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2311 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2312 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2313 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2314 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2315 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2316 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2317 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2318 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2319 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2320 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2321 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2322
2323 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2324
2325 static final String[] WIFI_SUPPL_STATE_NAMES = {
2326 "invalid", "disconn", "disabled", "inactive", "scanning",
2327 "authenticating", "associating", "associated", "4-way-handshake",
2328 "group-handshake", "completed", "dormant", "uninit"
2329 };
2330
2331 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2332 "inv", "dsc", "dis", "inact", "scan",
2333 "auth", "ascing", "asced", "4-way",
2334 "group", "compl", "dorm", "uninit"
2335 };
2336
Mike Mac2f518a2017-09-19 16:06:03 -07002337 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002338 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002339 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2340 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002341 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002342 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2343 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2344 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002345 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002346 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002347 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002348 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002349 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2350 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Mike Mac2f518a2017-09-19 16:06:03 -07002351 new BitDescription(HistoryItem.STATE_SCREEN_DOZE_FLAG, "screen_doze", "Sd"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002352 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2353 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2354 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2355 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2356 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2357 new String[] {"in", "out", "emergency", "off"},
2358 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002359 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2360 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2361 SignalStrength.SIGNAL_STRENGTH_NAMES,
2362 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002363 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2364 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2365 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002366 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002367
Mike Ma926a97c2018-03-25 02:32:35 -07002368 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002369 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002370 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002371 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002372 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002373 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002374 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2375 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2376 new String[] { "off", "light", "full", "???" },
2377 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002378 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
Mike Ma926a97c2018-03-25 02:32:35 -07002379 new BitDescription(HistoryItem.STATE2_USB_DATA_LINK_FLAG, "usb_data", "Ud"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002380 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2381 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002382 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2383 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2384 new String[] { "0", "1", "2", "3", "4" },
2385 new String[] { "0", "1", "2", "3", "4" }),
2386 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2387 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2388 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002389 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002390 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Siddharth Rayf5e796a2018-01-22 18:18:17 -08002391 new BitDescription(HistoryItem.STATE2_CELLULAR_HIGH_TX_POWER_FLAG,
2392 "cellular_high_tx_power", "Chtp"),
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002393 new BitDescription(HistoryItem.STATE2_GPS_SIGNAL_QUALITY_MASK,
2394 HistoryItem.STATE2_GPS_SIGNAL_QUALITY_SHIFT, "gps_signal_quality", "Gss",
Siddharth Rayf5e796a2018-01-22 18:18:17 -08002395 new String[] { "poor", "good"}, new String[] { "poor", "good"})
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002396 };
2397
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002398 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002399 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002400 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2401 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002402 };
2403
2404 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002405 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002406 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002407 "Esw", "Ewa", "Elw", "Eec"
2408 };
2409
2410 @FunctionalInterface
2411 public interface IntToString {
2412 String applyAsString(int val);
2413 }
2414
2415 private static final IntToString sUidToString = UserHandle::formatUid;
2416 private static final IntToString sIntToString = Integer::toString;
2417
2418 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2419 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2420 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2421 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2422 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002423 };
2424
Dianne Hackborn617f8772009-03-31 15:04:46 -07002425 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08002426 * Returns total time for WiFi Multicast Wakelock timer.
2427 * Note that this may be different from the sum of per uid timer values.
2428 *
2429 * {@hide}
2430 */
2431 public abstract long getWifiMulticastWakelockTime(long elapsedRealtimeUs, int which);
2432
2433 /**
2434 * Returns total time for WiFi Multicast Wakelock timer
2435 * Note that this may be different from the sum of per uid timer values.
2436 *
2437 * {@hide}
2438 */
2439 public abstract int getWifiMulticastWakelockCount(int which);
2440
2441 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002442 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002443 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002444 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002445 * {@hide}
2446 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002447 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002448
2449 /**
Siddharth Rayb50a6842017-12-14 15:15:28 -08002450 * Returns the time in microseconds that wifi has been active while the device was
2451 * running on battery.
2452 *
2453 * {@hide}
2454 */
2455 public abstract long getWifiActiveTime(long elapsedRealtimeUs, int which);
2456
2457 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002458 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002459 * been in the running state while the device was running on battery.
2460 *
2461 * {@hide}
2462 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002463 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002464
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002465 public static final int WIFI_STATE_OFF = 0;
2466 public static final int WIFI_STATE_OFF_SCANNING = 1;
2467 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2468 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2469 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2470 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2471 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2472 public static final int WIFI_STATE_SOFT_AP = 7;
2473
2474 static final String[] WIFI_STATE_NAMES = {
2475 "off", "scanning", "no_net", "disconn",
2476 "sta", "p2p", "sta_p2p", "soft_ap"
2477 };
2478
2479 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2480
2481 /**
2482 * Returns the time in microseconds that WiFi has been running in the given state.
2483 *
2484 * {@hide}
2485 */
2486 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002487 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002488
2489 /**
2490 * Returns the number of times that WiFi has entered the given state.
2491 *
2492 * {@hide}
2493 */
2494 public abstract int getWifiStateCount(int wifiState, int which);
2495
The Android Open Source Project10592532009-03-18 17:39:46 -07002496 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002497 * Returns the {@link Timer} object that tracks the given WiFi state.
2498 *
2499 * {@hide}
2500 */
2501 public abstract Timer getWifiStateTimer(int wifiState);
2502
2503 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002504 * Returns the time in microseconds that the wifi supplicant has been
2505 * in a given state.
2506 *
2507 * {@hide}
2508 */
2509 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2510
2511 /**
2512 * Returns the number of times that the wifi supplicant has transitioned
2513 * to a given state.
2514 *
2515 * {@hide}
2516 */
2517 public abstract int getWifiSupplStateCount(int state, int which);
2518
Kweku Adams87b19ec2017-10-09 12:40:03 -07002519 /**
2520 * Returns the {@link Timer} object that tracks the given wifi supplicant state.
2521 *
2522 * {@hide}
2523 */
2524 public abstract Timer getWifiSupplStateTimer(int state);
2525
Dianne Hackborn3251b902014-06-20 14:40:53 -07002526 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2527
2528 /**
2529 * Returns the time in microseconds that WIFI has been running with
2530 * the given signal strength.
2531 *
2532 * {@hide}
2533 */
2534 public abstract long getWifiSignalStrengthTime(int strengthBin,
2535 long elapsedRealtimeUs, int which);
2536
2537 /**
2538 * Returns the number of times WIFI has entered the given signal strength.
2539 *
2540 * {@hide}
2541 */
2542 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2543
2544 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002545 * Returns the {@link Timer} object that tracks the given WIFI signal strength.
2546 *
2547 * {@hide}
2548 */
2549 public abstract Timer getWifiSignalStrengthTimer(int strengthBin);
2550
2551 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002552 * Returns the time in microseconds that the flashlight has been on while the device was
2553 * running on battery.
2554 *
2555 * {@hide}
2556 */
2557 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2558
2559 /**
2560 * Returns the number of times that the flashlight has been turned on while the device was
2561 * running on battery.
2562 *
2563 * {@hide}
2564 */
2565 public abstract long getFlashlightOnCount(int which);
2566
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002567 /**
2568 * Returns the time in microseconds that the camera has been on while the device was
2569 * running on battery.
2570 *
2571 * {@hide}
2572 */
2573 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2574
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002575 /**
2576 * Returns the time in microseconds that bluetooth scans were running while the device was
2577 * on battery.
2578 *
2579 * {@hide}
2580 */
2581 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002582
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002583 public static final int NETWORK_MOBILE_RX_DATA = 0;
2584 public static final int NETWORK_MOBILE_TX_DATA = 1;
2585 public static final int NETWORK_WIFI_RX_DATA = 2;
2586 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002587 public static final int NETWORK_BT_RX_DATA = 4;
2588 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002589 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2590 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2591 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2592 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2593 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002594
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002595 public abstract long getNetworkActivityBytes(int type, int which);
2596 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002597
Adam Lesinskie08af192015-03-25 16:42:59 -07002598 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002599 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002600 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002601 * actual power data.
2602 */
2603 public abstract boolean hasWifiActivityReporting();
2604
2605 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002606 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2607 * in various radio controller states, such as transmit, receive, and idle.
2608 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002609 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002610 public abstract ControllerActivityCounter getWifiControllerActivity();
2611
2612 /**
2613 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2614 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2615 * actual power data.
2616 */
2617 public abstract boolean hasBluetoothActivityReporting();
2618
2619 /**
2620 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2621 * in various radio controller states, such as transmit, receive, and idle.
2622 * @return non-null {@link ControllerActivityCounter}
2623 */
2624 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2625
2626 /**
2627 * Returns true if the BatteryStats object has detailed modem power reports.
2628 * When true, calling {@link #getModemControllerActivity()} will yield the
2629 * actual power data.
2630 */
2631 public abstract boolean hasModemActivityReporting();
2632
2633 /**
2634 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2635 * in various radio controller states, such as transmit, receive, and idle.
2636 * @return non-null {@link ControllerActivityCounter}
2637 */
2638 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002639
The Android Open Source Project10592532009-03-18 17:39:46 -07002640 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002641 * Return the wall clock time when battery stats data collection started.
2642 */
2643 public abstract long getStartClockTime();
2644
2645 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002646 * Return platform version tag that we were running in when the battery stats started.
2647 */
2648 public abstract String getStartPlatformVersion();
2649
2650 /**
2651 * Return platform version tag that we were running in when the battery stats ended.
2652 */
2653 public abstract String getEndPlatformVersion();
2654
2655 /**
2656 * Return the internal version code of the parcelled format.
2657 */
2658 public abstract int getParcelVersion();
2659
2660 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 * Return whether we are currently running on battery.
2662 */
2663 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 /**
2666 * Returns a SparseArray containing the statistics for each uid.
2667 */
2668 public abstract SparseArray<? extends Uid> getUidStats();
2669
2670 /**
2671 * Returns the current battery uptime in microseconds.
2672 *
2673 * @param curTime the amount of elapsed realtime in microseconds.
2674 */
2675 public abstract long getBatteryUptime(long curTime);
2676
2677 /**
2678 * Returns the current battery realtime in microseconds.
2679 *
2680 * @param curTime the amount of elapsed realtime in microseconds.
2681 */
2682 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002683
The Android Open Source Project10592532009-03-18 17:39:46 -07002684 /**
Evan Millar633a1742009-04-02 16:36:33 -07002685 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002686 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002687 */
Evan Millar633a1742009-04-02 16:36:33 -07002688 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002689
The Android Open Source Project10592532009-03-18 17:39:46 -07002690 /**
Evan Millar633a1742009-04-02 16:36:33 -07002691 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2692 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002693 */
Evan Millar633a1742009-04-02 16:36:33 -07002694 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695
2696 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002697 * Get the amount the battery has discharged since the stats were
2698 * last reset after charging, as a lower-end approximation.
2699 */
2700 public abstract int getLowDischargeAmountSinceCharge();
2701
2702 /**
2703 * Get the amount the battery has discharged since the stats were
2704 * last reset after charging, as an upper-end approximation.
2705 */
2706 public abstract int getHighDischargeAmountSinceCharge();
2707
2708 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002709 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2710 */
2711 public abstract int getDischargeAmount(int which);
2712
2713 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002714 * Get the amount the battery has discharged while the screen was on,
2715 * since the last time power was unplugged.
2716 */
2717 public abstract int getDischargeAmountScreenOn();
2718
2719 /**
2720 * Get the amount the battery has discharged while the screen was on,
2721 * since the last time the device was charged.
2722 */
2723 public abstract int getDischargeAmountScreenOnSinceCharge();
2724
2725 /**
2726 * Get the amount the battery has discharged while the screen was off,
2727 * since the last time power was unplugged.
2728 */
2729 public abstract int getDischargeAmountScreenOff();
2730
2731 /**
2732 * Get the amount the battery has discharged while the screen was off,
2733 * since the last time the device was charged.
2734 */
2735 public abstract int getDischargeAmountScreenOffSinceCharge();
2736
2737 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002738 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002739 * since the last time power was unplugged.
2740 */
2741 public abstract int getDischargeAmountScreenDoze();
2742
2743 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002744 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002745 * since the last time the device was charged.
2746 */
2747 public abstract int getDischargeAmountScreenDozeSinceCharge();
2748
2749 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 * Returns the total, last, or current battery uptime in microseconds.
2751 *
2752 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002753 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 */
2755 public abstract long computeBatteryUptime(long curTime, int which);
2756
2757 /**
2758 * Returns the total, last, or current battery realtime in microseconds.
2759 *
2760 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002761 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 */
2763 public abstract long computeBatteryRealtime(long curTime, int which);
2764
2765 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002766 * Returns the total, last, or current battery screen off/doze uptime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002767 *
2768 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002769 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002770 */
2771 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2772
2773 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002774 * Returns the total, last, or current battery screen off/doze realtime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002775 *
2776 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002777 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002778 */
2779 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2780
2781 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 * Returns the total, last, or current uptime in microseconds.
2783 *
2784 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002785 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 */
2787 public abstract long computeUptime(long curTime, int which);
2788
2789 /**
2790 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002791 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002793 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 */
2795 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002796
2797 /**
2798 * Compute an approximation for how much run time (in microseconds) is remaining on
2799 * the battery. Returns -1 if no time can be computed: either there is not
2800 * enough current data to make a decision, or the battery is currently
2801 * charging.
2802 *
2803 * @param curTime The current elepsed realtime in microseconds.
2804 */
2805 public abstract long computeBatteryTimeRemaining(long curTime);
2806
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002807 // The part of a step duration that is the actual time.
2808 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2809
2810 // Bits in a step duration that are the new battery level we are at.
2811 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002812 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002813
2814 // Bits in a step duration that are the initial mode we were in at that step.
2815 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002816 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002817
2818 // Bits in a step duration that indicate which modes changed during that step.
2819 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002820 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002821
2822 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2823 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2824
Santos Cordone94f0502017-02-24 12:31:20 -08002825 // The largest value for screen state that is tracked in battery states. Any values above
2826 // this should be mapped back to one of the tracked values before being tracked here.
2827 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2828
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002829 // Step duration mode: power save is on.
2830 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2831
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002832 // Step duration mode: device is currently in idle mode.
2833 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2834
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002835 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2836 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002837 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2838 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002839 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2840 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2841 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2842 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2843 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002844 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2845 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002846 };
2847 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2848 (Display.STATE_OFF-1),
2849 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002850 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002851 (Display.STATE_ON-1),
2852 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2853 (Display.STATE_DOZE-1),
2854 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2855 (Display.STATE_DOZE_SUSPEND-1),
2856 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002857 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002858 };
2859 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2860 "screen off",
2861 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002862 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002863 "screen on",
2864 "screen on power save",
2865 "screen doze",
2866 "screen doze power save",
2867 "screen doze-suspend",
2868 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002869 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002870 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002871
2872 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002873 * Return the amount of battery discharge while the screen was off, measured in
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002874 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2875 * a coulomb counter.
2876 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002877 public abstract long getUahDischargeScreenOff(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002878
2879 /**
2880 * Return the amount of battery discharge while the screen was in doze mode, measured in
2881 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2882 * a coulomb counter.
2883 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002884 public abstract long getUahDischargeScreenDoze(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002885
2886 /**
2887 * Return the amount of battery discharge measured in micro-Ampere-hours. This will be
2888 * non-zero only if the device's battery has a coulomb counter.
2889 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002890 public abstract long getUahDischarge(int which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002891
2892 /**
Mike Ma15313c92017-11-15 17:58:21 -08002893 * @return the amount of battery discharge while the device is in light idle mode, measured in
2894 * micro-Ampere-hours.
2895 */
2896 public abstract long getUahDischargeLightDoze(int which);
2897
2898 /**
2899 * @return the amount of battery discharge while the device is in deep idle mode, measured in
2900 * micro-Ampere-hours.
2901 */
2902 public abstract long getUahDischargeDeepDoze(int which);
2903
2904 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002905 * Returns the estimated real battery capacity, which may be less than the capacity
2906 * declared by the PowerProfile.
2907 * @return The estimated battery capacity in mAh.
2908 */
2909 public abstract int getEstimatedBatteryCapacity();
2910
2911 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002912 * @return The minimum learned battery capacity in uAh.
2913 */
2914 public abstract int getMinLearnedBatteryCapacity();
2915
2916 /**
2917 * @return The maximum learned battery capacity in uAh.
2918 */
2919 public abstract int getMaxLearnedBatteryCapacity() ;
2920
2921 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002922 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002923 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002924 public abstract LevelStepTracker getDischargeLevelStepTracker();
2925
2926 /**
2927 * Return the array of daily discharge step durations.
2928 */
2929 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002930
2931 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002932 * Compute an approximation for how much time (in microseconds) remains until the battery
2933 * is fully charged. Returns -1 if no time can be computed: either there is not
2934 * enough current data to make a decision, or the battery is currently
2935 * discharging.
2936 *
2937 * @param curTime The current elepsed realtime in microseconds.
2938 */
2939 public abstract long computeChargeTimeRemaining(long curTime);
2940
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002941 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002942 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002943 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002944 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002945
2946 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002947 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002948 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002949 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002950
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002951 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2952
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002953 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002954
Evan Millarc64edde2009-04-18 12:26:32 -07002955 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956
Bookatz50df7112017-08-04 14:53:26 -07002957 /**
2958 * Returns Timers tracking the total time of each Resource Power Manager state and voter.
2959 */
2960 public abstract Map<String, ? extends Timer> getRpmStats();
2961 /**
2962 * Returns Timers tracking the screen-off time of each Resource Power Manager state and voter.
2963 */
2964 public abstract Map<String, ? extends Timer> getScreenOffRpmStats();
2965
2966
James Carr2dd7e5e2016-07-20 18:48:39 -07002967 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2968
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002969 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2970
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002971 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 long days = seconds / (60 * 60 * 24);
2973 if (days != 0) {
2974 out.append(days);
2975 out.append("d ");
2976 }
2977 long used = days * 60 * 60 * 24;
2978
2979 long hours = (seconds - used) / (60 * 60);
2980 if (hours != 0 || used != 0) {
2981 out.append(hours);
2982 out.append("h ");
2983 }
2984 used += hours * 60 * 60;
2985
2986 long mins = (seconds-used) / 60;
2987 if (mins != 0 || used != 0) {
2988 out.append(mins);
2989 out.append("m ");
2990 }
2991 used += mins * 60;
2992
2993 if (seconds != 0 || used != 0) {
2994 out.append(seconds-used);
2995 out.append("s ");
2996 }
2997 }
2998
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002999 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003000 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003001 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 sb.append(time - (sec * 1000));
3003 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 }
3005
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003006 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003007 long sec = time / 1000;
3008 formatTimeRaw(sb, sec);
3009 sb.append(time - (sec * 1000));
3010 sb.append("ms");
3011 }
3012
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003013 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003015 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 }
3017 float perc = ((float)num) / ((float)den) * 100;
3018 mFormatBuilder.setLength(0);
3019 mFormatter.format("%.1f%%", perc);
3020 return mFormatBuilder.toString();
3021 }
3022
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003023 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07003024 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07003025
Evan Millar22ac0432009-03-31 11:33:18 -07003026 if (bytes < BYTES_PER_KB) {
3027 return bytes + "B";
3028 } else if (bytes < BYTES_PER_MB) {
3029 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
3030 return mFormatBuilder.toString();
3031 } else if (bytes < BYTES_PER_GB){
3032 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
3033 return mFormatBuilder.toString();
3034 } else {
3035 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
3036 return mFormatBuilder.toString();
3037 }
3038 }
3039
Kweku Adams103351f2017-10-16 14:39:34 -07003040 private static long roundUsToMs(long timeUs) {
3041 return (timeUs + 500) / 1000;
3042 }
3043
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003044 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003045 if (timer != null) {
3046 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003047 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003048 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
3049 return totalTimeMillis;
3050 }
3051 return 0;
3052 }
3053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003054 /**
3055 *
3056 * @param sb a StringBuilder object.
3057 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003058 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003060 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003061 * @param linePrefix a String to be prepended to each line of output.
3062 * @return the line prefix
3063 */
3064 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003065 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003068 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003069
Evan Millarc64edde2009-04-18 12:26:32 -07003070 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 if (totalTimeMillis != 0) {
3072 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003073 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08003074 if (name != null) {
3075 sb.append(name);
3076 sb.append(' ');
3077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 sb.append('(');
3079 sb.append(count);
3080 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003081 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
3082 if (maxDurationMs >= 0) {
3083 sb.append(" max=");
3084 sb.append(maxDurationMs);
3085 }
Bookatz506a8182017-05-01 14:18:42 -07003086 // Put actual time if it is available and different from totalTimeMillis.
3087 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
3088 if (totalDurMs > totalTimeMillis) {
3089 sb.append(" actual=");
3090 sb.append(totalDurMs);
3091 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07003092 if (timer.isRunningLocked()) {
3093 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3094 if (currentMs >= 0) {
3095 sb.append(" (running for ");
3096 sb.append(currentMs);
3097 sb.append("ms)");
3098 } else {
3099 sb.append(" (running)");
3100 }
3101 }
3102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 return ", ";
3104 }
3105 }
3106 return linePrefix;
3107 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003108
3109 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07003110 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003111 *
3112 * @param pw a PrintWriter object to print to.
3113 * @param sb a StringBuilder object.
3114 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08003115 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003116 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
3117 * @param prefix a String to be prepended to each line of output.
3118 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07003119 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003120 */
3121 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07003122 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003123 if (timer != null) {
3124 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07003125 final long totalTimeMs = (timer.getTotalTimeLocked(
3126 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003127 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003128 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003129 sb.setLength(0);
3130 sb.append(prefix);
3131 sb.append(" ");
3132 sb.append(type);
3133 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003134 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003135 sb.append("realtime (");
3136 sb.append(count);
3137 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003138 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
3139 if (maxDurationMs >= 0) {
3140 sb.append(" max=");
3141 sb.append(maxDurationMs);
3142 }
3143 if (timer.isRunningLocked()) {
3144 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
3145 if (currentMs >= 0) {
3146 sb.append(" (running for ");
3147 sb.append(currentMs);
3148 sb.append("ms)");
3149 } else {
3150 sb.append(" (running)");
3151 }
3152 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003153 pw.println(sb.toString());
3154 return true;
3155 }
3156 }
3157 return false;
3158 }
Bookatzc8c44962017-05-11 12:12:54 -07003159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003160 /**
3161 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07003162 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 * @param sb a StringBuilder object.
3164 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003165 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003167 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 * @param linePrefix a String to be prepended to each line of output.
3169 * @return the line prefix
3170 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003171 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
3172 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003173 long totalTimeMicros = 0;
3174 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07003175 long max = 0;
3176 long current = 0;
3177 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003179 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07003180 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003181 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3182 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07003183 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 }
3185 sb.append(linePrefix);
3186 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
3187 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07003188 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003190 sb.append(',');
3191 sb.append(current);
3192 sb.append(',');
3193 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07003194 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
3195 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
3196 // totalDuration independent of totalTimeMicros (since they are not pooled).
3197 if (name != null) {
3198 sb.append(',');
3199 sb.append(totalDuration);
3200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 return ",";
3202 }
Bookatz506a8182017-05-01 14:18:42 -07003203
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003204 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
3205 String type) {
3206 pw.print(BATTERY_STATS_CHECKIN_VERSION);
3207 pw.print(',');
3208 pw.print(uid);
3209 pw.print(',');
3210 pw.print(category);
3211 pw.print(',');
3212 pw.print(type);
3213 }
3214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 /**
3216 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07003217 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 * @param pw the PageWriter to dump log to
3219 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3220 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3221 * @param args type-dependent data arguments
3222 */
Bookatzc8c44962017-05-11 12:12:54 -07003223 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003225 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003226 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003227 pw.print(',');
3228 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003230 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07003232
3233 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003234 * Dump a given timer stat for terse checkin mode.
3235 *
3236 * @param pw the PageWriter to dump log to
3237 * @param uid the UID to log
3238 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3239 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3240 * @param timer a {@link Timer} to dump stats for
3241 * @param rawRealtime the current elapsed realtime of the system in microseconds
3242 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3243 */
3244 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
3245 Timer timer, long rawRealtime, int which) {
3246 if (timer != null) {
3247 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003248 final long totalTime = roundUsToMs(timer.getTotalTimeLocked(rawRealtime, which));
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003249 final int count = timer.getCountLocked(which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003250 if (totalTime != 0 || count != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003251 dumpLine(pw, uid, category, type, totalTime, count);
3252 }
3253 }
3254 }
3255
3256 /**
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003257 * Dump a given timer stat to the proto stream.
3258 *
3259 * @param proto the ProtoOutputStream to log to
3260 * @param fieldId type of data, the field to save to (e.g. AggregatedBatteryStats.WAKELOCK)
3261 * @param timer a {@link Timer} to dump stats for
3262 * @param rawRealtimeUs the current elapsed realtime of the system in microseconds
3263 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3264 */
3265 private static void dumpTimer(ProtoOutputStream proto, long fieldId,
Kweku Adams87b19ec2017-10-09 12:40:03 -07003266 Timer timer, long rawRealtimeUs, int which) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003267 if (timer == null) {
3268 return;
3269 }
3270 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003271 final long timeMs = roundUsToMs(timer.getTotalTimeLocked(rawRealtimeUs, which));
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003272 final int count = timer.getCountLocked(which);
Kweku Adams103351f2017-10-16 14:39:34 -07003273 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs / 1000);
3274 final long curDurationMs = timer.getCurrentDurationMsLocked(rawRealtimeUs / 1000);
3275 final long totalDurationMs = timer.getTotalDurationMsLocked(rawRealtimeUs / 1000);
3276 if (timeMs != 0 || count != 0 || maxDurationMs != -1 || curDurationMs != -1
3277 || totalDurationMs != -1) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003278 final long token = proto.start(fieldId);
Kweku Adams103351f2017-10-16 14:39:34 -07003279 proto.write(TimerProto.DURATION_MS, timeMs);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003280 proto.write(TimerProto.COUNT, count);
Kweku Adams103351f2017-10-16 14:39:34 -07003281 // These values will be -1 for timers that don't implement the functionality.
3282 if (maxDurationMs != -1) {
3283 proto.write(TimerProto.MAX_DURATION_MS, maxDurationMs);
3284 }
3285 if (curDurationMs != -1) {
3286 proto.write(TimerProto.CURRENT_DURATION_MS, curDurationMs);
3287 }
3288 if (totalDurationMs != -1) {
3289 proto.write(TimerProto.TOTAL_DURATION_MS, totalDurationMs);
3290 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003291 proto.end(token);
3292 }
3293 }
3294
3295 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003296 * Checks if the ControllerActivityCounter has any data worth dumping.
3297 */
3298 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
3299 if (counter == null) {
3300 return false;
3301 }
3302
3303 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
3304 || counter.getRxTimeCounter().getCountLocked(which) != 0
3305 || counter.getPowerCounter().getCountLocked(which) != 0) {
3306 return true;
3307 }
3308
3309 for (LongCounter c : counter.getTxTimeCounters()) {
3310 if (c.getCountLocked(which) != 0) {
3311 return true;
3312 }
3313 }
3314 return false;
3315 }
3316
3317 /**
3318 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3319 * The order of the arguments in the final check in line is:
3320 *
3321 * idle, rx, power, tx...
3322 *
3323 * where tx... is one or more transmit level times.
3324 */
3325 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
3326 String type,
3327 ControllerActivityCounter counter,
3328 int which) {
3329 if (!controllerActivityHasData(counter, which)) {
3330 return;
3331 }
3332
3333 dumpLineHeader(pw, uid, category, type);
3334 pw.print(",");
3335 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
3336 pw.print(",");
3337 pw.print(counter.getRxTimeCounter().getCountLocked(which));
3338 pw.print(",");
3339 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3340 for (LongCounter c : counter.getTxTimeCounters()) {
3341 pw.print(",");
3342 pw.print(c.getCountLocked(which));
3343 }
3344 pw.println();
3345 }
3346
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003347 /**
3348 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3349 */
3350 private static void dumpControllerActivityProto(ProtoOutputStream proto, long fieldId,
3351 ControllerActivityCounter counter,
3352 int which) {
3353 if (!controllerActivityHasData(counter, which)) {
3354 return;
3355 }
3356
3357 final long cToken = proto.start(fieldId);
3358
3359 proto.write(ControllerActivityProto.IDLE_DURATION_MS,
3360 counter.getIdleTimeCounter().getCountLocked(which));
3361 proto.write(ControllerActivityProto.RX_DURATION_MS,
3362 counter.getRxTimeCounter().getCountLocked(which));
3363 proto.write(ControllerActivityProto.POWER_MAH,
3364 counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3365
3366 long tToken;
3367 LongCounter[] txCounters = counter.getTxTimeCounters();
3368 for (int i = 0; i < txCounters.length; ++i) {
3369 LongCounter c = txCounters[i];
3370 tToken = proto.start(ControllerActivityProto.TX);
3371 proto.write(ControllerActivityProto.TxLevel.LEVEL, i);
3372 proto.write(ControllerActivityProto.TxLevel.DURATION_MS, c.getCountLocked(which));
3373 proto.end(tToken);
3374 }
3375
3376 proto.end(cToken);
3377 }
3378
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003379 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
3380 String prefix, String controllerName,
3381 ControllerActivityCounter counter,
3382 int which) {
3383 if (controllerActivityHasData(counter, which)) {
3384 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
3385 }
3386 }
3387
3388 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
3389 String controllerName,
3390 ControllerActivityCounter counter, int which) {
3391 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
3392 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
3393 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003394 // Battery real time
3395 final long totalControllerActivityTimeMs
3396 = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003397 long totalTxTimeMs = 0;
3398 for (LongCounter txState : counter.getTxTimeCounters()) {
3399 totalTxTimeMs += txState.getCountLocked(which);
3400 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003401
Siddharth Rayb50a6842017-12-14 15:15:28 -08003402 if (controllerName.equals(WIFI_CONTROLLER_NAME)) {
3403 final long scanTimeMs = counter.getScanTimeCounter().getCountLocked(which);
3404 sb.setLength(0);
3405 sb.append(prefix);
3406 sb.append(" ");
3407 sb.append(controllerName);
3408 sb.append(" Scan time: ");
3409 formatTimeMs(sb, scanTimeMs);
3410 sb.append("(");
3411 sb.append(formatRatioLocked(scanTimeMs, totalControllerActivityTimeMs));
3412 sb.append(")");
3413 pw.println(sb.toString());
Siddharth Rayed754702018-02-15 12:44:37 -08003414
3415 final long sleepTimeMs
3416 = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + totalTxTimeMs);
3417 sb.setLength(0);
3418 sb.append(prefix);
3419 sb.append(" ");
3420 sb.append(controllerName);
3421 sb.append(" Sleep time: ");
3422 formatTimeMs(sb, sleepTimeMs);
3423 sb.append("(");
3424 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3425 sb.append(")");
3426 pw.println(sb.toString());
Siddharth Rayb50a6842017-12-14 15:15:28 -08003427 }
3428
Siddharth Rayed754702018-02-15 12:44:37 -08003429 if (controllerName.equals(CELLULAR_CONTROLLER_NAME)) {
3430 final long sleepTimeMs = counter.getSleepTimeCounter().getCountLocked(which);
3431 sb.setLength(0);
3432 sb.append(prefix);
3433 sb.append(" ");
3434 sb.append(controllerName);
3435 sb.append(" Sleep time: ");
3436 formatTimeMs(sb, sleepTimeMs);
3437 sb.append("(");
3438 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3439 sb.append(")");
3440 pw.println(sb.toString());
3441 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003442
3443 sb.setLength(0);
3444 sb.append(prefix);
3445 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003446 sb.append(controllerName);
3447 sb.append(" Idle time: ");
3448 formatTimeMs(sb, idleTimeMs);
3449 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003450 sb.append(formatRatioLocked(idleTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003451 sb.append(")");
3452 pw.println(sb.toString());
3453
3454 sb.setLength(0);
3455 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003456 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003457 sb.append(controllerName);
3458 sb.append(" Rx time: ");
3459 formatTimeMs(sb, rxTimeMs);
3460 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003461 sb.append(formatRatioLocked(rxTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003462 sb.append(")");
3463 pw.println(sb.toString());
3464
3465 sb.setLength(0);
3466 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003467 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003468 sb.append(controllerName);
3469 sb.append(" Tx time: ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003470
Siddharth Ray3c648c42017-10-02 17:30:58 -07003471 String [] powerLevel;
3472 switch(controllerName) {
Siddharth Rayb50a6842017-12-14 15:15:28 -08003473 case CELLULAR_CONTROLLER_NAME:
Siddharth Ray3c648c42017-10-02 17:30:58 -07003474 powerLevel = new String[] {
3475 " less than 0dBm: ",
3476 " 0dBm to 8dBm: ",
3477 " 8dBm to 15dBm: ",
3478 " 15dBm to 20dBm: ",
3479 " above 20dBm: "};
3480 break;
3481 default:
3482 powerLevel = new String[] {"[0]", "[1]", "[2]", "[3]", "[4]"};
3483 break;
3484 }
3485 final int numTxLvls = Math.min(counter.getTxTimeCounters().length, powerLevel.length);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003486 if (numTxLvls > 1) {
Siddharth Ray3c648c42017-10-02 17:30:58 -07003487 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003488 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3489 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3490 sb.setLength(0);
3491 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003492 sb.append(" ");
3493 sb.append(powerLevel[lvl]);
3494 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003495 formatTimeMs(sb, txLvlTimeMs);
3496 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003497 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003498 sb.append(")");
3499 pw.println(sb.toString());
3500 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003501 } else {
3502 final long txLvlTimeMs = counter.getTxTimeCounters()[0].getCountLocked(which);
3503 formatTimeMs(sb, txLvlTimeMs);
3504 sb.append("(");
3505 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
3506 sb.append(")");
3507 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003508 }
3509
Siddharth Ray3c648c42017-10-02 17:30:58 -07003510 if (powerDrainMaMs > 0) {
3511 sb.setLength(0);
3512 sb.append(prefix);
3513 sb.append(" ");
3514 sb.append(controllerName);
3515 sb.append(" Battery drain: ").append(
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003516 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
Siddharth Ray3c648c42017-10-02 17:30:58 -07003517 sb.append("mAh");
3518 pw.println(sb.toString());
3519 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003520 }
3521
3522 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003523 * Temporary for settings.
3524 */
3525 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3526 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3527 }
3528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003529 /**
3530 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003531 *
Kweku Adams87b19ec2017-10-09 12:40:03 -07003532 * NOTE: all times are expressed in microseconds, unless specified otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003533 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003534 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3535 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 final long rawUptime = SystemClock.uptimeMillis() * 1000;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003537 final long rawRealtimeMs = SystemClock.elapsedRealtime();
3538 final long rawRealtime = rawRealtimeMs * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3541 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003542 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3543 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3544 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003545 final long totalRealtime = computeRealtime(rawRealtime, which);
3546 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003547 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Mike Mac2f518a2017-09-19 16:06:03 -07003548 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003549 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003550 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003551 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3552 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003553 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003554 rawRealtime, which);
3555 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3556 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003557 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003558 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003559 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003560 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003561 final long dischargeCount = getUahDischarge(which);
3562 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
3563 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Ma15313c92017-11-15 17:58:21 -08003564 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
3565 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003566
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003567 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003568
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003569 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003570 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003571
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003572 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003574 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003575 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003576 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003577 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003578 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003579 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003580 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003581 getEstimatedBatteryCapacity(),
Mike Mac2f518a2017-09-19 16:06:03 -07003582 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity(),
3583 screenDozeTime / 1000);
Adam Lesinski67c134f2016-06-10 15:15:08 -07003584
Bookatzc8c44962017-05-11 12:12:54 -07003585
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003586 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003587 long fullWakeLockTimeTotal = 0;
3588 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003589
Evan Millar22ac0432009-03-31 11:33:18 -07003590 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003591 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003592
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003593 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3594 = u.getWakelockStats();
3595 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3596 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003597
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003598 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3599 if (fullWakeTimer != null) {
3600 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3601 which);
3602 }
3603
3604 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3605 if (partialWakeTimer != null) {
3606 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3607 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003608 }
3609 }
3610 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003611
3612 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003613 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3614 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3615 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3616 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3617 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3618 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3619 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3620 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003621 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3622 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003623 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3624 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003625 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3626 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003627
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003628 // Dump Modem controller stats
3629 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3630 getModemControllerActivity(), which);
3631
Adam Lesinskie283d332015-04-16 12:29:25 -07003632 // Dump Wifi controller stats
3633 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3634 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003635 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003636 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003637
3638 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3639 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003640
3641 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003642 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3643 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003645 // Dump misc stats
3646 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003647 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003648 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003649 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003650 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003651 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003652 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3653 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003654 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003655 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3656 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3657 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3658 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003659 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003660
Dianne Hackborn617f8772009-03-31 15:04:46 -07003661 // Dump screen brightness stats
3662 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3663 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003664 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003665 }
3666 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003667
Dianne Hackborn627bba72009-03-24 22:32:56 -07003668 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003669 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3670 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003671 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003672 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003673 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003674 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003675 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003676 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003677 args[i] = getPhoneSignalStrengthCount(i, which);
3678 }
3679 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003680
Dianne Hackborn627bba72009-03-24 22:32:56 -07003681 // Dump network type stats
3682 args = new Object[NUM_DATA_CONNECTION_TYPES];
3683 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003684 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003685 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003686 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3687 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3688 args[i] = getPhoneDataConnectionCount(i, which);
3689 }
3690 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003691
3692 // Dump wifi state stats
3693 args = new Object[NUM_WIFI_STATES];
3694 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003695 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003696 }
3697 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3698 for (int i=0; i<NUM_WIFI_STATES; i++) {
3699 args[i] = getWifiStateCount(i, which);
3700 }
3701 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3702
Dianne Hackborn3251b902014-06-20 14:40:53 -07003703 // Dump wifi suppl state stats
3704 args = new Object[NUM_WIFI_SUPPL_STATES];
3705 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3706 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3707 }
3708 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3709 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3710 args[i] = getWifiSupplStateCount(i, which);
3711 }
3712 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3713
3714 // Dump wifi signal strength stats
3715 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3716 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3717 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3718 }
3719 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3720 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3721 args[i] = getWifiSignalStrengthCount(i, which);
3722 }
3723 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3724
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003725 // Dump Multicast total stats
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003726 final long multicastWakeLockTimeTotalMicros =
3727 getWifiMulticastWakelockTime(rawRealtime, which);
3728 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003729 dumpLine(pw, 0 /* uid */, category, WIFI_MULTICAST_TOTAL_DATA,
3730 multicastWakeLockTimeTotalMicros / 1000,
3731 multicastWakeLockCountTotal);
3732
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003733 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003734 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003735 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003736 }
Bookatzc8c44962017-05-11 12:12:54 -07003737
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003738 if (which == STATS_SINCE_UNPLUGGED) {
3739 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3740 getDischargeStartLevel()-getDischargeCurrentLevel(),
3741 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003742 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
Mike Mac2f518a2017-09-19 16:06:03 -07003743 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003744 getDischargeAmountScreenDoze(), dischargeScreenDozeCount / 1000,
3745 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003746 } else {
3747 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3748 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003749 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003750 getDischargeAmountScreenOffSinceCharge(),
Mike Mac2f518a2017-09-19 16:06:03 -07003751 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003752 getDischargeAmountScreenDozeSinceCharge(), dischargeScreenDozeCount / 1000,
3753 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003754 }
Bookatzc8c44962017-05-11 12:12:54 -07003755
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003756 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003757 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003758 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003759 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003760 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003761 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003762 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3763 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003764 }
Evan Millarc64edde2009-04-18 12:26:32 -07003765 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003766 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003767 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003768 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3769 // Not doing the regular wake lock formatting to remain compatible
3770 // with the old checkin format.
3771 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3772 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003773 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003774 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003775 }
3776 }
Evan Millarc64edde2009-04-18 12:26:32 -07003777 }
Bookatzc8c44962017-05-11 12:12:54 -07003778
Bookatz50df7112017-08-04 14:53:26 -07003779 final Map<String, ? extends Timer> rpmStats = getRpmStats();
3780 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
3781 if (rpmStats.size() > 0) {
3782 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
3783 sb.setLength(0);
3784 Timer totalTimer = ent.getValue();
3785 long timeMs = (totalTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3786 int count = totalTimer.getCountLocked(which);
3787 Timer screenOffTimer = screenOffRpmStats.get(ent.getKey());
3788 long screenOffTimeMs = screenOffTimer != null
3789 ? (screenOffTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3790 int screenOffCount = screenOffTimer != null
3791 ? screenOffTimer.getCountLocked(which) : 0;
Bookatz82b341172017-09-07 19:06:08 -07003792 if (SCREEN_OFF_RPM_STATS_ENABLED) {
3793 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3794 "\"" + ent.getKey() + "\"", timeMs, count, screenOffTimeMs,
3795 screenOffCount);
3796 } else {
3797 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3798 "\"" + ent.getKey() + "\"", timeMs, count);
3799 }
Bookatz50df7112017-08-04 14:53:26 -07003800 }
3801 }
3802
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003803 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003804 helper.create(this);
3805 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003806 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003807 if (sippers != null && sippers.size() > 0) {
3808 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3809 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003810 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003811 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3812 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
Kweku Adams87b19ec2017-10-09 12:40:03 -07003813 int uid = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003814 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003815 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003816 String label;
3817 switch (bs.drainType) {
Kweku Adamsef728952018-04-10 17:56:16 -07003818 case AMBIENT_DISPLAY:
3819 label = "ambi";
3820 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003821 case IDLE:
3822 label="idle";
3823 break;
3824 case CELL:
3825 label="cell";
3826 break;
3827 case PHONE:
3828 label="phone";
3829 break;
3830 case WIFI:
3831 label="wifi";
3832 break;
3833 case BLUETOOTH:
3834 label="blue";
3835 break;
3836 case SCREEN:
3837 label="scrn";
3838 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003839 case FLASHLIGHT:
3840 label="flashlight";
3841 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003842 case APP:
3843 uid = bs.uidObj.getUid();
3844 label = "uid";
3845 break;
3846 case USER:
3847 uid = UserHandle.getUid(bs.userId, 0);
3848 label = "user";
3849 break;
3850 case UNACCOUNTED:
3851 label = "unacc";
3852 break;
3853 case OVERCOUNTED:
3854 label = "over";
3855 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003856 case CAMERA:
3857 label = "camera";
3858 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003859 case MEMORY:
3860 label = "memory";
3861 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003862 default:
3863 label = "???";
3864 }
3865 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07003866 BatteryStatsHelper.makemAh(bs.totalPowerMah),
3867 bs.shouldHide ? 1 : 0,
3868 BatteryStatsHelper.makemAh(bs.screenPowerMah),
3869 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003870 }
3871 }
3872
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003873 final long[] cpuFreqs = getCpuFreqs();
3874 if (cpuFreqs != null) {
3875 sb.setLength(0);
3876 for (int i = 0; i < cpuFreqs.length; ++i) {
3877 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3878 }
3879 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3880 }
3881
Kweku Adams87b19ec2017-10-09 12:40:03 -07003882 // Dump stats per UID.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003883 for (int iu = 0; iu < NU; iu++) {
3884 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003885 if (reqUid >= 0 && uid != reqUid) {
3886 continue;
3887 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003888 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003890 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003891 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3892 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3893 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3894 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3895 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3896 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3897 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3898 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003899 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003900 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3901 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003902 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003903 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3904 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003905 // Background data transfers
3906 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3907 which);
3908 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3909 which);
3910 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3911 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3912 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3913 which);
3914 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3915 which);
3916 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3917 which);
3918 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3919 which);
3920
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003921 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3922 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003923 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003924 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3925 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3926 || wifiBytesBgTx > 0
3927 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3928 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003929 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3930 wifiBytesRx, wifiBytesTx,
3931 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003932 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003933 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003934 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3935 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3936 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3937 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003938 }
3939
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003940 // Dump modem controller data, per UID.
3941 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3942 u.getModemControllerActivity(), which);
3943
3944 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003945 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3946 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3947 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003948 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3949 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003950 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3951 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3952 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003953 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003954 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003955 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3956 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003957 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3958 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003959 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003960 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003962
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003963 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3964 u.getWifiControllerActivity(), which);
3965
Bookatz867c0d72017-03-07 18:23:42 -08003966 final Timer bleTimer = u.getBluetoothScanTimer();
3967 if (bleTimer != null) {
3968 // Convert from microseconds to milliseconds with rounding
3969 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3970 / 1000;
3971 if (totalTime != 0) {
3972 final int count = bleTimer.getCountLocked(which);
3973 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3974 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003975 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3976 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3977 final long actualTimeBg = bleTimerBg != null ?
3978 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003979 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003980 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3981 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003982 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3983 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3984 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3985 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3986 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
3987 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3988 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
3989 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3990 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
3991 final Timer unoptimizedScanTimerBg =
3992 u.getBluetoothUnoptimizedScanBackgroundTimer();
3993 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
3994 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3995 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
3996 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3997
Bookatz867c0d72017-03-07 18:23:42 -08003998 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07003999 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
4000 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
4001 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08004002 }
4003 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004004
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004005 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
4006 u.getBluetoothControllerActivity(), which);
4007
Dianne Hackborn617f8772009-03-31 15:04:46 -07004008 if (u.hasUserActivity()) {
4009 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
4010 boolean hasData = false;
4011 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
4012 int val = u.getUserActivityCount(i, which);
4013 args[i] = val;
4014 if (val != 0) hasData = true;
4015 }
4016 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07004017 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004018 }
4019 }
Bookatzc8c44962017-05-11 12:12:54 -07004020
4021 if (u.getAggregatedPartialWakelockTimer() != null) {
4022 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07004023 // Times are since reset (regardless of 'which')
4024 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07004025 final Timer bgTimer = timer.getSubTimer();
4026 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004027 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07004028 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
4029 }
4030
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004031 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
4032 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4033 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4034 String linePrefix = "";
4035 sb.setLength(0);
4036 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
4037 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004038 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4039 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004040 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004041 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
4042 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004043 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
4044 rawRealtime, "w", which, linePrefix);
4045
Kweku Adams103351f2017-10-16 14:39:34 -07004046 // Only log if we had at least one wakelock...
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004047 if (sb.length() > 0) {
4048 String name = wakelocks.keyAt(iw);
4049 if (name.indexOf(',') >= 0) {
4050 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004051 }
Yi Jin02483362017-08-04 11:30:44 -07004052 if (name.indexOf('\n') >= 0) {
4053 name = name.replace('\n', '_');
4054 }
4055 if (name.indexOf('\r') >= 0) {
4056 name = name.replace('\r', '_');
4057 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004058 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 }
4060 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004061
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004062 // WiFi Multicast Wakelock Statistics
4063 final Timer mcTimer = u.getMulticastWakelockStats();
4064 if (mcTimer != null) {
4065 final long totalMcWakelockTimeMs =
4066 mcTimer.getTotalTimeLocked(rawRealtime, which) / 1000 ;
4067 final int countMcWakelock = mcTimer.getCountLocked(which);
4068 if(totalMcWakelockTimeMs > 0) {
4069 dumpLine(pw, uid, category, WIFI_MULTICAST_DATA,
4070 totalMcWakelockTimeMs, countMcWakelock);
4071 }
4072 }
4073
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004074 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4075 for (int isy=syncs.size()-1; isy>=0; isy--) {
4076 final Timer timer = syncs.valueAt(isy);
4077 // Convert from microseconds to milliseconds with rounding
4078 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4079 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07004080 final Timer bgTimer = timer.getSubTimer();
4081 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004082 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07004083 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004084 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004085 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07004086 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004087 }
4088 }
4089
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004090 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4091 for (int ij=jobs.size()-1; ij>=0; ij--) {
4092 final Timer timer = jobs.valueAt(ij);
4093 // Convert from microseconds to milliseconds with rounding
4094 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4095 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07004096 final Timer bgTimer = timer.getSubTimer();
4097 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004098 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07004099 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004100 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004101 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07004102 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004103 }
4104 }
4105
Dianne Hackborn94326cb2017-06-28 16:17:20 -07004106 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
4107 for (int ic=completions.size()-1; ic>=0; ic--) {
4108 SparseIntArray types = completions.valueAt(ic);
4109 if (types != null) {
4110 dumpLine(pw, uid, category, JOB_COMPLETION_DATA,
4111 "\"" + completions.keyAt(ic) + "\"",
4112 types.get(JobParameters.REASON_CANCELED, 0),
4113 types.get(JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, 0),
4114 types.get(JobParameters.REASON_PREEMPT, 0),
4115 types.get(JobParameters.REASON_TIMEOUT, 0),
4116 types.get(JobParameters.REASON_DEVICE_IDLE, 0));
4117 }
4118 }
4119
Amith Yamasani977e11f2018-02-16 11:29:54 -08004120 // Dump deferred jobs stats
4121 u.getDeferredJobsCheckinLineLocked(sb, which);
4122 if (sb.length() > 0) {
4123 dumpLine(pw, uid, category, JOBS_DEFERRED_DATA, sb.toString());
4124 }
4125
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004126 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
4127 rawRealtime, which);
4128 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
4129 rawRealtime, which);
4130 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
4131 rawRealtime, which);
4132 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
4133 rawRealtime, which);
4134
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004135 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4136 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004137 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004138 final Uid.Sensor se = sensors.valueAt(ise);
4139 final int sensorNumber = sensors.keyAt(ise);
4140 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004141 if (timer != null) {
4142 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004143 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4144 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004145 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004146 final int count = timer.getCountLocked(which);
4147 final Timer bgTimer = se.getSensorBackgroundTime();
4148 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004149 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4150 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4151 final long bgActualTime = bgTimer != null ?
4152 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4153 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
4154 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004156 }
4157 }
4158
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004159 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
4160 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004161
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07004162 dumpTimer(pw, uid, category, FOREGROUND_ACTIVITY_DATA, u.getForegroundActivityTimer(),
4163 rawRealtime, which);
4164
4165 dumpTimer(pw, uid, category, FOREGROUND_SERVICE_DATA, u.getForegroundServiceTimer(),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004166 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004167
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004168 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07004169 long totalStateTime = 0;
4170 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08004171 final long time = u.getProcessStateTime(ips, rawRealtime, which);
4172 totalStateTime += time;
4173 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004174 }
4175 if (totalStateTime > 0) {
4176 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
4177 }
4178
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004179 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4180 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004181 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004182 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004183 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004184 }
4185
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004186 // If the cpuFreqs is null, then don't bother checking for cpu freq times.
4187 if (cpuFreqs != null) {
4188 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
4189 // If total cpuFreqTimes is null, then we don't need to check for
4190 // screenOffCpuFreqTimes.
4191 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
4192 sb.setLength(0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004193 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004194 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004195 }
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004196 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
4197 if (screenOffCpuFreqTimeMs != null) {
4198 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
4199 sb.append("," + screenOffCpuFreqTimeMs[i]);
4200 }
4201 } else {
4202 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
4203 sb.append(",0");
4204 }
4205 }
4206 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
4207 cpuFreqTimeMs.length, sb.toString());
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004208 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -08004209
4210 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
4211 final long[] timesMs = u.getCpuFreqTimes(which, procState);
4212 if (timesMs != null && timesMs.length == cpuFreqs.length) {
4213 sb.setLength(0);
4214 for (int i = 0; i < timesMs.length; ++i) {
4215 sb.append((i == 0 ? "" : ",") + timesMs[i]);
4216 }
4217 final long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(
4218 which, procState);
4219 if (screenOffTimesMs != null) {
4220 for (int i = 0; i < screenOffTimesMs.length; ++i) {
4221 sb.append("," + screenOffTimesMs[i]);
4222 }
4223 } else {
4224 for (int i = 0; i < timesMs.length; ++i) {
4225 sb.append(",0");
4226 }
4227 }
4228 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA,
4229 Uid.UID_PROCESS_TYPES[procState], timesMs.length, sb.toString());
4230 }
4231 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004232 }
4233
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004234 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4235 = u.getProcessStats();
4236 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4237 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004238
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004239 final long userMillis = ps.getUserTime(which);
4240 final long systemMillis = ps.getSystemTime(which);
4241 final long foregroundMillis = ps.getForegroundTime(which);
4242 final int starts = ps.getStarts(which);
4243 final int numCrashes = ps.getNumCrashes(which);
4244 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004245
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004246 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
4247 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004248 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
4249 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004250 }
4251 }
4252
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004253 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4254 = u.getPackageStats();
4255 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4256 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4257 int wakeups = 0;
4258 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4259 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07004260 int count = alarms.valueAt(iwa).getCountLocked(which);
4261 wakeups += count;
4262 String name = alarms.keyAt(iwa).replace(',', '_');
4263 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004264 }
4265 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4266 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4267 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4268 final long startTime = ss.getStartTime(batteryUptime, which);
4269 final int starts = ss.getStarts(which);
4270 final int launches = ss.getLaunches(which);
4271 if (startTime != 0 || starts != 0 || launches != 0) {
4272 dumpLine(pw, uid, category, APK_DATA,
4273 wakeups, // wakeup alarms
4274 packageStats.keyAt(ipkg), // Apk
4275 serviceStats.keyAt(isvc), // service
4276 startTime / 1000, // time spent started, in ms
4277 starts,
4278 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004279 }
4280 }
4281 }
4282 }
4283 }
4284
Dianne Hackborn81038902012-11-26 17:04:09 -08004285 static final class TimerEntry {
4286 final String mName;
4287 final int mId;
4288 final BatteryStats.Timer mTimer;
4289 final long mTime;
4290 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
4291 mName = name;
4292 mId = id;
4293 mTimer = timer;
4294 mTime = time;
4295 }
4296 }
4297
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004298 private void printmAh(PrintWriter printer, double power) {
4299 printer.print(BatteryStatsHelper.makemAh(power));
4300 }
4301
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004302 private void printmAh(StringBuilder sb, double power) {
4303 sb.append(BatteryStatsHelper.makemAh(power));
4304 }
4305
Dianne Hackbornd953c532014-08-16 18:17:38 -07004306 /**
4307 * Temporary for settings.
4308 */
4309 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
4310 int reqUid) {
4311 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
4312 }
4313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004314 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004315 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07004316 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004317 final long rawUptime = SystemClock.uptimeMillis() * 1000;
4318 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07004319 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004320 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321
4322 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
4323 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
4324 final long totalRealtime = computeRealtime(rawRealtime, which);
4325 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004326 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
4327 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
4328 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004329 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
4330 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -07004331 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004332
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004333 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07004334
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004335 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07004336 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004337
Adam Lesinskif9b20a92016-06-17 17:30:01 -07004338 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
4339 if (estimatedBatteryCapacity > 0) {
4340 sb.setLength(0);
4341 sb.append(prefix);
4342 sb.append(" Estimated battery capacity: ");
4343 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
4344 sb.append(" mAh");
4345 pw.println(sb.toString());
4346 }
4347
Jocelyn Dangc627d102017-04-14 13:15:14 -07004348 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
4349 if (minLearnedBatteryCapacity > 0) {
4350 sb.setLength(0);
4351 sb.append(prefix);
4352 sb.append(" Min learned battery capacity: ");
4353 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
4354 sb.append(" mAh");
4355 pw.println(sb.toString());
4356 }
4357 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
4358 if (maxLearnedBatteryCapacity > 0) {
4359 sb.setLength(0);
4360 sb.append(prefix);
4361 sb.append(" Max learned battery capacity: ");
4362 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
4363 sb.append(" mAh");
4364 pw.println(sb.toString());
4365 }
4366
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004367 sb.setLength(0);
4368 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004369 sb.append(" Time on battery: ");
4370 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
4371 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
4372 sb.append(") realtime, ");
4373 formatTimeMs(sb, whichBatteryUptime / 1000);
4374 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, whichBatteryRealtime));
4375 sb.append(") uptime");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004376 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004377
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004378 sb.setLength(0);
4379 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004380 sb.append(" Time on battery screen off: ");
4381 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
4382 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, whichBatteryRealtime));
4383 sb.append(") realtime, ");
4384 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
4385 sb.append("(");
4386 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, whichBatteryRealtime));
4387 sb.append(") uptime");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004388 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004389
4390 sb.setLength(0);
4391 sb.append(prefix);
4392 sb.append(" Time on battery screen doze: ");
4393 formatTimeMs(sb, screenDozeTime / 1000); sb.append("(");
4394 sb.append(formatRatioLocked(screenDozeTime, whichBatteryRealtime));
4395 sb.append(")");
4396 pw.println(sb.toString());
4397
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004398 sb.setLength(0);
4399 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004400 sb.append(" Total run time: ");
4401 formatTimeMs(sb, totalRealtime / 1000);
4402 sb.append("realtime, ");
4403 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004404 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07004405 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004406 if (batteryTimeRemaining >= 0) {
4407 sb.setLength(0);
4408 sb.append(prefix);
4409 sb.append(" Battery time remaining: ");
4410 formatTimeMs(sb, batteryTimeRemaining / 1000);
4411 pw.println(sb.toString());
4412 }
4413 if (chargeTimeRemaining >= 0) {
4414 sb.setLength(0);
4415 sb.append(prefix);
4416 sb.append(" Charge time remaining: ");
4417 formatTimeMs(sb, chargeTimeRemaining / 1000);
4418 pw.println(sb.toString());
4419 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004420
Kweku Adams87b19ec2017-10-09 12:40:03 -07004421 final long dischargeCount = getUahDischarge(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004422 if (dischargeCount >= 0) {
4423 sb.setLength(0);
4424 sb.append(prefix);
4425 sb.append(" Discharge: ");
4426 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
4427 sb.append(" mAh");
4428 pw.println(sb.toString());
4429 }
4430
Kweku Adams87b19ec2017-10-09 12:40:03 -07004431 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004432 if (dischargeScreenOffCount >= 0) {
4433 sb.setLength(0);
4434 sb.append(prefix);
4435 sb.append(" Screen off discharge: ");
4436 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
4437 sb.append(" mAh");
4438 pw.println(sb.toString());
4439 }
4440
Kweku Adams87b19ec2017-10-09 12:40:03 -07004441 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Mac2f518a2017-09-19 16:06:03 -07004442 if (dischargeScreenDozeCount >= 0) {
4443 sb.setLength(0);
4444 sb.append(prefix);
4445 sb.append(" Screen doze discharge: ");
4446 sb.append(BatteryStatsHelper.makemAh(dischargeScreenDozeCount / 1000.0));
4447 sb.append(" mAh");
4448 pw.println(sb.toString());
4449 }
4450
Mike Ma90902652018-04-17 14:07:24 -07004451 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004452 if (dischargeScreenOnCount >= 0) {
4453 sb.setLength(0);
4454 sb.append(prefix);
4455 sb.append(" Screen on discharge: ");
4456 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
4457 sb.append(" mAh");
4458 pw.println(sb.toString());
4459 }
4460
Mike Ma15313c92017-11-15 17:58:21 -08004461 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
4462 if (dischargeLightDozeCount >= 0) {
4463 sb.setLength(0);
4464 sb.append(prefix);
4465 sb.append(" Device light doze discharge: ");
4466 sb.append(BatteryStatsHelper.makemAh(dischargeLightDozeCount / 1000.0));
4467 sb.append(" mAh");
4468 pw.println(sb.toString());
4469 }
4470
4471 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
4472 if (dischargeDeepDozeCount >= 0) {
4473 sb.setLength(0);
4474 sb.append(prefix);
4475 sb.append(" Device deep doze discharge: ");
4476 sb.append(BatteryStatsHelper.makemAh(dischargeDeepDozeCount / 1000.0));
4477 sb.append(" mAh");
4478 pw.println(sb.toString());
4479 }
4480
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004481 pw.print(" Start clock time: ");
4482 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
4483
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004484 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004485 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004486 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004487 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
4488 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004489 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004490 rawRealtime, which);
4491 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
4492 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004493 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004494 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004495 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
4496 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
4497 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004498 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004499 sb.append(prefix);
4500 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
4501 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004502 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004503 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
4504 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004505 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004506 pw.println(sb.toString());
4507 sb.setLength(0);
4508 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004509 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004510 boolean didOne = false;
4511 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004512 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004513 if (time == 0) {
4514 continue;
4515 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004516 sb.append("\n ");
4517 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004518 didOne = true;
4519 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
4520 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004521 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004522 sb.append("(");
4523 sb.append(formatRatioLocked(time, screenOnTime));
4524 sb.append(")");
4525 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004526 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004527 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004528 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004529 sb.setLength(0);
4530 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004531 sb.append(" Power save mode enabled: ");
4532 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004533 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004534 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004535 sb.append(")");
4536 pw.println(sb.toString());
4537 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004538 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004539 sb.setLength(0);
4540 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004541 sb.append(" Device light idling: ");
4542 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004543 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004544 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
4545 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004546 sb.append("x");
4547 pw.println(sb.toString());
4548 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004549 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004550 sb.setLength(0);
4551 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004552 sb.append(" Idle mode light time: ");
4553 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004554 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004555 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
4556 sb.append(") ");
4557 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004558 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004559 sb.append(" -- longest ");
4560 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
4561 pw.println(sb.toString());
4562 }
4563 if (deviceIdlingTime != 0) {
4564 sb.setLength(0);
4565 sb.append(prefix);
4566 sb.append(" Device full idling: ");
4567 formatTimeMs(sb, deviceIdlingTime / 1000);
4568 sb.append("(");
4569 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004570 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004571 sb.append("x");
4572 pw.println(sb.toString());
4573 }
4574 if (deviceIdleModeFullTime != 0) {
4575 sb.setLength(0);
4576 sb.append(prefix);
4577 sb.append(" Idle mode full time: ");
4578 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
4579 sb.append("(");
4580 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
4581 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004582 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004583 sb.append("x");
4584 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004585 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004586 pw.println(sb.toString());
4587 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004588 if (phoneOnTime != 0) {
4589 sb.setLength(0);
4590 sb.append(prefix);
4591 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
4592 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004593 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004594 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004595 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004596 if (connChanges != 0) {
4597 pw.print(prefix);
4598 pw.print(" Connectivity changes: "); pw.println(connChanges);
4599 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004600
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004601 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07004602 long fullWakeLockTimeTotalMicros = 0;
4603 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08004604
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004605 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08004606
Evan Millar22ac0432009-03-31 11:33:18 -07004607 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004608 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004609
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004610 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4611 = u.getWakelockStats();
4612 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4613 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07004614
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004615 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
4616 if (fullWakeTimer != null) {
4617 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
4618 rawRealtime, which);
4619 }
4620
4621 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4622 if (partialWakeTimer != null) {
4623 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
4624 rawRealtime, which);
4625 if (totalTimeMicros > 0) {
4626 if (reqUid < 0) {
4627 // Only show the ordered list of all wake
4628 // locks if the caller is not asking for data
4629 // about a specific uid.
4630 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
4631 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08004632 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004633 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07004634 }
4635 }
4636 }
4637 }
Bookatzc8c44962017-05-11 12:12:54 -07004638
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004639 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4640 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4641 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4642 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4643 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4644 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4645 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4646 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004647 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4648 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004649
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004650 if (fullWakeLockTimeTotalMicros != 0) {
4651 sb.setLength(0);
4652 sb.append(prefix);
4653 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4654 (fullWakeLockTimeTotalMicros + 500) / 1000);
4655 pw.println(sb.toString());
4656 }
4657
4658 if (partialWakeLockTimeTotalMicros != 0) {
4659 sb.setLength(0);
4660 sb.append(prefix);
4661 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4662 (partialWakeLockTimeTotalMicros + 500) / 1000);
4663 pw.println(sb.toString());
4664 }
4665
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004666 final long multicastWakeLockTimeTotalMicros =
4667 getWifiMulticastWakelockTime(rawRealtime, which);
4668 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004669 if (multicastWakeLockTimeTotalMicros != 0) {
4670 sb.setLength(0);
4671 sb.append(prefix);
4672 sb.append(" Total WiFi Multicast wakelock Count: ");
4673 sb.append(multicastWakeLockCountTotal);
4674 pw.println(sb.toString());
4675
4676 sb.setLength(0);
4677 sb.append(prefix);
4678 sb.append(" Total WiFi Multicast wakelock time: ");
4679 formatTimeMsNoSpace(sb, (multicastWakeLockTimeTotalMicros + 500) / 1000);
4680 pw.println(sb.toString());
4681 }
4682
Siddharth Ray3c648c42017-10-02 17:30:58 -07004683 pw.println("");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004684 pw.print(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004685 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004686 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004687 sb.append(" CONNECTIVITY POWER SUMMARY START");
4688 pw.println(sb.toString());
4689
4690 pw.print(prefix);
4691 sb.setLength(0);
4692 sb.append(prefix);
4693 sb.append(" Logging duration for connectivity statistics: ");
4694 formatTimeMs(sb, whichBatteryRealtime / 1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004695 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004696
4697 sb.setLength(0);
4698 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004699 sb.append(" Cellular Statistics:");
Amith Yamasanif37447b2009-10-08 18:28:01 -07004700 pw.println(sb.toString());
4701
Siddharth Ray3c648c42017-10-02 17:30:58 -07004702 pw.print(prefix);
4703 sb.setLength(0);
4704 sb.append(prefix);
4705 sb.append(" Cellular kernel active time: ");
4706 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
4707 formatTimeMs(sb, mobileActiveTime / 1000);
4708 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4709 sb.append(")");
4710 pw.println(sb.toString());
4711
4712 pw.print(" Cellular data received: "); pw.println(formatBytesLocked(mobileRxTotalBytes));
4713 pw.print(" Cellular data sent: "); pw.println(formatBytesLocked(mobileTxTotalBytes));
4714 pw.print(" Cellular packets received: "); pw.println(mobileRxTotalPackets);
4715 pw.print(" Cellular packets sent: "); pw.println(mobileTxTotalPackets);
4716
Dianne Hackborn627bba72009-03-24 22:32:56 -07004717 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004718 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004719 sb.append(" Cellular Radio Access Technology:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004720 didOne = false;
4721 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004722 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004723 if (time == 0) {
4724 continue;
4725 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004726 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004727 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004728 didOne = true;
4729 sb.append(DATA_CONNECTION_NAMES[i]);
4730 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004731 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004732 sb.append("(");
4733 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004734 sb.append(") ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004735 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004736 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004737 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004738
4739 sb.setLength(0);
4740 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004741 sb.append(" Cellular Rx signal strength (RSRP):");
4742 final String[] cellularRxSignalStrengthDescription = new String[]{
4743 "very poor (less than -128dBm): ",
4744 "poor (-128dBm to -118dBm): ",
4745 "moderate (-118dBm to -108dBm): ",
4746 "good (-108dBm to -98dBm): ",
4747 "great (greater than -98dBm): "};
4748 didOne = false;
4749 final int numCellularRxBins = Math.min(SignalStrength.NUM_SIGNAL_STRENGTH_BINS,
4750 cellularRxSignalStrengthDescription.length);
4751 for (int i=0; i<numCellularRxBins; i++) {
4752 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
4753 if (time == 0) {
4754 continue;
4755 }
4756 sb.append("\n ");
4757 sb.append(prefix);
4758 didOne = true;
4759 sb.append(cellularRxSignalStrengthDescription[i]);
4760 sb.append(" ");
4761 formatTimeMs(sb, time/1000);
4762 sb.append("(");
4763 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4764 sb.append(") ");
4765 }
4766 if (!didOne) sb.append(" (no activity)");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004767 pw.println(sb.toString());
4768
Siddharth Rayb50a6842017-12-14 15:15:28 -08004769 printControllerActivity(pw, sb, prefix, CELLULAR_CONTROLLER_NAME,
Siddharth Ray3c648c42017-10-02 17:30:58 -07004770 getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004771
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004772 pw.print(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004773 sb.setLength(0);
4774 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004775 sb.append(" Wifi Statistics:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004776 pw.println(sb.toString());
4777
Siddharth Rayb50a6842017-12-14 15:15:28 -08004778 pw.print(prefix);
4779 sb.setLength(0);
4780 sb.append(prefix);
4781 sb.append(" Wifi kernel active time: ");
4782 final long wifiActiveTime = getWifiActiveTime(rawRealtime, which);
4783 formatTimeMs(sb, wifiActiveTime / 1000);
4784 sb.append("("); sb.append(formatRatioLocked(wifiActiveTime, whichBatteryRealtime));
4785 sb.append(")");
4786 pw.println(sb.toString());
4787
Siddharth Ray3c648c42017-10-02 17:30:58 -07004788 pw.print(" Wifi data received: "); pw.println(formatBytesLocked(wifiRxTotalBytes));
4789 pw.print(" Wifi data sent: "); pw.println(formatBytesLocked(wifiTxTotalBytes));
4790 pw.print(" Wifi packets received: "); pw.println(wifiRxTotalPackets);
4791 pw.print(" Wifi packets sent: "); pw.println(wifiTxTotalPackets);
4792
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004793 sb.setLength(0);
4794 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004795 sb.append(" Wifi states:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004796 didOne = false;
4797 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004798 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004799 if (time == 0) {
4800 continue;
4801 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004802 sb.append("\n ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004803 didOne = true;
4804 sb.append(WIFI_STATE_NAMES[i]);
4805 sb.append(" ");
4806 formatTimeMs(sb, time/1000);
4807 sb.append("(");
4808 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4809 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004810 }
4811 if (!didOne) sb.append(" (no activity)");
4812 pw.println(sb.toString());
4813
4814 sb.setLength(0);
4815 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004816 sb.append(" Wifi supplicant states:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004817 didOne = false;
4818 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4819 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4820 if (time == 0) {
4821 continue;
4822 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004823 sb.append("\n ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004824 didOne = true;
4825 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4826 sb.append(" ");
4827 formatTimeMs(sb, time/1000);
4828 sb.append("(");
4829 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4830 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004831 }
4832 if (!didOne) sb.append(" (no activity)");
4833 pw.println(sb.toString());
4834
4835 sb.setLength(0);
4836 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004837 sb.append(" Wifi Rx signal strength (RSSI):");
4838 final String[] wifiRxSignalStrengthDescription = new String[]{
4839 "very poor (less than -88.75dBm): ",
4840 "poor (-88.75 to -77.5dBm): ",
4841 "moderate (-77.5dBm to -66.25dBm): ",
4842 "good (-66.25dBm to -55dBm): ",
4843 "great (greater than -55dBm): "};
Dianne Hackborn3251b902014-06-20 14:40:53 -07004844 didOne = false;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004845 final int numWifiRxBins = Math.min(NUM_WIFI_SIGNAL_STRENGTH_BINS,
4846 wifiRxSignalStrengthDescription.length);
4847 for (int i=0; i<numWifiRxBins; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004848 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4849 if (time == 0) {
4850 continue;
4851 }
4852 sb.append("\n ");
4853 sb.append(prefix);
4854 didOne = true;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004855 sb.append(" ");
4856 sb.append(wifiRxSignalStrengthDescription[i]);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004857 formatTimeMs(sb, time/1000);
4858 sb.append("(");
4859 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4860 sb.append(") ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004861 }
4862 if (!didOne) sb.append(" (no activity)");
4863 pw.println(sb.toString());
4864
Siddharth Rayb50a6842017-12-14 15:15:28 -08004865 printControllerActivity(pw, sb, prefix, WIFI_CONTROLLER_NAME,
4866 getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004867
Adam Lesinski50e47602015-12-04 17:04:54 -08004868 pw.print(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004869 sb.setLength(0);
4870 sb.append(prefix);
Siddharth Ray78ccaf52017-12-23 16:16:21 -08004871 sb.append(" GPS Statistics:");
4872 pw.println(sb.toString());
4873
4874 sb.setLength(0);
4875 sb.append(prefix);
4876 sb.append(" GPS signal quality (Top 4 Average CN0):");
4877 final String[] gpsSignalQualityDescription = new String[]{
4878 "poor (less than 20 dBHz): ",
4879 "good (greater than 20 dBHz): "};
4880 final int numGpsSignalQualityBins = Math.min(GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS,
4881 gpsSignalQualityDescription.length);
4882 for (int i=0; i<numGpsSignalQualityBins; i++) {
4883 final long time = getGpsSignalQualityTime(i, rawRealtime, which);
4884 sb.append("\n ");
4885 sb.append(prefix);
4886 sb.append(" ");
4887 sb.append(gpsSignalQualityDescription[i]);
4888 formatTimeMs(sb, time/1000);
4889 sb.append("(");
4890 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4891 sb.append(") ");
4892 }
4893 pw.println(sb.toString());
4894
4895 final long gpsBatteryDrainMaMs = getGpsBatteryDrainMaMs();
4896 if (gpsBatteryDrainMaMs > 0) {
4897 pw.print(prefix);
4898 sb.setLength(0);
4899 sb.append(prefix);
4900 sb.append(" Battery Drain (mAh): ");
4901 sb.append(Double.toString(((double) gpsBatteryDrainMaMs)/(3600 * 1000)));
4902 pw.println(sb.toString());
4903 }
4904
4905 pw.print(prefix);
4906 sb.setLength(0);
4907 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004908 sb.append(" CONNECTIVITY POWER SUMMARY END");
4909 pw.println(sb.toString());
4910 pw.println("");
4911
4912 pw.print(prefix);
Adam Lesinski50e47602015-12-04 17:04:54 -08004913 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4914 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4915
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004916 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4917 sb.setLength(0);
4918 sb.append(prefix);
4919 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4920 pw.println(sb.toString());
4921
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004922 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4923 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004924
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004925 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004926
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004927 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004928 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004929 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004930 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004931 pw.println(getDischargeStartLevel());
4932 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4933 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004934 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004935 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004936 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004937 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004938 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004939 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004940 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004941 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004942 pw.println(getDischargeAmountScreenOn());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004943 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004944 pw.println(getDischargeAmountScreenOff());
4945 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4946 pw.println(getDischargeAmountScreenDoze());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004947 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004948 } else {
4949 pw.print(prefix); pw.println(" Device battery use since last full charge");
4950 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004951 pw.println(getLowDischargeAmountSinceCharge());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004952 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004953 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004954 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004955 pw.println(getDischargeAmountScreenOnSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004956 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004957 pw.println(getDischargeAmountScreenOffSinceCharge());
4958 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4959 pw.println(getDischargeAmountScreenDozeSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004960 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004961 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004962
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004963 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004964 helper.create(this);
4965 helper.refreshStats(which, UserHandle.USER_ALL);
4966 List<BatterySipper> sippers = helper.getUsageList();
4967 if (sippers != null && sippers.size() > 0) {
4968 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4969 pw.print(prefix); pw.print(" Capacity: ");
4970 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004971 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004972 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4973 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4974 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4975 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004976 pw.println();
4977 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004978 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004979 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004980 switch (bs.drainType) {
Kweku Adamsef728952018-04-10 17:56:16 -07004981 case AMBIENT_DISPLAY:
4982 pw.print(" Ambient display: ");
4983 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004984 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004985 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004986 break;
4987 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004988 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004989 break;
4990 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004991 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004992 break;
4993 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004994 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004995 break;
4996 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004997 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004998 break;
4999 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005000 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005001 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005002 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005003 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005004 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005005 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005006 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005007 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005008 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005009 break;
5010 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005011 pw.print(" User "); pw.print(bs.userId);
5012 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005013 break;
5014 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005015 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005016 break;
5017 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005018 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005019 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005020 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005021 pw.print(" Camera: ");
5022 break;
5023 default:
5024 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005025 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005026 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005027 printmAh(pw, bs.totalPowerMah);
5028
Adam Lesinski57123002015-06-12 16:12:07 -07005029 if (bs.usagePowerMah != bs.totalPowerMah) {
5030 // If the usage (generic power) isn't the whole amount, we list out
5031 // what components are involved in the calculation.
5032
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005033 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07005034 if (bs.usagePowerMah != 0) {
5035 pw.print(" usage=");
5036 printmAh(pw, bs.usagePowerMah);
5037 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005038 if (bs.cpuPowerMah != 0) {
5039 pw.print(" cpu=");
5040 printmAh(pw, bs.cpuPowerMah);
5041 }
5042 if (bs.wakeLockPowerMah != 0) {
5043 pw.print(" wake=");
5044 printmAh(pw, bs.wakeLockPowerMah);
5045 }
5046 if (bs.mobileRadioPowerMah != 0) {
5047 pw.print(" radio=");
5048 printmAh(pw, bs.mobileRadioPowerMah);
5049 }
5050 if (bs.wifiPowerMah != 0) {
5051 pw.print(" wifi=");
5052 printmAh(pw, bs.wifiPowerMah);
5053 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005054 if (bs.bluetoothPowerMah != 0) {
5055 pw.print(" bt=");
5056 printmAh(pw, bs.bluetoothPowerMah);
5057 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005058 if (bs.gpsPowerMah != 0) {
5059 pw.print(" gps=");
5060 printmAh(pw, bs.gpsPowerMah);
5061 }
5062 if (bs.sensorPowerMah != 0) {
5063 pw.print(" sensor=");
5064 printmAh(pw, bs.sensorPowerMah);
5065 }
5066 if (bs.cameraPowerMah != 0) {
5067 pw.print(" camera=");
5068 printmAh(pw, bs.cameraPowerMah);
5069 }
5070 if (bs.flashlightPowerMah != 0) {
5071 pw.print(" flash=");
5072 printmAh(pw, bs.flashlightPowerMah);
5073 }
5074 pw.print(" )");
5075 }
Bookatz17d7d9d2017-06-08 14:50:46 -07005076
5077 // If there is additional smearing information, include it.
5078 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
5079 pw.print(" Including smearing: ");
5080 printmAh(pw, bs.totalSmearedPowerMah);
5081 pw.print(" (");
5082 if (bs.screenPowerMah != 0) {
5083 pw.print(" screen=");
5084 printmAh(pw, bs.screenPowerMah);
5085 }
5086 if (bs.proportionalSmearMah != 0) {
5087 pw.print(" proportional=");
5088 printmAh(pw, bs.proportionalSmearMah);
5089 }
5090 pw.print(" )");
5091 }
5092 if (bs.shouldHide) {
5093 pw.print(" Excluded from smearing");
5094 }
5095
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005096 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005097 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08005098 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005099 }
5100
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005101 sippers = helper.getMobilemsppList();
5102 if (sippers != null && sippers.size() > 0) {
5103 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005104 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005105 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005106 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005107 sb.setLength(0);
5108 sb.append(prefix); sb.append(" Uid ");
5109 UserHandle.formatUid(sb, bs.uidObj.getUid());
5110 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
5111 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
5112 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005113 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005114 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005115 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005116 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005117 sb.setLength(0);
5118 sb.append(prefix);
5119 sb.append(" TOTAL TIME: ");
5120 formatTimeMs(sb, totalTime);
5121 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
5122 sb.append(")");
5123 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005124 pw.println();
5125 }
5126
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005127 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
5128 @Override
5129 public int compare(TimerEntry lhs, TimerEntry rhs) {
5130 long lhsTime = lhs.mTime;
5131 long rhsTime = rhs.mTime;
5132 if (lhsTime < rhsTime) {
5133 return 1;
5134 }
5135 if (lhsTime > rhsTime) {
5136 return -1;
5137 }
5138 return 0;
5139 }
5140 };
5141
5142 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005143 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
5144 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005145 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005146 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
5147 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
5148 : kernelWakelocks.entrySet()) {
5149 final BatteryStats.Timer timer = ent.getValue();
5150 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005151 if (totalTimeMillis > 0) {
5152 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
5153 }
5154 }
5155 if (ktimers.size() > 0) {
5156 Collections.sort(ktimers, timerComparator);
5157 pw.print(prefix); pw.println(" All kernel wake locks:");
5158 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005159 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005160 String linePrefix = ": ";
5161 sb.setLength(0);
5162 sb.append(prefix);
5163 sb.append(" Kernel Wake lock ");
5164 sb.append(timer.mName);
5165 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
5166 which, linePrefix);
5167 if (!linePrefix.equals(": ")) {
5168 sb.append(" realtime");
5169 // Only print out wake locks that were held
5170 pw.println(sb.toString());
5171 }
5172 }
5173 pw.println();
5174 }
5175 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005176
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005177 if (timers.size() > 0) {
5178 Collections.sort(timers, timerComparator);
5179 pw.print(prefix); pw.println(" All partial wake locks:");
5180 for (int i=0; i<timers.size(); i++) {
5181 TimerEntry timer = timers.get(i);
5182 sb.setLength(0);
5183 sb.append(" Wake lock ");
5184 UserHandle.formatUid(sb, timer.mId);
5185 sb.append(" ");
5186 sb.append(timer.mName);
5187 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5188 sb.append(" realtime");
5189 pw.println(sb.toString());
5190 }
5191 timers.clear();
5192 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08005193 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005194
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005195 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005196 if (wakeupReasons.size() > 0) {
5197 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005198 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005199 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005200 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005201 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
5202 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005203 }
5204 Collections.sort(reasons, timerComparator);
5205 for (int i=0; i<reasons.size(); i++) {
5206 TimerEntry timer = reasons.get(i);
5207 String linePrefix = ": ";
5208 sb.setLength(0);
5209 sb.append(prefix);
5210 sb.append(" Wakeup reason ");
5211 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005212 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5213 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005214 pw.println(sb.toString());
5215 }
5216 pw.println();
5217 }
Dianne Hackborn81038902012-11-26 17:04:09 -08005218 }
Evan Millar22ac0432009-03-31 11:33:18 -07005219
James Carr2dd7e5e2016-07-20 18:48:39 -07005220 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
Bookatz50df7112017-08-04 14:53:26 -07005221 if (mMemoryStats.size() > 0) {
5222 pw.println(" Memory Stats");
5223 for (int i = 0; i < mMemoryStats.size(); i++) {
5224 sb.setLength(0);
5225 sb.append(" Bandwidth ");
5226 sb.append(mMemoryStats.keyAt(i));
5227 sb.append(" Time ");
5228 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
5229 pw.println(sb.toString());
5230 }
5231 pw.println();
5232 }
5233
5234 final Map<String, ? extends Timer> rpmStats = getRpmStats();
5235 if (rpmStats.size() > 0) {
5236 pw.print(prefix); pw.println(" Resource Power Manager Stats");
5237 if (rpmStats.size() > 0) {
5238 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
5239 final String timerName = ent.getKey();
5240 final Timer timer = ent.getValue();
5241 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5242 }
5243 }
5244 pw.println();
5245 }
Bookatz82b341172017-09-07 19:06:08 -07005246 if (SCREEN_OFF_RPM_STATS_ENABLED) {
5247 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
Bookatz50df7112017-08-04 14:53:26 -07005248 if (screenOffRpmStats.size() > 0) {
Bookatz82b341172017-09-07 19:06:08 -07005249 pw.print(prefix);
5250 pw.println(" Resource Power Manager Stats for when screen was off");
5251 if (screenOffRpmStats.size() > 0) {
5252 for (Map.Entry<String, ? extends Timer> ent : screenOffRpmStats.entrySet()) {
5253 final String timerName = ent.getKey();
5254 final Timer timer = ent.getValue();
5255 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5256 }
Bookatz50df7112017-08-04 14:53:26 -07005257 }
Bookatz82b341172017-09-07 19:06:08 -07005258 pw.println();
Bookatz50df7112017-08-04 14:53:26 -07005259 }
James Carr2dd7e5e2016-07-20 18:48:39 -07005260 }
5261
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005262 final long[] cpuFreqs = getCpuFreqs();
5263 if (cpuFreqs != null) {
5264 sb.setLength(0);
Bookatz50df7112017-08-04 14:53:26 -07005265 sb.append(" CPU freqs:");
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005266 for (int i = 0; i < cpuFreqs.length; ++i) {
5267 sb.append(" " + cpuFreqs[i]);
5268 }
5269 pw.println(sb.toString());
Bookatz50df7112017-08-04 14:53:26 -07005270 pw.println();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005271 }
5272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005273 for (int iu=0; iu<NU; iu++) {
5274 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005275 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005276 continue;
5277 }
Bookatzc8c44962017-05-11 12:12:54 -07005278
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005279 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07005280
5281 pw.print(prefix);
5282 pw.print(" ");
5283 UserHandle.formatUid(pw, uid);
5284 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005285 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005286
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005287 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
5288 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
5289 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
5290 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005291 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
5292 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
5293
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005294 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
5295 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005296 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
5297 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005298
5299 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
5300 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
5301
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005302 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
5303 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
5304 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08005305 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
5306 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5307 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
5308 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005309 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005310
Adam Lesinski5f056f62016-07-14 16:56:08 -07005311 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
5312 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
5313
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005314 if (mobileRxBytes > 0 || mobileTxBytes > 0
5315 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005316 pw.print(prefix); pw.print(" Mobile network: ");
5317 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005318 pw.print(formatBytesLocked(mobileTxBytes));
5319 pw.print(" sent (packets "); pw.print(mobileRxPackets);
5320 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005321 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005322 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
5323 sb.setLength(0);
5324 sb.append(prefix); sb.append(" Mobile radio active: ");
5325 formatTimeMs(sb, uidMobileActiveTime / 1000);
5326 sb.append("(");
5327 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
5328 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
5329 long packets = mobileRxPackets + mobileTxPackets;
5330 if (packets == 0) {
5331 packets = 1;
5332 }
5333 sb.append(" @ ");
5334 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
5335 sb.append(" mspp");
5336 pw.println(sb.toString());
5337 }
5338
Adam Lesinski5f056f62016-07-14 16:56:08 -07005339 if (mobileWakeup > 0) {
5340 sb.setLength(0);
5341 sb.append(prefix);
5342 sb.append(" Mobile radio AP wakeups: ");
5343 sb.append(mobileWakeup);
5344 pw.println(sb.toString());
5345 }
5346
Siddharth Rayb50a6842017-12-14 15:15:28 -08005347 printControllerActivityIfInteresting(pw, sb, prefix + " ",
5348 CELLULAR_CONTROLLER_NAME, u.getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005349
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005350 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005351 pw.print(prefix); pw.print(" Wi-Fi network: ");
5352 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005353 pw.print(formatBytesLocked(wifiTxBytes));
5354 pw.print(" sent (packets "); pw.print(wifiRxPackets);
5355 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005356 }
5357
Dianne Hackborn62793e42015-03-09 11:15:41 -07005358 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08005359 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005360 || uidWifiRunningTime != 0) {
5361 sb.setLength(0);
5362 sb.append(prefix); sb.append(" Wifi Running: ");
5363 formatTimeMs(sb, uidWifiRunningTime / 1000);
5364 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
5365 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07005366 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005367 formatTimeMs(sb, fullWifiLockOnTime / 1000);
5368 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
5369 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08005370 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005371 formatTimeMs(sb, wifiScanTime / 1000);
5372 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07005373 whichBatteryRealtime)); sb.append(") ");
5374 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08005375 sb.append("x\n");
5376 // actual and background times are unpooled and since reset (regardless of 'which')
5377 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
5378 formatTimeMs(sb, wifiScanActualTime / 1000);
5379 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
5380 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5381 sb.append(") ");
5382 sb.append(wifiScanCount);
5383 sb.append("x\n");
5384 sb.append(prefix); sb.append(" Background Wifi Scan: ");
5385 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
5386 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
5387 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5388 sb.append(") ");
5389 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07005390 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005391 pw.println(sb.toString());
5392 }
5393
Adam Lesinski5f056f62016-07-14 16:56:08 -07005394 if (wifiWakeup > 0) {
5395 sb.setLength(0);
5396 sb.append(prefix);
5397 sb.append(" WiFi AP wakeups: ");
5398 sb.append(wifiWakeup);
5399 pw.println(sb.toString());
5400 }
5401
Siddharth Rayb50a6842017-12-14 15:15:28 -08005402 printControllerActivityIfInteresting(pw, sb, prefix + " ", WIFI_CONTROLLER_NAME,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005403 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07005404
Adam Lesinski50e47602015-12-04 17:04:54 -08005405 if (btRxBytes > 0 || btTxBytes > 0) {
5406 pw.print(prefix); pw.print(" Bluetooth network: ");
5407 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
5408 pw.print(formatBytesLocked(btTxBytes));
5409 pw.println(" sent");
5410 }
5411
Bookatz867c0d72017-03-07 18:23:42 -08005412 final Timer bleTimer = u.getBluetoothScanTimer();
5413 if (bleTimer != null) {
5414 // Convert from microseconds to milliseconds with rounding
5415 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
5416 / 1000;
5417 if (totalTimeMs != 0) {
5418 final int count = bleTimer.getCountLocked(which);
5419 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
5420 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005421 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5422 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
5423 final long actualTimeMsBg = bleTimerBg != null ?
5424 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005425 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07005426 final int resultCount = u.getBluetoothScanResultCounter() != null ?
5427 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005428 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
5429 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
5430 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
5431 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
5432 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
5433 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5434 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
5435 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
5436 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
5437 final Timer unoptimizedScanTimerBg =
5438 u.getBluetoothUnoptimizedScanBackgroundTimer();
5439 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
5440 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5441 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
5442 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005443
5444 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08005445 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07005446 sb.append(prefix);
5447 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08005448 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07005449 sb.append(" (");
5450 sb.append(count);
5451 sb.append(" times)");
5452 if (bleTimer.isRunningLocked()) {
5453 sb.append(" (currently running)");
5454 }
5455 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005456 }
Bookatzb1f04f32017-05-19 13:57:32 -07005457
5458 sb.append(prefix);
5459 sb.append(" Bluetooth Scan (total actual realtime): ");
5460 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
5461 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005462 sb.append(count);
5463 sb.append(" times)");
5464 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07005465 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08005466 }
Bookatzb1f04f32017-05-19 13:57:32 -07005467 sb.append("\n");
5468 if (actualTimeMsBg > 0 || countBg > 0) {
5469 sb.append(prefix);
5470 sb.append(" Bluetooth Scan (background realtime): ");
5471 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
5472 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005473 sb.append(countBg);
5474 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07005475 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
5476 sb.append(" (currently running in background)");
5477 }
5478 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005479 }
Bookatzb1f04f32017-05-19 13:57:32 -07005480
5481 sb.append(prefix);
5482 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07005483 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07005484 sb.append(" (");
5485 sb.append(resultCountBg);
5486 sb.append(" in background)");
5487
5488 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
5489 sb.append("\n");
5490 sb.append(prefix);
5491 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
5492 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
5493 sb.append(" (max ");
5494 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
5495 sb.append(")");
5496 if (unoptimizedScanTimer != null
5497 && unoptimizedScanTimer.isRunningLocked()) {
5498 sb.append(" (currently running unoptimized)");
5499 }
5500 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
5501 sb.append("\n");
5502 sb.append(prefix);
5503 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
5504 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
5505 sb.append(" (max ");
5506 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
5507 sb.append(")");
5508 if (unoptimizedScanTimerBg.isRunningLocked()) {
5509 sb.append(" (currently running unoptimized in background)");
5510 }
5511 }
5512 }
Bookatz867c0d72017-03-07 18:23:42 -08005513 pw.println(sb.toString());
5514 uidActivity = true;
5515 }
5516 }
5517
5518
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005519
Dianne Hackborn617f8772009-03-31 15:04:46 -07005520 if (u.hasUserActivity()) {
5521 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07005522 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005523 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005524 if (val != 0) {
5525 if (!hasData) {
5526 sb.setLength(0);
5527 sb.append(" User activity: ");
5528 hasData = true;
5529 } else {
5530 sb.append(", ");
5531 }
5532 sb.append(val);
5533 sb.append(" ");
5534 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
5535 }
5536 }
5537 if (hasData) {
5538 pw.println(sb.toString());
5539 }
5540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005541
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005542 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
5543 = u.getWakelockStats();
5544 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005545 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005546 int countWakelock = 0;
5547 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
5548 final Uid.Wakelock wl = wakelocks.valueAt(iw);
5549 String linePrefix = ": ";
5550 sb.setLength(0);
5551 sb.append(prefix);
5552 sb.append(" Wake lock ");
5553 sb.append(wakelocks.keyAt(iw));
5554 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
5555 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005556 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
5557 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005558 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005559 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
5560 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005561 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
5562 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005563 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
5564 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07005565 sb.append(" realtime");
5566 pw.println(sb.toString());
5567 uidActivity = true;
5568 countWakelock++;
5569
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005570 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
5571 rawRealtime, which);
5572 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
5573 rawRealtime, which);
5574 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
5575 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005576 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07005577 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005578 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005579 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07005580 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
5581 // pooled and therefore just a lower bound)
5582 long actualTotalPartialWakelock = 0;
5583 long actualBgPartialWakelock = 0;
5584 if (u.getAggregatedPartialWakelockTimer() != null) {
5585 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
5586 // Convert from microseconds to milliseconds with rounding
5587 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07005588 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07005589 final Timer bgAggTimer = aggTimer.getSubTimer();
5590 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005591 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07005592 }
5593
5594 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
5595 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
5596 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005597 sb.setLength(0);
5598 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005599 sb.append(" TOTAL wake: ");
5600 boolean needComma = false;
5601 if (totalFullWakelock != 0) {
5602 needComma = true;
5603 formatTimeMs(sb, totalFullWakelock);
5604 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005605 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005606 if (totalPartialWakelock != 0) {
5607 if (needComma) {
5608 sb.append(", ");
5609 }
5610 needComma = true;
5611 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07005612 sb.append("blamed partial");
5613 }
5614 if (actualTotalPartialWakelock != 0) {
5615 if (needComma) {
5616 sb.append(", ");
5617 }
5618 needComma = true;
5619 formatTimeMs(sb, actualTotalPartialWakelock);
5620 sb.append("actual partial");
5621 }
5622 if (actualBgPartialWakelock != 0) {
5623 if (needComma) {
5624 sb.append(", ");
5625 }
5626 needComma = true;
5627 formatTimeMs(sb, actualBgPartialWakelock);
5628 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005629 }
5630 if (totalWindowWakelock != 0) {
5631 if (needComma) {
5632 sb.append(", ");
5633 }
5634 needComma = true;
5635 formatTimeMs(sb, totalWindowWakelock);
5636 sb.append("window");
5637 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005638 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07005639 if (needComma) {
5640 sb.append(",");
5641 }
5642 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005643 formatTimeMs(sb, totalDrawWakelock);
5644 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07005645 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005646 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005647 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005648 }
5649 }
5650
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07005651 // Calculate multicast wakelock stats
5652 final Timer mcTimer = u.getMulticastWakelockStats();
5653 if (mcTimer != null) {
5654 final long multicastWakeLockTimeMicros = mcTimer.getTotalTimeLocked(rawRealtime, which);
5655 final int multicastWakeLockCount = mcTimer.getCountLocked(which);
5656
5657 if (multicastWakeLockTimeMicros > 0) {
5658 sb.setLength(0);
5659 sb.append(prefix);
5660 sb.append(" WiFi Multicast Wakelock");
5661 sb.append(" count = ");
5662 sb.append(multicastWakeLockCount);
5663 sb.append(" time = ");
5664 formatTimeMsNoSpace(sb, (multicastWakeLockTimeMicros + 500) / 1000);
5665 pw.println(sb.toString());
5666 }
5667 }
5668
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005669 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
5670 for (int isy=syncs.size()-1; isy>=0; isy--) {
5671 final Timer timer = syncs.valueAt(isy);
5672 // Convert from microseconds to milliseconds with rounding
5673 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5674 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07005675 final Timer bgTimer = timer.getSubTimer();
5676 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005677 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07005678 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005679 sb.setLength(0);
5680 sb.append(prefix);
5681 sb.append(" Sync ");
5682 sb.append(syncs.keyAt(isy));
5683 sb.append(": ");
5684 if (totalTime != 0) {
5685 formatTimeMs(sb, totalTime);
5686 sb.append("realtime (");
5687 sb.append(count);
5688 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07005689 if (bgTime > 0) {
5690 sb.append(", ");
5691 formatTimeMs(sb, bgTime);
5692 sb.append("background (");
5693 sb.append(bgCount);
5694 sb.append(" times)");
5695 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005696 } else {
5697 sb.append("(not used)");
5698 }
5699 pw.println(sb.toString());
5700 uidActivity = true;
5701 }
5702
5703 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
5704 for (int ij=jobs.size()-1; ij>=0; ij--) {
5705 final Timer timer = jobs.valueAt(ij);
5706 // Convert from microseconds to milliseconds with rounding
5707 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5708 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07005709 final Timer bgTimer = timer.getSubTimer();
5710 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005711 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07005712 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005713 sb.setLength(0);
5714 sb.append(prefix);
5715 sb.append(" Job ");
5716 sb.append(jobs.keyAt(ij));
5717 sb.append(": ");
5718 if (totalTime != 0) {
5719 formatTimeMs(sb, totalTime);
5720 sb.append("realtime (");
5721 sb.append(count);
5722 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07005723 if (bgTime > 0) {
5724 sb.append(", ");
5725 formatTimeMs(sb, bgTime);
5726 sb.append("background (");
5727 sb.append(bgCount);
5728 sb.append(" times)");
5729 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005730 } else {
5731 sb.append("(not used)");
5732 }
5733 pw.println(sb.toString());
5734 uidActivity = true;
5735 }
5736
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005737 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
5738 for (int ic=completions.size()-1; ic>=0; ic--) {
5739 SparseIntArray types = completions.valueAt(ic);
5740 if (types != null) {
5741 pw.print(prefix);
5742 pw.print(" Job Completions ");
5743 pw.print(completions.keyAt(ic));
5744 pw.print(":");
5745 for (int it=0; it<types.size(); it++) {
5746 pw.print(" ");
5747 pw.print(JobParameters.getReasonName(types.keyAt(it)));
5748 pw.print("(");
5749 pw.print(types.valueAt(it));
5750 pw.print("x)");
5751 }
5752 pw.println();
5753 }
5754 }
5755
Amith Yamasani977e11f2018-02-16 11:29:54 -08005756 u.getDeferredJobsLineLocked(sb, which);
5757 if (sb.length() > 0) {
5758 pw.print(" Jobs deferred on launch "); pw.println(sb.toString());
5759 }
5760
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005761 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
5762 prefix, "Flashlight");
5763 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
5764 prefix, "Camera");
5765 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
5766 prefix, "Video");
5767 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
5768 prefix, "Audio");
5769
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005770 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
5771 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005772 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005773 final Uid.Sensor se = sensors.valueAt(ise);
5774 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005775 sb.setLength(0);
5776 sb.append(prefix);
5777 sb.append(" Sensor ");
5778 int handle = se.getHandle();
5779 if (handle == Uid.Sensor.GPS) {
5780 sb.append("GPS");
5781 } else {
5782 sb.append(handle);
5783 }
5784 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005785
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005786 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005787 if (timer != null) {
5788 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005789 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5790 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005791 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005792 final Timer bgTimer = se.getSensorBackgroundTime();
5793 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005794 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5795 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5796 final long bgActualTime = bgTimer != null ?
5797 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5798
Dianne Hackborn61659e52014-07-09 16:13:01 -07005799 //timer.logState();
5800 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005801 if (actualTime != totalTime) {
5802 formatTimeMs(sb, totalTime);
5803 sb.append("blamed realtime, ");
5804 }
5805
5806 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005807 sb.append("realtime (");
5808 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005809 sb.append(" times)");
5810
5811 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005812 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005813 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5814 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005815 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005816 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005818 } else {
5819 sb.append("(not used)");
5820 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005821 } else {
5822 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005823 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005824
5825 pw.println(sb.toString());
5826 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005827 }
5828
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005829 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5830 "Vibrator");
5831 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5832 prefix, "Foreground activities");
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005833 uidActivity |= printTimer(pw, sb, u.getForegroundServiceTimer(), rawRealtime, which,
5834 prefix, "Foreground services");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005835
Dianne Hackborn61659e52014-07-09 16:13:01 -07005836 long totalStateTime = 0;
5837 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5838 long time = u.getProcessStateTime(ips, rawRealtime, which);
5839 if (time > 0) {
5840 totalStateTime += time;
5841 sb.setLength(0);
5842 sb.append(prefix);
5843 sb.append(" ");
5844 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5845 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005846 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005847 pw.println(sb.toString());
5848 uidActivity = true;
5849 }
5850 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005851 if (totalStateTime > 0) {
5852 sb.setLength(0);
5853 sb.append(prefix);
5854 sb.append(" Total running: ");
5855 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5856 pw.println(sb.toString());
5857 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005858
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005859 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5860 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005861 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005862 sb.setLength(0);
5863 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005864 sb.append(" Total cpu time: u=");
5865 formatTimeMs(sb, userCpuTimeUs / 1000);
5866 sb.append("s=");
5867 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005868 pw.println(sb.toString());
5869 }
5870
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005871 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5872 if (cpuFreqTimes != null) {
5873 sb.setLength(0);
5874 sb.append(" Total cpu time per freq:");
5875 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5876 sb.append(" " + cpuFreqTimes[i]);
5877 }
5878 pw.println(sb.toString());
5879 }
5880 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5881 if (screenOffCpuFreqTimes != null) {
5882 sb.setLength(0);
5883 sb.append(" Total screen-off cpu time per freq:");
5884 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5885 sb.append(" " + screenOffCpuFreqTimes[i]);
5886 }
5887 pw.println(sb.toString());
5888 }
5889
Sudheer Shankab2f83c12017-11-13 19:25:01 -08005890 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
5891 final long[] cpuTimes = u.getCpuFreqTimes(which, procState);
5892 if (cpuTimes != null) {
5893 sb.setLength(0);
5894 sb.append(" Cpu times per freq at state "
5895 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5896 for (int i = 0; i < cpuTimes.length; ++i) {
5897 sb.append(" " + cpuTimes[i]);
5898 }
5899 pw.println(sb.toString());
5900 }
5901
5902 final long[] screenOffCpuTimes = u.getScreenOffCpuFreqTimes(which, procState);
5903 if (screenOffCpuTimes != null) {
5904 sb.setLength(0);
5905 sb.append(" Screen-off cpu times per freq at state "
5906 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5907 for (int i = 0; i < screenOffCpuTimes.length; ++i) {
5908 sb.append(" " + screenOffCpuTimes[i]);
5909 }
5910 pw.println(sb.toString());
5911 }
5912 }
5913
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005914 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5915 = u.getProcessStats();
5916 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5917 final Uid.Proc ps = processStats.valueAt(ipr);
5918 long userTime;
5919 long systemTime;
5920 long foregroundTime;
5921 int starts;
5922 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005924 userTime = ps.getUserTime(which);
5925 systemTime = ps.getSystemTime(which);
5926 foregroundTime = ps.getForegroundTime(which);
5927 starts = ps.getStarts(which);
5928 final int numCrashes = ps.getNumCrashes(which);
5929 final int numAnrs = ps.getNumAnrs(which);
5930 numExcessive = which == STATS_SINCE_CHARGED
5931 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005932
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005933 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5934 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5935 sb.setLength(0);
5936 sb.append(prefix); sb.append(" Proc ");
5937 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5938 sb.append(prefix); sb.append(" CPU: ");
5939 formatTimeMs(sb, userTime); sb.append("usr + ");
5940 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5941 formatTimeMs(sb, foregroundTime); sb.append("fg");
5942 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5943 sb.append("\n"); sb.append(prefix); sb.append(" ");
5944 boolean hasOne = false;
5945 if (starts != 0) {
5946 hasOne = true;
5947 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005948 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005949 if (numCrashes != 0) {
5950 if (hasOne) {
5951 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005952 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005953 hasOne = true;
5954 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005955 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005956 if (numAnrs != 0) {
5957 if (hasOne) {
5958 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005959 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005960 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005961 }
5962 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005963 pw.println(sb.toString());
5964 for (int e=0; e<numExcessive; e++) {
5965 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5966 if (ew != null) {
5967 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07005968 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005969 pw.print("cpu");
5970 } else {
5971 pw.print("unknown");
5972 }
5973 pw.print(" use: ");
5974 TimeUtils.formatDuration(ew.usedTime, pw);
5975 pw.print(" over ");
5976 TimeUtils.formatDuration(ew.overTime, pw);
5977 if (ew.overTime != 0) {
5978 pw.print(" (");
5979 pw.print((ew.usedTime*100)/ew.overTime);
5980 pw.println("%)");
5981 }
5982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005983 }
5984 uidActivity = true;
5985 }
5986 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005987
5988 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5989 = u.getPackageStats();
5990 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
5991 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
5992 pw.println(":");
5993 boolean apkActivity = false;
5994 final Uid.Pkg ps = packageStats.valueAt(ipkg);
5995 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
5996 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
5997 pw.print(prefix); pw.print(" Wakeup alarm ");
5998 pw.print(alarms.keyAt(iwa)); pw.print(": ");
5999 pw.print(alarms.valueAt(iwa).getCountLocked(which));
6000 pw.println(" times");
6001 apkActivity = true;
6002 }
6003 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
6004 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
6005 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
6006 final long startTime = ss.getStartTime(batteryUptime, which);
6007 final int starts = ss.getStarts(which);
6008 final int launches = ss.getLaunches(which);
6009 if (startTime != 0 || starts != 0 || launches != 0) {
6010 sb.setLength(0);
6011 sb.append(prefix); sb.append(" Service ");
6012 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
6013 sb.append(prefix); sb.append(" Created for: ");
6014 formatTimeMs(sb, startTime / 1000);
6015 sb.append("uptime\n");
6016 sb.append(prefix); sb.append(" Starts: ");
6017 sb.append(starts);
6018 sb.append(", launches: "); sb.append(launches);
6019 pw.println(sb.toString());
6020 apkActivity = true;
6021 }
6022 }
6023 if (!apkActivity) {
6024 pw.print(prefix); pw.println(" (nothing executed)");
6025 }
6026 uidActivity = true;
6027 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006028 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006029 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006030 }
6031 }
6032 }
6033
Kweku Adams71a95312018-04-16 16:54:24 -07006034 static void printBitDescriptions(StringBuilder sb, int oldval, int newval,
6035 HistoryTag wakelockTag, BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006036 int diff = oldval ^ newval;
6037 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006038 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006039 for (int i=0; i<descriptions.length; i++) {
6040 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006041 if ((diff&bd.mask) != 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006042 sb.append(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006043 if (bd.shift < 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006044 sb.append((newval & bd.mask) != 0 ? "+" : "-");
6045 sb.append(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006046 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
6047 didWake = true;
Kweku Adams71a95312018-04-16 16:54:24 -07006048 sb.append("=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006049 if (longNames) {
Kweku Adams71a95312018-04-16 16:54:24 -07006050 UserHandle.formatUid(sb, wakelockTag.uid);
6051 sb.append(":\"");
6052 sb.append(wakelockTag.string);
6053 sb.append("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006054 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006055 sb.append(wakelockTag.poolIdx);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006056 }
6057 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006058 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006059 sb.append(longNames ? bd.name : bd.shortName);
6060 sb.append("=");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006061 int val = (newval&bd.mask)>>bd.shift;
6062 if (bd.values != null && val >= 0 && val < bd.values.length) {
Kweku Adams71a95312018-04-16 16:54:24 -07006063 sb.append(longNames ? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006064 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006065 sb.append(val);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006066 }
6067 }
6068 }
6069 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006070 if (!didWake && wakelockTag != null) {
Kweku Adams71a95312018-04-16 16:54:24 -07006071 sb.append(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006072 if (longNames) {
Kweku Adams71a95312018-04-16 16:54:24 -07006073 UserHandle.formatUid(sb, wakelockTag.uid);
6074 sb.append(":\"");
6075 sb.append(wakelockTag.string);
6076 sb.append("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006077 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006078 sb.append(wakelockTag.poolIdx);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006079 }
6080 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006081 }
Mike Mac2f518a2017-09-19 16:06:03 -07006082
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006083 public void prepareForDumpLocked() {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006084 // We don't need to require subclasses implement this.
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006085 }
6086
6087 public static class HistoryPrinter {
6088 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006089 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006090 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006091 int oldStatus = -1;
6092 int oldHealth = -1;
6093 int oldPlug = -1;
6094 int oldTemp = -1;
6095 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006096 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006097 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006098
Dianne Hackborn3251b902014-06-20 14:40:53 -07006099 void reset() {
6100 oldState = oldState2 = 0;
6101 oldLevel = -1;
6102 oldStatus = -1;
6103 oldHealth = -1;
6104 oldPlug = -1;
6105 oldTemp = -1;
6106 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006107 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07006108 }
6109
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006110 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006111 boolean verbose) {
Kweku Adams71a95312018-04-16 16:54:24 -07006112 pw.print(printNextItem(rec, baseTime, checkin, verbose));
6113 }
6114
6115 /** Print the next history item to proto. */
6116 public void printNextItem(ProtoOutputStream proto, HistoryItem rec, long baseTime,
6117 boolean verbose) {
6118 String item = printNextItem(rec, baseTime, true, verbose);
6119 for (String line : item.split("\n")) {
6120 proto.write(BatteryStatsServiceDumpHistoryProto.CSV_LINES, line);
6121 }
6122 }
6123
6124 private String printNextItem(HistoryItem rec, long baseTime, boolean checkin,
6125 boolean verbose) {
6126 StringBuilder item = new StringBuilder();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006127 if (!checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006128 item.append(" ");
6129 TimeUtils.formatDuration(
6130 rec.time - baseTime, item, TimeUtils.HUNDRED_DAY_FIELD_LEN);
6131 item.append(" (");
6132 item.append(rec.numReadInts);
6133 item.append(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006134 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006135 item.append(BATTERY_STATS_CHECKIN_VERSION); item.append(',');
6136 item.append(HISTORY_DATA); item.append(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006137 if (lastTime < 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006138 item.append(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006139 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006140 item.append(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006141 }
6142 lastTime = rec.time;
6143 }
6144 if (rec.cmd == HistoryItem.CMD_START) {
6145 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006146 item.append(":");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006147 }
Kweku Adams71a95312018-04-16 16:54:24 -07006148 item.append("START\n");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006149 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006150 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
6151 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006152 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006153 item.append(":");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006154 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006155 if (rec.cmd == HistoryItem.CMD_RESET) {
Kweku Adams71a95312018-04-16 16:54:24 -07006156 item.append("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006157 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006158 }
Kweku Adams71a95312018-04-16 16:54:24 -07006159 item.append("TIME:");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006160 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006161 item.append(rec.currentTime);
6162 item.append("\n");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006163 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006164 item.append(" ");
6165 item.append(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006166 rec.currentTime).toString());
Kweku Adams71a95312018-04-16 16:54:24 -07006167 item.append("\n");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006168 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006169 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
6170 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006171 item.append(":");
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006172 }
Kweku Adams71a95312018-04-16 16:54:24 -07006173 item.append("SHUTDOWN\n");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006174 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
6175 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006176 item.append(":");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006177 }
Kweku Adams71a95312018-04-16 16:54:24 -07006178 item.append("*OVERFLOW*\n");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006179 } else {
6180 if (!checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006181 if (rec.batteryLevel < 10) item.append("00");
6182 else if (rec.batteryLevel < 100) item.append("0");
6183 item.append(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006184 if (verbose) {
Kweku Adams71a95312018-04-16 16:54:24 -07006185 item.append(" ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006186 if (rec.states < 0) ;
Kweku Adams71a95312018-04-16 16:54:24 -07006187 else if (rec.states < 0x10) item.append("0000000");
6188 else if (rec.states < 0x100) item.append("000000");
6189 else if (rec.states < 0x1000) item.append("00000");
6190 else if (rec.states < 0x10000) item.append("0000");
6191 else if (rec.states < 0x100000) item.append("000");
6192 else if (rec.states < 0x1000000) item.append("00");
6193 else if (rec.states < 0x10000000) item.append("0");
6194 item.append(Integer.toHexString(rec.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006195 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006196 } else {
6197 if (oldLevel != rec.batteryLevel) {
6198 oldLevel = rec.batteryLevel;
Kweku Adams71a95312018-04-16 16:54:24 -07006199 item.append(",Bl="); item.append(rec.batteryLevel);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006200 }
6201 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006202 if (oldStatus != rec.batteryStatus) {
6203 oldStatus = rec.batteryStatus;
Kweku Adams71a95312018-04-16 16:54:24 -07006204 item.append(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006205 switch (oldStatus) {
6206 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Kweku Adams71a95312018-04-16 16:54:24 -07006207 item.append(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006208 break;
6209 case BatteryManager.BATTERY_STATUS_CHARGING:
Kweku Adams71a95312018-04-16 16:54:24 -07006210 item.append(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006211 break;
6212 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Kweku Adams71a95312018-04-16 16:54:24 -07006213 item.append(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006214 break;
6215 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Kweku Adams71a95312018-04-16 16:54:24 -07006216 item.append(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006217 break;
6218 case BatteryManager.BATTERY_STATUS_FULL:
Kweku Adams71a95312018-04-16 16:54:24 -07006219 item.append(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006220 break;
6221 default:
Kweku Adams71a95312018-04-16 16:54:24 -07006222 item.append(oldStatus);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006223 break;
6224 }
6225 }
6226 if (oldHealth != rec.batteryHealth) {
6227 oldHealth = rec.batteryHealth;
Kweku Adams71a95312018-04-16 16:54:24 -07006228 item.append(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006229 switch (oldHealth) {
6230 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Kweku Adams71a95312018-04-16 16:54:24 -07006231 item.append(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006232 break;
6233 case BatteryManager.BATTERY_HEALTH_GOOD:
Kweku Adams71a95312018-04-16 16:54:24 -07006234 item.append(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006235 break;
6236 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Kweku Adams71a95312018-04-16 16:54:24 -07006237 item.append(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006238 break;
6239 case BatteryManager.BATTERY_HEALTH_DEAD:
Kweku Adams71a95312018-04-16 16:54:24 -07006240 item.append(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006241 break;
6242 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Kweku Adams71a95312018-04-16 16:54:24 -07006243 item.append(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006244 break;
6245 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Kweku Adams71a95312018-04-16 16:54:24 -07006246 item.append(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006247 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006248 case BatteryManager.BATTERY_HEALTH_COLD:
Kweku Adams71a95312018-04-16 16:54:24 -07006249 item.append(checkin ? "c" : "cold");
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006250 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006251 default:
Kweku Adams71a95312018-04-16 16:54:24 -07006252 item.append(oldHealth);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006253 break;
6254 }
6255 }
6256 if (oldPlug != rec.batteryPlugType) {
6257 oldPlug = rec.batteryPlugType;
Kweku Adams71a95312018-04-16 16:54:24 -07006258 item.append(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006259 switch (oldPlug) {
6260 case 0:
Kweku Adams71a95312018-04-16 16:54:24 -07006261 item.append(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006262 break;
6263 case BatteryManager.BATTERY_PLUGGED_AC:
Kweku Adams71a95312018-04-16 16:54:24 -07006264 item.append(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006265 break;
6266 case BatteryManager.BATTERY_PLUGGED_USB:
Kweku Adams71a95312018-04-16 16:54:24 -07006267 item.append(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006268 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006269 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Kweku Adams71a95312018-04-16 16:54:24 -07006270 item.append(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006271 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006272 default:
Kweku Adams71a95312018-04-16 16:54:24 -07006273 item.append(oldPlug);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006274 break;
6275 }
6276 }
6277 if (oldTemp != rec.batteryTemperature) {
6278 oldTemp = rec.batteryTemperature;
Kweku Adams71a95312018-04-16 16:54:24 -07006279 item.append(checkin ? ",Bt=" : " temp=");
6280 item.append(oldTemp);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006281 }
6282 if (oldVolt != rec.batteryVoltage) {
6283 oldVolt = rec.batteryVoltage;
Kweku Adams71a95312018-04-16 16:54:24 -07006284 item.append(checkin ? ",Bv=" : " volt=");
6285 item.append(oldVolt);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006286 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006287 final int chargeMAh = rec.batteryChargeUAh / 1000;
6288 if (oldChargeMAh != chargeMAh) {
6289 oldChargeMAh = chargeMAh;
Kweku Adams71a95312018-04-16 16:54:24 -07006290 item.append(checkin ? ",Bcc=" : " charge=");
6291 item.append(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07006292 }
Kweku Adams71a95312018-04-16 16:54:24 -07006293 printBitDescriptions(item, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006294 HISTORY_STATE_DESCRIPTIONS, !checkin);
Kweku Adams71a95312018-04-16 16:54:24 -07006295 printBitDescriptions(item, oldState2, rec.states2, null,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006296 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006297 if (rec.wakeReasonTag != null) {
6298 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006299 item.append(",wr=");
6300 item.append(rec.wakeReasonTag.poolIdx);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006301 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006302 item.append(" wake_reason=");
6303 item.append(rec.wakeReasonTag.uid);
6304 item.append(":\"");
6305 item.append(rec.wakeReasonTag.string);
6306 item.append("\"");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006307 }
6308 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08006309 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Kweku Adams71a95312018-04-16 16:54:24 -07006310 item.append(checkin ? "," : " ");
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006311 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006312 item.append("+");
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006313 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006314 item.append("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006315 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006316 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
6317 : HISTORY_EVENT_NAMES;
6318 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
6319 | HistoryItem.EVENT_FLAG_FINISH);
6320 if (idx >= 0 && idx < eventNames.length) {
Kweku Adams71a95312018-04-16 16:54:24 -07006321 item.append(eventNames[idx]);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006322 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006323 item.append(checkin ? "Ev" : "event");
6324 item.append(idx);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006325 }
Kweku Adams71a95312018-04-16 16:54:24 -07006326 item.append("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006327 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006328 item.append(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08006329 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006330 item.append(HISTORY_EVENT_INT_FORMATTERS[idx]
Adam Lesinski041d9172016-12-12 12:03:56 -08006331 .applyAsString(rec.eventTag.uid));
Kweku Adams71a95312018-04-16 16:54:24 -07006332 item.append(":\"");
6333 item.append(rec.eventTag.string);
6334 item.append("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006335 }
6336 }
Kweku Adams71a95312018-04-16 16:54:24 -07006337 item.append("\n");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006338 if (rec.stepDetails != null) {
6339 if (!checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006340 item.append(" Details: cpu=");
6341 item.append(rec.stepDetails.userTime);
6342 item.append("u+");
6343 item.append(rec.stepDetails.systemTime);
6344 item.append("s");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006345 if (rec.stepDetails.appCpuUid1 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006346 item.append(" (");
6347 printStepCpuUidDetails(item, rec.stepDetails.appCpuUid1,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006348 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6349 if (rec.stepDetails.appCpuUid2 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006350 item.append(", ");
6351 printStepCpuUidDetails(item, rec.stepDetails.appCpuUid2,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006352 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6353 }
6354 if (rec.stepDetails.appCpuUid3 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006355 item.append(", ");
6356 printStepCpuUidDetails(item, rec.stepDetails.appCpuUid3,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006357 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6358 }
Kweku Adams71a95312018-04-16 16:54:24 -07006359 item.append(')');
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006360 }
Kweku Adams71a95312018-04-16 16:54:24 -07006361 item.append("\n");
6362 item.append(" /proc/stat=");
6363 item.append(rec.stepDetails.statUserTime);
6364 item.append(" usr, ");
6365 item.append(rec.stepDetails.statSystemTime);
6366 item.append(" sys, ");
6367 item.append(rec.stepDetails.statIOWaitTime);
6368 item.append(" io, ");
6369 item.append(rec.stepDetails.statIrqTime);
6370 item.append(" irq, ");
6371 item.append(rec.stepDetails.statSoftIrqTime);
6372 item.append(" sirq, ");
6373 item.append(rec.stepDetails.statIdlTime);
6374 item.append(" idle");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006375 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
6376 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
6377 + rec.stepDetails.statSoftIrqTime;
6378 int total = totalRun + rec.stepDetails.statIdlTime;
6379 if (total > 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006380 item.append(" (");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006381 float perc = ((float)totalRun) / ((float)total) * 100;
Kweku Adams71a95312018-04-16 16:54:24 -07006382 item.append(String.format("%.1f%%", perc));
6383 item.append(" of ");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006384 StringBuilder sb = new StringBuilder(64);
6385 formatTimeMsNoSpace(sb, total*10);
Kweku Adams71a95312018-04-16 16:54:24 -07006386 item.append(sb);
6387 item.append(")");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006388 }
Kweku Adams71a95312018-04-16 16:54:24 -07006389 item.append(", PlatformIdleStat ");
6390 item.append(rec.stepDetails.statPlatformIdleState);
6391 item.append("\n");
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006392
Kweku Adams71a95312018-04-16 16:54:24 -07006393 item.append(", SubsystemPowerState ");
6394 item.append(rec.stepDetails.statSubsystemPowerState);
6395 item.append("\n");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006396 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006397 item.append(BATTERY_STATS_CHECKIN_VERSION); item.append(',');
6398 item.append(HISTORY_DATA); item.append(",0,Dcpu=");
6399 item.append(rec.stepDetails.userTime);
6400 item.append(":");
6401 item.append(rec.stepDetails.systemTime);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006402 if (rec.stepDetails.appCpuUid1 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006403 printStepCpuUidCheckinDetails(item, rec.stepDetails.appCpuUid1,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006404 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6405 if (rec.stepDetails.appCpuUid2 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006406 printStepCpuUidCheckinDetails(item, rec.stepDetails.appCpuUid2,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006407 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6408 }
6409 if (rec.stepDetails.appCpuUid3 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006410 printStepCpuUidCheckinDetails(item, rec.stepDetails.appCpuUid3,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006411 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6412 }
6413 }
Kweku Adams71a95312018-04-16 16:54:24 -07006414 item.append("\n");
6415 item.append(BATTERY_STATS_CHECKIN_VERSION); item.append(',');
6416 item.append(HISTORY_DATA); item.append(",0,Dpst=");
6417 item.append(rec.stepDetails.statUserTime);
6418 item.append(',');
6419 item.append(rec.stepDetails.statSystemTime);
6420 item.append(',');
6421 item.append(rec.stepDetails.statIOWaitTime);
6422 item.append(',');
6423 item.append(rec.stepDetails.statIrqTime);
6424 item.append(',');
6425 item.append(rec.stepDetails.statSoftIrqTime);
6426 item.append(',');
6427 item.append(rec.stepDetails.statIdlTime);
6428 item.append(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006429 if (rec.stepDetails.statPlatformIdleState != null) {
Kweku Adams71a95312018-04-16 16:54:24 -07006430 item.append(rec.stepDetails.statPlatformIdleState);
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006431 if (rec.stepDetails.statSubsystemPowerState != null) {
Kweku Adams71a95312018-04-16 16:54:24 -07006432 item.append(',');
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006433 }
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006434 }
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006435
6436 if (rec.stepDetails.statSubsystemPowerState != null) {
Kweku Adams71a95312018-04-16 16:54:24 -07006437 item.append(rec.stepDetails.statSubsystemPowerState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006438 }
Kweku Adams71a95312018-04-16 16:54:24 -07006439 item.append("\n");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006440 }
6441 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006442 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006443 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006444 }
Kweku Adams71a95312018-04-16 16:54:24 -07006445
6446 return item.toString();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006447 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006448
Kweku Adams71a95312018-04-16 16:54:24 -07006449 private void printStepCpuUidDetails(StringBuilder sb, int uid, int utime, int stime) {
6450 UserHandle.formatUid(sb, uid);
6451 sb.append("=");
6452 sb.append(utime);
6453 sb.append("u+");
6454 sb.append(stime);
6455 sb.append("s");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006456 }
6457
Kweku Adams71a95312018-04-16 16:54:24 -07006458 private void printStepCpuUidCheckinDetails(StringBuilder sb, int uid, int utime,
6459 int stime) {
6460 sb.append('/');
6461 sb.append(uid);
6462 sb.append(":");
6463 sb.append(utime);
6464 sb.append(":");
6465 sb.append(stime);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006466 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006467 }
6468
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006469 private void printSizeValue(PrintWriter pw, long size) {
6470 float result = size;
6471 String suffix = "";
6472 if (result >= 10*1024) {
6473 suffix = "KB";
6474 result = result / 1024;
6475 }
6476 if (result >= 10*1024) {
6477 suffix = "MB";
6478 result = result / 1024;
6479 }
6480 if (result >= 10*1024) {
6481 suffix = "GB";
6482 result = result / 1024;
6483 }
6484 if (result >= 10*1024) {
6485 suffix = "TB";
6486 result = result / 1024;
6487 }
6488 if (result >= 10*1024) {
6489 suffix = "PB";
6490 result = result / 1024;
6491 }
6492 pw.print((int)result);
6493 pw.print(suffix);
6494 }
6495
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006496 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
6497 String label3, long estimatedTime) {
6498 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006499 return false;
6500 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006501 pw.print(label1);
6502 pw.print(label2);
6503 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006504 StringBuilder sb = new StringBuilder(64);
6505 formatTimeMs(sb, estimatedTime);
6506 pw.print(sb);
6507 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006508 return true;
6509 }
6510
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006511 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
6512 LevelStepTracker steps, boolean checkin) {
6513 if (steps == null) {
6514 return false;
6515 }
6516 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006517 if (count <= 0) {
6518 return false;
6519 }
6520 if (!checkin) {
6521 pw.println(header);
6522 }
Kweku Adams030980a2015-04-01 16:07:48 -07006523 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006524 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006525 long duration = steps.getDurationAt(i);
6526 int level = steps.getLevelAt(i);
6527 long initMode = steps.getInitModeAt(i);
6528 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006529 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006530 lineArgs[0] = Long.toString(duration);
6531 lineArgs[1] = Integer.toString(level);
6532 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6533 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6534 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
6535 case Display.STATE_ON: lineArgs[2] = "s+"; break;
6536 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
6537 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07006538 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006539 }
6540 } else {
6541 lineArgs[2] = "";
6542 }
6543 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6544 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
6545 } else {
6546 lineArgs[3] = "";
6547 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006548 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07006549 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006550 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07006551 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006552 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006553 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
6554 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006555 pw.print(prefix);
6556 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006557 TimeUtils.formatDuration(duration, pw);
6558 pw.print(" to "); pw.print(level);
6559 boolean haveModes = false;
6560 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6561 pw.print(" (");
6562 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6563 case Display.STATE_OFF: pw.print("screen-off"); break;
6564 case Display.STATE_ON: pw.print("screen-on"); break;
6565 case Display.STATE_DOZE: pw.print("screen-doze"); break;
6566 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07006567 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006568 }
6569 haveModes = true;
6570 }
6571 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6572 pw.print(haveModes ? ", " : " (");
6573 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
6574 ? "power-save-on" : "power-save-off");
6575 haveModes = true;
6576 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006577 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6578 pw.print(haveModes ? ", " : " (");
6579 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6580 ? "device-idle-on" : "device-idle-off");
6581 haveModes = true;
6582 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006583 if (haveModes) {
6584 pw.print(")");
6585 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006586 pw.println();
6587 }
6588 }
6589 return true;
6590 }
6591
Kweku Adams87b19ec2017-10-09 12:40:03 -07006592 private static void dumpDurationSteps(ProtoOutputStream proto, long fieldId,
6593 LevelStepTracker steps) {
6594 if (steps == null) {
6595 return;
6596 }
6597 int count = steps.mNumStepDurations;
Kweku Adams87b19ec2017-10-09 12:40:03 -07006598 for (int i = 0; i < count; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07006599 long token = proto.start(fieldId);
Kweku Adams87b19ec2017-10-09 12:40:03 -07006600 proto.write(SystemProto.BatteryLevelStep.DURATION_MS, steps.getDurationAt(i));
6601 proto.write(SystemProto.BatteryLevelStep.LEVEL, steps.getLevelAt(i));
6602
6603 final long initMode = steps.getInitModeAt(i);
6604 final long modMode = steps.getModModeAt(i);
6605
6606 int ds = SystemProto.BatteryLevelStep.DS_MIXED;
6607 if ((modMode & STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6608 switch ((int) (initMode & STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6609 case Display.STATE_OFF:
6610 ds = SystemProto.BatteryLevelStep.DS_OFF;
6611 break;
6612 case Display.STATE_ON:
6613 ds = SystemProto.BatteryLevelStep.DS_ON;
6614 break;
6615 case Display.STATE_DOZE:
6616 ds = SystemProto.BatteryLevelStep.DS_DOZE;
6617 break;
6618 case Display.STATE_DOZE_SUSPEND:
6619 ds = SystemProto.BatteryLevelStep.DS_DOZE_SUSPEND;
6620 break;
6621 default:
6622 ds = SystemProto.BatteryLevelStep.DS_ERROR;
6623 break;
6624 }
6625 }
6626 proto.write(SystemProto.BatteryLevelStep.DISPLAY_STATE, ds);
6627
6628 int psm = SystemProto.BatteryLevelStep.PSM_MIXED;
6629 if ((modMode & STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6630 psm = (initMode & STEP_LEVEL_MODE_POWER_SAVE) != 0
6631 ? SystemProto.BatteryLevelStep.PSM_ON : SystemProto.BatteryLevelStep.PSM_OFF;
6632 }
6633 proto.write(SystemProto.BatteryLevelStep.POWER_SAVE_MODE, psm);
6634
6635 int im = SystemProto.BatteryLevelStep.IM_MIXED;
6636 if ((modMode & STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6637 im = (initMode & STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6638 ? SystemProto.BatteryLevelStep.IM_ON : SystemProto.BatteryLevelStep.IM_OFF;
6639 }
6640 proto.write(SystemProto.BatteryLevelStep.IDLE_MODE, im);
6641
6642 proto.end(token);
6643 }
6644 }
6645
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006646 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006647 public static final int DUMP_DAILY_ONLY = 1<<2;
6648 public static final int DUMP_HISTORY_ONLY = 1<<3;
6649 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
6650 public static final int DUMP_VERBOSE = 1<<5;
6651 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006652
Dianne Hackborn37de0982014-05-09 09:32:18 -07006653 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
6654 final HistoryPrinter hprinter = new HistoryPrinter();
6655 final HistoryItem rec = new HistoryItem();
6656 long lastTime = -1;
6657 long baseTime = -1;
6658 boolean printed = false;
6659 HistoryEventTracker tracker = null;
6660 while (getNextHistoryLocked(rec)) {
6661 lastTime = rec.time;
6662 if (baseTime < 0) {
6663 baseTime = lastTime;
6664 }
6665 if (rec.time >= histStart) {
6666 if (histStart >= 0 && !printed) {
6667 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07006668 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006669 || rec.cmd == HistoryItem.CMD_START
6670 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006671 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006672 hprinter.printNextItem(pw, rec, baseTime, checkin,
6673 (flags&DUMP_VERBOSE) != 0);
6674 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006675 } else if (rec.currentTime != 0) {
6676 printed = true;
6677 byte cmd = rec.cmd;
6678 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006679 hprinter.printNextItem(pw, rec, baseTime, checkin,
6680 (flags&DUMP_VERBOSE) != 0);
6681 rec.cmd = cmd;
6682 }
6683 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006684 if (rec.cmd != HistoryItem.CMD_UPDATE) {
6685 hprinter.printNextItem(pw, rec, baseTime, checkin,
6686 (flags&DUMP_VERBOSE) != 0);
6687 rec.cmd = HistoryItem.CMD_UPDATE;
6688 }
6689 int oldEventCode = rec.eventCode;
6690 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006691 rec.eventTag = new HistoryTag();
6692 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
6693 HashMap<String, SparseIntArray> active
6694 = tracker.getStateForEvent(i);
6695 if (active == null) {
6696 continue;
6697 }
6698 for (HashMap.Entry<String, SparseIntArray> ent
6699 : active.entrySet()) {
6700 SparseIntArray uids = ent.getValue();
6701 for (int j=0; j<uids.size(); j++) {
6702 rec.eventCode = i;
6703 rec.eventTag.string = ent.getKey();
6704 rec.eventTag.uid = uids.keyAt(j);
6705 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006706 hprinter.printNextItem(pw, rec, baseTime, checkin,
6707 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006708 rec.wakeReasonTag = null;
6709 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006710 }
6711 }
6712 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006713 rec.eventCode = oldEventCode;
6714 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006715 tracker = null;
6716 }
6717 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006718 hprinter.printNextItem(pw, rec, baseTime, checkin,
6719 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07006720 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
6721 // This is an attempt to aggregate the previous state and generate
6722 // fake events to reflect that state at the point where we start
6723 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07006724 if (tracker == null) {
6725 tracker = new HistoryEventTracker();
6726 }
6727 tracker.updateState(rec.eventCode, rec.eventTag.string,
6728 rec.eventTag.uid, rec.eventTag.poolIdx);
6729 }
6730 }
6731 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07006732 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006733 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
6734 }
6735 }
6736
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006737 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
6738 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
6739 if (steps == null) {
6740 return;
6741 }
6742 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
6743 if (timeRemaining >= 0) {
6744 pw.print(prefix); pw.print(label); pw.print(" total time: ");
6745 tmpSb.setLength(0);
6746 formatTimeMs(tmpSb, timeRemaining);
6747 pw.print(tmpSb);
6748 pw.print(" (from "); pw.print(tmpOutInt[0]);
6749 pw.println(" steps)");
6750 }
6751 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6752 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6753 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
6754 if (estimatedTime > 0) {
6755 pw.print(prefix); pw.print(label); pw.print(" ");
6756 pw.print(STEP_LEVEL_MODE_LABELS[i]);
6757 pw.print(" time: ");
6758 tmpSb.setLength(0);
6759 formatTimeMs(tmpSb, estimatedTime);
6760 pw.print(tmpSb);
6761 pw.print(" (from "); pw.print(tmpOutInt[0]);
6762 pw.println(" steps)");
6763 }
6764 }
6765 }
6766
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006767 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
6768 ArrayList<PackageChange> changes) {
6769 if (changes == null) {
6770 return;
6771 }
6772 pw.print(prefix); pw.println("Package changes:");
6773 for (int i=0; i<changes.size(); i++) {
6774 PackageChange pc = changes.get(i);
6775 if (pc.mUpdate) {
6776 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
6777 pw.print(" vers="); pw.println(pc.mVersionCode);
6778 } else {
6779 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
6780 }
6781 }
6782 }
6783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006784 /**
6785 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
6786 *
6787 * @param pw a Printer to receive the dump output.
6788 */
6789 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006790 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006791 prepareForDumpLocked();
6792
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006793 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006794 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006795
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006796 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006797 final long historyTotalSize = getHistoryTotalSize();
6798 final long historyUsedSize = getHistoryUsedSize();
6799 if (startIteratingHistoryLocked()) {
6800 try {
6801 pw.print("Battery History (");
6802 pw.print((100*historyUsedSize)/historyTotalSize);
6803 pw.print("% used, ");
6804 printSizeValue(pw, historyUsedSize);
6805 pw.print(" used of ");
6806 printSizeValue(pw, historyTotalSize);
6807 pw.print(", ");
6808 pw.print(getHistoryStringPoolSize());
6809 pw.print(" strings using ");
6810 printSizeValue(pw, getHistoryStringPoolBytes());
6811 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07006812 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006813 pw.println();
6814 } finally {
6815 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006816 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006817 }
6818
6819 if (startIteratingOldHistoryLocked()) {
6820 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006821 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006822 pw.println("Old battery History:");
6823 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006824 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006825 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006826 if (baseTime < 0) {
6827 baseTime = rec.time;
6828 }
6829 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006830 }
6831 pw.println();
6832 } finally {
6833 finishIteratingOldHistoryLocked();
6834 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006835 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006836 }
6837
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006838 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006839 return;
6840 }
6841
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006842 if (!filtering) {
6843 SparseArray<? extends Uid> uidStats = getUidStats();
6844 final int NU = uidStats.size();
6845 boolean didPid = false;
6846 long nowRealtime = SystemClock.elapsedRealtime();
6847 for (int i=0; i<NU; i++) {
6848 Uid uid = uidStats.valueAt(i);
6849 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
6850 if (pids != null) {
6851 for (int j=0; j<pids.size(); j++) {
6852 Uid.Pid pid = pids.valueAt(j);
6853 if (!didPid) {
6854 pw.println("Per-PID Stats:");
6855 didPid = true;
6856 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006857 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
6858 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006859 pw.print(" PID "); pw.print(pids.keyAt(j));
6860 pw.print(" wake time: ");
6861 TimeUtils.formatDuration(time, pw);
6862 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006863 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006864 }
6865 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006866 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006867 pw.println();
6868 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006869 }
6870
6871 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006872 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
6873 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006874 long timeRemaining = computeBatteryTimeRemaining(
6875 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006876 if (timeRemaining >= 0) {
6877 pw.print(" Estimated discharge time remaining: ");
6878 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6879 pw.println();
6880 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006881 final LevelStepTracker steps = getDischargeLevelStepTracker();
6882 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6883 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
6884 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6885 STEP_LEVEL_MODE_VALUES[i], null));
6886 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006887 pw.println();
6888 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006889 if (dumpDurationSteps(pw, " ", "Charge step durations:",
6890 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006891 long timeRemaining = computeChargeTimeRemaining(
6892 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006893 if (timeRemaining >= 0) {
6894 pw.print(" Estimated charge time remaining: ");
6895 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6896 pw.println();
6897 }
6898 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006899 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006900 }
Dianne Hackbornc81983a2017-10-20 16:16:32 -07006901 if (!filtering || (flags & DUMP_DAILY_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006902 pw.println("Daily stats:");
6903 pw.print(" Current start time: ");
6904 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6905 getCurrentDailyStartTime()).toString());
6906 pw.print(" Next min deadline: ");
6907 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6908 getNextMinDailyDeadline()).toString());
6909 pw.print(" Next max deadline: ");
6910 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6911 getNextMaxDailyDeadline()).toString());
6912 StringBuilder sb = new StringBuilder(64);
6913 int[] outInt = new int[1];
6914 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
6915 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006916 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
6917 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006918 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006919 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
6920 dsteps, false)) {
6921 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6922 sb, outInt);
6923 }
6924 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6925 csteps, false)) {
6926 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6927 sb, outInt);
6928 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006929 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006930 } else {
6931 pw.println(" Current daily steps:");
6932 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6933 sb, outInt);
6934 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6935 sb, outInt);
6936 }
6937 }
6938 DailyItem dit;
6939 int curIndex = 0;
6940 while ((dit=getDailyItemLocked(curIndex)) != null) {
6941 curIndex++;
6942 if ((flags&DUMP_DAILY_ONLY) != 0) {
6943 pw.println();
6944 }
6945 pw.print(" Daily from ");
6946 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6947 pw.print(" to ");
6948 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6949 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006950 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006951 if (dumpDurationSteps(pw, " ",
6952 " Discharge step durations:", dit.mDischargeSteps, false)) {
6953 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6954 sb, outInt);
6955 }
6956 if (dumpDurationSteps(pw, " ",
6957 " Charge step durations:", dit.mChargeSteps, false)) {
6958 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6959 sb, outInt);
6960 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006961 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006962 } else {
6963 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6964 sb, outInt);
6965 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6966 sb, outInt);
6967 }
6968 }
6969 pw.println();
6970 }
6971 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006972 pw.println("Statistics since last charge:");
6973 pw.println(" System starts: " + getStartCount()
6974 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006975 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6976 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006977 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006979 }
Mike Mac2f518a2017-09-19 16:06:03 -07006980
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006981 // This is called from BatteryStatsService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006982 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006983 public void dumpCheckinLocked(Context context, PrintWriter pw,
6984 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006985 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006986
6987 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006988 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6989 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006990
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006991 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6992
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006993 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07006994 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006995 try {
6996 for (int i=0; i<getHistoryStringPoolSize(); i++) {
6997 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6998 pw.print(HISTORY_STRING_POOL); pw.print(',');
6999 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007000 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007001 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007002 pw.print(",\"");
7003 String str = getHistoryTagPoolString(i);
7004 str = str.replace("\\", "\\\\");
7005 str = str.replace("\"", "\\\"");
7006 pw.print(str);
7007 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007008 pw.println();
7009 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07007010 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007011 } finally {
7012 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08007013 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07007014 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07007015 }
7016
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007017 if ((flags & DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007018 return;
7019 }
7020
Dianne Hackborne4a59512010-12-07 11:08:07 -08007021 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007022 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08007023 for (int i=0; i<apps.size(); i++) {
7024 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007025 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
7026 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08007027 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007028 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
7029 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007030 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007031 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007032 }
7033 SparseArray<? extends Uid> uidStats = getUidStats();
7034 final int NU = uidStats.size();
7035 String[] lineArgs = new String[2];
7036 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007037 int uid = UserHandle.getAppId(uidStats.keyAt(i));
7038 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
7039 if (pkgs != null && !pkgs.second.value) {
7040 pkgs.second.value = true;
7041 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08007042 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007043 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007044 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
7045 (Object[])lineArgs);
7046 }
7047 }
7048 }
7049 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007050 if ((flags & DUMP_DAILY_ONLY) == 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007051 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007052 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07007053 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007054 if (timeRemaining >= 0) {
7055 lineArgs[0] = Long.toString(timeRemaining);
7056 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
7057 (Object[])lineArgs);
7058 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007059 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07007060 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007061 if (timeRemaining >= 0) {
7062 lineArgs[0] = Long.toString(timeRemaining);
7063 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
7064 (Object[])lineArgs);
7065 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007066 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
7067 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08007068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007069 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007070
Kweku Adams71a95312018-04-16 16:54:24 -07007071 /**
7072 * Dump #STATS_SINCE_CHARGED batterystats data to a proto. If the flags include
7073 * DUMP_INCLUDE_HISTORY or DUMP_HISTORY_ONLY, only the history will be dumped.
7074 * @hide
7075 */
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007076 public void dumpProtoLocked(Context context, FileDescriptor fd, List<ApplicationInfo> apps,
Kweku Adams71a95312018-04-16 16:54:24 -07007077 int flags, long histStart) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007078 final ProtoOutputStream proto = new ProtoOutputStream(fd);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007079 prepareForDumpLocked();
7080
Kweku Adams71a95312018-04-16 16:54:24 -07007081 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
7082 dumpProtoHistoryLocked(proto, flags, histStart);
7083 proto.flush();
7084 return;
7085 }
7086
7087 final long bToken = proto.start(BatteryStatsServiceDumpProto.BATTERYSTATS);
7088
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007089 proto.write(BatteryStatsProto.REPORT_VERSION, CHECKIN_VERSION);
7090 proto.write(BatteryStatsProto.PARCEL_VERSION, getParcelVersion());
7091 proto.write(BatteryStatsProto.START_PLATFORM_VERSION, getStartPlatformVersion());
7092 proto.write(BatteryStatsProto.END_PLATFORM_VERSION, getEndPlatformVersion());
7093
Kweku Adams71a95312018-04-16 16:54:24 -07007094 if ((flags & DUMP_DAILY_ONLY) == 0) {
Kweku Adams103351f2017-10-16 14:39:34 -07007095 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false,
7096 (flags & DUMP_DEVICE_WIFI_ONLY) != 0);
7097 helper.create(this);
7098 helper.refreshStats(STATS_SINCE_CHARGED, UserHandle.USER_ALL);
7099
7100 dumpProtoAppsLocked(proto, helper, apps);
7101 dumpProtoSystemLocked(proto, helper);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007102 }
7103
7104 proto.end(bToken);
7105 proto.flush();
7106 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07007107
Kweku Adams103351f2017-10-16 14:39:34 -07007108 private void dumpProtoAppsLocked(ProtoOutputStream proto, BatteryStatsHelper helper,
7109 List<ApplicationInfo> apps) {
7110 final int which = STATS_SINCE_CHARGED;
7111 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7112 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7113 final long rawRealtimeUs = rawRealtimeMs * 1000;
7114 final long batteryUptimeUs = getBatteryUptime(rawUptimeUs);
7115
7116 SparseArray<ArrayList<String>> aidToPackages = new SparseArray<>();
7117 if (apps != null) {
7118 for (int i = 0; i < apps.size(); ++i) {
7119 ApplicationInfo ai = apps.get(i);
7120 int aid = UserHandle.getAppId(ai.uid);
7121 ArrayList<String> pkgs = aidToPackages.get(aid);
7122 if (pkgs == null) {
7123 pkgs = new ArrayList<String>();
7124 aidToPackages.put(aid, pkgs);
7125 }
7126 pkgs.add(ai.packageName);
7127 }
7128 }
7129
7130 SparseArray<BatterySipper> uidToSipper = new SparseArray<>();
7131 final List<BatterySipper> sippers = helper.getUsageList();
7132 if (sippers != null) {
7133 for (int i = 0; i < sippers.size(); ++i) {
7134 final BatterySipper bs = sippers.get(i);
7135 if (bs.drainType != BatterySipper.DrainType.APP) {
7136 // Others are handled by dumpProtoSystemLocked()
7137 continue;
7138 }
7139 uidToSipper.put(bs.uidObj.getUid(), bs);
7140 }
7141 }
7142
7143 SparseArray<? extends Uid> uidStats = getUidStats();
7144 final int n = uidStats.size();
7145 for (int iu = 0; iu < n; ++iu) {
7146 final long uTkn = proto.start(BatteryStatsProto.UIDS);
7147 final Uid u = uidStats.valueAt(iu);
7148
7149 final int uid = uidStats.keyAt(iu);
7150 proto.write(UidProto.UID, uid);
7151
7152 // Print packages and apk stats (UID_DATA & APK_DATA)
7153 ArrayList<String> pkgs = aidToPackages.get(UserHandle.getAppId(uid));
7154 if (pkgs == null) {
7155 pkgs = new ArrayList<String>();
7156 }
7157 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats =
7158 u.getPackageStats();
7159 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7160 String pkg = packageStats.keyAt(ipkg);
7161 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats =
7162 packageStats.valueAt(ipkg).getServiceStats();
7163 if (serviceStats.size() == 0) {
7164 // Due to the way ActivityManagerService logs wakeup alarms, some packages (for
7165 // example, "android") may be included in the packageStats that aren't part of
7166 // the UID. If they don't have any services, then they shouldn't be listed here.
7167 // These packages won't be a part in the pkgs List.
7168 continue;
7169 }
7170
7171 final long pToken = proto.start(UidProto.PACKAGES);
7172 proto.write(UidProto.Package.NAME, pkg);
7173 // Remove from the packages list since we're logging it here.
7174 pkgs.remove(pkg);
7175
7176 for (int isvc = serviceStats.size() - 1; isvc >= 0; --isvc) {
7177 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
Kweku Adams14f3d222018-03-22 14:12:55 -07007178
7179 final long startTimeMs = roundUsToMs(ss.getStartTime(batteryUptimeUs, which));
7180 final int starts = ss.getStarts(which);
7181 final int launches = ss.getLaunches(which);
7182 if (startTimeMs == 0 && starts == 0 && launches == 0) {
7183 continue;
7184 }
7185
Kweku Adams103351f2017-10-16 14:39:34 -07007186 long sToken = proto.start(UidProto.Package.SERVICES);
7187
7188 proto.write(UidProto.Package.Service.NAME, serviceStats.keyAt(isvc));
Kweku Adams14f3d222018-03-22 14:12:55 -07007189 proto.write(UidProto.Package.Service.START_DURATION_MS, startTimeMs);
7190 proto.write(UidProto.Package.Service.START_COUNT, starts);
7191 proto.write(UidProto.Package.Service.LAUNCH_COUNT, launches);
Kweku Adams103351f2017-10-16 14:39:34 -07007192
7193 proto.end(sToken);
7194 }
7195 proto.end(pToken);
7196 }
7197 // Print any remaining packages that weren't in the packageStats map. pkgs is pulled
7198 // from PackageManager data. Packages are only included in packageStats if there was
7199 // specific data tracked for them (services and wakeup alarms, etc.).
7200 for (String p : pkgs) {
7201 final long pToken = proto.start(UidProto.PACKAGES);
7202 proto.write(UidProto.Package.NAME, p);
7203 proto.end(pToken);
7204 }
7205
7206 // Total wakelock data (AGGREGATED_WAKELOCK_DATA)
7207 if (u.getAggregatedPartialWakelockTimer() != null) {
7208 final Timer timer = u.getAggregatedPartialWakelockTimer();
7209 // Times are since reset (regardless of 'which')
7210 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
7211 final Timer bgTimer = timer.getSubTimer();
7212 final long bgTimeMs = bgTimer != null
7213 ? bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
7214 final long awToken = proto.start(UidProto.AGGREGATED_WAKELOCK);
7215 proto.write(UidProto.AggregatedWakelock.PARTIAL_DURATION_MS, totTimeMs);
7216 proto.write(UidProto.AggregatedWakelock.BACKGROUND_PARTIAL_DURATION_MS, bgTimeMs);
7217 proto.end(awToken);
7218 }
7219
7220 // Audio (AUDIO_DATA)
7221 dumpTimer(proto, UidProto.AUDIO, u.getAudioTurnedOnTimer(), rawRealtimeUs, which);
7222
7223 // Bluetooth Controller (BLUETOOTH_CONTROLLER_DATA)
7224 dumpControllerActivityProto(proto, UidProto.BLUETOOTH_CONTROLLER,
7225 u.getBluetoothControllerActivity(), which);
7226
7227 // BLE scans (BLUETOOTH_MISC_DATA) (uses totalDurationMsLocked and MaxDurationMsLocked)
7228 final Timer bleTimer = u.getBluetoothScanTimer();
7229 if (bleTimer != null) {
7230 final long bmToken = proto.start(UidProto.BLUETOOTH_MISC);
7231
7232 dumpTimer(proto, UidProto.BluetoothMisc.APPORTIONED_BLE_SCAN, bleTimer,
7233 rawRealtimeUs, which);
7234 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN,
7235 u.getBluetoothScanBackgroundTimer(), rawRealtimeUs, which);
7236 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
7237 dumpTimer(proto, UidProto.BluetoothMisc.UNOPTIMIZED_BLE_SCAN,
7238 u.getBluetoothUnoptimizedScanTimer(), rawRealtimeUs, which);
7239 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
7240 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_UNOPTIMIZED_BLE_SCAN,
7241 u.getBluetoothUnoptimizedScanBackgroundTimer(), rawRealtimeUs, which);
7242 // Result counters
7243 proto.write(UidProto.BluetoothMisc.BLE_SCAN_RESULT_COUNT,
7244 u.getBluetoothScanResultCounter() != null
7245 ? u.getBluetoothScanResultCounter().getCountLocked(which) : 0);
7246 proto.write(UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN_RESULT_COUNT,
7247 u.getBluetoothScanResultBgCounter() != null
7248 ? u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0);
7249
7250 proto.end(bmToken);
7251 }
7252
7253 // Camera (CAMERA_DATA)
7254 dumpTimer(proto, UidProto.CAMERA, u.getCameraTurnedOnTimer(), rawRealtimeUs, which);
7255
7256 // CPU stats (CPU_DATA & CPU_TIMES_AT_FREQ_DATA)
7257 final long cpuToken = proto.start(UidProto.CPU);
7258 proto.write(UidProto.Cpu.USER_DURATION_MS, roundUsToMs(u.getUserCpuTimeUs(which)));
7259 proto.write(UidProto.Cpu.SYSTEM_DURATION_MS, roundUsToMs(u.getSystemCpuTimeUs(which)));
7260
7261 final long[] cpuFreqs = getCpuFreqs();
7262 if (cpuFreqs != null) {
7263 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
7264 // If total cpuFreqTimes is null, then we don't need to check for
7265 // screenOffCpuFreqTimes.
7266 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
7267 long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
7268 if (screenOffCpuFreqTimeMs == null) {
7269 screenOffCpuFreqTimeMs = new long[cpuFreqTimeMs.length];
7270 }
7271 for (int ic = 0; ic < cpuFreqTimeMs.length; ++ic) {
7272 long cToken = proto.start(UidProto.Cpu.BY_FREQUENCY);
7273 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7274 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7275 cpuFreqTimeMs[ic]);
7276 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7277 screenOffCpuFreqTimeMs[ic]);
7278 proto.end(cToken);
7279 }
7280 }
7281 }
Sudheer Shanka6d658d72018-01-01 01:36:49 -08007282
7283 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
7284 final long[] timesMs = u.getCpuFreqTimes(which, procState);
7285 if (timesMs != null && timesMs.length == cpuFreqs.length) {
7286 long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(which, procState);
7287 if (screenOffTimesMs == null) {
7288 screenOffTimesMs = new long[timesMs.length];
7289 }
7290 final long procToken = proto.start(UidProto.Cpu.BY_PROCESS_STATE);
7291 proto.write(UidProto.Cpu.ByProcessState.PROCESS_STATE, procState);
7292 for (int ic = 0; ic < timesMs.length; ++ic) {
7293 long cToken = proto.start(UidProto.Cpu.ByProcessState.BY_FREQUENCY);
7294 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7295 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7296 timesMs[ic]);
7297 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7298 screenOffTimesMs[ic]);
7299 proto.end(cToken);
7300 }
7301 proto.end(procToken);
7302 }
7303 }
Kweku Adams103351f2017-10-16 14:39:34 -07007304 proto.end(cpuToken);
7305
7306 // Flashlight (FLASHLIGHT_DATA)
7307 dumpTimer(proto, UidProto.FLASHLIGHT, u.getFlashlightTurnedOnTimer(),
7308 rawRealtimeUs, which);
7309
7310 // Foreground activity (FOREGROUND_ACTIVITY_DATA)
7311 dumpTimer(proto, UidProto.FOREGROUND_ACTIVITY, u.getForegroundActivityTimer(),
7312 rawRealtimeUs, which);
7313
7314 // Foreground service (FOREGROUND_SERVICE_DATA)
7315 dumpTimer(proto, UidProto.FOREGROUND_SERVICE, u.getForegroundServiceTimer(),
7316 rawRealtimeUs, which);
7317
7318 // Job completion (JOB_COMPLETION_DATA)
7319 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
7320 final int[] reasons = new int[]{
7321 JobParameters.REASON_CANCELED,
7322 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED,
7323 JobParameters.REASON_PREEMPT,
7324 JobParameters.REASON_TIMEOUT,
7325 JobParameters.REASON_DEVICE_IDLE,
7326 };
7327 for (int ic = 0; ic < completions.size(); ++ic) {
7328 SparseIntArray types = completions.valueAt(ic);
7329 if (types != null) {
7330 final long jcToken = proto.start(UidProto.JOB_COMPLETION);
7331
7332 proto.write(UidProto.JobCompletion.NAME, completions.keyAt(ic));
7333
7334 for (int r : reasons) {
7335 long rToken = proto.start(UidProto.JobCompletion.REASON_COUNT);
7336 proto.write(UidProto.JobCompletion.ReasonCount.NAME, r);
7337 proto.write(UidProto.JobCompletion.ReasonCount.COUNT, types.get(r, 0));
7338 proto.end(rToken);
7339 }
7340
7341 proto.end(jcToken);
7342 }
7343 }
7344
7345 // Scheduled jobs (JOB_DATA)
7346 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
7347 for (int ij = jobs.size() - 1; ij >= 0; --ij) {
7348 final Timer timer = jobs.valueAt(ij);
7349 final Timer bgTimer = timer.getSubTimer();
7350 final long jToken = proto.start(UidProto.JOBS);
7351
7352 proto.write(UidProto.Job.NAME, jobs.keyAt(ij));
7353 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7354 dumpTimer(proto, UidProto.Job.TOTAL, timer, rawRealtimeUs, which);
7355 dumpTimer(proto, UidProto.Job.BACKGROUND, bgTimer, rawRealtimeUs, which);
7356
7357 proto.end(jToken);
7358 }
7359
7360 // Modem Controller (MODEM_CONTROLLER_DATA)
7361 dumpControllerActivityProto(proto, UidProto.MODEM_CONTROLLER,
7362 u.getModemControllerActivity(), which);
7363
7364 // Network stats (NETWORK_DATA)
7365 final long nToken = proto.start(UidProto.NETWORK);
7366 proto.write(UidProto.Network.MOBILE_BYTES_RX,
7367 u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7368 proto.write(UidProto.Network.MOBILE_BYTES_TX,
7369 u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7370 proto.write(UidProto.Network.WIFI_BYTES_RX,
7371 u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7372 proto.write(UidProto.Network.WIFI_BYTES_TX,
7373 u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7374 proto.write(UidProto.Network.BT_BYTES_RX,
7375 u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7376 proto.write(UidProto.Network.BT_BYTES_TX,
7377 u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
7378 proto.write(UidProto.Network.MOBILE_PACKETS_RX,
7379 u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7380 proto.write(UidProto.Network.MOBILE_PACKETS_TX,
7381 u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7382 proto.write(UidProto.Network.WIFI_PACKETS_RX,
7383 u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7384 proto.write(UidProto.Network.WIFI_PACKETS_TX,
7385 u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7386 proto.write(UidProto.Network.MOBILE_ACTIVE_DURATION_MS,
7387 roundUsToMs(u.getMobileRadioActiveTime(which)));
7388 proto.write(UidProto.Network.MOBILE_ACTIVE_COUNT,
7389 u.getMobileRadioActiveCount(which));
7390 proto.write(UidProto.Network.MOBILE_WAKEUP_COUNT,
7391 u.getMobileRadioApWakeupCount(which));
7392 proto.write(UidProto.Network.WIFI_WAKEUP_COUNT,
7393 u.getWifiRadioApWakeupCount(which));
7394 proto.write(UidProto.Network.MOBILE_BYTES_BG_RX,
7395 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA, which));
7396 proto.write(UidProto.Network.MOBILE_BYTES_BG_TX,
7397 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA, which));
7398 proto.write(UidProto.Network.WIFI_BYTES_BG_RX,
7399 u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which));
7400 proto.write(UidProto.Network.WIFI_BYTES_BG_TX,
7401 u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which));
7402 proto.write(UidProto.Network.MOBILE_PACKETS_BG_RX,
7403 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA, which));
7404 proto.write(UidProto.Network.MOBILE_PACKETS_BG_TX,
7405 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA, which));
7406 proto.write(UidProto.Network.WIFI_PACKETS_BG_RX,
7407 u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA, which));
7408 proto.write(UidProto.Network.WIFI_PACKETS_BG_TX,
7409 u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA, which));
7410 proto.end(nToken);
7411
7412 // Power use item (POWER_USE_ITEM_DATA)
7413 BatterySipper bs = uidToSipper.get(uid);
7414 if (bs != null) {
7415 final long bsToken = proto.start(UidProto.POWER_USE_ITEM);
7416 proto.write(UidProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7417 proto.write(UidProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7418 proto.write(UidProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7419 proto.write(UidProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7420 bs.proportionalSmearMah);
7421 proto.end(bsToken);
7422 }
7423
7424 // Processes (PROCESS_DATA)
7425 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats =
7426 u.getProcessStats();
7427 for (int ipr = processStats.size() - 1; ipr >= 0; --ipr) {
7428 final Uid.Proc ps = processStats.valueAt(ipr);
7429 final long prToken = proto.start(UidProto.PROCESS);
7430
7431 proto.write(UidProto.Process.NAME, processStats.keyAt(ipr));
7432 proto.write(UidProto.Process.USER_DURATION_MS, ps.getUserTime(which));
7433 proto.write(UidProto.Process.SYSTEM_DURATION_MS, ps.getSystemTime(which));
7434 proto.write(UidProto.Process.FOREGROUND_DURATION_MS, ps.getForegroundTime(which));
7435 proto.write(UidProto.Process.START_COUNT, ps.getStarts(which));
7436 proto.write(UidProto.Process.ANR_COUNT, ps.getNumAnrs(which));
7437 proto.write(UidProto.Process.CRASH_COUNT, ps.getNumCrashes(which));
7438
7439 proto.end(prToken);
7440 }
7441
7442 // Sensors (SENSOR_DATA)
7443 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
7444 for (int ise = 0; ise < sensors.size(); ++ise) {
7445 final Uid.Sensor se = sensors.valueAt(ise);
7446 final Timer timer = se.getSensorTime();
7447 if (timer == null) {
7448 continue;
7449 }
7450 final Timer bgTimer = se.getSensorBackgroundTime();
7451 final int sensorNumber = sensors.keyAt(ise);
7452 final long seToken = proto.start(UidProto.SENSORS);
7453
7454 proto.write(UidProto.Sensor.ID, sensorNumber);
7455 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7456 dumpTimer(proto, UidProto.Sensor.APPORTIONED, timer, rawRealtimeUs, which);
7457 dumpTimer(proto, UidProto.Sensor.BACKGROUND, bgTimer, rawRealtimeUs, which);
7458
7459 proto.end(seToken);
7460 }
7461
7462 // State times (STATE_TIME_DATA)
7463 for (int ips = 0; ips < Uid.NUM_PROCESS_STATE; ++ips) {
7464 long durMs = roundUsToMs(u.getProcessStateTime(ips, rawRealtimeUs, which));
7465 if (durMs == 0) {
7466 continue;
7467 }
7468 final long stToken = proto.start(UidProto.STATES);
7469 proto.write(UidProto.StateTime.STATE, ips);
7470 proto.write(UidProto.StateTime.DURATION_MS, durMs);
7471 proto.end(stToken);
7472 }
7473
7474 // Syncs (SYNC_DATA)
7475 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
7476 for (int isy = syncs.size() - 1; isy >= 0; --isy) {
7477 final Timer timer = syncs.valueAt(isy);
7478 final Timer bgTimer = timer.getSubTimer();
7479 final long syToken = proto.start(UidProto.SYNCS);
7480
7481 proto.write(UidProto.Sync.NAME, syncs.keyAt(isy));
7482 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7483 dumpTimer(proto, UidProto.Sync.TOTAL, timer, rawRealtimeUs, which);
7484 dumpTimer(proto, UidProto.Sync.BACKGROUND, bgTimer, rawRealtimeUs, which);
7485
7486 proto.end(syToken);
7487 }
7488
7489 // User activity (USER_ACTIVITY_DATA)
7490 if (u.hasUserActivity()) {
7491 for (int i = 0; i < Uid.NUM_USER_ACTIVITY_TYPES; ++i) {
7492 int val = u.getUserActivityCount(i, which);
7493 if (val != 0) {
7494 final long uaToken = proto.start(UidProto.USER_ACTIVITY);
7495 proto.write(UidProto.UserActivity.NAME, i);
7496 proto.write(UidProto.UserActivity.COUNT, val);
7497 proto.end(uaToken);
7498 }
7499 }
7500 }
7501
7502 // Vibrator (VIBRATOR_DATA)
7503 dumpTimer(proto, UidProto.VIBRATOR, u.getVibratorOnTimer(), rawRealtimeUs, which);
7504
7505 // Video (VIDEO_DATA)
7506 dumpTimer(proto, UidProto.VIDEO, u.getVideoTurnedOnTimer(), rawRealtimeUs, which);
7507
7508 // Wakelocks (WAKELOCK_DATA)
7509 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
7510 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7511 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7512 final long wToken = proto.start(UidProto.WAKELOCKS);
7513 proto.write(UidProto.Wakelock.NAME, wakelocks.keyAt(iw));
7514 dumpTimer(proto, UidProto.Wakelock.FULL, wl.getWakeTime(WAKE_TYPE_FULL),
7515 rawRealtimeUs, which);
7516 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7517 if (pTimer != null) {
7518 dumpTimer(proto, UidProto.Wakelock.PARTIAL, pTimer, rawRealtimeUs, which);
7519 dumpTimer(proto, UidProto.Wakelock.BACKGROUND_PARTIAL, pTimer.getSubTimer(),
7520 rawRealtimeUs, which);
7521 }
7522 dumpTimer(proto, UidProto.Wakelock.WINDOW, wl.getWakeTime(WAKE_TYPE_WINDOW),
7523 rawRealtimeUs, which);
7524 proto.end(wToken);
7525 }
7526
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007527 // Wifi Multicast Wakelock (WIFI_MULTICAST_WAKELOCK_DATA)
7528 dumpTimer(proto, UidProto.WIFI_MULTICAST_WAKELOCK, u.getMulticastWakelockStats(),
7529 rawRealtimeUs, which);
7530
Kweku Adams103351f2017-10-16 14:39:34 -07007531 // Wakeup alarms (WAKEUP_ALARM_DATA)
7532 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7533 final Uid.Pkg ps = packageStats.valueAt(ipkg);
7534 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
7535 for (int iwa = alarms.size() - 1; iwa >= 0; --iwa) {
7536 final long waToken = proto.start(UidProto.WAKEUP_ALARM);
7537 proto.write(UidProto.WakeupAlarm.NAME, alarms.keyAt(iwa));
7538 proto.write(UidProto.WakeupAlarm.COUNT,
7539 alarms.valueAt(iwa).getCountLocked(which));
7540 proto.end(waToken);
7541 }
7542 }
7543
7544 // Wifi Controller (WIFI_CONTROLLER_DATA)
7545 dumpControllerActivityProto(proto, UidProto.WIFI_CONTROLLER,
7546 u.getWifiControllerActivity(), which);
7547
7548 // Wifi data (WIFI_DATA)
7549 final long wToken = proto.start(UidProto.WIFI);
7550 proto.write(UidProto.Wifi.FULL_WIFI_LOCK_DURATION_MS,
7551 roundUsToMs(u.getFullWifiLockTime(rawRealtimeUs, which)));
7552 dumpTimer(proto, UidProto.Wifi.APPORTIONED_SCAN, u.getWifiScanTimer(),
7553 rawRealtimeUs, which);
7554 proto.write(UidProto.Wifi.RUNNING_DURATION_MS,
7555 roundUsToMs(u.getWifiRunningTime(rawRealtimeUs, which)));
7556 dumpTimer(proto, UidProto.Wifi.BACKGROUND_SCAN, u.getWifiScanBackgroundTimer(),
7557 rawRealtimeUs, which);
7558 proto.end(wToken);
7559
7560 proto.end(uTkn);
7561 }
7562 }
7563
Kweku Adams71a95312018-04-16 16:54:24 -07007564 private void dumpProtoHistoryLocked(ProtoOutputStream proto, int flags, long histStart) {
7565 if (!startIteratingHistoryLocked()) {
7566 return;
7567 }
7568
7569 proto.write(BatteryStatsServiceDumpHistoryProto.REPORT_VERSION, CHECKIN_VERSION);
7570 proto.write(BatteryStatsServiceDumpHistoryProto.PARCEL_VERSION, getParcelVersion());
7571 proto.write(BatteryStatsServiceDumpHistoryProto.START_PLATFORM_VERSION,
7572 getStartPlatformVersion());
7573 proto.write(BatteryStatsServiceDumpHistoryProto.END_PLATFORM_VERSION,
7574 getEndPlatformVersion());
7575 try {
7576 long token;
7577 // History string pool (HISTORY_STRING_POOL)
7578 for (int i = 0; i < getHistoryStringPoolSize(); ++i) {
7579 token = proto.start(BatteryStatsServiceDumpHistoryProto.KEYS);
7580 proto.write(BatteryStatsServiceDumpHistoryProto.Key.INDEX, i);
7581 proto.write(BatteryStatsServiceDumpHistoryProto.Key.UID, getHistoryTagPoolUid(i));
7582 proto.write(BatteryStatsServiceDumpHistoryProto.Key.TAG,
7583 getHistoryTagPoolString(i));
7584 proto.end(token);
7585 }
7586
7587 // History data (HISTORY_DATA)
7588 final HistoryPrinter hprinter = new HistoryPrinter();
7589 final HistoryItem rec = new HistoryItem();
7590 long lastTime = -1;
7591 long baseTime = -1;
7592 boolean printed = false;
7593 HistoryEventTracker tracker = null;
7594 while (getNextHistoryLocked(rec)) {
7595 lastTime = rec.time;
7596 if (baseTime < 0) {
7597 baseTime = lastTime;
7598 }
7599 if (rec.time >= histStart) {
7600 if (histStart >= 0 && !printed) {
7601 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
7602 || rec.cmd == HistoryItem.CMD_RESET
7603 || rec.cmd == HistoryItem.CMD_START
7604 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
7605 printed = true;
7606 hprinter.printNextItem(proto, rec, baseTime,
7607 (flags & DUMP_VERBOSE) != 0);
7608 rec.cmd = HistoryItem.CMD_UPDATE;
7609 } else if (rec.currentTime != 0) {
7610 printed = true;
7611 byte cmd = rec.cmd;
7612 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
7613 hprinter.printNextItem(proto, rec, baseTime,
7614 (flags & DUMP_VERBOSE) != 0);
7615 rec.cmd = cmd;
7616 }
7617 if (tracker != null) {
7618 if (rec.cmd != HistoryItem.CMD_UPDATE) {
7619 hprinter.printNextItem(proto, rec, baseTime,
7620 (flags & DUMP_VERBOSE) != 0);
7621 rec.cmd = HistoryItem.CMD_UPDATE;
7622 }
7623 int oldEventCode = rec.eventCode;
7624 HistoryTag oldEventTag = rec.eventTag;
7625 rec.eventTag = new HistoryTag();
7626 for (int i = 0; i < HistoryItem.EVENT_COUNT; i++) {
7627 HashMap<String, SparseIntArray> active =
7628 tracker.getStateForEvent(i);
7629 if (active == null) {
7630 continue;
7631 }
7632 for (HashMap.Entry<String, SparseIntArray> ent
7633 : active.entrySet()) {
7634 SparseIntArray uids = ent.getValue();
7635 for (int j = 0; j < uids.size(); j++) {
7636 rec.eventCode = i;
7637 rec.eventTag.string = ent.getKey();
7638 rec.eventTag.uid = uids.keyAt(j);
7639 rec.eventTag.poolIdx = uids.valueAt(j);
7640 hprinter.printNextItem(proto, rec, baseTime,
7641 (flags & DUMP_VERBOSE) != 0);
7642 rec.wakeReasonTag = null;
7643 rec.wakelockTag = null;
7644 }
7645 }
7646 }
7647 rec.eventCode = oldEventCode;
7648 rec.eventTag = oldEventTag;
7649 tracker = null;
7650 }
7651 }
7652 hprinter.printNextItem(proto, rec, baseTime,
7653 (flags & DUMP_VERBOSE) != 0);
7654 }
7655 }
7656 if (histStart >= 0) {
7657 commitCurrentHistoryBatchLocked();
7658 proto.write(BatteryStatsServiceDumpHistoryProto.CSV_LINES,
7659 "NEXT: " + (lastTime + 1));
7660 }
7661 } finally {
7662 finishIteratingHistoryLocked();
7663 }
7664 }
7665
Kweku Adams103351f2017-10-16 14:39:34 -07007666 private void dumpProtoSystemLocked(ProtoOutputStream proto, BatteryStatsHelper helper) {
Kweku Adams87b19ec2017-10-09 12:40:03 -07007667 final long sToken = proto.start(BatteryStatsProto.SYSTEM);
7668 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7669 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7670 final long rawRealtimeUs = rawRealtimeMs * 1000;
7671 final int which = STATS_SINCE_CHARGED;
7672
7673 // Battery data (BATTERY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007674 final long bToken = proto.start(SystemProto.BATTERY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007675 proto.write(SystemProto.Battery.START_CLOCK_TIME_MS, getStartClockTime());
7676 proto.write(SystemProto.Battery.START_COUNT, getStartCount());
7677 proto.write(SystemProto.Battery.TOTAL_REALTIME_MS,
7678 computeRealtime(rawRealtimeUs, which) / 1000);
7679 proto.write(SystemProto.Battery.TOTAL_UPTIME_MS,
7680 computeUptime(rawUptimeUs, which) / 1000);
7681 proto.write(SystemProto.Battery.BATTERY_REALTIME_MS,
7682 computeBatteryRealtime(rawRealtimeUs, which) / 1000);
7683 proto.write(SystemProto.Battery.BATTERY_UPTIME_MS,
7684 computeBatteryUptime(rawUptimeUs, which) / 1000);
7685 proto.write(SystemProto.Battery.SCREEN_OFF_REALTIME_MS,
7686 computeBatteryScreenOffRealtime(rawRealtimeUs, which) / 1000);
7687 proto.write(SystemProto.Battery.SCREEN_OFF_UPTIME_MS,
7688 computeBatteryScreenOffUptime(rawUptimeUs, which) / 1000);
7689 proto.write(SystemProto.Battery.SCREEN_DOZE_DURATION_MS,
7690 getScreenDozeTime(rawRealtimeUs, which) / 1000);
7691 proto.write(SystemProto.Battery.ESTIMATED_BATTERY_CAPACITY_MAH,
7692 getEstimatedBatteryCapacity());
7693 proto.write(SystemProto.Battery.MIN_LEARNED_BATTERY_CAPACITY_UAH,
7694 getMinLearnedBatteryCapacity());
7695 proto.write(SystemProto.Battery.MAX_LEARNED_BATTERY_CAPACITY_UAH,
7696 getMaxLearnedBatteryCapacity());
Kweku Adams103351f2017-10-16 14:39:34 -07007697 proto.end(bToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007698
7699 // Battery discharge (BATTERY_DISCHARGE_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007700 final long bdToken = proto.start(SystemProto.BATTERY_DISCHARGE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007701 proto.write(SystemProto.BatteryDischarge.LOWER_BOUND_SINCE_CHARGE,
7702 getLowDischargeAmountSinceCharge());
7703 proto.write(SystemProto.BatteryDischarge.UPPER_BOUND_SINCE_CHARGE,
7704 getHighDischargeAmountSinceCharge());
7705 proto.write(SystemProto.BatteryDischarge.SCREEN_ON_SINCE_CHARGE,
7706 getDischargeAmountScreenOnSinceCharge());
7707 proto.write(SystemProto.BatteryDischarge.SCREEN_OFF_SINCE_CHARGE,
7708 getDischargeAmountScreenOffSinceCharge());
7709 proto.write(SystemProto.BatteryDischarge.SCREEN_DOZE_SINCE_CHARGE,
7710 getDischargeAmountScreenDozeSinceCharge());
7711 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH,
7712 getUahDischarge(which) / 1000);
7713 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_OFF,
7714 getUahDischargeScreenOff(which) / 1000);
7715 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_DOZE,
7716 getUahDischargeScreenDoze(which) / 1000);
Mike Ma15313c92017-11-15 17:58:21 -08007717 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_LIGHT_DOZE,
7718 getUahDischargeLightDoze(which) / 1000);
7719 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_DEEP_DOZE,
7720 getUahDischargeDeepDoze(which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007721 proto.end(bdToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007722
7723 // Time remaining
7724 long timeRemainingUs = computeChargeTimeRemaining(rawRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -07007725 // These are part of a oneof, so we should only set one of them.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007726 if (timeRemainingUs >= 0) {
7727 // Charge time remaining (CHARGE_TIME_REMAIN_DATA)
7728 proto.write(SystemProto.CHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7729 } else {
7730 timeRemainingUs = computeBatteryTimeRemaining(rawRealtimeUs);
7731 // Discharge time remaining (DISCHARGE_TIME_REMAIN_DATA)
7732 if (timeRemainingUs >= 0) {
7733 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7734 } else {
7735 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, -1);
7736 }
7737 }
7738
7739 // Charge step (CHARGE_STEP_DATA)
7740 dumpDurationSteps(proto, SystemProto.CHARGE_STEP, getChargeLevelStepTracker());
7741
7742 // Phone data connection (DATA_CONNECTION_TIME_DATA and DATA_CONNECTION_COUNT_DATA)
7743 for (int i = 0; i < NUM_DATA_CONNECTION_TYPES; ++i) {
Tej Singheee317b2018-03-07 19:28:05 -08007744 // Map OTHER to TelephonyManager.NETWORK_TYPE_UNKNOWN and mark NONE as a boolean.
7745 boolean isNone = (i == DATA_CONNECTION_NONE);
7746 int telephonyNetworkType = i;
7747 if (i == DATA_CONNECTION_OTHER) {
7748 telephonyNetworkType = TelephonyManager.NETWORK_TYPE_UNKNOWN;
7749 }
Kweku Adams103351f2017-10-16 14:39:34 -07007750 final long pdcToken = proto.start(SystemProto.DATA_CONNECTION);
Tej Singheee317b2018-03-07 19:28:05 -08007751 if (isNone) {
7752 proto.write(SystemProto.DataConnection.IS_NONE, isNone);
7753 } else {
7754 proto.write(SystemProto.DataConnection.NAME, telephonyNetworkType);
7755 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07007756 dumpTimer(proto, SystemProto.DataConnection.TOTAL, getPhoneDataConnectionTimer(i),
7757 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007758 proto.end(pdcToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007759 }
7760
7761 // Discharge step (DISCHARGE_STEP_DATA)
7762 dumpDurationSteps(proto, SystemProto.DISCHARGE_STEP, getDischargeLevelStepTracker());
7763
7764 // CPU frequencies (GLOBAL_CPU_FREQ_DATA)
7765 final long[] cpuFreqs = getCpuFreqs();
7766 if (cpuFreqs != null) {
7767 for (long i : cpuFreqs) {
7768 proto.write(SystemProto.CPU_FREQUENCY, i);
7769 }
7770 }
7771
7772 // Bluetooth controller (GLOBAL_BLUETOOTH_CONTROLLER_DATA)
7773 dumpControllerActivityProto(proto, SystemProto.GLOBAL_BLUETOOTH_CONTROLLER,
7774 getBluetoothControllerActivity(), which);
7775
7776 // Modem controller (GLOBAL_MODEM_CONTROLLER_DATA)
7777 dumpControllerActivityProto(proto, SystemProto.GLOBAL_MODEM_CONTROLLER,
7778 getModemControllerActivity(), which);
7779
7780 // Global network data (GLOBAL_NETWORK_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007781 final long gnToken = proto.start(SystemProto.GLOBAL_NETWORK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007782 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_RX,
7783 getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7784 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_TX,
7785 getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7786 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_RX,
7787 getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7788 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_TX,
7789 getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7790 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_RX,
7791 getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7792 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_TX,
7793 getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7794 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_RX,
7795 getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7796 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_TX,
7797 getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7798 proto.write(SystemProto.GlobalNetwork.BT_BYTES_RX,
7799 getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7800 proto.write(SystemProto.GlobalNetwork.BT_BYTES_TX,
7801 getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
Kweku Adams103351f2017-10-16 14:39:34 -07007802 proto.end(gnToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007803
7804 // Wifi controller (GLOBAL_WIFI_CONTROLLER_DATA)
7805 dumpControllerActivityProto(proto, SystemProto.GLOBAL_WIFI_CONTROLLER,
7806 getWifiControllerActivity(), which);
7807
7808
7809 // Global wifi (GLOBAL_WIFI_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007810 final long gwToken = proto.start(SystemProto.GLOBAL_WIFI);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007811 proto.write(SystemProto.GlobalWifi.ON_DURATION_MS,
7812 getWifiOnTime(rawRealtimeUs, which) / 1000);
7813 proto.write(SystemProto.GlobalWifi.RUNNING_DURATION_MS,
7814 getGlobalWifiRunningTime(rawRealtimeUs, which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007815 proto.end(gwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007816
7817 // Kernel wakelock (KERNEL_WAKELOCK_DATA)
7818 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
7819 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007820 final long kwToken = proto.start(SystemProto.KERNEL_WAKELOCK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007821 proto.write(SystemProto.KernelWakelock.NAME, ent.getKey());
7822 dumpTimer(proto, SystemProto.KernelWakelock.TOTAL, ent.getValue(),
7823 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007824 proto.end(kwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007825 }
7826
7827 // Misc (MISC_DATA)
7828 // Calculate wakelock times across all uids.
7829 long fullWakeLockTimeTotalUs = 0;
7830 long partialWakeLockTimeTotalUs = 0;
7831
7832 final SparseArray<? extends Uid> uidStats = getUidStats();
7833 for (int iu = 0; iu < uidStats.size(); iu++) {
7834 final Uid u = uidStats.valueAt(iu);
7835
7836 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks =
7837 u.getWakelockStats();
7838 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7839 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7840
7841 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
7842 if (fullWakeTimer != null) {
7843 fullWakeLockTimeTotalUs += fullWakeTimer.getTotalTimeLocked(rawRealtimeUs,
7844 which);
7845 }
7846
7847 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7848 if (partialWakeTimer != null) {
7849 partialWakeLockTimeTotalUs += partialWakeTimer.getTotalTimeLocked(
7850 rawRealtimeUs, which);
7851 }
7852 }
7853 }
Kweku Adams103351f2017-10-16 14:39:34 -07007854 final long mToken = proto.start(SystemProto.MISC);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007855 proto.write(SystemProto.Misc.SCREEN_ON_DURATION_MS,
7856 getScreenOnTime(rawRealtimeUs, which) / 1000);
7857 proto.write(SystemProto.Misc.PHONE_ON_DURATION_MS,
7858 getPhoneOnTime(rawRealtimeUs, which) / 1000);
7859 proto.write(SystemProto.Misc.FULL_WAKELOCK_TOTAL_DURATION_MS,
7860 fullWakeLockTimeTotalUs / 1000);
7861 proto.write(SystemProto.Misc.PARTIAL_WAKELOCK_TOTAL_DURATION_MS,
7862 partialWakeLockTimeTotalUs / 1000);
7863 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_DURATION_MS,
7864 getMobileRadioActiveTime(rawRealtimeUs, which) / 1000);
7865 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_ADJUSTED_TIME_MS,
7866 getMobileRadioActiveAdjustedTime(which) / 1000);
7867 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_COUNT,
7868 getMobileRadioActiveCount(which));
7869 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_UNKNOWN_DURATION_MS,
7870 getMobileRadioActiveUnknownTime(which) / 1000);
7871 proto.write(SystemProto.Misc.INTERACTIVE_DURATION_MS,
7872 getInteractiveTime(rawRealtimeUs, which) / 1000);
7873 proto.write(SystemProto.Misc.BATTERY_SAVER_MODE_ENABLED_DURATION_MS,
7874 getPowerSaveModeEnabledTime(rawRealtimeUs, which) / 1000);
7875 proto.write(SystemProto.Misc.NUM_CONNECTIVITY_CHANGES,
7876 getNumConnectivityChange(which));
7877 proto.write(SystemProto.Misc.DEEP_DOZE_ENABLED_DURATION_MS,
7878 getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7879 proto.write(SystemProto.Misc.DEEP_DOZE_COUNT,
7880 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
7881 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_DURATION_MS,
7882 getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7883 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_COUNT,
7884 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
7885 proto.write(SystemProto.Misc.LONGEST_DEEP_DOZE_DURATION_MS,
7886 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
7887 proto.write(SystemProto.Misc.LIGHT_DOZE_ENABLED_DURATION_MS,
7888 getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7889 proto.write(SystemProto.Misc.LIGHT_DOZE_COUNT,
7890 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
7891 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_DURATION_MS,
7892 getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7893 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_COUNT,
7894 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
7895 proto.write(SystemProto.Misc.LONGEST_LIGHT_DOZE_DURATION_MS,
7896 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
Kweku Adams103351f2017-10-16 14:39:34 -07007897 proto.end(mToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007898
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007899 // Wifi multicast wakelock total stats (WIFI_MULTICAST_WAKELOCK_TOTAL_DATA)
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08007900 final long multicastWakeLockTimeTotalUs =
7901 getWifiMulticastWakelockTime(rawRealtimeUs, which);
7902 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007903 final long wmctToken = proto.start(SystemProto.WIFI_MULTICAST_WAKELOCK_TOTAL);
7904 proto.write(SystemProto.WifiMulticastWakelockTotal.DURATION_MS,
7905 multicastWakeLockTimeTotalUs / 1000);
7906 proto.write(SystemProto.WifiMulticastWakelockTotal.COUNT,
7907 multicastWakeLockCountTotal);
7908 proto.end(wmctToken);
7909
Kweku Adams87b19ec2017-10-09 12:40:03 -07007910 // Power use item (POWER_USE_ITEM_DATA)
7911 final List<BatterySipper> sippers = helper.getUsageList();
7912 if (sippers != null) {
7913 for (int i = 0; i < sippers.size(); ++i) {
7914 final BatterySipper bs = sippers.get(i);
7915 int n = SystemProto.PowerUseItem.UNKNOWN_SIPPER;
7916 int uid = 0;
7917 switch (bs.drainType) {
Kweku Adamsef728952018-04-10 17:56:16 -07007918 case AMBIENT_DISPLAY:
7919 n = SystemProto.PowerUseItem.AMBIENT_DISPLAY;
7920 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07007921 case IDLE:
7922 n = SystemProto.PowerUseItem.IDLE;
7923 break;
7924 case CELL:
7925 n = SystemProto.PowerUseItem.CELL;
7926 break;
7927 case PHONE:
7928 n = SystemProto.PowerUseItem.PHONE;
7929 break;
7930 case WIFI:
7931 n = SystemProto.PowerUseItem.WIFI;
7932 break;
7933 case BLUETOOTH:
7934 n = SystemProto.PowerUseItem.BLUETOOTH;
7935 break;
7936 case SCREEN:
7937 n = SystemProto.PowerUseItem.SCREEN;
7938 break;
7939 case FLASHLIGHT:
7940 n = SystemProto.PowerUseItem.FLASHLIGHT;
7941 break;
7942 case APP:
Kweku Adams103351f2017-10-16 14:39:34 -07007943 // dumpProtoAppsLocked will handle this.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007944 continue;
7945 case USER:
7946 n = SystemProto.PowerUseItem.USER;
7947 uid = UserHandle.getUid(bs.userId, 0);
7948 break;
7949 case UNACCOUNTED:
7950 n = SystemProto.PowerUseItem.UNACCOUNTED;
7951 break;
7952 case OVERCOUNTED:
7953 n = SystemProto.PowerUseItem.OVERCOUNTED;
7954 break;
7955 case CAMERA:
7956 n = SystemProto.PowerUseItem.CAMERA;
7957 break;
7958 case MEMORY:
7959 n = SystemProto.PowerUseItem.MEMORY;
7960 break;
7961 }
Kweku Adams103351f2017-10-16 14:39:34 -07007962 final long puiToken = proto.start(SystemProto.POWER_USE_ITEM);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007963 proto.write(SystemProto.PowerUseItem.NAME, n);
7964 proto.write(SystemProto.PowerUseItem.UID, uid);
7965 proto.write(SystemProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7966 proto.write(SystemProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7967 proto.write(SystemProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7968 proto.write(SystemProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7969 bs.proportionalSmearMah);
Kweku Adams103351f2017-10-16 14:39:34 -07007970 proto.end(puiToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007971 }
7972 }
7973
7974 // Power use summary (POWER_USE_SUMMARY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007975 final long pusToken = proto.start(SystemProto.POWER_USE_SUMMARY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007976 proto.write(SystemProto.PowerUseSummary.BATTERY_CAPACITY_MAH,
7977 helper.getPowerProfile().getBatteryCapacity());
7978 proto.write(SystemProto.PowerUseSummary.COMPUTED_POWER_MAH, helper.getComputedPower());
7979 proto.write(SystemProto.PowerUseSummary.MIN_DRAINED_POWER_MAH, helper.getMinDrainedPower());
7980 proto.write(SystemProto.PowerUseSummary.MAX_DRAINED_POWER_MAH, helper.getMaxDrainedPower());
Kweku Adams103351f2017-10-16 14:39:34 -07007981 proto.end(pusToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007982
7983 // RPM stats (RESOURCE_POWER_MANAGER_DATA)
7984 final Map<String, ? extends Timer> rpmStats = getRpmStats();
7985 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
7986 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007987 final long rpmToken = proto.start(SystemProto.RESOURCE_POWER_MANAGER);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007988 proto.write(SystemProto.ResourcePowerManager.NAME, ent.getKey());
7989 dumpTimer(proto, SystemProto.ResourcePowerManager.TOTAL,
7990 ent.getValue(), rawRealtimeUs, which);
7991 dumpTimer(proto, SystemProto.ResourcePowerManager.SCREEN_OFF,
7992 screenOffRpmStats.get(ent.getKey()), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007993 proto.end(rpmToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007994 }
7995
7996 // Screen brightness (SCREEN_BRIGHTNESS_DATA)
7997 for (int i = 0; i < NUM_SCREEN_BRIGHTNESS_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007998 final long sbToken = proto.start(SystemProto.SCREEN_BRIGHTNESS);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007999 proto.write(SystemProto.ScreenBrightness.NAME, i);
8000 dumpTimer(proto, SystemProto.ScreenBrightness.TOTAL, getScreenBrightnessTimer(i),
8001 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008002 proto.end(sbToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008003 }
8004
8005 // Signal scanning time (SIGNAL_SCANNING_TIME_DATA)
8006 dumpTimer(proto, SystemProto.SIGNAL_SCANNING, getPhoneSignalScanningTimer(), rawRealtimeUs,
8007 which);
8008
8009 // Phone signal strength (SIGNAL_STRENGTH_TIME_DATA and SIGNAL_STRENGTH_COUNT_DATA)
8010 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008011 final long pssToken = proto.start(SystemProto.PHONE_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008012 proto.write(SystemProto.PhoneSignalStrength.NAME, i);
8013 dumpTimer(proto, SystemProto.PhoneSignalStrength.TOTAL, getPhoneSignalStrengthTimer(i),
8014 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008015 proto.end(pssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008016 }
8017
8018 // Wakeup reasons (WAKEUP_REASON_DATA)
8019 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
8020 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07008021 final long wrToken = proto.start(SystemProto.WAKEUP_REASON);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008022 proto.write(SystemProto.WakeupReason.NAME, ent.getKey());
8023 dumpTimer(proto, SystemProto.WakeupReason.TOTAL, ent.getValue(), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008024 proto.end(wrToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008025 }
8026
8027 // Wifi signal strength (WIFI_SIGNAL_STRENGTH_TIME_DATA and WIFI_SIGNAL_STRENGTH_COUNT_DATA)
8028 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008029 final long wssToken = proto.start(SystemProto.WIFI_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008030 proto.write(SystemProto.WifiSignalStrength.NAME, i);
8031 dumpTimer(proto, SystemProto.WifiSignalStrength.TOTAL, getWifiSignalStrengthTimer(i),
8032 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008033 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008034 }
8035
8036 // Wifi state (WIFI_STATE_TIME_DATA and WIFI_STATE_COUNT_DATA)
8037 for (int i = 0; i < NUM_WIFI_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008038 final long wsToken = proto.start(SystemProto.WIFI_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008039 proto.write(SystemProto.WifiState.NAME, i);
8040 dumpTimer(proto, SystemProto.WifiState.TOTAL, getWifiStateTimer(i),
8041 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008042 proto.end(wsToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008043 }
8044
8045 // Wifi supplicant state (WIFI_SUPPL_STATE_TIME_DATA and WIFI_SUPPL_STATE_COUNT_DATA)
8046 for (int i = 0; i < NUM_WIFI_SUPPL_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008047 final long wssToken = proto.start(SystemProto.WIFI_SUPPLICANT_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008048 proto.write(SystemProto.WifiSupplicantState.NAME, i);
8049 dumpTimer(proto, SystemProto.WifiSupplicantState.TOTAL, getWifiSupplStateTimer(i),
8050 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008051 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008052 }
8053
8054 proto.end(sToken);
8055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008056}