blob: ecd16dd1f612d3fdbcaec789009dde62b7eabfd2 [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.os;
18
Amith Yamasanif235d0b2019-03-20 22:49:43 -070019import static android.app.ActivityManager.PROCESS_STATE_BOUND_TOP;
Amith Yamasania0a30a12019-01-22 11:38:06 -080020import static android.app.ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE_LOCATION;
21
Andrei Onea24ec3212019-03-15 17:35:05 +000022import android.annotation.UnsupportedAppUsage;
Sudheer Shankab2f83c12017-11-13 19:25:01 -080023import android.app.ActivityManager;
Dianne Hackborn94326cb2017-06-28 16:17:20 -070024import android.app.job.JobParameters;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080025import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080026import android.content.pm.ApplicationInfo;
Bookatz8bdae8d2018-01-16 11:24:30 -080027import android.server.ServerProtoEnums;
Kweku Adams71a95312018-04-16 16:54:24 -070028import android.service.batterystats.BatteryStatsServiceDumpHistoryProto;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070029import android.service.batterystats.BatteryStatsServiceDumpProto;
Wink Saville52840902011-02-18 12:40:47 -080030import android.telephony.SignalStrength;
Tej Singheee317b2018-03-07 19:28:05 -080031import android.telephony.TelephonyManager;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080032import android.text.format.DateFormat;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070033import android.util.ArrayMap;
James Carr2dd7e5e2016-07-20 18:48:39 -070034import android.util.LongSparseArray;
Dianne Hackborn9cfba352016-03-24 17:31:28 -070035import android.util.MutableBoolean;
36import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.util.Printer;
38import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070039import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070040import android.util.TimeUtils;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070041import android.util.proto.ProtoOutputStream;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070042import android.view.Display;
Amith Yamasaniab9ad192016-12-06 12:46:59 -080043
Sudheer Shankab2f83c12017-11-13 19:25:01 -080044import com.android.internal.annotations.VisibleForTesting;
Siddharth Ray78ccaf52017-12-23 16:16:21 -080045import com.android.internal.location.gnssmetrics.GnssMetrics;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080046import com.android.internal.os.BatterySipper;
47import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
Kweku Adams2f73ecd2017-09-27 16:59:19 -070049import java.io.FileDescriptor;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070050import java.io.PrintWriter;
Blake Kragtencb308d92019-01-28 10:54:13 -080051import java.text.DecimalFormat;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070052import java.util.ArrayList;
53import java.util.Collections;
54import java.util.Comparator;
55import java.util.Formatter;
56import java.util.HashMap;
57import java.util.List;
58import java.util.Map;
59
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060/**
61 * A class providing access to battery usage statistics, including information on
62 * wakelocks, processes, packages, and services. All times are represented in microseconds
63 * except where indicated otherwise.
64 * @hide
65 */
66public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070067 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068
69 private static final boolean LOCAL_LOGV = false;
Bookatz82b341172017-09-07 19:06:08 -070070 /** Fetching RPM stats is too slow to do each time screen changes, so disable it. */
71 protected static final boolean SCREEN_OFF_RPM_STATS_ENABLED = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070072
73 /** @hide */
74 public static final String SERVICE_NAME = "batterystats";
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 /**
77 * A constant indicating a partial wake lock timer.
78 */
Andrei Onea24ec3212019-03-15 17:35:05 +000079 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 public static final int WAKE_TYPE_PARTIAL = 0;
81
82 /**
83 * A constant indicating a full wake lock timer.
84 */
85 public static final int WAKE_TYPE_FULL = 1;
86
87 /**
88 * A constant indicating a window wake lock timer.
89 */
90 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 /**
93 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 */
95 public static final int SENSOR = 3;
Mike Mac2f518a2017-09-19 16:06:03 -070096
The Android Open Source Project10592532009-03-18 17:39:46 -070097 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070098 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070099 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700100 public static final int WIFI_RUNNING = 4;
Mike Mac2f518a2017-09-19 16:06:03 -0700101
Dianne Hackborn617f8772009-03-31 15:04:46 -0700102 /**
The Android Open Source Project10592532009-03-18 17:39:46 -0700103 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -0700104 */
Dianne Hackborn617f8772009-03-31 15:04:46 -0700105 public static final int FULL_WIFI_LOCK = 5;
Mike Mac2f518a2017-09-19 16:06:03 -0700106
The Android Open Source Project10592532009-03-18 17:39:46 -0700107 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -0700108 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -0700109 */
Nick Pelly6ccaa542012-06-15 15:22:47 -0700110 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111
Dianne Hackborn62793e42015-03-09 11:15:41 -0700112 /**
113 * A constant indicating a wifi multicast timer
114 */
115 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700118 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700119 */
120 public static final int VIDEO_TURNED_ON = 8;
121
122 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800123 * A constant indicating a vibrator on timer
124 */
125 public static final int VIBRATOR_ON = 9;
126
127 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700128 * A constant indicating a foreground activity timer
129 */
130 public static final int FOREGROUND_ACTIVITY = 10;
131
132 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700133 * A constant indicating a wifi batched scan is active
134 */
135 public static final int WIFI_BATCHED_SCAN = 11;
136
137 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700138 * A constant indicating a process state timer
139 */
140 public static final int PROCESS_STATE = 12;
141
142 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700143 * A constant indicating a sync timer
144 */
145 public static final int SYNC = 13;
146
147 /**
148 * A constant indicating a job timer
149 */
150 public static final int JOB = 14;
151
152 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800153 * A constant indicating an audio turn on timer
154 */
155 public static final int AUDIO_TURNED_ON = 15;
156
157 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700158 * A constant indicating a flashlight turn on timer
159 */
160 public static final int FLASHLIGHT_TURNED_ON = 16;
161
162 /**
163 * A constant indicating a camera turn on timer
164 */
165 public static final int CAMERA_TURNED_ON = 17;
166
167 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700168 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700169 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700170 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700171
172 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800173 * A constant indicating a bluetooth scan timer.
174 */
175 public static final int BLUETOOTH_SCAN_ON = 19;
176
177 /**
Bookatzc8c44962017-05-11 12:12:54 -0700178 * A constant indicating an aggregated partial wake lock timer.
179 */
180 public static final int AGGREGATED_WAKE_TYPE_PARTIAL = 20;
181
182 /**
Bookatzb1f04f32017-05-19 13:57:32 -0700183 * A constant indicating a bluetooth scan timer for unoptimized scans.
184 */
185 public static final int BLUETOOTH_UNOPTIMIZED_SCAN_ON = 21;
186
187 /**
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700188 * A constant indicating a foreground service timer
189 */
190 public static final int FOREGROUND_SERVICE = 22;
191
192 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -0800193 * A constant indicating an aggregate wifi multicast timer
194 */
195 public static final int WIFI_AGGREGATE_MULTICAST_ENABLED = 23;
196
197 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 * Include all of the data in the stats, including previously saved data.
199 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700200 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201
202 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 * Include only the current run in the stats.
Mike Ma99e57c32018-03-15 14:40:37 -0700204 *
205 * @deprecated As of {@link android.os.Build.VERSION_CODES#Q}, only {@link #STATS_SINCE_CHARGED}
206 * is supported.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000208 @UnsupportedAppUsage
Mike Ma99e57c32018-03-15 14:40:37 -0700209 @Deprecated
Dianne Hackborn4590e522014-03-24 13:36:46 -0700210 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211
212 /**
213 * Include only the run since the last time the device was unplugged in the stats.
Mike Ma99e57c32018-03-15 14:40:37 -0700214 *
215 * @deprecated As of {@link android.os.Build.VERSION_CODES#Q}, only {@link #STATS_SINCE_CHARGED}
216 * is supported.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 */
Mike Ma99e57c32018-03-15 14:40:37 -0700218 @Deprecated
Dianne Hackborn4590e522014-03-24 13:36:46 -0700219 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700220
221 // NOTE: Update this list if you add/change any stats above.
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700222 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700223 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700224 private static final String[] STAT_NAMES = { "l", "c", "u" };
225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700227 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700228 *
229 * New in version 19:
230 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800231 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700232 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Bookatz506a8182017-05-01 14:18:42 -0700233 * New in version 21:
234 * - Actual (not just apportioned) Wakelock time is also recorded.
Bookatzc8c44962017-05-11 12:12:54 -0700235 * - Aggregated partial wakelock time (per uid, instead of per wakelock) is recorded.
Bookatzb1f04f32017-05-19 13:57:32 -0700236 * - BLE scan result count
237 * - CPU frequency time per uid
238 * New in version 22:
239 * - BLE scan result background count, BLE unoptimized scan time
Bookatz98d4d5c2017-08-01 19:07:54 -0700240 * - Background partial wakelock time & count
241 * New in version 23:
242 * - Logging smeared power model values
243 * New in version 24:
244 * - Fixed bugs in background timers and BLE scan time
245 * New in version 25:
246 * - Package wakeup alarms are now on screen-off timebase
Bookatz50df7112017-08-04 14:53:26 -0700247 * New in version 26:
Bookatz82b341172017-09-07 19:06:08 -0700248 * - Resource power manager (rpm) states [but screenOffRpm is disabled from working properly]
Mike Mac2f518a2017-09-19 16:06:03 -0700249 * New in version 27:
250 * - Always On Display (screen doze mode) time and power
Mike Ma15313c92017-11-15 17:58:21 -0800251 * New in version 28:
252 * - Light/Deep Doze power
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700253 * - WiFi Multicast Wakelock statistics (count & duration)
Kweku Adamsa8943cb2017-12-22 13:21:06 -0800254 * New in version 29:
255 * - Process states re-ordered. TOP_SLEEPING now below BACKGROUND. HEAVY_WEIGHT introduced.
256 * - CPU times per UID process state
zhouwenjie46712bc2018-01-11 15:21:27 -0800257 * New in version 30:
258 * - Uid.PROCESS_STATE_FOREGROUND_SERVICE only tracks
259 * ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE.
Kweku Adamsb78430e2018-02-20 18:06:09 -0800260 * New in version 31:
261 * - New cellular network types.
262 * - Deferred job metrics.
Kweku Adams6fe1a5a2018-05-22 15:02:50 -0700263 * New in version 32:
264 * - Ambient display properly output in data dump.
zhouwenjied707da92018-07-06 10:23:00 -0700265 * New in version 33:
266 * - Fixed bug in min learned capacity updating process.
Mike Ma99e57c32018-03-15 14:40:37 -0700267 * New in version 34:
268 * - Deprecated STATS_SINCE_UNPLUGGED and STATS_CURRENT.
Blake Kragtenbc75c722019-03-20 17:14:58 -0700269 * New in version 35:
270 * - Fixed bug that was not reporting high cellular tx power correctly
271 * - Added out of service and emergency service modes to data connection types
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700272 */
Blake Kragtenbc75c722019-03-20 17:14:58 -0700273 static final int CHECKIN_VERSION = 35;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700274
275 /**
276 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700278 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700279
Evan Millar22ac0432009-03-31 11:33:18 -0700280 private static final long BYTES_PER_KB = 1024;
281 private static final long BYTES_PER_MB = 1048576; // 1024^2
282 private static final long BYTES_PER_GB = 1073741824; //1024^3
Blake Kragtencb308d92019-01-28 10:54:13 -0800283 public static final double MILLISECONDS_IN_HOUR = 3600 * 1000;
Bookatz506a8182017-05-01 14:18:42 -0700284
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700285 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800286 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700287 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700289 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700290 private static final String CPU_DATA = "cpu";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700291 private static final String GLOBAL_CPU_FREQ_DATA = "gcf";
292 private static final String CPU_TIMES_AT_FREQ_DATA = "ctf";
Bookatz50df7112017-08-04 14:53:26 -0700293 // rpm line is:
294 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "rpm", state/voter name, total time, total count,
295 // screen-off time, screen-off count
296 private static final String RESOURCE_POWER_MANAGER_DATA = "rpm";
Evan Millare84de8d2009-04-02 22:16:12 -0700297 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800298 private static final String VIBRATOR_DATA = "vib";
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700299 private static final String FOREGROUND_ACTIVITY_DATA = "fg";
300 // fgs line is:
301 // BATTERY_STATS_CHECKIN_VERSION, uid, category, "fgs",
302 // foreground service time, count
303 private static final String FOREGROUND_SERVICE_DATA = "fgs";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700304 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700305 // wl line is:
306 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
Bookatz5b5ec322017-05-26 09:40:38 -0700307 // full totalTime, 'f', count, current duration, max duration, total duration,
308 // partial totalTime, 'p', count, current duration, max duration, total duration,
309 // bg partial totalTime, 'bp', count, current duration, max duration, total duration,
310 // window totalTime, 'w', count, current duration, max duration, total duration
Bookatz506a8182017-05-01 14:18:42 -0700311 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700312 private static final String WAKELOCK_DATA = "wl";
Bookatzc8c44962017-05-11 12:12:54 -0700313 // awl line is:
314 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "awl",
315 // cumulative partial wakelock duration, cumulative background partial wakelock duration
316 private static final String AGGREGATED_WAKELOCK_DATA = "awl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700317 private static final String SYNC_DATA = "sy";
318 private static final String JOB_DATA = "jb";
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700319 private static final String JOB_COMPLETION_DATA = "jbc";
Amith Yamasani977e11f2018-02-16 11:29:54 -0800320
321 /**
322 * jbd line is:
323 * BATTERY_STATS_CHECKIN_VERSION, uid, which, "jbd",
Amith Yamasani0ca706b2018-03-01 17:28:59 -0800324 * jobsDeferredEventCount, jobsDeferredCount, totalLatencyMillis,
325 * 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 -0800326 * <p>
327 * @see #JOB_FRESHNESS_BUCKETS
328 */
329 private static final String JOBS_DEFERRED_DATA = "jbd";
Evan Millarc64edde2009-04-18 12:26:32 -0700330 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700331 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700332 private static final String NETWORK_DATA = "nt";
333 private static final String USER_ACTIVITY_DATA = "ua";
334 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800335 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700336 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700337 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700338 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800339 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
340 private static final String WIFI_CONTROLLER_DATA = "wfcd";
341 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
342 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700343 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700344 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800345 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800346 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
347 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800348 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700349 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700350 private static final String SCREEN_BRIGHTNESS_DATA = "br";
351 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700352 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700353 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
354 private static final String DATA_CONNECTION_TIME_DATA = "dct";
355 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800356 private static final String WIFI_STATE_TIME_DATA = "wst";
357 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700358 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
359 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
360 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
361 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800362 private static final String POWER_USE_SUMMARY_DATA = "pws";
363 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700364 private static final String DISCHARGE_STEP_DATA = "dsd";
365 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700366 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
367 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700368 private static final String FLASHLIGHT_DATA = "fla";
369 private static final String CAMERA_DATA = "cam";
370 private static final String VIDEO_DATA = "vid";
371 private static final String AUDIO_DATA = "aud";
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700372 private static final String WIFI_MULTICAST_TOTAL_DATA = "wmct";
373 private static final String WIFI_MULTICAST_DATA = "wmc";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374
Adam Lesinski010bf372016-04-11 12:18:18 -0700375 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
376
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700377 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 private final Formatter mFormatter = new Formatter(mFormatBuilder);
379
Siddharth Rayb50a6842017-12-14 15:15:28 -0800380 private static final String CELLULAR_CONTROLLER_NAME = "Cellular";
381 private static final String WIFI_CONTROLLER_NAME = "WiFi";
382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700384 * Indicates times spent by the uid at each cpu frequency in all process states.
385 *
386 * Other types might include times spent in foreground, background etc.
387 */
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800388 @VisibleForTesting
389 public static final String UID_TIMES_TYPE_ALL = "A";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700390
391 /**
Amith Yamasani977e11f2018-02-16 11:29:54 -0800392 * These are the thresholds for bucketing last time since a job was run for an app
393 * 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 -0800394 * than 1 hour ago, then it's reasonably fresh, 2 hours ago, not so fresh and so
Amith Yamasani977e11f2018-02-16 11:29:54 -0800395 * on.
396 */
397 public static final long[] JOB_FRESHNESS_BUCKETS = {
398 1 * 60 * 60 * 1000L,
399 2 * 60 * 60 * 1000L,
Amith Yamasani0ca706b2018-03-01 17:28:59 -0800400 4 * 60 * 60 * 1000L,
401 8 * 60 * 60 * 1000L,
Amith Yamasani977e11f2018-02-16 11:29:54 -0800402 Long.MAX_VALUE
403 };
404
405 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700406 * State for keeping track of counting information.
407 */
408 public static abstract class Counter {
409
410 /**
411 * Returns the count associated with this Counter for the
412 * selected type of statistics.
413 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700414 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700415 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000416 @UnsupportedAppUsage
Evan Millarc64edde2009-04-18 12:26:32 -0700417 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700418
419 /**
420 * Temporary for debugging.
421 */
422 public abstract void logState(Printer pw, String prefix);
423 }
424
425 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700426 * State for keeping track of long counting information.
427 */
428 public static abstract class LongCounter {
429
430 /**
431 * Returns the count associated with this Counter for the
432 * selected type of statistics.
433 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700434 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700435 */
436 public abstract long getCountLocked(int which);
437
438 /**
439 * Temporary for debugging.
440 */
441 public abstract void logState(Printer pw, String prefix);
442 }
443
444 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700445 * State for keeping track of array of long counting information.
446 */
447 public static abstract class LongCounterArray {
448 /**
449 * Returns the counts associated with this Counter for the
450 * selected type of statistics.
451 *
452 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
453 */
454 public abstract long[] getCountsLocked(int which);
455
456 /**
457 * Temporary for debugging.
458 */
459 public abstract void logState(Printer pw, String prefix);
460 }
461
462 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800463 * Container class that aggregates counters for transmit, receive, and idle state of a
464 * radio controller.
465 */
466 public static abstract class ControllerActivityCounter {
467 /**
468 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
469 * idle state.
470 */
471 public abstract LongCounter getIdleTimeCounter();
472
473 /**
474 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
Siddharth Rayb50a6842017-12-14 15:15:28 -0800475 * scan state.
476 */
477 public abstract LongCounter getScanTimeCounter();
478
Siddharth Rayed754702018-02-15 12:44:37 -0800479 /**
480 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
481 * sleep state.
482 */
483 public abstract LongCounter getSleepTimeCounter();
Siddharth Rayb50a6842017-12-14 15:15:28 -0800484
485 /**
486 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800487 * receive state.
488 */
489 public abstract LongCounter getRxTimeCounter();
490
491 /**
492 * An array of {@link LongCounter}, representing various transmit levels, where each level
493 * may draw a different amount of power. The levels themselves are controller-specific.
494 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
495 * various transmit level states.
496 */
497 public abstract LongCounter[] getTxTimeCounters();
498
499 /**
500 * @return a non-null {@link LongCounter} representing the power consumed by the controller
501 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
502 * yield a value of 0 if the device doesn't support power calculations.
503 */
504 public abstract LongCounter getPowerCounter();
Blake Kragtencb308d92019-01-28 10:54:13 -0800505
506 /**
507 * @return a non-null {@link LongCounter} representing total power monitored on the rails
508 * in mAms (miliamps-milliseconds). The counter may always yield a value of 0 if the device
509 * doesn't support power rail monitoring.
510 */
511 public abstract LongCounter getMonitoredRailChargeConsumedMaMs();
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800512 }
513
514 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 * State for keeping track of timing information.
516 */
517 public static abstract class Timer {
518
519 /**
520 * Returns the count associated with this Timer for the
521 * selected type of statistics.
522 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700523 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000525 @UnsupportedAppUsage
Evan Millarc64edde2009-04-18 12:26:32 -0700526 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527
528 /**
529 * Returns the total time in microseconds associated with this Timer for the
530 * selected type of statistics.
531 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800532 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700533 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 * @return a time in microseconds
535 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000536 @UnsupportedAppUsage
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800537 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700540 * Returns the total time in microseconds associated with this Timer since the
541 * 'mark' was last set.
542 *
543 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
544 * @return a time in microseconds
545 */
546 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
547
548 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700549 * Returns the max duration if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700550 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700551 */
552 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
553 return -1;
554 }
555
556 /**
557 * Returns the current time the timer has been active, if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700558 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700559 */
560 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
561 return -1;
562 }
563
564 /**
Kweku Adams103351f2017-10-16 14:39:34 -0700565 * Returns the total time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800566 *
567 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
568 * been on since reset.
569 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
570 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
571 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
572 * the actual total time.
Kweku Adams103351f2017-10-16 14:39:34 -0700573 * Not all Timer subclasses track the max, total, and current durations.
Bookatz867c0d72017-03-07 18:23:42 -0800574 */
575 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
576 return -1;
577 }
578
579 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700580 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
581 * used, for example, for tracking background usage. Secondary timers are never pooled.
582 *
583 * Not all Timer subclasses have a secondary timer; those that don't return null.
584 */
585 public Timer getSubTimer() {
586 return null;
587 }
588
589 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700590 * Returns whether the timer is currently running. Some types of timers
591 * (e.g. BatchTimers) don't know whether the event is currently active,
592 * and report false.
593 */
594 public boolean isRunningLocked() {
595 return false;
596 }
597
598 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 * Temporary for debugging.
600 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700601 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 }
603
604 /**
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800605 * Maps the ActivityManager procstate into corresponding BatteryStats procstate.
606 */
607 public static int mapToInternalProcessState(int procState) {
608 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
609 return ActivityManager.PROCESS_STATE_NONEXISTENT;
610 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
611 return Uid.PROCESS_STATE_TOP;
Amith Yamasania0a30a12019-01-22 11:38:06 -0800612 } else if (ActivityManager.isForegroundService(procState)) {
Dianne Hackborn10fc4fd2017-12-19 17:23:13 -0800613 // State when app has put itself in the foreground.
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800614 return Uid.PROCESS_STATE_FOREGROUND_SERVICE;
615 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
616 // Persistent and other foreground states go here.
617 return Uid.PROCESS_STATE_FOREGROUND;
618 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
619 return Uid.PROCESS_STATE_BACKGROUND;
620 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
621 return Uid.PROCESS_STATE_TOP_SLEEPING;
622 } else if (procState <= ActivityManager.PROCESS_STATE_HEAVY_WEIGHT) {
623 return Uid.PROCESS_STATE_HEAVY_WEIGHT;
624 } else {
625 return Uid.PROCESS_STATE_CACHED;
626 }
627 }
628
629 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 * The statistics associated with a particular uid.
631 */
632 public static abstract class Uid {
633
634 /**
635 * Returns a mapping containing wakelock statistics.
636 *
637 * @return a Map from Strings to Uid.Wakelock objects.
638 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000639 @UnsupportedAppUsage
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700640 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641
642 /**
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700643 * Returns the WiFi Multicast Wakelock statistics.
644 *
645 * @return a Timer Object for the per uid Multicast statistics.
646 */
647 public abstract Timer getMulticastWakelockStats();
648
649 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700650 * Returns a mapping containing sync statistics.
651 *
652 * @return a Map from Strings to Timer objects.
653 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700654 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700655
656 /**
657 * Returns a mapping containing scheduled job statistics.
658 *
659 * @return a Map from Strings to Timer objects.
660 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700661 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700662
663 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700664 * Returns statistics about how jobs have completed.
665 *
666 * @return A Map of String job names to completion type -> count mapping.
667 */
668 public abstract ArrayMap<String, SparseIntArray> getJobCompletionStats();
669
670 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 * The statistics associated with a particular wake lock.
672 */
673 public static abstract class Wakelock {
Andrei Onea24ec3212019-03-15 17:35:05 +0000674 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 public abstract Timer getWakeTime(int type);
676 }
677
678 /**
Bookatzc8c44962017-05-11 12:12:54 -0700679 * The cumulative time the uid spent holding any partial wakelocks. This will generally
680 * differ from summing over the Wakelocks in getWakelockStats since the latter may have
681 * wakelocks that overlap in time (and therefore over-counts).
682 */
683 public abstract Timer getAggregatedPartialWakelockTimer();
684
685 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 * Returns a mapping containing sensor statistics.
687 *
688 * @return a Map from Integer sensor ids to Uid.Sensor objects.
689 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000690 @UnsupportedAppUsage
Dianne Hackborn61659e52014-07-09 16:13:01 -0700691 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692
693 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700694 * Returns a mapping containing active process data.
695 */
696 public abstract SparseArray<? extends Pid> getPidStats();
Bookatzc8c44962017-05-11 12:12:54 -0700697
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700698 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 * Returns a mapping containing process statistics.
700 *
701 * @return a Map from Strings to Uid.Proc objects.
702 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000703 @UnsupportedAppUsage
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700704 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705
706 /**
707 * Returns a mapping containing package statistics.
708 *
709 * @return a Map from Strings to Uid.Pkg objects.
710 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000711 @UnsupportedAppUsage
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700712 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700713
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800714 public abstract ControllerActivityCounter getWifiControllerActivity();
715 public abstract ControllerActivityCounter getBluetoothControllerActivity();
716 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800717
718 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 * {@hide}
720 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000721 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700723
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800724 public abstract void noteWifiRunningLocked(long elapsedRealtime);
725 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
726 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
727 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
728 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
729 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
730 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
731 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
732 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
733 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800734 public abstract void noteActivityResumedLocked(long elapsedRealtime);
735 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Andrei Onea24ec3212019-03-15 17:35:05 +0000736 @UnsupportedAppUsage
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800737 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
Andrei Onea24ec3212019-03-15 17:35:05 +0000738 @UnsupportedAppUsage
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800739 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
Andrei Onea24ec3212019-03-15 17:35:05 +0000740 @UnsupportedAppUsage
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800741 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700742 public abstract int getWifiScanCount(int which);
Kweku Adams103351f2017-10-16 14:39:34 -0700743 /**
744 * Returns the timer keeping track of wifi scans.
745 */
746 public abstract Timer getWifiScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800747 public abstract int getWifiScanBackgroundCount(int which);
748 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
749 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -0700750 /**
751 * Returns the timer keeping track of background wifi scans.
752 */
753 public abstract Timer getWifiScanBackgroundTimer();
Andrei Onea24ec3212019-03-15 17:35:05 +0000754 @UnsupportedAppUsage
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800755 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700756 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Andrei Onea24ec3212019-03-15 17:35:05 +0000757 @UnsupportedAppUsage
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800758 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Andrei Onea24ec3212019-03-15 17:35:05 +0000759 @UnsupportedAppUsage
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700760 public abstract Timer getAudioTurnedOnTimer();
Andrei Onea24ec3212019-03-15 17:35:05 +0000761 @UnsupportedAppUsage
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700762 public abstract Timer getVideoTurnedOnTimer();
763 public abstract Timer getFlashlightTurnedOnTimer();
764 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700765 public abstract Timer getForegroundActivityTimer();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700766
767 /**
768 * Returns the timer keeping track of Foreground Service time
769 */
770 public abstract Timer getForegroundServiceTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800771 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800772 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatzb1f04f32017-05-19 13:57:32 -0700773 public abstract Timer getBluetoothUnoptimizedScanTimer();
774 public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700775 public abstract Counter getBluetoothScanResultCounter();
Bookatzb1f04f32017-05-19 13:57:32 -0700776 public abstract Counter getBluetoothScanResultBgCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700777
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700778 public abstract long[] getCpuFreqTimes(int which);
779 public abstract long[] getScreenOffCpuFreqTimes(int which);
Mike Ma3d422c32017-10-25 11:08:57 -0700780 /**
781 * Returns cpu active time of an uid.
782 */
783 public abstract long getCpuActiveTime();
784 /**
785 * Returns cpu times of an uid on each cluster
786 */
787 public abstract long[] getCpuClusterTimes();
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700788
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800789 /**
790 * Returns cpu times of an uid at a particular process state.
791 */
792 public abstract long[] getCpuFreqTimes(int which, int procState);
793 /**
794 * Returns cpu times of an uid while the screen if off at a particular process state.
795 */
796 public abstract long[] getScreenOffCpuFreqTimes(int which, int procState);
797
Dianne Hackborna0200e32016-03-30 18:01:41 -0700798 // Note: the following times are disjoint. They can be added together to find the
799 // total time a uid has had any processes running at all.
800
801 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800802 * Time this uid has any processes in the top state.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700803 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800804 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700805 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800806 * Time this uid has any process with a started foreground service, but
Dianne Hackborna0200e32016-03-30 18:01:41 -0700807 * none in the "top" state.
808 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800809 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700810 /**
Dianne Hackborna0200e32016-03-30 18:01:41 -0700811 * Time this uid has any process in an active foreground state, but none in the
zhouwenjie46712bc2018-01-11 15:21:27 -0800812 * "foreground service" or better state. Persistent and other foreground states go here.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700813 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800814 public static final int PROCESS_STATE_FOREGROUND = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700815 /**
816 * Time this uid has any process in an active background state, but none in the
817 * "foreground" or better state.
818 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800819 public static final int PROCESS_STATE_BACKGROUND = 3;
820 /**
821 * Time this uid has any process that is top while the device is sleeping, but not
822 * active for any other reason. We kind-of consider it a kind of cached process
823 * for execution restrictions.
824 */
825 public static final int PROCESS_STATE_TOP_SLEEPING = 4;
826 /**
827 * Time this uid has any process that is in the background but it has an activity
828 * marked as "can't save state". This is essentially a cached process, though the
829 * system will try much harder than normal to avoid killing it.
830 */
831 public static final int PROCESS_STATE_HEAVY_WEIGHT = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700832 /**
833 * Time this uid has any processes that are sitting around cached, not in one of the
834 * other active states.
835 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800836 public static final int PROCESS_STATE_CACHED = 6;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700837 /**
838 * Total number of process states we track.
839 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800840 public static final int NUM_PROCESS_STATE = 7;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700841
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800842 // Used in dump
Dianne Hackborn61659e52014-07-09 16:13:01 -0700843 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800844 "Top", "Fg Service", "Foreground", "Background", "Top Sleeping", "Heavy Weight",
845 "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700846 };
847
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800848 // Used in checkin dump
849 @VisibleForTesting
850 public static final String[] UID_PROCESS_TYPES = {
851 "T", // TOP
852 "FS", // FOREGROUND_SERVICE
853 "F", // FOREGROUND
854 "B", // BACKGROUND
855 "TS", // TOP_SLEEPING
856 "HW", // HEAVY_WEIGHT
857 "C" // CACHED
858 };
859
860 /**
861 * When the process exits one of these states, we need to make sure cpu time in this state
862 * is not attributed to any non-critical process states.
863 */
864 public static final int[] CRITICAL_PROC_STATES = {
Amith Yamasania0a30a12019-01-22 11:38:06 -0800865 PROCESS_STATE_TOP,
Amith Yamasanif235d0b2019-03-20 22:49:43 -0700866 PROCESS_STATE_FOREGROUND_SERVICE_LOCATION,
867 PROCESS_STATE_BOUND_TOP, PROCESS_STATE_FOREGROUND_SERVICE,
Amith Yamasania0a30a12019-01-22 11:38:06 -0800868 PROCESS_STATE_FOREGROUND
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800869 };
870
Dianne Hackborn61659e52014-07-09 16:13:01 -0700871 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800872 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700873
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800874 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875
Robert Greenwalta029ea12013-09-25 16:38:12 -0700876 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
877
Dianne Hackborn617f8772009-03-31 15:04:46 -0700878 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700879 * Note that these must match the constants in android.os.PowerManager.
880 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
881 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700882 */
883 static final String[] USER_ACTIVITY_TYPES = {
Lucas Dupin0a5d7972019-01-16 18:52:30 -0800884 "other", "button", "touch", "accessibility", "attention"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700885 };
Bookatzc8c44962017-05-11 12:12:54 -0700886
Lucas Dupin0a5d7972019-01-16 18:52:30 -0800887 public static final int NUM_USER_ACTIVITY_TYPES = USER_ACTIVITY_TYPES.length;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700888
Dianne Hackborn617f8772009-03-31 15:04:46 -0700889 public abstract void noteUserActivityLocked(int type);
890 public abstract boolean hasUserActivity();
891 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700892
893 public abstract boolean hasNetworkActivity();
Andrei Onea24ec3212019-03-15 17:35:05 +0000894 @UnsupportedAppUsage
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800895 public abstract long getNetworkActivityBytes(int type, int which);
896 public abstract long getNetworkActivityPackets(int type, int which);
Andrei Onea24ec3212019-03-15 17:35:05 +0000897 @UnsupportedAppUsage
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800898 public abstract long getMobileRadioActiveTime(int which);
899 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700900
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700901 /**
902 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
903 */
904 public abstract long getUserCpuTimeUs(int which);
905
906 /**
907 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
908 */
909 public abstract long getSystemCpuTimeUs(int which);
910
911 /**
Sudheer Shanka71f34b32017-07-21 00:14:24 -0700912 * Returns the approximate cpu time (in microseconds) spent at a certain CPU speed for a
Adam Lesinski6832f392015-09-05 18:05:40 -0700913 * given CPU cluster.
914 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700915 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700916 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700917 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
918 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700919 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700920 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700921
Adam Lesinski5f056f62016-07-14 16:56:08 -0700922 /**
923 * Returns the number of times this UID woke up the Application Processor to
924 * process a mobile radio packet.
925 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
926 */
927 public abstract long getMobileRadioApWakeupCount(int which);
928
929 /**
930 * Returns the number of times this UID woke up the Application Processor to
931 * process a WiFi packet.
932 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
933 */
934 public abstract long getWifiRadioApWakeupCount(int which);
935
Amith Yamasani977e11f2018-02-16 11:29:54 -0800936 /**
937 * Appends the deferred jobs data to the StringBuilder passed in, in checkin format
938 * @param sb StringBuilder that can be overwritten with the deferred jobs data
939 * @param which one of STATS_*
940 */
941 public abstract void getDeferredJobsCheckinLineLocked(StringBuilder sb, int which);
942
943 /**
944 * Appends the deferred jobs data to the StringBuilder passed in
945 * @param sb StringBuilder that can be overwritten with the deferred jobs data
946 * @param which one of STATS_*
947 */
948 public abstract void getDeferredJobsLineLocked(StringBuilder sb, int which);
949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800951 /*
952 * FIXME: it's not correct to use this magic value because it
953 * could clash with a sensor handle (which are defined by
954 * the sensor HAL, and therefore out of our control
955 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 // Magic sensor number for the GPS.
Andrei Onea24ec3212019-03-15 17:35:05 +0000957 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800959
Andrei Onea24ec3212019-03-15 17:35:05 +0000960 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800962
Andrei Onea24ec3212019-03-15 17:35:05 +0000963 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800965
Bookatz867c0d72017-03-07 18:23:42 -0800966 /** Returns a Timer for sensor usage when app is in the background. */
967 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 }
969
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700970 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800971 public int mWakeNesting;
972 public long mWakeSumMs;
973 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700974 }
975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 /**
977 * The statistics associated with a particular process.
978 */
979 public static abstract class Proc {
980
Dianne Hackborn287952c2010-09-22 22:34:31 -0700981 public static class ExcessivePower {
982 public static final int TYPE_WAKE = 1;
983 public static final int TYPE_CPU = 2;
984
Andrei Onea24ec3212019-03-15 17:35:05 +0000985 @UnsupportedAppUsage
Dianne Hackborn287952c2010-09-22 22:34:31 -0700986 public int type;
Andrei Onea24ec3212019-03-15 17:35:05 +0000987 @UnsupportedAppUsage
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700988 public long overTime;
Andrei Onea24ec3212019-03-15 17:35:05 +0000989 @UnsupportedAppUsage
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700990 public long usedTime;
991 }
992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800994 * Returns true if this process is still active in the battery stats.
995 */
996 public abstract boolean isActive();
997
998 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700999 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001001 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001003 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 public abstract long getUserTime(int which);
1005
1006 /**
Adam Lesinski33dac552015-03-09 15:24:48 -07001007 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001009 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001011 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 public abstract long getSystemTime(int which);
1013
1014 /**
1015 * Returns the number of times the process has been started.
1016 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001017 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001019 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001021
1022 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001023 * Returns the number of times the process has crashed.
1024 *
1025 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
1026 */
1027 public abstract int getNumCrashes(int which);
1028
1029 /**
1030 * Returns the number of times the process has ANRed.
1031 *
1032 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
1033 */
1034 public abstract int getNumAnrs(int which);
1035
1036 /**
Adam Lesinski33dac552015-03-09 15:24:48 -07001037 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001038 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001039 * @return foreground cpu time in microseconds
1040 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001041 @UnsupportedAppUsage
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001042 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -07001043
Andrei Onea24ec3212019-03-15 17:35:05 +00001044 @UnsupportedAppUsage
Dianne Hackborn287952c2010-09-22 22:34:31 -07001045 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001046
Andrei Onea24ec3212019-03-15 17:35:05 +00001047 @UnsupportedAppUsage
Dianne Hackborn287952c2010-09-22 22:34:31 -07001048 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 }
1050
1051 /**
1052 * The statistics associated with a particular package.
1053 */
1054 public static abstract class Pkg {
1055
1056 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001057 * Returns information about all wakeup alarms that have been triggered for this
1058 * package. The mapping keys are tag names for the alarms, the counter contains
1059 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001061 @UnsupportedAppUsage
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001062 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063
1064 /**
1065 * Returns a mapping containing service statistics.
1066 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001067 @UnsupportedAppUsage
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001068 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069
1070 /**
1071 * The statistics associated with a particular service.
1072 */
Joe Onoratoabded112016-02-08 16:49:39 -08001073 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074
1075 /**
1076 * Returns the amount of time spent started.
1077 *
1078 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001079 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 * @return
1081 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001082 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 public abstract long getStartTime(long batteryUptime, int which);
1084
1085 /**
1086 * Returns the total number of times startService() has been called.
1087 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001088 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001090 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 public abstract int getStarts(int which);
1092
1093 /**
1094 * Returns the total number times the service has been launched.
1095 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001096 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001098 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 public abstract int getLaunches(int which);
1100 }
1101 }
1102 }
1103
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001104 public static final class LevelStepTracker {
1105 public long mLastStepTime = -1;
1106 public int mNumStepDurations;
1107 public final long[] mStepDurations;
1108
1109 public LevelStepTracker(int maxLevelSteps) {
1110 mStepDurations = new long[maxLevelSteps];
1111 }
1112
1113 public LevelStepTracker(int numSteps, long[] steps) {
1114 mNumStepDurations = numSteps;
1115 mStepDurations = new long[numSteps];
1116 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
1117 }
1118
1119 public long getDurationAt(int index) {
1120 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
1121 }
1122
1123 public int getLevelAt(int index) {
1124 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
1125 >> STEP_LEVEL_LEVEL_SHIFT);
1126 }
1127
1128 public int getInitModeAt(int index) {
1129 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
1130 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1131 }
1132
1133 public int getModModeAt(int index) {
1134 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
1135 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1136 }
1137
1138 private void appendHex(long val, int topOffset, StringBuilder out) {
1139 boolean hasData = false;
1140 while (topOffset >= 0) {
1141 int digit = (int)( (val>>topOffset) & 0xf );
1142 topOffset -= 4;
1143 if (!hasData && digit == 0) {
1144 continue;
1145 }
1146 hasData = true;
1147 if (digit >= 0 && digit <= 9) {
1148 out.append((char)('0' + digit));
1149 } else {
1150 out.append((char)('a' + digit - 10));
1151 }
1152 }
1153 }
1154
1155 public void encodeEntryAt(int index, StringBuilder out) {
1156 long item = mStepDurations[index];
1157 long duration = item & STEP_LEVEL_TIME_MASK;
1158 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
1159 >> STEP_LEVEL_LEVEL_SHIFT);
1160 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
1161 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1162 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
1163 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1164 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1165 case Display.STATE_OFF: out.append('f'); break;
1166 case Display.STATE_ON: out.append('o'); break;
1167 case Display.STATE_DOZE: out.append('d'); break;
1168 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
1169 }
1170 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1171 out.append('p');
1172 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001173 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1174 out.append('i');
1175 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001176 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1177 case Display.STATE_OFF: out.append('F'); break;
1178 case Display.STATE_ON: out.append('O'); break;
1179 case Display.STATE_DOZE: out.append('D'); break;
1180 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
1181 }
1182 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1183 out.append('P');
1184 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001185 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1186 out.append('I');
1187 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001188 out.append('-');
1189 appendHex(level, 4, out);
1190 out.append('-');
1191 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
1192 }
1193
1194 public void decodeEntryAt(int index, String value) {
1195 final int N = value.length();
1196 int i = 0;
1197 char c;
1198 long out = 0;
1199 while (i < N && (c=value.charAt(i)) != '-') {
1200 i++;
1201 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001202 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001203 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001204 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001205 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001206 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001207 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001208 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1209 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1210 break;
1211 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1212 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1213 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001214 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1215 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1216 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001217 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1218 break;
1219 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1220 break;
1221 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1222 break;
1223 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1224 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1225 break;
1226 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1227 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001228 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001229 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1230 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1231 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001232 }
1233 }
1234 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001235 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001236 while (i < N && (c=value.charAt(i)) != '-') {
1237 i++;
1238 level <<= 4;
1239 if (c >= '0' && c <= '9') {
1240 level += c - '0';
1241 } else if (c >= 'a' && c <= 'f') {
1242 level += c - 'a' + 10;
1243 } else if (c >= 'A' && c <= 'F') {
1244 level += c - 'A' + 10;
1245 }
1246 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001247 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001248 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
1249 long duration = 0;
1250 while (i < N && (c=value.charAt(i)) != '-') {
1251 i++;
1252 duration <<= 4;
1253 if (c >= '0' && c <= '9') {
1254 duration += c - '0';
1255 } else if (c >= 'a' && c <= 'f') {
1256 duration += c - 'a' + 10;
1257 } else if (c >= 'A' && c <= 'F') {
1258 duration += c - 'A' + 10;
1259 }
1260 }
1261 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
1262 }
1263
1264 public void init() {
1265 mLastStepTime = -1;
1266 mNumStepDurations = 0;
1267 }
1268
1269 public void clearTime() {
1270 mLastStepTime = -1;
1271 }
1272
1273 public long computeTimePerLevel() {
1274 final long[] steps = mStepDurations;
1275 final int numSteps = mNumStepDurations;
1276
1277 // For now we'll do a simple average across all steps.
1278 if (numSteps <= 0) {
1279 return -1;
1280 }
1281 long total = 0;
1282 for (int i=0; i<numSteps; i++) {
1283 total += steps[i] & STEP_LEVEL_TIME_MASK;
1284 }
1285 return total / numSteps;
1286 /*
1287 long[] buckets = new long[numSteps];
1288 int numBuckets = 0;
1289 int numToAverage = 4;
1290 int i = 0;
1291 while (i < numSteps) {
1292 long totalTime = 0;
1293 int num = 0;
1294 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1295 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1296 num++;
1297 }
1298 buckets[numBuckets] = totalTime / num;
1299 numBuckets++;
1300 numToAverage *= 2;
1301 i += num;
1302 }
1303 if (numBuckets < 1) {
1304 return -1;
1305 }
1306 long averageTime = buckets[numBuckets-1];
1307 for (i=numBuckets-2; i>=0; i--) {
1308 averageTime = (averageTime + buckets[i]) / 2;
1309 }
1310 return averageTime;
1311 */
1312 }
1313
1314 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1315 int[] outNumOfInterest) {
1316 final long[] steps = mStepDurations;
1317 final int count = mNumStepDurations;
1318 if (count <= 0) {
1319 return -1;
1320 }
1321 long total = 0;
1322 int numOfInterest = 0;
1323 for (int i=0; i<count; i++) {
1324 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1325 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1326 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1327 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1328 // If the modes of interest didn't change during this step period...
1329 if ((modMode&modesOfInterest) == 0) {
1330 // And the mode values during this period match those we are measuring...
1331 if ((initMode&modesOfInterest) == modeValues) {
1332 // Then this can be used to estimate the total time!
1333 numOfInterest++;
1334 total += steps[i] & STEP_LEVEL_TIME_MASK;
1335 }
1336 }
1337 }
1338 if (numOfInterest <= 0) {
1339 return -1;
1340 }
1341
1342 if (outNumOfInterest != null) {
1343 outNumOfInterest[0] = numOfInterest;
1344 }
1345
1346 // The estimated time is the average time we spend in each level, multipled
1347 // by 100 -- the total number of battery levels
1348 return (total / numOfInterest) * 100;
1349 }
1350
1351 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1352 int stepCount = mNumStepDurations;
1353 final long lastStepTime = mLastStepTime;
1354 if (lastStepTime >= 0 && numStepLevels > 0) {
1355 final long[] steps = mStepDurations;
1356 long duration = elapsedRealtime - lastStepTime;
1357 for (int i=0; i<numStepLevels; i++) {
1358 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1359 long thisDuration = duration / (numStepLevels-i);
1360 duration -= thisDuration;
1361 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1362 thisDuration = STEP_LEVEL_TIME_MASK;
1363 }
1364 steps[0] = thisDuration | modeBits;
1365 }
1366 stepCount += numStepLevels;
1367 if (stepCount > steps.length) {
1368 stepCount = steps.length;
1369 }
1370 }
1371 mNumStepDurations = stepCount;
1372 mLastStepTime = elapsedRealtime;
1373 }
1374
1375 public void readFromParcel(Parcel in) {
1376 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001377 if (N > mStepDurations.length) {
1378 throw new ParcelFormatException("more step durations than available: " + N);
1379 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001380 mNumStepDurations = N;
1381 for (int i=0; i<N; i++) {
1382 mStepDurations[i] = in.readLong();
1383 }
1384 }
1385
1386 public void writeToParcel(Parcel out) {
1387 final int N = mNumStepDurations;
1388 out.writeInt(N);
1389 for (int i=0; i<N; i++) {
1390 out.writeLong(mStepDurations[i]);
1391 }
1392 }
1393 }
1394
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001395 public static final class PackageChange {
1396 public String mPackageName;
1397 public boolean mUpdate;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001398 public long mVersionCode;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001399 }
1400
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001401 public static final class DailyItem {
1402 public long mStartTime;
1403 public long mEndTime;
1404 public LevelStepTracker mDischargeSteps;
1405 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001406 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001407 }
1408
1409 public abstract DailyItem getDailyItemLocked(int daysAgo);
1410
1411 public abstract long getCurrentDailyStartTime();
1412
1413 public abstract long getNextMinDailyDeadline();
1414
1415 public abstract long getNextMaxDailyDeadline();
1416
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001417 public abstract long[] getCpuFreqs();
1418
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001419 public final static class HistoryTag {
1420 public String string;
1421 public int uid;
1422
1423 public int poolIdx;
1424
1425 public void setTo(HistoryTag o) {
1426 string = o.string;
1427 uid = o.uid;
1428 poolIdx = o.poolIdx;
1429 }
1430
1431 public void setTo(String _string, int _uid) {
1432 string = _string;
1433 uid = _uid;
1434 poolIdx = -1;
1435 }
1436
1437 public void writeToParcel(Parcel dest, int flags) {
1438 dest.writeString(string);
1439 dest.writeInt(uid);
1440 }
1441
1442 public void readFromParcel(Parcel src) {
1443 string = src.readString();
1444 uid = src.readInt();
1445 poolIdx = -1;
1446 }
1447
1448 @Override
1449 public boolean equals(Object o) {
1450 if (this == o) return true;
1451 if (o == null || getClass() != o.getClass()) return false;
1452
1453 HistoryTag that = (HistoryTag) o;
1454
1455 if (uid != that.uid) return false;
1456 if (!string.equals(that.string)) return false;
1457
1458 return true;
1459 }
1460
1461 @Override
1462 public int hashCode() {
1463 int result = string.hashCode();
1464 result = 31 * result + uid;
1465 return result;
1466 }
1467 }
1468
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001469 /**
1470 * Optional detailed information that can go into a history step. This is typically
1471 * generated each time the battery level changes.
1472 */
1473 public final static class HistoryStepDetails {
1474 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1475 public int userTime;
1476 public int systemTime;
1477
1478 // Top three apps using CPU in the last step, with times in 1/100 second.
1479 public int appCpuUid1;
1480 public int appCpuUTime1;
1481 public int appCpuSTime1;
1482 public int appCpuUid2;
1483 public int appCpuUTime2;
1484 public int appCpuSTime2;
1485 public int appCpuUid3;
1486 public int appCpuUTime3;
1487 public int appCpuSTime3;
1488
1489 // Information from /proc/stat
1490 public int statUserTime;
1491 public int statSystemTime;
1492 public int statIOWaitTime;
1493 public int statIrqTime;
1494 public int statSoftIrqTime;
1495 public int statIdlTime;
1496
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001497 // Platform-level low power state stats
1498 public String statPlatformIdleState;
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001499 public String statSubsystemPowerState;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001500
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001501 public HistoryStepDetails() {
1502 clear();
1503 }
1504
1505 public void clear() {
1506 userTime = systemTime = 0;
1507 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1508 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1509 = appCpuUTime3 = appCpuSTime3 = 0;
1510 }
1511
1512 public void writeToParcel(Parcel out) {
1513 out.writeInt(userTime);
1514 out.writeInt(systemTime);
1515 out.writeInt(appCpuUid1);
1516 out.writeInt(appCpuUTime1);
1517 out.writeInt(appCpuSTime1);
1518 out.writeInt(appCpuUid2);
1519 out.writeInt(appCpuUTime2);
1520 out.writeInt(appCpuSTime2);
1521 out.writeInt(appCpuUid3);
1522 out.writeInt(appCpuUTime3);
1523 out.writeInt(appCpuSTime3);
1524 out.writeInt(statUserTime);
1525 out.writeInt(statSystemTime);
1526 out.writeInt(statIOWaitTime);
1527 out.writeInt(statIrqTime);
1528 out.writeInt(statSoftIrqTime);
1529 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001530 out.writeString(statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001531 out.writeString(statSubsystemPowerState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001532 }
1533
1534 public void readFromParcel(Parcel in) {
1535 userTime = in.readInt();
1536 systemTime = in.readInt();
1537 appCpuUid1 = in.readInt();
1538 appCpuUTime1 = in.readInt();
1539 appCpuSTime1 = in.readInt();
1540 appCpuUid2 = in.readInt();
1541 appCpuUTime2 = in.readInt();
1542 appCpuSTime2 = in.readInt();
1543 appCpuUid3 = in.readInt();
1544 appCpuUTime3 = in.readInt();
1545 appCpuSTime3 = in.readInt();
1546 statUserTime = in.readInt();
1547 statSystemTime = in.readInt();
1548 statIOWaitTime = in.readInt();
1549 statIrqTime = in.readInt();
1550 statSoftIrqTime = in.readInt();
1551 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001552 statPlatformIdleState = in.readString();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001553 statSubsystemPowerState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001554 }
1555 }
1556
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001557 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001558 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001559
1560 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Andrei Onea24ec3212019-03-15 17:35:05 +00001561 @UnsupportedAppUsage
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001562 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001563
Andrei Onea24ec3212019-03-15 17:35:05 +00001564 @UnsupportedAppUsage
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001565 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001566 public static final byte CMD_NULL = -1;
1567 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001568 public static final byte CMD_CURRENT_TIME = 5;
1569 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001570 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001571 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001572
Andrei Onea24ec3212019-03-15 17:35:05 +00001573 @UnsupportedAppUsage
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001574 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001575
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001576 /**
1577 * Return whether the command code is a delta data update.
1578 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001579 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001580 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001581 }
1582
Andrei Onea24ec3212019-03-15 17:35:05 +00001583 @UnsupportedAppUsage
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001584 public byte batteryLevel;
Andrei Onea24ec3212019-03-15 17:35:05 +00001585 @UnsupportedAppUsage
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001586 public byte batteryStatus;
Andrei Onea24ec3212019-03-15 17:35:05 +00001587 @UnsupportedAppUsage
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001588 public byte batteryHealth;
Andrei Onea24ec3212019-03-15 17:35:05 +00001589 @UnsupportedAppUsage
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001590 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001591
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001592 public short batteryTemperature;
Andrei Onea24ec3212019-03-15 17:35:05 +00001593 @UnsupportedAppUsage
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001594 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001595
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001596 // The charge of the battery in micro-Ampere-hours.
1597 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001598
Blake Kragtencb308d92019-01-28 10:54:13 -08001599 public double modemRailChargeMah;
1600 public double wifiRailChargeMah;
1601
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001602 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001603 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001604 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001605 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001606 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1607 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001608 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001609 public static final int STATE_PHONE_STATE_SHIFT = 6;
1610 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001611 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001612 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001613 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001614
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001615 // These states always appear directly in the first int token
1616 // of a delta change; they should be ones that change relatively
1617 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001618 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1619 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001620 public static final int STATE_GPS_ON_FLAG = 1<<29;
1621 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001622 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001623 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001624 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001625 // Do not use, this is used for coulomb delta count.
1626 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001627 // These are on the lower bits used for the command; if they change
1628 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001629 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001630 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001631 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001632 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1633 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
Mike Mac2f518a2017-09-19 16:06:03 -07001634 public static final int STATE_SCREEN_DOZE_FLAG = 1 << 18;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001635 // empty slot
1636 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001637
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001638 public static final int MOST_INTERESTING_STATES =
Mike Mac2f518a2017-09-19 16:06:03 -07001639 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG | STATE_SCREEN_DOZE_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001640
1641 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001642
Andrei Onea24ec3212019-03-15 17:35:05 +00001643 @UnsupportedAppUsage
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001644 public int states;
1645
Dianne Hackborn3251b902014-06-20 14:40:53 -07001646 // Constants from WIFI_SUPPL_STATE_*
1647 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1648 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1649 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1650 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1651 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1652 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
Siddharth Ray78ccaf52017-12-23 16:16:21 -08001653 // Values for NUM_GPS_SIGNAL_QUALITY_LEVELS
1654 public static final int STATE2_GPS_SIGNAL_QUALITY_SHIFT = 7;
1655 public static final int STATE2_GPS_SIGNAL_QUALITY_MASK =
1656 0x1 << STATE2_GPS_SIGNAL_QUALITY_SHIFT;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001657
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001658 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001659 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1660 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1661 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001662 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001663 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1664 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1665 public static final int STATE2_CHARGING_FLAG = 1<<24;
1666 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1667 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1668 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001669 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Siddharth Rayf5e796a2018-01-22 18:18:17 -08001670 public static final int STATE2_CELLULAR_HIGH_TX_POWER_FLAG = 1 << 19;
Mike Ma926a97c2018-03-25 02:32:35 -07001671 public static final int STATE2_USB_DATA_LINK_FLAG = 1 << 18;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001672
1673 public static final int MOST_INTERESTING_STATES2 =
Mike Mac2f518a2017-09-19 16:06:03 -07001674 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
1675 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001676
1677 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001678
Andrei Onea24ec3212019-03-15 17:35:05 +00001679 @UnsupportedAppUsage
Dianne Hackborn40c87252014-03-19 16:55:40 -07001680 public int states2;
1681
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001682 // The wake lock that was acquired at this point.
1683 public HistoryTag wakelockTag;
1684
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001685 // Kernel wakeup reason at this point.
1686 public HistoryTag wakeReasonTag;
1687
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001688 // Non-null when there is more detailed information at this step.
1689 public HistoryStepDetails stepDetails;
1690
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001691 public static final int EVENT_FLAG_START = 0x8000;
1692 public static final int EVENT_FLAG_FINISH = 0x4000;
1693
1694 // No event in this item.
1695 public static final int EVENT_NONE = 0x0000;
1696 // Event is about a process that is running.
1697 public static final int EVENT_PROC = 0x0001;
1698 // Event is about an application package that is in the foreground.
1699 public static final int EVENT_FOREGROUND = 0x0002;
1700 // Event is about an application package that is at the top of the screen.
1701 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001702 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001703 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001704 // Events for all additional wake locks aquired/release within a wake block.
1705 // These are not generated by default.
1706 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001707 // Event is about an application executing a scheduled job.
1708 public static final int EVENT_JOB = 0x0006;
1709 // Events for users running.
1710 public static final int EVENT_USER_RUNNING = 0x0007;
1711 // Events for foreground user.
1712 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001713 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001714 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001715 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001716 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001717 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001718 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001719 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001720 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001721 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001722 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001723 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001724 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001725 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001726 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001727 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001728 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001729 // Event for a package being on the temporary whitelist.
1730 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001731 // Event for the screen waking up.
1732 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001733 // Event for the UID that woke up the application processor.
1734 // Used for wakeups coming from WiFi, modem, etc.
1735 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001736 // Event for reporting that a specific partial wake lock has been held for a long duration.
1737 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001738
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001739 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001740 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001741 // Mask to extract out only the type part of the event.
1742 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001743
1744 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1745 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1746 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1747 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1748 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1749 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001750 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1751 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001752 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1753 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001754 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1755 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1756 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1757 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1758 public static final int EVENT_USER_FOREGROUND_START =
1759 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1760 public static final int EVENT_USER_FOREGROUND_FINISH =
1761 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001762 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1763 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001764 public static final int EVENT_TEMP_WHITELIST_START =
1765 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1766 public static final int EVENT_TEMP_WHITELIST_FINISH =
1767 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001768 public static final int EVENT_LONG_WAKE_LOCK_START =
1769 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1770 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1771 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001772
1773 // For CMD_EVENT.
1774 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001775 public HistoryTag eventTag;
1776
Dianne Hackborn9a755432014-05-15 17:05:22 -07001777 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001778 public long currentTime;
1779
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001780 // Meta-data when reading.
1781 public int numReadInts;
1782
1783 // Pre-allocated objects.
1784 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001785 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001786 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001787
Andrei Onea24ec3212019-03-15 17:35:05 +00001788 @UnsupportedAppUsage
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001789 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001790 }
Bookatzc8c44962017-05-11 12:12:54 -07001791
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001792 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001793 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001794 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001795 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001796 }
Bookatzc8c44962017-05-11 12:12:54 -07001797
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001798 public int describeContents() {
1799 return 0;
1800 }
1801
1802 public void writeToParcel(Parcel dest, int flags) {
1803 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001804 int bat = (((int)cmd)&0xff)
1805 | ((((int)batteryLevel)<<8)&0xff00)
1806 | ((((int)batteryStatus)<<16)&0xf0000)
1807 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001808 | ((((int)batteryPlugType)<<24)&0xf000000)
1809 | (wakelockTag != null ? 0x10000000 : 0)
1810 | (wakeReasonTag != null ? 0x20000000 : 0)
1811 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001812 dest.writeInt(bat);
1813 bat = (((int)batteryTemperature)&0xffff)
1814 | ((((int)batteryVoltage)<<16)&0xffff0000);
1815 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001816 dest.writeInt(batteryChargeUAh);
Blake Kragtencb308d92019-01-28 10:54:13 -08001817 dest.writeDouble(modemRailChargeMah);
1818 dest.writeDouble(wifiRailChargeMah);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001819 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001820 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001821 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001822 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001823 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001824 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001825 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001826 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001827 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001828 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001829 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001830 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001831 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001832 dest.writeLong(currentTime);
1833 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001834 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001835
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001836 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001837 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001838 int bat = src.readInt();
1839 cmd = (byte)(bat&0xff);
1840 batteryLevel = (byte)((bat>>8)&0xff);
1841 batteryStatus = (byte)((bat>>16)&0xf);
1842 batteryHealth = (byte)((bat>>20)&0xf);
1843 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001844 int bat2 = src.readInt();
1845 batteryTemperature = (short)(bat2&0xffff);
1846 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001847 batteryChargeUAh = src.readInt();
Blake Kragtencb308d92019-01-28 10:54:13 -08001848 modemRailChargeMah = src.readDouble();
1849 wifiRailChargeMah = src.readDouble();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001850 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001851 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001852 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001853 wakelockTag = localWakelockTag;
1854 wakelockTag.readFromParcel(src);
1855 } else {
1856 wakelockTag = null;
1857 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001858 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001859 wakeReasonTag = localWakeReasonTag;
1860 wakeReasonTag.readFromParcel(src);
1861 } else {
1862 wakeReasonTag = null;
1863 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001864 if ((bat&0x40000000) != 0) {
1865 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001866 eventTag = localEventTag;
1867 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001868 } else {
1869 eventCode = EVENT_NONE;
1870 eventTag = null;
1871 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001872 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001873 currentTime = src.readLong();
1874 } else {
1875 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001876 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001877 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001878 }
1879
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001880 public void clear() {
1881 time = 0;
1882 cmd = CMD_NULL;
1883 batteryLevel = 0;
1884 batteryStatus = 0;
1885 batteryHealth = 0;
1886 batteryPlugType = 0;
1887 batteryTemperature = 0;
1888 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001889 batteryChargeUAh = 0;
Blake Kragtencb308d92019-01-28 10:54:13 -08001890 modemRailChargeMah = 0;
1891 wifiRailChargeMah = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001892 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001893 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001894 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001895 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001896 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001897 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001898 }
Bookatzc8c44962017-05-11 12:12:54 -07001899
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001900 public void setTo(HistoryItem o) {
1901 time = o.time;
1902 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001903 setToCommon(o);
1904 }
1905
1906 public void setTo(long time, byte cmd, HistoryItem o) {
1907 this.time = time;
1908 this.cmd = cmd;
1909 setToCommon(o);
1910 }
1911
1912 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001913 batteryLevel = o.batteryLevel;
1914 batteryStatus = o.batteryStatus;
1915 batteryHealth = o.batteryHealth;
1916 batteryPlugType = o.batteryPlugType;
1917 batteryTemperature = o.batteryTemperature;
1918 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001919 batteryChargeUAh = o.batteryChargeUAh;
Blake Kragtencb308d92019-01-28 10:54:13 -08001920 modemRailChargeMah = o.modemRailChargeMah;
1921 wifiRailChargeMah = o.wifiRailChargeMah;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001922 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001923 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001924 if (o.wakelockTag != null) {
1925 wakelockTag = localWakelockTag;
1926 wakelockTag.setTo(o.wakelockTag);
1927 } else {
1928 wakelockTag = null;
1929 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001930 if (o.wakeReasonTag != null) {
1931 wakeReasonTag = localWakeReasonTag;
1932 wakeReasonTag.setTo(o.wakeReasonTag);
1933 } else {
1934 wakeReasonTag = null;
1935 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001936 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001937 if (o.eventTag != null) {
1938 eventTag = localEventTag;
1939 eventTag.setTo(o.eventTag);
1940 } else {
1941 eventTag = null;
1942 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001943 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001944 }
1945
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001946 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001947 return batteryLevel == o.batteryLevel
1948 && batteryStatus == o.batteryStatus
1949 && batteryHealth == o.batteryHealth
1950 && batteryPlugType == o.batteryPlugType
1951 && batteryTemperature == o.batteryTemperature
1952 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001953 && batteryChargeUAh == o.batteryChargeUAh
Blake Kragtencb308d92019-01-28 10:54:13 -08001954 && modemRailChargeMah == o.modemRailChargeMah
1955 && wifiRailChargeMah == o.wifiRailChargeMah
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001956 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001957 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001958 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001959 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001960
1961 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001962 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001963 return false;
1964 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001965 if (wakelockTag != o.wakelockTag) {
1966 if (wakelockTag == null || o.wakelockTag == null) {
1967 return false;
1968 }
1969 if (!wakelockTag.equals(o.wakelockTag)) {
1970 return false;
1971 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001972 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001973 if (wakeReasonTag != o.wakeReasonTag) {
1974 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1975 return false;
1976 }
1977 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1978 return false;
1979 }
1980 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001981 if (eventTag != o.eventTag) {
1982 if (eventTag == null || o.eventTag == null) {
1983 return false;
1984 }
1985 if (!eventTag.equals(o.eventTag)) {
1986 return false;
1987 }
1988 }
1989 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001990 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001991 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001992
1993 public final static class HistoryEventTracker {
1994 private final HashMap<String, SparseIntArray>[] mActiveEvents
1995 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1996
1997 public boolean updateState(int code, String name, int uid, int poolIdx) {
1998 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1999 int idx = code&HistoryItem.EVENT_TYPE_MASK;
2000 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
2001 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002002 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07002003 mActiveEvents[idx] = active;
2004 }
2005 SparseIntArray uids = active.get(name);
2006 if (uids == null) {
2007 uids = new SparseIntArray();
2008 active.put(name, uids);
2009 }
2010 if (uids.indexOfKey(uid) >= 0) {
2011 // Already set, nothing to do!
2012 return false;
2013 }
2014 uids.put(uid, poolIdx);
2015 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
2016 int idx = code&HistoryItem.EVENT_TYPE_MASK;
2017 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
2018 if (active == null) {
2019 // not currently active, nothing to do.
2020 return false;
2021 }
2022 SparseIntArray uids = active.get(name);
2023 if (uids == null) {
2024 // not currently active, nothing to do.
2025 return false;
2026 }
2027 idx = uids.indexOfKey(uid);
2028 if (idx < 0) {
2029 // not currently active, nothing to do.
2030 return false;
2031 }
2032 uids.removeAt(idx);
2033 if (uids.size() <= 0) {
2034 active.remove(name);
2035 }
2036 }
2037 return true;
2038 }
2039
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002040 public void removeEvents(int code) {
2041 int idx = code&HistoryItem.EVENT_TYPE_MASK;
2042 mActiveEvents[idx] = null;
2043 }
2044
Dianne Hackborn37de0982014-05-09 09:32:18 -07002045 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
2046 return mActiveEvents[code];
2047 }
2048 }
2049
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002050 public static final class BitDescription {
2051 public final int mask;
2052 public final int shift;
2053 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002054 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002055 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002056 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07002057
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002058 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002059 this.mask = mask;
2060 this.shift = -1;
2061 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002062 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002063 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002064 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002065 }
Bookatzc8c44962017-05-11 12:12:54 -07002066
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002067 public BitDescription(int mask, int shift, String name, String shortName,
2068 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002069 this.mask = mask;
2070 this.shift = shift;
2071 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002072 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002073 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002074 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002075 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002076 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07002077
Dianne Hackbornfc064132014-06-02 12:42:12 -07002078 /**
2079 * Don't allow any more batching in to the current history event. This
2080 * is called when printing partial histories, so to ensure that the next
2081 * history event will go in to a new batch after what was printed in the
2082 * last partial history.
2083 */
2084 public abstract void commitCurrentHistoryBatchLocked();
2085
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002086 public abstract int getHistoryTotalSize();
2087
2088 public abstract int getHistoryUsedSize();
2089
Andrei Onea24ec3212019-03-15 17:35:05 +00002090 @UnsupportedAppUsage
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002091 public abstract boolean startIteratingHistoryLocked();
2092
Dianne Hackborn099bc622014-01-22 13:39:16 -08002093 public abstract int getHistoryStringPoolSize();
2094
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002095 public abstract int getHistoryStringPoolBytes();
2096
2097 public abstract String getHistoryTagPoolString(int index);
2098
2099 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002100
Andrei Onea24ec3212019-03-15 17:35:05 +00002101 @UnsupportedAppUsage
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002102 public abstract boolean getNextHistoryLocked(HistoryItem out);
2103
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002104 public abstract void finishIteratingHistoryLocked();
2105
2106 public abstract boolean startIteratingOldHistoryLocked();
2107
2108 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
2109
2110 public abstract void finishIteratingOldHistoryLocked();
2111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002113 * Return the base time offset for the battery history.
2114 */
2115 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07002116
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002117 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 * Returns the number of times the device has been started.
2119 */
2120 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07002121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002123 * 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 -08002124 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002125 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 * {@hide}
2127 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002128 @UnsupportedAppUsage
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002129 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002130
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002131 /**
2132 * Returns the number of times the screen was turned on.
2133 *
2134 * {@hide}
2135 */
2136 public abstract int getScreenOnCount(int which);
2137
Mike Mac2f518a2017-09-19 16:06:03 -07002138 /**
2139 * Returns the time in microseconds that the screen has been dozing while the device was
2140 * running on battery.
2141 *
2142 * {@hide}
2143 */
2144 public abstract long getScreenDozeTime(long elapsedRealtimeUs, int which);
2145
2146 /**
2147 * Returns the number of times the screen was turned dozing.
2148 *
2149 * {@hide}
2150 */
2151 public abstract int getScreenDozeCount(int which);
2152
Jeff Browne95c3cd2014-05-02 16:59:26 -07002153 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
2154
Dianne Hackborn617f8772009-03-31 15:04:46 -07002155 public static final int SCREEN_BRIGHTNESS_DARK = 0;
2156 public static final int SCREEN_BRIGHTNESS_DIM = 1;
2157 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
2158 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
2159 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07002160
Dianne Hackborn617f8772009-03-31 15:04:46 -07002161 static final String[] SCREEN_BRIGHTNESS_NAMES = {
2162 "dark", "dim", "medium", "light", "bright"
2163 };
Bookatzc8c44962017-05-11 12:12:54 -07002164
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002165 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
2166 "0", "1", "2", "3", "4"
2167 };
2168
Andrei Onea24ec3212019-03-15 17:35:05 +00002169 @UnsupportedAppUsage
Dianne Hackborn617f8772009-03-31 15:04:46 -07002170 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07002171
Dianne Hackborn617f8772009-03-31 15:04:46 -07002172 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002173 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07002174 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07002175 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002176 * {@hide}
2177 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002178 @UnsupportedAppUsage
Dianne Hackborn617f8772009-03-31 15:04:46 -07002179 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002180 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002183 * Returns the {@link Timer} object that tracks the given screen brightness.
2184 *
2185 * {@hide}
2186 */
2187 public abstract Timer getScreenBrightnessTimer(int brightnessBin);
2188
2189 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002190 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002191 * running on battery.
2192 *
2193 * {@hide}
2194 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002195 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002196
2197 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002198 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002199 *
2200 * {@hide}
2201 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002202 public abstract int getPowerSaveModeEnabledCount(int which);
2203
2204 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002205 * Constant for device idle mode: not active.
2206 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002207 public static final int DEVICE_IDLE_MODE_OFF = ServerProtoEnums.DEVICE_IDLE_MODE_OFF; // 0
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002208
2209 /**
2210 * Constant for device idle mode: active in lightweight mode.
2211 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002212 public static final int DEVICE_IDLE_MODE_LIGHT = ServerProtoEnums.DEVICE_IDLE_MODE_LIGHT; // 1
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002213
2214 /**
2215 * Constant for device idle mode: active in full mode.
2216 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002217 public static final int DEVICE_IDLE_MODE_DEEP = ServerProtoEnums.DEVICE_IDLE_MODE_DEEP; // 2
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002218
2219 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002220 * Returns the time in microseconds that device has been in idle mode while
2221 * running on battery.
2222 *
2223 * {@hide}
2224 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002225 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002226
2227 /**
2228 * Returns the number of times that the devie has gone in to idle mode.
2229 *
2230 * {@hide}
2231 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002232 public abstract int getDeviceIdleModeCount(int mode, int which);
2233
2234 /**
2235 * Return the longest duration we spent in a particular device idle mode (fully in the
2236 * mode, not in idle maintenance etc).
2237 */
2238 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002239
2240 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002241 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002242 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002243 * counts all of the time that we consider the device to be idle, whether or not
2244 * it is currently in the actual device idle mode.
2245 *
2246 * {@hide}
2247 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002248 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002249
2250 /**
Bookatz8c6571b2017-10-24 15:04:41 -07002251 * Returns the number of times that the device has started idling.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002252 *
2253 * {@hide}
2254 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002255 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002256
2257 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002258 * Returns the number of times that connectivity state changed.
2259 *
2260 * {@hide}
2261 */
2262 public abstract int getNumConnectivityChange(int which);
2263
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002264
2265 /**
2266 * Returns the time in microseconds that the phone has been running with
2267 * the given GPS signal quality level
2268 *
2269 * {@hide}
2270 */
2271 public abstract long getGpsSignalQualityTime(int strengthBin,
2272 long elapsedRealtimeUs, int which);
2273
2274 /**
2275 * Returns the GPS battery drain in mA-ms
2276 *
2277 * {@hide}
2278 */
2279 public abstract long getGpsBatteryDrainMaMs();
2280
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002281 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002282 * 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 -08002283 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002284 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 * {@hide}
2286 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002287 @UnsupportedAppUsage
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002288 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002289
Dianne Hackborn627bba72009-03-24 22:32:56 -07002290 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002291 * Returns the number of times a phone call was activated.
2292 *
2293 * {@hide}
2294 */
2295 public abstract int getPhoneOnCount(int which);
2296
2297 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002298 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002299 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002300 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002301 * {@hide}
2302 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002303 @UnsupportedAppUsage
Dianne Hackborn627bba72009-03-24 22:32:56 -07002304 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002305 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002306
Dianne Hackborn617f8772009-03-31 15:04:46 -07002307 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07002308 * Returns the time in microseconds that the phone has been trying to
2309 * acquire a signal.
2310 *
2311 * {@hide}
2312 */
2313 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002314 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002315
2316 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002317 * Returns the {@link Timer} object that tracks how much the phone has been trying to
2318 * acquire a signal.
2319 *
2320 * {@hide}
2321 */
2322 public abstract Timer getPhoneSignalScanningTimer();
2323
2324 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002325 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002326 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002327 * {@hide}
2328 */
2329 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
2330
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002331 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002332 * Return the {@link Timer} object used to track the given signal strength's duration and
2333 * counts.
2334 */
2335 protected abstract Timer getPhoneSignalStrengthTimer(int strengthBin);
2336
2337 /**
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002338 * Returns the time in microseconds that the mobile network has been active
2339 * (in a high power state).
2340 *
2341 * {@hide}
2342 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002343 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002344
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002345 /**
2346 * Returns the number of times that the mobile network has transitioned to the
2347 * active state.
2348 *
2349 * {@hide}
2350 */
2351 public abstract int getMobileRadioActiveCount(int which);
2352
2353 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002354 * Returns the time in microseconds that is the difference between the mobile radio
2355 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
2356 * from the radio.
2357 *
2358 * {@hide}
2359 */
2360 public abstract long getMobileRadioActiveAdjustedTime(int which);
2361
2362 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002363 * Returns the time in microseconds that the mobile network has been active
2364 * (in a high power state) but not being able to blame on an app.
2365 *
2366 * {@hide}
2367 */
2368 public abstract long getMobileRadioActiveUnknownTime(int which);
2369
2370 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002371 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002372 *
2373 * {@hide}
2374 */
2375 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002376
Blake Kragtenbc75c722019-03-20 17:14:58 -07002377 public static final int DATA_CONNECTION_OUT_OF_SERVICE = 0;
2378 public static final int DATA_CONNECTION_EMERGENCY_SERVICE =
2379 TelephonyManager.MAX_NETWORK_TYPE + 1;
2380 public static final int DATA_CONNECTION_OTHER = DATA_CONNECTION_EMERGENCY_SERVICE + 1;
2381
Robert Greenwalt962a9902010-11-02 11:10:25 -07002382
Dianne Hackborn627bba72009-03-24 22:32:56 -07002383 static final String[] DATA_CONNECTION_NAMES = {
Blake Kragtenbc75c722019-03-20 17:14:58 -07002384 "oos", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002385 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Bookatzdf6baff2018-12-12 19:38:11 -08002386 "ehrpd", "hspap", "gsm", "td_scdma", "iwlan", "lte_ca", "nr",
Blake Kragtenbc75c722019-03-20 17:14:58 -07002387 "emngcy", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002388 };
Bookatzc8c44962017-05-11 12:12:54 -07002389
Andrei Onea24ec3212019-03-15 17:35:05 +00002390 @UnsupportedAppUsage
Blake Kragtenbc75c722019-03-20 17:14:58 -07002391 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER + 1;
Bookatzc8c44962017-05-11 12:12:54 -07002392
Dianne Hackborn627bba72009-03-24 22:32:56 -07002393 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002394 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002395 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002396 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002397 * {@hide}
2398 */
2399 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002400 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002403 * Returns the number of times the phone has entered the given data
2404 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002405 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002406 * {@hide}
2407 */
2408 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002409
Kweku Adams87b19ec2017-10-09 12:40:03 -07002410 /**
2411 * Returns the {@link Timer} object that tracks the phone's data connection type stats.
2412 */
2413 public abstract Timer getPhoneDataConnectionTimer(int dataType);
2414
Dianne Hackborn3251b902014-06-20 14:40:53 -07002415 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2416 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2417 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2418 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2419 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2420 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2421 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2422 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2423 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2424 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2425 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2426 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2427 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2428
2429 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2430
2431 static final String[] WIFI_SUPPL_STATE_NAMES = {
2432 "invalid", "disconn", "disabled", "inactive", "scanning",
2433 "authenticating", "associating", "associated", "4-way-handshake",
2434 "group-handshake", "completed", "dormant", "uninit"
2435 };
2436
2437 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2438 "inv", "dsc", "dis", "inact", "scan",
2439 "auth", "ascing", "asced", "4-way",
2440 "group", "compl", "dorm", "uninit"
2441 };
2442
Mike Mac2f518a2017-09-19 16:06:03 -07002443 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002444 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002445 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2446 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002447 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002448 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2449 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2450 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002451 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002452 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002453 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002454 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002455 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2456 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Mike Mac2f518a2017-09-19 16:06:03 -07002457 new BitDescription(HistoryItem.STATE_SCREEN_DOZE_FLAG, "screen_doze", "Sd"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002458 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2459 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2460 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2461 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2462 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2463 new String[] {"in", "out", "emergency", "off"},
2464 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002465 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2466 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2467 SignalStrength.SIGNAL_STRENGTH_NAMES,
2468 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002469 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2470 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2471 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002472 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002473
Mike Ma926a97c2018-03-25 02:32:35 -07002474 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002475 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002476 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002477 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002478 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002479 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002480 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2481 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2482 new String[] { "off", "light", "full", "???" },
2483 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002484 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
Mike Ma926a97c2018-03-25 02:32:35 -07002485 new BitDescription(HistoryItem.STATE2_USB_DATA_LINK_FLAG, "usb_data", "Ud"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002486 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2487 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002488 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2489 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2490 new String[] { "0", "1", "2", "3", "4" },
2491 new String[] { "0", "1", "2", "3", "4" }),
2492 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2493 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2494 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002495 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002496 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Siddharth Rayf5e796a2018-01-22 18:18:17 -08002497 new BitDescription(HistoryItem.STATE2_CELLULAR_HIGH_TX_POWER_FLAG,
2498 "cellular_high_tx_power", "Chtp"),
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002499 new BitDescription(HistoryItem.STATE2_GPS_SIGNAL_QUALITY_MASK,
2500 HistoryItem.STATE2_GPS_SIGNAL_QUALITY_SHIFT, "gps_signal_quality", "Gss",
Siddharth Rayf5e796a2018-01-22 18:18:17 -08002501 new String[] { "poor", "good"}, new String[] { "poor", "good"})
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002502 };
2503
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002504 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002505 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002506 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2507 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002508 };
2509
2510 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002511 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002512 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002513 "Esw", "Ewa", "Elw", "Eec"
2514 };
2515
2516 @FunctionalInterface
2517 public interface IntToString {
2518 String applyAsString(int val);
2519 }
2520
2521 private static final IntToString sUidToString = UserHandle::formatUid;
2522 private static final IntToString sIntToString = Integer::toString;
2523
2524 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2525 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
Hirono Shiyama50ea4d02018-08-27 16:41:28 +09002526 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sIntToString,
Adam Lesinski041d9172016-12-12 12:03:56 -08002527 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2528 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002529 };
2530
Dianne Hackborn617f8772009-03-31 15:04:46 -07002531 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08002532 * Returns total time for WiFi Multicast Wakelock timer.
2533 * Note that this may be different from the sum of per uid timer values.
2534 *
2535 * {@hide}
2536 */
2537 public abstract long getWifiMulticastWakelockTime(long elapsedRealtimeUs, int which);
2538
2539 /**
2540 * Returns total time for WiFi Multicast Wakelock timer
2541 * Note that this may be different from the sum of per uid timer values.
2542 *
2543 * {@hide}
2544 */
2545 public abstract int getWifiMulticastWakelockCount(int which);
2546
2547 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002548 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002549 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002550 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002551 * {@hide}
2552 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002553 @UnsupportedAppUsage
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002554 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002555
2556 /**
Siddharth Rayb50a6842017-12-14 15:15:28 -08002557 * Returns the time in microseconds that wifi has been active while the device was
2558 * running on battery.
2559 *
2560 * {@hide}
2561 */
2562 public abstract long getWifiActiveTime(long elapsedRealtimeUs, int which);
2563
2564 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002565 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002566 * been in the running state while the device was running on battery.
2567 *
2568 * {@hide}
2569 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002570 @UnsupportedAppUsage
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002571 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002572
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002573 public static final int WIFI_STATE_OFF = 0;
2574 public static final int WIFI_STATE_OFF_SCANNING = 1;
2575 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2576 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2577 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2578 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2579 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2580 public static final int WIFI_STATE_SOFT_AP = 7;
2581
2582 static final String[] WIFI_STATE_NAMES = {
2583 "off", "scanning", "no_net", "disconn",
2584 "sta", "p2p", "sta_p2p", "soft_ap"
2585 };
2586
2587 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2588
2589 /**
2590 * Returns the time in microseconds that WiFi has been running in the given state.
2591 *
2592 * {@hide}
2593 */
2594 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002595 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002596
2597 /**
2598 * Returns the number of times that WiFi has entered the given state.
2599 *
2600 * {@hide}
2601 */
2602 public abstract int getWifiStateCount(int wifiState, int which);
2603
The Android Open Source Project10592532009-03-18 17:39:46 -07002604 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002605 * Returns the {@link Timer} object that tracks the given WiFi state.
2606 *
2607 * {@hide}
2608 */
2609 public abstract Timer getWifiStateTimer(int wifiState);
2610
2611 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002612 * Returns the time in microseconds that the wifi supplicant has been
2613 * in a given state.
2614 *
2615 * {@hide}
2616 */
2617 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2618
2619 /**
2620 * Returns the number of times that the wifi supplicant has transitioned
2621 * to a given state.
2622 *
2623 * {@hide}
2624 */
2625 public abstract int getWifiSupplStateCount(int state, int which);
2626
Kweku Adams87b19ec2017-10-09 12:40:03 -07002627 /**
2628 * Returns the {@link Timer} object that tracks the given wifi supplicant state.
2629 *
2630 * {@hide}
2631 */
2632 public abstract Timer getWifiSupplStateTimer(int state);
2633
Dianne Hackborn3251b902014-06-20 14:40:53 -07002634 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2635
2636 /**
2637 * Returns the time in microseconds that WIFI has been running with
2638 * the given signal strength.
2639 *
2640 * {@hide}
2641 */
2642 public abstract long getWifiSignalStrengthTime(int strengthBin,
2643 long elapsedRealtimeUs, int which);
2644
2645 /**
2646 * Returns the number of times WIFI has entered the given signal strength.
2647 *
2648 * {@hide}
2649 */
2650 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2651
2652 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002653 * Returns the {@link Timer} object that tracks the given WIFI signal strength.
2654 *
2655 * {@hide}
2656 */
2657 public abstract Timer getWifiSignalStrengthTimer(int strengthBin);
2658
2659 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002660 * Returns the time in microseconds that the flashlight has been on while the device was
2661 * running on battery.
2662 *
2663 * {@hide}
2664 */
2665 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2666
2667 /**
2668 * Returns the number of times that the flashlight has been turned on while the device was
2669 * running on battery.
2670 *
2671 * {@hide}
2672 */
2673 public abstract long getFlashlightOnCount(int which);
2674
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002675 /**
2676 * Returns the time in microseconds that the camera has been on while the device was
2677 * running on battery.
2678 *
2679 * {@hide}
2680 */
2681 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2682
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002683 /**
2684 * Returns the time in microseconds that bluetooth scans were running while the device was
2685 * on battery.
2686 *
2687 * {@hide}
2688 */
2689 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002690
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002691 public static final int NETWORK_MOBILE_RX_DATA = 0;
2692 public static final int NETWORK_MOBILE_TX_DATA = 1;
2693 public static final int NETWORK_WIFI_RX_DATA = 2;
2694 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002695 public static final int NETWORK_BT_RX_DATA = 4;
2696 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002697 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2698 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2699 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2700 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2701 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002702
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002703 public abstract long getNetworkActivityBytes(int type, int which);
2704 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002705
Adam Lesinskie08af192015-03-25 16:42:59 -07002706 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002707 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002708 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002709 * actual power data.
2710 */
2711 public abstract boolean hasWifiActivityReporting();
2712
2713 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002714 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2715 * in various radio controller states, such as transmit, receive, and idle.
2716 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002717 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002718 public abstract ControllerActivityCounter getWifiControllerActivity();
2719
2720 /**
2721 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2722 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2723 * actual power data.
2724 */
2725 public abstract boolean hasBluetoothActivityReporting();
2726
2727 /**
2728 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2729 * in various radio controller states, such as transmit, receive, and idle.
2730 * @return non-null {@link ControllerActivityCounter}
2731 */
2732 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2733
2734 /**
2735 * Returns true if the BatteryStats object has detailed modem power reports.
2736 * When true, calling {@link #getModemControllerActivity()} will yield the
2737 * actual power data.
2738 */
2739 public abstract boolean hasModemActivityReporting();
2740
2741 /**
2742 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2743 * in various radio controller states, such as transmit, receive, and idle.
2744 * @return non-null {@link ControllerActivityCounter}
2745 */
2746 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002747
The Android Open Source Project10592532009-03-18 17:39:46 -07002748 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002749 * Return the wall clock time when battery stats data collection started.
2750 */
2751 public abstract long getStartClockTime();
2752
2753 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002754 * Return platform version tag that we were running in when the battery stats started.
2755 */
2756 public abstract String getStartPlatformVersion();
2757
2758 /**
2759 * Return platform version tag that we were running in when the battery stats ended.
2760 */
2761 public abstract String getEndPlatformVersion();
2762
2763 /**
2764 * Return the internal version code of the parcelled format.
2765 */
2766 public abstract int getParcelVersion();
2767
2768 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 * Return whether we are currently running on battery.
2770 */
2771 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 /**
2774 * Returns a SparseArray containing the statistics for each uid.
2775 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002776 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 public abstract SparseArray<? extends Uid> getUidStats();
2778
2779 /**
2780 * Returns the current battery uptime in microseconds.
2781 *
2782 * @param curTime the amount of elapsed realtime in microseconds.
2783 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002784 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 public abstract long getBatteryUptime(long curTime);
2786
2787 /**
2788 * Returns the current battery realtime in microseconds.
2789 *
2790 * @param curTime the amount of elapsed realtime in microseconds.
2791 */
2792 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002793
The Android Open Source Project10592532009-03-18 17:39:46 -07002794 /**
Evan Millar633a1742009-04-02 16:36:33 -07002795 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002796 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002797 */
Evan Millar633a1742009-04-02 16:36:33 -07002798 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002799
The Android Open Source Project10592532009-03-18 17:39:46 -07002800 /**
Evan Millar633a1742009-04-02 16:36:33 -07002801 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2802 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002803 */
Evan Millar633a1742009-04-02 16:36:33 -07002804 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805
2806 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002807 * Get the amount the battery has discharged since the stats were
2808 * last reset after charging, as a lower-end approximation.
2809 */
2810 public abstract int getLowDischargeAmountSinceCharge();
2811
2812 /**
2813 * Get the amount the battery has discharged since the stats were
2814 * last reset after charging, as an upper-end approximation.
2815 */
2816 public abstract int getHighDischargeAmountSinceCharge();
2817
2818 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002819 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2820 */
2821 public abstract int getDischargeAmount(int which);
2822
2823 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002824 * Get the amount the battery has discharged while the screen was on,
2825 * since the last time power was unplugged.
2826 */
2827 public abstract int getDischargeAmountScreenOn();
2828
2829 /**
2830 * Get the amount the battery has discharged while the screen was on,
2831 * since the last time the device was charged.
2832 */
2833 public abstract int getDischargeAmountScreenOnSinceCharge();
2834
2835 /**
2836 * Get the amount the battery has discharged while the screen was off,
2837 * since the last time power was unplugged.
2838 */
2839 public abstract int getDischargeAmountScreenOff();
2840
2841 /**
2842 * Get the amount the battery has discharged while the screen was off,
2843 * since the last time the device was charged.
2844 */
2845 public abstract int getDischargeAmountScreenOffSinceCharge();
2846
2847 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002848 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002849 * since the last time power was unplugged.
2850 */
2851 public abstract int getDischargeAmountScreenDoze();
2852
2853 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002854 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002855 * since the last time the device was charged.
2856 */
2857 public abstract int getDischargeAmountScreenDozeSinceCharge();
2858
2859 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 * Returns the total, last, or current battery uptime in microseconds.
2861 *
2862 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002863 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002865 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 public abstract long computeBatteryUptime(long curTime, int which);
2867
2868 /**
2869 * Returns the total, last, or current battery realtime in microseconds.
2870 *
2871 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002872 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002874 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 public abstract long computeBatteryRealtime(long curTime, int which);
2876
2877 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002878 * Returns the total, last, or current battery screen off/doze uptime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002879 *
2880 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002881 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002882 */
2883 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2884
2885 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002886 * Returns the total, last, or current battery screen off/doze realtime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002887 *
2888 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002889 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002890 */
2891 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2892
2893 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 * Returns the total, last, or current uptime in microseconds.
2895 *
2896 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002897 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 */
2899 public abstract long computeUptime(long curTime, int which);
2900
2901 /**
2902 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002903 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002905 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 */
2907 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002908
2909 /**
2910 * Compute an approximation for how much run time (in microseconds) is remaining on
2911 * the battery. Returns -1 if no time can be computed: either there is not
2912 * enough current data to make a decision, or the battery is currently
2913 * charging.
2914 *
2915 * @param curTime The current elepsed realtime in microseconds.
2916 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002917 @UnsupportedAppUsage
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002918 public abstract long computeBatteryTimeRemaining(long curTime);
2919
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002920 // The part of a step duration that is the actual time.
2921 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2922
2923 // Bits in a step duration that are the new battery level we are at.
2924 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002925 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002926
2927 // Bits in a step duration that are the initial mode we were in at that step.
2928 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002929 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002930
2931 // Bits in a step duration that indicate which modes changed during that step.
2932 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002933 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002934
2935 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2936 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2937
Santos Cordone94f0502017-02-24 12:31:20 -08002938 // The largest value for screen state that is tracked in battery states. Any values above
2939 // this should be mapped back to one of the tracked values before being tracked here.
2940 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2941
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002942 // Step duration mode: power save is on.
2943 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2944
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002945 // Step duration mode: device is currently in idle mode.
2946 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2947
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002948 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2949 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002950 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2951 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002952 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2953 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2954 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2955 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2956 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002957 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2958 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002959 };
2960 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2961 (Display.STATE_OFF-1),
2962 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002963 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002964 (Display.STATE_ON-1),
2965 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2966 (Display.STATE_DOZE-1),
2967 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2968 (Display.STATE_DOZE_SUSPEND-1),
2969 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002970 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002971 };
2972 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2973 "screen off",
2974 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002975 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002976 "screen on",
2977 "screen on power save",
2978 "screen doze",
2979 "screen doze power save",
2980 "screen doze-suspend",
2981 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002982 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002983 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002984
2985 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002986 * Return the amount of battery discharge while the screen was off, measured in
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002987 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2988 * a coulomb counter.
2989 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002990 public abstract long getUahDischargeScreenOff(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002991
2992 /**
2993 * Return the amount of battery discharge while the screen was in doze mode, measured in
2994 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2995 * a coulomb counter.
2996 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002997 public abstract long getUahDischargeScreenDoze(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002998
2999 /**
3000 * Return the amount of battery discharge measured in micro-Ampere-hours. This will be
3001 * non-zero only if the device's battery has a coulomb counter.
3002 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07003003 public abstract long getUahDischarge(int which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003004
3005 /**
Mike Ma15313c92017-11-15 17:58:21 -08003006 * @return the amount of battery discharge while the device is in light idle mode, measured in
3007 * micro-Ampere-hours.
3008 */
3009 public abstract long getUahDischargeLightDoze(int which);
3010
3011 /**
3012 * @return the amount of battery discharge while the device is in deep idle mode, measured in
3013 * micro-Ampere-hours.
3014 */
3015 public abstract long getUahDischargeDeepDoze(int which);
3016
3017 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003018 * Returns the estimated real battery capacity, which may be less than the capacity
3019 * declared by the PowerProfile.
3020 * @return The estimated battery capacity in mAh.
3021 */
3022 public abstract int getEstimatedBatteryCapacity();
3023
3024 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07003025 * @return The minimum learned battery capacity in uAh.
3026 */
3027 public abstract int getMinLearnedBatteryCapacity();
3028
3029 /**
3030 * @return The maximum learned battery capacity in uAh.
3031 */
3032 public abstract int getMaxLearnedBatteryCapacity() ;
3033
3034 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003035 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003036 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003037 public abstract LevelStepTracker getDischargeLevelStepTracker();
3038
3039 /**
3040 * Return the array of daily discharge step durations.
3041 */
3042 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003043
3044 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003045 * Compute an approximation for how much time (in microseconds) remains until the battery
3046 * is fully charged. Returns -1 if no time can be computed: either there is not
3047 * enough current data to make a decision, or the battery is currently
3048 * discharging.
3049 *
3050 * @param curTime The current elepsed realtime in microseconds.
3051 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003052 @UnsupportedAppUsage
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003053 public abstract long computeChargeTimeRemaining(long curTime);
3054
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003055 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003056 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003057 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003058 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003059
3060 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003061 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003062 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003063 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003064
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003065 public abstract ArrayList<PackageChange> getDailyPackageChanges();
3066
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003067 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003068
Evan Millarc64edde2009-04-18 12:26:32 -07003069 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003070
Bookatz50df7112017-08-04 14:53:26 -07003071 /**
3072 * Returns Timers tracking the total time of each Resource Power Manager state and voter.
3073 */
3074 public abstract Map<String, ? extends Timer> getRpmStats();
3075 /**
3076 * Returns Timers tracking the screen-off time of each Resource Power Manager state and voter.
3077 */
3078 public abstract Map<String, ? extends Timer> getScreenOffRpmStats();
3079
3080
James Carr2dd7e5e2016-07-20 18:48:39 -07003081 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
3082
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003083 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
3084
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003085 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086 long days = seconds / (60 * 60 * 24);
3087 if (days != 0) {
3088 out.append(days);
3089 out.append("d ");
3090 }
3091 long used = days * 60 * 60 * 24;
3092
3093 long hours = (seconds - used) / (60 * 60);
3094 if (hours != 0 || used != 0) {
3095 out.append(hours);
3096 out.append("h ");
3097 }
3098 used += hours * 60 * 60;
3099
3100 long mins = (seconds-used) / 60;
3101 if (mins != 0 || used != 0) {
3102 out.append(mins);
3103 out.append("m ");
3104 }
3105 used += mins * 60;
3106
3107 if (seconds != 0 || used != 0) {
3108 out.append(seconds-used);
3109 out.append("s ");
3110 }
3111 }
3112
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003113 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003115 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116 sb.append(time - (sec * 1000));
3117 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118 }
3119
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003120 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003121 long sec = time / 1000;
3122 formatTimeRaw(sb, sec);
3123 sb.append(time - (sec * 1000));
3124 sb.append("ms");
3125 }
3126
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003127 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003128 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003129 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130 }
3131 float perc = ((float)num) / ((float)den) * 100;
3132 mFormatBuilder.setLength(0);
3133 mFormatter.format("%.1f%%", perc);
3134 return mFormatBuilder.toString();
3135 }
3136
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003137 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07003138 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07003139
Evan Millar22ac0432009-03-31 11:33:18 -07003140 if (bytes < BYTES_PER_KB) {
3141 return bytes + "B";
3142 } else if (bytes < BYTES_PER_MB) {
3143 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
3144 return mFormatBuilder.toString();
3145 } else if (bytes < BYTES_PER_GB){
3146 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
3147 return mFormatBuilder.toString();
3148 } else {
3149 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
3150 return mFormatBuilder.toString();
3151 }
3152 }
3153
Kweku Adams103351f2017-10-16 14:39:34 -07003154 private static long roundUsToMs(long timeUs) {
3155 return (timeUs + 500) / 1000;
3156 }
3157
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003158 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003159 if (timer != null) {
3160 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003161 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003162 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
3163 return totalTimeMillis;
3164 }
3165 return 0;
3166 }
3167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 /**
3169 *
3170 * @param sb a StringBuilder object.
3171 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003172 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003173 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003174 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 * @param linePrefix a String to be prepended to each line of output.
3176 * @return the line prefix
3177 */
3178 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003179 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003182 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003183
Evan Millarc64edde2009-04-18 12:26:32 -07003184 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003185 if (totalTimeMillis != 0) {
3186 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003187 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08003188 if (name != null) {
3189 sb.append(name);
3190 sb.append(' ');
3191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003192 sb.append('(');
3193 sb.append(count);
3194 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003195 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
3196 if (maxDurationMs >= 0) {
3197 sb.append(" max=");
3198 sb.append(maxDurationMs);
3199 }
Bookatz506a8182017-05-01 14:18:42 -07003200 // Put actual time if it is available and different from totalTimeMillis.
3201 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
3202 if (totalDurMs > totalTimeMillis) {
3203 sb.append(" actual=");
3204 sb.append(totalDurMs);
3205 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07003206 if (timer.isRunningLocked()) {
3207 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3208 if (currentMs >= 0) {
3209 sb.append(" (running for ");
3210 sb.append(currentMs);
3211 sb.append("ms)");
3212 } else {
3213 sb.append(" (running)");
3214 }
3215 }
3216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 return ", ";
3218 }
3219 }
3220 return linePrefix;
3221 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003222
3223 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07003224 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003225 *
3226 * @param pw a PrintWriter object to print to.
3227 * @param sb a StringBuilder object.
3228 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08003229 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003230 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
3231 * @param prefix a String to be prepended to each line of output.
3232 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07003233 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003234 */
3235 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07003236 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003237 if (timer != null) {
3238 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07003239 final long totalTimeMs = (timer.getTotalTimeLocked(
3240 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003241 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003242 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003243 sb.setLength(0);
3244 sb.append(prefix);
3245 sb.append(" ");
3246 sb.append(type);
3247 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003248 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003249 sb.append("realtime (");
3250 sb.append(count);
3251 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003252 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
3253 if (maxDurationMs >= 0) {
3254 sb.append(" max=");
3255 sb.append(maxDurationMs);
3256 }
3257 if (timer.isRunningLocked()) {
3258 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
3259 if (currentMs >= 0) {
3260 sb.append(" (running for ");
3261 sb.append(currentMs);
3262 sb.append("ms)");
3263 } else {
3264 sb.append(" (running)");
3265 }
3266 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003267 pw.println(sb.toString());
3268 return true;
3269 }
3270 }
3271 return false;
3272 }
Bookatzc8c44962017-05-11 12:12:54 -07003273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274 /**
3275 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07003276 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003277 * @param sb a StringBuilder object.
3278 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003279 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003280 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003281 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 * @param linePrefix a String to be prepended to each line of output.
3283 * @return the line prefix
3284 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003285 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
3286 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 long totalTimeMicros = 0;
3288 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07003289 long max = 0;
3290 long current = 0;
3291 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003293 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07003294 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003295 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3296 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07003297 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003298 }
3299 sb.append(linePrefix);
3300 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
3301 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07003302 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003304 sb.append(',');
3305 sb.append(current);
3306 sb.append(',');
3307 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07003308 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
3309 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
3310 // totalDuration independent of totalTimeMicros (since they are not pooled).
3311 if (name != null) {
3312 sb.append(',');
3313 sb.append(totalDuration);
3314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003315 return ",";
3316 }
Bookatz506a8182017-05-01 14:18:42 -07003317
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003318 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
3319 String type) {
3320 pw.print(BATTERY_STATS_CHECKIN_VERSION);
3321 pw.print(',');
3322 pw.print(uid);
3323 pw.print(',');
3324 pw.print(category);
3325 pw.print(',');
3326 pw.print(type);
3327 }
3328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003329 /**
3330 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07003331 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 * @param pw the PageWriter to dump log to
3333 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3334 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3335 * @param args type-dependent data arguments
3336 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003337 @UnsupportedAppUsage
Bookatzc8c44962017-05-11 12:12:54 -07003338 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003340 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003341 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003342 pw.print(',');
3343 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003345 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07003347
3348 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003349 * Dump a given timer stat for terse checkin mode.
3350 *
3351 * @param pw the PageWriter to dump log to
3352 * @param uid the UID to log
3353 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3354 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3355 * @param timer a {@link Timer} to dump stats for
3356 * @param rawRealtime the current elapsed realtime of the system in microseconds
3357 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3358 */
3359 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
3360 Timer timer, long rawRealtime, int which) {
3361 if (timer != null) {
3362 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003363 final long totalTime = roundUsToMs(timer.getTotalTimeLocked(rawRealtime, which));
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003364 final int count = timer.getCountLocked(which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003365 if (totalTime != 0 || count != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003366 dumpLine(pw, uid, category, type, totalTime, count);
3367 }
3368 }
3369 }
3370
3371 /**
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003372 * Dump a given timer stat to the proto stream.
3373 *
3374 * @param proto the ProtoOutputStream to log to
3375 * @param fieldId type of data, the field to save to (e.g. AggregatedBatteryStats.WAKELOCK)
3376 * @param timer a {@link Timer} to dump stats for
3377 * @param rawRealtimeUs the current elapsed realtime of the system in microseconds
3378 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3379 */
3380 private static void dumpTimer(ProtoOutputStream proto, long fieldId,
Kweku Adams87b19ec2017-10-09 12:40:03 -07003381 Timer timer, long rawRealtimeUs, int which) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003382 if (timer == null) {
3383 return;
3384 }
3385 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003386 final long timeMs = roundUsToMs(timer.getTotalTimeLocked(rawRealtimeUs, which));
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003387 final int count = timer.getCountLocked(which);
Kweku Adams103351f2017-10-16 14:39:34 -07003388 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs / 1000);
3389 final long curDurationMs = timer.getCurrentDurationMsLocked(rawRealtimeUs / 1000);
3390 final long totalDurationMs = timer.getTotalDurationMsLocked(rawRealtimeUs / 1000);
3391 if (timeMs != 0 || count != 0 || maxDurationMs != -1 || curDurationMs != -1
3392 || totalDurationMs != -1) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003393 final long token = proto.start(fieldId);
Kweku Adams103351f2017-10-16 14:39:34 -07003394 proto.write(TimerProto.DURATION_MS, timeMs);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003395 proto.write(TimerProto.COUNT, count);
Kweku Adams103351f2017-10-16 14:39:34 -07003396 // These values will be -1 for timers that don't implement the functionality.
3397 if (maxDurationMs != -1) {
3398 proto.write(TimerProto.MAX_DURATION_MS, maxDurationMs);
3399 }
3400 if (curDurationMs != -1) {
3401 proto.write(TimerProto.CURRENT_DURATION_MS, curDurationMs);
3402 }
3403 if (totalDurationMs != -1) {
3404 proto.write(TimerProto.TOTAL_DURATION_MS, totalDurationMs);
3405 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003406 proto.end(token);
3407 }
3408 }
3409
3410 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003411 * Checks if the ControllerActivityCounter has any data worth dumping.
3412 */
3413 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
3414 if (counter == null) {
3415 return false;
3416 }
3417
3418 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
3419 || counter.getRxTimeCounter().getCountLocked(which) != 0
Blake Kragtencb308d92019-01-28 10:54:13 -08003420 || counter.getPowerCounter().getCountLocked(which) != 0
3421 || counter.getMonitoredRailChargeConsumedMaMs().getCountLocked(which) != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003422 return true;
3423 }
3424
3425 for (LongCounter c : counter.getTxTimeCounters()) {
3426 if (c.getCountLocked(which) != 0) {
3427 return true;
3428 }
3429 }
3430 return false;
3431 }
3432
3433 /**
3434 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3435 * The order of the arguments in the final check in line is:
3436 *
3437 * idle, rx, power, tx...
3438 *
3439 * where tx... is one or more transmit level times.
3440 */
3441 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
3442 String type,
3443 ControllerActivityCounter counter,
3444 int which) {
3445 if (!controllerActivityHasData(counter, which)) {
3446 return;
3447 }
3448
3449 dumpLineHeader(pw, uid, category, type);
3450 pw.print(",");
3451 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
3452 pw.print(",");
3453 pw.print(counter.getRxTimeCounter().getCountLocked(which));
3454 pw.print(",");
Blake Kragtencb308d92019-01-28 10:54:13 -08003455 pw.print(counter.getPowerCounter().getCountLocked(which) / (MILLISECONDS_IN_HOUR));
3456 pw.print(",");
3457 pw.print(counter.getMonitoredRailChargeConsumedMaMs().getCountLocked(which)
3458 / (MILLISECONDS_IN_HOUR));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003459 for (LongCounter c : counter.getTxTimeCounters()) {
3460 pw.print(",");
3461 pw.print(c.getCountLocked(which));
3462 }
3463 pw.println();
3464 }
3465
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003466 /**
3467 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3468 */
3469 private static void dumpControllerActivityProto(ProtoOutputStream proto, long fieldId,
3470 ControllerActivityCounter counter,
3471 int which) {
3472 if (!controllerActivityHasData(counter, which)) {
3473 return;
3474 }
3475
3476 final long cToken = proto.start(fieldId);
3477
3478 proto.write(ControllerActivityProto.IDLE_DURATION_MS,
3479 counter.getIdleTimeCounter().getCountLocked(which));
3480 proto.write(ControllerActivityProto.RX_DURATION_MS,
3481 counter.getRxTimeCounter().getCountLocked(which));
3482 proto.write(ControllerActivityProto.POWER_MAH,
Blake Kragtencb308d92019-01-28 10:54:13 -08003483 counter.getPowerCounter().getCountLocked(which) / (MILLISECONDS_IN_HOUR));
3484 proto.write(ControllerActivityProto.MONITORED_RAIL_CHARGE_MAH,
3485 counter.getMonitoredRailChargeConsumedMaMs().getCountLocked(which)
3486 / (MILLISECONDS_IN_HOUR));
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003487
3488 long tToken;
3489 LongCounter[] txCounters = counter.getTxTimeCounters();
3490 for (int i = 0; i < txCounters.length; ++i) {
3491 LongCounter c = txCounters[i];
3492 tToken = proto.start(ControllerActivityProto.TX);
3493 proto.write(ControllerActivityProto.TxLevel.LEVEL, i);
3494 proto.write(ControllerActivityProto.TxLevel.DURATION_MS, c.getCountLocked(which));
3495 proto.end(tToken);
3496 }
3497
3498 proto.end(cToken);
3499 }
3500
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003501 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
3502 String prefix, String controllerName,
3503 ControllerActivityCounter counter,
3504 int which) {
3505 if (controllerActivityHasData(counter, which)) {
3506 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
3507 }
3508 }
3509
3510 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
3511 String controllerName,
3512 ControllerActivityCounter counter, int which) {
3513 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
3514 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
3515 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
Blake Kragtencb308d92019-01-28 10:54:13 -08003516 final long monitoredRailChargeConsumedMaMs =
3517 counter.getMonitoredRailChargeConsumedMaMs().getCountLocked(which);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003518 // Battery real time
3519 final long totalControllerActivityTimeMs
3520 = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003521 long totalTxTimeMs = 0;
3522 for (LongCounter txState : counter.getTxTimeCounters()) {
3523 totalTxTimeMs += txState.getCountLocked(which);
3524 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003525
Siddharth Rayb50a6842017-12-14 15:15:28 -08003526 if (controllerName.equals(WIFI_CONTROLLER_NAME)) {
3527 final long scanTimeMs = counter.getScanTimeCounter().getCountLocked(which);
3528 sb.setLength(0);
3529 sb.append(prefix);
3530 sb.append(" ");
3531 sb.append(controllerName);
3532 sb.append(" Scan time: ");
3533 formatTimeMs(sb, scanTimeMs);
3534 sb.append("(");
3535 sb.append(formatRatioLocked(scanTimeMs, totalControllerActivityTimeMs));
3536 sb.append(")");
3537 pw.println(sb.toString());
Siddharth Rayed754702018-02-15 12:44:37 -08003538
3539 final long sleepTimeMs
3540 = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + totalTxTimeMs);
3541 sb.setLength(0);
3542 sb.append(prefix);
3543 sb.append(" ");
3544 sb.append(controllerName);
3545 sb.append(" Sleep time: ");
3546 formatTimeMs(sb, sleepTimeMs);
3547 sb.append("(");
3548 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3549 sb.append(")");
3550 pw.println(sb.toString());
Siddharth Rayb50a6842017-12-14 15:15:28 -08003551 }
3552
Siddharth Rayed754702018-02-15 12:44:37 -08003553 if (controllerName.equals(CELLULAR_CONTROLLER_NAME)) {
3554 final long sleepTimeMs = counter.getSleepTimeCounter().getCountLocked(which);
3555 sb.setLength(0);
3556 sb.append(prefix);
3557 sb.append(" ");
3558 sb.append(controllerName);
3559 sb.append(" Sleep time: ");
3560 formatTimeMs(sb, sleepTimeMs);
3561 sb.append("(");
3562 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3563 sb.append(")");
3564 pw.println(sb.toString());
3565 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003566
3567 sb.setLength(0);
3568 sb.append(prefix);
3569 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003570 sb.append(controllerName);
3571 sb.append(" Idle time: ");
3572 formatTimeMs(sb, idleTimeMs);
3573 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003574 sb.append(formatRatioLocked(idleTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003575 sb.append(")");
3576 pw.println(sb.toString());
3577
3578 sb.setLength(0);
3579 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003580 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003581 sb.append(controllerName);
3582 sb.append(" Rx time: ");
3583 formatTimeMs(sb, rxTimeMs);
3584 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003585 sb.append(formatRatioLocked(rxTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003586 sb.append(")");
3587 pw.println(sb.toString());
3588
3589 sb.setLength(0);
3590 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003591 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003592 sb.append(controllerName);
3593 sb.append(" Tx time: ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003594
Siddharth Ray3c648c42017-10-02 17:30:58 -07003595 String [] powerLevel;
3596 switch(controllerName) {
Siddharth Rayb50a6842017-12-14 15:15:28 -08003597 case CELLULAR_CONTROLLER_NAME:
Siddharth Ray3c648c42017-10-02 17:30:58 -07003598 powerLevel = new String[] {
3599 " less than 0dBm: ",
3600 " 0dBm to 8dBm: ",
3601 " 8dBm to 15dBm: ",
3602 " 15dBm to 20dBm: ",
3603 " above 20dBm: "};
3604 break;
3605 default:
3606 powerLevel = new String[] {"[0]", "[1]", "[2]", "[3]", "[4]"};
3607 break;
3608 }
3609 final int numTxLvls = Math.min(counter.getTxTimeCounters().length, powerLevel.length);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003610 if (numTxLvls > 1) {
Siddharth Ray3c648c42017-10-02 17:30:58 -07003611 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003612 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3613 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3614 sb.setLength(0);
3615 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003616 sb.append(" ");
3617 sb.append(powerLevel[lvl]);
3618 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003619 formatTimeMs(sb, txLvlTimeMs);
3620 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003621 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003622 sb.append(")");
3623 pw.println(sb.toString());
3624 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003625 } else {
3626 final long txLvlTimeMs = counter.getTxTimeCounters()[0].getCountLocked(which);
3627 formatTimeMs(sb, txLvlTimeMs);
3628 sb.append("(");
3629 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
3630 sb.append(")");
3631 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003632 }
3633
Siddharth Ray3c648c42017-10-02 17:30:58 -07003634 if (powerDrainMaMs > 0) {
3635 sb.setLength(0);
3636 sb.append(prefix);
3637 sb.append(" ");
3638 sb.append(controllerName);
3639 sb.append(" Battery drain: ").append(
Blake Kragtencb308d92019-01-28 10:54:13 -08003640 BatteryStatsHelper.makemAh(powerDrainMaMs / MILLISECONDS_IN_HOUR));
Siddharth Ray3c648c42017-10-02 17:30:58 -07003641 sb.append("mAh");
3642 pw.println(sb.toString());
3643 }
Blake Kragtencb308d92019-01-28 10:54:13 -08003644
3645 if (monitoredRailChargeConsumedMaMs > 0) {
3646 sb.setLength(0);
3647 sb.append(prefix);
3648 sb.append(" ");
3649 sb.append(controllerName);
3650 sb.append(" Monitored rail energy drain: ").append(
3651 new DecimalFormat("#.##").format(
3652 monitoredRailChargeConsumedMaMs / MILLISECONDS_IN_HOUR));
3653 sb.append(" mAh");
3654 pw.println(sb.toString());
3655 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003656 }
3657
3658 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003659 * Temporary for settings.
3660 */
3661 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3662 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3663 }
3664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003665 /**
3666 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003667 *
Kweku Adams87b19ec2017-10-09 12:40:03 -07003668 * NOTE: all times are expressed in microseconds, unless specified otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003670 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3671 boolean wifiOnly) {
Bookatzdb6d6242019-03-18 15:42:55 -07003672
3673 if (which != BatteryStats.STATS_SINCE_CHARGED) {
3674 dumpLine(pw, 0, STAT_NAMES[which], "err",
3675 "ERROR: BatteryStats.dumpCheckin called for which type " + which
3676 + " but only STATS_SINCE_CHARGED is supported.");
3677 return;
3678 }
3679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680 final long rawUptime = SystemClock.uptimeMillis() * 1000;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003681 final long rawRealtimeMs = SystemClock.elapsedRealtime();
3682 final long rawRealtime = rawRealtimeMs * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003684 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3685 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003686 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3687 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3688 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003689 final long totalRealtime = computeRealtime(rawRealtime, which);
3690 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003691 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Mike Mac2f518a2017-09-19 16:06:03 -07003692 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003693 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003694 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003695 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3696 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003697 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003698 rawRealtime, which);
3699 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3700 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003701 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003702 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003703 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003704 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003705 final long dischargeCount = getUahDischarge(which);
3706 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
3707 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Ma15313c92017-11-15 17:58:21 -08003708 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
3709 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003710
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003711 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003712
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003713 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003714 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003715
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003716 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003718 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003719 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003720 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003721 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003722 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003723 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003724 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003725 getEstimatedBatteryCapacity(),
Mike Mac2f518a2017-09-19 16:06:03 -07003726 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity(),
3727 screenDozeTime / 1000);
Adam Lesinski67c134f2016-06-10 15:15:08 -07003728
Bookatzc8c44962017-05-11 12:12:54 -07003729
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003730 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003731 long fullWakeLockTimeTotal = 0;
3732 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003733
Evan Millar22ac0432009-03-31 11:33:18 -07003734 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003735 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003736
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003737 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3738 = u.getWakelockStats();
3739 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3740 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003741
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003742 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3743 if (fullWakeTimer != null) {
3744 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3745 which);
3746 }
3747
3748 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3749 if (partialWakeTimer != null) {
3750 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3751 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003752 }
3753 }
3754 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003755
3756 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003757 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3758 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3759 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3760 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3761 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3762 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3763 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3764 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003765 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3766 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003767 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3768 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003769 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3770 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003771
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003772 // Dump Modem controller stats
3773 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3774 getModemControllerActivity(), which);
3775
Adam Lesinskie283d332015-04-16 12:29:25 -07003776 // Dump Wifi controller stats
3777 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3778 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003779 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003780 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003781
3782 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3783 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003784
3785 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003786 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3787 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003789 // Dump misc stats
3790 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003791 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003792 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003793 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003794 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003795 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003796 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3797 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003798 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003799 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3800 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3801 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3802 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003803 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003804
Dianne Hackborn617f8772009-03-31 15:04:46 -07003805 // Dump screen brightness stats
3806 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3807 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003808 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003809 }
3810 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003811
Dianne Hackborn627bba72009-03-24 22:32:56 -07003812 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003813 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3814 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003815 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003816 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003817 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003818 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003819 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003820 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003821 args[i] = getPhoneSignalStrengthCount(i, which);
3822 }
3823 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003824
Dianne Hackborn627bba72009-03-24 22:32:56 -07003825 // Dump network type stats
3826 args = new Object[NUM_DATA_CONNECTION_TYPES];
3827 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003828 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003829 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003830 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3831 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3832 args[i] = getPhoneDataConnectionCount(i, which);
3833 }
3834 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003835
3836 // Dump wifi state stats
3837 args = new Object[NUM_WIFI_STATES];
3838 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003839 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003840 }
3841 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3842 for (int i=0; i<NUM_WIFI_STATES; i++) {
3843 args[i] = getWifiStateCount(i, which);
3844 }
3845 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3846
Dianne Hackborn3251b902014-06-20 14:40:53 -07003847 // Dump wifi suppl state stats
3848 args = new Object[NUM_WIFI_SUPPL_STATES];
3849 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3850 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3851 }
3852 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3853 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3854 args[i] = getWifiSupplStateCount(i, which);
3855 }
3856 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3857
3858 // Dump wifi signal strength stats
3859 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3860 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3861 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3862 }
3863 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3864 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3865 args[i] = getWifiSignalStrengthCount(i, which);
3866 }
3867 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3868
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003869 // Dump Multicast total stats
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003870 final long multicastWakeLockTimeTotalMicros =
3871 getWifiMulticastWakelockTime(rawRealtime, which);
3872 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003873 dumpLine(pw, 0 /* uid */, category, WIFI_MULTICAST_TOTAL_DATA,
3874 multicastWakeLockTimeTotalMicros / 1000,
3875 multicastWakeLockCountTotal);
3876
Mike Ma99e57c32018-03-15 14:40:37 -07003877 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3878 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
3879 getDischargeAmountScreenOnSinceCharge(),
3880 getDischargeAmountScreenOffSinceCharge(),
3881 dischargeCount / 1000, dischargeScreenOffCount / 1000,
3882 getDischargeAmountScreenDozeSinceCharge(), dischargeScreenDozeCount / 1000,
3883 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Bookatzc8c44962017-05-11 12:12:54 -07003884
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003885 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003886 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003887 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003888 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003889 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003890 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003891 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3892 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003893 }
Evan Millarc64edde2009-04-18 12:26:32 -07003894 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003895 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003896 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003897 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3898 // Not doing the regular wake lock formatting to remain compatible
3899 // with the old checkin format.
3900 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3901 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003902 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003903 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003904 }
3905 }
Evan Millarc64edde2009-04-18 12:26:32 -07003906 }
Bookatzc8c44962017-05-11 12:12:54 -07003907
Bookatz50df7112017-08-04 14:53:26 -07003908 final Map<String, ? extends Timer> rpmStats = getRpmStats();
3909 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
3910 if (rpmStats.size() > 0) {
3911 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
3912 sb.setLength(0);
3913 Timer totalTimer = ent.getValue();
3914 long timeMs = (totalTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3915 int count = totalTimer.getCountLocked(which);
3916 Timer screenOffTimer = screenOffRpmStats.get(ent.getKey());
3917 long screenOffTimeMs = screenOffTimer != null
3918 ? (screenOffTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3919 int screenOffCount = screenOffTimer != null
3920 ? screenOffTimer.getCountLocked(which) : 0;
Bookatz82b341172017-09-07 19:06:08 -07003921 if (SCREEN_OFF_RPM_STATS_ENABLED) {
3922 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3923 "\"" + ent.getKey() + "\"", timeMs, count, screenOffTimeMs,
3924 screenOffCount);
3925 } else {
3926 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3927 "\"" + ent.getKey() + "\"", timeMs, count);
3928 }
Bookatz50df7112017-08-04 14:53:26 -07003929 }
3930 }
3931
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003932 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003933 helper.create(this);
3934 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003935 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003936 if (sippers != null && sippers.size() > 0) {
3937 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3938 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003939 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003940 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3941 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
Kweku Adams87b19ec2017-10-09 12:40:03 -07003942 int uid = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003943 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003944 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003945 String label;
3946 switch (bs.drainType) {
Kweku Adamsef728952018-04-10 17:56:16 -07003947 case AMBIENT_DISPLAY:
3948 label = "ambi";
3949 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003950 case IDLE:
3951 label="idle";
3952 break;
3953 case CELL:
3954 label="cell";
3955 break;
3956 case PHONE:
3957 label="phone";
3958 break;
3959 case WIFI:
3960 label="wifi";
3961 break;
3962 case BLUETOOTH:
3963 label="blue";
3964 break;
3965 case SCREEN:
3966 label="scrn";
3967 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003968 case FLASHLIGHT:
3969 label="flashlight";
3970 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003971 case APP:
3972 uid = bs.uidObj.getUid();
3973 label = "uid";
3974 break;
3975 case USER:
3976 uid = UserHandle.getUid(bs.userId, 0);
3977 label = "user";
3978 break;
3979 case UNACCOUNTED:
3980 label = "unacc";
3981 break;
3982 case OVERCOUNTED:
3983 label = "over";
3984 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003985 case CAMERA:
3986 label = "camera";
3987 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003988 case MEMORY:
3989 label = "memory";
3990 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003991 default:
3992 label = "???";
3993 }
3994 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07003995 BatteryStatsHelper.makemAh(bs.totalPowerMah),
3996 bs.shouldHide ? 1 : 0,
3997 BatteryStatsHelper.makemAh(bs.screenPowerMah),
3998 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003999 }
4000 }
4001
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004002 final long[] cpuFreqs = getCpuFreqs();
4003 if (cpuFreqs != null) {
4004 sb.setLength(0);
4005 for (int i = 0; i < cpuFreqs.length; ++i) {
4006 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
4007 }
4008 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
4009 }
4010
Kweku Adams87b19ec2017-10-09 12:40:03 -07004011 // Dump stats per UID.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004012 for (int iu = 0; iu < NU; iu++) {
4013 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004014 if (reqUid >= 0 && uid != reqUid) {
4015 continue;
4016 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004017 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07004018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004019 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004020 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4021 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4022 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4023 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4024 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4025 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4026 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
4027 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07004028 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004029 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4030 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07004031 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004032 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4033 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08004034 // Background data transfers
4035 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
4036 which);
4037 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
4038 which);
4039 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
4040 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
4041 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
4042 which);
4043 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
4044 which);
4045 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
4046 which);
4047 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
4048 which);
4049
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004050 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
4051 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004052 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08004053 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
4054 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
4055 || wifiBytesBgTx > 0
4056 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
4057 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004058 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
4059 wifiBytesRx, wifiBytesTx,
4060 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004061 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004062 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08004063 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
4064 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
4065 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
4066 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004067 }
4068
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004069 // Dump modem controller data, per UID.
4070 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
4071 u.getModemControllerActivity(), which);
4072
4073 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07004074 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4075 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4076 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08004077 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
4078 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07004079 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
4080 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
4081 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07004082 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07004083 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07004084 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
4085 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004086 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
4087 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08004088 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07004089 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07004090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004091
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004092 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
4093 u.getWifiControllerActivity(), which);
4094
Bookatz867c0d72017-03-07 18:23:42 -08004095 final Timer bleTimer = u.getBluetoothScanTimer();
4096 if (bleTimer != null) {
4097 // Convert from microseconds to milliseconds with rounding
4098 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
4099 / 1000;
4100 if (totalTime != 0) {
4101 final int count = bleTimer.getCountLocked(which);
4102 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
4103 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004104 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4105 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
4106 final long actualTimeBg = bleTimerBg != null ?
4107 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07004108 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07004109 final int resultCount = u.getBluetoothScanResultCounter() != null ?
4110 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07004111 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
4112 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
4113 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
4114 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
4115 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
4116 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4117 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
4118 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
4119 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
4120 final Timer unoptimizedScanTimerBg =
4121 u.getBluetoothUnoptimizedScanBackgroundTimer();
4122 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
4123 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4124 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
4125 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
4126
Bookatz867c0d72017-03-07 18:23:42 -08004127 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07004128 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
4129 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
4130 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08004131 }
4132 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004133
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004134 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
4135 u.getBluetoothControllerActivity(), which);
4136
Dianne Hackborn617f8772009-03-31 15:04:46 -07004137 if (u.hasUserActivity()) {
4138 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
4139 boolean hasData = false;
4140 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
4141 int val = u.getUserActivityCount(i, which);
4142 args[i] = val;
4143 if (val != 0) hasData = true;
4144 }
4145 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07004146 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004147 }
4148 }
Bookatzc8c44962017-05-11 12:12:54 -07004149
4150 if (u.getAggregatedPartialWakelockTimer() != null) {
4151 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07004152 // Times are since reset (regardless of 'which')
4153 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07004154 final Timer bgTimer = timer.getSubTimer();
4155 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004156 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07004157 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
4158 }
4159
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004160 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
4161 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4162 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4163 String linePrefix = "";
4164 sb.setLength(0);
4165 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
4166 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004167 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4168 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004169 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004170 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
4171 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004172 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
4173 rawRealtime, "w", which, linePrefix);
4174
Kweku Adams103351f2017-10-16 14:39:34 -07004175 // Only log if we had at least one wakelock...
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004176 if (sb.length() > 0) {
4177 String name = wakelocks.keyAt(iw);
4178 if (name.indexOf(',') >= 0) {
4179 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 }
Yi Jin02483362017-08-04 11:30:44 -07004181 if (name.indexOf('\n') >= 0) {
4182 name = name.replace('\n', '_');
4183 }
4184 if (name.indexOf('\r') >= 0) {
4185 name = name.replace('\r', '_');
4186 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004187 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004188 }
4189 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004190
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004191 // WiFi Multicast Wakelock Statistics
4192 final Timer mcTimer = u.getMulticastWakelockStats();
4193 if (mcTimer != null) {
4194 final long totalMcWakelockTimeMs =
4195 mcTimer.getTotalTimeLocked(rawRealtime, which) / 1000 ;
4196 final int countMcWakelock = mcTimer.getCountLocked(which);
4197 if(totalMcWakelockTimeMs > 0) {
4198 dumpLine(pw, uid, category, WIFI_MULTICAST_DATA,
4199 totalMcWakelockTimeMs, countMcWakelock);
4200 }
4201 }
4202
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004203 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4204 for (int isy=syncs.size()-1; isy>=0; isy--) {
4205 final Timer timer = syncs.valueAt(isy);
4206 // Convert from microseconds to milliseconds with rounding
4207 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4208 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07004209 final Timer bgTimer = timer.getSubTimer();
4210 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004211 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07004212 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004213 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004214 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07004215 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004216 }
4217 }
4218
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004219 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4220 for (int ij=jobs.size()-1; ij>=0; ij--) {
4221 final Timer timer = jobs.valueAt(ij);
4222 // Convert from microseconds to milliseconds with rounding
4223 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4224 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07004225 final Timer bgTimer = timer.getSubTimer();
4226 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004227 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07004228 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004229 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004230 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07004231 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004232 }
4233 }
4234
Dianne Hackborn94326cb2017-06-28 16:17:20 -07004235 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
4236 for (int ic=completions.size()-1; ic>=0; ic--) {
4237 SparseIntArray types = completions.valueAt(ic);
4238 if (types != null) {
4239 dumpLine(pw, uid, category, JOB_COMPLETION_DATA,
4240 "\"" + completions.keyAt(ic) + "\"",
4241 types.get(JobParameters.REASON_CANCELED, 0),
4242 types.get(JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, 0),
4243 types.get(JobParameters.REASON_PREEMPT, 0),
4244 types.get(JobParameters.REASON_TIMEOUT, 0),
4245 types.get(JobParameters.REASON_DEVICE_IDLE, 0));
4246 }
4247 }
4248
Amith Yamasani977e11f2018-02-16 11:29:54 -08004249 // Dump deferred jobs stats
4250 u.getDeferredJobsCheckinLineLocked(sb, which);
4251 if (sb.length() > 0) {
4252 dumpLine(pw, uid, category, JOBS_DEFERRED_DATA, sb.toString());
4253 }
4254
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004255 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
4256 rawRealtime, which);
4257 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
4258 rawRealtime, which);
4259 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
4260 rawRealtime, which);
4261 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
4262 rawRealtime, which);
4263
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004264 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4265 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004266 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004267 final Uid.Sensor se = sensors.valueAt(ise);
4268 final int sensorNumber = sensors.keyAt(ise);
4269 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004270 if (timer != null) {
4271 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004272 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4273 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004274 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004275 final int count = timer.getCountLocked(which);
4276 final Timer bgTimer = se.getSensorBackgroundTime();
4277 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004278 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4279 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4280 final long bgActualTime = bgTimer != null ?
4281 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4282 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
4283 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 }
4286 }
4287
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004288 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
4289 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004290
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07004291 dumpTimer(pw, uid, category, FOREGROUND_ACTIVITY_DATA, u.getForegroundActivityTimer(),
4292 rawRealtime, which);
4293
4294 dumpTimer(pw, uid, category, FOREGROUND_SERVICE_DATA, u.getForegroundServiceTimer(),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004295 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004296
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004297 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07004298 long totalStateTime = 0;
4299 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08004300 final long time = u.getProcessStateTime(ips, rawRealtime, which);
4301 totalStateTime += time;
4302 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004303 }
4304 if (totalStateTime > 0) {
4305 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
4306 }
4307
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004308 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4309 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004310 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004311 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004312 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004313 }
4314
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004315 // If the cpuFreqs is null, then don't bother checking for cpu freq times.
4316 if (cpuFreqs != null) {
4317 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
4318 // If total cpuFreqTimes is null, then we don't need to check for
4319 // screenOffCpuFreqTimes.
4320 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
4321 sb.setLength(0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004322 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004323 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004324 }
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004325 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
4326 if (screenOffCpuFreqTimeMs != null) {
4327 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
4328 sb.append("," + screenOffCpuFreqTimeMs[i]);
4329 }
4330 } else {
4331 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
4332 sb.append(",0");
4333 }
4334 }
4335 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
4336 cpuFreqTimeMs.length, sb.toString());
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004337 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -08004338
4339 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
4340 final long[] timesMs = u.getCpuFreqTimes(which, procState);
4341 if (timesMs != null && timesMs.length == cpuFreqs.length) {
4342 sb.setLength(0);
4343 for (int i = 0; i < timesMs.length; ++i) {
4344 sb.append((i == 0 ? "" : ",") + timesMs[i]);
4345 }
4346 final long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(
4347 which, procState);
4348 if (screenOffTimesMs != null) {
4349 for (int i = 0; i < screenOffTimesMs.length; ++i) {
4350 sb.append("," + screenOffTimesMs[i]);
4351 }
4352 } else {
4353 for (int i = 0; i < timesMs.length; ++i) {
4354 sb.append(",0");
4355 }
4356 }
4357 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA,
4358 Uid.UID_PROCESS_TYPES[procState], timesMs.length, sb.toString());
4359 }
4360 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004361 }
4362
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004363 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4364 = u.getProcessStats();
4365 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4366 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004367
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004368 final long userMillis = ps.getUserTime(which);
4369 final long systemMillis = ps.getSystemTime(which);
4370 final long foregroundMillis = ps.getForegroundTime(which);
4371 final int starts = ps.getStarts(which);
4372 final int numCrashes = ps.getNumCrashes(which);
4373 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004374
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004375 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
4376 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004377 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
4378 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379 }
4380 }
4381
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004382 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4383 = u.getPackageStats();
4384 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4385 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4386 int wakeups = 0;
4387 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4388 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07004389 int count = alarms.valueAt(iwa).getCountLocked(which);
4390 wakeups += count;
4391 String name = alarms.keyAt(iwa).replace(',', '_');
4392 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004393 }
4394 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4395 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4396 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4397 final long startTime = ss.getStartTime(batteryUptime, which);
4398 final int starts = ss.getStarts(which);
4399 final int launches = ss.getLaunches(which);
4400 if (startTime != 0 || starts != 0 || launches != 0) {
4401 dumpLine(pw, uid, category, APK_DATA,
4402 wakeups, // wakeup alarms
4403 packageStats.keyAt(ipkg), // Apk
4404 serviceStats.keyAt(isvc), // service
4405 startTime / 1000, // time spent started, in ms
4406 starts,
4407 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 }
4409 }
4410 }
4411 }
4412 }
4413
Dianne Hackborn81038902012-11-26 17:04:09 -08004414 static final class TimerEntry {
4415 final String mName;
4416 final int mId;
4417 final BatteryStats.Timer mTimer;
4418 final long mTime;
4419 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
4420 mName = name;
4421 mId = id;
4422 mTimer = timer;
4423 mTime = time;
4424 }
4425 }
4426
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004427 private void printmAh(PrintWriter printer, double power) {
4428 printer.print(BatteryStatsHelper.makemAh(power));
4429 }
4430
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004431 private void printmAh(StringBuilder sb, double power) {
4432 sb.append(BatteryStatsHelper.makemAh(power));
4433 }
4434
Dianne Hackbornd953c532014-08-16 18:17:38 -07004435 /**
4436 * Temporary for settings.
4437 */
4438 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
4439 int reqUid) {
4440 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
4441 }
4442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004444 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07004445 int reqUid, boolean wifiOnly) {
Bookatzdb6d6242019-03-18 15:42:55 -07004446
4447 if (which != BatteryStats.STATS_SINCE_CHARGED) {
4448 pw.println("ERROR: BatteryStats.dump called for which type " + which
4449 + " but only STATS_SINCE_CHARGED is supported");
4450 return;
4451 }
4452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004453 final long rawUptime = SystemClock.uptimeMillis() * 1000;
4454 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07004455 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004456 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004457
4458 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
4459 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
4460 final long totalRealtime = computeRealtime(rawRealtime, which);
4461 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004462 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
4463 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
4464 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004465 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
4466 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -07004467 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004468
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004469 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07004470
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004471 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07004472 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004473
Adam Lesinskif9b20a92016-06-17 17:30:01 -07004474 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
4475 if (estimatedBatteryCapacity > 0) {
4476 sb.setLength(0);
4477 sb.append(prefix);
4478 sb.append(" Estimated battery capacity: ");
4479 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
4480 sb.append(" mAh");
4481 pw.println(sb.toString());
4482 }
4483
Jocelyn Dangc627d102017-04-14 13:15:14 -07004484 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
4485 if (minLearnedBatteryCapacity > 0) {
4486 sb.setLength(0);
4487 sb.append(prefix);
4488 sb.append(" Min learned battery capacity: ");
4489 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
4490 sb.append(" mAh");
4491 pw.println(sb.toString());
4492 }
4493 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
4494 if (maxLearnedBatteryCapacity > 0) {
4495 sb.setLength(0);
4496 sb.append(prefix);
4497 sb.append(" Max learned battery capacity: ");
4498 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
4499 sb.append(" mAh");
4500 pw.println(sb.toString());
4501 }
4502
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004503 sb.setLength(0);
4504 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004505 sb.append(" Time on battery: ");
4506 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
4507 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
4508 sb.append(") realtime, ");
4509 formatTimeMs(sb, whichBatteryUptime / 1000);
4510 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, whichBatteryRealtime));
4511 sb.append(") uptime");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004512 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004513
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004514 sb.setLength(0);
4515 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004516 sb.append(" Time on battery screen off: ");
4517 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
4518 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, whichBatteryRealtime));
4519 sb.append(") realtime, ");
4520 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
4521 sb.append("(");
4522 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, whichBatteryRealtime));
4523 sb.append(") uptime");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004524 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004525
4526 sb.setLength(0);
4527 sb.append(prefix);
4528 sb.append(" Time on battery screen doze: ");
4529 formatTimeMs(sb, screenDozeTime / 1000); sb.append("(");
4530 sb.append(formatRatioLocked(screenDozeTime, whichBatteryRealtime));
4531 sb.append(")");
4532 pw.println(sb.toString());
4533
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004534 sb.setLength(0);
4535 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004536 sb.append(" Total run time: ");
4537 formatTimeMs(sb, totalRealtime / 1000);
4538 sb.append("realtime, ");
4539 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004540 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07004541 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004542 if (batteryTimeRemaining >= 0) {
4543 sb.setLength(0);
4544 sb.append(prefix);
4545 sb.append(" Battery time remaining: ");
4546 formatTimeMs(sb, batteryTimeRemaining / 1000);
4547 pw.println(sb.toString());
4548 }
4549 if (chargeTimeRemaining >= 0) {
4550 sb.setLength(0);
4551 sb.append(prefix);
4552 sb.append(" Charge time remaining: ");
4553 formatTimeMs(sb, chargeTimeRemaining / 1000);
4554 pw.println(sb.toString());
4555 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004556
Kweku Adams87b19ec2017-10-09 12:40:03 -07004557 final long dischargeCount = getUahDischarge(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004558 if (dischargeCount >= 0) {
4559 sb.setLength(0);
4560 sb.append(prefix);
4561 sb.append(" Discharge: ");
4562 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
4563 sb.append(" mAh");
4564 pw.println(sb.toString());
4565 }
4566
Kweku Adams87b19ec2017-10-09 12:40:03 -07004567 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004568 if (dischargeScreenOffCount >= 0) {
4569 sb.setLength(0);
4570 sb.append(prefix);
4571 sb.append(" Screen off discharge: ");
4572 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
4573 sb.append(" mAh");
4574 pw.println(sb.toString());
4575 }
4576
Kweku Adams87b19ec2017-10-09 12:40:03 -07004577 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Mac2f518a2017-09-19 16:06:03 -07004578 if (dischargeScreenDozeCount >= 0) {
4579 sb.setLength(0);
4580 sb.append(prefix);
4581 sb.append(" Screen doze discharge: ");
4582 sb.append(BatteryStatsHelper.makemAh(dischargeScreenDozeCount / 1000.0));
4583 sb.append(" mAh");
4584 pw.println(sb.toString());
4585 }
4586
Mike Ma90902652018-04-17 14:07:24 -07004587 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004588 if (dischargeScreenOnCount >= 0) {
4589 sb.setLength(0);
4590 sb.append(prefix);
4591 sb.append(" Screen on discharge: ");
4592 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
4593 sb.append(" mAh");
4594 pw.println(sb.toString());
4595 }
4596
Mike Ma15313c92017-11-15 17:58:21 -08004597 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
4598 if (dischargeLightDozeCount >= 0) {
4599 sb.setLength(0);
4600 sb.append(prefix);
4601 sb.append(" Device light doze discharge: ");
4602 sb.append(BatteryStatsHelper.makemAh(dischargeLightDozeCount / 1000.0));
4603 sb.append(" mAh");
4604 pw.println(sb.toString());
4605 }
4606
4607 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
4608 if (dischargeDeepDozeCount >= 0) {
4609 sb.setLength(0);
4610 sb.append(prefix);
4611 sb.append(" Device deep doze discharge: ");
4612 sb.append(BatteryStatsHelper.makemAh(dischargeDeepDozeCount / 1000.0));
4613 sb.append(" mAh");
4614 pw.println(sb.toString());
4615 }
4616
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004617 pw.print(" Start clock time: ");
4618 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
4619
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004620 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004621 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004622 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004623 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
4624 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004625 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004626 rawRealtime, which);
4627 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
4628 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004629 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004630 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004631 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
4632 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
4633 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004634 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004635 sb.append(prefix);
4636 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
4637 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004638 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004639 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
4640 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004641 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004642 pw.println(sb.toString());
4643 sb.setLength(0);
4644 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004645 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004646 boolean didOne = false;
4647 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004648 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004649 if (time == 0) {
4650 continue;
4651 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004652 sb.append("\n ");
4653 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004654 didOne = true;
4655 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
4656 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004657 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004658 sb.append("(");
4659 sb.append(formatRatioLocked(time, screenOnTime));
4660 sb.append(")");
4661 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004662 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004663 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004664 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004665 sb.setLength(0);
4666 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004667 sb.append(" Power save mode enabled: ");
4668 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004669 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004670 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004671 sb.append(")");
4672 pw.println(sb.toString());
4673 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004674 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004675 sb.setLength(0);
4676 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004677 sb.append(" Device light idling: ");
4678 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004679 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004680 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
4681 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004682 sb.append("x");
4683 pw.println(sb.toString());
4684 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004685 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004686 sb.setLength(0);
4687 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004688 sb.append(" Idle mode light time: ");
4689 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004690 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004691 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
4692 sb.append(") ");
4693 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004694 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004695 sb.append(" -- longest ");
4696 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
4697 pw.println(sb.toString());
4698 }
4699 if (deviceIdlingTime != 0) {
4700 sb.setLength(0);
4701 sb.append(prefix);
4702 sb.append(" Device full idling: ");
4703 formatTimeMs(sb, deviceIdlingTime / 1000);
4704 sb.append("(");
4705 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004706 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004707 sb.append("x");
4708 pw.println(sb.toString());
4709 }
4710 if (deviceIdleModeFullTime != 0) {
4711 sb.setLength(0);
4712 sb.append(prefix);
4713 sb.append(" Idle mode full time: ");
4714 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
4715 sb.append("(");
4716 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
4717 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004718 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004719 sb.append("x");
4720 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004721 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004722 pw.println(sb.toString());
4723 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004724 if (phoneOnTime != 0) {
4725 sb.setLength(0);
4726 sb.append(prefix);
4727 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
4728 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004729 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004730 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004731 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004732 if (connChanges != 0) {
4733 pw.print(prefix);
4734 pw.print(" Connectivity changes: "); pw.println(connChanges);
4735 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004736
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004737 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07004738 long fullWakeLockTimeTotalMicros = 0;
4739 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08004740
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004741 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08004742
Evan Millar22ac0432009-03-31 11:33:18 -07004743 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004744 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004745
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004746 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4747 = u.getWakelockStats();
4748 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4749 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07004750
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004751 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
4752 if (fullWakeTimer != null) {
4753 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
4754 rawRealtime, which);
4755 }
4756
4757 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4758 if (partialWakeTimer != null) {
4759 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
4760 rawRealtime, which);
4761 if (totalTimeMicros > 0) {
4762 if (reqUid < 0) {
4763 // Only show the ordered list of all wake
4764 // locks if the caller is not asking for data
4765 // about a specific uid.
4766 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
4767 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08004768 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004769 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07004770 }
4771 }
4772 }
4773 }
Bookatzc8c44962017-05-11 12:12:54 -07004774
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004775 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4776 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4777 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4778 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4779 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4780 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4781 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4782 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004783 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4784 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004785
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004786 if (fullWakeLockTimeTotalMicros != 0) {
4787 sb.setLength(0);
4788 sb.append(prefix);
4789 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4790 (fullWakeLockTimeTotalMicros + 500) / 1000);
4791 pw.println(sb.toString());
4792 }
4793
4794 if (partialWakeLockTimeTotalMicros != 0) {
4795 sb.setLength(0);
4796 sb.append(prefix);
4797 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4798 (partialWakeLockTimeTotalMicros + 500) / 1000);
4799 pw.println(sb.toString());
4800 }
4801
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004802 final long multicastWakeLockTimeTotalMicros =
4803 getWifiMulticastWakelockTime(rawRealtime, which);
4804 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004805 if (multicastWakeLockTimeTotalMicros != 0) {
4806 sb.setLength(0);
4807 sb.append(prefix);
4808 sb.append(" Total WiFi Multicast wakelock Count: ");
4809 sb.append(multicastWakeLockCountTotal);
4810 pw.println(sb.toString());
4811
4812 sb.setLength(0);
4813 sb.append(prefix);
4814 sb.append(" Total WiFi Multicast wakelock time: ");
4815 formatTimeMsNoSpace(sb, (multicastWakeLockTimeTotalMicros + 500) / 1000);
4816 pw.println(sb.toString());
4817 }
4818
Siddharth Ray3c648c42017-10-02 17:30:58 -07004819 pw.println("");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004820 pw.print(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004821 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004822 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004823 sb.append(" CONNECTIVITY POWER SUMMARY START");
4824 pw.println(sb.toString());
4825
4826 pw.print(prefix);
4827 sb.setLength(0);
4828 sb.append(prefix);
4829 sb.append(" Logging duration for connectivity statistics: ");
4830 formatTimeMs(sb, whichBatteryRealtime / 1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004831 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004832
4833 sb.setLength(0);
4834 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004835 sb.append(" Cellular Statistics:");
Amith Yamasanif37447b2009-10-08 18:28:01 -07004836 pw.println(sb.toString());
4837
Siddharth Ray3c648c42017-10-02 17:30:58 -07004838 pw.print(prefix);
4839 sb.setLength(0);
4840 sb.append(prefix);
4841 sb.append(" Cellular kernel active time: ");
4842 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
4843 formatTimeMs(sb, mobileActiveTime / 1000);
4844 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4845 sb.append(")");
4846 pw.println(sb.toString());
4847
Blake Kragtenbff255d2019-03-05 15:44:31 -08004848 printControllerActivity(pw, sb, prefix, CELLULAR_CONTROLLER_NAME,
4849 getModemControllerActivity(), which);
4850
Siddharth Ray3c648c42017-10-02 17:30:58 -07004851 pw.print(" Cellular data received: "); pw.println(formatBytesLocked(mobileRxTotalBytes));
4852 pw.print(" Cellular data sent: "); pw.println(formatBytesLocked(mobileTxTotalBytes));
4853 pw.print(" Cellular packets received: "); pw.println(mobileRxTotalPackets);
4854 pw.print(" Cellular packets sent: "); pw.println(mobileTxTotalPackets);
4855
Dianne Hackborn627bba72009-03-24 22:32:56 -07004856 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004857 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004858 sb.append(" Cellular Radio Access Technology:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004859 didOne = false;
4860 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004861 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004862 if (time == 0) {
4863 continue;
4864 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004865 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004866 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004867 didOne = true;
Bookatzdf6baff2018-12-12 19:38:11 -08004868 sb.append(i < DATA_CONNECTION_NAMES.length ? DATA_CONNECTION_NAMES[i] : "ERROR");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004869 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004870 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004871 sb.append("(");
4872 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004873 sb.append(") ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004874 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004875 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004876 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004877
4878 sb.setLength(0);
4879 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004880 sb.append(" Cellular Rx signal strength (RSRP):");
4881 final String[] cellularRxSignalStrengthDescription = new String[]{
4882 "very poor (less than -128dBm): ",
4883 "poor (-128dBm to -118dBm): ",
4884 "moderate (-118dBm to -108dBm): ",
4885 "good (-108dBm to -98dBm): ",
4886 "great (greater than -98dBm): "};
4887 didOne = false;
4888 final int numCellularRxBins = Math.min(SignalStrength.NUM_SIGNAL_STRENGTH_BINS,
4889 cellularRxSignalStrengthDescription.length);
4890 for (int i=0; i<numCellularRxBins; i++) {
4891 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
4892 if (time == 0) {
4893 continue;
4894 }
4895 sb.append("\n ");
4896 sb.append(prefix);
4897 didOne = true;
4898 sb.append(cellularRxSignalStrengthDescription[i]);
4899 sb.append(" ");
4900 formatTimeMs(sb, time/1000);
4901 sb.append("(");
4902 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4903 sb.append(") ");
4904 }
4905 if (!didOne) sb.append(" (no activity)");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004906 pw.println(sb.toString());
4907
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004908 pw.print(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004909 sb.setLength(0);
4910 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004911 sb.append(" Wifi Statistics:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004912 pw.println(sb.toString());
4913
Siddharth Rayb50a6842017-12-14 15:15:28 -08004914 pw.print(prefix);
4915 sb.setLength(0);
4916 sb.append(prefix);
4917 sb.append(" Wifi kernel active time: ");
4918 final long wifiActiveTime = getWifiActiveTime(rawRealtime, which);
4919 formatTimeMs(sb, wifiActiveTime / 1000);
4920 sb.append("("); sb.append(formatRatioLocked(wifiActiveTime, whichBatteryRealtime));
4921 sb.append(")");
4922 pw.println(sb.toString());
4923
Blake Kragtenbff255d2019-03-05 15:44:31 -08004924 printControllerActivity(pw, sb, prefix, WIFI_CONTROLLER_NAME,
4925 getWifiControllerActivity(), which);
4926
Siddharth Ray3c648c42017-10-02 17:30:58 -07004927 pw.print(" Wifi data received: "); pw.println(formatBytesLocked(wifiRxTotalBytes));
4928 pw.print(" Wifi data sent: "); pw.println(formatBytesLocked(wifiTxTotalBytes));
4929 pw.print(" Wifi packets received: "); pw.println(wifiRxTotalPackets);
4930 pw.print(" Wifi packets sent: "); pw.println(wifiTxTotalPackets);
4931
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004932 sb.setLength(0);
4933 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004934 sb.append(" Wifi states:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004935 didOne = false;
4936 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004937 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004938 if (time == 0) {
4939 continue;
4940 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004941 sb.append("\n ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004942 didOne = true;
4943 sb.append(WIFI_STATE_NAMES[i]);
4944 sb.append(" ");
4945 formatTimeMs(sb, time/1000);
4946 sb.append("(");
4947 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4948 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004949 }
4950 if (!didOne) sb.append(" (no activity)");
4951 pw.println(sb.toString());
4952
4953 sb.setLength(0);
4954 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004955 sb.append(" Wifi supplicant states:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004956 didOne = false;
4957 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4958 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4959 if (time == 0) {
4960 continue;
4961 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004962 sb.append("\n ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004963 didOne = true;
4964 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4965 sb.append(" ");
4966 formatTimeMs(sb, time/1000);
4967 sb.append("(");
4968 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4969 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004970 }
4971 if (!didOne) sb.append(" (no activity)");
4972 pw.println(sb.toString());
4973
4974 sb.setLength(0);
4975 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004976 sb.append(" Wifi Rx signal strength (RSSI):");
4977 final String[] wifiRxSignalStrengthDescription = new String[]{
4978 "very poor (less than -88.75dBm): ",
4979 "poor (-88.75 to -77.5dBm): ",
4980 "moderate (-77.5dBm to -66.25dBm): ",
4981 "good (-66.25dBm to -55dBm): ",
4982 "great (greater than -55dBm): "};
Dianne Hackborn3251b902014-06-20 14:40:53 -07004983 didOne = false;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004984 final int numWifiRxBins = Math.min(NUM_WIFI_SIGNAL_STRENGTH_BINS,
4985 wifiRxSignalStrengthDescription.length);
4986 for (int i=0; i<numWifiRxBins; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004987 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4988 if (time == 0) {
4989 continue;
4990 }
4991 sb.append("\n ");
4992 sb.append(prefix);
4993 didOne = true;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004994 sb.append(" ");
4995 sb.append(wifiRxSignalStrengthDescription[i]);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004996 formatTimeMs(sb, time/1000);
4997 sb.append("(");
4998 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4999 sb.append(") ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005000 }
5001 if (!didOne) sb.append(" (no activity)");
5002 pw.println(sb.toString());
5003
Adam Lesinski50e47602015-12-04 17:04:54 -08005004 pw.print(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07005005 sb.setLength(0);
5006 sb.append(prefix);
Siddharth Ray78ccaf52017-12-23 16:16:21 -08005007 sb.append(" GPS Statistics:");
5008 pw.println(sb.toString());
5009
5010 sb.setLength(0);
5011 sb.append(prefix);
5012 sb.append(" GPS signal quality (Top 4 Average CN0):");
5013 final String[] gpsSignalQualityDescription = new String[]{
5014 "poor (less than 20 dBHz): ",
5015 "good (greater than 20 dBHz): "};
5016 final int numGpsSignalQualityBins = Math.min(GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS,
5017 gpsSignalQualityDescription.length);
5018 for (int i=0; i<numGpsSignalQualityBins; i++) {
5019 final long time = getGpsSignalQualityTime(i, rawRealtime, which);
5020 sb.append("\n ");
5021 sb.append(prefix);
5022 sb.append(" ");
5023 sb.append(gpsSignalQualityDescription[i]);
5024 formatTimeMs(sb, time/1000);
5025 sb.append("(");
5026 sb.append(formatRatioLocked(time, whichBatteryRealtime));
5027 sb.append(") ");
5028 }
5029 pw.println(sb.toString());
5030
5031 final long gpsBatteryDrainMaMs = getGpsBatteryDrainMaMs();
5032 if (gpsBatteryDrainMaMs > 0) {
5033 pw.print(prefix);
5034 sb.setLength(0);
5035 sb.append(prefix);
Blake Kragtenbff255d2019-03-05 15:44:31 -08005036 sb.append(" GPS Battery Drain: ");
5037 sb.append(new DecimalFormat("#.##").format(
5038 ((double) gpsBatteryDrainMaMs) / (3600 * 1000)));
5039 sb.append("mAh");
Siddharth Ray78ccaf52017-12-23 16:16:21 -08005040 pw.println(sb.toString());
5041 }
5042
5043 pw.print(prefix);
5044 sb.setLength(0);
5045 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07005046 sb.append(" CONNECTIVITY POWER SUMMARY END");
5047 pw.println(sb.toString());
5048 pw.println("");
5049
5050 pw.print(prefix);
Adam Lesinski50e47602015-12-04 17:04:54 -08005051 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
5052 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
5053
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005054 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
5055 sb.setLength(0);
5056 sb.append(prefix);
5057 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
5058 pw.println(sb.toString());
5059
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005060 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
5061 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07005062
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005063 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07005064
Mike Ma99e57c32018-03-15 14:40:37 -07005065 pw.print(prefix); pw.println(" Device battery use since last full charge");
5066 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
5067 pw.println(getLowDischargeAmountSinceCharge());
5068 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
5069 pw.println(getHighDischargeAmountSinceCharge());
5070 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
5071 pw.println(getDischargeAmountScreenOnSinceCharge());
5072 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
5073 pw.println(getDischargeAmountScreenOffSinceCharge());
5074 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
5075 pw.println(getDischargeAmountScreenDozeSinceCharge());
5076 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08005077
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005078 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005079 helper.create(this);
5080 helper.refreshStats(which, UserHandle.USER_ALL);
5081 List<BatterySipper> sippers = helper.getUsageList();
5082 if (sippers != null && sippers.size() > 0) {
5083 pw.print(prefix); pw.println(" Estimated power use (mAh):");
5084 pw.print(prefix); pw.print(" Capacity: ");
5085 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08005086 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07005087 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
5088 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
5089 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
5090 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005091 pw.println();
5092 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005093 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005094 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005095 switch (bs.drainType) {
Kweku Adamsef728952018-04-10 17:56:16 -07005096 case AMBIENT_DISPLAY:
5097 pw.print(" Ambient display: ");
5098 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005099 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005100 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005101 break;
5102 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005103 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005104 break;
5105 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005106 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005107 break;
5108 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005109 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005110 break;
5111 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005112 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005113 break;
5114 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005115 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005116 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005117 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005118 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005119 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005120 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005121 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005122 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005123 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005124 break;
5125 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005126 pw.print(" User "); pw.print(bs.userId);
5127 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005128 break;
5129 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005130 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005131 break;
5132 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005133 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005134 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005135 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005136 pw.print(" Camera: ");
5137 break;
5138 default:
5139 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005140 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005141 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005142 printmAh(pw, bs.totalPowerMah);
5143
Adam Lesinski57123002015-06-12 16:12:07 -07005144 if (bs.usagePowerMah != bs.totalPowerMah) {
5145 // If the usage (generic power) isn't the whole amount, we list out
5146 // what components are involved in the calculation.
5147
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005148 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07005149 if (bs.usagePowerMah != 0) {
5150 pw.print(" usage=");
5151 printmAh(pw, bs.usagePowerMah);
5152 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005153 if (bs.cpuPowerMah != 0) {
5154 pw.print(" cpu=");
5155 printmAh(pw, bs.cpuPowerMah);
5156 }
5157 if (bs.wakeLockPowerMah != 0) {
5158 pw.print(" wake=");
5159 printmAh(pw, bs.wakeLockPowerMah);
5160 }
5161 if (bs.mobileRadioPowerMah != 0) {
5162 pw.print(" radio=");
5163 printmAh(pw, bs.mobileRadioPowerMah);
5164 }
5165 if (bs.wifiPowerMah != 0) {
5166 pw.print(" wifi=");
5167 printmAh(pw, bs.wifiPowerMah);
5168 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005169 if (bs.bluetoothPowerMah != 0) {
5170 pw.print(" bt=");
5171 printmAh(pw, bs.bluetoothPowerMah);
5172 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005173 if (bs.gpsPowerMah != 0) {
5174 pw.print(" gps=");
5175 printmAh(pw, bs.gpsPowerMah);
5176 }
5177 if (bs.sensorPowerMah != 0) {
5178 pw.print(" sensor=");
5179 printmAh(pw, bs.sensorPowerMah);
5180 }
5181 if (bs.cameraPowerMah != 0) {
5182 pw.print(" camera=");
5183 printmAh(pw, bs.cameraPowerMah);
5184 }
5185 if (bs.flashlightPowerMah != 0) {
5186 pw.print(" flash=");
5187 printmAh(pw, bs.flashlightPowerMah);
5188 }
5189 pw.print(" )");
5190 }
Bookatz17d7d9d2017-06-08 14:50:46 -07005191
5192 // If there is additional smearing information, include it.
5193 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
5194 pw.print(" Including smearing: ");
5195 printmAh(pw, bs.totalSmearedPowerMah);
5196 pw.print(" (");
5197 if (bs.screenPowerMah != 0) {
5198 pw.print(" screen=");
5199 printmAh(pw, bs.screenPowerMah);
5200 }
5201 if (bs.proportionalSmearMah != 0) {
5202 pw.print(" proportional=");
5203 printmAh(pw, bs.proportionalSmearMah);
5204 }
5205 pw.print(" )");
5206 }
5207 if (bs.shouldHide) {
5208 pw.print(" Excluded from smearing");
5209 }
5210
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005211 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005212 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08005213 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005214 }
5215
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005216 sippers = helper.getMobilemsppList();
5217 if (sippers != null && sippers.size() > 0) {
5218 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005219 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005220 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005221 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005222 sb.setLength(0);
5223 sb.append(prefix); sb.append(" Uid ");
5224 UserHandle.formatUid(sb, bs.uidObj.getUid());
5225 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
5226 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
5227 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005228 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005229 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005230 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005231 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005232 sb.setLength(0);
5233 sb.append(prefix);
5234 sb.append(" TOTAL TIME: ");
5235 formatTimeMs(sb, totalTime);
5236 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
5237 sb.append(")");
5238 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005239 pw.println();
5240 }
5241
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005242 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
5243 @Override
5244 public int compare(TimerEntry lhs, TimerEntry rhs) {
5245 long lhsTime = lhs.mTime;
5246 long rhsTime = rhs.mTime;
5247 if (lhsTime < rhsTime) {
5248 return 1;
5249 }
5250 if (lhsTime > rhsTime) {
5251 return -1;
5252 }
5253 return 0;
5254 }
5255 };
5256
5257 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005258 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
5259 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005260 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005261 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
5262 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
5263 : kernelWakelocks.entrySet()) {
5264 final BatteryStats.Timer timer = ent.getValue();
5265 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005266 if (totalTimeMillis > 0) {
5267 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
5268 }
5269 }
5270 if (ktimers.size() > 0) {
5271 Collections.sort(ktimers, timerComparator);
5272 pw.print(prefix); pw.println(" All kernel wake locks:");
5273 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005274 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005275 String linePrefix = ": ";
5276 sb.setLength(0);
5277 sb.append(prefix);
5278 sb.append(" Kernel Wake lock ");
5279 sb.append(timer.mName);
5280 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
5281 which, linePrefix);
5282 if (!linePrefix.equals(": ")) {
5283 sb.append(" realtime");
5284 // Only print out wake locks that were held
5285 pw.println(sb.toString());
5286 }
5287 }
5288 pw.println();
5289 }
5290 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005291
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005292 if (timers.size() > 0) {
5293 Collections.sort(timers, timerComparator);
5294 pw.print(prefix); pw.println(" All partial wake locks:");
5295 for (int i=0; i<timers.size(); i++) {
5296 TimerEntry timer = timers.get(i);
5297 sb.setLength(0);
5298 sb.append(" Wake lock ");
5299 UserHandle.formatUid(sb, timer.mId);
5300 sb.append(" ");
5301 sb.append(timer.mName);
5302 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5303 sb.append(" realtime");
5304 pw.println(sb.toString());
5305 }
5306 timers.clear();
5307 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08005308 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005309
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005310 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005311 if (wakeupReasons.size() > 0) {
5312 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005313 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005314 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005315 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005316 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
5317 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005318 }
5319 Collections.sort(reasons, timerComparator);
5320 for (int i=0; i<reasons.size(); i++) {
5321 TimerEntry timer = reasons.get(i);
5322 String linePrefix = ": ";
5323 sb.setLength(0);
5324 sb.append(prefix);
5325 sb.append(" Wakeup reason ");
5326 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005327 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5328 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005329 pw.println(sb.toString());
5330 }
5331 pw.println();
5332 }
Dianne Hackborn81038902012-11-26 17:04:09 -08005333 }
Evan Millar22ac0432009-03-31 11:33:18 -07005334
James Carr2dd7e5e2016-07-20 18:48:39 -07005335 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
Bookatz50df7112017-08-04 14:53:26 -07005336 if (mMemoryStats.size() > 0) {
5337 pw.println(" Memory Stats");
5338 for (int i = 0; i < mMemoryStats.size(); i++) {
5339 sb.setLength(0);
5340 sb.append(" Bandwidth ");
5341 sb.append(mMemoryStats.keyAt(i));
5342 sb.append(" Time ");
5343 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
5344 pw.println(sb.toString());
5345 }
5346 pw.println();
5347 }
5348
5349 final Map<String, ? extends Timer> rpmStats = getRpmStats();
5350 if (rpmStats.size() > 0) {
5351 pw.print(prefix); pw.println(" Resource Power Manager Stats");
5352 if (rpmStats.size() > 0) {
5353 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
5354 final String timerName = ent.getKey();
5355 final Timer timer = ent.getValue();
5356 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5357 }
5358 }
5359 pw.println();
5360 }
Bookatz82b341172017-09-07 19:06:08 -07005361 if (SCREEN_OFF_RPM_STATS_ENABLED) {
5362 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
Bookatz50df7112017-08-04 14:53:26 -07005363 if (screenOffRpmStats.size() > 0) {
Bookatz82b341172017-09-07 19:06:08 -07005364 pw.print(prefix);
5365 pw.println(" Resource Power Manager Stats for when screen was off");
5366 if (screenOffRpmStats.size() > 0) {
5367 for (Map.Entry<String, ? extends Timer> ent : screenOffRpmStats.entrySet()) {
5368 final String timerName = ent.getKey();
5369 final Timer timer = ent.getValue();
5370 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5371 }
Bookatz50df7112017-08-04 14:53:26 -07005372 }
Bookatz82b341172017-09-07 19:06:08 -07005373 pw.println();
Bookatz50df7112017-08-04 14:53:26 -07005374 }
James Carr2dd7e5e2016-07-20 18:48:39 -07005375 }
5376
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005377 final long[] cpuFreqs = getCpuFreqs();
5378 if (cpuFreqs != null) {
5379 sb.setLength(0);
Bookatz50df7112017-08-04 14:53:26 -07005380 sb.append(" CPU freqs:");
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005381 for (int i = 0; i < cpuFreqs.length; ++i) {
5382 sb.append(" " + cpuFreqs[i]);
5383 }
5384 pw.println(sb.toString());
Bookatz50df7112017-08-04 14:53:26 -07005385 pw.println();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005386 }
5387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005388 for (int iu=0; iu<NU; iu++) {
5389 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005390 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005391 continue;
5392 }
Bookatzc8c44962017-05-11 12:12:54 -07005393
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005394 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07005395
5396 pw.print(prefix);
5397 pw.print(" ");
5398 UserHandle.formatUid(pw, uid);
5399 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005400 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005401
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005402 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
5403 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
5404 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
5405 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005406 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
5407 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
5408
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005409 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
5410 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005411 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
5412 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005413
5414 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
5415 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
5416
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005417 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
5418 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
5419 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08005420 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
5421 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5422 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
5423 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005424 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005425
Adam Lesinski5f056f62016-07-14 16:56:08 -07005426 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
5427 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
5428
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005429 if (mobileRxBytes > 0 || mobileTxBytes > 0
5430 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005431 pw.print(prefix); pw.print(" Mobile network: ");
5432 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005433 pw.print(formatBytesLocked(mobileTxBytes));
5434 pw.print(" sent (packets "); pw.print(mobileRxPackets);
5435 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005436 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005437 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
5438 sb.setLength(0);
5439 sb.append(prefix); sb.append(" Mobile radio active: ");
5440 formatTimeMs(sb, uidMobileActiveTime / 1000);
5441 sb.append("(");
5442 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
5443 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
5444 long packets = mobileRxPackets + mobileTxPackets;
5445 if (packets == 0) {
5446 packets = 1;
5447 }
5448 sb.append(" @ ");
5449 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
5450 sb.append(" mspp");
5451 pw.println(sb.toString());
5452 }
5453
Adam Lesinski5f056f62016-07-14 16:56:08 -07005454 if (mobileWakeup > 0) {
5455 sb.setLength(0);
5456 sb.append(prefix);
5457 sb.append(" Mobile radio AP wakeups: ");
5458 sb.append(mobileWakeup);
5459 pw.println(sb.toString());
5460 }
5461
Siddharth Rayb50a6842017-12-14 15:15:28 -08005462 printControllerActivityIfInteresting(pw, sb, prefix + " ",
5463 CELLULAR_CONTROLLER_NAME, u.getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005464
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005465 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005466 pw.print(prefix); pw.print(" Wi-Fi network: ");
5467 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005468 pw.print(formatBytesLocked(wifiTxBytes));
5469 pw.print(" sent (packets "); pw.print(wifiRxPackets);
5470 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005471 }
5472
Dianne Hackborn62793e42015-03-09 11:15:41 -07005473 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08005474 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005475 || uidWifiRunningTime != 0) {
5476 sb.setLength(0);
5477 sb.append(prefix); sb.append(" Wifi Running: ");
5478 formatTimeMs(sb, uidWifiRunningTime / 1000);
5479 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
5480 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07005481 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005482 formatTimeMs(sb, fullWifiLockOnTime / 1000);
5483 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
5484 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08005485 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005486 formatTimeMs(sb, wifiScanTime / 1000);
5487 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07005488 whichBatteryRealtime)); sb.append(") ");
5489 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08005490 sb.append("x\n");
5491 // actual and background times are unpooled and since reset (regardless of 'which')
5492 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
5493 formatTimeMs(sb, wifiScanActualTime / 1000);
5494 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
5495 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5496 sb.append(") ");
5497 sb.append(wifiScanCount);
5498 sb.append("x\n");
5499 sb.append(prefix); sb.append(" Background Wifi Scan: ");
5500 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
5501 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
5502 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5503 sb.append(") ");
5504 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07005505 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005506 pw.println(sb.toString());
5507 }
5508
Adam Lesinski5f056f62016-07-14 16:56:08 -07005509 if (wifiWakeup > 0) {
5510 sb.setLength(0);
5511 sb.append(prefix);
5512 sb.append(" WiFi AP wakeups: ");
5513 sb.append(wifiWakeup);
5514 pw.println(sb.toString());
5515 }
5516
Siddharth Rayb50a6842017-12-14 15:15:28 -08005517 printControllerActivityIfInteresting(pw, sb, prefix + " ", WIFI_CONTROLLER_NAME,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005518 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07005519
Adam Lesinski50e47602015-12-04 17:04:54 -08005520 if (btRxBytes > 0 || btTxBytes > 0) {
5521 pw.print(prefix); pw.print(" Bluetooth network: ");
5522 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
5523 pw.print(formatBytesLocked(btTxBytes));
5524 pw.println(" sent");
5525 }
5526
Bookatz867c0d72017-03-07 18:23:42 -08005527 final Timer bleTimer = u.getBluetoothScanTimer();
5528 if (bleTimer != null) {
5529 // Convert from microseconds to milliseconds with rounding
5530 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
5531 / 1000;
5532 if (totalTimeMs != 0) {
5533 final int count = bleTimer.getCountLocked(which);
5534 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
5535 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005536 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5537 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
5538 final long actualTimeMsBg = bleTimerBg != null ?
5539 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005540 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07005541 final int resultCount = u.getBluetoothScanResultCounter() != null ?
5542 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005543 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
5544 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
5545 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
5546 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
5547 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
5548 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5549 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
5550 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
5551 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
5552 final Timer unoptimizedScanTimerBg =
5553 u.getBluetoothUnoptimizedScanBackgroundTimer();
5554 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
5555 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5556 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
5557 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005558
5559 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08005560 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07005561 sb.append(prefix);
5562 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08005563 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07005564 sb.append(" (");
5565 sb.append(count);
5566 sb.append(" times)");
5567 if (bleTimer.isRunningLocked()) {
5568 sb.append(" (currently running)");
5569 }
5570 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005571 }
Bookatzb1f04f32017-05-19 13:57:32 -07005572
5573 sb.append(prefix);
5574 sb.append(" Bluetooth Scan (total actual realtime): ");
5575 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
5576 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005577 sb.append(count);
5578 sb.append(" times)");
5579 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07005580 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08005581 }
Bookatzb1f04f32017-05-19 13:57:32 -07005582 sb.append("\n");
5583 if (actualTimeMsBg > 0 || countBg > 0) {
5584 sb.append(prefix);
5585 sb.append(" Bluetooth Scan (background realtime): ");
5586 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
5587 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005588 sb.append(countBg);
5589 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07005590 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
5591 sb.append(" (currently running in background)");
5592 }
5593 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005594 }
Bookatzb1f04f32017-05-19 13:57:32 -07005595
5596 sb.append(prefix);
5597 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07005598 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07005599 sb.append(" (");
5600 sb.append(resultCountBg);
5601 sb.append(" in background)");
5602
5603 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
5604 sb.append("\n");
5605 sb.append(prefix);
5606 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
5607 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
5608 sb.append(" (max ");
5609 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
5610 sb.append(")");
5611 if (unoptimizedScanTimer != null
5612 && unoptimizedScanTimer.isRunningLocked()) {
5613 sb.append(" (currently running unoptimized)");
5614 }
5615 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
5616 sb.append("\n");
5617 sb.append(prefix);
5618 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
5619 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
5620 sb.append(" (max ");
5621 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
5622 sb.append(")");
5623 if (unoptimizedScanTimerBg.isRunningLocked()) {
5624 sb.append(" (currently running unoptimized in background)");
5625 }
5626 }
5627 }
Bookatz867c0d72017-03-07 18:23:42 -08005628 pw.println(sb.toString());
5629 uidActivity = true;
5630 }
5631 }
5632
5633
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005634
Dianne Hackborn617f8772009-03-31 15:04:46 -07005635 if (u.hasUserActivity()) {
5636 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07005637 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005638 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005639 if (val != 0) {
5640 if (!hasData) {
5641 sb.setLength(0);
5642 sb.append(" User activity: ");
5643 hasData = true;
5644 } else {
5645 sb.append(", ");
5646 }
5647 sb.append(val);
5648 sb.append(" ");
5649 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
5650 }
5651 }
5652 if (hasData) {
5653 pw.println(sb.toString());
5654 }
5655 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005656
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005657 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
5658 = u.getWakelockStats();
5659 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005660 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005661 int countWakelock = 0;
5662 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
5663 final Uid.Wakelock wl = wakelocks.valueAt(iw);
5664 String linePrefix = ": ";
5665 sb.setLength(0);
5666 sb.append(prefix);
5667 sb.append(" Wake lock ");
5668 sb.append(wakelocks.keyAt(iw));
5669 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
5670 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005671 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
5672 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005673 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005674 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
5675 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005676 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
5677 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005678 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
5679 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07005680 sb.append(" realtime");
5681 pw.println(sb.toString());
5682 uidActivity = true;
5683 countWakelock++;
5684
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005685 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
5686 rawRealtime, which);
5687 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
5688 rawRealtime, which);
5689 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
5690 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005691 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07005692 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005693 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005694 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07005695 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
5696 // pooled and therefore just a lower bound)
5697 long actualTotalPartialWakelock = 0;
5698 long actualBgPartialWakelock = 0;
5699 if (u.getAggregatedPartialWakelockTimer() != null) {
5700 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
5701 // Convert from microseconds to milliseconds with rounding
5702 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07005703 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07005704 final Timer bgAggTimer = aggTimer.getSubTimer();
5705 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005706 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07005707 }
5708
5709 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
5710 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
5711 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005712 sb.setLength(0);
5713 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005714 sb.append(" TOTAL wake: ");
5715 boolean needComma = false;
5716 if (totalFullWakelock != 0) {
5717 needComma = true;
5718 formatTimeMs(sb, totalFullWakelock);
5719 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005720 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005721 if (totalPartialWakelock != 0) {
5722 if (needComma) {
5723 sb.append(", ");
5724 }
5725 needComma = true;
5726 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07005727 sb.append("blamed partial");
5728 }
5729 if (actualTotalPartialWakelock != 0) {
5730 if (needComma) {
5731 sb.append(", ");
5732 }
5733 needComma = true;
5734 formatTimeMs(sb, actualTotalPartialWakelock);
5735 sb.append("actual partial");
5736 }
5737 if (actualBgPartialWakelock != 0) {
5738 if (needComma) {
5739 sb.append(", ");
5740 }
5741 needComma = true;
5742 formatTimeMs(sb, actualBgPartialWakelock);
5743 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005744 }
5745 if (totalWindowWakelock != 0) {
5746 if (needComma) {
5747 sb.append(", ");
5748 }
5749 needComma = true;
5750 formatTimeMs(sb, totalWindowWakelock);
5751 sb.append("window");
5752 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005753 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07005754 if (needComma) {
5755 sb.append(",");
5756 }
5757 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005758 formatTimeMs(sb, totalDrawWakelock);
5759 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07005760 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005761 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005762 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005763 }
5764 }
5765
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07005766 // Calculate multicast wakelock stats
5767 final Timer mcTimer = u.getMulticastWakelockStats();
5768 if (mcTimer != null) {
5769 final long multicastWakeLockTimeMicros = mcTimer.getTotalTimeLocked(rawRealtime, which);
5770 final int multicastWakeLockCount = mcTimer.getCountLocked(which);
5771
5772 if (multicastWakeLockTimeMicros > 0) {
5773 sb.setLength(0);
5774 sb.append(prefix);
5775 sb.append(" WiFi Multicast Wakelock");
5776 sb.append(" count = ");
5777 sb.append(multicastWakeLockCount);
5778 sb.append(" time = ");
5779 formatTimeMsNoSpace(sb, (multicastWakeLockTimeMicros + 500) / 1000);
5780 pw.println(sb.toString());
5781 }
5782 }
5783
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005784 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
5785 for (int isy=syncs.size()-1; isy>=0; isy--) {
5786 final Timer timer = syncs.valueAt(isy);
5787 // Convert from microseconds to milliseconds with rounding
5788 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5789 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07005790 final Timer bgTimer = timer.getSubTimer();
5791 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005792 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07005793 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005794 sb.setLength(0);
5795 sb.append(prefix);
5796 sb.append(" Sync ");
5797 sb.append(syncs.keyAt(isy));
5798 sb.append(": ");
5799 if (totalTime != 0) {
5800 formatTimeMs(sb, totalTime);
5801 sb.append("realtime (");
5802 sb.append(count);
5803 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07005804 if (bgTime > 0) {
5805 sb.append(", ");
5806 formatTimeMs(sb, bgTime);
5807 sb.append("background (");
5808 sb.append(bgCount);
5809 sb.append(" times)");
5810 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005811 } else {
5812 sb.append("(not used)");
5813 }
5814 pw.println(sb.toString());
5815 uidActivity = true;
5816 }
5817
5818 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
5819 for (int ij=jobs.size()-1; ij>=0; ij--) {
5820 final Timer timer = jobs.valueAt(ij);
5821 // Convert from microseconds to milliseconds with rounding
5822 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5823 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07005824 final Timer bgTimer = timer.getSubTimer();
5825 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005826 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07005827 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005828 sb.setLength(0);
5829 sb.append(prefix);
5830 sb.append(" Job ");
5831 sb.append(jobs.keyAt(ij));
5832 sb.append(": ");
5833 if (totalTime != 0) {
5834 formatTimeMs(sb, totalTime);
5835 sb.append("realtime (");
5836 sb.append(count);
5837 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07005838 if (bgTime > 0) {
5839 sb.append(", ");
5840 formatTimeMs(sb, bgTime);
5841 sb.append("background (");
5842 sb.append(bgCount);
5843 sb.append(" times)");
5844 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005845 } else {
5846 sb.append("(not used)");
5847 }
5848 pw.println(sb.toString());
5849 uidActivity = true;
5850 }
5851
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005852 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
5853 for (int ic=completions.size()-1; ic>=0; ic--) {
5854 SparseIntArray types = completions.valueAt(ic);
5855 if (types != null) {
5856 pw.print(prefix);
5857 pw.print(" Job Completions ");
5858 pw.print(completions.keyAt(ic));
5859 pw.print(":");
5860 for (int it=0; it<types.size(); it++) {
5861 pw.print(" ");
5862 pw.print(JobParameters.getReasonName(types.keyAt(it)));
5863 pw.print("(");
5864 pw.print(types.valueAt(it));
5865 pw.print("x)");
5866 }
5867 pw.println();
5868 }
5869 }
5870
Amith Yamasani977e11f2018-02-16 11:29:54 -08005871 u.getDeferredJobsLineLocked(sb, which);
5872 if (sb.length() > 0) {
5873 pw.print(" Jobs deferred on launch "); pw.println(sb.toString());
5874 }
5875
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005876 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
5877 prefix, "Flashlight");
5878 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
5879 prefix, "Camera");
5880 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
5881 prefix, "Video");
5882 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
5883 prefix, "Audio");
5884
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005885 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
5886 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005887 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005888 final Uid.Sensor se = sensors.valueAt(ise);
5889 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005890 sb.setLength(0);
5891 sb.append(prefix);
5892 sb.append(" Sensor ");
5893 int handle = se.getHandle();
5894 if (handle == Uid.Sensor.GPS) {
5895 sb.append("GPS");
5896 } else {
5897 sb.append(handle);
5898 }
5899 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005900
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005901 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005902 if (timer != null) {
5903 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005904 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5905 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005906 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005907 final Timer bgTimer = se.getSensorBackgroundTime();
5908 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005909 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5910 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5911 final long bgActualTime = bgTimer != null ?
5912 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5913
Dianne Hackborn61659e52014-07-09 16:13:01 -07005914 //timer.logState();
5915 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005916 if (actualTime != totalTime) {
5917 formatTimeMs(sb, totalTime);
5918 sb.append("blamed realtime, ");
5919 }
5920
5921 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005922 sb.append("realtime (");
5923 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005924 sb.append(" times)");
5925
5926 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005927 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005928 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5929 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005930 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005931 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005933 } else {
5934 sb.append("(not used)");
5935 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005936 } else {
5937 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005938 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005939
5940 pw.println(sb.toString());
5941 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005942 }
5943
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005944 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5945 "Vibrator");
5946 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5947 prefix, "Foreground activities");
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005948 uidActivity |= printTimer(pw, sb, u.getForegroundServiceTimer(), rawRealtime, which,
5949 prefix, "Foreground services");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005950
Dianne Hackborn61659e52014-07-09 16:13:01 -07005951 long totalStateTime = 0;
5952 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5953 long time = u.getProcessStateTime(ips, rawRealtime, which);
5954 if (time > 0) {
5955 totalStateTime += time;
5956 sb.setLength(0);
5957 sb.append(prefix);
5958 sb.append(" ");
5959 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5960 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005961 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005962 pw.println(sb.toString());
5963 uidActivity = true;
5964 }
5965 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005966 if (totalStateTime > 0) {
5967 sb.setLength(0);
5968 sb.append(prefix);
5969 sb.append(" Total running: ");
5970 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5971 pw.println(sb.toString());
5972 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005973
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005974 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5975 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005976 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005977 sb.setLength(0);
5978 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005979 sb.append(" Total cpu time: u=");
5980 formatTimeMs(sb, userCpuTimeUs / 1000);
5981 sb.append("s=");
5982 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005983 pw.println(sb.toString());
5984 }
5985
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005986 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5987 if (cpuFreqTimes != null) {
5988 sb.setLength(0);
5989 sb.append(" Total cpu time per freq:");
5990 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5991 sb.append(" " + cpuFreqTimes[i]);
5992 }
5993 pw.println(sb.toString());
5994 }
5995 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5996 if (screenOffCpuFreqTimes != null) {
5997 sb.setLength(0);
5998 sb.append(" Total screen-off cpu time per freq:");
5999 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
6000 sb.append(" " + screenOffCpuFreqTimes[i]);
6001 }
6002 pw.println(sb.toString());
6003 }
6004
Sudheer Shankab2f83c12017-11-13 19:25:01 -08006005 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
6006 final long[] cpuTimes = u.getCpuFreqTimes(which, procState);
6007 if (cpuTimes != null) {
6008 sb.setLength(0);
6009 sb.append(" Cpu times per freq at state "
6010 + Uid.PROCESS_STATE_NAMES[procState] + ":");
6011 for (int i = 0; i < cpuTimes.length; ++i) {
6012 sb.append(" " + cpuTimes[i]);
6013 }
6014 pw.println(sb.toString());
6015 }
6016
6017 final long[] screenOffCpuTimes = u.getScreenOffCpuFreqTimes(which, procState);
6018 if (screenOffCpuTimes != null) {
6019 sb.setLength(0);
6020 sb.append(" Screen-off cpu times per freq at state "
6021 + Uid.PROCESS_STATE_NAMES[procState] + ":");
6022 for (int i = 0; i < screenOffCpuTimes.length; ++i) {
6023 sb.append(" " + screenOffCpuTimes[i]);
6024 }
6025 pw.println(sb.toString());
6026 }
6027 }
6028
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006029 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
6030 = u.getProcessStats();
6031 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
6032 final Uid.Proc ps = processStats.valueAt(ipr);
6033 long userTime;
6034 long systemTime;
6035 long foregroundTime;
6036 int starts;
6037 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006038
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006039 userTime = ps.getUserTime(which);
6040 systemTime = ps.getSystemTime(which);
6041 foregroundTime = ps.getForegroundTime(which);
6042 starts = ps.getStarts(which);
6043 final int numCrashes = ps.getNumCrashes(which);
6044 final int numAnrs = ps.getNumAnrs(which);
6045 numExcessive = which == STATS_SINCE_CHARGED
6046 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006047
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006048 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
6049 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
6050 sb.setLength(0);
6051 sb.append(prefix); sb.append(" Proc ");
6052 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
6053 sb.append(prefix); sb.append(" CPU: ");
6054 formatTimeMs(sb, userTime); sb.append("usr + ");
6055 formatTimeMs(sb, systemTime); sb.append("krn ; ");
6056 formatTimeMs(sb, foregroundTime); sb.append("fg");
6057 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
6058 sb.append("\n"); sb.append(prefix); sb.append(" ");
6059 boolean hasOne = false;
6060 if (starts != 0) {
6061 hasOne = true;
6062 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07006063 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006064 if (numCrashes != 0) {
6065 if (hasOne) {
6066 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006067 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006068 hasOne = true;
6069 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006070 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006071 if (numAnrs != 0) {
6072 if (hasOne) {
6073 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006074 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006075 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006076 }
6077 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006078 pw.println(sb.toString());
6079 for (int e=0; e<numExcessive; e++) {
6080 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
6081 if (ew != null) {
6082 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07006083 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006084 pw.print("cpu");
6085 } else {
6086 pw.print("unknown");
6087 }
6088 pw.print(" use: ");
6089 TimeUtils.formatDuration(ew.usedTime, pw);
6090 pw.print(" over ");
6091 TimeUtils.formatDuration(ew.overTime, pw);
6092 if (ew.overTime != 0) {
6093 pw.print(" (");
6094 pw.print((ew.usedTime*100)/ew.overTime);
6095 pw.println("%)");
6096 }
6097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006098 }
6099 uidActivity = true;
6100 }
6101 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006102
6103 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
6104 = u.getPackageStats();
6105 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
6106 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
6107 pw.println(":");
6108 boolean apkActivity = false;
6109 final Uid.Pkg ps = packageStats.valueAt(ipkg);
6110 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
6111 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
6112 pw.print(prefix); pw.print(" Wakeup alarm ");
6113 pw.print(alarms.keyAt(iwa)); pw.print(": ");
6114 pw.print(alarms.valueAt(iwa).getCountLocked(which));
6115 pw.println(" times");
6116 apkActivity = true;
6117 }
6118 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
6119 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
6120 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
6121 final long startTime = ss.getStartTime(batteryUptime, which);
6122 final int starts = ss.getStarts(which);
6123 final int launches = ss.getLaunches(which);
6124 if (startTime != 0 || starts != 0 || launches != 0) {
6125 sb.setLength(0);
6126 sb.append(prefix); sb.append(" Service ");
6127 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
6128 sb.append(prefix); sb.append(" Created for: ");
6129 formatTimeMs(sb, startTime / 1000);
6130 sb.append("uptime\n");
6131 sb.append(prefix); sb.append(" Starts: ");
6132 sb.append(starts);
6133 sb.append(", launches: "); sb.append(launches);
6134 pw.println(sb.toString());
6135 apkActivity = true;
6136 }
6137 }
6138 if (!apkActivity) {
6139 pw.print(prefix); pw.println(" (nothing executed)");
6140 }
6141 uidActivity = true;
6142 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006143 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006144 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006145 }
6146 }
6147 }
6148
Kweku Adams71a95312018-04-16 16:54:24 -07006149 static void printBitDescriptions(StringBuilder sb, int oldval, int newval,
6150 HistoryTag wakelockTag, BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006151 int diff = oldval ^ newval;
6152 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006153 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006154 for (int i=0; i<descriptions.length; i++) {
6155 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006156 if ((diff&bd.mask) != 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006157 sb.append(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006158 if (bd.shift < 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006159 sb.append((newval & bd.mask) != 0 ? "+" : "-");
6160 sb.append(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006161 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
6162 didWake = true;
Kweku Adams71a95312018-04-16 16:54:24 -07006163 sb.append("=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006164 if (longNames) {
Kweku Adams71a95312018-04-16 16:54:24 -07006165 UserHandle.formatUid(sb, wakelockTag.uid);
6166 sb.append(":\"");
6167 sb.append(wakelockTag.string);
6168 sb.append("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006169 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006170 sb.append(wakelockTag.poolIdx);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006171 }
6172 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006173 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006174 sb.append(longNames ? bd.name : bd.shortName);
6175 sb.append("=");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006176 int val = (newval&bd.mask)>>bd.shift;
6177 if (bd.values != null && val >= 0 && val < bd.values.length) {
Kweku Adams71a95312018-04-16 16:54:24 -07006178 sb.append(longNames ? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006179 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006180 sb.append(val);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006181 }
6182 }
6183 }
6184 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006185 if (!didWake && wakelockTag != null) {
Kweku Adams71a95312018-04-16 16:54:24 -07006186 sb.append(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006187 if (longNames) {
Kweku Adams71a95312018-04-16 16:54:24 -07006188 UserHandle.formatUid(sb, wakelockTag.uid);
6189 sb.append(":\"");
6190 sb.append(wakelockTag.string);
6191 sb.append("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006192 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006193 sb.append(wakelockTag.poolIdx);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006194 }
6195 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006196 }
Mike Mac2f518a2017-09-19 16:06:03 -07006197
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006198 public void prepareForDumpLocked() {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006199 // We don't need to require subclasses implement this.
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006200 }
6201
6202 public static class HistoryPrinter {
6203 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006204 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006205 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006206 int oldStatus = -1;
6207 int oldHealth = -1;
6208 int oldPlug = -1;
6209 int oldTemp = -1;
6210 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006211 int oldChargeMAh = -1;
Blake Kragtencb308d92019-01-28 10:54:13 -08006212 double oldModemRailChargeMah = -1;
6213 double oldWifiRailChargeMah = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006214 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006215
Dianne Hackborn3251b902014-06-20 14:40:53 -07006216 void reset() {
6217 oldState = oldState2 = 0;
6218 oldLevel = -1;
6219 oldStatus = -1;
6220 oldHealth = -1;
6221 oldPlug = -1;
6222 oldTemp = -1;
6223 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006224 oldChargeMAh = -1;
Blake Kragtencb308d92019-01-28 10:54:13 -08006225 oldModemRailChargeMah = -1;
6226 oldWifiRailChargeMah = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07006227 }
6228
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006229 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006230 boolean verbose) {
Kweku Adams71a95312018-04-16 16:54:24 -07006231 pw.print(printNextItem(rec, baseTime, checkin, verbose));
6232 }
6233
6234 /** Print the next history item to proto. */
6235 public void printNextItem(ProtoOutputStream proto, HistoryItem rec, long baseTime,
6236 boolean verbose) {
6237 String item = printNextItem(rec, baseTime, true, verbose);
6238 for (String line : item.split("\n")) {
6239 proto.write(BatteryStatsServiceDumpHistoryProto.CSV_LINES, line);
6240 }
6241 }
6242
6243 private String printNextItem(HistoryItem rec, long baseTime, boolean checkin,
6244 boolean verbose) {
6245 StringBuilder item = new StringBuilder();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006246 if (!checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006247 item.append(" ");
6248 TimeUtils.formatDuration(
6249 rec.time - baseTime, item, TimeUtils.HUNDRED_DAY_FIELD_LEN);
6250 item.append(" (");
6251 item.append(rec.numReadInts);
6252 item.append(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006253 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006254 item.append(BATTERY_STATS_CHECKIN_VERSION); item.append(',');
6255 item.append(HISTORY_DATA); item.append(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006256 if (lastTime < 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006257 item.append(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006258 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006259 item.append(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006260 }
6261 lastTime = rec.time;
6262 }
6263 if (rec.cmd == HistoryItem.CMD_START) {
6264 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006265 item.append(":");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006266 }
Kweku Adams71a95312018-04-16 16:54:24 -07006267 item.append("START\n");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006268 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006269 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
6270 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006271 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006272 item.append(":");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006273 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006274 if (rec.cmd == HistoryItem.CMD_RESET) {
Kweku Adams71a95312018-04-16 16:54:24 -07006275 item.append("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006276 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006277 }
Kweku Adams71a95312018-04-16 16:54:24 -07006278 item.append("TIME:");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006279 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006280 item.append(rec.currentTime);
6281 item.append("\n");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006282 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006283 item.append(" ");
6284 item.append(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006285 rec.currentTime).toString());
Kweku Adams71a95312018-04-16 16:54:24 -07006286 item.append("\n");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006287 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006288 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
6289 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006290 item.append(":");
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006291 }
Kweku Adams71a95312018-04-16 16:54:24 -07006292 item.append("SHUTDOWN\n");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006293 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
6294 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006295 item.append(":");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006296 }
Kweku Adams71a95312018-04-16 16:54:24 -07006297 item.append("*OVERFLOW*\n");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006298 } else {
6299 if (!checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006300 if (rec.batteryLevel < 10) item.append("00");
6301 else if (rec.batteryLevel < 100) item.append("0");
6302 item.append(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006303 if (verbose) {
Kweku Adams71a95312018-04-16 16:54:24 -07006304 item.append(" ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006305 if (rec.states < 0) ;
Kweku Adams71a95312018-04-16 16:54:24 -07006306 else if (rec.states < 0x10) item.append("0000000");
6307 else if (rec.states < 0x100) item.append("000000");
6308 else if (rec.states < 0x1000) item.append("00000");
6309 else if (rec.states < 0x10000) item.append("0000");
6310 else if (rec.states < 0x100000) item.append("000");
6311 else if (rec.states < 0x1000000) item.append("00");
6312 else if (rec.states < 0x10000000) item.append("0");
6313 item.append(Integer.toHexString(rec.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006314 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006315 } else {
6316 if (oldLevel != rec.batteryLevel) {
6317 oldLevel = rec.batteryLevel;
Kweku Adams71a95312018-04-16 16:54:24 -07006318 item.append(",Bl="); item.append(rec.batteryLevel);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006319 }
6320 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006321 if (oldStatus != rec.batteryStatus) {
6322 oldStatus = rec.batteryStatus;
Kweku Adams71a95312018-04-16 16:54:24 -07006323 item.append(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006324 switch (oldStatus) {
6325 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Kweku Adams71a95312018-04-16 16:54:24 -07006326 item.append(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006327 break;
6328 case BatteryManager.BATTERY_STATUS_CHARGING:
Kweku Adams71a95312018-04-16 16:54:24 -07006329 item.append(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006330 break;
6331 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Kweku Adams71a95312018-04-16 16:54:24 -07006332 item.append(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006333 break;
6334 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Kweku Adams71a95312018-04-16 16:54:24 -07006335 item.append(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006336 break;
6337 case BatteryManager.BATTERY_STATUS_FULL:
Kweku Adams71a95312018-04-16 16:54:24 -07006338 item.append(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006339 break;
6340 default:
Kweku Adams71a95312018-04-16 16:54:24 -07006341 item.append(oldStatus);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006342 break;
6343 }
6344 }
6345 if (oldHealth != rec.batteryHealth) {
6346 oldHealth = rec.batteryHealth;
Kweku Adams71a95312018-04-16 16:54:24 -07006347 item.append(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006348 switch (oldHealth) {
6349 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Kweku Adams71a95312018-04-16 16:54:24 -07006350 item.append(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006351 break;
6352 case BatteryManager.BATTERY_HEALTH_GOOD:
Kweku Adams71a95312018-04-16 16:54:24 -07006353 item.append(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006354 break;
6355 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Kweku Adams71a95312018-04-16 16:54:24 -07006356 item.append(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006357 break;
6358 case BatteryManager.BATTERY_HEALTH_DEAD:
Kweku Adams71a95312018-04-16 16:54:24 -07006359 item.append(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006360 break;
6361 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Kweku Adams71a95312018-04-16 16:54:24 -07006362 item.append(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006363 break;
6364 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Kweku Adams71a95312018-04-16 16:54:24 -07006365 item.append(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006366 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006367 case BatteryManager.BATTERY_HEALTH_COLD:
Kweku Adams71a95312018-04-16 16:54:24 -07006368 item.append(checkin ? "c" : "cold");
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006369 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006370 default:
Kweku Adams71a95312018-04-16 16:54:24 -07006371 item.append(oldHealth);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006372 break;
6373 }
6374 }
6375 if (oldPlug != rec.batteryPlugType) {
6376 oldPlug = rec.batteryPlugType;
Kweku Adams71a95312018-04-16 16:54:24 -07006377 item.append(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006378 switch (oldPlug) {
6379 case 0:
Kweku Adams71a95312018-04-16 16:54:24 -07006380 item.append(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006381 break;
6382 case BatteryManager.BATTERY_PLUGGED_AC:
Kweku Adams71a95312018-04-16 16:54:24 -07006383 item.append(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006384 break;
6385 case BatteryManager.BATTERY_PLUGGED_USB:
Kweku Adams71a95312018-04-16 16:54:24 -07006386 item.append(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006387 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006388 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Kweku Adams71a95312018-04-16 16:54:24 -07006389 item.append(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006390 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006391 default:
Kweku Adams71a95312018-04-16 16:54:24 -07006392 item.append(oldPlug);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006393 break;
6394 }
6395 }
6396 if (oldTemp != rec.batteryTemperature) {
6397 oldTemp = rec.batteryTemperature;
Kweku Adams71a95312018-04-16 16:54:24 -07006398 item.append(checkin ? ",Bt=" : " temp=");
6399 item.append(oldTemp);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006400 }
6401 if (oldVolt != rec.batteryVoltage) {
6402 oldVolt = rec.batteryVoltage;
Kweku Adams71a95312018-04-16 16:54:24 -07006403 item.append(checkin ? ",Bv=" : " volt=");
6404 item.append(oldVolt);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006405 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006406 final int chargeMAh = rec.batteryChargeUAh / 1000;
6407 if (oldChargeMAh != chargeMAh) {
6408 oldChargeMAh = chargeMAh;
Kweku Adams71a95312018-04-16 16:54:24 -07006409 item.append(checkin ? ",Bcc=" : " charge=");
6410 item.append(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07006411 }
Blake Kragtencb308d92019-01-28 10:54:13 -08006412 if (oldModemRailChargeMah != rec.modemRailChargeMah) {
6413 oldModemRailChargeMah = rec.modemRailChargeMah;
6414 item.append(checkin ? ",Mrc=" : " modemRailChargemAh=");
6415 item.append(new DecimalFormat("#.##").format(oldModemRailChargeMah));
6416 }
6417 if (oldWifiRailChargeMah != rec.wifiRailChargeMah) {
6418 oldWifiRailChargeMah = rec.wifiRailChargeMah;
6419 item.append(checkin ? ",Wrc=" : " wifiRailChargemAh=");
6420 item.append(new DecimalFormat("#.##").format(oldWifiRailChargeMah));
6421 }
Kweku Adams71a95312018-04-16 16:54:24 -07006422 printBitDescriptions(item, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006423 HISTORY_STATE_DESCRIPTIONS, !checkin);
Kweku Adams71a95312018-04-16 16:54:24 -07006424 printBitDescriptions(item, oldState2, rec.states2, null,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006425 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006426 if (rec.wakeReasonTag != null) {
6427 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006428 item.append(",wr=");
6429 item.append(rec.wakeReasonTag.poolIdx);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006430 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006431 item.append(" wake_reason=");
6432 item.append(rec.wakeReasonTag.uid);
6433 item.append(":\"");
6434 item.append(rec.wakeReasonTag.string);
6435 item.append("\"");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006436 }
6437 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08006438 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Kweku Adams71a95312018-04-16 16:54:24 -07006439 item.append(checkin ? "," : " ");
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006440 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006441 item.append("+");
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006442 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006443 item.append("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006444 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006445 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
6446 : HISTORY_EVENT_NAMES;
6447 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
6448 | HistoryItem.EVENT_FLAG_FINISH);
6449 if (idx >= 0 && idx < eventNames.length) {
Kweku Adams71a95312018-04-16 16:54:24 -07006450 item.append(eventNames[idx]);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006451 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006452 item.append(checkin ? "Ev" : "event");
6453 item.append(idx);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006454 }
Kweku Adams71a95312018-04-16 16:54:24 -07006455 item.append("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006456 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006457 item.append(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08006458 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006459 item.append(HISTORY_EVENT_INT_FORMATTERS[idx]
Adam Lesinski041d9172016-12-12 12:03:56 -08006460 .applyAsString(rec.eventTag.uid));
Kweku Adams71a95312018-04-16 16:54:24 -07006461 item.append(":\"");
6462 item.append(rec.eventTag.string);
6463 item.append("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006464 }
6465 }
Kweku Adams71a95312018-04-16 16:54:24 -07006466 item.append("\n");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006467 if (rec.stepDetails != null) {
6468 if (!checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006469 item.append(" Details: cpu=");
6470 item.append(rec.stepDetails.userTime);
6471 item.append("u+");
6472 item.append(rec.stepDetails.systemTime);
6473 item.append("s");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006474 if (rec.stepDetails.appCpuUid1 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006475 item.append(" (");
6476 printStepCpuUidDetails(item, rec.stepDetails.appCpuUid1,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006477 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6478 if (rec.stepDetails.appCpuUid2 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006479 item.append(", ");
6480 printStepCpuUidDetails(item, rec.stepDetails.appCpuUid2,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006481 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6482 }
6483 if (rec.stepDetails.appCpuUid3 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006484 item.append(", ");
6485 printStepCpuUidDetails(item, rec.stepDetails.appCpuUid3,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006486 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6487 }
Kweku Adams71a95312018-04-16 16:54:24 -07006488 item.append(')');
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006489 }
Kweku Adams71a95312018-04-16 16:54:24 -07006490 item.append("\n");
6491 item.append(" /proc/stat=");
6492 item.append(rec.stepDetails.statUserTime);
6493 item.append(" usr, ");
6494 item.append(rec.stepDetails.statSystemTime);
6495 item.append(" sys, ");
6496 item.append(rec.stepDetails.statIOWaitTime);
6497 item.append(" io, ");
6498 item.append(rec.stepDetails.statIrqTime);
6499 item.append(" irq, ");
6500 item.append(rec.stepDetails.statSoftIrqTime);
6501 item.append(" sirq, ");
6502 item.append(rec.stepDetails.statIdlTime);
6503 item.append(" idle");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006504 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
6505 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
6506 + rec.stepDetails.statSoftIrqTime;
6507 int total = totalRun + rec.stepDetails.statIdlTime;
6508 if (total > 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006509 item.append(" (");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006510 float perc = ((float)totalRun) / ((float)total) * 100;
Kweku Adams71a95312018-04-16 16:54:24 -07006511 item.append(String.format("%.1f%%", perc));
6512 item.append(" of ");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006513 StringBuilder sb = new StringBuilder(64);
6514 formatTimeMsNoSpace(sb, total*10);
Kweku Adams71a95312018-04-16 16:54:24 -07006515 item.append(sb);
6516 item.append(")");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006517 }
Kweku Adams71a95312018-04-16 16:54:24 -07006518 item.append(", PlatformIdleStat ");
6519 item.append(rec.stepDetails.statPlatformIdleState);
6520 item.append("\n");
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006521
Kweku Adams71a95312018-04-16 16:54:24 -07006522 item.append(", SubsystemPowerState ");
6523 item.append(rec.stepDetails.statSubsystemPowerState);
6524 item.append("\n");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006525 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006526 item.append(BATTERY_STATS_CHECKIN_VERSION); item.append(',');
6527 item.append(HISTORY_DATA); item.append(",0,Dcpu=");
6528 item.append(rec.stepDetails.userTime);
6529 item.append(":");
6530 item.append(rec.stepDetails.systemTime);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006531 if (rec.stepDetails.appCpuUid1 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006532 printStepCpuUidCheckinDetails(item, rec.stepDetails.appCpuUid1,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006533 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6534 if (rec.stepDetails.appCpuUid2 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006535 printStepCpuUidCheckinDetails(item, rec.stepDetails.appCpuUid2,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006536 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6537 }
6538 if (rec.stepDetails.appCpuUid3 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006539 printStepCpuUidCheckinDetails(item, rec.stepDetails.appCpuUid3,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006540 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6541 }
6542 }
Kweku Adams71a95312018-04-16 16:54:24 -07006543 item.append("\n");
6544 item.append(BATTERY_STATS_CHECKIN_VERSION); item.append(',');
6545 item.append(HISTORY_DATA); item.append(",0,Dpst=");
6546 item.append(rec.stepDetails.statUserTime);
6547 item.append(',');
6548 item.append(rec.stepDetails.statSystemTime);
6549 item.append(',');
6550 item.append(rec.stepDetails.statIOWaitTime);
6551 item.append(',');
6552 item.append(rec.stepDetails.statIrqTime);
6553 item.append(',');
6554 item.append(rec.stepDetails.statSoftIrqTime);
6555 item.append(',');
6556 item.append(rec.stepDetails.statIdlTime);
6557 item.append(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006558 if (rec.stepDetails.statPlatformIdleState != null) {
Kweku Adams71a95312018-04-16 16:54:24 -07006559 item.append(rec.stepDetails.statPlatformIdleState);
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006560 if (rec.stepDetails.statSubsystemPowerState != null) {
Kweku Adams71a95312018-04-16 16:54:24 -07006561 item.append(',');
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006562 }
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006563 }
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006564
6565 if (rec.stepDetails.statSubsystemPowerState != null) {
Kweku Adams71a95312018-04-16 16:54:24 -07006566 item.append(rec.stepDetails.statSubsystemPowerState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006567 }
Kweku Adams71a95312018-04-16 16:54:24 -07006568 item.append("\n");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006569 }
6570 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006571 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006572 oldState2 = rec.states2;
Blake Kragtenbc75c722019-03-20 17:14:58 -07006573 // Clear High Tx Power Flag for volta positioning
6574 if ((rec.states2 & HistoryItem.STATE2_CELLULAR_HIGH_TX_POWER_FLAG) != 0) {
6575 rec.states2 &= ~HistoryItem.STATE2_CELLULAR_HIGH_TX_POWER_FLAG;
6576 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006577 }
Kweku Adams71a95312018-04-16 16:54:24 -07006578
6579 return item.toString();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006580 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006581
Kweku Adams71a95312018-04-16 16:54:24 -07006582 private void printStepCpuUidDetails(StringBuilder sb, int uid, int utime, int stime) {
6583 UserHandle.formatUid(sb, uid);
6584 sb.append("=");
6585 sb.append(utime);
6586 sb.append("u+");
6587 sb.append(stime);
6588 sb.append("s");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006589 }
6590
Kweku Adams71a95312018-04-16 16:54:24 -07006591 private void printStepCpuUidCheckinDetails(StringBuilder sb, int uid, int utime,
6592 int stime) {
6593 sb.append('/');
6594 sb.append(uid);
6595 sb.append(":");
6596 sb.append(utime);
6597 sb.append(":");
6598 sb.append(stime);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006599 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006600 }
6601
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006602 private void printSizeValue(PrintWriter pw, long size) {
6603 float result = size;
6604 String suffix = "";
6605 if (result >= 10*1024) {
6606 suffix = "KB";
6607 result = result / 1024;
6608 }
6609 if (result >= 10*1024) {
6610 suffix = "MB";
6611 result = result / 1024;
6612 }
6613 if (result >= 10*1024) {
6614 suffix = "GB";
6615 result = result / 1024;
6616 }
6617 if (result >= 10*1024) {
6618 suffix = "TB";
6619 result = result / 1024;
6620 }
6621 if (result >= 10*1024) {
6622 suffix = "PB";
6623 result = result / 1024;
6624 }
6625 pw.print((int)result);
6626 pw.print(suffix);
6627 }
6628
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006629 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
6630 String label3, long estimatedTime) {
6631 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006632 return false;
6633 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006634 pw.print(label1);
6635 pw.print(label2);
6636 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006637 StringBuilder sb = new StringBuilder(64);
6638 formatTimeMs(sb, estimatedTime);
6639 pw.print(sb);
6640 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006641 return true;
6642 }
6643
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006644 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
6645 LevelStepTracker steps, boolean checkin) {
6646 if (steps == null) {
6647 return false;
6648 }
6649 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006650 if (count <= 0) {
6651 return false;
6652 }
6653 if (!checkin) {
6654 pw.println(header);
6655 }
Kweku Adams030980a2015-04-01 16:07:48 -07006656 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006657 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006658 long duration = steps.getDurationAt(i);
6659 int level = steps.getLevelAt(i);
6660 long initMode = steps.getInitModeAt(i);
6661 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006662 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006663 lineArgs[0] = Long.toString(duration);
6664 lineArgs[1] = Integer.toString(level);
6665 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6666 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6667 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
6668 case Display.STATE_ON: lineArgs[2] = "s+"; break;
6669 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
6670 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07006671 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006672 }
6673 } else {
6674 lineArgs[2] = "";
6675 }
6676 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6677 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
6678 } else {
6679 lineArgs[3] = "";
6680 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006681 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07006682 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006683 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07006684 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006685 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006686 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
6687 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006688 pw.print(prefix);
6689 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006690 TimeUtils.formatDuration(duration, pw);
6691 pw.print(" to "); pw.print(level);
6692 boolean haveModes = false;
6693 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6694 pw.print(" (");
6695 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6696 case Display.STATE_OFF: pw.print("screen-off"); break;
6697 case Display.STATE_ON: pw.print("screen-on"); break;
6698 case Display.STATE_DOZE: pw.print("screen-doze"); break;
6699 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07006700 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006701 }
6702 haveModes = true;
6703 }
6704 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6705 pw.print(haveModes ? ", " : " (");
6706 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
6707 ? "power-save-on" : "power-save-off");
6708 haveModes = true;
6709 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006710 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6711 pw.print(haveModes ? ", " : " (");
6712 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6713 ? "device-idle-on" : "device-idle-off");
6714 haveModes = true;
6715 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006716 if (haveModes) {
6717 pw.print(")");
6718 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006719 pw.println();
6720 }
6721 }
6722 return true;
6723 }
6724
Kweku Adams87b19ec2017-10-09 12:40:03 -07006725 private static void dumpDurationSteps(ProtoOutputStream proto, long fieldId,
6726 LevelStepTracker steps) {
6727 if (steps == null) {
6728 return;
6729 }
6730 int count = steps.mNumStepDurations;
Kweku Adams87b19ec2017-10-09 12:40:03 -07006731 for (int i = 0; i < count; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07006732 long token = proto.start(fieldId);
Kweku Adams87b19ec2017-10-09 12:40:03 -07006733 proto.write(SystemProto.BatteryLevelStep.DURATION_MS, steps.getDurationAt(i));
6734 proto.write(SystemProto.BatteryLevelStep.LEVEL, steps.getLevelAt(i));
6735
6736 final long initMode = steps.getInitModeAt(i);
6737 final long modMode = steps.getModModeAt(i);
6738
6739 int ds = SystemProto.BatteryLevelStep.DS_MIXED;
6740 if ((modMode & STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6741 switch ((int) (initMode & STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6742 case Display.STATE_OFF:
6743 ds = SystemProto.BatteryLevelStep.DS_OFF;
6744 break;
6745 case Display.STATE_ON:
6746 ds = SystemProto.BatteryLevelStep.DS_ON;
6747 break;
6748 case Display.STATE_DOZE:
6749 ds = SystemProto.BatteryLevelStep.DS_DOZE;
6750 break;
6751 case Display.STATE_DOZE_SUSPEND:
6752 ds = SystemProto.BatteryLevelStep.DS_DOZE_SUSPEND;
6753 break;
6754 default:
6755 ds = SystemProto.BatteryLevelStep.DS_ERROR;
6756 break;
6757 }
6758 }
6759 proto.write(SystemProto.BatteryLevelStep.DISPLAY_STATE, ds);
6760
6761 int psm = SystemProto.BatteryLevelStep.PSM_MIXED;
6762 if ((modMode & STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6763 psm = (initMode & STEP_LEVEL_MODE_POWER_SAVE) != 0
6764 ? SystemProto.BatteryLevelStep.PSM_ON : SystemProto.BatteryLevelStep.PSM_OFF;
6765 }
6766 proto.write(SystemProto.BatteryLevelStep.POWER_SAVE_MODE, psm);
6767
6768 int im = SystemProto.BatteryLevelStep.IM_MIXED;
6769 if ((modMode & STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6770 im = (initMode & STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6771 ? SystemProto.BatteryLevelStep.IM_ON : SystemProto.BatteryLevelStep.IM_OFF;
6772 }
6773 proto.write(SystemProto.BatteryLevelStep.IDLE_MODE, im);
6774
6775 proto.end(token);
6776 }
6777 }
6778
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006779 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006780 public static final int DUMP_DAILY_ONLY = 1<<2;
6781 public static final int DUMP_HISTORY_ONLY = 1<<3;
6782 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
6783 public static final int DUMP_VERBOSE = 1<<5;
6784 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006785
Dianne Hackborn37de0982014-05-09 09:32:18 -07006786 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
6787 final HistoryPrinter hprinter = new HistoryPrinter();
6788 final HistoryItem rec = new HistoryItem();
6789 long lastTime = -1;
6790 long baseTime = -1;
6791 boolean printed = false;
6792 HistoryEventTracker tracker = null;
6793 while (getNextHistoryLocked(rec)) {
6794 lastTime = rec.time;
6795 if (baseTime < 0) {
6796 baseTime = lastTime;
6797 }
6798 if (rec.time >= histStart) {
6799 if (histStart >= 0 && !printed) {
6800 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07006801 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006802 || rec.cmd == HistoryItem.CMD_START
6803 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006804 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006805 hprinter.printNextItem(pw, rec, baseTime, checkin,
6806 (flags&DUMP_VERBOSE) != 0);
6807 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006808 } else if (rec.currentTime != 0) {
6809 printed = true;
6810 byte cmd = rec.cmd;
6811 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006812 hprinter.printNextItem(pw, rec, baseTime, checkin,
6813 (flags&DUMP_VERBOSE) != 0);
6814 rec.cmd = cmd;
6815 }
6816 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006817 if (rec.cmd != HistoryItem.CMD_UPDATE) {
6818 hprinter.printNextItem(pw, rec, baseTime, checkin,
6819 (flags&DUMP_VERBOSE) != 0);
6820 rec.cmd = HistoryItem.CMD_UPDATE;
6821 }
6822 int oldEventCode = rec.eventCode;
6823 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006824 rec.eventTag = new HistoryTag();
6825 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
6826 HashMap<String, SparseIntArray> active
6827 = tracker.getStateForEvent(i);
6828 if (active == null) {
6829 continue;
6830 }
6831 for (HashMap.Entry<String, SparseIntArray> ent
6832 : active.entrySet()) {
6833 SparseIntArray uids = ent.getValue();
6834 for (int j=0; j<uids.size(); j++) {
6835 rec.eventCode = i;
6836 rec.eventTag.string = ent.getKey();
6837 rec.eventTag.uid = uids.keyAt(j);
6838 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006839 hprinter.printNextItem(pw, rec, baseTime, checkin,
6840 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006841 rec.wakeReasonTag = null;
6842 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006843 }
6844 }
6845 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006846 rec.eventCode = oldEventCode;
6847 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006848 tracker = null;
6849 }
6850 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006851 hprinter.printNextItem(pw, rec, baseTime, checkin,
6852 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07006853 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
6854 // This is an attempt to aggregate the previous state and generate
6855 // fake events to reflect that state at the point where we start
6856 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07006857 if (tracker == null) {
6858 tracker = new HistoryEventTracker();
6859 }
6860 tracker.updateState(rec.eventCode, rec.eventTag.string,
6861 rec.eventTag.uid, rec.eventTag.poolIdx);
6862 }
6863 }
6864 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07006865 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006866 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
6867 }
6868 }
6869
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006870 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
6871 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
6872 if (steps == null) {
6873 return;
6874 }
6875 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
6876 if (timeRemaining >= 0) {
6877 pw.print(prefix); pw.print(label); pw.print(" total time: ");
6878 tmpSb.setLength(0);
6879 formatTimeMs(tmpSb, timeRemaining);
6880 pw.print(tmpSb);
6881 pw.print(" (from "); pw.print(tmpOutInt[0]);
6882 pw.println(" steps)");
6883 }
6884 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6885 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6886 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
6887 if (estimatedTime > 0) {
6888 pw.print(prefix); pw.print(label); pw.print(" ");
6889 pw.print(STEP_LEVEL_MODE_LABELS[i]);
6890 pw.print(" time: ");
6891 tmpSb.setLength(0);
6892 formatTimeMs(tmpSb, estimatedTime);
6893 pw.print(tmpSb);
6894 pw.print(" (from "); pw.print(tmpOutInt[0]);
6895 pw.println(" steps)");
6896 }
6897 }
6898 }
6899
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006900 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
6901 ArrayList<PackageChange> changes) {
6902 if (changes == null) {
6903 return;
6904 }
6905 pw.print(prefix); pw.println("Package changes:");
6906 for (int i=0; i<changes.size(); i++) {
6907 PackageChange pc = changes.get(i);
6908 if (pc.mUpdate) {
6909 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
6910 pw.print(" vers="); pw.println(pc.mVersionCode);
6911 } else {
6912 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
6913 }
6914 }
6915 }
6916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006917 /**
6918 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
6919 *
6920 * @param pw a Printer to receive the dump output.
6921 */
6922 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006923 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006924 prepareForDumpLocked();
6925
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006926 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006927 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006928
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006929 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006930 final long historyTotalSize = getHistoryTotalSize();
6931 final long historyUsedSize = getHistoryUsedSize();
6932 if (startIteratingHistoryLocked()) {
6933 try {
6934 pw.print("Battery History (");
6935 pw.print((100*historyUsedSize)/historyTotalSize);
6936 pw.print("% used, ");
6937 printSizeValue(pw, historyUsedSize);
6938 pw.print(" used of ");
6939 printSizeValue(pw, historyTotalSize);
6940 pw.print(", ");
6941 pw.print(getHistoryStringPoolSize());
6942 pw.print(" strings using ");
6943 printSizeValue(pw, getHistoryStringPoolBytes());
6944 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07006945 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006946 pw.println();
6947 } finally {
6948 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006949 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006950 }
6951
6952 if (startIteratingOldHistoryLocked()) {
6953 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006954 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006955 pw.println("Old battery History:");
6956 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006957 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006958 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006959 if (baseTime < 0) {
6960 baseTime = rec.time;
6961 }
6962 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006963 }
6964 pw.println();
6965 } finally {
6966 finishIteratingOldHistoryLocked();
6967 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006968 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006969 }
6970
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006971 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006972 return;
6973 }
6974
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006975 if (!filtering) {
6976 SparseArray<? extends Uid> uidStats = getUidStats();
6977 final int NU = uidStats.size();
6978 boolean didPid = false;
6979 long nowRealtime = SystemClock.elapsedRealtime();
6980 for (int i=0; i<NU; i++) {
6981 Uid uid = uidStats.valueAt(i);
6982 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
6983 if (pids != null) {
6984 for (int j=0; j<pids.size(); j++) {
6985 Uid.Pid pid = pids.valueAt(j);
6986 if (!didPid) {
6987 pw.println("Per-PID Stats:");
6988 didPid = true;
6989 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006990 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
6991 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006992 pw.print(" PID "); pw.print(pids.keyAt(j));
6993 pw.print(" wake time: ");
6994 TimeUtils.formatDuration(time, pw);
6995 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006996 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006997 }
6998 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006999 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07007000 pw.println();
7001 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007002 }
7003
7004 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007005 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
7006 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07007007 long timeRemaining = computeBatteryTimeRemaining(
7008 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07007009 if (timeRemaining >= 0) {
7010 pw.print(" Estimated discharge time remaining: ");
7011 TimeUtils.formatDuration(timeRemaining / 1000, pw);
7012 pw.println();
7013 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007014 final LevelStepTracker steps = getDischargeLevelStepTracker();
7015 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
7016 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
7017 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
7018 STEP_LEVEL_MODE_VALUES[i], null));
7019 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07007020 pw.println();
7021 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007022 if (dumpDurationSteps(pw, " ", "Charge step durations:",
7023 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07007024 long timeRemaining = computeChargeTimeRemaining(
7025 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07007026 if (timeRemaining >= 0) {
7027 pw.print(" Estimated charge time remaining: ");
7028 TimeUtils.formatDuration(timeRemaining / 1000, pw);
7029 pw.println();
7030 }
7031 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08007032 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007033 }
Dianne Hackbornc81983a2017-10-20 16:16:32 -07007034 if (!filtering || (flags & DUMP_DAILY_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007035 pw.println("Daily stats:");
7036 pw.print(" Current start time: ");
7037 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
7038 getCurrentDailyStartTime()).toString());
7039 pw.print(" Next min deadline: ");
7040 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
7041 getNextMinDailyDeadline()).toString());
7042 pw.print(" Next max deadline: ");
7043 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
7044 getNextMaxDailyDeadline()).toString());
7045 StringBuilder sb = new StringBuilder(64);
7046 int[] outInt = new int[1];
7047 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
7048 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007049 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
7050 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007051 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007052 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
7053 dsteps, false)) {
7054 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
7055 sb, outInt);
7056 }
7057 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
7058 csteps, false)) {
7059 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
7060 sb, outInt);
7061 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007062 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007063 } else {
7064 pw.println(" Current daily steps:");
7065 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
7066 sb, outInt);
7067 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
7068 sb, outInt);
7069 }
7070 }
7071 DailyItem dit;
7072 int curIndex = 0;
7073 while ((dit=getDailyItemLocked(curIndex)) != null) {
7074 curIndex++;
7075 if ((flags&DUMP_DAILY_ONLY) != 0) {
7076 pw.println();
7077 }
7078 pw.print(" Daily from ");
7079 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
7080 pw.print(" to ");
7081 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
7082 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007083 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007084 if (dumpDurationSteps(pw, " ",
7085 " Discharge step durations:", dit.mDischargeSteps, false)) {
7086 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
7087 sb, outInt);
7088 }
7089 if (dumpDurationSteps(pw, " ",
7090 " Charge step durations:", dit.mChargeSteps, false)) {
7091 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
7092 sb, outInt);
7093 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007094 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007095 } else {
7096 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
7097 sb, outInt);
7098 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
7099 sb, outInt);
7100 }
7101 }
7102 pw.println();
7103 }
7104 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07007105 pw.println("Statistics since last charge:");
7106 pw.println(" System starts: " + getStartCount()
7107 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07007108 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
7109 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07007110 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07007111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007112 }
Mike Mac2f518a2017-09-19 16:06:03 -07007113
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007114 // This is called from BatteryStatsService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007115 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08007116 public void dumpCheckinLocked(Context context, PrintWriter pw,
7117 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007118 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007119
7120 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07007121 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
7122 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007123
Dianne Hackborn13ac0412013-06-25 19:34:49 -07007124 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
7125
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007126 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07007127 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007128 try {
7129 for (int i=0; i<getHistoryStringPoolSize(); i++) {
7130 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
7131 pw.print(HISTORY_STRING_POOL); pw.print(',');
7132 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007133 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007134 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007135 pw.print(",\"");
7136 String str = getHistoryTagPoolString(i);
7137 str = str.replace("\\", "\\\\");
7138 str = str.replace("\"", "\\\"");
7139 pw.print(str);
7140 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007141 pw.println();
7142 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07007143 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007144 } finally {
7145 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08007146 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07007147 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07007148 }
7149
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007150 if ((flags & DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007151 return;
7152 }
7153
Dianne Hackborne4a59512010-12-07 11:08:07 -08007154 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007155 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08007156 for (int i=0; i<apps.size(); i++) {
7157 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007158 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
7159 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08007160 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007161 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
7162 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007163 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007164 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007165 }
7166 SparseArray<? extends Uid> uidStats = getUidStats();
7167 final int NU = uidStats.size();
7168 String[] lineArgs = new String[2];
7169 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007170 int uid = UserHandle.getAppId(uidStats.keyAt(i));
7171 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
7172 if (pkgs != null && !pkgs.second.value) {
7173 pkgs.second.value = true;
7174 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08007175 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007176 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007177 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
7178 (Object[])lineArgs);
7179 }
7180 }
7181 }
7182 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007183 if ((flags & DUMP_DAILY_ONLY) == 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007184 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007185 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07007186 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007187 if (timeRemaining >= 0) {
7188 lineArgs[0] = Long.toString(timeRemaining);
7189 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
7190 (Object[])lineArgs);
7191 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007192 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07007193 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007194 if (timeRemaining >= 0) {
7195 lineArgs[0] = Long.toString(timeRemaining);
7196 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
7197 (Object[])lineArgs);
7198 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007199 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
7200 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08007201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007202 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007203
Kweku Adams71a95312018-04-16 16:54:24 -07007204 /**
7205 * Dump #STATS_SINCE_CHARGED batterystats data to a proto. If the flags include
7206 * DUMP_INCLUDE_HISTORY or DUMP_HISTORY_ONLY, only the history will be dumped.
7207 * @hide
7208 */
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007209 public void dumpProtoLocked(Context context, FileDescriptor fd, List<ApplicationInfo> apps,
Kweku Adams71a95312018-04-16 16:54:24 -07007210 int flags, long histStart) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007211 final ProtoOutputStream proto = new ProtoOutputStream(fd);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007212 prepareForDumpLocked();
7213
Kweku Adams71a95312018-04-16 16:54:24 -07007214 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
7215 dumpProtoHistoryLocked(proto, flags, histStart);
7216 proto.flush();
7217 return;
7218 }
7219
7220 final long bToken = proto.start(BatteryStatsServiceDumpProto.BATTERYSTATS);
7221
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007222 proto.write(BatteryStatsProto.REPORT_VERSION, CHECKIN_VERSION);
7223 proto.write(BatteryStatsProto.PARCEL_VERSION, getParcelVersion());
7224 proto.write(BatteryStatsProto.START_PLATFORM_VERSION, getStartPlatformVersion());
7225 proto.write(BatteryStatsProto.END_PLATFORM_VERSION, getEndPlatformVersion());
7226
Kweku Adams71a95312018-04-16 16:54:24 -07007227 if ((flags & DUMP_DAILY_ONLY) == 0) {
Kweku Adams103351f2017-10-16 14:39:34 -07007228 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false,
7229 (flags & DUMP_DEVICE_WIFI_ONLY) != 0);
7230 helper.create(this);
7231 helper.refreshStats(STATS_SINCE_CHARGED, UserHandle.USER_ALL);
7232
7233 dumpProtoAppsLocked(proto, helper, apps);
7234 dumpProtoSystemLocked(proto, helper);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007235 }
7236
7237 proto.end(bToken);
7238 proto.flush();
7239 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07007240
Kweku Adams103351f2017-10-16 14:39:34 -07007241 private void dumpProtoAppsLocked(ProtoOutputStream proto, BatteryStatsHelper helper,
7242 List<ApplicationInfo> apps) {
7243 final int which = STATS_SINCE_CHARGED;
7244 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7245 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7246 final long rawRealtimeUs = rawRealtimeMs * 1000;
7247 final long batteryUptimeUs = getBatteryUptime(rawUptimeUs);
7248
7249 SparseArray<ArrayList<String>> aidToPackages = new SparseArray<>();
7250 if (apps != null) {
7251 for (int i = 0; i < apps.size(); ++i) {
7252 ApplicationInfo ai = apps.get(i);
7253 int aid = UserHandle.getAppId(ai.uid);
7254 ArrayList<String> pkgs = aidToPackages.get(aid);
7255 if (pkgs == null) {
7256 pkgs = new ArrayList<String>();
7257 aidToPackages.put(aid, pkgs);
7258 }
7259 pkgs.add(ai.packageName);
7260 }
7261 }
7262
7263 SparseArray<BatterySipper> uidToSipper = new SparseArray<>();
7264 final List<BatterySipper> sippers = helper.getUsageList();
7265 if (sippers != null) {
7266 for (int i = 0; i < sippers.size(); ++i) {
7267 final BatterySipper bs = sippers.get(i);
7268 if (bs.drainType != BatterySipper.DrainType.APP) {
7269 // Others are handled by dumpProtoSystemLocked()
7270 continue;
7271 }
7272 uidToSipper.put(bs.uidObj.getUid(), bs);
7273 }
7274 }
7275
7276 SparseArray<? extends Uid> uidStats = getUidStats();
7277 final int n = uidStats.size();
7278 for (int iu = 0; iu < n; ++iu) {
7279 final long uTkn = proto.start(BatteryStatsProto.UIDS);
7280 final Uid u = uidStats.valueAt(iu);
7281
7282 final int uid = uidStats.keyAt(iu);
7283 proto.write(UidProto.UID, uid);
7284
7285 // Print packages and apk stats (UID_DATA & APK_DATA)
7286 ArrayList<String> pkgs = aidToPackages.get(UserHandle.getAppId(uid));
7287 if (pkgs == null) {
7288 pkgs = new ArrayList<String>();
7289 }
7290 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats =
7291 u.getPackageStats();
7292 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7293 String pkg = packageStats.keyAt(ipkg);
7294 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats =
7295 packageStats.valueAt(ipkg).getServiceStats();
7296 if (serviceStats.size() == 0) {
7297 // Due to the way ActivityManagerService logs wakeup alarms, some packages (for
7298 // example, "android") may be included in the packageStats that aren't part of
7299 // the UID. If they don't have any services, then they shouldn't be listed here.
7300 // These packages won't be a part in the pkgs List.
7301 continue;
7302 }
7303
7304 final long pToken = proto.start(UidProto.PACKAGES);
7305 proto.write(UidProto.Package.NAME, pkg);
7306 // Remove from the packages list since we're logging it here.
7307 pkgs.remove(pkg);
7308
7309 for (int isvc = serviceStats.size() - 1; isvc >= 0; --isvc) {
7310 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
Kweku Adams14f3d222018-03-22 14:12:55 -07007311
7312 final long startTimeMs = roundUsToMs(ss.getStartTime(batteryUptimeUs, which));
7313 final int starts = ss.getStarts(which);
7314 final int launches = ss.getLaunches(which);
7315 if (startTimeMs == 0 && starts == 0 && launches == 0) {
7316 continue;
7317 }
7318
Kweku Adams103351f2017-10-16 14:39:34 -07007319 long sToken = proto.start(UidProto.Package.SERVICES);
7320
7321 proto.write(UidProto.Package.Service.NAME, serviceStats.keyAt(isvc));
Kweku Adams14f3d222018-03-22 14:12:55 -07007322 proto.write(UidProto.Package.Service.START_DURATION_MS, startTimeMs);
7323 proto.write(UidProto.Package.Service.START_COUNT, starts);
7324 proto.write(UidProto.Package.Service.LAUNCH_COUNT, launches);
Kweku Adams103351f2017-10-16 14:39:34 -07007325
7326 proto.end(sToken);
7327 }
7328 proto.end(pToken);
7329 }
7330 // Print any remaining packages that weren't in the packageStats map. pkgs is pulled
7331 // from PackageManager data. Packages are only included in packageStats if there was
7332 // specific data tracked for them (services and wakeup alarms, etc.).
7333 for (String p : pkgs) {
7334 final long pToken = proto.start(UidProto.PACKAGES);
7335 proto.write(UidProto.Package.NAME, p);
7336 proto.end(pToken);
7337 }
7338
7339 // Total wakelock data (AGGREGATED_WAKELOCK_DATA)
7340 if (u.getAggregatedPartialWakelockTimer() != null) {
7341 final Timer timer = u.getAggregatedPartialWakelockTimer();
7342 // Times are since reset (regardless of 'which')
7343 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
7344 final Timer bgTimer = timer.getSubTimer();
7345 final long bgTimeMs = bgTimer != null
7346 ? bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
7347 final long awToken = proto.start(UidProto.AGGREGATED_WAKELOCK);
7348 proto.write(UidProto.AggregatedWakelock.PARTIAL_DURATION_MS, totTimeMs);
7349 proto.write(UidProto.AggregatedWakelock.BACKGROUND_PARTIAL_DURATION_MS, bgTimeMs);
7350 proto.end(awToken);
7351 }
7352
7353 // Audio (AUDIO_DATA)
7354 dumpTimer(proto, UidProto.AUDIO, u.getAudioTurnedOnTimer(), rawRealtimeUs, which);
7355
7356 // Bluetooth Controller (BLUETOOTH_CONTROLLER_DATA)
7357 dumpControllerActivityProto(proto, UidProto.BLUETOOTH_CONTROLLER,
7358 u.getBluetoothControllerActivity(), which);
7359
7360 // BLE scans (BLUETOOTH_MISC_DATA) (uses totalDurationMsLocked and MaxDurationMsLocked)
7361 final Timer bleTimer = u.getBluetoothScanTimer();
7362 if (bleTimer != null) {
7363 final long bmToken = proto.start(UidProto.BLUETOOTH_MISC);
7364
7365 dumpTimer(proto, UidProto.BluetoothMisc.APPORTIONED_BLE_SCAN, bleTimer,
7366 rawRealtimeUs, which);
7367 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN,
7368 u.getBluetoothScanBackgroundTimer(), rawRealtimeUs, which);
7369 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
7370 dumpTimer(proto, UidProto.BluetoothMisc.UNOPTIMIZED_BLE_SCAN,
7371 u.getBluetoothUnoptimizedScanTimer(), rawRealtimeUs, which);
7372 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
7373 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_UNOPTIMIZED_BLE_SCAN,
7374 u.getBluetoothUnoptimizedScanBackgroundTimer(), rawRealtimeUs, which);
7375 // Result counters
7376 proto.write(UidProto.BluetoothMisc.BLE_SCAN_RESULT_COUNT,
7377 u.getBluetoothScanResultCounter() != null
7378 ? u.getBluetoothScanResultCounter().getCountLocked(which) : 0);
7379 proto.write(UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN_RESULT_COUNT,
7380 u.getBluetoothScanResultBgCounter() != null
7381 ? u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0);
7382
7383 proto.end(bmToken);
7384 }
7385
7386 // Camera (CAMERA_DATA)
7387 dumpTimer(proto, UidProto.CAMERA, u.getCameraTurnedOnTimer(), rawRealtimeUs, which);
7388
7389 // CPU stats (CPU_DATA & CPU_TIMES_AT_FREQ_DATA)
7390 final long cpuToken = proto.start(UidProto.CPU);
7391 proto.write(UidProto.Cpu.USER_DURATION_MS, roundUsToMs(u.getUserCpuTimeUs(which)));
7392 proto.write(UidProto.Cpu.SYSTEM_DURATION_MS, roundUsToMs(u.getSystemCpuTimeUs(which)));
7393
7394 final long[] cpuFreqs = getCpuFreqs();
7395 if (cpuFreqs != null) {
7396 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
7397 // If total cpuFreqTimes is null, then we don't need to check for
7398 // screenOffCpuFreqTimes.
7399 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
7400 long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
7401 if (screenOffCpuFreqTimeMs == null) {
7402 screenOffCpuFreqTimeMs = new long[cpuFreqTimeMs.length];
7403 }
7404 for (int ic = 0; ic < cpuFreqTimeMs.length; ++ic) {
7405 long cToken = proto.start(UidProto.Cpu.BY_FREQUENCY);
7406 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7407 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7408 cpuFreqTimeMs[ic]);
7409 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7410 screenOffCpuFreqTimeMs[ic]);
7411 proto.end(cToken);
7412 }
7413 }
7414 }
Sudheer Shanka6d658d72018-01-01 01:36:49 -08007415
7416 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
7417 final long[] timesMs = u.getCpuFreqTimes(which, procState);
7418 if (timesMs != null && timesMs.length == cpuFreqs.length) {
7419 long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(which, procState);
7420 if (screenOffTimesMs == null) {
7421 screenOffTimesMs = new long[timesMs.length];
7422 }
7423 final long procToken = proto.start(UidProto.Cpu.BY_PROCESS_STATE);
7424 proto.write(UidProto.Cpu.ByProcessState.PROCESS_STATE, procState);
7425 for (int ic = 0; ic < timesMs.length; ++ic) {
7426 long cToken = proto.start(UidProto.Cpu.ByProcessState.BY_FREQUENCY);
7427 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7428 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7429 timesMs[ic]);
7430 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7431 screenOffTimesMs[ic]);
7432 proto.end(cToken);
7433 }
7434 proto.end(procToken);
7435 }
7436 }
Kweku Adams103351f2017-10-16 14:39:34 -07007437 proto.end(cpuToken);
7438
7439 // Flashlight (FLASHLIGHT_DATA)
7440 dumpTimer(proto, UidProto.FLASHLIGHT, u.getFlashlightTurnedOnTimer(),
7441 rawRealtimeUs, which);
7442
7443 // Foreground activity (FOREGROUND_ACTIVITY_DATA)
7444 dumpTimer(proto, UidProto.FOREGROUND_ACTIVITY, u.getForegroundActivityTimer(),
7445 rawRealtimeUs, which);
7446
7447 // Foreground service (FOREGROUND_SERVICE_DATA)
7448 dumpTimer(proto, UidProto.FOREGROUND_SERVICE, u.getForegroundServiceTimer(),
7449 rawRealtimeUs, which);
7450
7451 // Job completion (JOB_COMPLETION_DATA)
7452 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
7453 final int[] reasons = new int[]{
7454 JobParameters.REASON_CANCELED,
7455 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED,
7456 JobParameters.REASON_PREEMPT,
7457 JobParameters.REASON_TIMEOUT,
7458 JobParameters.REASON_DEVICE_IDLE,
7459 };
7460 for (int ic = 0; ic < completions.size(); ++ic) {
7461 SparseIntArray types = completions.valueAt(ic);
7462 if (types != null) {
7463 final long jcToken = proto.start(UidProto.JOB_COMPLETION);
7464
7465 proto.write(UidProto.JobCompletion.NAME, completions.keyAt(ic));
7466
7467 for (int r : reasons) {
7468 long rToken = proto.start(UidProto.JobCompletion.REASON_COUNT);
7469 proto.write(UidProto.JobCompletion.ReasonCount.NAME, r);
7470 proto.write(UidProto.JobCompletion.ReasonCount.COUNT, types.get(r, 0));
7471 proto.end(rToken);
7472 }
7473
7474 proto.end(jcToken);
7475 }
7476 }
7477
7478 // Scheduled jobs (JOB_DATA)
7479 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
7480 for (int ij = jobs.size() - 1; ij >= 0; --ij) {
7481 final Timer timer = jobs.valueAt(ij);
7482 final Timer bgTimer = timer.getSubTimer();
7483 final long jToken = proto.start(UidProto.JOBS);
7484
7485 proto.write(UidProto.Job.NAME, jobs.keyAt(ij));
7486 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7487 dumpTimer(proto, UidProto.Job.TOTAL, timer, rawRealtimeUs, which);
7488 dumpTimer(proto, UidProto.Job.BACKGROUND, bgTimer, rawRealtimeUs, which);
7489
7490 proto.end(jToken);
7491 }
7492
7493 // Modem Controller (MODEM_CONTROLLER_DATA)
7494 dumpControllerActivityProto(proto, UidProto.MODEM_CONTROLLER,
7495 u.getModemControllerActivity(), which);
7496
7497 // Network stats (NETWORK_DATA)
7498 final long nToken = proto.start(UidProto.NETWORK);
7499 proto.write(UidProto.Network.MOBILE_BYTES_RX,
7500 u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7501 proto.write(UidProto.Network.MOBILE_BYTES_TX,
7502 u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7503 proto.write(UidProto.Network.WIFI_BYTES_RX,
7504 u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7505 proto.write(UidProto.Network.WIFI_BYTES_TX,
7506 u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7507 proto.write(UidProto.Network.BT_BYTES_RX,
7508 u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7509 proto.write(UidProto.Network.BT_BYTES_TX,
7510 u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
7511 proto.write(UidProto.Network.MOBILE_PACKETS_RX,
7512 u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7513 proto.write(UidProto.Network.MOBILE_PACKETS_TX,
7514 u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7515 proto.write(UidProto.Network.WIFI_PACKETS_RX,
7516 u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7517 proto.write(UidProto.Network.WIFI_PACKETS_TX,
7518 u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7519 proto.write(UidProto.Network.MOBILE_ACTIVE_DURATION_MS,
7520 roundUsToMs(u.getMobileRadioActiveTime(which)));
7521 proto.write(UidProto.Network.MOBILE_ACTIVE_COUNT,
7522 u.getMobileRadioActiveCount(which));
7523 proto.write(UidProto.Network.MOBILE_WAKEUP_COUNT,
7524 u.getMobileRadioApWakeupCount(which));
7525 proto.write(UidProto.Network.WIFI_WAKEUP_COUNT,
7526 u.getWifiRadioApWakeupCount(which));
7527 proto.write(UidProto.Network.MOBILE_BYTES_BG_RX,
7528 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA, which));
7529 proto.write(UidProto.Network.MOBILE_BYTES_BG_TX,
7530 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA, which));
7531 proto.write(UidProto.Network.WIFI_BYTES_BG_RX,
7532 u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which));
7533 proto.write(UidProto.Network.WIFI_BYTES_BG_TX,
7534 u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which));
7535 proto.write(UidProto.Network.MOBILE_PACKETS_BG_RX,
7536 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA, which));
7537 proto.write(UidProto.Network.MOBILE_PACKETS_BG_TX,
7538 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA, which));
7539 proto.write(UidProto.Network.WIFI_PACKETS_BG_RX,
7540 u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA, which));
7541 proto.write(UidProto.Network.WIFI_PACKETS_BG_TX,
7542 u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA, which));
7543 proto.end(nToken);
7544
7545 // Power use item (POWER_USE_ITEM_DATA)
7546 BatterySipper bs = uidToSipper.get(uid);
7547 if (bs != null) {
7548 final long bsToken = proto.start(UidProto.POWER_USE_ITEM);
7549 proto.write(UidProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7550 proto.write(UidProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7551 proto.write(UidProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7552 proto.write(UidProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7553 bs.proportionalSmearMah);
7554 proto.end(bsToken);
7555 }
7556
7557 // Processes (PROCESS_DATA)
7558 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats =
7559 u.getProcessStats();
7560 for (int ipr = processStats.size() - 1; ipr >= 0; --ipr) {
7561 final Uid.Proc ps = processStats.valueAt(ipr);
7562 final long prToken = proto.start(UidProto.PROCESS);
7563
7564 proto.write(UidProto.Process.NAME, processStats.keyAt(ipr));
7565 proto.write(UidProto.Process.USER_DURATION_MS, ps.getUserTime(which));
7566 proto.write(UidProto.Process.SYSTEM_DURATION_MS, ps.getSystemTime(which));
7567 proto.write(UidProto.Process.FOREGROUND_DURATION_MS, ps.getForegroundTime(which));
7568 proto.write(UidProto.Process.START_COUNT, ps.getStarts(which));
7569 proto.write(UidProto.Process.ANR_COUNT, ps.getNumAnrs(which));
7570 proto.write(UidProto.Process.CRASH_COUNT, ps.getNumCrashes(which));
7571
7572 proto.end(prToken);
7573 }
7574
7575 // Sensors (SENSOR_DATA)
7576 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
7577 for (int ise = 0; ise < sensors.size(); ++ise) {
7578 final Uid.Sensor se = sensors.valueAt(ise);
7579 final Timer timer = se.getSensorTime();
7580 if (timer == null) {
7581 continue;
7582 }
7583 final Timer bgTimer = se.getSensorBackgroundTime();
7584 final int sensorNumber = sensors.keyAt(ise);
7585 final long seToken = proto.start(UidProto.SENSORS);
7586
7587 proto.write(UidProto.Sensor.ID, sensorNumber);
7588 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7589 dumpTimer(proto, UidProto.Sensor.APPORTIONED, timer, rawRealtimeUs, which);
7590 dumpTimer(proto, UidProto.Sensor.BACKGROUND, bgTimer, rawRealtimeUs, which);
7591
7592 proto.end(seToken);
7593 }
7594
7595 // State times (STATE_TIME_DATA)
7596 for (int ips = 0; ips < Uid.NUM_PROCESS_STATE; ++ips) {
7597 long durMs = roundUsToMs(u.getProcessStateTime(ips, rawRealtimeUs, which));
7598 if (durMs == 0) {
7599 continue;
7600 }
7601 final long stToken = proto.start(UidProto.STATES);
7602 proto.write(UidProto.StateTime.STATE, ips);
7603 proto.write(UidProto.StateTime.DURATION_MS, durMs);
7604 proto.end(stToken);
7605 }
7606
7607 // Syncs (SYNC_DATA)
7608 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
7609 for (int isy = syncs.size() - 1; isy >= 0; --isy) {
7610 final Timer timer = syncs.valueAt(isy);
7611 final Timer bgTimer = timer.getSubTimer();
7612 final long syToken = proto.start(UidProto.SYNCS);
7613
7614 proto.write(UidProto.Sync.NAME, syncs.keyAt(isy));
7615 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7616 dumpTimer(proto, UidProto.Sync.TOTAL, timer, rawRealtimeUs, which);
7617 dumpTimer(proto, UidProto.Sync.BACKGROUND, bgTimer, rawRealtimeUs, which);
7618
7619 proto.end(syToken);
7620 }
7621
7622 // User activity (USER_ACTIVITY_DATA)
7623 if (u.hasUserActivity()) {
7624 for (int i = 0; i < Uid.NUM_USER_ACTIVITY_TYPES; ++i) {
7625 int val = u.getUserActivityCount(i, which);
7626 if (val != 0) {
7627 final long uaToken = proto.start(UidProto.USER_ACTIVITY);
7628 proto.write(UidProto.UserActivity.NAME, i);
7629 proto.write(UidProto.UserActivity.COUNT, val);
7630 proto.end(uaToken);
7631 }
7632 }
7633 }
7634
7635 // Vibrator (VIBRATOR_DATA)
7636 dumpTimer(proto, UidProto.VIBRATOR, u.getVibratorOnTimer(), rawRealtimeUs, which);
7637
7638 // Video (VIDEO_DATA)
7639 dumpTimer(proto, UidProto.VIDEO, u.getVideoTurnedOnTimer(), rawRealtimeUs, which);
7640
7641 // Wakelocks (WAKELOCK_DATA)
7642 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
7643 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7644 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7645 final long wToken = proto.start(UidProto.WAKELOCKS);
7646 proto.write(UidProto.Wakelock.NAME, wakelocks.keyAt(iw));
7647 dumpTimer(proto, UidProto.Wakelock.FULL, wl.getWakeTime(WAKE_TYPE_FULL),
7648 rawRealtimeUs, which);
7649 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7650 if (pTimer != null) {
7651 dumpTimer(proto, UidProto.Wakelock.PARTIAL, pTimer, rawRealtimeUs, which);
7652 dumpTimer(proto, UidProto.Wakelock.BACKGROUND_PARTIAL, pTimer.getSubTimer(),
7653 rawRealtimeUs, which);
7654 }
7655 dumpTimer(proto, UidProto.Wakelock.WINDOW, wl.getWakeTime(WAKE_TYPE_WINDOW),
7656 rawRealtimeUs, which);
7657 proto.end(wToken);
7658 }
7659
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007660 // Wifi Multicast Wakelock (WIFI_MULTICAST_WAKELOCK_DATA)
7661 dumpTimer(proto, UidProto.WIFI_MULTICAST_WAKELOCK, u.getMulticastWakelockStats(),
7662 rawRealtimeUs, which);
7663
Kweku Adams103351f2017-10-16 14:39:34 -07007664 // Wakeup alarms (WAKEUP_ALARM_DATA)
7665 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7666 final Uid.Pkg ps = packageStats.valueAt(ipkg);
7667 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
7668 for (int iwa = alarms.size() - 1; iwa >= 0; --iwa) {
7669 final long waToken = proto.start(UidProto.WAKEUP_ALARM);
7670 proto.write(UidProto.WakeupAlarm.NAME, alarms.keyAt(iwa));
7671 proto.write(UidProto.WakeupAlarm.COUNT,
7672 alarms.valueAt(iwa).getCountLocked(which));
7673 proto.end(waToken);
7674 }
7675 }
7676
7677 // Wifi Controller (WIFI_CONTROLLER_DATA)
7678 dumpControllerActivityProto(proto, UidProto.WIFI_CONTROLLER,
7679 u.getWifiControllerActivity(), which);
7680
7681 // Wifi data (WIFI_DATA)
7682 final long wToken = proto.start(UidProto.WIFI);
7683 proto.write(UidProto.Wifi.FULL_WIFI_LOCK_DURATION_MS,
7684 roundUsToMs(u.getFullWifiLockTime(rawRealtimeUs, which)));
7685 dumpTimer(proto, UidProto.Wifi.APPORTIONED_SCAN, u.getWifiScanTimer(),
7686 rawRealtimeUs, which);
7687 proto.write(UidProto.Wifi.RUNNING_DURATION_MS,
7688 roundUsToMs(u.getWifiRunningTime(rawRealtimeUs, which)));
7689 dumpTimer(proto, UidProto.Wifi.BACKGROUND_SCAN, u.getWifiScanBackgroundTimer(),
7690 rawRealtimeUs, which);
7691 proto.end(wToken);
7692
7693 proto.end(uTkn);
7694 }
7695 }
7696
Kweku Adams71a95312018-04-16 16:54:24 -07007697 private void dumpProtoHistoryLocked(ProtoOutputStream proto, int flags, long histStart) {
7698 if (!startIteratingHistoryLocked()) {
7699 return;
7700 }
7701
7702 proto.write(BatteryStatsServiceDumpHistoryProto.REPORT_VERSION, CHECKIN_VERSION);
7703 proto.write(BatteryStatsServiceDumpHistoryProto.PARCEL_VERSION, getParcelVersion());
7704 proto.write(BatteryStatsServiceDumpHistoryProto.START_PLATFORM_VERSION,
7705 getStartPlatformVersion());
7706 proto.write(BatteryStatsServiceDumpHistoryProto.END_PLATFORM_VERSION,
7707 getEndPlatformVersion());
7708 try {
7709 long token;
7710 // History string pool (HISTORY_STRING_POOL)
7711 for (int i = 0; i < getHistoryStringPoolSize(); ++i) {
7712 token = proto.start(BatteryStatsServiceDumpHistoryProto.KEYS);
7713 proto.write(BatteryStatsServiceDumpHistoryProto.Key.INDEX, i);
7714 proto.write(BatteryStatsServiceDumpHistoryProto.Key.UID, getHistoryTagPoolUid(i));
7715 proto.write(BatteryStatsServiceDumpHistoryProto.Key.TAG,
7716 getHistoryTagPoolString(i));
7717 proto.end(token);
7718 }
7719
7720 // History data (HISTORY_DATA)
7721 final HistoryPrinter hprinter = new HistoryPrinter();
7722 final HistoryItem rec = new HistoryItem();
7723 long lastTime = -1;
7724 long baseTime = -1;
7725 boolean printed = false;
7726 HistoryEventTracker tracker = null;
7727 while (getNextHistoryLocked(rec)) {
7728 lastTime = rec.time;
7729 if (baseTime < 0) {
7730 baseTime = lastTime;
7731 }
7732 if (rec.time >= histStart) {
7733 if (histStart >= 0 && !printed) {
7734 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
7735 || rec.cmd == HistoryItem.CMD_RESET
7736 || rec.cmd == HistoryItem.CMD_START
7737 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
7738 printed = true;
7739 hprinter.printNextItem(proto, rec, baseTime,
7740 (flags & DUMP_VERBOSE) != 0);
7741 rec.cmd = HistoryItem.CMD_UPDATE;
7742 } else if (rec.currentTime != 0) {
7743 printed = true;
7744 byte cmd = rec.cmd;
7745 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
7746 hprinter.printNextItem(proto, rec, baseTime,
7747 (flags & DUMP_VERBOSE) != 0);
7748 rec.cmd = cmd;
7749 }
7750 if (tracker != null) {
7751 if (rec.cmd != HistoryItem.CMD_UPDATE) {
7752 hprinter.printNextItem(proto, rec, baseTime,
7753 (flags & DUMP_VERBOSE) != 0);
7754 rec.cmd = HistoryItem.CMD_UPDATE;
7755 }
7756 int oldEventCode = rec.eventCode;
7757 HistoryTag oldEventTag = rec.eventTag;
7758 rec.eventTag = new HistoryTag();
7759 for (int i = 0; i < HistoryItem.EVENT_COUNT; i++) {
7760 HashMap<String, SparseIntArray> active =
7761 tracker.getStateForEvent(i);
7762 if (active == null) {
7763 continue;
7764 }
7765 for (HashMap.Entry<String, SparseIntArray> ent
7766 : active.entrySet()) {
7767 SparseIntArray uids = ent.getValue();
7768 for (int j = 0; j < uids.size(); j++) {
7769 rec.eventCode = i;
7770 rec.eventTag.string = ent.getKey();
7771 rec.eventTag.uid = uids.keyAt(j);
7772 rec.eventTag.poolIdx = uids.valueAt(j);
7773 hprinter.printNextItem(proto, rec, baseTime,
7774 (flags & DUMP_VERBOSE) != 0);
7775 rec.wakeReasonTag = null;
7776 rec.wakelockTag = null;
7777 }
7778 }
7779 }
7780 rec.eventCode = oldEventCode;
7781 rec.eventTag = oldEventTag;
7782 tracker = null;
7783 }
7784 }
7785 hprinter.printNextItem(proto, rec, baseTime,
7786 (flags & DUMP_VERBOSE) != 0);
7787 }
7788 }
7789 if (histStart >= 0) {
7790 commitCurrentHistoryBatchLocked();
7791 proto.write(BatteryStatsServiceDumpHistoryProto.CSV_LINES,
7792 "NEXT: " + (lastTime + 1));
7793 }
7794 } finally {
7795 finishIteratingHistoryLocked();
7796 }
7797 }
7798
Kweku Adams103351f2017-10-16 14:39:34 -07007799 private void dumpProtoSystemLocked(ProtoOutputStream proto, BatteryStatsHelper helper) {
Kweku Adams87b19ec2017-10-09 12:40:03 -07007800 final long sToken = proto.start(BatteryStatsProto.SYSTEM);
7801 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7802 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7803 final long rawRealtimeUs = rawRealtimeMs * 1000;
7804 final int which = STATS_SINCE_CHARGED;
7805
7806 // Battery data (BATTERY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007807 final long bToken = proto.start(SystemProto.BATTERY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007808 proto.write(SystemProto.Battery.START_CLOCK_TIME_MS, getStartClockTime());
7809 proto.write(SystemProto.Battery.START_COUNT, getStartCount());
7810 proto.write(SystemProto.Battery.TOTAL_REALTIME_MS,
7811 computeRealtime(rawRealtimeUs, which) / 1000);
7812 proto.write(SystemProto.Battery.TOTAL_UPTIME_MS,
7813 computeUptime(rawUptimeUs, which) / 1000);
7814 proto.write(SystemProto.Battery.BATTERY_REALTIME_MS,
7815 computeBatteryRealtime(rawRealtimeUs, which) / 1000);
7816 proto.write(SystemProto.Battery.BATTERY_UPTIME_MS,
7817 computeBatteryUptime(rawUptimeUs, which) / 1000);
7818 proto.write(SystemProto.Battery.SCREEN_OFF_REALTIME_MS,
7819 computeBatteryScreenOffRealtime(rawRealtimeUs, which) / 1000);
7820 proto.write(SystemProto.Battery.SCREEN_OFF_UPTIME_MS,
7821 computeBatteryScreenOffUptime(rawUptimeUs, which) / 1000);
7822 proto.write(SystemProto.Battery.SCREEN_DOZE_DURATION_MS,
7823 getScreenDozeTime(rawRealtimeUs, which) / 1000);
7824 proto.write(SystemProto.Battery.ESTIMATED_BATTERY_CAPACITY_MAH,
7825 getEstimatedBatteryCapacity());
7826 proto.write(SystemProto.Battery.MIN_LEARNED_BATTERY_CAPACITY_UAH,
7827 getMinLearnedBatteryCapacity());
7828 proto.write(SystemProto.Battery.MAX_LEARNED_BATTERY_CAPACITY_UAH,
7829 getMaxLearnedBatteryCapacity());
Kweku Adams103351f2017-10-16 14:39:34 -07007830 proto.end(bToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007831
7832 // Battery discharge (BATTERY_DISCHARGE_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007833 final long bdToken = proto.start(SystemProto.BATTERY_DISCHARGE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007834 proto.write(SystemProto.BatteryDischarge.LOWER_BOUND_SINCE_CHARGE,
7835 getLowDischargeAmountSinceCharge());
7836 proto.write(SystemProto.BatteryDischarge.UPPER_BOUND_SINCE_CHARGE,
7837 getHighDischargeAmountSinceCharge());
7838 proto.write(SystemProto.BatteryDischarge.SCREEN_ON_SINCE_CHARGE,
7839 getDischargeAmountScreenOnSinceCharge());
7840 proto.write(SystemProto.BatteryDischarge.SCREEN_OFF_SINCE_CHARGE,
7841 getDischargeAmountScreenOffSinceCharge());
7842 proto.write(SystemProto.BatteryDischarge.SCREEN_DOZE_SINCE_CHARGE,
7843 getDischargeAmountScreenDozeSinceCharge());
7844 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH,
7845 getUahDischarge(which) / 1000);
7846 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_OFF,
7847 getUahDischargeScreenOff(which) / 1000);
7848 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_DOZE,
7849 getUahDischargeScreenDoze(which) / 1000);
Mike Ma15313c92017-11-15 17:58:21 -08007850 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_LIGHT_DOZE,
7851 getUahDischargeLightDoze(which) / 1000);
7852 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_DEEP_DOZE,
7853 getUahDischargeDeepDoze(which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007854 proto.end(bdToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007855
7856 // Time remaining
7857 long timeRemainingUs = computeChargeTimeRemaining(rawRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -07007858 // These are part of a oneof, so we should only set one of them.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007859 if (timeRemainingUs >= 0) {
7860 // Charge time remaining (CHARGE_TIME_REMAIN_DATA)
7861 proto.write(SystemProto.CHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7862 } else {
7863 timeRemainingUs = computeBatteryTimeRemaining(rawRealtimeUs);
7864 // Discharge time remaining (DISCHARGE_TIME_REMAIN_DATA)
7865 if (timeRemainingUs >= 0) {
7866 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7867 } else {
7868 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, -1);
7869 }
7870 }
7871
7872 // Charge step (CHARGE_STEP_DATA)
7873 dumpDurationSteps(proto, SystemProto.CHARGE_STEP, getChargeLevelStepTracker());
7874
7875 // Phone data connection (DATA_CONNECTION_TIME_DATA and DATA_CONNECTION_COUNT_DATA)
7876 for (int i = 0; i < NUM_DATA_CONNECTION_TYPES; ++i) {
Tej Singheee317b2018-03-07 19:28:05 -08007877 // Map OTHER to TelephonyManager.NETWORK_TYPE_UNKNOWN and mark NONE as a boolean.
Blake Kragtenbc75c722019-03-20 17:14:58 -07007878 boolean isNone = (i == DATA_CONNECTION_OUT_OF_SERVICE);
Tej Singheee317b2018-03-07 19:28:05 -08007879 int telephonyNetworkType = i;
Blake Kragtenbc75c722019-03-20 17:14:58 -07007880 if (i == DATA_CONNECTION_OTHER || i == DATA_CONNECTION_EMERGENCY_SERVICE) {
Tej Singheee317b2018-03-07 19:28:05 -08007881 telephonyNetworkType = TelephonyManager.NETWORK_TYPE_UNKNOWN;
7882 }
Kweku Adams103351f2017-10-16 14:39:34 -07007883 final long pdcToken = proto.start(SystemProto.DATA_CONNECTION);
Tej Singheee317b2018-03-07 19:28:05 -08007884 if (isNone) {
7885 proto.write(SystemProto.DataConnection.IS_NONE, isNone);
7886 } else {
7887 proto.write(SystemProto.DataConnection.NAME, telephonyNetworkType);
7888 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07007889 dumpTimer(proto, SystemProto.DataConnection.TOTAL, getPhoneDataConnectionTimer(i),
7890 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007891 proto.end(pdcToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007892 }
7893
7894 // Discharge step (DISCHARGE_STEP_DATA)
7895 dumpDurationSteps(proto, SystemProto.DISCHARGE_STEP, getDischargeLevelStepTracker());
7896
7897 // CPU frequencies (GLOBAL_CPU_FREQ_DATA)
7898 final long[] cpuFreqs = getCpuFreqs();
7899 if (cpuFreqs != null) {
7900 for (long i : cpuFreqs) {
7901 proto.write(SystemProto.CPU_FREQUENCY, i);
7902 }
7903 }
7904
7905 // Bluetooth controller (GLOBAL_BLUETOOTH_CONTROLLER_DATA)
7906 dumpControllerActivityProto(proto, SystemProto.GLOBAL_BLUETOOTH_CONTROLLER,
7907 getBluetoothControllerActivity(), which);
7908
7909 // Modem controller (GLOBAL_MODEM_CONTROLLER_DATA)
7910 dumpControllerActivityProto(proto, SystemProto.GLOBAL_MODEM_CONTROLLER,
7911 getModemControllerActivity(), which);
7912
7913 // Global network data (GLOBAL_NETWORK_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007914 final long gnToken = proto.start(SystemProto.GLOBAL_NETWORK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007915 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_RX,
7916 getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7917 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_TX,
7918 getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7919 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_RX,
7920 getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7921 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_TX,
7922 getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7923 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_RX,
7924 getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7925 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_TX,
7926 getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7927 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_RX,
7928 getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7929 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_TX,
7930 getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7931 proto.write(SystemProto.GlobalNetwork.BT_BYTES_RX,
7932 getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7933 proto.write(SystemProto.GlobalNetwork.BT_BYTES_TX,
7934 getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
Kweku Adams103351f2017-10-16 14:39:34 -07007935 proto.end(gnToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007936
7937 // Wifi controller (GLOBAL_WIFI_CONTROLLER_DATA)
7938 dumpControllerActivityProto(proto, SystemProto.GLOBAL_WIFI_CONTROLLER,
7939 getWifiControllerActivity(), which);
7940
7941
7942 // Global wifi (GLOBAL_WIFI_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007943 final long gwToken = proto.start(SystemProto.GLOBAL_WIFI);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007944 proto.write(SystemProto.GlobalWifi.ON_DURATION_MS,
7945 getWifiOnTime(rawRealtimeUs, which) / 1000);
7946 proto.write(SystemProto.GlobalWifi.RUNNING_DURATION_MS,
7947 getGlobalWifiRunningTime(rawRealtimeUs, which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007948 proto.end(gwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007949
7950 // Kernel wakelock (KERNEL_WAKELOCK_DATA)
7951 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
7952 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007953 final long kwToken = proto.start(SystemProto.KERNEL_WAKELOCK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007954 proto.write(SystemProto.KernelWakelock.NAME, ent.getKey());
7955 dumpTimer(proto, SystemProto.KernelWakelock.TOTAL, ent.getValue(),
7956 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007957 proto.end(kwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007958 }
7959
7960 // Misc (MISC_DATA)
7961 // Calculate wakelock times across all uids.
7962 long fullWakeLockTimeTotalUs = 0;
7963 long partialWakeLockTimeTotalUs = 0;
7964
7965 final SparseArray<? extends Uid> uidStats = getUidStats();
7966 for (int iu = 0; iu < uidStats.size(); iu++) {
7967 final Uid u = uidStats.valueAt(iu);
7968
7969 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks =
7970 u.getWakelockStats();
7971 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7972 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7973
7974 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
7975 if (fullWakeTimer != null) {
7976 fullWakeLockTimeTotalUs += fullWakeTimer.getTotalTimeLocked(rawRealtimeUs,
7977 which);
7978 }
7979
7980 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7981 if (partialWakeTimer != null) {
7982 partialWakeLockTimeTotalUs += partialWakeTimer.getTotalTimeLocked(
7983 rawRealtimeUs, which);
7984 }
7985 }
7986 }
Kweku Adams103351f2017-10-16 14:39:34 -07007987 final long mToken = proto.start(SystemProto.MISC);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007988 proto.write(SystemProto.Misc.SCREEN_ON_DURATION_MS,
7989 getScreenOnTime(rawRealtimeUs, which) / 1000);
7990 proto.write(SystemProto.Misc.PHONE_ON_DURATION_MS,
7991 getPhoneOnTime(rawRealtimeUs, which) / 1000);
7992 proto.write(SystemProto.Misc.FULL_WAKELOCK_TOTAL_DURATION_MS,
7993 fullWakeLockTimeTotalUs / 1000);
7994 proto.write(SystemProto.Misc.PARTIAL_WAKELOCK_TOTAL_DURATION_MS,
7995 partialWakeLockTimeTotalUs / 1000);
7996 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_DURATION_MS,
7997 getMobileRadioActiveTime(rawRealtimeUs, which) / 1000);
7998 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_ADJUSTED_TIME_MS,
7999 getMobileRadioActiveAdjustedTime(which) / 1000);
8000 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_COUNT,
8001 getMobileRadioActiveCount(which));
8002 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_UNKNOWN_DURATION_MS,
8003 getMobileRadioActiveUnknownTime(which) / 1000);
8004 proto.write(SystemProto.Misc.INTERACTIVE_DURATION_MS,
8005 getInteractiveTime(rawRealtimeUs, which) / 1000);
8006 proto.write(SystemProto.Misc.BATTERY_SAVER_MODE_ENABLED_DURATION_MS,
8007 getPowerSaveModeEnabledTime(rawRealtimeUs, which) / 1000);
8008 proto.write(SystemProto.Misc.NUM_CONNECTIVITY_CHANGES,
8009 getNumConnectivityChange(which));
8010 proto.write(SystemProto.Misc.DEEP_DOZE_ENABLED_DURATION_MS,
8011 getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
8012 proto.write(SystemProto.Misc.DEEP_DOZE_COUNT,
8013 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
8014 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_DURATION_MS,
8015 getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
8016 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_COUNT,
8017 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
8018 proto.write(SystemProto.Misc.LONGEST_DEEP_DOZE_DURATION_MS,
8019 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
8020 proto.write(SystemProto.Misc.LIGHT_DOZE_ENABLED_DURATION_MS,
8021 getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
8022 proto.write(SystemProto.Misc.LIGHT_DOZE_COUNT,
8023 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
8024 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_DURATION_MS,
8025 getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
8026 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_COUNT,
8027 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
8028 proto.write(SystemProto.Misc.LONGEST_LIGHT_DOZE_DURATION_MS,
8029 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
Kweku Adams103351f2017-10-16 14:39:34 -07008030 proto.end(mToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008031
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07008032 // Wifi multicast wakelock total stats (WIFI_MULTICAST_WAKELOCK_TOTAL_DATA)
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08008033 final long multicastWakeLockTimeTotalUs =
8034 getWifiMulticastWakelockTime(rawRealtimeUs, which);
8035 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07008036 final long wmctToken = proto.start(SystemProto.WIFI_MULTICAST_WAKELOCK_TOTAL);
8037 proto.write(SystemProto.WifiMulticastWakelockTotal.DURATION_MS,
8038 multicastWakeLockTimeTotalUs / 1000);
8039 proto.write(SystemProto.WifiMulticastWakelockTotal.COUNT,
8040 multicastWakeLockCountTotal);
8041 proto.end(wmctToken);
8042
Kweku Adams87b19ec2017-10-09 12:40:03 -07008043 // Power use item (POWER_USE_ITEM_DATA)
8044 final List<BatterySipper> sippers = helper.getUsageList();
8045 if (sippers != null) {
8046 for (int i = 0; i < sippers.size(); ++i) {
8047 final BatterySipper bs = sippers.get(i);
8048 int n = SystemProto.PowerUseItem.UNKNOWN_SIPPER;
8049 int uid = 0;
8050 switch (bs.drainType) {
Kweku Adamsef728952018-04-10 17:56:16 -07008051 case AMBIENT_DISPLAY:
8052 n = SystemProto.PowerUseItem.AMBIENT_DISPLAY;
8053 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07008054 case IDLE:
8055 n = SystemProto.PowerUseItem.IDLE;
8056 break;
8057 case CELL:
8058 n = SystemProto.PowerUseItem.CELL;
8059 break;
8060 case PHONE:
8061 n = SystemProto.PowerUseItem.PHONE;
8062 break;
8063 case WIFI:
8064 n = SystemProto.PowerUseItem.WIFI;
8065 break;
8066 case BLUETOOTH:
8067 n = SystemProto.PowerUseItem.BLUETOOTH;
8068 break;
8069 case SCREEN:
8070 n = SystemProto.PowerUseItem.SCREEN;
8071 break;
8072 case FLASHLIGHT:
8073 n = SystemProto.PowerUseItem.FLASHLIGHT;
8074 break;
8075 case APP:
Kweku Adams103351f2017-10-16 14:39:34 -07008076 // dumpProtoAppsLocked will handle this.
Kweku Adams87b19ec2017-10-09 12:40:03 -07008077 continue;
8078 case USER:
8079 n = SystemProto.PowerUseItem.USER;
8080 uid = UserHandle.getUid(bs.userId, 0);
8081 break;
8082 case UNACCOUNTED:
8083 n = SystemProto.PowerUseItem.UNACCOUNTED;
8084 break;
8085 case OVERCOUNTED:
8086 n = SystemProto.PowerUseItem.OVERCOUNTED;
8087 break;
8088 case CAMERA:
8089 n = SystemProto.PowerUseItem.CAMERA;
8090 break;
8091 case MEMORY:
8092 n = SystemProto.PowerUseItem.MEMORY;
8093 break;
8094 }
Kweku Adams103351f2017-10-16 14:39:34 -07008095 final long puiToken = proto.start(SystemProto.POWER_USE_ITEM);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008096 proto.write(SystemProto.PowerUseItem.NAME, n);
8097 proto.write(SystemProto.PowerUseItem.UID, uid);
8098 proto.write(SystemProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
8099 proto.write(SystemProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
8100 proto.write(SystemProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
8101 proto.write(SystemProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
8102 bs.proportionalSmearMah);
Kweku Adams103351f2017-10-16 14:39:34 -07008103 proto.end(puiToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008104 }
8105 }
8106
8107 // Power use summary (POWER_USE_SUMMARY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07008108 final long pusToken = proto.start(SystemProto.POWER_USE_SUMMARY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008109 proto.write(SystemProto.PowerUseSummary.BATTERY_CAPACITY_MAH,
8110 helper.getPowerProfile().getBatteryCapacity());
8111 proto.write(SystemProto.PowerUseSummary.COMPUTED_POWER_MAH, helper.getComputedPower());
8112 proto.write(SystemProto.PowerUseSummary.MIN_DRAINED_POWER_MAH, helper.getMinDrainedPower());
8113 proto.write(SystemProto.PowerUseSummary.MAX_DRAINED_POWER_MAH, helper.getMaxDrainedPower());
Kweku Adams103351f2017-10-16 14:39:34 -07008114 proto.end(pusToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008115
8116 // RPM stats (RESOURCE_POWER_MANAGER_DATA)
8117 final Map<String, ? extends Timer> rpmStats = getRpmStats();
8118 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
8119 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07008120 final long rpmToken = proto.start(SystemProto.RESOURCE_POWER_MANAGER);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008121 proto.write(SystemProto.ResourcePowerManager.NAME, ent.getKey());
8122 dumpTimer(proto, SystemProto.ResourcePowerManager.TOTAL,
8123 ent.getValue(), rawRealtimeUs, which);
8124 dumpTimer(proto, SystemProto.ResourcePowerManager.SCREEN_OFF,
8125 screenOffRpmStats.get(ent.getKey()), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008126 proto.end(rpmToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008127 }
8128
8129 // Screen brightness (SCREEN_BRIGHTNESS_DATA)
8130 for (int i = 0; i < NUM_SCREEN_BRIGHTNESS_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008131 final long sbToken = proto.start(SystemProto.SCREEN_BRIGHTNESS);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008132 proto.write(SystemProto.ScreenBrightness.NAME, i);
8133 dumpTimer(proto, SystemProto.ScreenBrightness.TOTAL, getScreenBrightnessTimer(i),
8134 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008135 proto.end(sbToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008136 }
8137
8138 // Signal scanning time (SIGNAL_SCANNING_TIME_DATA)
8139 dumpTimer(proto, SystemProto.SIGNAL_SCANNING, getPhoneSignalScanningTimer(), rawRealtimeUs,
8140 which);
8141
8142 // Phone signal strength (SIGNAL_STRENGTH_TIME_DATA and SIGNAL_STRENGTH_COUNT_DATA)
8143 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008144 final long pssToken = proto.start(SystemProto.PHONE_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008145 proto.write(SystemProto.PhoneSignalStrength.NAME, i);
8146 dumpTimer(proto, SystemProto.PhoneSignalStrength.TOTAL, getPhoneSignalStrengthTimer(i),
8147 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008148 proto.end(pssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008149 }
8150
8151 // Wakeup reasons (WAKEUP_REASON_DATA)
8152 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
8153 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07008154 final long wrToken = proto.start(SystemProto.WAKEUP_REASON);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008155 proto.write(SystemProto.WakeupReason.NAME, ent.getKey());
8156 dumpTimer(proto, SystemProto.WakeupReason.TOTAL, ent.getValue(), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008157 proto.end(wrToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008158 }
8159
8160 // Wifi signal strength (WIFI_SIGNAL_STRENGTH_TIME_DATA and WIFI_SIGNAL_STRENGTH_COUNT_DATA)
8161 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008162 final long wssToken = proto.start(SystemProto.WIFI_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008163 proto.write(SystemProto.WifiSignalStrength.NAME, i);
8164 dumpTimer(proto, SystemProto.WifiSignalStrength.TOTAL, getWifiSignalStrengthTimer(i),
8165 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008166 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008167 }
8168
8169 // Wifi state (WIFI_STATE_TIME_DATA and WIFI_STATE_COUNT_DATA)
8170 for (int i = 0; i < NUM_WIFI_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008171 final long wsToken = proto.start(SystemProto.WIFI_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008172 proto.write(SystemProto.WifiState.NAME, i);
8173 dumpTimer(proto, SystemProto.WifiState.TOTAL, getWifiStateTimer(i),
8174 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008175 proto.end(wsToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008176 }
8177
8178 // Wifi supplicant state (WIFI_SUPPL_STATE_TIME_DATA and WIFI_SUPPL_STATE_COUNT_DATA)
8179 for (int i = 0; i < NUM_WIFI_SUPPL_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008180 final long wssToken = proto.start(SystemProto.WIFI_SUPPLICANT_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008181 proto.write(SystemProto.WifiSupplicantState.NAME, i);
8182 dumpTimer(proto, SystemProto.WifiSupplicantState.TOTAL, getWifiSupplStateTimer(i),
8183 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008184 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008185 }
8186
8187 proto.end(sToken);
8188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008189}