blob: 59e9972abde81576c420999d6eda9d81b4caa8d4 [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
Artur Satayevee9c6e72019-11-04 15:58:52 +000069 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
70 public BatteryStats() {
71 }
72
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 private static final boolean LOCAL_LOGV = false;
Bookatz82b341172017-09-07 19:06:08 -070074 /** Fetching RPM stats is too slow to do each time screen changes, so disable it. */
75 protected static final boolean SCREEN_OFF_RPM_STATS_ENABLED = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070076
77 /** @hide */
78 public static final String SERVICE_NAME = "batterystats";
79
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 /**
81 * A constant indicating a partial wake lock timer.
82 */
Andrei Onea24ec3212019-03-15 17:35:05 +000083 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 public static final int WAKE_TYPE_PARTIAL = 0;
85
86 /**
87 * A constant indicating a full wake lock timer.
88 */
89 public static final int WAKE_TYPE_FULL = 1;
90
91 /**
92 * A constant indicating a window wake lock timer.
93 */
94 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 /**
97 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 */
99 public static final int SENSOR = 3;
Mike Mac2f518a2017-09-19 16:06:03 -0700100
The Android Open Source Project10592532009-03-18 17:39:46 -0700101 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700102 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -0700103 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700104 public static final int WIFI_RUNNING = 4;
Mike Mac2f518a2017-09-19 16:06:03 -0700105
Dianne Hackborn617f8772009-03-31 15:04:46 -0700106 /**
The Android Open Source Project10592532009-03-18 17:39:46 -0700107 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -0700108 */
Dianne Hackborn617f8772009-03-31 15:04:46 -0700109 public static final int FULL_WIFI_LOCK = 5;
Mike Mac2f518a2017-09-19 16:06:03 -0700110
The Android Open Source Project10592532009-03-18 17:39:46 -0700111 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -0700112 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -0700113 */
Nick Pelly6ccaa542012-06-15 15:22:47 -0700114 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
Dianne Hackborn62793e42015-03-09 11:15:41 -0700116 /**
117 * A constant indicating a wifi multicast timer
118 */
119 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700122 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700123 */
124 public static final int VIDEO_TURNED_ON = 8;
125
126 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800127 * A constant indicating a vibrator on timer
128 */
129 public static final int VIBRATOR_ON = 9;
130
131 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700132 * A constant indicating a foreground activity timer
133 */
134 public static final int FOREGROUND_ACTIVITY = 10;
135
136 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700137 * A constant indicating a wifi batched scan is active
138 */
139 public static final int WIFI_BATCHED_SCAN = 11;
140
141 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700142 * A constant indicating a process state timer
143 */
144 public static final int PROCESS_STATE = 12;
145
146 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700147 * A constant indicating a sync timer
148 */
149 public static final int SYNC = 13;
150
151 /**
152 * A constant indicating a job timer
153 */
154 public static final int JOB = 14;
155
156 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800157 * A constant indicating an audio turn on timer
158 */
159 public static final int AUDIO_TURNED_ON = 15;
160
161 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700162 * A constant indicating a flashlight turn on timer
163 */
164 public static final int FLASHLIGHT_TURNED_ON = 16;
165
166 /**
167 * A constant indicating a camera turn on timer
168 */
169 public static final int CAMERA_TURNED_ON = 17;
170
171 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700172 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700173 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700174 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700175
176 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800177 * A constant indicating a bluetooth scan timer.
178 */
179 public static final int BLUETOOTH_SCAN_ON = 19;
180
181 /**
Bookatzc8c44962017-05-11 12:12:54 -0700182 * A constant indicating an aggregated partial wake lock timer.
183 */
184 public static final int AGGREGATED_WAKE_TYPE_PARTIAL = 20;
185
186 /**
Bookatzb1f04f32017-05-19 13:57:32 -0700187 * A constant indicating a bluetooth scan timer for unoptimized scans.
188 */
189 public static final int BLUETOOTH_UNOPTIMIZED_SCAN_ON = 21;
190
191 /**
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700192 * A constant indicating a foreground service timer
193 */
194 public static final int FOREGROUND_SERVICE = 22;
195
196 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -0800197 * A constant indicating an aggregate wifi multicast timer
198 */
199 public static final int WIFI_AGGREGATE_MULTICAST_ENABLED = 23;
200
201 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 * Include all of the data in the stats, including previously saved data.
203 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700204 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205
206 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * Include only the current run in the stats.
Mike Ma99e57c32018-03-15 14:40:37 -0700208 *
209 * @deprecated As of {@link android.os.Build.VERSION_CODES#Q}, only {@link #STATS_SINCE_CHARGED}
210 * is supported.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000212 @UnsupportedAppUsage
Mike Ma99e57c32018-03-15 14:40:37 -0700213 @Deprecated
Dianne Hackborn4590e522014-03-24 13:36:46 -0700214 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215
216 /**
217 * Include only the run since the last time the device was unplugged in the stats.
Mike Ma99e57c32018-03-15 14:40:37 -0700218 *
219 * @deprecated As of {@link android.os.Build.VERSION_CODES#Q}, only {@link #STATS_SINCE_CHARGED}
220 * is supported.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 */
Mike Ma99e57c32018-03-15 14:40:37 -0700222 @Deprecated
Dianne Hackborn4590e522014-03-24 13:36:46 -0700223 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700224
225 // NOTE: Update this list if you add/change any stats above.
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700226 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700227 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700228 private static final String[] STAT_NAMES = { "l", "c", "u" };
229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700231 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700232 *
233 * New in version 19:
234 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800235 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700236 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Bookatz506a8182017-05-01 14:18:42 -0700237 * New in version 21:
238 * - Actual (not just apportioned) Wakelock time is also recorded.
Bookatzc8c44962017-05-11 12:12:54 -0700239 * - Aggregated partial wakelock time (per uid, instead of per wakelock) is recorded.
Bookatzb1f04f32017-05-19 13:57:32 -0700240 * - BLE scan result count
241 * - CPU frequency time per uid
242 * New in version 22:
243 * - BLE scan result background count, BLE unoptimized scan time
Bookatz98d4d5c2017-08-01 19:07:54 -0700244 * - Background partial wakelock time & count
245 * New in version 23:
246 * - Logging smeared power model values
247 * New in version 24:
248 * - Fixed bugs in background timers and BLE scan time
249 * New in version 25:
250 * - Package wakeup alarms are now on screen-off timebase
Bookatz50df7112017-08-04 14:53:26 -0700251 * New in version 26:
Bookatz82b341172017-09-07 19:06:08 -0700252 * - Resource power manager (rpm) states [but screenOffRpm is disabled from working properly]
Mike Mac2f518a2017-09-19 16:06:03 -0700253 * New in version 27:
254 * - Always On Display (screen doze mode) time and power
Mike Ma15313c92017-11-15 17:58:21 -0800255 * New in version 28:
256 * - Light/Deep Doze power
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700257 * - WiFi Multicast Wakelock statistics (count & duration)
Kweku Adamsa8943cb2017-12-22 13:21:06 -0800258 * New in version 29:
259 * - Process states re-ordered. TOP_SLEEPING now below BACKGROUND. HEAVY_WEIGHT introduced.
260 * - CPU times per UID process state
zhouwenjie46712bc2018-01-11 15:21:27 -0800261 * New in version 30:
262 * - Uid.PROCESS_STATE_FOREGROUND_SERVICE only tracks
263 * ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE.
Kweku Adamsb78430e2018-02-20 18:06:09 -0800264 * New in version 31:
265 * - New cellular network types.
266 * - Deferred job metrics.
Kweku Adams6fe1a5a2018-05-22 15:02:50 -0700267 * New in version 32:
268 * - Ambient display properly output in data dump.
zhouwenjied707da92018-07-06 10:23:00 -0700269 * New in version 33:
270 * - Fixed bug in min learned capacity updating process.
Mike Ma99e57c32018-03-15 14:40:37 -0700271 * New in version 34:
272 * - Deprecated STATS_SINCE_UNPLUGGED and STATS_CURRENT.
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700273 */
Mike Ma99e57c32018-03-15 14:40:37 -0700274 static final int CHECKIN_VERSION = 34;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700275
276 /**
277 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700279 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700280
Evan Millar22ac0432009-03-31 11:33:18 -0700281 private static final long BYTES_PER_KB = 1024;
282 private static final long BYTES_PER_MB = 1048576; // 1024^2
283 private static final long BYTES_PER_GB = 1073741824; //1024^3
Blake Kragtencb308d92019-01-28 10:54:13 -0800284 public static final double MILLISECONDS_IN_HOUR = 3600 * 1000;
Bookatz506a8182017-05-01 14:18:42 -0700285
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700286 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800287 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700288 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700290 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700291 private static final String CPU_DATA = "cpu";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700292 private static final String GLOBAL_CPU_FREQ_DATA = "gcf";
293 private static final String CPU_TIMES_AT_FREQ_DATA = "ctf";
Bookatz50df7112017-08-04 14:53:26 -0700294 // rpm line is:
295 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "rpm", state/voter name, total time, total count,
296 // screen-off time, screen-off count
297 private static final String RESOURCE_POWER_MANAGER_DATA = "rpm";
Evan Millare84de8d2009-04-02 22:16:12 -0700298 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800299 private static final String VIBRATOR_DATA = "vib";
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700300 private static final String FOREGROUND_ACTIVITY_DATA = "fg";
301 // fgs line is:
302 // BATTERY_STATS_CHECKIN_VERSION, uid, category, "fgs",
303 // foreground service time, count
304 private static final String FOREGROUND_SERVICE_DATA = "fgs";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700305 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700306 // wl line is:
307 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
Bookatz5b5ec322017-05-26 09:40:38 -0700308 // full totalTime, 'f', count, current duration, max duration, total duration,
309 // partial totalTime, 'p', count, current duration, max duration, total duration,
310 // bg partial totalTime, 'bp', count, current duration, max duration, total duration,
311 // window totalTime, 'w', count, current duration, max duration, total duration
Bookatz506a8182017-05-01 14:18:42 -0700312 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700313 private static final String WAKELOCK_DATA = "wl";
Bookatzc8c44962017-05-11 12:12:54 -0700314 // awl line is:
315 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "awl",
316 // cumulative partial wakelock duration, cumulative background partial wakelock duration
317 private static final String AGGREGATED_WAKELOCK_DATA = "awl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700318 private static final String SYNC_DATA = "sy";
319 private static final String JOB_DATA = "jb";
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700320 private static final String JOB_COMPLETION_DATA = "jbc";
Amith Yamasani977e11f2018-02-16 11:29:54 -0800321
322 /**
323 * jbd line is:
324 * BATTERY_STATS_CHECKIN_VERSION, uid, which, "jbd",
Amith Yamasani0ca706b2018-03-01 17:28:59 -0800325 * jobsDeferredEventCount, jobsDeferredCount, totalLatencyMillis,
326 * 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 -0800327 * <p>
328 * @see #JOB_FRESHNESS_BUCKETS
329 */
330 private static final String JOBS_DEFERRED_DATA = "jbd";
Evan Millarc64edde2009-04-18 12:26:32 -0700331 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700332 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700333 private static final String NETWORK_DATA = "nt";
334 private static final String USER_ACTIVITY_DATA = "ua";
335 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800336 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700337 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700338 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700339 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800340 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
341 private static final String WIFI_CONTROLLER_DATA = "wfcd";
342 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
343 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700344 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700345 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800346 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800347 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
348 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800349 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700350 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700351 private static final String SCREEN_BRIGHTNESS_DATA = "br";
352 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700353 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700354 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
355 private static final String DATA_CONNECTION_TIME_DATA = "dct";
356 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800357 private static final String WIFI_STATE_TIME_DATA = "wst";
358 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700359 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
360 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
361 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
362 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800363 private static final String POWER_USE_SUMMARY_DATA = "pws";
364 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700365 private static final String DISCHARGE_STEP_DATA = "dsd";
366 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700367 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
368 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700369 private static final String FLASHLIGHT_DATA = "fla";
370 private static final String CAMERA_DATA = "cam";
371 private static final String VIDEO_DATA = "vid";
372 private static final String AUDIO_DATA = "aud";
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700373 private static final String WIFI_MULTICAST_TOTAL_DATA = "wmct";
374 private static final String WIFI_MULTICAST_DATA = "wmc";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375
Adam Lesinski010bf372016-04-11 12:18:18 -0700376 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
377
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700378 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 private final Formatter mFormatter = new Formatter(mFormatBuilder);
380
Siddharth Rayb50a6842017-12-14 15:15:28 -0800381 private static final String CELLULAR_CONTROLLER_NAME = "Cellular";
382 private static final String WIFI_CONTROLLER_NAME = "WiFi";
383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700385 * Indicates times spent by the uid at each cpu frequency in all process states.
386 *
387 * Other types might include times spent in foreground, background etc.
388 */
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800389 @VisibleForTesting
390 public static final String UID_TIMES_TYPE_ALL = "A";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700391
392 /**
Amith Yamasani977e11f2018-02-16 11:29:54 -0800393 * These are the thresholds for bucketing last time since a job was run for an app
394 * 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 -0800395 * than 1 hour ago, then it's reasonably fresh, 2 hours ago, not so fresh and so
Amith Yamasani977e11f2018-02-16 11:29:54 -0800396 * on.
397 */
398 public static final long[] JOB_FRESHNESS_BUCKETS = {
399 1 * 60 * 60 * 1000L,
400 2 * 60 * 60 * 1000L,
Amith Yamasani0ca706b2018-03-01 17:28:59 -0800401 4 * 60 * 60 * 1000L,
402 8 * 60 * 60 * 1000L,
Amith Yamasani977e11f2018-02-16 11:29:54 -0800403 Long.MAX_VALUE
404 };
405
406 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700407 * State for keeping track of counting information.
408 */
409 public static abstract class Counter {
410
Artur Satayevee9c6e72019-11-04 15:58:52 +0000411 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
412 public Counter() {
413 }
414
Dianne Hackborn617f8772009-03-31 15:04:46 -0700415 /**
416 * Returns the count associated with this Counter for the
417 * selected type of statistics.
418 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700419 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700420 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000421 @UnsupportedAppUsage
Evan Millarc64edde2009-04-18 12:26:32 -0700422 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700423
424 /**
425 * Temporary for debugging.
426 */
427 public abstract void logState(Printer pw, String prefix);
428 }
429
430 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700431 * State for keeping track of long counting information.
432 */
433 public static abstract class LongCounter {
434
435 /**
436 * Returns the count associated with this Counter for the
437 * selected type of statistics.
438 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700439 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700440 */
441 public abstract long getCountLocked(int which);
442
443 /**
444 * Temporary for debugging.
445 */
446 public abstract void logState(Printer pw, String prefix);
447 }
448
449 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700450 * State for keeping track of array of long counting information.
451 */
452 public static abstract class LongCounterArray {
453 /**
454 * Returns the counts associated with this Counter for the
455 * selected type of statistics.
456 *
457 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
458 */
459 public abstract long[] getCountsLocked(int which);
460
461 /**
462 * Temporary for debugging.
463 */
464 public abstract void logState(Printer pw, String prefix);
465 }
466
467 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800468 * Container class that aggregates counters for transmit, receive, and idle state of a
469 * radio controller.
470 */
471 public static abstract class ControllerActivityCounter {
472 /**
473 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
474 * idle state.
475 */
476 public abstract LongCounter getIdleTimeCounter();
477
478 /**
479 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
Siddharth Rayb50a6842017-12-14 15:15:28 -0800480 * scan state.
481 */
482 public abstract LongCounter getScanTimeCounter();
483
Siddharth Rayed754702018-02-15 12:44:37 -0800484 /**
485 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
486 * sleep state.
487 */
488 public abstract LongCounter getSleepTimeCounter();
Siddharth Rayb50a6842017-12-14 15:15:28 -0800489
490 /**
491 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800492 * receive state.
493 */
494 public abstract LongCounter getRxTimeCounter();
495
496 /**
497 * An array of {@link LongCounter}, representing various transmit levels, where each level
498 * may draw a different amount of power. The levels themselves are controller-specific.
499 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
500 * various transmit level states.
501 */
502 public abstract LongCounter[] getTxTimeCounters();
503
504 /**
505 * @return a non-null {@link LongCounter} representing the power consumed by the controller
506 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
507 * yield a value of 0 if the device doesn't support power calculations.
508 */
509 public abstract LongCounter getPowerCounter();
Blake Kragtencb308d92019-01-28 10:54:13 -0800510
511 /**
512 * @return a non-null {@link LongCounter} representing total power monitored on the rails
513 * in mAms (miliamps-milliseconds). The counter may always yield a value of 0 if the device
514 * doesn't support power rail monitoring.
515 */
516 public abstract LongCounter getMonitoredRailChargeConsumedMaMs();
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800517 }
518
519 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 * State for keeping track of timing information.
521 */
522 public static abstract class Timer {
523
Artur Satayevee9c6e72019-11-04 15:58:52 +0000524 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
525 public Timer() {
526 }
527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 /**
529 * Returns the count associated with this Timer for the
530 * selected type of statistics.
531 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700532 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000534 @UnsupportedAppUsage
Evan Millarc64edde2009-04-18 12:26:32 -0700535 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536
537 /**
538 * Returns the total time in microseconds associated with this Timer for the
539 * selected type of statistics.
540 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800541 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700542 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 * @return a time in microseconds
544 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000545 @UnsupportedAppUsage
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800546 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700549 * Returns the total time in microseconds associated with this Timer since the
550 * 'mark' was last set.
551 *
552 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
553 * @return a time in microseconds
554 */
555 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
556
557 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700558 * Returns the max duration if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700559 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700560 */
561 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
562 return -1;
563 }
564
565 /**
566 * Returns the current time the timer has been active, if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700567 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700568 */
569 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
570 return -1;
571 }
572
573 /**
Kweku Adams103351f2017-10-16 14:39:34 -0700574 * Returns the total time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800575 *
576 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
577 * been on since reset.
578 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
579 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
580 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
581 * the actual total time.
Kweku Adams103351f2017-10-16 14:39:34 -0700582 * Not all Timer subclasses track the max, total, and current durations.
Bookatz867c0d72017-03-07 18:23:42 -0800583 */
584 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
585 return -1;
586 }
587
588 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700589 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
590 * used, for example, for tracking background usage. Secondary timers are never pooled.
591 *
592 * Not all Timer subclasses have a secondary timer; those that don't return null.
593 */
594 public Timer getSubTimer() {
595 return null;
596 }
597
598 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700599 * Returns whether the timer is currently running. Some types of timers
600 * (e.g. BatchTimers) don't know whether the event is currently active,
601 * and report false.
602 */
603 public boolean isRunningLocked() {
604 return false;
605 }
606
607 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 * Temporary for debugging.
609 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700610 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 }
612
613 /**
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800614 * Maps the ActivityManager procstate into corresponding BatteryStats procstate.
615 */
616 public static int mapToInternalProcessState(int procState) {
617 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
618 return ActivityManager.PROCESS_STATE_NONEXISTENT;
619 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
620 return Uid.PROCESS_STATE_TOP;
Amith Yamasania0a30a12019-01-22 11:38:06 -0800621 } else if (ActivityManager.isForegroundService(procState)) {
Dianne Hackborn10fc4fd2017-12-19 17:23:13 -0800622 // State when app has put itself in the foreground.
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800623 return Uid.PROCESS_STATE_FOREGROUND_SERVICE;
624 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
625 // Persistent and other foreground states go here.
626 return Uid.PROCESS_STATE_FOREGROUND;
627 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
628 return Uid.PROCESS_STATE_BACKGROUND;
629 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
630 return Uid.PROCESS_STATE_TOP_SLEEPING;
631 } else if (procState <= ActivityManager.PROCESS_STATE_HEAVY_WEIGHT) {
632 return Uid.PROCESS_STATE_HEAVY_WEIGHT;
633 } else {
634 return Uid.PROCESS_STATE_CACHED;
635 }
636 }
637
638 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 * The statistics associated with a particular uid.
640 */
641 public static abstract class Uid {
642
Artur Satayevfc46be72019-11-04 17:50:59 +0000643 @UnsupportedAppUsage
644 public Uid() {
645 }
646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 /**
648 * Returns a mapping containing wakelock statistics.
649 *
650 * @return a Map from Strings to Uid.Wakelock objects.
651 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000652 @UnsupportedAppUsage
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700653 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654
655 /**
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700656 * Returns the WiFi Multicast Wakelock statistics.
657 *
658 * @return a Timer Object for the per uid Multicast statistics.
659 */
660 public abstract Timer getMulticastWakelockStats();
661
662 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700663 * Returns a mapping containing sync statistics.
664 *
665 * @return a Map from Strings to Timer objects.
666 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700667 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700668
669 /**
670 * Returns a mapping containing scheduled job statistics.
671 *
672 * @return a Map from Strings to Timer objects.
673 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700674 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700675
676 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700677 * Returns statistics about how jobs have completed.
678 *
679 * @return A Map of String job names to completion type -> count mapping.
680 */
681 public abstract ArrayMap<String, SparseIntArray> getJobCompletionStats();
682
683 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 * The statistics associated with a particular wake lock.
685 */
686 public static abstract class Wakelock {
Artur Satayevee9c6e72019-11-04 15:58:52 +0000687
688 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
689 public Wakelock(){
690 }
691
Andrei Onea24ec3212019-03-15 17:35:05 +0000692 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 public abstract Timer getWakeTime(int type);
694 }
695
696 /**
Bookatzc8c44962017-05-11 12:12:54 -0700697 * The cumulative time the uid spent holding any partial wakelocks. This will generally
698 * differ from summing over the Wakelocks in getWakelockStats since the latter may have
699 * wakelocks that overlap in time (and therefore over-counts).
700 */
701 public abstract Timer getAggregatedPartialWakelockTimer();
702
703 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 * Returns a mapping containing sensor statistics.
705 *
706 * @return a Map from Integer sensor ids to Uid.Sensor objects.
707 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000708 @UnsupportedAppUsage
Dianne Hackborn61659e52014-07-09 16:13:01 -0700709 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710
711 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700712 * Returns a mapping containing active process data.
713 */
714 public abstract SparseArray<? extends Pid> getPidStats();
Bookatzc8c44962017-05-11 12:12:54 -0700715
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700716 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 * Returns a mapping containing process statistics.
718 *
719 * @return a Map from Strings to Uid.Proc objects.
720 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000721 @UnsupportedAppUsage
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700722 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723
724 /**
725 * Returns a mapping containing package statistics.
726 *
727 * @return a Map from Strings to Uid.Pkg objects.
728 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000729 @UnsupportedAppUsage
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700730 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700731
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800732 public abstract ControllerActivityCounter getWifiControllerActivity();
733 public abstract ControllerActivityCounter getBluetoothControllerActivity();
734 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800735
736 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 * {@hide}
738 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000739 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700741
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800742 public abstract void noteWifiRunningLocked(long elapsedRealtime);
743 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
744 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
745 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
746 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
747 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
748 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
749 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
750 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
751 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800752 public abstract void noteActivityResumedLocked(long elapsedRealtime);
753 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Andrei Onea24ec3212019-03-15 17:35:05 +0000754 @UnsupportedAppUsage
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800755 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
Andrei Onea24ec3212019-03-15 17:35:05 +0000756 @UnsupportedAppUsage
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800757 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
Andrei Onea24ec3212019-03-15 17:35:05 +0000758 @UnsupportedAppUsage
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800759 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700760 public abstract int getWifiScanCount(int which);
Kweku Adams103351f2017-10-16 14:39:34 -0700761 /**
762 * Returns the timer keeping track of wifi scans.
763 */
764 public abstract Timer getWifiScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800765 public abstract int getWifiScanBackgroundCount(int which);
766 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
767 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -0700768 /**
769 * Returns the timer keeping track of background wifi scans.
770 */
771 public abstract Timer getWifiScanBackgroundTimer();
Andrei Onea24ec3212019-03-15 17:35:05 +0000772 @UnsupportedAppUsage
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800773 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700774 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Andrei Onea24ec3212019-03-15 17:35:05 +0000775 @UnsupportedAppUsage
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800776 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Andrei Onea24ec3212019-03-15 17:35:05 +0000777 @UnsupportedAppUsage
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700778 public abstract Timer getAudioTurnedOnTimer();
Andrei Onea24ec3212019-03-15 17:35:05 +0000779 @UnsupportedAppUsage
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700780 public abstract Timer getVideoTurnedOnTimer();
781 public abstract Timer getFlashlightTurnedOnTimer();
782 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700783 public abstract Timer getForegroundActivityTimer();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700784
785 /**
786 * Returns the timer keeping track of Foreground Service time
787 */
788 public abstract Timer getForegroundServiceTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800789 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800790 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatzb1f04f32017-05-19 13:57:32 -0700791 public abstract Timer getBluetoothUnoptimizedScanTimer();
792 public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700793 public abstract Counter getBluetoothScanResultCounter();
Bookatzb1f04f32017-05-19 13:57:32 -0700794 public abstract Counter getBluetoothScanResultBgCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700795
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700796 public abstract long[] getCpuFreqTimes(int which);
797 public abstract long[] getScreenOffCpuFreqTimes(int which);
Mike Ma3d422c32017-10-25 11:08:57 -0700798 /**
799 * Returns cpu active time of an uid.
800 */
801 public abstract long getCpuActiveTime();
802 /**
803 * Returns cpu times of an uid on each cluster
804 */
805 public abstract long[] getCpuClusterTimes();
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700806
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800807 /**
808 * Returns cpu times of an uid at a particular process state.
809 */
810 public abstract long[] getCpuFreqTimes(int which, int procState);
811 /**
812 * Returns cpu times of an uid while the screen if off at a particular process state.
813 */
814 public abstract long[] getScreenOffCpuFreqTimes(int which, int procState);
815
Dianne Hackborna0200e32016-03-30 18:01:41 -0700816 // Note: the following times are disjoint. They can be added together to find the
817 // total time a uid has had any processes running at all.
818
819 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800820 * Time this uid has any processes in the top state.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700821 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800822 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700823 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800824 * Time this uid has any process with a started foreground service, but
Dianne Hackborna0200e32016-03-30 18:01:41 -0700825 * none in the "top" state.
826 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800827 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700828 /**
Dianne Hackborna0200e32016-03-30 18:01:41 -0700829 * Time this uid has any process in an active foreground state, but none in the
zhouwenjie46712bc2018-01-11 15:21:27 -0800830 * "foreground service" or better state. Persistent and other foreground states go here.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700831 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800832 public static final int PROCESS_STATE_FOREGROUND = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700833 /**
834 * Time this uid has any process in an active background state, but none in the
835 * "foreground" or better state.
836 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800837 public static final int PROCESS_STATE_BACKGROUND = 3;
838 /**
839 * Time this uid has any process that is top while the device is sleeping, but not
840 * active for any other reason. We kind-of consider it a kind of cached process
841 * for execution restrictions.
842 */
843 public static final int PROCESS_STATE_TOP_SLEEPING = 4;
844 /**
845 * Time this uid has any process that is in the background but it has an activity
846 * marked as "can't save state". This is essentially a cached process, though the
847 * system will try much harder than normal to avoid killing it.
848 */
849 public static final int PROCESS_STATE_HEAVY_WEIGHT = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700850 /**
851 * Time this uid has any processes that are sitting around cached, not in one of the
852 * other active states.
853 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800854 public static final int PROCESS_STATE_CACHED = 6;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700855 /**
856 * Total number of process states we track.
857 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800858 public static final int NUM_PROCESS_STATE = 7;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700859
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800860 // Used in dump
Dianne Hackborn61659e52014-07-09 16:13:01 -0700861 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800862 "Top", "Fg Service", "Foreground", "Background", "Top Sleeping", "Heavy Weight",
863 "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700864 };
865
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800866 // Used in checkin dump
867 @VisibleForTesting
868 public static final String[] UID_PROCESS_TYPES = {
869 "T", // TOP
870 "FS", // FOREGROUND_SERVICE
871 "F", // FOREGROUND
872 "B", // BACKGROUND
873 "TS", // TOP_SLEEPING
874 "HW", // HEAVY_WEIGHT
875 "C" // CACHED
876 };
877
878 /**
879 * When the process exits one of these states, we need to make sure cpu time in this state
880 * is not attributed to any non-critical process states.
881 */
882 public static final int[] CRITICAL_PROC_STATES = {
Amith Yamasania0a30a12019-01-22 11:38:06 -0800883 PROCESS_STATE_TOP,
Amith Yamasanif235d0b2019-03-20 22:49:43 -0700884 PROCESS_STATE_FOREGROUND_SERVICE_LOCATION,
885 PROCESS_STATE_BOUND_TOP, PROCESS_STATE_FOREGROUND_SERVICE,
Amith Yamasania0a30a12019-01-22 11:38:06 -0800886 PROCESS_STATE_FOREGROUND
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800887 };
888
Dianne Hackborn61659e52014-07-09 16:13:01 -0700889 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800890 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700891
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800892 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893
Robert Greenwalta029ea12013-09-25 16:38:12 -0700894 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
895
Dianne Hackborn617f8772009-03-31 15:04:46 -0700896 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700897 * Note that these must match the constants in android.os.PowerManager.
898 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
899 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700900 */
901 static final String[] USER_ACTIVITY_TYPES = {
Lucas Dupin0a5d7972019-01-16 18:52:30 -0800902 "other", "button", "touch", "accessibility", "attention"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700903 };
Bookatzc8c44962017-05-11 12:12:54 -0700904
Lucas Dupin0a5d7972019-01-16 18:52:30 -0800905 public static final int NUM_USER_ACTIVITY_TYPES = USER_ACTIVITY_TYPES.length;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700906
Dianne Hackborn617f8772009-03-31 15:04:46 -0700907 public abstract void noteUserActivityLocked(int type);
908 public abstract boolean hasUserActivity();
909 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700910
911 public abstract boolean hasNetworkActivity();
Andrei Onea24ec3212019-03-15 17:35:05 +0000912 @UnsupportedAppUsage
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800913 public abstract long getNetworkActivityBytes(int type, int which);
914 public abstract long getNetworkActivityPackets(int type, int which);
Andrei Onea24ec3212019-03-15 17:35:05 +0000915 @UnsupportedAppUsage
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800916 public abstract long getMobileRadioActiveTime(int which);
917 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700918
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700919 /**
920 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
921 */
922 public abstract long getUserCpuTimeUs(int which);
923
924 /**
925 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
926 */
927 public abstract long getSystemCpuTimeUs(int which);
928
929 /**
Sudheer Shanka71f34b32017-07-21 00:14:24 -0700930 * Returns the approximate cpu time (in microseconds) spent at a certain CPU speed for a
Adam Lesinski6832f392015-09-05 18:05:40 -0700931 * given CPU cluster.
932 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700933 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700934 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700935 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
936 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700937 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700938 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700939
Adam Lesinski5f056f62016-07-14 16:56:08 -0700940 /**
941 * Returns the number of times this UID woke up the Application Processor to
942 * process a mobile radio packet.
943 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
944 */
945 public abstract long getMobileRadioApWakeupCount(int which);
946
947 /**
948 * Returns the number of times this UID woke up the Application Processor to
949 * process a WiFi packet.
950 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
951 */
952 public abstract long getWifiRadioApWakeupCount(int which);
953
Amith Yamasani977e11f2018-02-16 11:29:54 -0800954 /**
955 * Appends the deferred jobs data to the StringBuilder passed in, in checkin format
956 * @param sb StringBuilder that can be overwritten with the deferred jobs data
957 * @param which one of STATS_*
958 */
959 public abstract void getDeferredJobsCheckinLineLocked(StringBuilder sb, int which);
960
961 /**
962 * Appends the deferred jobs data to the StringBuilder passed in
963 * @param sb StringBuilder that can be overwritten with the deferred jobs data
964 * @param which one of STATS_*
965 */
966 public abstract void getDeferredJobsLineLocked(StringBuilder sb, int which);
967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 public static abstract class Sensor {
Artur Satayevee9c6e72019-11-04 15:58:52 +0000969
970 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
971 public Sensor() {
972 }
973
Mathias Agopian7f84c062013-02-04 19:22:47 -0800974 /*
975 * FIXME: it's not correct to use this magic value because it
976 * could clash with a sensor handle (which are defined by
977 * the sensor HAL, and therefore out of our control
978 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 // Magic sensor number for the GPS.
Andrei Onea24ec3212019-03-15 17:35:05 +0000980 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800982
Andrei Onea24ec3212019-03-15 17:35:05 +0000983 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800985
Andrei Onea24ec3212019-03-15 17:35:05 +0000986 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800988
Bookatz867c0d72017-03-07 18:23:42 -0800989 /** Returns a Timer for sensor usage when app is in the background. */
990 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 }
992
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700993 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800994 public int mWakeNesting;
995 public long mWakeSumMs;
996 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700997 }
998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 /**
1000 * The statistics associated with a particular process.
1001 */
1002 public static abstract class Proc {
1003
Artur Satayevee9c6e72019-11-04 15:58:52 +00001004 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
1005 public Proc() {
1006 }
1007
Dianne Hackborn287952c2010-09-22 22:34:31 -07001008 public static class ExcessivePower {
Artur Satayevfc46be72019-11-04 17:50:59 +00001009
1010 @UnsupportedAppUsage
1011 public ExcessivePower() {
1012 }
1013
Dianne Hackborn287952c2010-09-22 22:34:31 -07001014 public static final int TYPE_WAKE = 1;
1015 public static final int TYPE_CPU = 2;
1016
Andrei Onea24ec3212019-03-15 17:35:05 +00001017 @UnsupportedAppUsage
Dianne Hackborn287952c2010-09-22 22:34:31 -07001018 public int type;
Andrei Onea24ec3212019-03-15 17:35:05 +00001019 @UnsupportedAppUsage
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001020 public long overTime;
Andrei Onea24ec3212019-03-15 17:35:05 +00001021 @UnsupportedAppUsage
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001022 public long usedTime;
1023 }
1024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08001026 * Returns true if this process is still active in the battery stats.
1027 */
1028 public abstract boolean isActive();
1029
1030 /**
Adam Lesinski33dac552015-03-09 15:24:48 -07001031 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001033 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001035 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 public abstract long getUserTime(int which);
1037
1038 /**
Adam Lesinski33dac552015-03-09 15:24:48 -07001039 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001041 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001043 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 public abstract long getSystemTime(int which);
1045
1046 /**
1047 * Returns the number of times the process has been started.
1048 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001049 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001051 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001053
1054 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001055 * Returns the number of times the process has crashed.
1056 *
1057 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
1058 */
1059 public abstract int getNumCrashes(int which);
1060
1061 /**
1062 * Returns the number of times the process has ANRed.
1063 *
1064 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
1065 */
1066 public abstract int getNumAnrs(int which);
1067
1068 /**
Adam Lesinski33dac552015-03-09 15:24:48 -07001069 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001070 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001071 * @return foreground cpu time in microseconds
1072 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001073 @UnsupportedAppUsage
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001074 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -07001075
Andrei Onea24ec3212019-03-15 17:35:05 +00001076 @UnsupportedAppUsage
Dianne Hackborn287952c2010-09-22 22:34:31 -07001077 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001078
Andrei Onea24ec3212019-03-15 17:35:05 +00001079 @UnsupportedAppUsage
Dianne Hackborn287952c2010-09-22 22:34:31 -07001080 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 }
1082
1083 /**
1084 * The statistics associated with a particular package.
1085 */
1086 public static abstract class Pkg {
1087
Artur Satayevee9c6e72019-11-04 15:58:52 +00001088 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
1089 public Pkg() {
1090 }
1091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001093 * Returns information about all wakeup alarms that have been triggered for this
1094 * package. The mapping keys are tag names for the alarms, the counter contains
1095 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001097 @UnsupportedAppUsage
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001098 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099
1100 /**
1101 * Returns a mapping containing service statistics.
1102 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001103 @UnsupportedAppUsage
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001104 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105
1106 /**
1107 * The statistics associated with a particular service.
1108 */
Joe Onoratoabded112016-02-08 16:49:39 -08001109 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110
1111 /**
1112 * Returns the amount of time spent started.
1113 *
1114 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001115 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 * @return
1117 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001118 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 public abstract long getStartTime(long batteryUptime, int which);
1120
1121 /**
1122 * Returns the total number of times startService() has been called.
1123 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001124 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001126 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 public abstract int getStarts(int which);
1128
1129 /**
1130 * Returns the total number times the service has been launched.
1131 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001132 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001134 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 public abstract int getLaunches(int which);
1136 }
1137 }
1138 }
1139
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001140 public static final class LevelStepTracker {
1141 public long mLastStepTime = -1;
1142 public int mNumStepDurations;
1143 public final long[] mStepDurations;
1144
1145 public LevelStepTracker(int maxLevelSteps) {
1146 mStepDurations = new long[maxLevelSteps];
1147 }
1148
1149 public LevelStepTracker(int numSteps, long[] steps) {
1150 mNumStepDurations = numSteps;
1151 mStepDurations = new long[numSteps];
1152 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
1153 }
1154
1155 public long getDurationAt(int index) {
1156 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
1157 }
1158
1159 public int getLevelAt(int index) {
1160 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
1161 >> STEP_LEVEL_LEVEL_SHIFT);
1162 }
1163
1164 public int getInitModeAt(int index) {
1165 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
1166 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1167 }
1168
1169 public int getModModeAt(int index) {
1170 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
1171 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1172 }
1173
1174 private void appendHex(long val, int topOffset, StringBuilder out) {
1175 boolean hasData = false;
1176 while (topOffset >= 0) {
1177 int digit = (int)( (val>>topOffset) & 0xf );
1178 topOffset -= 4;
1179 if (!hasData && digit == 0) {
1180 continue;
1181 }
1182 hasData = true;
1183 if (digit >= 0 && digit <= 9) {
1184 out.append((char)('0' + digit));
1185 } else {
1186 out.append((char)('a' + digit - 10));
1187 }
1188 }
1189 }
1190
1191 public void encodeEntryAt(int index, StringBuilder out) {
1192 long item = mStepDurations[index];
1193 long duration = item & STEP_LEVEL_TIME_MASK;
1194 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
1195 >> STEP_LEVEL_LEVEL_SHIFT);
1196 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
1197 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1198 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
1199 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1200 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1201 case Display.STATE_OFF: out.append('f'); break;
1202 case Display.STATE_ON: out.append('o'); break;
1203 case Display.STATE_DOZE: out.append('d'); break;
1204 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
1205 }
1206 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1207 out.append('p');
1208 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001209 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1210 out.append('i');
1211 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001212 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1213 case Display.STATE_OFF: out.append('F'); break;
1214 case Display.STATE_ON: out.append('O'); break;
1215 case Display.STATE_DOZE: out.append('D'); break;
1216 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
1217 }
1218 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1219 out.append('P');
1220 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001221 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1222 out.append('I');
1223 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001224 out.append('-');
1225 appendHex(level, 4, out);
1226 out.append('-');
1227 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
1228 }
1229
1230 public void decodeEntryAt(int index, String value) {
1231 final int N = value.length();
1232 int i = 0;
1233 char c;
1234 long out = 0;
1235 while (i < N && (c=value.charAt(i)) != '-') {
1236 i++;
1237 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001238 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001239 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001240 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001241 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001242 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001243 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001244 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1245 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1246 break;
1247 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1248 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1249 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001250 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1251 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1252 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001253 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1254 break;
1255 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1256 break;
1257 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1258 break;
1259 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1260 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1261 break;
1262 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1263 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001264 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001265 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1266 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1267 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001268 }
1269 }
1270 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001271 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001272 while (i < N && (c=value.charAt(i)) != '-') {
1273 i++;
1274 level <<= 4;
1275 if (c >= '0' && c <= '9') {
1276 level += c - '0';
1277 } else if (c >= 'a' && c <= 'f') {
1278 level += c - 'a' + 10;
1279 } else if (c >= 'A' && c <= 'F') {
1280 level += c - 'A' + 10;
1281 }
1282 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001283 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001284 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
1285 long duration = 0;
1286 while (i < N && (c=value.charAt(i)) != '-') {
1287 i++;
1288 duration <<= 4;
1289 if (c >= '0' && c <= '9') {
1290 duration += c - '0';
1291 } else if (c >= 'a' && c <= 'f') {
1292 duration += c - 'a' + 10;
1293 } else if (c >= 'A' && c <= 'F') {
1294 duration += c - 'A' + 10;
1295 }
1296 }
1297 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
1298 }
1299
1300 public void init() {
1301 mLastStepTime = -1;
1302 mNumStepDurations = 0;
1303 }
1304
1305 public void clearTime() {
1306 mLastStepTime = -1;
1307 }
1308
1309 public long computeTimePerLevel() {
1310 final long[] steps = mStepDurations;
1311 final int numSteps = mNumStepDurations;
1312
1313 // For now we'll do a simple average across all steps.
1314 if (numSteps <= 0) {
1315 return -1;
1316 }
1317 long total = 0;
1318 for (int i=0; i<numSteps; i++) {
1319 total += steps[i] & STEP_LEVEL_TIME_MASK;
1320 }
1321 return total / numSteps;
1322 /*
1323 long[] buckets = new long[numSteps];
1324 int numBuckets = 0;
1325 int numToAverage = 4;
1326 int i = 0;
1327 while (i < numSteps) {
1328 long totalTime = 0;
1329 int num = 0;
1330 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1331 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1332 num++;
1333 }
1334 buckets[numBuckets] = totalTime / num;
1335 numBuckets++;
1336 numToAverage *= 2;
1337 i += num;
1338 }
1339 if (numBuckets < 1) {
1340 return -1;
1341 }
1342 long averageTime = buckets[numBuckets-1];
1343 for (i=numBuckets-2; i>=0; i--) {
1344 averageTime = (averageTime + buckets[i]) / 2;
1345 }
1346 return averageTime;
1347 */
1348 }
1349
1350 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1351 int[] outNumOfInterest) {
1352 final long[] steps = mStepDurations;
1353 final int count = mNumStepDurations;
1354 if (count <= 0) {
1355 return -1;
1356 }
1357 long total = 0;
1358 int numOfInterest = 0;
1359 for (int i=0; i<count; i++) {
1360 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1361 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1362 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1363 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1364 // If the modes of interest didn't change during this step period...
1365 if ((modMode&modesOfInterest) == 0) {
1366 // And the mode values during this period match those we are measuring...
1367 if ((initMode&modesOfInterest) == modeValues) {
1368 // Then this can be used to estimate the total time!
1369 numOfInterest++;
1370 total += steps[i] & STEP_LEVEL_TIME_MASK;
1371 }
1372 }
1373 }
1374 if (numOfInterest <= 0) {
1375 return -1;
1376 }
1377
1378 if (outNumOfInterest != null) {
1379 outNumOfInterest[0] = numOfInterest;
1380 }
1381
1382 // The estimated time is the average time we spend in each level, multipled
1383 // by 100 -- the total number of battery levels
1384 return (total / numOfInterest) * 100;
1385 }
1386
1387 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1388 int stepCount = mNumStepDurations;
1389 final long lastStepTime = mLastStepTime;
1390 if (lastStepTime >= 0 && numStepLevels > 0) {
1391 final long[] steps = mStepDurations;
1392 long duration = elapsedRealtime - lastStepTime;
1393 for (int i=0; i<numStepLevels; i++) {
1394 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1395 long thisDuration = duration / (numStepLevels-i);
1396 duration -= thisDuration;
1397 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1398 thisDuration = STEP_LEVEL_TIME_MASK;
1399 }
1400 steps[0] = thisDuration | modeBits;
1401 }
1402 stepCount += numStepLevels;
1403 if (stepCount > steps.length) {
1404 stepCount = steps.length;
1405 }
1406 }
1407 mNumStepDurations = stepCount;
1408 mLastStepTime = elapsedRealtime;
1409 }
1410
1411 public void readFromParcel(Parcel in) {
1412 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001413 if (N > mStepDurations.length) {
1414 throw new ParcelFormatException("more step durations than available: " + N);
1415 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001416 mNumStepDurations = N;
1417 for (int i=0; i<N; i++) {
1418 mStepDurations[i] = in.readLong();
1419 }
1420 }
1421
1422 public void writeToParcel(Parcel out) {
1423 final int N = mNumStepDurations;
1424 out.writeInt(N);
1425 for (int i=0; i<N; i++) {
1426 out.writeLong(mStepDurations[i]);
1427 }
1428 }
1429 }
1430
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001431 public static final class PackageChange {
1432 public String mPackageName;
1433 public boolean mUpdate;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001434 public long mVersionCode;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001435 }
1436
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001437 public static final class DailyItem {
1438 public long mStartTime;
1439 public long mEndTime;
1440 public LevelStepTracker mDischargeSteps;
1441 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001442 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001443 }
1444
1445 public abstract DailyItem getDailyItemLocked(int daysAgo);
1446
1447 public abstract long getCurrentDailyStartTime();
1448
1449 public abstract long getNextMinDailyDeadline();
1450
1451 public abstract long getNextMaxDailyDeadline();
1452
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001453 public abstract long[] getCpuFreqs();
1454
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001455 public final static class HistoryTag {
1456 public String string;
1457 public int uid;
1458
1459 public int poolIdx;
1460
1461 public void setTo(HistoryTag o) {
1462 string = o.string;
1463 uid = o.uid;
1464 poolIdx = o.poolIdx;
1465 }
1466
1467 public void setTo(String _string, int _uid) {
1468 string = _string;
1469 uid = _uid;
1470 poolIdx = -1;
1471 }
1472
1473 public void writeToParcel(Parcel dest, int flags) {
1474 dest.writeString(string);
1475 dest.writeInt(uid);
1476 }
1477
1478 public void readFromParcel(Parcel src) {
1479 string = src.readString();
1480 uid = src.readInt();
1481 poolIdx = -1;
1482 }
1483
1484 @Override
1485 public boolean equals(Object o) {
1486 if (this == o) return true;
1487 if (o == null || getClass() != o.getClass()) return false;
1488
1489 HistoryTag that = (HistoryTag) o;
1490
1491 if (uid != that.uid) return false;
1492 if (!string.equals(that.string)) return false;
1493
1494 return true;
1495 }
1496
1497 @Override
1498 public int hashCode() {
1499 int result = string.hashCode();
1500 result = 31 * result + uid;
1501 return result;
1502 }
1503 }
1504
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001505 /**
1506 * Optional detailed information that can go into a history step. This is typically
1507 * generated each time the battery level changes.
1508 */
1509 public final static class HistoryStepDetails {
1510 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1511 public int userTime;
1512 public int systemTime;
1513
1514 // Top three apps using CPU in the last step, with times in 1/100 second.
1515 public int appCpuUid1;
1516 public int appCpuUTime1;
1517 public int appCpuSTime1;
1518 public int appCpuUid2;
1519 public int appCpuUTime2;
1520 public int appCpuSTime2;
1521 public int appCpuUid3;
1522 public int appCpuUTime3;
1523 public int appCpuSTime3;
1524
1525 // Information from /proc/stat
1526 public int statUserTime;
1527 public int statSystemTime;
1528 public int statIOWaitTime;
1529 public int statIrqTime;
1530 public int statSoftIrqTime;
1531 public int statIdlTime;
1532
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001533 // Platform-level low power state stats
1534 public String statPlatformIdleState;
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001535 public String statSubsystemPowerState;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001536
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001537 public HistoryStepDetails() {
1538 clear();
1539 }
1540
1541 public void clear() {
1542 userTime = systemTime = 0;
1543 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1544 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1545 = appCpuUTime3 = appCpuSTime3 = 0;
1546 }
1547
1548 public void writeToParcel(Parcel out) {
1549 out.writeInt(userTime);
1550 out.writeInt(systemTime);
1551 out.writeInt(appCpuUid1);
1552 out.writeInt(appCpuUTime1);
1553 out.writeInt(appCpuSTime1);
1554 out.writeInt(appCpuUid2);
1555 out.writeInt(appCpuUTime2);
1556 out.writeInt(appCpuSTime2);
1557 out.writeInt(appCpuUid3);
1558 out.writeInt(appCpuUTime3);
1559 out.writeInt(appCpuSTime3);
1560 out.writeInt(statUserTime);
1561 out.writeInt(statSystemTime);
1562 out.writeInt(statIOWaitTime);
1563 out.writeInt(statIrqTime);
1564 out.writeInt(statSoftIrqTime);
1565 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001566 out.writeString(statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001567 out.writeString(statSubsystemPowerState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001568 }
1569
1570 public void readFromParcel(Parcel in) {
1571 userTime = in.readInt();
1572 systemTime = in.readInt();
1573 appCpuUid1 = in.readInt();
1574 appCpuUTime1 = in.readInt();
1575 appCpuSTime1 = in.readInt();
1576 appCpuUid2 = in.readInt();
1577 appCpuUTime2 = in.readInt();
1578 appCpuSTime2 = in.readInt();
1579 appCpuUid3 = in.readInt();
1580 appCpuUTime3 = in.readInt();
1581 appCpuSTime3 = in.readInt();
1582 statUserTime = in.readInt();
1583 statSystemTime = in.readInt();
1584 statIOWaitTime = in.readInt();
1585 statIrqTime = in.readInt();
1586 statSoftIrqTime = in.readInt();
1587 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001588 statPlatformIdleState = in.readString();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001589 statSubsystemPowerState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001590 }
1591 }
1592
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001593 public final static class HistoryItem implements Parcelable {
Artur Satayevee9c6e72019-11-04 15:58:52 +00001594 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001595 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001596
1597 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Andrei Onea24ec3212019-03-15 17:35:05 +00001598 @UnsupportedAppUsage
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001599 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001600
Andrei Onea24ec3212019-03-15 17:35:05 +00001601 @UnsupportedAppUsage
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001602 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001603 public static final byte CMD_NULL = -1;
1604 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001605 public static final byte CMD_CURRENT_TIME = 5;
1606 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001607 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001608 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001609
Andrei Onea24ec3212019-03-15 17:35:05 +00001610 @UnsupportedAppUsage
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001611 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001612
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001613 /**
1614 * Return whether the command code is a delta data update.
1615 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001616 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001617 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001618 }
1619
Andrei Onea24ec3212019-03-15 17:35:05 +00001620 @UnsupportedAppUsage
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001621 public byte batteryLevel;
Andrei Onea24ec3212019-03-15 17:35:05 +00001622 @UnsupportedAppUsage
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001623 public byte batteryStatus;
Andrei Onea24ec3212019-03-15 17:35:05 +00001624 @UnsupportedAppUsage
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001625 public byte batteryHealth;
Andrei Onea24ec3212019-03-15 17:35:05 +00001626 @UnsupportedAppUsage
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001627 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001628
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001629 public short batteryTemperature;
Andrei Onea24ec3212019-03-15 17:35:05 +00001630 @UnsupportedAppUsage
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001631 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001632
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001633 // The charge of the battery in micro-Ampere-hours.
1634 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001635
Blake Kragtencb308d92019-01-28 10:54:13 -08001636 public double modemRailChargeMah;
1637 public double wifiRailChargeMah;
1638
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001639 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001640 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001641 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001642 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001643 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1644 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001645 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001646 public static final int STATE_PHONE_STATE_SHIFT = 6;
1647 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001648 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001649 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001650 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001651
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001652 // These states always appear directly in the first int token
1653 // of a delta change; they should be ones that change relatively
1654 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001655 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1656 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001657 public static final int STATE_GPS_ON_FLAG = 1<<29;
1658 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001659 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001660 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001661 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001662 // Do not use, this is used for coulomb delta count.
1663 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001664 // These are on the lower bits used for the command; if they change
1665 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001666 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001667 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001668 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001669 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1670 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
Mike Mac2f518a2017-09-19 16:06:03 -07001671 public static final int STATE_SCREEN_DOZE_FLAG = 1 << 18;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001672 // empty slot
1673 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001674
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001675 public static final int MOST_INTERESTING_STATES =
Mike Mac2f518a2017-09-19 16:06:03 -07001676 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG | STATE_SCREEN_DOZE_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001677
1678 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001679
Andrei Onea24ec3212019-03-15 17:35:05 +00001680 @UnsupportedAppUsage
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001681 public int states;
1682
Dianne Hackborn3251b902014-06-20 14:40:53 -07001683 // Constants from WIFI_SUPPL_STATE_*
1684 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1685 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1686 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1687 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1688 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1689 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
Siddharth Ray78ccaf52017-12-23 16:16:21 -08001690 // Values for NUM_GPS_SIGNAL_QUALITY_LEVELS
1691 public static final int STATE2_GPS_SIGNAL_QUALITY_SHIFT = 7;
1692 public static final int STATE2_GPS_SIGNAL_QUALITY_MASK =
1693 0x1 << STATE2_GPS_SIGNAL_QUALITY_SHIFT;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001694
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001695 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001696 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1697 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1698 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001699 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001700 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1701 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1702 public static final int STATE2_CHARGING_FLAG = 1<<24;
1703 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1704 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1705 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001706 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Siddharth Rayf5e796a2018-01-22 18:18:17 -08001707 public static final int STATE2_CELLULAR_HIGH_TX_POWER_FLAG = 1 << 19;
Mike Ma926a97c2018-03-25 02:32:35 -07001708 public static final int STATE2_USB_DATA_LINK_FLAG = 1 << 18;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001709
1710 public static final int MOST_INTERESTING_STATES2 =
Mike Mac2f518a2017-09-19 16:06:03 -07001711 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
1712 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001713
1714 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001715
Andrei Onea24ec3212019-03-15 17:35:05 +00001716 @UnsupportedAppUsage
Dianne Hackborn40c87252014-03-19 16:55:40 -07001717 public int states2;
1718
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001719 // The wake lock that was acquired at this point.
1720 public HistoryTag wakelockTag;
1721
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001722 // Kernel wakeup reason at this point.
1723 public HistoryTag wakeReasonTag;
1724
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001725 // Non-null when there is more detailed information at this step.
1726 public HistoryStepDetails stepDetails;
1727
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001728 public static final int EVENT_FLAG_START = 0x8000;
1729 public static final int EVENT_FLAG_FINISH = 0x4000;
1730
1731 // No event in this item.
1732 public static final int EVENT_NONE = 0x0000;
1733 // Event is about a process that is running.
1734 public static final int EVENT_PROC = 0x0001;
1735 // Event is about an application package that is in the foreground.
1736 public static final int EVENT_FOREGROUND = 0x0002;
1737 // Event is about an application package that is at the top of the screen.
1738 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001739 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001740 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001741 // Events for all additional wake locks aquired/release within a wake block.
1742 // These are not generated by default.
1743 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001744 // Event is about an application executing a scheduled job.
1745 public static final int EVENT_JOB = 0x0006;
1746 // Events for users running.
1747 public static final int EVENT_USER_RUNNING = 0x0007;
1748 // Events for foreground user.
1749 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001750 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001751 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001752 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001753 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001754 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001755 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001756 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001757 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001758 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001759 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001760 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001761 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001762 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001763 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001764 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001765 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001766 // Event for a package being on the temporary whitelist.
1767 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001768 // Event for the screen waking up.
1769 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001770 // Event for the UID that woke up the application processor.
1771 // Used for wakeups coming from WiFi, modem, etc.
1772 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001773 // Event for reporting that a specific partial wake lock has been held for a long duration.
1774 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001775
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001776 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001777 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001778 // Mask to extract out only the type part of the event.
1779 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001780
1781 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1782 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1783 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1784 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1785 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1786 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001787 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1788 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001789 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1790 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001791 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1792 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1793 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1794 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1795 public static final int EVENT_USER_FOREGROUND_START =
1796 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1797 public static final int EVENT_USER_FOREGROUND_FINISH =
1798 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001799 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1800 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001801 public static final int EVENT_TEMP_WHITELIST_START =
1802 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1803 public static final int EVENT_TEMP_WHITELIST_FINISH =
1804 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001805 public static final int EVENT_LONG_WAKE_LOCK_START =
1806 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1807 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1808 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001809
1810 // For CMD_EVENT.
1811 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001812 public HistoryTag eventTag;
1813
Dianne Hackborn9a755432014-05-15 17:05:22 -07001814 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001815 public long currentTime;
1816
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001817 // Meta-data when reading.
1818 public int numReadInts;
1819
1820 // Pre-allocated objects.
1821 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001822 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001823 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001824
Andrei Onea24ec3212019-03-15 17:35:05 +00001825 @UnsupportedAppUsage
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001826 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001827 }
Bookatzc8c44962017-05-11 12:12:54 -07001828
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001829 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001830 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001831 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001832 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001833 }
Bookatzc8c44962017-05-11 12:12:54 -07001834
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001835 public int describeContents() {
1836 return 0;
1837 }
1838
1839 public void writeToParcel(Parcel dest, int flags) {
1840 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001841 int bat = (((int)cmd)&0xff)
1842 | ((((int)batteryLevel)<<8)&0xff00)
1843 | ((((int)batteryStatus)<<16)&0xf0000)
1844 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001845 | ((((int)batteryPlugType)<<24)&0xf000000)
1846 | (wakelockTag != null ? 0x10000000 : 0)
1847 | (wakeReasonTag != null ? 0x20000000 : 0)
1848 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001849 dest.writeInt(bat);
1850 bat = (((int)batteryTemperature)&0xffff)
1851 | ((((int)batteryVoltage)<<16)&0xffff0000);
1852 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001853 dest.writeInt(batteryChargeUAh);
Blake Kragtencb308d92019-01-28 10:54:13 -08001854 dest.writeDouble(modemRailChargeMah);
1855 dest.writeDouble(wifiRailChargeMah);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001856 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001857 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001858 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001859 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001860 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001861 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001862 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001863 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001864 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001865 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001866 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001867 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001868 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001869 dest.writeLong(currentTime);
1870 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001871 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001872
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001873 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001874 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001875 int bat = src.readInt();
1876 cmd = (byte)(bat&0xff);
1877 batteryLevel = (byte)((bat>>8)&0xff);
1878 batteryStatus = (byte)((bat>>16)&0xf);
1879 batteryHealth = (byte)((bat>>20)&0xf);
1880 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001881 int bat2 = src.readInt();
1882 batteryTemperature = (short)(bat2&0xffff);
1883 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001884 batteryChargeUAh = src.readInt();
Blake Kragtencb308d92019-01-28 10:54:13 -08001885 modemRailChargeMah = src.readDouble();
1886 wifiRailChargeMah = src.readDouble();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001887 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001888 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001889 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001890 wakelockTag = localWakelockTag;
1891 wakelockTag.readFromParcel(src);
1892 } else {
1893 wakelockTag = null;
1894 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001895 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001896 wakeReasonTag = localWakeReasonTag;
1897 wakeReasonTag.readFromParcel(src);
1898 } else {
1899 wakeReasonTag = null;
1900 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001901 if ((bat&0x40000000) != 0) {
1902 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001903 eventTag = localEventTag;
1904 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001905 } else {
1906 eventCode = EVENT_NONE;
1907 eventTag = null;
1908 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001909 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001910 currentTime = src.readLong();
1911 } else {
1912 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001913 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001914 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001915 }
1916
Artur Satayevee9c6e72019-11-04 15:58:52 +00001917 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001918 public void clear() {
1919 time = 0;
1920 cmd = CMD_NULL;
1921 batteryLevel = 0;
1922 batteryStatus = 0;
1923 batteryHealth = 0;
1924 batteryPlugType = 0;
1925 batteryTemperature = 0;
1926 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001927 batteryChargeUAh = 0;
Blake Kragtencb308d92019-01-28 10:54:13 -08001928 modemRailChargeMah = 0;
1929 wifiRailChargeMah = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001930 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001931 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001932 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001933 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001934 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001935 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001936 }
Bookatzc8c44962017-05-11 12:12:54 -07001937
Artur Satayevee9c6e72019-11-04 15:58:52 +00001938 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001939 public void setTo(HistoryItem o) {
1940 time = o.time;
1941 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001942 setToCommon(o);
1943 }
1944
Artur Satayevee9c6e72019-11-04 15:58:52 +00001945 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001946 public void setTo(long time, byte cmd, HistoryItem o) {
1947 this.time = time;
1948 this.cmd = cmd;
1949 setToCommon(o);
1950 }
1951
1952 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001953 batteryLevel = o.batteryLevel;
1954 batteryStatus = o.batteryStatus;
1955 batteryHealth = o.batteryHealth;
1956 batteryPlugType = o.batteryPlugType;
1957 batteryTemperature = o.batteryTemperature;
1958 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001959 batteryChargeUAh = o.batteryChargeUAh;
Blake Kragtencb308d92019-01-28 10:54:13 -08001960 modemRailChargeMah = o.modemRailChargeMah;
1961 wifiRailChargeMah = o.wifiRailChargeMah;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001962 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001963 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001964 if (o.wakelockTag != null) {
1965 wakelockTag = localWakelockTag;
1966 wakelockTag.setTo(o.wakelockTag);
1967 } else {
1968 wakelockTag = null;
1969 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001970 if (o.wakeReasonTag != null) {
1971 wakeReasonTag = localWakeReasonTag;
1972 wakeReasonTag.setTo(o.wakeReasonTag);
1973 } else {
1974 wakeReasonTag = null;
1975 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001976 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001977 if (o.eventTag != null) {
1978 eventTag = localEventTag;
1979 eventTag.setTo(o.eventTag);
1980 } else {
1981 eventTag = null;
1982 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001983 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001984 }
1985
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001986 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001987 return batteryLevel == o.batteryLevel
1988 && batteryStatus == o.batteryStatus
1989 && batteryHealth == o.batteryHealth
1990 && batteryPlugType == o.batteryPlugType
1991 && batteryTemperature == o.batteryTemperature
1992 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001993 && batteryChargeUAh == o.batteryChargeUAh
Blake Kragtencb308d92019-01-28 10:54:13 -08001994 && modemRailChargeMah == o.modemRailChargeMah
1995 && wifiRailChargeMah == o.wifiRailChargeMah
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001996 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001997 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001998 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001999 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002000
Artur Satayevee9c6e72019-11-04 15:58:52 +00002001 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002002 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002003 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002004 return false;
2005 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002006 if (wakelockTag != o.wakelockTag) {
2007 if (wakelockTag == null || o.wakelockTag == null) {
2008 return false;
2009 }
2010 if (!wakelockTag.equals(o.wakelockTag)) {
2011 return false;
2012 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002013 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002014 if (wakeReasonTag != o.wakeReasonTag) {
2015 if (wakeReasonTag == null || o.wakeReasonTag == null) {
2016 return false;
2017 }
2018 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
2019 return false;
2020 }
2021 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002022 if (eventTag != o.eventTag) {
2023 if (eventTag == null || o.eventTag == null) {
2024 return false;
2025 }
2026 if (!eventTag.equals(o.eventTag)) {
2027 return false;
2028 }
2029 }
2030 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002031 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002032 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07002033
2034 public final static class HistoryEventTracker {
2035 private final HashMap<String, SparseIntArray>[] mActiveEvents
2036 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
2037
2038 public boolean updateState(int code, String name, int uid, int poolIdx) {
2039 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
2040 int idx = code&HistoryItem.EVENT_TYPE_MASK;
2041 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
2042 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002043 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07002044 mActiveEvents[idx] = active;
2045 }
2046 SparseIntArray uids = active.get(name);
2047 if (uids == null) {
2048 uids = new SparseIntArray();
2049 active.put(name, uids);
2050 }
2051 if (uids.indexOfKey(uid) >= 0) {
2052 // Already set, nothing to do!
2053 return false;
2054 }
2055 uids.put(uid, poolIdx);
2056 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
2057 int idx = code&HistoryItem.EVENT_TYPE_MASK;
2058 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
2059 if (active == null) {
2060 // not currently active, nothing to do.
2061 return false;
2062 }
2063 SparseIntArray uids = active.get(name);
2064 if (uids == null) {
2065 // not currently active, nothing to do.
2066 return false;
2067 }
2068 idx = uids.indexOfKey(uid);
2069 if (idx < 0) {
2070 // not currently active, nothing to do.
2071 return false;
2072 }
2073 uids.removeAt(idx);
2074 if (uids.size() <= 0) {
2075 active.remove(name);
2076 }
2077 }
2078 return true;
2079 }
2080
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002081 public void removeEvents(int code) {
2082 int idx = code&HistoryItem.EVENT_TYPE_MASK;
2083 mActiveEvents[idx] = null;
2084 }
2085
Dianne Hackborn37de0982014-05-09 09:32:18 -07002086 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
2087 return mActiveEvents[code];
2088 }
2089 }
2090
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002091 public static final class BitDescription {
2092 public final int mask;
2093 public final int shift;
2094 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002095 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002096 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002097 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07002098
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002099 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002100 this.mask = mask;
2101 this.shift = -1;
2102 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002103 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002104 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002105 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002106 }
Bookatzc8c44962017-05-11 12:12:54 -07002107
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002108 public BitDescription(int mask, int shift, String name, String shortName,
2109 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002110 this.mask = mask;
2111 this.shift = shift;
2112 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002113 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002114 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002115 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002116 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002117 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07002118
Dianne Hackbornfc064132014-06-02 12:42:12 -07002119 /**
2120 * Don't allow any more batching in to the current history event. This
2121 * is called when printing partial histories, so to ensure that the next
2122 * history event will go in to a new batch after what was printed in the
2123 * last partial history.
2124 */
2125 public abstract void commitCurrentHistoryBatchLocked();
2126
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002127 public abstract int getHistoryTotalSize();
2128
2129 public abstract int getHistoryUsedSize();
2130
Andrei Onea24ec3212019-03-15 17:35:05 +00002131 @UnsupportedAppUsage
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002132 public abstract boolean startIteratingHistoryLocked();
2133
Dianne Hackborn099bc622014-01-22 13:39:16 -08002134 public abstract int getHistoryStringPoolSize();
2135
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002136 public abstract int getHistoryStringPoolBytes();
2137
2138 public abstract String getHistoryTagPoolString(int index);
2139
2140 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002141
Andrei Onea24ec3212019-03-15 17:35:05 +00002142 @UnsupportedAppUsage
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002143 public abstract boolean getNextHistoryLocked(HistoryItem out);
2144
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002145 public abstract void finishIteratingHistoryLocked();
2146
2147 public abstract boolean startIteratingOldHistoryLocked();
2148
2149 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
2150
2151 public abstract void finishIteratingOldHistoryLocked();
2152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002154 * Return the base time offset for the battery history.
2155 */
2156 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07002157
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002158 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 * Returns the number of times the device has been started.
2160 */
2161 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07002162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002164 * 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 -08002165 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002166 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 * {@hide}
2168 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002169 @UnsupportedAppUsage
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002170 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002171
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002172 /**
2173 * Returns the number of times the screen was turned on.
2174 *
2175 * {@hide}
2176 */
2177 public abstract int getScreenOnCount(int which);
2178
Mike Mac2f518a2017-09-19 16:06:03 -07002179 /**
2180 * Returns the time in microseconds that the screen has been dozing while the device was
2181 * running on battery.
2182 *
2183 * {@hide}
2184 */
2185 public abstract long getScreenDozeTime(long elapsedRealtimeUs, int which);
2186
2187 /**
2188 * Returns the number of times the screen was turned dozing.
2189 *
2190 * {@hide}
2191 */
2192 public abstract int getScreenDozeCount(int which);
2193
Jeff Browne95c3cd2014-05-02 16:59:26 -07002194 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
2195
Dianne Hackborn617f8772009-03-31 15:04:46 -07002196 public static final int SCREEN_BRIGHTNESS_DARK = 0;
2197 public static final int SCREEN_BRIGHTNESS_DIM = 1;
2198 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
2199 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
2200 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07002201
Dianne Hackborn617f8772009-03-31 15:04:46 -07002202 static final String[] SCREEN_BRIGHTNESS_NAMES = {
2203 "dark", "dim", "medium", "light", "bright"
2204 };
Bookatzc8c44962017-05-11 12:12:54 -07002205
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002206 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
2207 "0", "1", "2", "3", "4"
2208 };
2209
Andrei Onea24ec3212019-03-15 17:35:05 +00002210 @UnsupportedAppUsage
Dianne Hackborn617f8772009-03-31 15:04:46 -07002211 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07002212
Dianne Hackborn617f8772009-03-31 15:04:46 -07002213 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002214 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07002215 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07002216 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002217 * {@hide}
2218 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002219 @UnsupportedAppUsage
Dianne Hackborn617f8772009-03-31 15:04:46 -07002220 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002221 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002224 * Returns the {@link Timer} object that tracks the given screen brightness.
2225 *
2226 * {@hide}
2227 */
2228 public abstract Timer getScreenBrightnessTimer(int brightnessBin);
2229
2230 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002231 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002232 * running on battery.
2233 *
2234 * {@hide}
2235 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002236 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002237
2238 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002239 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002240 *
2241 * {@hide}
2242 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002243 public abstract int getPowerSaveModeEnabledCount(int which);
2244
2245 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002246 * Constant for device idle mode: not active.
2247 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002248 public static final int DEVICE_IDLE_MODE_OFF = ServerProtoEnums.DEVICE_IDLE_MODE_OFF; // 0
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002249
2250 /**
2251 * Constant for device idle mode: active in lightweight mode.
2252 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002253 public static final int DEVICE_IDLE_MODE_LIGHT = ServerProtoEnums.DEVICE_IDLE_MODE_LIGHT; // 1
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002254
2255 /**
2256 * Constant for device idle mode: active in full mode.
2257 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002258 public static final int DEVICE_IDLE_MODE_DEEP = ServerProtoEnums.DEVICE_IDLE_MODE_DEEP; // 2
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002259
2260 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002261 * Returns the time in microseconds that device has been in idle mode while
2262 * running on battery.
2263 *
2264 * {@hide}
2265 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002266 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002267
2268 /**
2269 * Returns the number of times that the devie has gone in to idle mode.
2270 *
2271 * {@hide}
2272 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002273 public abstract int getDeviceIdleModeCount(int mode, int which);
2274
2275 /**
2276 * Return the longest duration we spent in a particular device idle mode (fully in the
2277 * mode, not in idle maintenance etc).
2278 */
2279 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002280
2281 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002282 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002283 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002284 * counts all of the time that we consider the device to be idle, whether or not
2285 * it is currently in the actual device idle mode.
2286 *
2287 * {@hide}
2288 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002289 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002290
2291 /**
Bookatz8c6571b2017-10-24 15:04:41 -07002292 * Returns the number of times that the device has started idling.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002293 *
2294 * {@hide}
2295 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002296 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002297
2298 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002299 * Returns the number of times that connectivity state changed.
2300 *
2301 * {@hide}
2302 */
2303 public abstract int getNumConnectivityChange(int which);
2304
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002305
2306 /**
2307 * Returns the time in microseconds that the phone has been running with
2308 * the given GPS signal quality level
2309 *
2310 * {@hide}
2311 */
2312 public abstract long getGpsSignalQualityTime(int strengthBin,
2313 long elapsedRealtimeUs, int which);
2314
2315 /**
2316 * Returns the GPS battery drain in mA-ms
2317 *
2318 * {@hide}
2319 */
2320 public abstract long getGpsBatteryDrainMaMs();
2321
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002322 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002323 * 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 -08002324 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002325 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 * {@hide}
2327 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002328 @UnsupportedAppUsage
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002329 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002330
Dianne Hackborn627bba72009-03-24 22:32:56 -07002331 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002332 * Returns the number of times a phone call was activated.
2333 *
2334 * {@hide}
2335 */
2336 public abstract int getPhoneOnCount(int which);
2337
2338 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002339 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002340 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002341 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002342 * {@hide}
2343 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002344 @UnsupportedAppUsage
Dianne Hackborn627bba72009-03-24 22:32:56 -07002345 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002346 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002347
Dianne Hackborn617f8772009-03-31 15:04:46 -07002348 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07002349 * Returns the time in microseconds that the phone has been trying to
2350 * acquire a signal.
2351 *
2352 * {@hide}
2353 */
2354 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002355 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002356
2357 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002358 * Returns the {@link Timer} object that tracks how much the phone has been trying to
2359 * acquire a signal.
2360 *
2361 * {@hide}
2362 */
2363 public abstract Timer getPhoneSignalScanningTimer();
2364
2365 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002366 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002367 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002368 * {@hide}
2369 */
2370 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
2371
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002372 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002373 * Return the {@link Timer} object used to track the given signal strength's duration and
2374 * counts.
2375 */
2376 protected abstract Timer getPhoneSignalStrengthTimer(int strengthBin);
2377
2378 /**
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002379 * Returns the time in microseconds that the mobile network has been active
2380 * (in a high power state).
2381 *
2382 * {@hide}
2383 */
Artur Satayevee9c6e72019-11-04 15:58:52 +00002384 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002385 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002386
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002387 /**
2388 * Returns the number of times that the mobile network has transitioned to the
2389 * active state.
2390 *
2391 * {@hide}
2392 */
2393 public abstract int getMobileRadioActiveCount(int which);
2394
2395 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002396 * Returns the time in microseconds that is the difference between the mobile radio
2397 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
2398 * from the radio.
2399 *
2400 * {@hide}
2401 */
2402 public abstract long getMobileRadioActiveAdjustedTime(int which);
2403
2404 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002405 * Returns the time in microseconds that the mobile network has been active
2406 * (in a high power state) but not being able to blame on an app.
2407 *
2408 * {@hide}
2409 */
2410 public abstract long getMobileRadioActiveUnknownTime(int which);
2411
2412 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002413 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002414 *
2415 * {@hide}
2416 */
2417 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002418
Tej Singheee317b2018-03-07 19:28:05 -08002419 public static final int DATA_CONNECTION_NONE = 0;
2420 public static final int DATA_CONNECTION_OTHER = TelephonyManager.MAX_NETWORK_TYPE + 1;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002421
Dianne Hackborn627bba72009-03-24 22:32:56 -07002422 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002423 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002424 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Bookatzdf6baff2018-12-12 19:38:11 -08002425 "ehrpd", "hspap", "gsm", "td_scdma", "iwlan", "lte_ca", "nr",
2426 "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002427 };
Bookatzc8c44962017-05-11 12:12:54 -07002428
Andrei Onea24ec3212019-03-15 17:35:05 +00002429 @UnsupportedAppUsage
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002430 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002431
Dianne Hackborn627bba72009-03-24 22:32:56 -07002432 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002433 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002434 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002435 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002436 * {@hide}
2437 */
2438 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002439 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002442 * Returns the number of times the phone has entered the given data
2443 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002444 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002445 * {@hide}
2446 */
2447 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002448
Kweku Adams87b19ec2017-10-09 12:40:03 -07002449 /**
2450 * Returns the {@link Timer} object that tracks the phone's data connection type stats.
2451 */
2452 public abstract Timer getPhoneDataConnectionTimer(int dataType);
2453
Dianne Hackborn3251b902014-06-20 14:40:53 -07002454 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2455 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2456 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2457 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2458 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2459 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2460 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2461 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2462 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2463 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2464 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2465 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2466 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2467
2468 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2469
2470 static final String[] WIFI_SUPPL_STATE_NAMES = {
2471 "invalid", "disconn", "disabled", "inactive", "scanning",
2472 "authenticating", "associating", "associated", "4-way-handshake",
2473 "group-handshake", "completed", "dormant", "uninit"
2474 };
2475
2476 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2477 "inv", "dsc", "dis", "inact", "scan",
2478 "auth", "ascing", "asced", "4-way",
2479 "group", "compl", "dorm", "uninit"
2480 };
2481
Mike Mac2f518a2017-09-19 16:06:03 -07002482 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002483 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002484 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2485 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002486 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002487 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2488 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2489 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002490 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002491 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002492 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002493 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002494 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2495 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Mike Mac2f518a2017-09-19 16:06:03 -07002496 new BitDescription(HistoryItem.STATE_SCREEN_DOZE_FLAG, "screen_doze", "Sd"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002497 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2498 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2499 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2500 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2501 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2502 new String[] {"in", "out", "emergency", "off"},
2503 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002504 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2505 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2506 SignalStrength.SIGNAL_STRENGTH_NAMES,
2507 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002508 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2509 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2510 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002511 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002512
Mike Ma926a97c2018-03-25 02:32:35 -07002513 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002514 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002515 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002516 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002517 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002518 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002519 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2520 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2521 new String[] { "off", "light", "full", "???" },
2522 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002523 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
Mike Ma926a97c2018-03-25 02:32:35 -07002524 new BitDescription(HistoryItem.STATE2_USB_DATA_LINK_FLAG, "usb_data", "Ud"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002525 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2526 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002527 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2528 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2529 new String[] { "0", "1", "2", "3", "4" },
2530 new String[] { "0", "1", "2", "3", "4" }),
2531 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2532 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2533 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002534 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002535 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Siddharth Rayf5e796a2018-01-22 18:18:17 -08002536 new BitDescription(HistoryItem.STATE2_CELLULAR_HIGH_TX_POWER_FLAG,
2537 "cellular_high_tx_power", "Chtp"),
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002538 new BitDescription(HistoryItem.STATE2_GPS_SIGNAL_QUALITY_MASK,
2539 HistoryItem.STATE2_GPS_SIGNAL_QUALITY_SHIFT, "gps_signal_quality", "Gss",
Siddharth Rayf5e796a2018-01-22 18:18:17 -08002540 new String[] { "poor", "good"}, new String[] { "poor", "good"})
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002541 };
2542
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002543 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002544 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002545 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2546 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002547 };
2548
2549 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002550 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002551 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002552 "Esw", "Ewa", "Elw", "Eec"
2553 };
2554
2555 @FunctionalInterface
2556 public interface IntToString {
2557 String applyAsString(int val);
2558 }
2559
2560 private static final IntToString sUidToString = UserHandle::formatUid;
2561 private static final IntToString sIntToString = Integer::toString;
2562
2563 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2564 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
Hirono Shiyama50ea4d02018-08-27 16:41:28 +09002565 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sIntToString,
Adam Lesinski041d9172016-12-12 12:03:56 -08002566 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2567 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002568 };
2569
Dianne Hackborn617f8772009-03-31 15:04:46 -07002570 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08002571 * Returns total time for WiFi Multicast Wakelock timer.
2572 * Note that this may be different from the sum of per uid timer values.
2573 *
2574 * {@hide}
2575 */
2576 public abstract long getWifiMulticastWakelockTime(long elapsedRealtimeUs, int which);
2577
2578 /**
2579 * Returns total time for WiFi Multicast Wakelock timer
2580 * Note that this may be different from the sum of per uid timer values.
2581 *
2582 * {@hide}
2583 */
2584 public abstract int getWifiMulticastWakelockCount(int which);
2585
2586 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002587 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002588 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002589 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002590 * {@hide}
2591 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002592 @UnsupportedAppUsage
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002593 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002594
2595 /**
Siddharth Rayb50a6842017-12-14 15:15:28 -08002596 * Returns the time in microseconds that wifi has been active while the device was
2597 * running on battery.
2598 *
2599 * {@hide}
2600 */
2601 public abstract long getWifiActiveTime(long elapsedRealtimeUs, int which);
2602
2603 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002604 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002605 * been in the running state while the device was running on battery.
2606 *
2607 * {@hide}
2608 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002609 @UnsupportedAppUsage
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002610 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002611
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002612 public static final int WIFI_STATE_OFF = 0;
2613 public static final int WIFI_STATE_OFF_SCANNING = 1;
2614 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2615 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2616 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2617 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2618 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2619 public static final int WIFI_STATE_SOFT_AP = 7;
2620
2621 static final String[] WIFI_STATE_NAMES = {
2622 "off", "scanning", "no_net", "disconn",
2623 "sta", "p2p", "sta_p2p", "soft_ap"
2624 };
2625
2626 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2627
2628 /**
2629 * Returns the time in microseconds that WiFi has been running in the given state.
2630 *
2631 * {@hide}
2632 */
2633 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002634 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002635
2636 /**
2637 * Returns the number of times that WiFi has entered the given state.
2638 *
2639 * {@hide}
2640 */
2641 public abstract int getWifiStateCount(int wifiState, int which);
2642
The Android Open Source Project10592532009-03-18 17:39:46 -07002643 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002644 * Returns the {@link Timer} object that tracks the given WiFi state.
2645 *
2646 * {@hide}
2647 */
2648 public abstract Timer getWifiStateTimer(int wifiState);
2649
2650 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002651 * Returns the time in microseconds that the wifi supplicant has been
2652 * in a given state.
2653 *
2654 * {@hide}
2655 */
2656 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2657
2658 /**
2659 * Returns the number of times that the wifi supplicant has transitioned
2660 * to a given state.
2661 *
2662 * {@hide}
2663 */
2664 public abstract int getWifiSupplStateCount(int state, int which);
2665
Kweku Adams87b19ec2017-10-09 12:40:03 -07002666 /**
2667 * Returns the {@link Timer} object that tracks the given wifi supplicant state.
2668 *
2669 * {@hide}
2670 */
2671 public abstract Timer getWifiSupplStateTimer(int state);
2672
Dianne Hackborn3251b902014-06-20 14:40:53 -07002673 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2674
2675 /**
2676 * Returns the time in microseconds that WIFI has been running with
2677 * the given signal strength.
2678 *
2679 * {@hide}
2680 */
2681 public abstract long getWifiSignalStrengthTime(int strengthBin,
2682 long elapsedRealtimeUs, int which);
2683
2684 /**
2685 * Returns the number of times WIFI has entered the given signal strength.
2686 *
2687 * {@hide}
2688 */
2689 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2690
2691 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002692 * Returns the {@link Timer} object that tracks the given WIFI signal strength.
2693 *
2694 * {@hide}
2695 */
2696 public abstract Timer getWifiSignalStrengthTimer(int strengthBin);
2697
2698 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002699 * Returns the time in microseconds that the flashlight has been on while the device was
2700 * running on battery.
2701 *
2702 * {@hide}
2703 */
2704 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2705
2706 /**
2707 * Returns the number of times that the flashlight has been turned on while the device was
2708 * running on battery.
2709 *
2710 * {@hide}
2711 */
2712 public abstract long getFlashlightOnCount(int which);
2713
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002714 /**
2715 * Returns the time in microseconds that the camera has been on while the device was
2716 * running on battery.
2717 *
2718 * {@hide}
2719 */
2720 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2721
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002722 /**
2723 * Returns the time in microseconds that bluetooth scans were running while the device was
2724 * on battery.
2725 *
2726 * {@hide}
2727 */
2728 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002729
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002730 public static final int NETWORK_MOBILE_RX_DATA = 0;
2731 public static final int NETWORK_MOBILE_TX_DATA = 1;
2732 public static final int NETWORK_WIFI_RX_DATA = 2;
2733 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002734 public static final int NETWORK_BT_RX_DATA = 4;
2735 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002736 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2737 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2738 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2739 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2740 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002741
Artur Satayevee9c6e72019-11-04 15:58:52 +00002742 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002743 public abstract long getNetworkActivityBytes(int type, int which);
2744 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002745
Adam Lesinskie08af192015-03-25 16:42:59 -07002746 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002747 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002748 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002749 * actual power data.
2750 */
2751 public abstract boolean hasWifiActivityReporting();
2752
2753 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002754 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2755 * in various radio controller states, such as transmit, receive, and idle.
2756 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002757 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002758 public abstract ControllerActivityCounter getWifiControllerActivity();
2759
2760 /**
2761 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2762 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2763 * actual power data.
2764 */
2765 public abstract boolean hasBluetoothActivityReporting();
2766
2767 /**
2768 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2769 * in various radio controller states, such as transmit, receive, and idle.
2770 * @return non-null {@link ControllerActivityCounter}
2771 */
2772 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2773
2774 /**
2775 * Returns true if the BatteryStats object has detailed modem power reports.
2776 * When true, calling {@link #getModemControllerActivity()} will yield the
2777 * actual power data.
2778 */
2779 public abstract boolean hasModemActivityReporting();
2780
2781 /**
2782 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2783 * in various radio controller states, such as transmit, receive, and idle.
2784 * @return non-null {@link ControllerActivityCounter}
2785 */
2786 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002787
The Android Open Source Project10592532009-03-18 17:39:46 -07002788 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002789 * Return the wall clock time when battery stats data collection started.
2790 */
2791 public abstract long getStartClockTime();
2792
2793 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002794 * Return platform version tag that we were running in when the battery stats started.
2795 */
2796 public abstract String getStartPlatformVersion();
2797
2798 /**
2799 * Return platform version tag that we were running in when the battery stats ended.
2800 */
2801 public abstract String getEndPlatformVersion();
2802
2803 /**
2804 * Return the internal version code of the parcelled format.
2805 */
2806 public abstract int getParcelVersion();
2807
2808 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 * Return whether we are currently running on battery.
2810 */
2811 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 /**
2814 * Returns a SparseArray containing the statistics for each uid.
2815 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002816 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 public abstract SparseArray<? extends Uid> getUidStats();
2818
2819 /**
2820 * Returns the current battery uptime in microseconds.
2821 *
2822 * @param curTime the amount of elapsed realtime in microseconds.
2823 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002824 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 public abstract long getBatteryUptime(long curTime);
2826
2827 /**
2828 * Returns the current battery realtime in microseconds.
2829 *
2830 * @param curTime the amount of elapsed realtime in microseconds.
2831 */
2832 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002833
The Android Open Source Project10592532009-03-18 17:39:46 -07002834 /**
Evan Millar633a1742009-04-02 16:36:33 -07002835 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002836 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002837 */
Evan Millar633a1742009-04-02 16:36:33 -07002838 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002839
The Android Open Source Project10592532009-03-18 17:39:46 -07002840 /**
Evan Millar633a1742009-04-02 16:36:33 -07002841 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2842 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002843 */
Evan Millar633a1742009-04-02 16:36:33 -07002844 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845
2846 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002847 * Get the amount the battery has discharged since the stats were
2848 * last reset after charging, as a lower-end approximation.
2849 */
2850 public abstract int getLowDischargeAmountSinceCharge();
2851
2852 /**
2853 * Get the amount the battery has discharged since the stats were
2854 * last reset after charging, as an upper-end approximation.
2855 */
2856 public abstract int getHighDischargeAmountSinceCharge();
2857
2858 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002859 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2860 */
2861 public abstract int getDischargeAmount(int which);
2862
2863 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002864 * Get the amount the battery has discharged while the screen was on,
2865 * since the last time power was unplugged.
2866 */
2867 public abstract int getDischargeAmountScreenOn();
2868
2869 /**
2870 * Get the amount the battery has discharged while the screen was on,
2871 * since the last time the device was charged.
2872 */
2873 public abstract int getDischargeAmountScreenOnSinceCharge();
2874
2875 /**
2876 * Get the amount the battery has discharged while the screen was off,
2877 * since the last time power was unplugged.
2878 */
2879 public abstract int getDischargeAmountScreenOff();
2880
2881 /**
2882 * Get the amount the battery has discharged while the screen was off,
2883 * since the last time the device was charged.
2884 */
2885 public abstract int getDischargeAmountScreenOffSinceCharge();
2886
2887 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002888 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002889 * since the last time power was unplugged.
2890 */
2891 public abstract int getDischargeAmountScreenDoze();
2892
2893 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002894 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002895 * since the last time the device was charged.
2896 */
2897 public abstract int getDischargeAmountScreenDozeSinceCharge();
2898
2899 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 * Returns the total, last, or current battery uptime in microseconds.
2901 *
2902 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002903 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002905 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 public abstract long computeBatteryUptime(long curTime, int which);
2907
2908 /**
2909 * Returns the total, last, or current battery realtime in microseconds.
2910 *
2911 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002912 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002914 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 public abstract long computeBatteryRealtime(long curTime, int which);
2916
2917 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002918 * Returns the total, last, or current battery screen off/doze uptime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002919 *
2920 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002921 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002922 */
2923 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2924
2925 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002926 * Returns the total, last, or current battery screen off/doze realtime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002927 *
2928 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002929 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002930 */
2931 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2932
2933 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934 * Returns the total, last, or current uptime in microseconds.
2935 *
2936 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002937 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 */
2939 public abstract long computeUptime(long curTime, int which);
2940
2941 /**
2942 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002943 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002945 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 */
2947 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002948
2949 /**
2950 * Compute an approximation for how much run time (in microseconds) is remaining on
2951 * the battery. Returns -1 if no time can be computed: either there is not
2952 * enough current data to make a decision, or the battery is currently
2953 * charging.
2954 *
2955 * @param curTime The current elepsed realtime in microseconds.
2956 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002957 @UnsupportedAppUsage
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002958 public abstract long computeBatteryTimeRemaining(long curTime);
2959
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002960 // The part of a step duration that is the actual time.
2961 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2962
2963 // Bits in a step duration that are the new battery level we are at.
2964 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002965 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002966
2967 // Bits in a step duration that are the initial mode we were in at that step.
2968 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002969 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002970
2971 // Bits in a step duration that indicate which modes changed during that step.
2972 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002973 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002974
2975 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2976 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2977
Santos Cordone94f0502017-02-24 12:31:20 -08002978 // The largest value for screen state that is tracked in battery states. Any values above
2979 // this should be mapped back to one of the tracked values before being tracked here.
2980 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2981
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002982 // Step duration mode: power save is on.
2983 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2984
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002985 // Step duration mode: device is currently in idle mode.
2986 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2987
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002988 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2989 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002990 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2991 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002992 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2993 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2994 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2995 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2996 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002997 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2998 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002999 };
3000 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
3001 (Display.STATE_OFF-1),
3002 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003003 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003004 (Display.STATE_ON-1),
3005 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
3006 (Display.STATE_DOZE-1),
3007 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
3008 (Display.STATE_DOZE_SUSPEND-1),
3009 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003010 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003011 };
3012 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
3013 "screen off",
3014 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003015 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003016 "screen on",
3017 "screen on power save",
3018 "screen doze",
3019 "screen doze power save",
3020 "screen doze-suspend",
3021 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003022 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003023 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003024
3025 /**
Mike Mac2f518a2017-09-19 16:06:03 -07003026 * Return the amount of battery discharge while the screen was off, measured in
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003027 * micro-Ampere-hours. This will be non-zero only if the device's battery has
3028 * a coulomb counter.
3029 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07003030 public abstract long getUahDischargeScreenOff(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07003031
3032 /**
3033 * Return the amount of battery discharge while the screen was in doze mode, measured in
3034 * micro-Ampere-hours. This will be non-zero only if the device's battery has
3035 * a coulomb counter.
3036 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07003037 public abstract long getUahDischargeScreenDoze(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07003038
3039 /**
3040 * Return the amount of battery discharge measured in micro-Ampere-hours. This will be
3041 * non-zero only if the device's battery has a coulomb counter.
3042 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07003043 public abstract long getUahDischarge(int which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003044
3045 /**
Mike Ma15313c92017-11-15 17:58:21 -08003046 * @return the amount of battery discharge while the device is in light idle mode, measured in
3047 * micro-Ampere-hours.
3048 */
3049 public abstract long getUahDischargeLightDoze(int which);
3050
3051 /**
3052 * @return the amount of battery discharge while the device is in deep idle mode, measured in
3053 * micro-Ampere-hours.
3054 */
3055 public abstract long getUahDischargeDeepDoze(int which);
3056
3057 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003058 * Returns the estimated real battery capacity, which may be less than the capacity
3059 * declared by the PowerProfile.
3060 * @return The estimated battery capacity in mAh.
3061 */
3062 public abstract int getEstimatedBatteryCapacity();
3063
3064 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07003065 * @return The minimum learned battery capacity in uAh.
3066 */
3067 public abstract int getMinLearnedBatteryCapacity();
3068
3069 /**
3070 * @return The maximum learned battery capacity in uAh.
3071 */
3072 public abstract int getMaxLearnedBatteryCapacity() ;
3073
3074 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003075 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003076 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003077 public abstract LevelStepTracker getDischargeLevelStepTracker();
3078
3079 /**
3080 * Return the array of daily discharge step durations.
3081 */
3082 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003083
3084 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003085 * Compute an approximation for how much time (in microseconds) remains until the battery
3086 * is fully charged. Returns -1 if no time can be computed: either there is not
3087 * enough current data to make a decision, or the battery is currently
3088 * discharging.
3089 *
3090 * @param curTime The current elepsed realtime in microseconds.
3091 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003092 @UnsupportedAppUsage
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003093 public abstract long computeChargeTimeRemaining(long curTime);
3094
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003095 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003096 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003097 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003098 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003099
3100 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003101 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003102 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003103 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07003104
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003105 public abstract ArrayList<PackageChange> getDailyPackageChanges();
3106
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003107 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003108
Evan Millarc64edde2009-04-18 12:26:32 -07003109 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003110
Bookatz50df7112017-08-04 14:53:26 -07003111 /**
3112 * Returns Timers tracking the total time of each Resource Power Manager state and voter.
3113 */
3114 public abstract Map<String, ? extends Timer> getRpmStats();
3115 /**
3116 * Returns Timers tracking the screen-off time of each Resource Power Manager state and voter.
3117 */
3118 public abstract Map<String, ? extends Timer> getScreenOffRpmStats();
3119
3120
James Carr2dd7e5e2016-07-20 18:48:39 -07003121 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
3122
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003123 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
3124
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003125 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 long days = seconds / (60 * 60 * 24);
3127 if (days != 0) {
3128 out.append(days);
3129 out.append("d ");
3130 }
3131 long used = days * 60 * 60 * 24;
3132
3133 long hours = (seconds - used) / (60 * 60);
3134 if (hours != 0 || used != 0) {
3135 out.append(hours);
3136 out.append("h ");
3137 }
3138 used += hours * 60 * 60;
3139
3140 long mins = (seconds-used) / 60;
3141 if (mins != 0 || used != 0) {
3142 out.append(mins);
3143 out.append("m ");
3144 }
3145 used += mins * 60;
3146
3147 if (seconds != 0 || used != 0) {
3148 out.append(seconds-used);
3149 out.append("s ");
3150 }
3151 }
3152
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003153 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003155 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003156 sb.append(time - (sec * 1000));
3157 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 }
3159
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003160 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003161 long sec = time / 1000;
3162 formatTimeRaw(sb, sec);
3163 sb.append(time - (sec * 1000));
3164 sb.append("ms");
3165 }
3166
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003167 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003169 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 }
3171 float perc = ((float)num) / ((float)den) * 100;
3172 mFormatBuilder.setLength(0);
3173 mFormatter.format("%.1f%%", perc);
3174 return mFormatBuilder.toString();
3175 }
3176
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003177 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07003178 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07003179
Evan Millar22ac0432009-03-31 11:33:18 -07003180 if (bytes < BYTES_PER_KB) {
3181 return bytes + "B";
3182 } else if (bytes < BYTES_PER_MB) {
3183 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
3184 return mFormatBuilder.toString();
3185 } else if (bytes < BYTES_PER_GB){
3186 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
3187 return mFormatBuilder.toString();
3188 } else {
3189 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
3190 return mFormatBuilder.toString();
3191 }
3192 }
3193
Kweku Adams103351f2017-10-16 14:39:34 -07003194 private static long roundUsToMs(long timeUs) {
3195 return (timeUs + 500) / 1000;
3196 }
3197
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003198 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003199 if (timer != null) {
3200 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003201 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003202 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
3203 return totalTimeMillis;
3204 }
3205 return 0;
3206 }
3207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 /**
3209 *
3210 * @param sb a StringBuilder object.
3211 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003212 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003214 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 * @param linePrefix a String to be prepended to each line of output.
3216 * @return the line prefix
3217 */
3218 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003219 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003222 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003223
Evan Millarc64edde2009-04-18 12:26:32 -07003224 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 if (totalTimeMillis != 0) {
3226 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003227 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08003228 if (name != null) {
3229 sb.append(name);
3230 sb.append(' ');
3231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 sb.append('(');
3233 sb.append(count);
3234 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003235 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
3236 if (maxDurationMs >= 0) {
3237 sb.append(" max=");
3238 sb.append(maxDurationMs);
3239 }
Bookatz506a8182017-05-01 14:18:42 -07003240 // Put actual time if it is available and different from totalTimeMillis.
3241 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
3242 if (totalDurMs > totalTimeMillis) {
3243 sb.append(" actual=");
3244 sb.append(totalDurMs);
3245 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07003246 if (timer.isRunningLocked()) {
3247 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3248 if (currentMs >= 0) {
3249 sb.append(" (running for ");
3250 sb.append(currentMs);
3251 sb.append("ms)");
3252 } else {
3253 sb.append(" (running)");
3254 }
3255 }
3256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 return ", ";
3258 }
3259 }
3260 return linePrefix;
3261 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003262
3263 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07003264 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003265 *
3266 * @param pw a PrintWriter object to print to.
3267 * @param sb a StringBuilder object.
3268 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08003269 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003270 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
3271 * @param prefix a String to be prepended to each line of output.
3272 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07003273 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003274 */
3275 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07003276 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003277 if (timer != null) {
3278 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07003279 final long totalTimeMs = (timer.getTotalTimeLocked(
3280 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003281 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003282 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003283 sb.setLength(0);
3284 sb.append(prefix);
3285 sb.append(" ");
3286 sb.append(type);
3287 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003288 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003289 sb.append("realtime (");
3290 sb.append(count);
3291 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003292 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
3293 if (maxDurationMs >= 0) {
3294 sb.append(" max=");
3295 sb.append(maxDurationMs);
3296 }
3297 if (timer.isRunningLocked()) {
3298 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
3299 if (currentMs >= 0) {
3300 sb.append(" (running for ");
3301 sb.append(currentMs);
3302 sb.append("ms)");
3303 } else {
3304 sb.append(" (running)");
3305 }
3306 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003307 pw.println(sb.toString());
3308 return true;
3309 }
3310 }
3311 return false;
3312 }
Bookatzc8c44962017-05-11 12:12:54 -07003313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003314 /**
3315 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07003316 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 * @param sb a StringBuilder object.
3318 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003319 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003321 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 * @param linePrefix a String to be prepended to each line of output.
3323 * @return the line prefix
3324 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003325 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
3326 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 long totalTimeMicros = 0;
3328 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07003329 long max = 0;
3330 long current = 0;
3331 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003333 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07003334 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003335 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3336 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07003337 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 }
3339 sb.append(linePrefix);
3340 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
3341 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07003342 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003344 sb.append(',');
3345 sb.append(current);
3346 sb.append(',');
3347 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07003348 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
3349 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
3350 // totalDuration independent of totalTimeMicros (since they are not pooled).
3351 if (name != null) {
3352 sb.append(',');
3353 sb.append(totalDuration);
3354 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003355 return ",";
3356 }
Bookatz506a8182017-05-01 14:18:42 -07003357
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003358 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
3359 String type) {
3360 pw.print(BATTERY_STATS_CHECKIN_VERSION);
3361 pw.print(',');
3362 pw.print(uid);
3363 pw.print(',');
3364 pw.print(category);
3365 pw.print(',');
3366 pw.print(type);
3367 }
3368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 /**
3370 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07003371 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 * @param pw the PageWriter to dump log to
3373 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3374 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3375 * @param args type-dependent data arguments
3376 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003377 @UnsupportedAppUsage
Bookatzc8c44962017-05-11 12:12:54 -07003378 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003380 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003381 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003382 pw.print(',');
3383 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003384 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003385 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07003387
3388 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003389 * Dump a given timer stat for terse checkin mode.
3390 *
3391 * @param pw the PageWriter to dump log to
3392 * @param uid the UID to log
3393 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3394 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3395 * @param timer a {@link Timer} to dump stats for
3396 * @param rawRealtime the current elapsed realtime of the system in microseconds
3397 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3398 */
3399 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
3400 Timer timer, long rawRealtime, int which) {
3401 if (timer != null) {
3402 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003403 final long totalTime = roundUsToMs(timer.getTotalTimeLocked(rawRealtime, which));
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003404 final int count = timer.getCountLocked(which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003405 if (totalTime != 0 || count != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003406 dumpLine(pw, uid, category, type, totalTime, count);
3407 }
3408 }
3409 }
3410
3411 /**
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003412 * Dump a given timer stat to the proto stream.
3413 *
3414 * @param proto the ProtoOutputStream to log to
3415 * @param fieldId type of data, the field to save to (e.g. AggregatedBatteryStats.WAKELOCK)
3416 * @param timer a {@link Timer} to dump stats for
3417 * @param rawRealtimeUs the current elapsed realtime of the system in microseconds
3418 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3419 */
3420 private static void dumpTimer(ProtoOutputStream proto, long fieldId,
Kweku Adams87b19ec2017-10-09 12:40:03 -07003421 Timer timer, long rawRealtimeUs, int which) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003422 if (timer == null) {
3423 return;
3424 }
3425 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003426 final long timeMs = roundUsToMs(timer.getTotalTimeLocked(rawRealtimeUs, which));
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003427 final int count = timer.getCountLocked(which);
Kweku Adams103351f2017-10-16 14:39:34 -07003428 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs / 1000);
3429 final long curDurationMs = timer.getCurrentDurationMsLocked(rawRealtimeUs / 1000);
3430 final long totalDurationMs = timer.getTotalDurationMsLocked(rawRealtimeUs / 1000);
3431 if (timeMs != 0 || count != 0 || maxDurationMs != -1 || curDurationMs != -1
3432 || totalDurationMs != -1) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003433 final long token = proto.start(fieldId);
Kweku Adams103351f2017-10-16 14:39:34 -07003434 proto.write(TimerProto.DURATION_MS, timeMs);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003435 proto.write(TimerProto.COUNT, count);
Kweku Adams103351f2017-10-16 14:39:34 -07003436 // These values will be -1 for timers that don't implement the functionality.
3437 if (maxDurationMs != -1) {
3438 proto.write(TimerProto.MAX_DURATION_MS, maxDurationMs);
3439 }
3440 if (curDurationMs != -1) {
3441 proto.write(TimerProto.CURRENT_DURATION_MS, curDurationMs);
3442 }
3443 if (totalDurationMs != -1) {
3444 proto.write(TimerProto.TOTAL_DURATION_MS, totalDurationMs);
3445 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003446 proto.end(token);
3447 }
3448 }
3449
3450 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003451 * Checks if the ControllerActivityCounter has any data worth dumping.
3452 */
3453 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
3454 if (counter == null) {
3455 return false;
3456 }
3457
3458 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
3459 || counter.getRxTimeCounter().getCountLocked(which) != 0
Blake Kragtencb308d92019-01-28 10:54:13 -08003460 || counter.getPowerCounter().getCountLocked(which) != 0
3461 || counter.getMonitoredRailChargeConsumedMaMs().getCountLocked(which) != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003462 return true;
3463 }
3464
3465 for (LongCounter c : counter.getTxTimeCounters()) {
3466 if (c.getCountLocked(which) != 0) {
3467 return true;
3468 }
3469 }
3470 return false;
3471 }
3472
3473 /**
3474 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3475 * The order of the arguments in the final check in line is:
3476 *
3477 * idle, rx, power, tx...
3478 *
3479 * where tx... is one or more transmit level times.
3480 */
3481 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
3482 String type,
3483 ControllerActivityCounter counter,
3484 int which) {
3485 if (!controllerActivityHasData(counter, which)) {
3486 return;
3487 }
3488
3489 dumpLineHeader(pw, uid, category, type);
3490 pw.print(",");
3491 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
3492 pw.print(",");
3493 pw.print(counter.getRxTimeCounter().getCountLocked(which));
3494 pw.print(",");
Blake Kragtencb308d92019-01-28 10:54:13 -08003495 pw.print(counter.getPowerCounter().getCountLocked(which) / (MILLISECONDS_IN_HOUR));
3496 pw.print(",");
3497 pw.print(counter.getMonitoredRailChargeConsumedMaMs().getCountLocked(which)
3498 / (MILLISECONDS_IN_HOUR));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003499 for (LongCounter c : counter.getTxTimeCounters()) {
3500 pw.print(",");
3501 pw.print(c.getCountLocked(which));
3502 }
3503 pw.println();
3504 }
3505
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003506 /**
3507 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3508 */
3509 private static void dumpControllerActivityProto(ProtoOutputStream proto, long fieldId,
3510 ControllerActivityCounter counter,
3511 int which) {
3512 if (!controllerActivityHasData(counter, which)) {
3513 return;
3514 }
3515
3516 final long cToken = proto.start(fieldId);
3517
3518 proto.write(ControllerActivityProto.IDLE_DURATION_MS,
3519 counter.getIdleTimeCounter().getCountLocked(which));
3520 proto.write(ControllerActivityProto.RX_DURATION_MS,
3521 counter.getRxTimeCounter().getCountLocked(which));
3522 proto.write(ControllerActivityProto.POWER_MAH,
Blake Kragtencb308d92019-01-28 10:54:13 -08003523 counter.getPowerCounter().getCountLocked(which) / (MILLISECONDS_IN_HOUR));
3524 proto.write(ControllerActivityProto.MONITORED_RAIL_CHARGE_MAH,
3525 counter.getMonitoredRailChargeConsumedMaMs().getCountLocked(which)
3526 / (MILLISECONDS_IN_HOUR));
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003527
3528 long tToken;
3529 LongCounter[] txCounters = counter.getTxTimeCounters();
3530 for (int i = 0; i < txCounters.length; ++i) {
3531 LongCounter c = txCounters[i];
3532 tToken = proto.start(ControllerActivityProto.TX);
3533 proto.write(ControllerActivityProto.TxLevel.LEVEL, i);
3534 proto.write(ControllerActivityProto.TxLevel.DURATION_MS, c.getCountLocked(which));
3535 proto.end(tToken);
3536 }
3537
3538 proto.end(cToken);
3539 }
3540
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003541 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
3542 String prefix, String controllerName,
3543 ControllerActivityCounter counter,
3544 int which) {
3545 if (controllerActivityHasData(counter, which)) {
3546 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
3547 }
3548 }
3549
3550 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
3551 String controllerName,
3552 ControllerActivityCounter counter, int which) {
3553 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
3554 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
3555 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
Blake Kragtencb308d92019-01-28 10:54:13 -08003556 final long monitoredRailChargeConsumedMaMs =
3557 counter.getMonitoredRailChargeConsumedMaMs().getCountLocked(which);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003558 // Battery real time
3559 final long totalControllerActivityTimeMs
3560 = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003561 long totalTxTimeMs = 0;
3562 for (LongCounter txState : counter.getTxTimeCounters()) {
3563 totalTxTimeMs += txState.getCountLocked(which);
3564 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003565
Siddharth Rayb50a6842017-12-14 15:15:28 -08003566 if (controllerName.equals(WIFI_CONTROLLER_NAME)) {
3567 final long scanTimeMs = counter.getScanTimeCounter().getCountLocked(which);
3568 sb.setLength(0);
3569 sb.append(prefix);
3570 sb.append(" ");
3571 sb.append(controllerName);
3572 sb.append(" Scan time: ");
3573 formatTimeMs(sb, scanTimeMs);
3574 sb.append("(");
3575 sb.append(formatRatioLocked(scanTimeMs, totalControllerActivityTimeMs));
3576 sb.append(")");
3577 pw.println(sb.toString());
Siddharth Rayed754702018-02-15 12:44:37 -08003578
3579 final long sleepTimeMs
3580 = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + totalTxTimeMs);
3581 sb.setLength(0);
3582 sb.append(prefix);
3583 sb.append(" ");
3584 sb.append(controllerName);
3585 sb.append(" Sleep time: ");
3586 formatTimeMs(sb, sleepTimeMs);
3587 sb.append("(");
3588 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3589 sb.append(")");
3590 pw.println(sb.toString());
Siddharth Rayb50a6842017-12-14 15:15:28 -08003591 }
3592
Siddharth Rayed754702018-02-15 12:44:37 -08003593 if (controllerName.equals(CELLULAR_CONTROLLER_NAME)) {
3594 final long sleepTimeMs = counter.getSleepTimeCounter().getCountLocked(which);
3595 sb.setLength(0);
3596 sb.append(prefix);
3597 sb.append(" ");
3598 sb.append(controllerName);
3599 sb.append(" Sleep time: ");
3600 formatTimeMs(sb, sleepTimeMs);
3601 sb.append("(");
3602 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3603 sb.append(")");
3604 pw.println(sb.toString());
3605 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003606
3607 sb.setLength(0);
3608 sb.append(prefix);
3609 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003610 sb.append(controllerName);
3611 sb.append(" Idle time: ");
3612 formatTimeMs(sb, idleTimeMs);
3613 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003614 sb.append(formatRatioLocked(idleTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003615 sb.append(")");
3616 pw.println(sb.toString());
3617
3618 sb.setLength(0);
3619 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003620 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003621 sb.append(controllerName);
3622 sb.append(" Rx time: ");
3623 formatTimeMs(sb, rxTimeMs);
3624 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003625 sb.append(formatRatioLocked(rxTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003626 sb.append(")");
3627 pw.println(sb.toString());
3628
3629 sb.setLength(0);
3630 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003631 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003632 sb.append(controllerName);
3633 sb.append(" Tx time: ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003634
Siddharth Ray3c648c42017-10-02 17:30:58 -07003635 String [] powerLevel;
3636 switch(controllerName) {
Siddharth Rayb50a6842017-12-14 15:15:28 -08003637 case CELLULAR_CONTROLLER_NAME:
Siddharth Ray3c648c42017-10-02 17:30:58 -07003638 powerLevel = new String[] {
3639 " less than 0dBm: ",
3640 " 0dBm to 8dBm: ",
3641 " 8dBm to 15dBm: ",
3642 " 15dBm to 20dBm: ",
3643 " above 20dBm: "};
3644 break;
3645 default:
3646 powerLevel = new String[] {"[0]", "[1]", "[2]", "[3]", "[4]"};
3647 break;
3648 }
3649 final int numTxLvls = Math.min(counter.getTxTimeCounters().length, powerLevel.length);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003650 if (numTxLvls > 1) {
Siddharth Ray3c648c42017-10-02 17:30:58 -07003651 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003652 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3653 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3654 sb.setLength(0);
3655 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003656 sb.append(" ");
3657 sb.append(powerLevel[lvl]);
3658 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003659 formatTimeMs(sb, txLvlTimeMs);
3660 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003661 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003662 sb.append(")");
3663 pw.println(sb.toString());
3664 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003665 } else {
3666 final long txLvlTimeMs = counter.getTxTimeCounters()[0].getCountLocked(which);
3667 formatTimeMs(sb, txLvlTimeMs);
3668 sb.append("(");
3669 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
3670 sb.append(")");
3671 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003672 }
3673
Siddharth Ray3c648c42017-10-02 17:30:58 -07003674 if (powerDrainMaMs > 0) {
3675 sb.setLength(0);
3676 sb.append(prefix);
3677 sb.append(" ");
3678 sb.append(controllerName);
3679 sb.append(" Battery drain: ").append(
Blake Kragtencb308d92019-01-28 10:54:13 -08003680 BatteryStatsHelper.makemAh(powerDrainMaMs / MILLISECONDS_IN_HOUR));
Siddharth Ray3c648c42017-10-02 17:30:58 -07003681 sb.append("mAh");
3682 pw.println(sb.toString());
3683 }
Blake Kragtencb308d92019-01-28 10:54:13 -08003684
3685 if (monitoredRailChargeConsumedMaMs > 0) {
3686 sb.setLength(0);
3687 sb.append(prefix);
3688 sb.append(" ");
3689 sb.append(controllerName);
3690 sb.append(" Monitored rail energy drain: ").append(
3691 new DecimalFormat("#.##").format(
3692 monitoredRailChargeConsumedMaMs / MILLISECONDS_IN_HOUR));
3693 sb.append(" mAh");
3694 pw.println(sb.toString());
3695 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003696 }
3697
3698 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003699 * Temporary for settings.
3700 */
3701 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3702 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3703 }
3704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003705 /**
3706 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003707 *
Kweku Adams87b19ec2017-10-09 12:40:03 -07003708 * NOTE: all times are expressed in microseconds, unless specified otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003710 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3711 boolean wifiOnly) {
Bookatzdb6d6242019-03-18 15:42:55 -07003712
3713 if (which != BatteryStats.STATS_SINCE_CHARGED) {
3714 dumpLine(pw, 0, STAT_NAMES[which], "err",
3715 "ERROR: BatteryStats.dumpCheckin called for which type " + which
3716 + " but only STATS_SINCE_CHARGED is supported.");
3717 return;
3718 }
3719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 final long rawUptime = SystemClock.uptimeMillis() * 1000;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003721 final long rawRealtimeMs = SystemClock.elapsedRealtime();
3722 final long rawRealtime = rawRealtimeMs * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003723 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003724 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3725 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003726 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3727 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3728 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 final long totalRealtime = computeRealtime(rawRealtime, which);
3730 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003731 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Mike Mac2f518a2017-09-19 16:06:03 -07003732 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003733 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003734 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003735 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3736 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003737 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003738 rawRealtime, which);
3739 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3740 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003741 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003742 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003743 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003744 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003745 final long dischargeCount = getUahDischarge(which);
3746 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
3747 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Ma15313c92017-11-15 17:58:21 -08003748 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
3749 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003750
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003751 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003752
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003753 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003754 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003755
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003756 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003759 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003760 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003761 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003762 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003763 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003764 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003765 getEstimatedBatteryCapacity(),
Mike Mac2f518a2017-09-19 16:06:03 -07003766 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity(),
3767 screenDozeTime / 1000);
Adam Lesinski67c134f2016-06-10 15:15:08 -07003768
Bookatzc8c44962017-05-11 12:12:54 -07003769
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003770 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003771 long fullWakeLockTimeTotal = 0;
3772 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003773
Evan Millar22ac0432009-03-31 11:33:18 -07003774 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003775 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003776
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003777 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3778 = u.getWakelockStats();
3779 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3780 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003781
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003782 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3783 if (fullWakeTimer != null) {
3784 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3785 which);
3786 }
3787
3788 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3789 if (partialWakeTimer != null) {
3790 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3791 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003792 }
3793 }
3794 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003795
3796 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003797 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3798 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3799 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3800 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3801 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3802 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3803 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3804 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003805 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3806 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003807 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3808 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003809 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3810 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003811
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003812 // Dump Modem controller stats
3813 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3814 getModemControllerActivity(), which);
3815
Adam Lesinskie283d332015-04-16 12:29:25 -07003816 // Dump Wifi controller stats
3817 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3818 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003819 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003820 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003821
3822 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3823 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003824
3825 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003826 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3827 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003829 // Dump misc stats
3830 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003831 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003832 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003833 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003834 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003835 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003836 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3837 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003838 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003839 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3840 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3841 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3842 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003843 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003844
Dianne Hackborn617f8772009-03-31 15:04:46 -07003845 // Dump screen brightness stats
3846 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3847 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003848 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003849 }
3850 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003851
Dianne Hackborn627bba72009-03-24 22:32:56 -07003852 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003853 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3854 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003855 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003856 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003857 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003858 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003859 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003860 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003861 args[i] = getPhoneSignalStrengthCount(i, which);
3862 }
3863 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003864
Dianne Hackborn627bba72009-03-24 22:32:56 -07003865 // Dump network type stats
3866 args = new Object[NUM_DATA_CONNECTION_TYPES];
3867 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003868 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003869 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003870 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3871 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3872 args[i] = getPhoneDataConnectionCount(i, which);
3873 }
3874 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003875
3876 // Dump wifi state stats
3877 args = new Object[NUM_WIFI_STATES];
3878 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003879 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003880 }
3881 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3882 for (int i=0; i<NUM_WIFI_STATES; i++) {
3883 args[i] = getWifiStateCount(i, which);
3884 }
3885 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3886
Dianne Hackborn3251b902014-06-20 14:40:53 -07003887 // Dump wifi suppl state stats
3888 args = new Object[NUM_WIFI_SUPPL_STATES];
3889 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3890 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3891 }
3892 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3893 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3894 args[i] = getWifiSupplStateCount(i, which);
3895 }
3896 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3897
3898 // Dump wifi signal strength stats
3899 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3900 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3901 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3902 }
3903 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3904 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3905 args[i] = getWifiSignalStrengthCount(i, which);
3906 }
3907 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3908
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003909 // Dump Multicast total stats
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003910 final long multicastWakeLockTimeTotalMicros =
3911 getWifiMulticastWakelockTime(rawRealtime, which);
3912 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003913 dumpLine(pw, 0 /* uid */, category, WIFI_MULTICAST_TOTAL_DATA,
3914 multicastWakeLockTimeTotalMicros / 1000,
3915 multicastWakeLockCountTotal);
3916
Mike Ma99e57c32018-03-15 14:40:37 -07003917 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3918 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
3919 getDischargeAmountScreenOnSinceCharge(),
3920 getDischargeAmountScreenOffSinceCharge(),
3921 dischargeCount / 1000, dischargeScreenOffCount / 1000,
3922 getDischargeAmountScreenDozeSinceCharge(), dischargeScreenDozeCount / 1000,
3923 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Bookatzc8c44962017-05-11 12:12:54 -07003924
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003925 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003926 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003927 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003928 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003929 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003930 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003931 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3932 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003933 }
Evan Millarc64edde2009-04-18 12:26:32 -07003934 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003935 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003936 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003937 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3938 // Not doing the regular wake lock formatting to remain compatible
3939 // with the old checkin format.
3940 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3941 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003942 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003943 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003944 }
3945 }
Evan Millarc64edde2009-04-18 12:26:32 -07003946 }
Bookatzc8c44962017-05-11 12:12:54 -07003947
Bookatz50df7112017-08-04 14:53:26 -07003948 final Map<String, ? extends Timer> rpmStats = getRpmStats();
3949 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
3950 if (rpmStats.size() > 0) {
3951 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
3952 sb.setLength(0);
3953 Timer totalTimer = ent.getValue();
3954 long timeMs = (totalTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3955 int count = totalTimer.getCountLocked(which);
3956 Timer screenOffTimer = screenOffRpmStats.get(ent.getKey());
3957 long screenOffTimeMs = screenOffTimer != null
3958 ? (screenOffTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3959 int screenOffCount = screenOffTimer != null
3960 ? screenOffTimer.getCountLocked(which) : 0;
Bookatz82b341172017-09-07 19:06:08 -07003961 if (SCREEN_OFF_RPM_STATS_ENABLED) {
3962 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3963 "\"" + ent.getKey() + "\"", timeMs, count, screenOffTimeMs,
3964 screenOffCount);
3965 } else {
3966 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3967 "\"" + ent.getKey() + "\"", timeMs, count);
3968 }
Bookatz50df7112017-08-04 14:53:26 -07003969 }
3970 }
3971
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003972 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003973 helper.create(this);
3974 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003975 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003976 if (sippers != null && sippers.size() > 0) {
3977 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3978 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003979 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003980 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3981 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
Kweku Adams87b19ec2017-10-09 12:40:03 -07003982 int uid = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003983 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003984 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003985 String label;
3986 switch (bs.drainType) {
Kweku Adamsef728952018-04-10 17:56:16 -07003987 case AMBIENT_DISPLAY:
3988 label = "ambi";
3989 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003990 case IDLE:
3991 label="idle";
3992 break;
3993 case CELL:
3994 label="cell";
3995 break;
3996 case PHONE:
3997 label="phone";
3998 break;
3999 case WIFI:
4000 label="wifi";
4001 break;
4002 case BLUETOOTH:
4003 label="blue";
4004 break;
4005 case SCREEN:
4006 label="scrn";
4007 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004008 case FLASHLIGHT:
4009 label="flashlight";
4010 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004011 case APP:
4012 uid = bs.uidObj.getUid();
4013 label = "uid";
4014 break;
4015 case USER:
4016 uid = UserHandle.getUid(bs.userId, 0);
4017 label = "user";
4018 break;
4019 case UNACCOUNTED:
4020 label = "unacc";
4021 break;
4022 case OVERCOUNTED:
4023 label = "over";
4024 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004025 case CAMERA:
4026 label = "camera";
4027 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07004028 case MEMORY:
4029 label = "memory";
4030 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004031 default:
4032 label = "???";
4033 }
4034 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07004035 BatteryStatsHelper.makemAh(bs.totalPowerMah),
4036 bs.shouldHide ? 1 : 0,
4037 BatteryStatsHelper.makemAh(bs.screenPowerMah),
4038 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004039 }
4040 }
4041
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004042 final long[] cpuFreqs = getCpuFreqs();
4043 if (cpuFreqs != null) {
4044 sb.setLength(0);
4045 for (int i = 0; i < cpuFreqs.length; ++i) {
4046 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
4047 }
4048 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
4049 }
4050
Kweku Adams87b19ec2017-10-09 12:40:03 -07004051 // Dump stats per UID.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004052 for (int iu = 0; iu < NU; iu++) {
4053 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004054 if (reqUid >= 0 && uid != reqUid) {
4055 continue;
4056 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004057 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07004058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004060 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4061 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4062 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4063 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4064 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4065 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4066 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
4067 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07004068 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004069 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4070 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07004071 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004072 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4073 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08004074 // Background data transfers
4075 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
4076 which);
4077 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
4078 which);
4079 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
4080 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
4081 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
4082 which);
4083 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
4084 which);
4085 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
4086 which);
4087 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
4088 which);
4089
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004090 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
4091 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004092 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08004093 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
4094 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
4095 || wifiBytesBgTx > 0
4096 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
4097 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004098 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
4099 wifiBytesRx, wifiBytesTx,
4100 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004101 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004102 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08004103 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
4104 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
4105 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
4106 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004107 }
4108
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004109 // Dump modem controller data, per UID.
4110 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
4111 u.getModemControllerActivity(), which);
4112
4113 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07004114 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4115 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4116 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08004117 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
4118 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07004119 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
4120 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
4121 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07004122 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07004123 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07004124 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
4125 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004126 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
4127 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08004128 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07004129 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07004130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004131
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004132 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
4133 u.getWifiControllerActivity(), which);
4134
Bookatz867c0d72017-03-07 18:23:42 -08004135 final Timer bleTimer = u.getBluetoothScanTimer();
4136 if (bleTimer != null) {
4137 // Convert from microseconds to milliseconds with rounding
4138 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
4139 / 1000;
4140 if (totalTime != 0) {
4141 final int count = bleTimer.getCountLocked(which);
4142 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
4143 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004144 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4145 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
4146 final long actualTimeBg = bleTimerBg != null ?
4147 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07004148 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07004149 final int resultCount = u.getBluetoothScanResultCounter() != null ?
4150 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07004151 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
4152 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
4153 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
4154 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
4155 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
4156 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4157 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
4158 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
4159 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
4160 final Timer unoptimizedScanTimerBg =
4161 u.getBluetoothUnoptimizedScanBackgroundTimer();
4162 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
4163 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4164 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
4165 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
4166
Bookatz867c0d72017-03-07 18:23:42 -08004167 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07004168 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
4169 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
4170 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08004171 }
4172 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004173
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004174 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
4175 u.getBluetoothControllerActivity(), which);
4176
Dianne Hackborn617f8772009-03-31 15:04:46 -07004177 if (u.hasUserActivity()) {
4178 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
4179 boolean hasData = false;
4180 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
4181 int val = u.getUserActivityCount(i, which);
4182 args[i] = val;
4183 if (val != 0) hasData = true;
4184 }
4185 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07004186 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004187 }
4188 }
Bookatzc8c44962017-05-11 12:12:54 -07004189
4190 if (u.getAggregatedPartialWakelockTimer() != null) {
4191 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07004192 // Times are since reset (regardless of 'which')
4193 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07004194 final Timer bgTimer = timer.getSubTimer();
4195 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004196 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07004197 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
4198 }
4199
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004200 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
4201 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4202 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4203 String linePrefix = "";
4204 sb.setLength(0);
4205 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
4206 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004207 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4208 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004209 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004210 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
4211 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004212 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
4213 rawRealtime, "w", which, linePrefix);
4214
Kweku Adams103351f2017-10-16 14:39:34 -07004215 // Only log if we had at least one wakelock...
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004216 if (sb.length() > 0) {
4217 String name = wakelocks.keyAt(iw);
4218 if (name.indexOf(',') >= 0) {
4219 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004220 }
Yi Jin02483362017-08-04 11:30:44 -07004221 if (name.indexOf('\n') >= 0) {
4222 name = name.replace('\n', '_');
4223 }
4224 if (name.indexOf('\r') >= 0) {
4225 name = name.replace('\r', '_');
4226 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004227 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004228 }
4229 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004230
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004231 // WiFi Multicast Wakelock Statistics
4232 final Timer mcTimer = u.getMulticastWakelockStats();
4233 if (mcTimer != null) {
4234 final long totalMcWakelockTimeMs =
4235 mcTimer.getTotalTimeLocked(rawRealtime, which) / 1000 ;
4236 final int countMcWakelock = mcTimer.getCountLocked(which);
4237 if(totalMcWakelockTimeMs > 0) {
4238 dumpLine(pw, uid, category, WIFI_MULTICAST_DATA,
4239 totalMcWakelockTimeMs, countMcWakelock);
4240 }
4241 }
4242
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004243 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4244 for (int isy=syncs.size()-1; isy>=0; isy--) {
4245 final Timer timer = syncs.valueAt(isy);
4246 // Convert from microseconds to milliseconds with rounding
4247 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4248 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07004249 final Timer bgTimer = timer.getSubTimer();
4250 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004251 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07004252 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004253 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004254 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07004255 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004256 }
4257 }
4258
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004259 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4260 for (int ij=jobs.size()-1; ij>=0; ij--) {
4261 final Timer timer = jobs.valueAt(ij);
4262 // Convert from microseconds to milliseconds with rounding
4263 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4264 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07004265 final Timer bgTimer = timer.getSubTimer();
4266 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004267 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07004268 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004269 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004270 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07004271 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004272 }
4273 }
4274
Dianne Hackborn94326cb2017-06-28 16:17:20 -07004275 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
4276 for (int ic=completions.size()-1; ic>=0; ic--) {
4277 SparseIntArray types = completions.valueAt(ic);
4278 if (types != null) {
4279 dumpLine(pw, uid, category, JOB_COMPLETION_DATA,
4280 "\"" + completions.keyAt(ic) + "\"",
4281 types.get(JobParameters.REASON_CANCELED, 0),
4282 types.get(JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, 0),
4283 types.get(JobParameters.REASON_PREEMPT, 0),
4284 types.get(JobParameters.REASON_TIMEOUT, 0),
4285 types.get(JobParameters.REASON_DEVICE_IDLE, 0));
4286 }
4287 }
4288
Amith Yamasani977e11f2018-02-16 11:29:54 -08004289 // Dump deferred jobs stats
4290 u.getDeferredJobsCheckinLineLocked(sb, which);
4291 if (sb.length() > 0) {
4292 dumpLine(pw, uid, category, JOBS_DEFERRED_DATA, sb.toString());
4293 }
4294
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004295 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
4296 rawRealtime, which);
4297 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
4298 rawRealtime, which);
4299 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
4300 rawRealtime, which);
4301 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
4302 rawRealtime, which);
4303
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004304 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4305 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004306 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004307 final Uid.Sensor se = sensors.valueAt(ise);
4308 final int sensorNumber = sensors.keyAt(ise);
4309 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004310 if (timer != null) {
4311 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004312 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4313 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004314 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004315 final int count = timer.getCountLocked(which);
4316 final Timer bgTimer = se.getSensorBackgroundTime();
4317 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004318 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4319 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4320 final long bgActualTime = bgTimer != null ?
4321 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4322 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
4323 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004325 }
4326 }
4327
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004328 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
4329 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004330
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07004331 dumpTimer(pw, uid, category, FOREGROUND_ACTIVITY_DATA, u.getForegroundActivityTimer(),
4332 rawRealtime, which);
4333
4334 dumpTimer(pw, uid, category, FOREGROUND_SERVICE_DATA, u.getForegroundServiceTimer(),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004335 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004336
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004337 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07004338 long totalStateTime = 0;
4339 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08004340 final long time = u.getProcessStateTime(ips, rawRealtime, which);
4341 totalStateTime += time;
4342 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004343 }
4344 if (totalStateTime > 0) {
4345 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
4346 }
4347
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004348 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4349 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004350 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004351 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004352 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004353 }
4354
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004355 // If the cpuFreqs is null, then don't bother checking for cpu freq times.
4356 if (cpuFreqs != null) {
4357 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
4358 // If total cpuFreqTimes is null, then we don't need to check for
4359 // screenOffCpuFreqTimes.
4360 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
4361 sb.setLength(0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004362 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004363 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004364 }
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004365 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
4366 if (screenOffCpuFreqTimeMs != null) {
4367 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
4368 sb.append("," + screenOffCpuFreqTimeMs[i]);
4369 }
4370 } else {
4371 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
4372 sb.append(",0");
4373 }
4374 }
4375 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
4376 cpuFreqTimeMs.length, sb.toString());
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004377 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -08004378
4379 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
4380 final long[] timesMs = u.getCpuFreqTimes(which, procState);
4381 if (timesMs != null && timesMs.length == cpuFreqs.length) {
4382 sb.setLength(0);
4383 for (int i = 0; i < timesMs.length; ++i) {
4384 sb.append((i == 0 ? "" : ",") + timesMs[i]);
4385 }
4386 final long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(
4387 which, procState);
4388 if (screenOffTimesMs != null) {
4389 for (int i = 0; i < screenOffTimesMs.length; ++i) {
4390 sb.append("," + screenOffTimesMs[i]);
4391 }
4392 } else {
4393 for (int i = 0; i < timesMs.length; ++i) {
4394 sb.append(",0");
4395 }
4396 }
4397 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA,
4398 Uid.UID_PROCESS_TYPES[procState], timesMs.length, sb.toString());
4399 }
4400 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004401 }
4402
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004403 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4404 = u.getProcessStats();
4405 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4406 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004407
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004408 final long userMillis = ps.getUserTime(which);
4409 final long systemMillis = ps.getSystemTime(which);
4410 final long foregroundMillis = ps.getForegroundTime(which);
4411 final int starts = ps.getStarts(which);
4412 final int numCrashes = ps.getNumCrashes(which);
4413 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004414
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004415 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
4416 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004417 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
4418 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004419 }
4420 }
4421
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004422 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4423 = u.getPackageStats();
4424 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4425 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4426 int wakeups = 0;
4427 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4428 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07004429 int count = alarms.valueAt(iwa).getCountLocked(which);
4430 wakeups += count;
4431 String name = alarms.keyAt(iwa).replace(',', '_');
4432 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004433 }
4434 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4435 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4436 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4437 final long startTime = ss.getStartTime(batteryUptime, which);
4438 final int starts = ss.getStarts(which);
4439 final int launches = ss.getLaunches(which);
4440 if (startTime != 0 || starts != 0 || launches != 0) {
4441 dumpLine(pw, uid, category, APK_DATA,
4442 wakeups, // wakeup alarms
4443 packageStats.keyAt(ipkg), // Apk
4444 serviceStats.keyAt(isvc), // service
4445 startTime / 1000, // time spent started, in ms
4446 starts,
4447 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004448 }
4449 }
4450 }
4451 }
4452 }
4453
Dianne Hackborn81038902012-11-26 17:04:09 -08004454 static final class TimerEntry {
4455 final String mName;
4456 final int mId;
4457 final BatteryStats.Timer mTimer;
4458 final long mTime;
4459 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
4460 mName = name;
4461 mId = id;
4462 mTimer = timer;
4463 mTime = time;
4464 }
4465 }
4466
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004467 private void printmAh(PrintWriter printer, double power) {
4468 printer.print(BatteryStatsHelper.makemAh(power));
4469 }
4470
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004471 private void printmAh(StringBuilder sb, double power) {
4472 sb.append(BatteryStatsHelper.makemAh(power));
4473 }
4474
Dianne Hackbornd953c532014-08-16 18:17:38 -07004475 /**
4476 * Temporary for settings.
4477 */
4478 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
4479 int reqUid) {
4480 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
4481 }
4482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004483 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004484 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07004485 int reqUid, boolean wifiOnly) {
Bookatzdb6d6242019-03-18 15:42:55 -07004486
4487 if (which != BatteryStats.STATS_SINCE_CHARGED) {
4488 pw.println("ERROR: BatteryStats.dump called for which type " + which
4489 + " but only STATS_SINCE_CHARGED is supported");
4490 return;
4491 }
4492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004493 final long rawUptime = SystemClock.uptimeMillis() * 1000;
4494 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07004495 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004496 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004497
4498 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
4499 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
4500 final long totalRealtime = computeRealtime(rawRealtime, which);
4501 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004502 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
4503 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
4504 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004505 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
4506 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -07004507 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004508
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004509 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07004510
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004511 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07004512 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004513
Adam Lesinskif9b20a92016-06-17 17:30:01 -07004514 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
4515 if (estimatedBatteryCapacity > 0) {
4516 sb.setLength(0);
4517 sb.append(prefix);
4518 sb.append(" Estimated battery capacity: ");
4519 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
4520 sb.append(" mAh");
4521 pw.println(sb.toString());
4522 }
4523
Jocelyn Dangc627d102017-04-14 13:15:14 -07004524 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
4525 if (minLearnedBatteryCapacity > 0) {
4526 sb.setLength(0);
4527 sb.append(prefix);
4528 sb.append(" Min learned battery capacity: ");
4529 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
4530 sb.append(" mAh");
4531 pw.println(sb.toString());
4532 }
4533 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
4534 if (maxLearnedBatteryCapacity > 0) {
4535 sb.setLength(0);
4536 sb.append(prefix);
4537 sb.append(" Max learned battery capacity: ");
4538 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
4539 sb.append(" mAh");
4540 pw.println(sb.toString());
4541 }
4542
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004543 sb.setLength(0);
4544 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004545 sb.append(" Time on battery: ");
4546 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
4547 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
4548 sb.append(") realtime, ");
4549 formatTimeMs(sb, whichBatteryUptime / 1000);
4550 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, whichBatteryRealtime));
4551 sb.append(") uptime");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004552 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004553
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004554 sb.setLength(0);
4555 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004556 sb.append(" Time on battery screen off: ");
4557 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
4558 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, whichBatteryRealtime));
4559 sb.append(") realtime, ");
4560 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
4561 sb.append("(");
4562 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, whichBatteryRealtime));
4563 sb.append(") uptime");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004564 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004565
4566 sb.setLength(0);
4567 sb.append(prefix);
4568 sb.append(" Time on battery screen doze: ");
4569 formatTimeMs(sb, screenDozeTime / 1000); sb.append("(");
4570 sb.append(formatRatioLocked(screenDozeTime, whichBatteryRealtime));
4571 sb.append(")");
4572 pw.println(sb.toString());
4573
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004574 sb.setLength(0);
4575 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004576 sb.append(" Total run time: ");
4577 formatTimeMs(sb, totalRealtime / 1000);
4578 sb.append("realtime, ");
4579 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004580 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07004581 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004582 if (batteryTimeRemaining >= 0) {
4583 sb.setLength(0);
4584 sb.append(prefix);
4585 sb.append(" Battery time remaining: ");
4586 formatTimeMs(sb, batteryTimeRemaining / 1000);
4587 pw.println(sb.toString());
4588 }
4589 if (chargeTimeRemaining >= 0) {
4590 sb.setLength(0);
4591 sb.append(prefix);
4592 sb.append(" Charge time remaining: ");
4593 formatTimeMs(sb, chargeTimeRemaining / 1000);
4594 pw.println(sb.toString());
4595 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004596
Kweku Adams87b19ec2017-10-09 12:40:03 -07004597 final long dischargeCount = getUahDischarge(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004598 if (dischargeCount >= 0) {
4599 sb.setLength(0);
4600 sb.append(prefix);
4601 sb.append(" Discharge: ");
4602 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
4603 sb.append(" mAh");
4604 pw.println(sb.toString());
4605 }
4606
Kweku Adams87b19ec2017-10-09 12:40:03 -07004607 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004608 if (dischargeScreenOffCount >= 0) {
4609 sb.setLength(0);
4610 sb.append(prefix);
4611 sb.append(" Screen off discharge: ");
4612 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
4613 sb.append(" mAh");
4614 pw.println(sb.toString());
4615 }
4616
Kweku Adams87b19ec2017-10-09 12:40:03 -07004617 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Mac2f518a2017-09-19 16:06:03 -07004618 if (dischargeScreenDozeCount >= 0) {
4619 sb.setLength(0);
4620 sb.append(prefix);
4621 sb.append(" Screen doze discharge: ");
4622 sb.append(BatteryStatsHelper.makemAh(dischargeScreenDozeCount / 1000.0));
4623 sb.append(" mAh");
4624 pw.println(sb.toString());
4625 }
4626
Mike Ma90902652018-04-17 14:07:24 -07004627 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004628 if (dischargeScreenOnCount >= 0) {
4629 sb.setLength(0);
4630 sb.append(prefix);
4631 sb.append(" Screen on discharge: ");
4632 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
4633 sb.append(" mAh");
4634 pw.println(sb.toString());
4635 }
4636
Mike Ma15313c92017-11-15 17:58:21 -08004637 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
4638 if (dischargeLightDozeCount >= 0) {
4639 sb.setLength(0);
4640 sb.append(prefix);
4641 sb.append(" Device light doze discharge: ");
4642 sb.append(BatteryStatsHelper.makemAh(dischargeLightDozeCount / 1000.0));
4643 sb.append(" mAh");
4644 pw.println(sb.toString());
4645 }
4646
4647 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
4648 if (dischargeDeepDozeCount >= 0) {
4649 sb.setLength(0);
4650 sb.append(prefix);
4651 sb.append(" Device deep doze discharge: ");
4652 sb.append(BatteryStatsHelper.makemAh(dischargeDeepDozeCount / 1000.0));
4653 sb.append(" mAh");
4654 pw.println(sb.toString());
4655 }
4656
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004657 pw.print(" Start clock time: ");
4658 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
4659
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004660 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004661 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004662 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004663 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
4664 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004665 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004666 rawRealtime, which);
4667 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
4668 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004669 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004670 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004671 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
4672 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
4673 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004674 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004675 sb.append(prefix);
4676 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
4677 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004678 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004679 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
4680 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004681 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004682 pw.println(sb.toString());
4683 sb.setLength(0);
4684 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004685 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004686 boolean didOne = false;
4687 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004688 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004689 if (time == 0) {
4690 continue;
4691 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004692 sb.append("\n ");
4693 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004694 didOne = true;
4695 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
4696 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004697 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004698 sb.append("(");
4699 sb.append(formatRatioLocked(time, screenOnTime));
4700 sb.append(")");
4701 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004702 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004703 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004704 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004705 sb.setLength(0);
4706 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004707 sb.append(" Power save mode enabled: ");
4708 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004709 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004710 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004711 sb.append(")");
4712 pw.println(sb.toString());
4713 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004714 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004715 sb.setLength(0);
4716 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004717 sb.append(" Device light idling: ");
4718 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004719 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004720 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
4721 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004722 sb.append("x");
4723 pw.println(sb.toString());
4724 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004725 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004726 sb.setLength(0);
4727 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004728 sb.append(" Idle mode light time: ");
4729 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004730 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004731 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
4732 sb.append(") ");
4733 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004734 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004735 sb.append(" -- longest ");
4736 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
4737 pw.println(sb.toString());
4738 }
4739 if (deviceIdlingTime != 0) {
4740 sb.setLength(0);
4741 sb.append(prefix);
4742 sb.append(" Device full idling: ");
4743 formatTimeMs(sb, deviceIdlingTime / 1000);
4744 sb.append("(");
4745 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004746 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004747 sb.append("x");
4748 pw.println(sb.toString());
4749 }
4750 if (deviceIdleModeFullTime != 0) {
4751 sb.setLength(0);
4752 sb.append(prefix);
4753 sb.append(" Idle mode full time: ");
4754 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
4755 sb.append("(");
4756 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
4757 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004758 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004759 sb.append("x");
4760 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004761 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004762 pw.println(sb.toString());
4763 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004764 if (phoneOnTime != 0) {
4765 sb.setLength(0);
4766 sb.append(prefix);
4767 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
4768 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004769 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004770 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004771 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004772 if (connChanges != 0) {
4773 pw.print(prefix);
4774 pw.print(" Connectivity changes: "); pw.println(connChanges);
4775 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004776
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004777 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07004778 long fullWakeLockTimeTotalMicros = 0;
4779 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08004780
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004781 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08004782
Evan Millar22ac0432009-03-31 11:33:18 -07004783 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004784 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004785
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004786 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4787 = u.getWakelockStats();
4788 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4789 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07004790
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004791 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
4792 if (fullWakeTimer != null) {
4793 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
4794 rawRealtime, which);
4795 }
4796
4797 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4798 if (partialWakeTimer != null) {
4799 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
4800 rawRealtime, which);
4801 if (totalTimeMicros > 0) {
4802 if (reqUid < 0) {
4803 // Only show the ordered list of all wake
4804 // locks if the caller is not asking for data
4805 // about a specific uid.
4806 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
4807 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08004808 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004809 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07004810 }
4811 }
4812 }
4813 }
Bookatzc8c44962017-05-11 12:12:54 -07004814
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004815 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4816 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4817 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4818 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4819 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4820 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4821 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4822 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004823 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4824 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004825
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004826 if (fullWakeLockTimeTotalMicros != 0) {
4827 sb.setLength(0);
4828 sb.append(prefix);
4829 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4830 (fullWakeLockTimeTotalMicros + 500) / 1000);
4831 pw.println(sb.toString());
4832 }
4833
4834 if (partialWakeLockTimeTotalMicros != 0) {
4835 sb.setLength(0);
4836 sb.append(prefix);
4837 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4838 (partialWakeLockTimeTotalMicros + 500) / 1000);
4839 pw.println(sb.toString());
4840 }
4841
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004842 final long multicastWakeLockTimeTotalMicros =
4843 getWifiMulticastWakelockTime(rawRealtime, which);
4844 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004845 if (multicastWakeLockTimeTotalMicros != 0) {
4846 sb.setLength(0);
4847 sb.append(prefix);
4848 sb.append(" Total WiFi Multicast wakelock Count: ");
4849 sb.append(multicastWakeLockCountTotal);
4850 pw.println(sb.toString());
4851
4852 sb.setLength(0);
4853 sb.append(prefix);
4854 sb.append(" Total WiFi Multicast wakelock time: ");
4855 formatTimeMsNoSpace(sb, (multicastWakeLockTimeTotalMicros + 500) / 1000);
4856 pw.println(sb.toString());
4857 }
4858
Siddharth Ray3c648c42017-10-02 17:30:58 -07004859 pw.println("");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004860 pw.print(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004861 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004862 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004863 sb.append(" CONNECTIVITY POWER SUMMARY START");
4864 pw.println(sb.toString());
4865
4866 pw.print(prefix);
4867 sb.setLength(0);
4868 sb.append(prefix);
4869 sb.append(" Logging duration for connectivity statistics: ");
4870 formatTimeMs(sb, whichBatteryRealtime / 1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004871 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004872
4873 sb.setLength(0);
4874 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004875 sb.append(" Cellular Statistics:");
Amith Yamasanif37447b2009-10-08 18:28:01 -07004876 pw.println(sb.toString());
4877
Siddharth Ray3c648c42017-10-02 17:30:58 -07004878 pw.print(prefix);
4879 sb.setLength(0);
4880 sb.append(prefix);
4881 sb.append(" Cellular kernel active time: ");
4882 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
4883 formatTimeMs(sb, mobileActiveTime / 1000);
4884 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4885 sb.append(")");
4886 pw.println(sb.toString());
4887
Blake Kragtenbff255d2019-03-05 15:44:31 -08004888 printControllerActivity(pw, sb, prefix, CELLULAR_CONTROLLER_NAME,
4889 getModemControllerActivity(), which);
4890
Siddharth Ray3c648c42017-10-02 17:30:58 -07004891 pw.print(" Cellular data received: "); pw.println(formatBytesLocked(mobileRxTotalBytes));
4892 pw.print(" Cellular data sent: "); pw.println(formatBytesLocked(mobileTxTotalBytes));
4893 pw.print(" Cellular packets received: "); pw.println(mobileRxTotalPackets);
4894 pw.print(" Cellular packets sent: "); pw.println(mobileTxTotalPackets);
4895
Dianne Hackborn627bba72009-03-24 22:32:56 -07004896 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004897 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004898 sb.append(" Cellular Radio Access Technology:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004899 didOne = false;
4900 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004901 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004902 if (time == 0) {
4903 continue;
4904 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004905 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004906 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004907 didOne = true;
Bookatzdf6baff2018-12-12 19:38:11 -08004908 sb.append(i < DATA_CONNECTION_NAMES.length ? DATA_CONNECTION_NAMES[i] : "ERROR");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004909 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004910 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004911 sb.append("(");
4912 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004913 sb.append(") ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004914 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004915 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004916 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004917
4918 sb.setLength(0);
4919 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004920 sb.append(" Cellular Rx signal strength (RSRP):");
4921 final String[] cellularRxSignalStrengthDescription = new String[]{
4922 "very poor (less than -128dBm): ",
4923 "poor (-128dBm to -118dBm): ",
4924 "moderate (-118dBm to -108dBm): ",
4925 "good (-108dBm to -98dBm): ",
4926 "great (greater than -98dBm): "};
4927 didOne = false;
4928 final int numCellularRxBins = Math.min(SignalStrength.NUM_SIGNAL_STRENGTH_BINS,
4929 cellularRxSignalStrengthDescription.length);
4930 for (int i=0; i<numCellularRxBins; i++) {
4931 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
4932 if (time == 0) {
4933 continue;
4934 }
4935 sb.append("\n ");
4936 sb.append(prefix);
4937 didOne = true;
4938 sb.append(cellularRxSignalStrengthDescription[i]);
4939 sb.append(" ");
4940 formatTimeMs(sb, time/1000);
4941 sb.append("(");
4942 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4943 sb.append(") ");
4944 }
4945 if (!didOne) sb.append(" (no activity)");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004946 pw.println(sb.toString());
4947
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004948 pw.print(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004949 sb.setLength(0);
4950 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004951 sb.append(" Wifi Statistics:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004952 pw.println(sb.toString());
4953
Siddharth Rayb50a6842017-12-14 15:15:28 -08004954 pw.print(prefix);
4955 sb.setLength(0);
4956 sb.append(prefix);
4957 sb.append(" Wifi kernel active time: ");
4958 final long wifiActiveTime = getWifiActiveTime(rawRealtime, which);
4959 formatTimeMs(sb, wifiActiveTime / 1000);
4960 sb.append("("); sb.append(formatRatioLocked(wifiActiveTime, whichBatteryRealtime));
4961 sb.append(")");
4962 pw.println(sb.toString());
4963
Blake Kragtenbff255d2019-03-05 15:44:31 -08004964 printControllerActivity(pw, sb, prefix, WIFI_CONTROLLER_NAME,
4965 getWifiControllerActivity(), which);
4966
Siddharth Ray3c648c42017-10-02 17:30:58 -07004967 pw.print(" Wifi data received: "); pw.println(formatBytesLocked(wifiRxTotalBytes));
4968 pw.print(" Wifi data sent: "); pw.println(formatBytesLocked(wifiTxTotalBytes));
4969 pw.print(" Wifi packets received: "); pw.println(wifiRxTotalPackets);
4970 pw.print(" Wifi packets sent: "); pw.println(wifiTxTotalPackets);
4971
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004972 sb.setLength(0);
4973 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004974 sb.append(" Wifi states:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004975 didOne = false;
4976 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004977 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004978 if (time == 0) {
4979 continue;
4980 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004981 sb.append("\n ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004982 didOne = true;
4983 sb.append(WIFI_STATE_NAMES[i]);
4984 sb.append(" ");
4985 formatTimeMs(sb, time/1000);
4986 sb.append("(");
4987 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4988 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004989 }
4990 if (!didOne) sb.append(" (no activity)");
4991 pw.println(sb.toString());
4992
4993 sb.setLength(0);
4994 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004995 sb.append(" Wifi supplicant states:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004996 didOne = false;
4997 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4998 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4999 if (time == 0) {
5000 continue;
5001 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07005002 sb.append("\n ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005003 didOne = true;
5004 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
5005 sb.append(" ");
5006 formatTimeMs(sb, time/1000);
5007 sb.append("(");
5008 sb.append(formatRatioLocked(time, whichBatteryRealtime));
5009 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005010 }
5011 if (!didOne) sb.append(" (no activity)");
5012 pw.println(sb.toString());
5013
5014 sb.setLength(0);
5015 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07005016 sb.append(" Wifi Rx signal strength (RSSI):");
5017 final String[] wifiRxSignalStrengthDescription = new String[]{
5018 "very poor (less than -88.75dBm): ",
5019 "poor (-88.75 to -77.5dBm): ",
5020 "moderate (-77.5dBm to -66.25dBm): ",
5021 "good (-66.25dBm to -55dBm): ",
5022 "great (greater than -55dBm): "};
Dianne Hackborn3251b902014-06-20 14:40:53 -07005023 didOne = false;
Siddharth Ray3c648c42017-10-02 17:30:58 -07005024 final int numWifiRxBins = Math.min(NUM_WIFI_SIGNAL_STRENGTH_BINS,
5025 wifiRxSignalStrengthDescription.length);
5026 for (int i=0; i<numWifiRxBins; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07005027 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
5028 if (time == 0) {
5029 continue;
5030 }
5031 sb.append("\n ");
5032 sb.append(prefix);
5033 didOne = true;
Siddharth Ray3c648c42017-10-02 17:30:58 -07005034 sb.append(" ");
5035 sb.append(wifiRxSignalStrengthDescription[i]);
Dianne Hackborn3251b902014-06-20 14:40:53 -07005036 formatTimeMs(sb, time/1000);
5037 sb.append("(");
5038 sb.append(formatRatioLocked(time, whichBatteryRealtime));
5039 sb.append(") ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005040 }
5041 if (!didOne) sb.append(" (no activity)");
5042 pw.println(sb.toString());
5043
Adam Lesinski50e47602015-12-04 17:04:54 -08005044 pw.print(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07005045 sb.setLength(0);
5046 sb.append(prefix);
Siddharth Ray78ccaf52017-12-23 16:16:21 -08005047 sb.append(" GPS Statistics:");
5048 pw.println(sb.toString());
5049
5050 sb.setLength(0);
5051 sb.append(prefix);
5052 sb.append(" GPS signal quality (Top 4 Average CN0):");
5053 final String[] gpsSignalQualityDescription = new String[]{
5054 "poor (less than 20 dBHz): ",
5055 "good (greater than 20 dBHz): "};
5056 final int numGpsSignalQualityBins = Math.min(GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS,
5057 gpsSignalQualityDescription.length);
5058 for (int i=0; i<numGpsSignalQualityBins; i++) {
5059 final long time = getGpsSignalQualityTime(i, rawRealtime, which);
5060 sb.append("\n ");
5061 sb.append(prefix);
5062 sb.append(" ");
5063 sb.append(gpsSignalQualityDescription[i]);
5064 formatTimeMs(sb, time/1000);
5065 sb.append("(");
5066 sb.append(formatRatioLocked(time, whichBatteryRealtime));
5067 sb.append(") ");
5068 }
5069 pw.println(sb.toString());
5070
5071 final long gpsBatteryDrainMaMs = getGpsBatteryDrainMaMs();
5072 if (gpsBatteryDrainMaMs > 0) {
5073 pw.print(prefix);
5074 sb.setLength(0);
5075 sb.append(prefix);
Blake Kragtenbff255d2019-03-05 15:44:31 -08005076 sb.append(" GPS Battery Drain: ");
5077 sb.append(new DecimalFormat("#.##").format(
5078 ((double) gpsBatteryDrainMaMs) / (3600 * 1000)));
5079 sb.append("mAh");
Siddharth Ray78ccaf52017-12-23 16:16:21 -08005080 pw.println(sb.toString());
5081 }
5082
5083 pw.print(prefix);
5084 sb.setLength(0);
5085 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07005086 sb.append(" CONNECTIVITY POWER SUMMARY END");
5087 pw.println(sb.toString());
5088 pw.println("");
5089
5090 pw.print(prefix);
Adam Lesinski50e47602015-12-04 17:04:54 -08005091 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
5092 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
5093
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005094 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
5095 sb.setLength(0);
5096 sb.append(prefix);
5097 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
5098 pw.println(sb.toString());
5099
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005100 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
5101 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07005102
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005103 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07005104
Mike Ma99e57c32018-03-15 14:40:37 -07005105 pw.print(prefix); pw.println(" Device battery use since last full charge");
5106 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
5107 pw.println(getLowDischargeAmountSinceCharge());
5108 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
5109 pw.println(getHighDischargeAmountSinceCharge());
5110 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
5111 pw.println(getDischargeAmountScreenOnSinceCharge());
5112 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
5113 pw.println(getDischargeAmountScreenOffSinceCharge());
5114 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
5115 pw.println(getDischargeAmountScreenDozeSinceCharge());
5116 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08005117
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005118 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005119 helper.create(this);
5120 helper.refreshStats(which, UserHandle.USER_ALL);
5121 List<BatterySipper> sippers = helper.getUsageList();
5122 if (sippers != null && sippers.size() > 0) {
5123 pw.print(prefix); pw.println(" Estimated power use (mAh):");
5124 pw.print(prefix); pw.print(" Capacity: ");
5125 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08005126 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07005127 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
5128 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
5129 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
5130 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005131 pw.println();
5132 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005133 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005134 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005135 switch (bs.drainType) {
Kweku Adamsef728952018-04-10 17:56:16 -07005136 case AMBIENT_DISPLAY:
5137 pw.print(" Ambient display: ");
5138 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005139 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005140 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005141 break;
5142 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005143 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005144 break;
5145 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005146 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005147 break;
5148 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005149 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005150 break;
5151 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005152 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005153 break;
5154 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005155 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005156 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005157 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005158 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005159 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005160 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005161 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005162 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005163 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005164 break;
5165 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005166 pw.print(" User "); pw.print(bs.userId);
5167 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005168 break;
5169 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005170 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005171 break;
5172 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005173 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005174 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005175 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005176 pw.print(" Camera: ");
5177 break;
5178 default:
5179 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005180 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005181 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005182 printmAh(pw, bs.totalPowerMah);
5183
Adam Lesinski57123002015-06-12 16:12:07 -07005184 if (bs.usagePowerMah != bs.totalPowerMah) {
5185 // If the usage (generic power) isn't the whole amount, we list out
5186 // what components are involved in the calculation.
5187
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005188 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07005189 if (bs.usagePowerMah != 0) {
5190 pw.print(" usage=");
5191 printmAh(pw, bs.usagePowerMah);
5192 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005193 if (bs.cpuPowerMah != 0) {
5194 pw.print(" cpu=");
5195 printmAh(pw, bs.cpuPowerMah);
5196 }
5197 if (bs.wakeLockPowerMah != 0) {
5198 pw.print(" wake=");
5199 printmAh(pw, bs.wakeLockPowerMah);
5200 }
5201 if (bs.mobileRadioPowerMah != 0) {
5202 pw.print(" radio=");
5203 printmAh(pw, bs.mobileRadioPowerMah);
5204 }
5205 if (bs.wifiPowerMah != 0) {
5206 pw.print(" wifi=");
5207 printmAh(pw, bs.wifiPowerMah);
5208 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005209 if (bs.bluetoothPowerMah != 0) {
5210 pw.print(" bt=");
5211 printmAh(pw, bs.bluetoothPowerMah);
5212 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005213 if (bs.gpsPowerMah != 0) {
5214 pw.print(" gps=");
5215 printmAh(pw, bs.gpsPowerMah);
5216 }
5217 if (bs.sensorPowerMah != 0) {
5218 pw.print(" sensor=");
5219 printmAh(pw, bs.sensorPowerMah);
5220 }
5221 if (bs.cameraPowerMah != 0) {
5222 pw.print(" camera=");
5223 printmAh(pw, bs.cameraPowerMah);
5224 }
5225 if (bs.flashlightPowerMah != 0) {
5226 pw.print(" flash=");
5227 printmAh(pw, bs.flashlightPowerMah);
5228 }
5229 pw.print(" )");
5230 }
Bookatz17d7d9d2017-06-08 14:50:46 -07005231
5232 // If there is additional smearing information, include it.
5233 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
5234 pw.print(" Including smearing: ");
5235 printmAh(pw, bs.totalSmearedPowerMah);
5236 pw.print(" (");
5237 if (bs.screenPowerMah != 0) {
5238 pw.print(" screen=");
5239 printmAh(pw, bs.screenPowerMah);
5240 }
5241 if (bs.proportionalSmearMah != 0) {
5242 pw.print(" proportional=");
5243 printmAh(pw, bs.proportionalSmearMah);
5244 }
5245 pw.print(" )");
5246 }
5247 if (bs.shouldHide) {
5248 pw.print(" Excluded from smearing");
5249 }
5250
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005251 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005252 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08005253 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005254 }
5255
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005256 sippers = helper.getMobilemsppList();
5257 if (sippers != null && sippers.size() > 0) {
5258 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005259 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005260 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005261 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005262 sb.setLength(0);
5263 sb.append(prefix); sb.append(" Uid ");
5264 UserHandle.formatUid(sb, bs.uidObj.getUid());
5265 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
5266 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
5267 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005268 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005269 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005270 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005271 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005272 sb.setLength(0);
5273 sb.append(prefix);
5274 sb.append(" TOTAL TIME: ");
5275 formatTimeMs(sb, totalTime);
5276 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
5277 sb.append(")");
5278 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005279 pw.println();
5280 }
5281
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005282 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
5283 @Override
5284 public int compare(TimerEntry lhs, TimerEntry rhs) {
5285 long lhsTime = lhs.mTime;
5286 long rhsTime = rhs.mTime;
5287 if (lhsTime < rhsTime) {
5288 return 1;
5289 }
5290 if (lhsTime > rhsTime) {
5291 return -1;
5292 }
5293 return 0;
5294 }
5295 };
5296
5297 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005298 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
5299 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005300 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005301 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
5302 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
5303 : kernelWakelocks.entrySet()) {
5304 final BatteryStats.Timer timer = ent.getValue();
5305 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005306 if (totalTimeMillis > 0) {
5307 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
5308 }
5309 }
5310 if (ktimers.size() > 0) {
5311 Collections.sort(ktimers, timerComparator);
5312 pw.print(prefix); pw.println(" All kernel wake locks:");
5313 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005314 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005315 String linePrefix = ": ";
5316 sb.setLength(0);
5317 sb.append(prefix);
5318 sb.append(" Kernel Wake lock ");
5319 sb.append(timer.mName);
5320 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
5321 which, linePrefix);
5322 if (!linePrefix.equals(": ")) {
5323 sb.append(" realtime");
5324 // Only print out wake locks that were held
5325 pw.println(sb.toString());
5326 }
5327 }
5328 pw.println();
5329 }
5330 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005331
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005332 if (timers.size() > 0) {
5333 Collections.sort(timers, timerComparator);
5334 pw.print(prefix); pw.println(" All partial wake locks:");
5335 for (int i=0; i<timers.size(); i++) {
5336 TimerEntry timer = timers.get(i);
5337 sb.setLength(0);
5338 sb.append(" Wake lock ");
5339 UserHandle.formatUid(sb, timer.mId);
5340 sb.append(" ");
5341 sb.append(timer.mName);
5342 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5343 sb.append(" realtime");
5344 pw.println(sb.toString());
5345 }
5346 timers.clear();
5347 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08005348 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005349
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005350 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005351 if (wakeupReasons.size() > 0) {
5352 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005353 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005354 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005355 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005356 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
5357 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005358 }
5359 Collections.sort(reasons, timerComparator);
5360 for (int i=0; i<reasons.size(); i++) {
5361 TimerEntry timer = reasons.get(i);
5362 String linePrefix = ": ";
5363 sb.setLength(0);
5364 sb.append(prefix);
5365 sb.append(" Wakeup reason ");
5366 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005367 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5368 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005369 pw.println(sb.toString());
5370 }
5371 pw.println();
5372 }
Dianne Hackborn81038902012-11-26 17:04:09 -08005373 }
Evan Millar22ac0432009-03-31 11:33:18 -07005374
James Carr2dd7e5e2016-07-20 18:48:39 -07005375 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
Bookatz50df7112017-08-04 14:53:26 -07005376 if (mMemoryStats.size() > 0) {
5377 pw.println(" Memory Stats");
5378 for (int i = 0; i < mMemoryStats.size(); i++) {
5379 sb.setLength(0);
5380 sb.append(" Bandwidth ");
5381 sb.append(mMemoryStats.keyAt(i));
5382 sb.append(" Time ");
5383 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
5384 pw.println(sb.toString());
5385 }
5386 pw.println();
5387 }
5388
5389 final Map<String, ? extends Timer> rpmStats = getRpmStats();
5390 if (rpmStats.size() > 0) {
5391 pw.print(prefix); pw.println(" Resource Power Manager Stats");
5392 if (rpmStats.size() > 0) {
5393 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
5394 final String timerName = ent.getKey();
5395 final Timer timer = ent.getValue();
5396 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5397 }
5398 }
5399 pw.println();
5400 }
Bookatz82b341172017-09-07 19:06:08 -07005401 if (SCREEN_OFF_RPM_STATS_ENABLED) {
5402 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
Bookatz50df7112017-08-04 14:53:26 -07005403 if (screenOffRpmStats.size() > 0) {
Bookatz82b341172017-09-07 19:06:08 -07005404 pw.print(prefix);
5405 pw.println(" Resource Power Manager Stats for when screen was off");
5406 if (screenOffRpmStats.size() > 0) {
5407 for (Map.Entry<String, ? extends Timer> ent : screenOffRpmStats.entrySet()) {
5408 final String timerName = ent.getKey();
5409 final Timer timer = ent.getValue();
5410 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5411 }
Bookatz50df7112017-08-04 14:53:26 -07005412 }
Bookatz82b341172017-09-07 19:06:08 -07005413 pw.println();
Bookatz50df7112017-08-04 14:53:26 -07005414 }
James Carr2dd7e5e2016-07-20 18:48:39 -07005415 }
5416
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005417 final long[] cpuFreqs = getCpuFreqs();
5418 if (cpuFreqs != null) {
5419 sb.setLength(0);
Bookatz50df7112017-08-04 14:53:26 -07005420 sb.append(" CPU freqs:");
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005421 for (int i = 0; i < cpuFreqs.length; ++i) {
5422 sb.append(" " + cpuFreqs[i]);
5423 }
5424 pw.println(sb.toString());
Bookatz50df7112017-08-04 14:53:26 -07005425 pw.println();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005426 }
5427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005428 for (int iu=0; iu<NU; iu++) {
5429 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005430 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005431 continue;
5432 }
Bookatzc8c44962017-05-11 12:12:54 -07005433
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005434 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07005435
5436 pw.print(prefix);
5437 pw.print(" ");
5438 UserHandle.formatUid(pw, uid);
5439 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005440 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005441
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005442 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
5443 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
5444 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
5445 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005446 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
5447 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
5448
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005449 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
5450 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005451 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
5452 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005453
5454 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
5455 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
5456
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005457 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
5458 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
5459 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08005460 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
5461 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5462 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
5463 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005464 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005465
Adam Lesinski5f056f62016-07-14 16:56:08 -07005466 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
5467 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
5468
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005469 if (mobileRxBytes > 0 || mobileTxBytes > 0
5470 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005471 pw.print(prefix); pw.print(" Mobile network: ");
5472 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005473 pw.print(formatBytesLocked(mobileTxBytes));
5474 pw.print(" sent (packets "); pw.print(mobileRxPackets);
5475 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005476 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005477 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
5478 sb.setLength(0);
5479 sb.append(prefix); sb.append(" Mobile radio active: ");
5480 formatTimeMs(sb, uidMobileActiveTime / 1000);
5481 sb.append("(");
5482 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
5483 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
5484 long packets = mobileRxPackets + mobileTxPackets;
5485 if (packets == 0) {
5486 packets = 1;
5487 }
5488 sb.append(" @ ");
5489 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
5490 sb.append(" mspp");
5491 pw.println(sb.toString());
5492 }
5493
Adam Lesinski5f056f62016-07-14 16:56:08 -07005494 if (mobileWakeup > 0) {
5495 sb.setLength(0);
5496 sb.append(prefix);
5497 sb.append(" Mobile radio AP wakeups: ");
5498 sb.append(mobileWakeup);
5499 pw.println(sb.toString());
5500 }
5501
Siddharth Rayb50a6842017-12-14 15:15:28 -08005502 printControllerActivityIfInteresting(pw, sb, prefix + " ",
5503 CELLULAR_CONTROLLER_NAME, u.getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005504
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005505 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005506 pw.print(prefix); pw.print(" Wi-Fi network: ");
5507 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005508 pw.print(formatBytesLocked(wifiTxBytes));
5509 pw.print(" sent (packets "); pw.print(wifiRxPackets);
5510 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005511 }
5512
Dianne Hackborn62793e42015-03-09 11:15:41 -07005513 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08005514 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005515 || uidWifiRunningTime != 0) {
5516 sb.setLength(0);
5517 sb.append(prefix); sb.append(" Wifi Running: ");
5518 formatTimeMs(sb, uidWifiRunningTime / 1000);
5519 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
5520 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07005521 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005522 formatTimeMs(sb, fullWifiLockOnTime / 1000);
5523 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
5524 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08005525 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005526 formatTimeMs(sb, wifiScanTime / 1000);
5527 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07005528 whichBatteryRealtime)); sb.append(") ");
5529 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08005530 sb.append("x\n");
5531 // actual and background times are unpooled and since reset (regardless of 'which')
5532 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
5533 formatTimeMs(sb, wifiScanActualTime / 1000);
5534 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
5535 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5536 sb.append(") ");
5537 sb.append(wifiScanCount);
5538 sb.append("x\n");
5539 sb.append(prefix); sb.append(" Background Wifi Scan: ");
5540 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
5541 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
5542 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5543 sb.append(") ");
5544 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07005545 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005546 pw.println(sb.toString());
5547 }
5548
Adam Lesinski5f056f62016-07-14 16:56:08 -07005549 if (wifiWakeup > 0) {
5550 sb.setLength(0);
5551 sb.append(prefix);
5552 sb.append(" WiFi AP wakeups: ");
5553 sb.append(wifiWakeup);
5554 pw.println(sb.toString());
5555 }
5556
Siddharth Rayb50a6842017-12-14 15:15:28 -08005557 printControllerActivityIfInteresting(pw, sb, prefix + " ", WIFI_CONTROLLER_NAME,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005558 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07005559
Adam Lesinski50e47602015-12-04 17:04:54 -08005560 if (btRxBytes > 0 || btTxBytes > 0) {
5561 pw.print(prefix); pw.print(" Bluetooth network: ");
5562 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
5563 pw.print(formatBytesLocked(btTxBytes));
5564 pw.println(" sent");
5565 }
5566
Bookatz867c0d72017-03-07 18:23:42 -08005567 final Timer bleTimer = u.getBluetoothScanTimer();
5568 if (bleTimer != null) {
5569 // Convert from microseconds to milliseconds with rounding
5570 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
5571 / 1000;
5572 if (totalTimeMs != 0) {
5573 final int count = bleTimer.getCountLocked(which);
5574 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
5575 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005576 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5577 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
5578 final long actualTimeMsBg = bleTimerBg != null ?
5579 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005580 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07005581 final int resultCount = u.getBluetoothScanResultCounter() != null ?
5582 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005583 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
5584 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
5585 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
5586 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
5587 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
5588 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5589 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
5590 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
5591 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
5592 final Timer unoptimizedScanTimerBg =
5593 u.getBluetoothUnoptimizedScanBackgroundTimer();
5594 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
5595 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5596 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
5597 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005598
5599 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08005600 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07005601 sb.append(prefix);
5602 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08005603 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07005604 sb.append(" (");
5605 sb.append(count);
5606 sb.append(" times)");
5607 if (bleTimer.isRunningLocked()) {
5608 sb.append(" (currently running)");
5609 }
5610 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005611 }
Bookatzb1f04f32017-05-19 13:57:32 -07005612
5613 sb.append(prefix);
5614 sb.append(" Bluetooth Scan (total actual realtime): ");
5615 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
5616 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005617 sb.append(count);
5618 sb.append(" times)");
5619 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07005620 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08005621 }
Bookatzb1f04f32017-05-19 13:57:32 -07005622 sb.append("\n");
5623 if (actualTimeMsBg > 0 || countBg > 0) {
5624 sb.append(prefix);
5625 sb.append(" Bluetooth Scan (background realtime): ");
5626 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
5627 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005628 sb.append(countBg);
5629 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07005630 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
5631 sb.append(" (currently running in background)");
5632 }
5633 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005634 }
Bookatzb1f04f32017-05-19 13:57:32 -07005635
5636 sb.append(prefix);
5637 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07005638 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07005639 sb.append(" (");
5640 sb.append(resultCountBg);
5641 sb.append(" in background)");
5642
5643 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
5644 sb.append("\n");
5645 sb.append(prefix);
5646 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
5647 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
5648 sb.append(" (max ");
5649 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
5650 sb.append(")");
5651 if (unoptimizedScanTimer != null
5652 && unoptimizedScanTimer.isRunningLocked()) {
5653 sb.append(" (currently running unoptimized)");
5654 }
5655 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
5656 sb.append("\n");
5657 sb.append(prefix);
5658 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
5659 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
5660 sb.append(" (max ");
5661 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
5662 sb.append(")");
5663 if (unoptimizedScanTimerBg.isRunningLocked()) {
5664 sb.append(" (currently running unoptimized in background)");
5665 }
5666 }
5667 }
Bookatz867c0d72017-03-07 18:23:42 -08005668 pw.println(sb.toString());
5669 uidActivity = true;
5670 }
5671 }
5672
5673
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005674
Dianne Hackborn617f8772009-03-31 15:04:46 -07005675 if (u.hasUserActivity()) {
5676 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07005677 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005678 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005679 if (val != 0) {
5680 if (!hasData) {
5681 sb.setLength(0);
5682 sb.append(" User activity: ");
5683 hasData = true;
5684 } else {
5685 sb.append(", ");
5686 }
5687 sb.append(val);
5688 sb.append(" ");
5689 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
5690 }
5691 }
5692 if (hasData) {
5693 pw.println(sb.toString());
5694 }
5695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005696
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005697 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
5698 = u.getWakelockStats();
5699 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005700 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005701 int countWakelock = 0;
5702 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
5703 final Uid.Wakelock wl = wakelocks.valueAt(iw);
5704 String linePrefix = ": ";
5705 sb.setLength(0);
5706 sb.append(prefix);
5707 sb.append(" Wake lock ");
5708 sb.append(wakelocks.keyAt(iw));
5709 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
5710 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005711 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
5712 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005713 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005714 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
5715 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005716 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
5717 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005718 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
5719 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07005720 sb.append(" realtime");
5721 pw.println(sb.toString());
5722 uidActivity = true;
5723 countWakelock++;
5724
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005725 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
5726 rawRealtime, which);
5727 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
5728 rawRealtime, which);
5729 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
5730 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005731 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07005732 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005733 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005734 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07005735 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
5736 // pooled and therefore just a lower bound)
5737 long actualTotalPartialWakelock = 0;
5738 long actualBgPartialWakelock = 0;
5739 if (u.getAggregatedPartialWakelockTimer() != null) {
5740 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
5741 // Convert from microseconds to milliseconds with rounding
5742 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07005743 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07005744 final Timer bgAggTimer = aggTimer.getSubTimer();
5745 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005746 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07005747 }
5748
5749 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
5750 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
5751 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005752 sb.setLength(0);
5753 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005754 sb.append(" TOTAL wake: ");
5755 boolean needComma = false;
5756 if (totalFullWakelock != 0) {
5757 needComma = true;
5758 formatTimeMs(sb, totalFullWakelock);
5759 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005760 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005761 if (totalPartialWakelock != 0) {
5762 if (needComma) {
5763 sb.append(", ");
5764 }
5765 needComma = true;
5766 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07005767 sb.append("blamed partial");
5768 }
5769 if (actualTotalPartialWakelock != 0) {
5770 if (needComma) {
5771 sb.append(", ");
5772 }
5773 needComma = true;
5774 formatTimeMs(sb, actualTotalPartialWakelock);
5775 sb.append("actual partial");
5776 }
5777 if (actualBgPartialWakelock != 0) {
5778 if (needComma) {
5779 sb.append(", ");
5780 }
5781 needComma = true;
5782 formatTimeMs(sb, actualBgPartialWakelock);
5783 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005784 }
5785 if (totalWindowWakelock != 0) {
5786 if (needComma) {
5787 sb.append(", ");
5788 }
5789 needComma = true;
5790 formatTimeMs(sb, totalWindowWakelock);
5791 sb.append("window");
5792 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005793 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07005794 if (needComma) {
5795 sb.append(",");
5796 }
5797 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005798 formatTimeMs(sb, totalDrawWakelock);
5799 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07005800 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005801 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005802 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005803 }
5804 }
5805
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07005806 // Calculate multicast wakelock stats
5807 final Timer mcTimer = u.getMulticastWakelockStats();
5808 if (mcTimer != null) {
5809 final long multicastWakeLockTimeMicros = mcTimer.getTotalTimeLocked(rawRealtime, which);
5810 final int multicastWakeLockCount = mcTimer.getCountLocked(which);
5811
5812 if (multicastWakeLockTimeMicros > 0) {
5813 sb.setLength(0);
5814 sb.append(prefix);
5815 sb.append(" WiFi Multicast Wakelock");
5816 sb.append(" count = ");
5817 sb.append(multicastWakeLockCount);
5818 sb.append(" time = ");
5819 formatTimeMsNoSpace(sb, (multicastWakeLockTimeMicros + 500) / 1000);
5820 pw.println(sb.toString());
5821 }
5822 }
5823
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005824 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
5825 for (int isy=syncs.size()-1; isy>=0; isy--) {
5826 final Timer timer = syncs.valueAt(isy);
5827 // Convert from microseconds to milliseconds with rounding
5828 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5829 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07005830 final Timer bgTimer = timer.getSubTimer();
5831 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005832 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07005833 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005834 sb.setLength(0);
5835 sb.append(prefix);
5836 sb.append(" Sync ");
5837 sb.append(syncs.keyAt(isy));
5838 sb.append(": ");
5839 if (totalTime != 0) {
5840 formatTimeMs(sb, totalTime);
5841 sb.append("realtime (");
5842 sb.append(count);
5843 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07005844 if (bgTime > 0) {
5845 sb.append(", ");
5846 formatTimeMs(sb, bgTime);
5847 sb.append("background (");
5848 sb.append(bgCount);
5849 sb.append(" times)");
5850 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005851 } else {
5852 sb.append("(not used)");
5853 }
5854 pw.println(sb.toString());
5855 uidActivity = true;
5856 }
5857
5858 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
5859 for (int ij=jobs.size()-1; ij>=0; ij--) {
5860 final Timer timer = jobs.valueAt(ij);
5861 // Convert from microseconds to milliseconds with rounding
5862 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5863 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07005864 final Timer bgTimer = timer.getSubTimer();
5865 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005866 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07005867 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005868 sb.setLength(0);
5869 sb.append(prefix);
5870 sb.append(" Job ");
5871 sb.append(jobs.keyAt(ij));
5872 sb.append(": ");
5873 if (totalTime != 0) {
5874 formatTimeMs(sb, totalTime);
5875 sb.append("realtime (");
5876 sb.append(count);
5877 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07005878 if (bgTime > 0) {
5879 sb.append(", ");
5880 formatTimeMs(sb, bgTime);
5881 sb.append("background (");
5882 sb.append(bgCount);
5883 sb.append(" times)");
5884 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005885 } else {
5886 sb.append("(not used)");
5887 }
5888 pw.println(sb.toString());
5889 uidActivity = true;
5890 }
5891
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005892 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
5893 for (int ic=completions.size()-1; ic>=0; ic--) {
5894 SparseIntArray types = completions.valueAt(ic);
5895 if (types != null) {
5896 pw.print(prefix);
5897 pw.print(" Job Completions ");
5898 pw.print(completions.keyAt(ic));
5899 pw.print(":");
5900 for (int it=0; it<types.size(); it++) {
5901 pw.print(" ");
5902 pw.print(JobParameters.getReasonName(types.keyAt(it)));
5903 pw.print("(");
5904 pw.print(types.valueAt(it));
5905 pw.print("x)");
5906 }
5907 pw.println();
5908 }
5909 }
5910
Amith Yamasani977e11f2018-02-16 11:29:54 -08005911 u.getDeferredJobsLineLocked(sb, which);
5912 if (sb.length() > 0) {
5913 pw.print(" Jobs deferred on launch "); pw.println(sb.toString());
5914 }
5915
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005916 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
5917 prefix, "Flashlight");
5918 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
5919 prefix, "Camera");
5920 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
5921 prefix, "Video");
5922 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
5923 prefix, "Audio");
5924
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005925 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
5926 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005927 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005928 final Uid.Sensor se = sensors.valueAt(ise);
5929 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005930 sb.setLength(0);
5931 sb.append(prefix);
5932 sb.append(" Sensor ");
5933 int handle = se.getHandle();
5934 if (handle == Uid.Sensor.GPS) {
5935 sb.append("GPS");
5936 } else {
5937 sb.append(handle);
5938 }
5939 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005940
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005941 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005942 if (timer != null) {
5943 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005944 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5945 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005946 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005947 final Timer bgTimer = se.getSensorBackgroundTime();
5948 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005949 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5950 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5951 final long bgActualTime = bgTimer != null ?
5952 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5953
Dianne Hackborn61659e52014-07-09 16:13:01 -07005954 //timer.logState();
5955 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005956 if (actualTime != totalTime) {
5957 formatTimeMs(sb, totalTime);
5958 sb.append("blamed realtime, ");
5959 }
5960
5961 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005962 sb.append("realtime (");
5963 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005964 sb.append(" times)");
5965
5966 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005967 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005968 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5969 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005970 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005971 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005973 } else {
5974 sb.append("(not used)");
5975 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005976 } else {
5977 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005978 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005979
5980 pw.println(sb.toString());
5981 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005982 }
5983
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005984 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5985 "Vibrator");
5986 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5987 prefix, "Foreground activities");
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005988 uidActivity |= printTimer(pw, sb, u.getForegroundServiceTimer(), rawRealtime, which,
5989 prefix, "Foreground services");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005990
Dianne Hackborn61659e52014-07-09 16:13:01 -07005991 long totalStateTime = 0;
5992 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5993 long time = u.getProcessStateTime(ips, rawRealtime, which);
5994 if (time > 0) {
5995 totalStateTime += time;
5996 sb.setLength(0);
5997 sb.append(prefix);
5998 sb.append(" ");
5999 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
6000 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08006001 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006002 pw.println(sb.toString());
6003 uidActivity = true;
6004 }
6005 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006006 if (totalStateTime > 0) {
6007 sb.setLength(0);
6008 sb.append(prefix);
6009 sb.append(" Total running: ");
6010 formatTimeMs(sb, (totalStateTime + 500) / 1000);
6011 pw.println(sb.toString());
6012 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07006013
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006014 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
6015 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07006016 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006017 sb.setLength(0);
6018 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07006019 sb.append(" Total cpu time: u=");
6020 formatTimeMs(sb, userCpuTimeUs / 1000);
6021 sb.append("s=");
6022 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006023 pw.println(sb.toString());
6024 }
6025
Sudheer Shanka9b735c52017-05-09 18:26:18 -07006026 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
6027 if (cpuFreqTimes != null) {
6028 sb.setLength(0);
6029 sb.append(" Total cpu time per freq:");
6030 for (int i = 0; i < cpuFreqTimes.length; ++i) {
6031 sb.append(" " + cpuFreqTimes[i]);
6032 }
6033 pw.println(sb.toString());
6034 }
6035 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
6036 if (screenOffCpuFreqTimes != null) {
6037 sb.setLength(0);
6038 sb.append(" Total screen-off cpu time per freq:");
6039 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
6040 sb.append(" " + screenOffCpuFreqTimes[i]);
6041 }
6042 pw.println(sb.toString());
6043 }
6044
Sudheer Shankab2f83c12017-11-13 19:25:01 -08006045 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
6046 final long[] cpuTimes = u.getCpuFreqTimes(which, procState);
6047 if (cpuTimes != null) {
6048 sb.setLength(0);
6049 sb.append(" Cpu times per freq at state "
6050 + Uid.PROCESS_STATE_NAMES[procState] + ":");
6051 for (int i = 0; i < cpuTimes.length; ++i) {
6052 sb.append(" " + cpuTimes[i]);
6053 }
6054 pw.println(sb.toString());
6055 }
6056
6057 final long[] screenOffCpuTimes = u.getScreenOffCpuFreqTimes(which, procState);
6058 if (screenOffCpuTimes != null) {
6059 sb.setLength(0);
6060 sb.append(" Screen-off cpu times per freq at state "
6061 + Uid.PROCESS_STATE_NAMES[procState] + ":");
6062 for (int i = 0; i < screenOffCpuTimes.length; ++i) {
6063 sb.append(" " + screenOffCpuTimes[i]);
6064 }
6065 pw.println(sb.toString());
6066 }
6067 }
6068
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006069 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
6070 = u.getProcessStats();
6071 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
6072 final Uid.Proc ps = processStats.valueAt(ipr);
6073 long userTime;
6074 long systemTime;
6075 long foregroundTime;
6076 int starts;
6077 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006078
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006079 userTime = ps.getUserTime(which);
6080 systemTime = ps.getSystemTime(which);
6081 foregroundTime = ps.getForegroundTime(which);
6082 starts = ps.getStarts(which);
6083 final int numCrashes = ps.getNumCrashes(which);
6084 final int numAnrs = ps.getNumAnrs(which);
6085 numExcessive = which == STATS_SINCE_CHARGED
6086 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006087
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006088 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
6089 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
6090 sb.setLength(0);
6091 sb.append(prefix); sb.append(" Proc ");
6092 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
6093 sb.append(prefix); sb.append(" CPU: ");
6094 formatTimeMs(sb, userTime); sb.append("usr + ");
6095 formatTimeMs(sb, systemTime); sb.append("krn ; ");
6096 formatTimeMs(sb, foregroundTime); sb.append("fg");
6097 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
6098 sb.append("\n"); sb.append(prefix); sb.append(" ");
6099 boolean hasOne = false;
6100 if (starts != 0) {
6101 hasOne = true;
6102 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07006103 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006104 if (numCrashes != 0) {
6105 if (hasOne) {
6106 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006107 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006108 hasOne = true;
6109 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006110 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006111 if (numAnrs != 0) {
6112 if (hasOne) {
6113 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006114 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006115 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006116 }
6117 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006118 pw.println(sb.toString());
6119 for (int e=0; e<numExcessive; e++) {
6120 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
6121 if (ew != null) {
6122 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07006123 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006124 pw.print("cpu");
6125 } else {
6126 pw.print("unknown");
6127 }
6128 pw.print(" use: ");
6129 TimeUtils.formatDuration(ew.usedTime, pw);
6130 pw.print(" over ");
6131 TimeUtils.formatDuration(ew.overTime, pw);
6132 if (ew.overTime != 0) {
6133 pw.print(" (");
6134 pw.print((ew.usedTime*100)/ew.overTime);
6135 pw.println("%)");
6136 }
6137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006138 }
6139 uidActivity = true;
6140 }
6141 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006142
6143 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
6144 = u.getPackageStats();
6145 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
6146 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
6147 pw.println(":");
6148 boolean apkActivity = false;
6149 final Uid.Pkg ps = packageStats.valueAt(ipkg);
6150 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
6151 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
6152 pw.print(prefix); pw.print(" Wakeup alarm ");
6153 pw.print(alarms.keyAt(iwa)); pw.print(": ");
6154 pw.print(alarms.valueAt(iwa).getCountLocked(which));
6155 pw.println(" times");
6156 apkActivity = true;
6157 }
6158 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
6159 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
6160 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
6161 final long startTime = ss.getStartTime(batteryUptime, which);
6162 final int starts = ss.getStarts(which);
6163 final int launches = ss.getLaunches(which);
6164 if (startTime != 0 || starts != 0 || launches != 0) {
6165 sb.setLength(0);
6166 sb.append(prefix); sb.append(" Service ");
6167 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
6168 sb.append(prefix); sb.append(" Created for: ");
6169 formatTimeMs(sb, startTime / 1000);
6170 sb.append("uptime\n");
6171 sb.append(prefix); sb.append(" Starts: ");
6172 sb.append(starts);
6173 sb.append(", launches: "); sb.append(launches);
6174 pw.println(sb.toString());
6175 apkActivity = true;
6176 }
6177 }
6178 if (!apkActivity) {
6179 pw.print(prefix); pw.println(" (nothing executed)");
6180 }
6181 uidActivity = true;
6182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006183 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006184 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006185 }
6186 }
6187 }
6188
Kweku Adams71a95312018-04-16 16:54:24 -07006189 static void printBitDescriptions(StringBuilder sb, int oldval, int newval,
6190 HistoryTag wakelockTag, BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006191 int diff = oldval ^ newval;
6192 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006193 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006194 for (int i=0; i<descriptions.length; i++) {
6195 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006196 if ((diff&bd.mask) != 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006197 sb.append(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006198 if (bd.shift < 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006199 sb.append((newval & bd.mask) != 0 ? "+" : "-");
6200 sb.append(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006201 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
6202 didWake = true;
Kweku Adams71a95312018-04-16 16:54:24 -07006203 sb.append("=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006204 if (longNames) {
Kweku Adams71a95312018-04-16 16:54:24 -07006205 UserHandle.formatUid(sb, wakelockTag.uid);
6206 sb.append(":\"");
6207 sb.append(wakelockTag.string);
6208 sb.append("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006209 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006210 sb.append(wakelockTag.poolIdx);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006211 }
6212 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006213 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006214 sb.append(longNames ? bd.name : bd.shortName);
6215 sb.append("=");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006216 int val = (newval&bd.mask)>>bd.shift;
6217 if (bd.values != null && val >= 0 && val < bd.values.length) {
Kweku Adams71a95312018-04-16 16:54:24 -07006218 sb.append(longNames ? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006219 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006220 sb.append(val);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006221 }
6222 }
6223 }
6224 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006225 if (!didWake && wakelockTag != null) {
Kweku Adams71a95312018-04-16 16:54:24 -07006226 sb.append(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006227 if (longNames) {
Kweku Adams71a95312018-04-16 16:54:24 -07006228 UserHandle.formatUid(sb, wakelockTag.uid);
6229 sb.append(":\"");
6230 sb.append(wakelockTag.string);
6231 sb.append("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006232 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006233 sb.append(wakelockTag.poolIdx);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006234 }
6235 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006236 }
Mike Mac2f518a2017-09-19 16:06:03 -07006237
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006238 public void prepareForDumpLocked() {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006239 // We don't need to require subclasses implement this.
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006240 }
6241
6242 public static class HistoryPrinter {
6243 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006244 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006245 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006246 int oldStatus = -1;
6247 int oldHealth = -1;
6248 int oldPlug = -1;
6249 int oldTemp = -1;
6250 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006251 int oldChargeMAh = -1;
Blake Kragtencb308d92019-01-28 10:54:13 -08006252 double oldModemRailChargeMah = -1;
6253 double oldWifiRailChargeMah = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006254 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006255
Dianne Hackborn3251b902014-06-20 14:40:53 -07006256 void reset() {
6257 oldState = oldState2 = 0;
6258 oldLevel = -1;
6259 oldStatus = -1;
6260 oldHealth = -1;
6261 oldPlug = -1;
6262 oldTemp = -1;
6263 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006264 oldChargeMAh = -1;
Blake Kragtencb308d92019-01-28 10:54:13 -08006265 oldModemRailChargeMah = -1;
6266 oldWifiRailChargeMah = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07006267 }
6268
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006269 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006270 boolean verbose) {
Kweku Adams71a95312018-04-16 16:54:24 -07006271 pw.print(printNextItem(rec, baseTime, checkin, verbose));
6272 }
6273
6274 /** Print the next history item to proto. */
6275 public void printNextItem(ProtoOutputStream proto, HistoryItem rec, long baseTime,
6276 boolean verbose) {
6277 String item = printNextItem(rec, baseTime, true, verbose);
6278 for (String line : item.split("\n")) {
6279 proto.write(BatteryStatsServiceDumpHistoryProto.CSV_LINES, line);
6280 }
6281 }
6282
6283 private String printNextItem(HistoryItem rec, long baseTime, boolean checkin,
6284 boolean verbose) {
6285 StringBuilder item = new StringBuilder();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006286 if (!checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006287 item.append(" ");
6288 TimeUtils.formatDuration(
6289 rec.time - baseTime, item, TimeUtils.HUNDRED_DAY_FIELD_LEN);
6290 item.append(" (");
6291 item.append(rec.numReadInts);
6292 item.append(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006293 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006294 item.append(BATTERY_STATS_CHECKIN_VERSION); item.append(',');
6295 item.append(HISTORY_DATA); item.append(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006296 if (lastTime < 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006297 item.append(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006298 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006299 item.append(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006300 }
6301 lastTime = rec.time;
6302 }
6303 if (rec.cmd == HistoryItem.CMD_START) {
6304 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006305 item.append(":");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006306 }
Kweku Adams71a95312018-04-16 16:54:24 -07006307 item.append("START\n");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006308 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006309 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
6310 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006311 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006312 item.append(":");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006313 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006314 if (rec.cmd == HistoryItem.CMD_RESET) {
Kweku Adams71a95312018-04-16 16:54:24 -07006315 item.append("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006316 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006317 }
Kweku Adams71a95312018-04-16 16:54:24 -07006318 item.append("TIME:");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006319 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006320 item.append(rec.currentTime);
6321 item.append("\n");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006322 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006323 item.append(" ");
6324 item.append(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006325 rec.currentTime).toString());
Kweku Adams71a95312018-04-16 16:54:24 -07006326 item.append("\n");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006327 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006328 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
6329 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006330 item.append(":");
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006331 }
Kweku Adams71a95312018-04-16 16:54:24 -07006332 item.append("SHUTDOWN\n");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006333 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
6334 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006335 item.append(":");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006336 }
Kweku Adams71a95312018-04-16 16:54:24 -07006337 item.append("*OVERFLOW*\n");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006338 } else {
6339 if (!checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006340 if (rec.batteryLevel < 10) item.append("00");
6341 else if (rec.batteryLevel < 100) item.append("0");
6342 item.append(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006343 if (verbose) {
Kweku Adams71a95312018-04-16 16:54:24 -07006344 item.append(" ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006345 if (rec.states < 0) ;
Kweku Adams71a95312018-04-16 16:54:24 -07006346 else if (rec.states < 0x10) item.append("0000000");
6347 else if (rec.states < 0x100) item.append("000000");
6348 else if (rec.states < 0x1000) item.append("00000");
6349 else if (rec.states < 0x10000) item.append("0000");
6350 else if (rec.states < 0x100000) item.append("000");
6351 else if (rec.states < 0x1000000) item.append("00");
6352 else if (rec.states < 0x10000000) item.append("0");
6353 item.append(Integer.toHexString(rec.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006354 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006355 } else {
6356 if (oldLevel != rec.batteryLevel) {
6357 oldLevel = rec.batteryLevel;
Kweku Adams71a95312018-04-16 16:54:24 -07006358 item.append(",Bl="); item.append(rec.batteryLevel);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006359 }
6360 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006361 if (oldStatus != rec.batteryStatus) {
6362 oldStatus = rec.batteryStatus;
Kweku Adams71a95312018-04-16 16:54:24 -07006363 item.append(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006364 switch (oldStatus) {
6365 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Kweku Adams71a95312018-04-16 16:54:24 -07006366 item.append(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006367 break;
6368 case BatteryManager.BATTERY_STATUS_CHARGING:
Kweku Adams71a95312018-04-16 16:54:24 -07006369 item.append(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006370 break;
6371 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Kweku Adams71a95312018-04-16 16:54:24 -07006372 item.append(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006373 break;
6374 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Kweku Adams71a95312018-04-16 16:54:24 -07006375 item.append(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006376 break;
6377 case BatteryManager.BATTERY_STATUS_FULL:
Kweku Adams71a95312018-04-16 16:54:24 -07006378 item.append(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006379 break;
6380 default:
Kweku Adams71a95312018-04-16 16:54:24 -07006381 item.append(oldStatus);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006382 break;
6383 }
6384 }
6385 if (oldHealth != rec.batteryHealth) {
6386 oldHealth = rec.batteryHealth;
Kweku Adams71a95312018-04-16 16:54:24 -07006387 item.append(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006388 switch (oldHealth) {
6389 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Kweku Adams71a95312018-04-16 16:54:24 -07006390 item.append(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006391 break;
6392 case BatteryManager.BATTERY_HEALTH_GOOD:
Kweku Adams71a95312018-04-16 16:54:24 -07006393 item.append(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006394 break;
6395 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Kweku Adams71a95312018-04-16 16:54:24 -07006396 item.append(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006397 break;
6398 case BatteryManager.BATTERY_HEALTH_DEAD:
Kweku Adams71a95312018-04-16 16:54:24 -07006399 item.append(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006400 break;
6401 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Kweku Adams71a95312018-04-16 16:54:24 -07006402 item.append(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006403 break;
6404 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Kweku Adams71a95312018-04-16 16:54:24 -07006405 item.append(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006406 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006407 case BatteryManager.BATTERY_HEALTH_COLD:
Kweku Adams71a95312018-04-16 16:54:24 -07006408 item.append(checkin ? "c" : "cold");
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006409 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006410 default:
Kweku Adams71a95312018-04-16 16:54:24 -07006411 item.append(oldHealth);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006412 break;
6413 }
6414 }
6415 if (oldPlug != rec.batteryPlugType) {
6416 oldPlug = rec.batteryPlugType;
Kweku Adams71a95312018-04-16 16:54:24 -07006417 item.append(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006418 switch (oldPlug) {
6419 case 0:
Kweku Adams71a95312018-04-16 16:54:24 -07006420 item.append(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006421 break;
6422 case BatteryManager.BATTERY_PLUGGED_AC:
Kweku Adams71a95312018-04-16 16:54:24 -07006423 item.append(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006424 break;
6425 case BatteryManager.BATTERY_PLUGGED_USB:
Kweku Adams71a95312018-04-16 16:54:24 -07006426 item.append(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006427 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006428 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Kweku Adams71a95312018-04-16 16:54:24 -07006429 item.append(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006430 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006431 default:
Kweku Adams71a95312018-04-16 16:54:24 -07006432 item.append(oldPlug);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006433 break;
6434 }
6435 }
6436 if (oldTemp != rec.batteryTemperature) {
6437 oldTemp = rec.batteryTemperature;
Kweku Adams71a95312018-04-16 16:54:24 -07006438 item.append(checkin ? ",Bt=" : " temp=");
6439 item.append(oldTemp);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006440 }
6441 if (oldVolt != rec.batteryVoltage) {
6442 oldVolt = rec.batteryVoltage;
Kweku Adams71a95312018-04-16 16:54:24 -07006443 item.append(checkin ? ",Bv=" : " volt=");
6444 item.append(oldVolt);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006445 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006446 final int chargeMAh = rec.batteryChargeUAh / 1000;
6447 if (oldChargeMAh != chargeMAh) {
6448 oldChargeMAh = chargeMAh;
Kweku Adams71a95312018-04-16 16:54:24 -07006449 item.append(checkin ? ",Bcc=" : " charge=");
6450 item.append(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07006451 }
Blake Kragtencb308d92019-01-28 10:54:13 -08006452 if (oldModemRailChargeMah != rec.modemRailChargeMah) {
6453 oldModemRailChargeMah = rec.modemRailChargeMah;
6454 item.append(checkin ? ",Mrc=" : " modemRailChargemAh=");
6455 item.append(new DecimalFormat("#.##").format(oldModemRailChargeMah));
6456 }
6457 if (oldWifiRailChargeMah != rec.wifiRailChargeMah) {
6458 oldWifiRailChargeMah = rec.wifiRailChargeMah;
6459 item.append(checkin ? ",Wrc=" : " wifiRailChargemAh=");
6460 item.append(new DecimalFormat("#.##").format(oldWifiRailChargeMah));
6461 }
Kweku Adams71a95312018-04-16 16:54:24 -07006462 printBitDescriptions(item, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006463 HISTORY_STATE_DESCRIPTIONS, !checkin);
Kweku Adams71a95312018-04-16 16:54:24 -07006464 printBitDescriptions(item, oldState2, rec.states2, null,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006465 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006466 if (rec.wakeReasonTag != null) {
6467 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006468 item.append(",wr=");
6469 item.append(rec.wakeReasonTag.poolIdx);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006470 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006471 item.append(" wake_reason=");
6472 item.append(rec.wakeReasonTag.uid);
6473 item.append(":\"");
6474 item.append(rec.wakeReasonTag.string);
6475 item.append("\"");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006476 }
6477 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08006478 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Kweku Adams71a95312018-04-16 16:54:24 -07006479 item.append(checkin ? "," : " ");
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006480 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006481 item.append("+");
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006482 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006483 item.append("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006484 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006485 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
6486 : HISTORY_EVENT_NAMES;
6487 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
6488 | HistoryItem.EVENT_FLAG_FINISH);
6489 if (idx >= 0 && idx < eventNames.length) {
Kweku Adams71a95312018-04-16 16:54:24 -07006490 item.append(eventNames[idx]);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006491 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006492 item.append(checkin ? "Ev" : "event");
6493 item.append(idx);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006494 }
Kweku Adams71a95312018-04-16 16:54:24 -07006495 item.append("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006496 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006497 item.append(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08006498 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006499 item.append(HISTORY_EVENT_INT_FORMATTERS[idx]
Adam Lesinski041d9172016-12-12 12:03:56 -08006500 .applyAsString(rec.eventTag.uid));
Kweku Adams71a95312018-04-16 16:54:24 -07006501 item.append(":\"");
6502 item.append(rec.eventTag.string);
6503 item.append("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006504 }
6505 }
Kweku Adams71a95312018-04-16 16:54:24 -07006506 item.append("\n");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006507 if (rec.stepDetails != null) {
6508 if (!checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006509 item.append(" Details: cpu=");
6510 item.append(rec.stepDetails.userTime);
6511 item.append("u+");
6512 item.append(rec.stepDetails.systemTime);
6513 item.append("s");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006514 if (rec.stepDetails.appCpuUid1 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006515 item.append(" (");
6516 printStepCpuUidDetails(item, rec.stepDetails.appCpuUid1,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006517 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6518 if (rec.stepDetails.appCpuUid2 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006519 item.append(", ");
6520 printStepCpuUidDetails(item, rec.stepDetails.appCpuUid2,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006521 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6522 }
6523 if (rec.stepDetails.appCpuUid3 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006524 item.append(", ");
6525 printStepCpuUidDetails(item, rec.stepDetails.appCpuUid3,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006526 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6527 }
Kweku Adams71a95312018-04-16 16:54:24 -07006528 item.append(')');
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006529 }
Kweku Adams71a95312018-04-16 16:54:24 -07006530 item.append("\n");
6531 item.append(" /proc/stat=");
6532 item.append(rec.stepDetails.statUserTime);
6533 item.append(" usr, ");
6534 item.append(rec.stepDetails.statSystemTime);
6535 item.append(" sys, ");
6536 item.append(rec.stepDetails.statIOWaitTime);
6537 item.append(" io, ");
6538 item.append(rec.stepDetails.statIrqTime);
6539 item.append(" irq, ");
6540 item.append(rec.stepDetails.statSoftIrqTime);
6541 item.append(" sirq, ");
6542 item.append(rec.stepDetails.statIdlTime);
6543 item.append(" idle");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006544 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
6545 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
6546 + rec.stepDetails.statSoftIrqTime;
6547 int total = totalRun + rec.stepDetails.statIdlTime;
6548 if (total > 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006549 item.append(" (");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006550 float perc = ((float)totalRun) / ((float)total) * 100;
Kweku Adams71a95312018-04-16 16:54:24 -07006551 item.append(String.format("%.1f%%", perc));
6552 item.append(" of ");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006553 StringBuilder sb = new StringBuilder(64);
6554 formatTimeMsNoSpace(sb, total*10);
Kweku Adams71a95312018-04-16 16:54:24 -07006555 item.append(sb);
6556 item.append(")");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006557 }
Kweku Adams71a95312018-04-16 16:54:24 -07006558 item.append(", PlatformIdleStat ");
6559 item.append(rec.stepDetails.statPlatformIdleState);
6560 item.append("\n");
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006561
Kweku Adams71a95312018-04-16 16:54:24 -07006562 item.append(", SubsystemPowerState ");
6563 item.append(rec.stepDetails.statSubsystemPowerState);
6564 item.append("\n");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006565 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006566 item.append(BATTERY_STATS_CHECKIN_VERSION); item.append(',');
6567 item.append(HISTORY_DATA); item.append(",0,Dcpu=");
6568 item.append(rec.stepDetails.userTime);
6569 item.append(":");
6570 item.append(rec.stepDetails.systemTime);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006571 if (rec.stepDetails.appCpuUid1 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006572 printStepCpuUidCheckinDetails(item, rec.stepDetails.appCpuUid1,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006573 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6574 if (rec.stepDetails.appCpuUid2 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006575 printStepCpuUidCheckinDetails(item, rec.stepDetails.appCpuUid2,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006576 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6577 }
6578 if (rec.stepDetails.appCpuUid3 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006579 printStepCpuUidCheckinDetails(item, rec.stepDetails.appCpuUid3,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006580 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6581 }
6582 }
Kweku Adams71a95312018-04-16 16:54:24 -07006583 item.append("\n");
6584 item.append(BATTERY_STATS_CHECKIN_VERSION); item.append(',');
6585 item.append(HISTORY_DATA); item.append(",0,Dpst=");
6586 item.append(rec.stepDetails.statUserTime);
6587 item.append(',');
6588 item.append(rec.stepDetails.statSystemTime);
6589 item.append(',');
6590 item.append(rec.stepDetails.statIOWaitTime);
6591 item.append(',');
6592 item.append(rec.stepDetails.statIrqTime);
6593 item.append(',');
6594 item.append(rec.stepDetails.statSoftIrqTime);
6595 item.append(',');
6596 item.append(rec.stepDetails.statIdlTime);
6597 item.append(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006598 if (rec.stepDetails.statPlatformIdleState != null) {
Kweku Adams71a95312018-04-16 16:54:24 -07006599 item.append(rec.stepDetails.statPlatformIdleState);
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006600 if (rec.stepDetails.statSubsystemPowerState != null) {
Kweku Adams71a95312018-04-16 16:54:24 -07006601 item.append(',');
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006602 }
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006603 }
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006604
6605 if (rec.stepDetails.statSubsystemPowerState != null) {
Kweku Adams71a95312018-04-16 16:54:24 -07006606 item.append(rec.stepDetails.statSubsystemPowerState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006607 }
Kweku Adams71a95312018-04-16 16:54:24 -07006608 item.append("\n");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006609 }
6610 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006611 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006612 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006613 }
Kweku Adams71a95312018-04-16 16:54:24 -07006614
6615 return item.toString();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006616 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006617
Kweku Adams71a95312018-04-16 16:54:24 -07006618 private void printStepCpuUidDetails(StringBuilder sb, int uid, int utime, int stime) {
6619 UserHandle.formatUid(sb, uid);
6620 sb.append("=");
6621 sb.append(utime);
6622 sb.append("u+");
6623 sb.append(stime);
6624 sb.append("s");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006625 }
6626
Kweku Adams71a95312018-04-16 16:54:24 -07006627 private void printStepCpuUidCheckinDetails(StringBuilder sb, int uid, int utime,
6628 int stime) {
6629 sb.append('/');
6630 sb.append(uid);
6631 sb.append(":");
6632 sb.append(utime);
6633 sb.append(":");
6634 sb.append(stime);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006635 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006636 }
6637
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006638 private void printSizeValue(PrintWriter pw, long size) {
6639 float result = size;
6640 String suffix = "";
6641 if (result >= 10*1024) {
6642 suffix = "KB";
6643 result = result / 1024;
6644 }
6645 if (result >= 10*1024) {
6646 suffix = "MB";
6647 result = result / 1024;
6648 }
6649 if (result >= 10*1024) {
6650 suffix = "GB";
6651 result = result / 1024;
6652 }
6653 if (result >= 10*1024) {
6654 suffix = "TB";
6655 result = result / 1024;
6656 }
6657 if (result >= 10*1024) {
6658 suffix = "PB";
6659 result = result / 1024;
6660 }
6661 pw.print((int)result);
6662 pw.print(suffix);
6663 }
6664
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006665 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
6666 String label3, long estimatedTime) {
6667 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006668 return false;
6669 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006670 pw.print(label1);
6671 pw.print(label2);
6672 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006673 StringBuilder sb = new StringBuilder(64);
6674 formatTimeMs(sb, estimatedTime);
6675 pw.print(sb);
6676 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006677 return true;
6678 }
6679
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006680 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
6681 LevelStepTracker steps, boolean checkin) {
6682 if (steps == null) {
6683 return false;
6684 }
6685 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006686 if (count <= 0) {
6687 return false;
6688 }
6689 if (!checkin) {
6690 pw.println(header);
6691 }
Kweku Adams030980a2015-04-01 16:07:48 -07006692 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006693 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006694 long duration = steps.getDurationAt(i);
6695 int level = steps.getLevelAt(i);
6696 long initMode = steps.getInitModeAt(i);
6697 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006698 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006699 lineArgs[0] = Long.toString(duration);
6700 lineArgs[1] = Integer.toString(level);
6701 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6702 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6703 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
6704 case Display.STATE_ON: lineArgs[2] = "s+"; break;
6705 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
6706 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07006707 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006708 }
6709 } else {
6710 lineArgs[2] = "";
6711 }
6712 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6713 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
6714 } else {
6715 lineArgs[3] = "";
6716 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006717 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07006718 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006719 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07006720 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006721 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006722 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
6723 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006724 pw.print(prefix);
6725 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006726 TimeUtils.formatDuration(duration, pw);
6727 pw.print(" to "); pw.print(level);
6728 boolean haveModes = false;
6729 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6730 pw.print(" (");
6731 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6732 case Display.STATE_OFF: pw.print("screen-off"); break;
6733 case Display.STATE_ON: pw.print("screen-on"); break;
6734 case Display.STATE_DOZE: pw.print("screen-doze"); break;
6735 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07006736 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006737 }
6738 haveModes = true;
6739 }
6740 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6741 pw.print(haveModes ? ", " : " (");
6742 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
6743 ? "power-save-on" : "power-save-off");
6744 haveModes = true;
6745 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006746 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6747 pw.print(haveModes ? ", " : " (");
6748 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6749 ? "device-idle-on" : "device-idle-off");
6750 haveModes = true;
6751 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006752 if (haveModes) {
6753 pw.print(")");
6754 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006755 pw.println();
6756 }
6757 }
6758 return true;
6759 }
6760
Kweku Adams87b19ec2017-10-09 12:40:03 -07006761 private static void dumpDurationSteps(ProtoOutputStream proto, long fieldId,
6762 LevelStepTracker steps) {
6763 if (steps == null) {
6764 return;
6765 }
6766 int count = steps.mNumStepDurations;
Kweku Adams87b19ec2017-10-09 12:40:03 -07006767 for (int i = 0; i < count; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07006768 long token = proto.start(fieldId);
Kweku Adams87b19ec2017-10-09 12:40:03 -07006769 proto.write(SystemProto.BatteryLevelStep.DURATION_MS, steps.getDurationAt(i));
6770 proto.write(SystemProto.BatteryLevelStep.LEVEL, steps.getLevelAt(i));
6771
6772 final long initMode = steps.getInitModeAt(i);
6773 final long modMode = steps.getModModeAt(i);
6774
6775 int ds = SystemProto.BatteryLevelStep.DS_MIXED;
6776 if ((modMode & STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6777 switch ((int) (initMode & STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6778 case Display.STATE_OFF:
6779 ds = SystemProto.BatteryLevelStep.DS_OFF;
6780 break;
6781 case Display.STATE_ON:
6782 ds = SystemProto.BatteryLevelStep.DS_ON;
6783 break;
6784 case Display.STATE_DOZE:
6785 ds = SystemProto.BatteryLevelStep.DS_DOZE;
6786 break;
6787 case Display.STATE_DOZE_SUSPEND:
6788 ds = SystemProto.BatteryLevelStep.DS_DOZE_SUSPEND;
6789 break;
6790 default:
6791 ds = SystemProto.BatteryLevelStep.DS_ERROR;
6792 break;
6793 }
6794 }
6795 proto.write(SystemProto.BatteryLevelStep.DISPLAY_STATE, ds);
6796
6797 int psm = SystemProto.BatteryLevelStep.PSM_MIXED;
6798 if ((modMode & STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6799 psm = (initMode & STEP_LEVEL_MODE_POWER_SAVE) != 0
6800 ? SystemProto.BatteryLevelStep.PSM_ON : SystemProto.BatteryLevelStep.PSM_OFF;
6801 }
6802 proto.write(SystemProto.BatteryLevelStep.POWER_SAVE_MODE, psm);
6803
6804 int im = SystemProto.BatteryLevelStep.IM_MIXED;
6805 if ((modMode & STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6806 im = (initMode & STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6807 ? SystemProto.BatteryLevelStep.IM_ON : SystemProto.BatteryLevelStep.IM_OFF;
6808 }
6809 proto.write(SystemProto.BatteryLevelStep.IDLE_MODE, im);
6810
6811 proto.end(token);
6812 }
6813 }
6814
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006815 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006816 public static final int DUMP_DAILY_ONLY = 1<<2;
6817 public static final int DUMP_HISTORY_ONLY = 1<<3;
6818 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
6819 public static final int DUMP_VERBOSE = 1<<5;
6820 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006821
Dianne Hackborn37de0982014-05-09 09:32:18 -07006822 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
6823 final HistoryPrinter hprinter = new HistoryPrinter();
6824 final HistoryItem rec = new HistoryItem();
6825 long lastTime = -1;
6826 long baseTime = -1;
6827 boolean printed = false;
6828 HistoryEventTracker tracker = null;
6829 while (getNextHistoryLocked(rec)) {
6830 lastTime = rec.time;
6831 if (baseTime < 0) {
6832 baseTime = lastTime;
6833 }
6834 if (rec.time >= histStart) {
6835 if (histStart >= 0 && !printed) {
6836 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07006837 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006838 || rec.cmd == HistoryItem.CMD_START
6839 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006840 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006841 hprinter.printNextItem(pw, rec, baseTime, checkin,
6842 (flags&DUMP_VERBOSE) != 0);
6843 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006844 } else if (rec.currentTime != 0) {
6845 printed = true;
6846 byte cmd = rec.cmd;
6847 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006848 hprinter.printNextItem(pw, rec, baseTime, checkin,
6849 (flags&DUMP_VERBOSE) != 0);
6850 rec.cmd = cmd;
6851 }
6852 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006853 if (rec.cmd != HistoryItem.CMD_UPDATE) {
6854 hprinter.printNextItem(pw, rec, baseTime, checkin,
6855 (flags&DUMP_VERBOSE) != 0);
6856 rec.cmd = HistoryItem.CMD_UPDATE;
6857 }
6858 int oldEventCode = rec.eventCode;
6859 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006860 rec.eventTag = new HistoryTag();
6861 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
6862 HashMap<String, SparseIntArray> active
6863 = tracker.getStateForEvent(i);
6864 if (active == null) {
6865 continue;
6866 }
6867 for (HashMap.Entry<String, SparseIntArray> ent
6868 : active.entrySet()) {
6869 SparseIntArray uids = ent.getValue();
6870 for (int j=0; j<uids.size(); j++) {
6871 rec.eventCode = i;
6872 rec.eventTag.string = ent.getKey();
6873 rec.eventTag.uid = uids.keyAt(j);
6874 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006875 hprinter.printNextItem(pw, rec, baseTime, checkin,
6876 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006877 rec.wakeReasonTag = null;
6878 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006879 }
6880 }
6881 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006882 rec.eventCode = oldEventCode;
6883 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006884 tracker = null;
6885 }
6886 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006887 hprinter.printNextItem(pw, rec, baseTime, checkin,
6888 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07006889 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
6890 // This is an attempt to aggregate the previous state and generate
6891 // fake events to reflect that state at the point where we start
6892 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07006893 if (tracker == null) {
6894 tracker = new HistoryEventTracker();
6895 }
6896 tracker.updateState(rec.eventCode, rec.eventTag.string,
6897 rec.eventTag.uid, rec.eventTag.poolIdx);
6898 }
6899 }
6900 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07006901 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006902 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
6903 }
6904 }
6905
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006906 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
6907 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
6908 if (steps == null) {
6909 return;
6910 }
6911 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
6912 if (timeRemaining >= 0) {
6913 pw.print(prefix); pw.print(label); pw.print(" total time: ");
6914 tmpSb.setLength(0);
6915 formatTimeMs(tmpSb, timeRemaining);
6916 pw.print(tmpSb);
6917 pw.print(" (from "); pw.print(tmpOutInt[0]);
6918 pw.println(" steps)");
6919 }
6920 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6921 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6922 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
6923 if (estimatedTime > 0) {
6924 pw.print(prefix); pw.print(label); pw.print(" ");
6925 pw.print(STEP_LEVEL_MODE_LABELS[i]);
6926 pw.print(" time: ");
6927 tmpSb.setLength(0);
6928 formatTimeMs(tmpSb, estimatedTime);
6929 pw.print(tmpSb);
6930 pw.print(" (from "); pw.print(tmpOutInt[0]);
6931 pw.println(" steps)");
6932 }
6933 }
6934 }
6935
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006936 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
6937 ArrayList<PackageChange> changes) {
6938 if (changes == null) {
6939 return;
6940 }
6941 pw.print(prefix); pw.println("Package changes:");
6942 for (int i=0; i<changes.size(); i++) {
6943 PackageChange pc = changes.get(i);
6944 if (pc.mUpdate) {
6945 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
6946 pw.print(" vers="); pw.println(pc.mVersionCode);
6947 } else {
6948 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
6949 }
6950 }
6951 }
6952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006953 /**
6954 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
6955 *
6956 * @param pw a Printer to receive the dump output.
6957 */
6958 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006959 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006960 prepareForDumpLocked();
6961
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006962 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006963 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006964
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006965 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006966 final long historyTotalSize = getHistoryTotalSize();
6967 final long historyUsedSize = getHistoryUsedSize();
6968 if (startIteratingHistoryLocked()) {
6969 try {
6970 pw.print("Battery History (");
6971 pw.print((100*historyUsedSize)/historyTotalSize);
6972 pw.print("% used, ");
6973 printSizeValue(pw, historyUsedSize);
6974 pw.print(" used of ");
6975 printSizeValue(pw, historyTotalSize);
6976 pw.print(", ");
6977 pw.print(getHistoryStringPoolSize());
6978 pw.print(" strings using ");
6979 printSizeValue(pw, getHistoryStringPoolBytes());
6980 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07006981 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006982 pw.println();
6983 } finally {
6984 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006985 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006986 }
6987
6988 if (startIteratingOldHistoryLocked()) {
6989 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006990 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006991 pw.println("Old battery History:");
6992 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006993 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006994 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006995 if (baseTime < 0) {
6996 baseTime = rec.time;
6997 }
6998 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006999 }
7000 pw.println();
7001 } finally {
7002 finishIteratingOldHistoryLocked();
7003 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007004 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007005 }
7006
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007007 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007008 return;
7009 }
7010
Dianne Hackbornc51cf032014-03-02 19:08:15 -08007011 if (!filtering) {
7012 SparseArray<? extends Uid> uidStats = getUidStats();
7013 final int NU = uidStats.size();
7014 boolean didPid = false;
7015 long nowRealtime = SystemClock.elapsedRealtime();
7016 for (int i=0; i<NU; i++) {
7017 Uid uid = uidStats.valueAt(i);
7018 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
7019 if (pids != null) {
7020 for (int j=0; j<pids.size(); j++) {
7021 Uid.Pid pid = pids.valueAt(j);
7022 if (!didPid) {
7023 pw.println("Per-PID Stats:");
7024 didPid = true;
7025 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007026 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
7027 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08007028 pw.print(" PID "); pw.print(pids.keyAt(j));
7029 pw.print(" wake time: ");
7030 TimeUtils.formatDuration(time, pw);
7031 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007032 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007033 }
7034 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08007035 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07007036 pw.println();
7037 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007038 }
7039
7040 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007041 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
7042 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07007043 long timeRemaining = computeBatteryTimeRemaining(
7044 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07007045 if (timeRemaining >= 0) {
7046 pw.print(" Estimated discharge time remaining: ");
7047 TimeUtils.formatDuration(timeRemaining / 1000, pw);
7048 pw.println();
7049 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007050 final LevelStepTracker steps = getDischargeLevelStepTracker();
7051 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
7052 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
7053 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
7054 STEP_LEVEL_MODE_VALUES[i], null));
7055 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07007056 pw.println();
7057 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007058 if (dumpDurationSteps(pw, " ", "Charge step durations:",
7059 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07007060 long timeRemaining = computeChargeTimeRemaining(
7061 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07007062 if (timeRemaining >= 0) {
7063 pw.print(" Estimated charge time remaining: ");
7064 TimeUtils.formatDuration(timeRemaining / 1000, pw);
7065 pw.println();
7066 }
7067 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08007068 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007069 }
Dianne Hackbornc81983a2017-10-20 16:16:32 -07007070 if (!filtering || (flags & DUMP_DAILY_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007071 pw.println("Daily stats:");
7072 pw.print(" Current start time: ");
7073 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
7074 getCurrentDailyStartTime()).toString());
7075 pw.print(" Next min deadline: ");
7076 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
7077 getNextMinDailyDeadline()).toString());
7078 pw.print(" Next max deadline: ");
7079 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
7080 getNextMaxDailyDeadline()).toString());
7081 StringBuilder sb = new StringBuilder(64);
7082 int[] outInt = new int[1];
7083 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
7084 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007085 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
7086 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007087 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007088 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
7089 dsteps, false)) {
7090 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
7091 sb, outInt);
7092 }
7093 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
7094 csteps, false)) {
7095 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
7096 sb, outInt);
7097 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007098 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007099 } else {
7100 pw.println(" Current daily steps:");
7101 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
7102 sb, outInt);
7103 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
7104 sb, outInt);
7105 }
7106 }
7107 DailyItem dit;
7108 int curIndex = 0;
7109 while ((dit=getDailyItemLocked(curIndex)) != null) {
7110 curIndex++;
7111 if ((flags&DUMP_DAILY_ONLY) != 0) {
7112 pw.println();
7113 }
7114 pw.print(" Daily from ");
7115 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
7116 pw.print(" to ");
7117 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
7118 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007119 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007120 if (dumpDurationSteps(pw, " ",
7121 " Discharge step durations:", dit.mDischargeSteps, false)) {
7122 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
7123 sb, outInt);
7124 }
7125 if (dumpDurationSteps(pw, " ",
7126 " Charge step durations:", dit.mChargeSteps, false)) {
7127 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
7128 sb, outInt);
7129 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007130 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007131 } else {
7132 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
7133 sb, outInt);
7134 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
7135 sb, outInt);
7136 }
7137 }
7138 pw.println();
7139 }
7140 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07007141 pw.println("Statistics since last charge:");
7142 pw.println(" System starts: " + getStartCount()
7143 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07007144 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
7145 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07007146 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07007147 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007148 }
Mike Mac2f518a2017-09-19 16:06:03 -07007149
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007150 // This is called from BatteryStatsService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007151 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08007152 public void dumpCheckinLocked(Context context, PrintWriter pw,
7153 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007154 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007155
7156 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07007157 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
7158 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007159
Dianne Hackborn13ac0412013-06-25 19:34:49 -07007160 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
7161
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007162 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07007163 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007164 try {
7165 for (int i=0; i<getHistoryStringPoolSize(); i++) {
7166 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
7167 pw.print(HISTORY_STRING_POOL); pw.print(',');
7168 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007169 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007170 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007171 pw.print(",\"");
7172 String str = getHistoryTagPoolString(i);
7173 str = str.replace("\\", "\\\\");
7174 str = str.replace("\"", "\\\"");
7175 pw.print(str);
7176 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007177 pw.println();
7178 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07007179 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007180 } finally {
7181 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08007182 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07007183 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07007184 }
7185
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007186 if ((flags & DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007187 return;
7188 }
7189
Dianne Hackborne4a59512010-12-07 11:08:07 -08007190 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007191 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08007192 for (int i=0; i<apps.size(); i++) {
7193 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007194 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
7195 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08007196 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007197 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
7198 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007199 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007200 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007201 }
7202 SparseArray<? extends Uid> uidStats = getUidStats();
7203 final int NU = uidStats.size();
7204 String[] lineArgs = new String[2];
7205 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007206 int uid = UserHandle.getAppId(uidStats.keyAt(i));
7207 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
7208 if (pkgs != null && !pkgs.second.value) {
7209 pkgs.second.value = true;
7210 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08007211 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007212 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007213 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
7214 (Object[])lineArgs);
7215 }
7216 }
7217 }
7218 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007219 if ((flags & DUMP_DAILY_ONLY) == 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007220 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007221 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07007222 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007223 if (timeRemaining >= 0) {
7224 lineArgs[0] = Long.toString(timeRemaining);
7225 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
7226 (Object[])lineArgs);
7227 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007228 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07007229 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007230 if (timeRemaining >= 0) {
7231 lineArgs[0] = Long.toString(timeRemaining);
7232 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
7233 (Object[])lineArgs);
7234 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007235 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
7236 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08007237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007238 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007239
Kweku Adams71a95312018-04-16 16:54:24 -07007240 /**
7241 * Dump #STATS_SINCE_CHARGED batterystats data to a proto. If the flags include
7242 * DUMP_INCLUDE_HISTORY or DUMP_HISTORY_ONLY, only the history will be dumped.
7243 * @hide
7244 */
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007245 public void dumpProtoLocked(Context context, FileDescriptor fd, List<ApplicationInfo> apps,
Kweku Adams71a95312018-04-16 16:54:24 -07007246 int flags, long histStart) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007247 final ProtoOutputStream proto = new ProtoOutputStream(fd);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007248 prepareForDumpLocked();
7249
Kweku Adams71a95312018-04-16 16:54:24 -07007250 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
7251 dumpProtoHistoryLocked(proto, flags, histStart);
7252 proto.flush();
7253 return;
7254 }
7255
7256 final long bToken = proto.start(BatteryStatsServiceDumpProto.BATTERYSTATS);
7257
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007258 proto.write(BatteryStatsProto.REPORT_VERSION, CHECKIN_VERSION);
7259 proto.write(BatteryStatsProto.PARCEL_VERSION, getParcelVersion());
7260 proto.write(BatteryStatsProto.START_PLATFORM_VERSION, getStartPlatformVersion());
7261 proto.write(BatteryStatsProto.END_PLATFORM_VERSION, getEndPlatformVersion());
7262
Kweku Adams71a95312018-04-16 16:54:24 -07007263 if ((flags & DUMP_DAILY_ONLY) == 0) {
Kweku Adams103351f2017-10-16 14:39:34 -07007264 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false,
7265 (flags & DUMP_DEVICE_WIFI_ONLY) != 0);
7266 helper.create(this);
7267 helper.refreshStats(STATS_SINCE_CHARGED, UserHandle.USER_ALL);
7268
7269 dumpProtoAppsLocked(proto, helper, apps);
7270 dumpProtoSystemLocked(proto, helper);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007271 }
7272
7273 proto.end(bToken);
7274 proto.flush();
7275 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07007276
Kweku Adams103351f2017-10-16 14:39:34 -07007277 private void dumpProtoAppsLocked(ProtoOutputStream proto, BatteryStatsHelper helper,
7278 List<ApplicationInfo> apps) {
7279 final int which = STATS_SINCE_CHARGED;
7280 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7281 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7282 final long rawRealtimeUs = rawRealtimeMs * 1000;
7283 final long batteryUptimeUs = getBatteryUptime(rawUptimeUs);
7284
7285 SparseArray<ArrayList<String>> aidToPackages = new SparseArray<>();
7286 if (apps != null) {
7287 for (int i = 0; i < apps.size(); ++i) {
7288 ApplicationInfo ai = apps.get(i);
7289 int aid = UserHandle.getAppId(ai.uid);
7290 ArrayList<String> pkgs = aidToPackages.get(aid);
7291 if (pkgs == null) {
7292 pkgs = new ArrayList<String>();
7293 aidToPackages.put(aid, pkgs);
7294 }
7295 pkgs.add(ai.packageName);
7296 }
7297 }
7298
7299 SparseArray<BatterySipper> uidToSipper = new SparseArray<>();
7300 final List<BatterySipper> sippers = helper.getUsageList();
7301 if (sippers != null) {
7302 for (int i = 0; i < sippers.size(); ++i) {
7303 final BatterySipper bs = sippers.get(i);
7304 if (bs.drainType != BatterySipper.DrainType.APP) {
7305 // Others are handled by dumpProtoSystemLocked()
7306 continue;
7307 }
7308 uidToSipper.put(bs.uidObj.getUid(), bs);
7309 }
7310 }
7311
7312 SparseArray<? extends Uid> uidStats = getUidStats();
7313 final int n = uidStats.size();
7314 for (int iu = 0; iu < n; ++iu) {
7315 final long uTkn = proto.start(BatteryStatsProto.UIDS);
7316 final Uid u = uidStats.valueAt(iu);
7317
7318 final int uid = uidStats.keyAt(iu);
7319 proto.write(UidProto.UID, uid);
7320
7321 // Print packages and apk stats (UID_DATA & APK_DATA)
7322 ArrayList<String> pkgs = aidToPackages.get(UserHandle.getAppId(uid));
7323 if (pkgs == null) {
7324 pkgs = new ArrayList<String>();
7325 }
7326 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats =
7327 u.getPackageStats();
7328 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7329 String pkg = packageStats.keyAt(ipkg);
7330 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats =
7331 packageStats.valueAt(ipkg).getServiceStats();
7332 if (serviceStats.size() == 0) {
7333 // Due to the way ActivityManagerService logs wakeup alarms, some packages (for
7334 // example, "android") may be included in the packageStats that aren't part of
7335 // the UID. If they don't have any services, then they shouldn't be listed here.
7336 // These packages won't be a part in the pkgs List.
7337 continue;
7338 }
7339
7340 final long pToken = proto.start(UidProto.PACKAGES);
7341 proto.write(UidProto.Package.NAME, pkg);
7342 // Remove from the packages list since we're logging it here.
7343 pkgs.remove(pkg);
7344
7345 for (int isvc = serviceStats.size() - 1; isvc >= 0; --isvc) {
7346 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
Kweku Adams14f3d222018-03-22 14:12:55 -07007347
7348 final long startTimeMs = roundUsToMs(ss.getStartTime(batteryUptimeUs, which));
7349 final int starts = ss.getStarts(which);
7350 final int launches = ss.getLaunches(which);
7351 if (startTimeMs == 0 && starts == 0 && launches == 0) {
7352 continue;
7353 }
7354
Kweku Adams103351f2017-10-16 14:39:34 -07007355 long sToken = proto.start(UidProto.Package.SERVICES);
7356
7357 proto.write(UidProto.Package.Service.NAME, serviceStats.keyAt(isvc));
Kweku Adams14f3d222018-03-22 14:12:55 -07007358 proto.write(UidProto.Package.Service.START_DURATION_MS, startTimeMs);
7359 proto.write(UidProto.Package.Service.START_COUNT, starts);
7360 proto.write(UidProto.Package.Service.LAUNCH_COUNT, launches);
Kweku Adams103351f2017-10-16 14:39:34 -07007361
7362 proto.end(sToken);
7363 }
7364 proto.end(pToken);
7365 }
7366 // Print any remaining packages that weren't in the packageStats map. pkgs is pulled
7367 // from PackageManager data. Packages are only included in packageStats if there was
7368 // specific data tracked for them (services and wakeup alarms, etc.).
7369 for (String p : pkgs) {
7370 final long pToken = proto.start(UidProto.PACKAGES);
7371 proto.write(UidProto.Package.NAME, p);
7372 proto.end(pToken);
7373 }
7374
7375 // Total wakelock data (AGGREGATED_WAKELOCK_DATA)
7376 if (u.getAggregatedPartialWakelockTimer() != null) {
7377 final Timer timer = u.getAggregatedPartialWakelockTimer();
7378 // Times are since reset (regardless of 'which')
7379 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
7380 final Timer bgTimer = timer.getSubTimer();
7381 final long bgTimeMs = bgTimer != null
7382 ? bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
7383 final long awToken = proto.start(UidProto.AGGREGATED_WAKELOCK);
7384 proto.write(UidProto.AggregatedWakelock.PARTIAL_DURATION_MS, totTimeMs);
7385 proto.write(UidProto.AggregatedWakelock.BACKGROUND_PARTIAL_DURATION_MS, bgTimeMs);
7386 proto.end(awToken);
7387 }
7388
7389 // Audio (AUDIO_DATA)
7390 dumpTimer(proto, UidProto.AUDIO, u.getAudioTurnedOnTimer(), rawRealtimeUs, which);
7391
7392 // Bluetooth Controller (BLUETOOTH_CONTROLLER_DATA)
7393 dumpControllerActivityProto(proto, UidProto.BLUETOOTH_CONTROLLER,
7394 u.getBluetoothControllerActivity(), which);
7395
7396 // BLE scans (BLUETOOTH_MISC_DATA) (uses totalDurationMsLocked and MaxDurationMsLocked)
7397 final Timer bleTimer = u.getBluetoothScanTimer();
7398 if (bleTimer != null) {
7399 final long bmToken = proto.start(UidProto.BLUETOOTH_MISC);
7400
7401 dumpTimer(proto, UidProto.BluetoothMisc.APPORTIONED_BLE_SCAN, bleTimer,
7402 rawRealtimeUs, which);
7403 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN,
7404 u.getBluetoothScanBackgroundTimer(), rawRealtimeUs, which);
7405 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
7406 dumpTimer(proto, UidProto.BluetoothMisc.UNOPTIMIZED_BLE_SCAN,
7407 u.getBluetoothUnoptimizedScanTimer(), rawRealtimeUs, which);
7408 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
7409 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_UNOPTIMIZED_BLE_SCAN,
7410 u.getBluetoothUnoptimizedScanBackgroundTimer(), rawRealtimeUs, which);
7411 // Result counters
7412 proto.write(UidProto.BluetoothMisc.BLE_SCAN_RESULT_COUNT,
7413 u.getBluetoothScanResultCounter() != null
7414 ? u.getBluetoothScanResultCounter().getCountLocked(which) : 0);
7415 proto.write(UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN_RESULT_COUNT,
7416 u.getBluetoothScanResultBgCounter() != null
7417 ? u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0);
7418
7419 proto.end(bmToken);
7420 }
7421
7422 // Camera (CAMERA_DATA)
7423 dumpTimer(proto, UidProto.CAMERA, u.getCameraTurnedOnTimer(), rawRealtimeUs, which);
7424
7425 // CPU stats (CPU_DATA & CPU_TIMES_AT_FREQ_DATA)
7426 final long cpuToken = proto.start(UidProto.CPU);
7427 proto.write(UidProto.Cpu.USER_DURATION_MS, roundUsToMs(u.getUserCpuTimeUs(which)));
7428 proto.write(UidProto.Cpu.SYSTEM_DURATION_MS, roundUsToMs(u.getSystemCpuTimeUs(which)));
7429
7430 final long[] cpuFreqs = getCpuFreqs();
7431 if (cpuFreqs != null) {
7432 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
7433 // If total cpuFreqTimes is null, then we don't need to check for
7434 // screenOffCpuFreqTimes.
7435 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
7436 long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
7437 if (screenOffCpuFreqTimeMs == null) {
7438 screenOffCpuFreqTimeMs = new long[cpuFreqTimeMs.length];
7439 }
7440 for (int ic = 0; ic < cpuFreqTimeMs.length; ++ic) {
7441 long cToken = proto.start(UidProto.Cpu.BY_FREQUENCY);
7442 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7443 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7444 cpuFreqTimeMs[ic]);
7445 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7446 screenOffCpuFreqTimeMs[ic]);
7447 proto.end(cToken);
7448 }
7449 }
7450 }
Sudheer Shanka6d658d72018-01-01 01:36:49 -08007451
7452 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
7453 final long[] timesMs = u.getCpuFreqTimes(which, procState);
7454 if (timesMs != null && timesMs.length == cpuFreqs.length) {
7455 long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(which, procState);
7456 if (screenOffTimesMs == null) {
7457 screenOffTimesMs = new long[timesMs.length];
7458 }
7459 final long procToken = proto.start(UidProto.Cpu.BY_PROCESS_STATE);
7460 proto.write(UidProto.Cpu.ByProcessState.PROCESS_STATE, procState);
7461 for (int ic = 0; ic < timesMs.length; ++ic) {
7462 long cToken = proto.start(UidProto.Cpu.ByProcessState.BY_FREQUENCY);
7463 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7464 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7465 timesMs[ic]);
7466 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7467 screenOffTimesMs[ic]);
7468 proto.end(cToken);
7469 }
7470 proto.end(procToken);
7471 }
7472 }
Kweku Adams103351f2017-10-16 14:39:34 -07007473 proto.end(cpuToken);
7474
7475 // Flashlight (FLASHLIGHT_DATA)
7476 dumpTimer(proto, UidProto.FLASHLIGHT, u.getFlashlightTurnedOnTimer(),
7477 rawRealtimeUs, which);
7478
7479 // Foreground activity (FOREGROUND_ACTIVITY_DATA)
7480 dumpTimer(proto, UidProto.FOREGROUND_ACTIVITY, u.getForegroundActivityTimer(),
7481 rawRealtimeUs, which);
7482
7483 // Foreground service (FOREGROUND_SERVICE_DATA)
7484 dumpTimer(proto, UidProto.FOREGROUND_SERVICE, u.getForegroundServiceTimer(),
7485 rawRealtimeUs, which);
7486
7487 // Job completion (JOB_COMPLETION_DATA)
7488 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
7489 final int[] reasons = new int[]{
7490 JobParameters.REASON_CANCELED,
7491 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED,
7492 JobParameters.REASON_PREEMPT,
7493 JobParameters.REASON_TIMEOUT,
7494 JobParameters.REASON_DEVICE_IDLE,
7495 };
7496 for (int ic = 0; ic < completions.size(); ++ic) {
7497 SparseIntArray types = completions.valueAt(ic);
7498 if (types != null) {
7499 final long jcToken = proto.start(UidProto.JOB_COMPLETION);
7500
7501 proto.write(UidProto.JobCompletion.NAME, completions.keyAt(ic));
7502
7503 for (int r : reasons) {
7504 long rToken = proto.start(UidProto.JobCompletion.REASON_COUNT);
7505 proto.write(UidProto.JobCompletion.ReasonCount.NAME, r);
7506 proto.write(UidProto.JobCompletion.ReasonCount.COUNT, types.get(r, 0));
7507 proto.end(rToken);
7508 }
7509
7510 proto.end(jcToken);
7511 }
7512 }
7513
7514 // Scheduled jobs (JOB_DATA)
7515 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
7516 for (int ij = jobs.size() - 1; ij >= 0; --ij) {
7517 final Timer timer = jobs.valueAt(ij);
7518 final Timer bgTimer = timer.getSubTimer();
7519 final long jToken = proto.start(UidProto.JOBS);
7520
7521 proto.write(UidProto.Job.NAME, jobs.keyAt(ij));
7522 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7523 dumpTimer(proto, UidProto.Job.TOTAL, timer, rawRealtimeUs, which);
7524 dumpTimer(proto, UidProto.Job.BACKGROUND, bgTimer, rawRealtimeUs, which);
7525
7526 proto.end(jToken);
7527 }
7528
7529 // Modem Controller (MODEM_CONTROLLER_DATA)
7530 dumpControllerActivityProto(proto, UidProto.MODEM_CONTROLLER,
7531 u.getModemControllerActivity(), which);
7532
7533 // Network stats (NETWORK_DATA)
7534 final long nToken = proto.start(UidProto.NETWORK);
7535 proto.write(UidProto.Network.MOBILE_BYTES_RX,
7536 u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7537 proto.write(UidProto.Network.MOBILE_BYTES_TX,
7538 u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7539 proto.write(UidProto.Network.WIFI_BYTES_RX,
7540 u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7541 proto.write(UidProto.Network.WIFI_BYTES_TX,
7542 u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7543 proto.write(UidProto.Network.BT_BYTES_RX,
7544 u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7545 proto.write(UidProto.Network.BT_BYTES_TX,
7546 u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
7547 proto.write(UidProto.Network.MOBILE_PACKETS_RX,
7548 u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7549 proto.write(UidProto.Network.MOBILE_PACKETS_TX,
7550 u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7551 proto.write(UidProto.Network.WIFI_PACKETS_RX,
7552 u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7553 proto.write(UidProto.Network.WIFI_PACKETS_TX,
7554 u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7555 proto.write(UidProto.Network.MOBILE_ACTIVE_DURATION_MS,
7556 roundUsToMs(u.getMobileRadioActiveTime(which)));
7557 proto.write(UidProto.Network.MOBILE_ACTIVE_COUNT,
7558 u.getMobileRadioActiveCount(which));
7559 proto.write(UidProto.Network.MOBILE_WAKEUP_COUNT,
7560 u.getMobileRadioApWakeupCount(which));
7561 proto.write(UidProto.Network.WIFI_WAKEUP_COUNT,
7562 u.getWifiRadioApWakeupCount(which));
7563 proto.write(UidProto.Network.MOBILE_BYTES_BG_RX,
7564 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA, which));
7565 proto.write(UidProto.Network.MOBILE_BYTES_BG_TX,
7566 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA, which));
7567 proto.write(UidProto.Network.WIFI_BYTES_BG_RX,
7568 u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which));
7569 proto.write(UidProto.Network.WIFI_BYTES_BG_TX,
7570 u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which));
7571 proto.write(UidProto.Network.MOBILE_PACKETS_BG_RX,
7572 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA, which));
7573 proto.write(UidProto.Network.MOBILE_PACKETS_BG_TX,
7574 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA, which));
7575 proto.write(UidProto.Network.WIFI_PACKETS_BG_RX,
7576 u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA, which));
7577 proto.write(UidProto.Network.WIFI_PACKETS_BG_TX,
7578 u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA, which));
7579 proto.end(nToken);
7580
7581 // Power use item (POWER_USE_ITEM_DATA)
7582 BatterySipper bs = uidToSipper.get(uid);
7583 if (bs != null) {
7584 final long bsToken = proto.start(UidProto.POWER_USE_ITEM);
7585 proto.write(UidProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7586 proto.write(UidProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7587 proto.write(UidProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7588 proto.write(UidProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7589 bs.proportionalSmearMah);
7590 proto.end(bsToken);
7591 }
7592
7593 // Processes (PROCESS_DATA)
7594 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats =
7595 u.getProcessStats();
7596 for (int ipr = processStats.size() - 1; ipr >= 0; --ipr) {
7597 final Uid.Proc ps = processStats.valueAt(ipr);
7598 final long prToken = proto.start(UidProto.PROCESS);
7599
7600 proto.write(UidProto.Process.NAME, processStats.keyAt(ipr));
7601 proto.write(UidProto.Process.USER_DURATION_MS, ps.getUserTime(which));
7602 proto.write(UidProto.Process.SYSTEM_DURATION_MS, ps.getSystemTime(which));
7603 proto.write(UidProto.Process.FOREGROUND_DURATION_MS, ps.getForegroundTime(which));
7604 proto.write(UidProto.Process.START_COUNT, ps.getStarts(which));
7605 proto.write(UidProto.Process.ANR_COUNT, ps.getNumAnrs(which));
7606 proto.write(UidProto.Process.CRASH_COUNT, ps.getNumCrashes(which));
7607
7608 proto.end(prToken);
7609 }
7610
7611 // Sensors (SENSOR_DATA)
7612 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
7613 for (int ise = 0; ise < sensors.size(); ++ise) {
7614 final Uid.Sensor se = sensors.valueAt(ise);
7615 final Timer timer = se.getSensorTime();
7616 if (timer == null) {
7617 continue;
7618 }
7619 final Timer bgTimer = se.getSensorBackgroundTime();
7620 final int sensorNumber = sensors.keyAt(ise);
7621 final long seToken = proto.start(UidProto.SENSORS);
7622
7623 proto.write(UidProto.Sensor.ID, sensorNumber);
7624 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7625 dumpTimer(proto, UidProto.Sensor.APPORTIONED, timer, rawRealtimeUs, which);
7626 dumpTimer(proto, UidProto.Sensor.BACKGROUND, bgTimer, rawRealtimeUs, which);
7627
7628 proto.end(seToken);
7629 }
7630
7631 // State times (STATE_TIME_DATA)
7632 for (int ips = 0; ips < Uid.NUM_PROCESS_STATE; ++ips) {
7633 long durMs = roundUsToMs(u.getProcessStateTime(ips, rawRealtimeUs, which));
7634 if (durMs == 0) {
7635 continue;
7636 }
7637 final long stToken = proto.start(UidProto.STATES);
7638 proto.write(UidProto.StateTime.STATE, ips);
7639 proto.write(UidProto.StateTime.DURATION_MS, durMs);
7640 proto.end(stToken);
7641 }
7642
7643 // Syncs (SYNC_DATA)
7644 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
7645 for (int isy = syncs.size() - 1; isy >= 0; --isy) {
7646 final Timer timer = syncs.valueAt(isy);
7647 final Timer bgTimer = timer.getSubTimer();
7648 final long syToken = proto.start(UidProto.SYNCS);
7649
7650 proto.write(UidProto.Sync.NAME, syncs.keyAt(isy));
7651 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7652 dumpTimer(proto, UidProto.Sync.TOTAL, timer, rawRealtimeUs, which);
7653 dumpTimer(proto, UidProto.Sync.BACKGROUND, bgTimer, rawRealtimeUs, which);
7654
7655 proto.end(syToken);
7656 }
7657
7658 // User activity (USER_ACTIVITY_DATA)
7659 if (u.hasUserActivity()) {
7660 for (int i = 0; i < Uid.NUM_USER_ACTIVITY_TYPES; ++i) {
7661 int val = u.getUserActivityCount(i, which);
7662 if (val != 0) {
7663 final long uaToken = proto.start(UidProto.USER_ACTIVITY);
7664 proto.write(UidProto.UserActivity.NAME, i);
7665 proto.write(UidProto.UserActivity.COUNT, val);
7666 proto.end(uaToken);
7667 }
7668 }
7669 }
7670
7671 // Vibrator (VIBRATOR_DATA)
7672 dumpTimer(proto, UidProto.VIBRATOR, u.getVibratorOnTimer(), rawRealtimeUs, which);
7673
7674 // Video (VIDEO_DATA)
7675 dumpTimer(proto, UidProto.VIDEO, u.getVideoTurnedOnTimer(), rawRealtimeUs, which);
7676
7677 // Wakelocks (WAKELOCK_DATA)
7678 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
7679 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7680 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7681 final long wToken = proto.start(UidProto.WAKELOCKS);
7682 proto.write(UidProto.Wakelock.NAME, wakelocks.keyAt(iw));
7683 dumpTimer(proto, UidProto.Wakelock.FULL, wl.getWakeTime(WAKE_TYPE_FULL),
7684 rawRealtimeUs, which);
7685 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7686 if (pTimer != null) {
7687 dumpTimer(proto, UidProto.Wakelock.PARTIAL, pTimer, rawRealtimeUs, which);
7688 dumpTimer(proto, UidProto.Wakelock.BACKGROUND_PARTIAL, pTimer.getSubTimer(),
7689 rawRealtimeUs, which);
7690 }
7691 dumpTimer(proto, UidProto.Wakelock.WINDOW, wl.getWakeTime(WAKE_TYPE_WINDOW),
7692 rawRealtimeUs, which);
7693 proto.end(wToken);
7694 }
7695
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007696 // Wifi Multicast Wakelock (WIFI_MULTICAST_WAKELOCK_DATA)
7697 dumpTimer(proto, UidProto.WIFI_MULTICAST_WAKELOCK, u.getMulticastWakelockStats(),
7698 rawRealtimeUs, which);
7699
Kweku Adams103351f2017-10-16 14:39:34 -07007700 // Wakeup alarms (WAKEUP_ALARM_DATA)
7701 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7702 final Uid.Pkg ps = packageStats.valueAt(ipkg);
7703 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
7704 for (int iwa = alarms.size() - 1; iwa >= 0; --iwa) {
7705 final long waToken = proto.start(UidProto.WAKEUP_ALARM);
7706 proto.write(UidProto.WakeupAlarm.NAME, alarms.keyAt(iwa));
7707 proto.write(UidProto.WakeupAlarm.COUNT,
7708 alarms.valueAt(iwa).getCountLocked(which));
7709 proto.end(waToken);
7710 }
7711 }
7712
7713 // Wifi Controller (WIFI_CONTROLLER_DATA)
7714 dumpControllerActivityProto(proto, UidProto.WIFI_CONTROLLER,
7715 u.getWifiControllerActivity(), which);
7716
7717 // Wifi data (WIFI_DATA)
7718 final long wToken = proto.start(UidProto.WIFI);
7719 proto.write(UidProto.Wifi.FULL_WIFI_LOCK_DURATION_MS,
7720 roundUsToMs(u.getFullWifiLockTime(rawRealtimeUs, which)));
7721 dumpTimer(proto, UidProto.Wifi.APPORTIONED_SCAN, u.getWifiScanTimer(),
7722 rawRealtimeUs, which);
7723 proto.write(UidProto.Wifi.RUNNING_DURATION_MS,
7724 roundUsToMs(u.getWifiRunningTime(rawRealtimeUs, which)));
7725 dumpTimer(proto, UidProto.Wifi.BACKGROUND_SCAN, u.getWifiScanBackgroundTimer(),
7726 rawRealtimeUs, which);
7727 proto.end(wToken);
7728
7729 proto.end(uTkn);
7730 }
7731 }
7732
Kweku Adams71a95312018-04-16 16:54:24 -07007733 private void dumpProtoHistoryLocked(ProtoOutputStream proto, int flags, long histStart) {
7734 if (!startIteratingHistoryLocked()) {
7735 return;
7736 }
7737
7738 proto.write(BatteryStatsServiceDumpHistoryProto.REPORT_VERSION, CHECKIN_VERSION);
7739 proto.write(BatteryStatsServiceDumpHistoryProto.PARCEL_VERSION, getParcelVersion());
7740 proto.write(BatteryStatsServiceDumpHistoryProto.START_PLATFORM_VERSION,
7741 getStartPlatformVersion());
7742 proto.write(BatteryStatsServiceDumpHistoryProto.END_PLATFORM_VERSION,
7743 getEndPlatformVersion());
7744 try {
7745 long token;
7746 // History string pool (HISTORY_STRING_POOL)
7747 for (int i = 0; i < getHistoryStringPoolSize(); ++i) {
7748 token = proto.start(BatteryStatsServiceDumpHistoryProto.KEYS);
7749 proto.write(BatteryStatsServiceDumpHistoryProto.Key.INDEX, i);
7750 proto.write(BatteryStatsServiceDumpHistoryProto.Key.UID, getHistoryTagPoolUid(i));
7751 proto.write(BatteryStatsServiceDumpHistoryProto.Key.TAG,
7752 getHistoryTagPoolString(i));
7753 proto.end(token);
7754 }
7755
7756 // History data (HISTORY_DATA)
7757 final HistoryPrinter hprinter = new HistoryPrinter();
7758 final HistoryItem rec = new HistoryItem();
7759 long lastTime = -1;
7760 long baseTime = -1;
7761 boolean printed = false;
7762 HistoryEventTracker tracker = null;
7763 while (getNextHistoryLocked(rec)) {
7764 lastTime = rec.time;
7765 if (baseTime < 0) {
7766 baseTime = lastTime;
7767 }
7768 if (rec.time >= histStart) {
7769 if (histStart >= 0 && !printed) {
7770 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
7771 || rec.cmd == HistoryItem.CMD_RESET
7772 || rec.cmd == HistoryItem.CMD_START
7773 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
7774 printed = true;
7775 hprinter.printNextItem(proto, rec, baseTime,
7776 (flags & DUMP_VERBOSE) != 0);
7777 rec.cmd = HistoryItem.CMD_UPDATE;
7778 } else if (rec.currentTime != 0) {
7779 printed = true;
7780 byte cmd = rec.cmd;
7781 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
7782 hprinter.printNextItem(proto, rec, baseTime,
7783 (flags & DUMP_VERBOSE) != 0);
7784 rec.cmd = cmd;
7785 }
7786 if (tracker != null) {
7787 if (rec.cmd != HistoryItem.CMD_UPDATE) {
7788 hprinter.printNextItem(proto, rec, baseTime,
7789 (flags & DUMP_VERBOSE) != 0);
7790 rec.cmd = HistoryItem.CMD_UPDATE;
7791 }
7792 int oldEventCode = rec.eventCode;
7793 HistoryTag oldEventTag = rec.eventTag;
7794 rec.eventTag = new HistoryTag();
7795 for (int i = 0; i < HistoryItem.EVENT_COUNT; i++) {
7796 HashMap<String, SparseIntArray> active =
7797 tracker.getStateForEvent(i);
7798 if (active == null) {
7799 continue;
7800 }
7801 for (HashMap.Entry<String, SparseIntArray> ent
7802 : active.entrySet()) {
7803 SparseIntArray uids = ent.getValue();
7804 for (int j = 0; j < uids.size(); j++) {
7805 rec.eventCode = i;
7806 rec.eventTag.string = ent.getKey();
7807 rec.eventTag.uid = uids.keyAt(j);
7808 rec.eventTag.poolIdx = uids.valueAt(j);
7809 hprinter.printNextItem(proto, rec, baseTime,
7810 (flags & DUMP_VERBOSE) != 0);
7811 rec.wakeReasonTag = null;
7812 rec.wakelockTag = null;
7813 }
7814 }
7815 }
7816 rec.eventCode = oldEventCode;
7817 rec.eventTag = oldEventTag;
7818 tracker = null;
7819 }
7820 }
7821 hprinter.printNextItem(proto, rec, baseTime,
7822 (flags & DUMP_VERBOSE) != 0);
7823 }
7824 }
7825 if (histStart >= 0) {
7826 commitCurrentHistoryBatchLocked();
7827 proto.write(BatteryStatsServiceDumpHistoryProto.CSV_LINES,
7828 "NEXT: " + (lastTime + 1));
7829 }
7830 } finally {
7831 finishIteratingHistoryLocked();
7832 }
7833 }
7834
Kweku Adams103351f2017-10-16 14:39:34 -07007835 private void dumpProtoSystemLocked(ProtoOutputStream proto, BatteryStatsHelper helper) {
Kweku Adams87b19ec2017-10-09 12:40:03 -07007836 final long sToken = proto.start(BatteryStatsProto.SYSTEM);
7837 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7838 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7839 final long rawRealtimeUs = rawRealtimeMs * 1000;
7840 final int which = STATS_SINCE_CHARGED;
7841
7842 // Battery data (BATTERY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007843 final long bToken = proto.start(SystemProto.BATTERY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007844 proto.write(SystemProto.Battery.START_CLOCK_TIME_MS, getStartClockTime());
7845 proto.write(SystemProto.Battery.START_COUNT, getStartCount());
7846 proto.write(SystemProto.Battery.TOTAL_REALTIME_MS,
7847 computeRealtime(rawRealtimeUs, which) / 1000);
7848 proto.write(SystemProto.Battery.TOTAL_UPTIME_MS,
7849 computeUptime(rawUptimeUs, which) / 1000);
7850 proto.write(SystemProto.Battery.BATTERY_REALTIME_MS,
7851 computeBatteryRealtime(rawRealtimeUs, which) / 1000);
7852 proto.write(SystemProto.Battery.BATTERY_UPTIME_MS,
7853 computeBatteryUptime(rawUptimeUs, which) / 1000);
7854 proto.write(SystemProto.Battery.SCREEN_OFF_REALTIME_MS,
7855 computeBatteryScreenOffRealtime(rawRealtimeUs, which) / 1000);
7856 proto.write(SystemProto.Battery.SCREEN_OFF_UPTIME_MS,
7857 computeBatteryScreenOffUptime(rawUptimeUs, which) / 1000);
7858 proto.write(SystemProto.Battery.SCREEN_DOZE_DURATION_MS,
7859 getScreenDozeTime(rawRealtimeUs, which) / 1000);
7860 proto.write(SystemProto.Battery.ESTIMATED_BATTERY_CAPACITY_MAH,
7861 getEstimatedBatteryCapacity());
7862 proto.write(SystemProto.Battery.MIN_LEARNED_BATTERY_CAPACITY_UAH,
7863 getMinLearnedBatteryCapacity());
7864 proto.write(SystemProto.Battery.MAX_LEARNED_BATTERY_CAPACITY_UAH,
7865 getMaxLearnedBatteryCapacity());
Kweku Adams103351f2017-10-16 14:39:34 -07007866 proto.end(bToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007867
7868 // Battery discharge (BATTERY_DISCHARGE_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007869 final long bdToken = proto.start(SystemProto.BATTERY_DISCHARGE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007870 proto.write(SystemProto.BatteryDischarge.LOWER_BOUND_SINCE_CHARGE,
7871 getLowDischargeAmountSinceCharge());
7872 proto.write(SystemProto.BatteryDischarge.UPPER_BOUND_SINCE_CHARGE,
7873 getHighDischargeAmountSinceCharge());
7874 proto.write(SystemProto.BatteryDischarge.SCREEN_ON_SINCE_CHARGE,
7875 getDischargeAmountScreenOnSinceCharge());
7876 proto.write(SystemProto.BatteryDischarge.SCREEN_OFF_SINCE_CHARGE,
7877 getDischargeAmountScreenOffSinceCharge());
7878 proto.write(SystemProto.BatteryDischarge.SCREEN_DOZE_SINCE_CHARGE,
7879 getDischargeAmountScreenDozeSinceCharge());
7880 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH,
7881 getUahDischarge(which) / 1000);
7882 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_OFF,
7883 getUahDischargeScreenOff(which) / 1000);
7884 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_DOZE,
7885 getUahDischargeScreenDoze(which) / 1000);
Mike Ma15313c92017-11-15 17:58:21 -08007886 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_LIGHT_DOZE,
7887 getUahDischargeLightDoze(which) / 1000);
7888 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_DEEP_DOZE,
7889 getUahDischargeDeepDoze(which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007890 proto.end(bdToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007891
7892 // Time remaining
7893 long timeRemainingUs = computeChargeTimeRemaining(rawRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -07007894 // These are part of a oneof, so we should only set one of them.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007895 if (timeRemainingUs >= 0) {
7896 // Charge time remaining (CHARGE_TIME_REMAIN_DATA)
7897 proto.write(SystemProto.CHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7898 } else {
7899 timeRemainingUs = computeBatteryTimeRemaining(rawRealtimeUs);
7900 // Discharge time remaining (DISCHARGE_TIME_REMAIN_DATA)
7901 if (timeRemainingUs >= 0) {
7902 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7903 } else {
7904 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, -1);
7905 }
7906 }
7907
7908 // Charge step (CHARGE_STEP_DATA)
7909 dumpDurationSteps(proto, SystemProto.CHARGE_STEP, getChargeLevelStepTracker());
7910
7911 // Phone data connection (DATA_CONNECTION_TIME_DATA and DATA_CONNECTION_COUNT_DATA)
7912 for (int i = 0; i < NUM_DATA_CONNECTION_TYPES; ++i) {
Tej Singheee317b2018-03-07 19:28:05 -08007913 // Map OTHER to TelephonyManager.NETWORK_TYPE_UNKNOWN and mark NONE as a boolean.
7914 boolean isNone = (i == DATA_CONNECTION_NONE);
7915 int telephonyNetworkType = i;
7916 if (i == DATA_CONNECTION_OTHER) {
7917 telephonyNetworkType = TelephonyManager.NETWORK_TYPE_UNKNOWN;
7918 }
Kweku Adams103351f2017-10-16 14:39:34 -07007919 final long pdcToken = proto.start(SystemProto.DATA_CONNECTION);
Tej Singheee317b2018-03-07 19:28:05 -08007920 if (isNone) {
7921 proto.write(SystemProto.DataConnection.IS_NONE, isNone);
7922 } else {
7923 proto.write(SystemProto.DataConnection.NAME, telephonyNetworkType);
7924 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07007925 dumpTimer(proto, SystemProto.DataConnection.TOTAL, getPhoneDataConnectionTimer(i),
7926 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007927 proto.end(pdcToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007928 }
7929
7930 // Discharge step (DISCHARGE_STEP_DATA)
7931 dumpDurationSteps(proto, SystemProto.DISCHARGE_STEP, getDischargeLevelStepTracker());
7932
7933 // CPU frequencies (GLOBAL_CPU_FREQ_DATA)
7934 final long[] cpuFreqs = getCpuFreqs();
7935 if (cpuFreqs != null) {
7936 for (long i : cpuFreqs) {
7937 proto.write(SystemProto.CPU_FREQUENCY, i);
7938 }
7939 }
7940
7941 // Bluetooth controller (GLOBAL_BLUETOOTH_CONTROLLER_DATA)
7942 dumpControllerActivityProto(proto, SystemProto.GLOBAL_BLUETOOTH_CONTROLLER,
7943 getBluetoothControllerActivity(), which);
7944
7945 // Modem controller (GLOBAL_MODEM_CONTROLLER_DATA)
7946 dumpControllerActivityProto(proto, SystemProto.GLOBAL_MODEM_CONTROLLER,
7947 getModemControllerActivity(), which);
7948
7949 // Global network data (GLOBAL_NETWORK_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007950 final long gnToken = proto.start(SystemProto.GLOBAL_NETWORK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007951 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_RX,
7952 getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7953 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_TX,
7954 getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7955 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_RX,
7956 getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7957 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_TX,
7958 getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7959 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_RX,
7960 getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7961 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_TX,
7962 getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7963 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_RX,
7964 getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7965 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_TX,
7966 getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7967 proto.write(SystemProto.GlobalNetwork.BT_BYTES_RX,
7968 getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7969 proto.write(SystemProto.GlobalNetwork.BT_BYTES_TX,
7970 getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
Kweku Adams103351f2017-10-16 14:39:34 -07007971 proto.end(gnToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007972
7973 // Wifi controller (GLOBAL_WIFI_CONTROLLER_DATA)
7974 dumpControllerActivityProto(proto, SystemProto.GLOBAL_WIFI_CONTROLLER,
7975 getWifiControllerActivity(), which);
7976
7977
7978 // Global wifi (GLOBAL_WIFI_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007979 final long gwToken = proto.start(SystemProto.GLOBAL_WIFI);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007980 proto.write(SystemProto.GlobalWifi.ON_DURATION_MS,
7981 getWifiOnTime(rawRealtimeUs, which) / 1000);
7982 proto.write(SystemProto.GlobalWifi.RUNNING_DURATION_MS,
7983 getGlobalWifiRunningTime(rawRealtimeUs, which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007984 proto.end(gwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007985
7986 // Kernel wakelock (KERNEL_WAKELOCK_DATA)
7987 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
7988 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007989 final long kwToken = proto.start(SystemProto.KERNEL_WAKELOCK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007990 proto.write(SystemProto.KernelWakelock.NAME, ent.getKey());
7991 dumpTimer(proto, SystemProto.KernelWakelock.TOTAL, ent.getValue(),
7992 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007993 proto.end(kwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007994 }
7995
7996 // Misc (MISC_DATA)
7997 // Calculate wakelock times across all uids.
7998 long fullWakeLockTimeTotalUs = 0;
7999 long partialWakeLockTimeTotalUs = 0;
8000
8001 final SparseArray<? extends Uid> uidStats = getUidStats();
8002 for (int iu = 0; iu < uidStats.size(); iu++) {
8003 final Uid u = uidStats.valueAt(iu);
8004
8005 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks =
8006 u.getWakelockStats();
8007 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
8008 final Uid.Wakelock wl = wakelocks.valueAt(iw);
8009
8010 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
8011 if (fullWakeTimer != null) {
8012 fullWakeLockTimeTotalUs += fullWakeTimer.getTotalTimeLocked(rawRealtimeUs,
8013 which);
8014 }
8015
8016 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
8017 if (partialWakeTimer != null) {
8018 partialWakeLockTimeTotalUs += partialWakeTimer.getTotalTimeLocked(
8019 rawRealtimeUs, which);
8020 }
8021 }
8022 }
Kweku Adams103351f2017-10-16 14:39:34 -07008023 final long mToken = proto.start(SystemProto.MISC);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008024 proto.write(SystemProto.Misc.SCREEN_ON_DURATION_MS,
8025 getScreenOnTime(rawRealtimeUs, which) / 1000);
8026 proto.write(SystemProto.Misc.PHONE_ON_DURATION_MS,
8027 getPhoneOnTime(rawRealtimeUs, which) / 1000);
8028 proto.write(SystemProto.Misc.FULL_WAKELOCK_TOTAL_DURATION_MS,
8029 fullWakeLockTimeTotalUs / 1000);
8030 proto.write(SystemProto.Misc.PARTIAL_WAKELOCK_TOTAL_DURATION_MS,
8031 partialWakeLockTimeTotalUs / 1000);
8032 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_DURATION_MS,
8033 getMobileRadioActiveTime(rawRealtimeUs, which) / 1000);
8034 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_ADJUSTED_TIME_MS,
8035 getMobileRadioActiveAdjustedTime(which) / 1000);
8036 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_COUNT,
8037 getMobileRadioActiveCount(which));
8038 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_UNKNOWN_DURATION_MS,
8039 getMobileRadioActiveUnknownTime(which) / 1000);
8040 proto.write(SystemProto.Misc.INTERACTIVE_DURATION_MS,
8041 getInteractiveTime(rawRealtimeUs, which) / 1000);
8042 proto.write(SystemProto.Misc.BATTERY_SAVER_MODE_ENABLED_DURATION_MS,
8043 getPowerSaveModeEnabledTime(rawRealtimeUs, which) / 1000);
8044 proto.write(SystemProto.Misc.NUM_CONNECTIVITY_CHANGES,
8045 getNumConnectivityChange(which));
8046 proto.write(SystemProto.Misc.DEEP_DOZE_ENABLED_DURATION_MS,
8047 getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
8048 proto.write(SystemProto.Misc.DEEP_DOZE_COUNT,
8049 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
8050 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_DURATION_MS,
8051 getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
8052 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_COUNT,
8053 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
8054 proto.write(SystemProto.Misc.LONGEST_DEEP_DOZE_DURATION_MS,
8055 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
8056 proto.write(SystemProto.Misc.LIGHT_DOZE_ENABLED_DURATION_MS,
8057 getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
8058 proto.write(SystemProto.Misc.LIGHT_DOZE_COUNT,
8059 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
8060 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_DURATION_MS,
8061 getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
8062 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_COUNT,
8063 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
8064 proto.write(SystemProto.Misc.LONGEST_LIGHT_DOZE_DURATION_MS,
8065 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
Kweku Adams103351f2017-10-16 14:39:34 -07008066 proto.end(mToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008067
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07008068 // Wifi multicast wakelock total stats (WIFI_MULTICAST_WAKELOCK_TOTAL_DATA)
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08008069 final long multicastWakeLockTimeTotalUs =
8070 getWifiMulticastWakelockTime(rawRealtimeUs, which);
8071 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07008072 final long wmctToken = proto.start(SystemProto.WIFI_MULTICAST_WAKELOCK_TOTAL);
8073 proto.write(SystemProto.WifiMulticastWakelockTotal.DURATION_MS,
8074 multicastWakeLockTimeTotalUs / 1000);
8075 proto.write(SystemProto.WifiMulticastWakelockTotal.COUNT,
8076 multicastWakeLockCountTotal);
8077 proto.end(wmctToken);
8078
Kweku Adams87b19ec2017-10-09 12:40:03 -07008079 // Power use item (POWER_USE_ITEM_DATA)
8080 final List<BatterySipper> sippers = helper.getUsageList();
8081 if (sippers != null) {
8082 for (int i = 0; i < sippers.size(); ++i) {
8083 final BatterySipper bs = sippers.get(i);
8084 int n = SystemProto.PowerUseItem.UNKNOWN_SIPPER;
8085 int uid = 0;
8086 switch (bs.drainType) {
Kweku Adamsef728952018-04-10 17:56:16 -07008087 case AMBIENT_DISPLAY:
8088 n = SystemProto.PowerUseItem.AMBIENT_DISPLAY;
8089 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07008090 case IDLE:
8091 n = SystemProto.PowerUseItem.IDLE;
8092 break;
8093 case CELL:
8094 n = SystemProto.PowerUseItem.CELL;
8095 break;
8096 case PHONE:
8097 n = SystemProto.PowerUseItem.PHONE;
8098 break;
8099 case WIFI:
8100 n = SystemProto.PowerUseItem.WIFI;
8101 break;
8102 case BLUETOOTH:
8103 n = SystemProto.PowerUseItem.BLUETOOTH;
8104 break;
8105 case SCREEN:
8106 n = SystemProto.PowerUseItem.SCREEN;
8107 break;
8108 case FLASHLIGHT:
8109 n = SystemProto.PowerUseItem.FLASHLIGHT;
8110 break;
8111 case APP:
Kweku Adams103351f2017-10-16 14:39:34 -07008112 // dumpProtoAppsLocked will handle this.
Kweku Adams87b19ec2017-10-09 12:40:03 -07008113 continue;
8114 case USER:
8115 n = SystemProto.PowerUseItem.USER;
8116 uid = UserHandle.getUid(bs.userId, 0);
8117 break;
8118 case UNACCOUNTED:
8119 n = SystemProto.PowerUseItem.UNACCOUNTED;
8120 break;
8121 case OVERCOUNTED:
8122 n = SystemProto.PowerUseItem.OVERCOUNTED;
8123 break;
8124 case CAMERA:
8125 n = SystemProto.PowerUseItem.CAMERA;
8126 break;
8127 case MEMORY:
8128 n = SystemProto.PowerUseItem.MEMORY;
8129 break;
8130 }
Kweku Adams103351f2017-10-16 14:39:34 -07008131 final long puiToken = proto.start(SystemProto.POWER_USE_ITEM);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008132 proto.write(SystemProto.PowerUseItem.NAME, n);
8133 proto.write(SystemProto.PowerUseItem.UID, uid);
8134 proto.write(SystemProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
8135 proto.write(SystemProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
8136 proto.write(SystemProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
8137 proto.write(SystemProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
8138 bs.proportionalSmearMah);
Kweku Adams103351f2017-10-16 14:39:34 -07008139 proto.end(puiToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008140 }
8141 }
8142
8143 // Power use summary (POWER_USE_SUMMARY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07008144 final long pusToken = proto.start(SystemProto.POWER_USE_SUMMARY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008145 proto.write(SystemProto.PowerUseSummary.BATTERY_CAPACITY_MAH,
8146 helper.getPowerProfile().getBatteryCapacity());
8147 proto.write(SystemProto.PowerUseSummary.COMPUTED_POWER_MAH, helper.getComputedPower());
8148 proto.write(SystemProto.PowerUseSummary.MIN_DRAINED_POWER_MAH, helper.getMinDrainedPower());
8149 proto.write(SystemProto.PowerUseSummary.MAX_DRAINED_POWER_MAH, helper.getMaxDrainedPower());
Kweku Adams103351f2017-10-16 14:39:34 -07008150 proto.end(pusToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008151
8152 // RPM stats (RESOURCE_POWER_MANAGER_DATA)
8153 final Map<String, ? extends Timer> rpmStats = getRpmStats();
8154 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
8155 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07008156 final long rpmToken = proto.start(SystemProto.RESOURCE_POWER_MANAGER);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008157 proto.write(SystemProto.ResourcePowerManager.NAME, ent.getKey());
8158 dumpTimer(proto, SystemProto.ResourcePowerManager.TOTAL,
8159 ent.getValue(), rawRealtimeUs, which);
8160 dumpTimer(proto, SystemProto.ResourcePowerManager.SCREEN_OFF,
8161 screenOffRpmStats.get(ent.getKey()), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008162 proto.end(rpmToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008163 }
8164
8165 // Screen brightness (SCREEN_BRIGHTNESS_DATA)
8166 for (int i = 0; i < NUM_SCREEN_BRIGHTNESS_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008167 final long sbToken = proto.start(SystemProto.SCREEN_BRIGHTNESS);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008168 proto.write(SystemProto.ScreenBrightness.NAME, i);
8169 dumpTimer(proto, SystemProto.ScreenBrightness.TOTAL, getScreenBrightnessTimer(i),
8170 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008171 proto.end(sbToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008172 }
8173
8174 // Signal scanning time (SIGNAL_SCANNING_TIME_DATA)
8175 dumpTimer(proto, SystemProto.SIGNAL_SCANNING, getPhoneSignalScanningTimer(), rawRealtimeUs,
8176 which);
8177
8178 // Phone signal strength (SIGNAL_STRENGTH_TIME_DATA and SIGNAL_STRENGTH_COUNT_DATA)
8179 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008180 final long pssToken = proto.start(SystemProto.PHONE_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008181 proto.write(SystemProto.PhoneSignalStrength.NAME, i);
8182 dumpTimer(proto, SystemProto.PhoneSignalStrength.TOTAL, getPhoneSignalStrengthTimer(i),
8183 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008184 proto.end(pssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008185 }
8186
8187 // Wakeup reasons (WAKEUP_REASON_DATA)
8188 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
8189 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07008190 final long wrToken = proto.start(SystemProto.WAKEUP_REASON);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008191 proto.write(SystemProto.WakeupReason.NAME, ent.getKey());
8192 dumpTimer(proto, SystemProto.WakeupReason.TOTAL, ent.getValue(), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008193 proto.end(wrToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008194 }
8195
8196 // Wifi signal strength (WIFI_SIGNAL_STRENGTH_TIME_DATA and WIFI_SIGNAL_STRENGTH_COUNT_DATA)
8197 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008198 final long wssToken = proto.start(SystemProto.WIFI_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008199 proto.write(SystemProto.WifiSignalStrength.NAME, i);
8200 dumpTimer(proto, SystemProto.WifiSignalStrength.TOTAL, getWifiSignalStrengthTimer(i),
8201 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008202 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008203 }
8204
8205 // Wifi state (WIFI_STATE_TIME_DATA and WIFI_STATE_COUNT_DATA)
8206 for (int i = 0; i < NUM_WIFI_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008207 final long wsToken = proto.start(SystemProto.WIFI_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008208 proto.write(SystemProto.WifiState.NAME, i);
8209 dumpTimer(proto, SystemProto.WifiState.TOTAL, getWifiStateTimer(i),
8210 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008211 proto.end(wsToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008212 }
8213
8214 // Wifi supplicant state (WIFI_SUPPL_STATE_TIME_DATA and WIFI_SUPPL_STATE_COUNT_DATA)
8215 for (int i = 0; i < NUM_WIFI_SUPPL_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008216 final long wssToken = proto.start(SystemProto.WIFI_SUPPLICANT_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008217 proto.write(SystemProto.WifiSupplicantState.NAME, i);
8218 dumpTimer(proto, SystemProto.WifiSupplicantState.TOTAL, getWifiSupplStateTimer(i),
8219 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008220 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008221 }
8222
8223 proto.end(sToken);
8224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008225}