blob: 9cf7de586d17ae6776c4cd94974822c88d55d12b [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.os;
18
Sudheer Shankab2f83c12017-11-13 19:25:01 -080019import android.app.ActivityManager;
Dianne Hackborn94326cb2017-06-28 16:17:20 -070020import android.app.job.JobParameters;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080021import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080022import android.content.pm.ApplicationInfo;
Bookatz8bdae8d2018-01-16 11:24:30 -080023import android.server.ServerProtoEnums;
Kweku Adams71a95312018-04-16 16:54:24 -070024import android.service.batterystats.BatteryStatsServiceDumpHistoryProto;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070025import android.service.batterystats.BatteryStatsServiceDumpProto;
Wink Saville52840902011-02-18 12:40:47 -080026import android.telephony.SignalStrength;
Tej Singheee317b2018-03-07 19:28:05 -080027import android.telephony.TelephonyManager;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080028import android.text.format.DateFormat;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070029import android.util.ArrayMap;
James Carr2dd7e5e2016-07-20 18:48:39 -070030import android.util.LongSparseArray;
Dianne Hackborn9cfba352016-03-24 17:31:28 -070031import android.util.MutableBoolean;
32import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.util.Printer;
34import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070035import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070036import android.util.TimeUtils;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070037import android.util.proto.ProtoOutputStream;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070038import android.view.Display;
Amith Yamasaniab9ad192016-12-06 12:46:59 -080039
Sudheer Shankab2f83c12017-11-13 19:25:01 -080040import com.android.internal.annotations.VisibleForTesting;
Siddharth Ray78ccaf52017-12-23 16:16:21 -080041import com.android.internal.location.gnssmetrics.GnssMetrics;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080042import com.android.internal.os.BatterySipper;
43import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
Kweku Adams2f73ecd2017-09-27 16:59:19 -070045import java.io.FileDescriptor;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070046import java.io.PrintWriter;
47import java.util.ArrayList;
48import java.util.Collections;
49import java.util.Comparator;
50import java.util.Formatter;
51import java.util.HashMap;
52import java.util.List;
53import java.util.Map;
54
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055/**
56 * A class providing access to battery usage statistics, including information on
57 * wakelocks, processes, packages, and services. All times are represented in microseconds
58 * except where indicated otherwise.
59 * @hide
60 */
61public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070062 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063
64 private static final boolean LOCAL_LOGV = false;
Bookatz82b341172017-09-07 19:06:08 -070065 /** Fetching RPM stats is too slow to do each time screen changes, so disable it. */
66 protected static final boolean SCREEN_OFF_RPM_STATS_ENABLED = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070067
68 /** @hide */
69 public static final String SERVICE_NAME = "batterystats";
70
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 /**
72 * A constant indicating a partial wake lock timer.
73 */
74 public static final int WAKE_TYPE_PARTIAL = 0;
75
76 /**
77 * A constant indicating a full wake lock timer.
78 */
79 public static final int WAKE_TYPE_FULL = 1;
80
81 /**
82 * A constant indicating a window wake lock timer.
83 */
84 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 /**
87 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 */
89 public static final int SENSOR = 3;
Mike Mac2f518a2017-09-19 16:06:03 -070090
The Android Open Source Project10592532009-03-18 17:39:46 -070091 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070092 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070093 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070094 public static final int WIFI_RUNNING = 4;
Mike Mac2f518a2017-09-19 16:06:03 -070095
Dianne Hackborn617f8772009-03-31 15:04:46 -070096 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070097 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070098 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070099 public static final int FULL_WIFI_LOCK = 5;
Mike Mac2f518a2017-09-19 16:06:03 -0700100
The Android Open Source Project10592532009-03-18 17:39:46 -0700101 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -0700102 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -0700103 */
Nick Pelly6ccaa542012-06-15 15:22:47 -0700104 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
Dianne Hackborn62793e42015-03-09 11:15:41 -0700106 /**
107 * A constant indicating a wifi multicast timer
108 */
109 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700112 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700113 */
114 public static final int VIDEO_TURNED_ON = 8;
115
116 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800117 * A constant indicating a vibrator on timer
118 */
119 public static final int VIBRATOR_ON = 9;
120
121 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700122 * A constant indicating a foreground activity timer
123 */
124 public static final int FOREGROUND_ACTIVITY = 10;
125
126 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700127 * A constant indicating a wifi batched scan is active
128 */
129 public static final int WIFI_BATCHED_SCAN = 11;
130
131 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700132 * A constant indicating a process state timer
133 */
134 public static final int PROCESS_STATE = 12;
135
136 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700137 * A constant indicating a sync timer
138 */
139 public static final int SYNC = 13;
140
141 /**
142 * A constant indicating a job timer
143 */
144 public static final int JOB = 14;
145
146 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800147 * A constant indicating an audio turn on timer
148 */
149 public static final int AUDIO_TURNED_ON = 15;
150
151 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700152 * A constant indicating a flashlight turn on timer
153 */
154 public static final int FLASHLIGHT_TURNED_ON = 16;
155
156 /**
157 * A constant indicating a camera turn on timer
158 */
159 public static final int CAMERA_TURNED_ON = 17;
160
161 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700162 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700163 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700164 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700165
166 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800167 * A constant indicating a bluetooth scan timer.
168 */
169 public static final int BLUETOOTH_SCAN_ON = 19;
170
171 /**
Bookatzc8c44962017-05-11 12:12:54 -0700172 * A constant indicating an aggregated partial wake lock timer.
173 */
174 public static final int AGGREGATED_WAKE_TYPE_PARTIAL = 20;
175
176 /**
Bookatzb1f04f32017-05-19 13:57:32 -0700177 * A constant indicating a bluetooth scan timer for unoptimized scans.
178 */
179 public static final int BLUETOOTH_UNOPTIMIZED_SCAN_ON = 21;
180
181 /**
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700182 * A constant indicating a foreground service timer
183 */
184 public static final int FOREGROUND_SERVICE = 22;
185
186 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -0800187 * A constant indicating an aggregate wifi multicast timer
188 */
189 public static final int WIFI_AGGREGATE_MULTICAST_ENABLED = 23;
190
191 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 * Include all of the data in the stats, including previously saved data.
193 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700194 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195
196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 * Include only the current run in the stats.
198 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700199 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200
201 /**
202 * Include only the run since the last time the device was unplugged in the stats.
203 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700204 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700205
206 // NOTE: Update this list if you add/change any stats above.
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700207 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700208 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700209 private static final String[] STAT_NAMES = { "l", "c", "u" };
210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700212 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700213 *
214 * New in version 19:
215 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800216 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700217 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Bookatz506a8182017-05-01 14:18:42 -0700218 * New in version 21:
219 * - Actual (not just apportioned) Wakelock time is also recorded.
Bookatzc8c44962017-05-11 12:12:54 -0700220 * - Aggregated partial wakelock time (per uid, instead of per wakelock) is recorded.
Bookatzb1f04f32017-05-19 13:57:32 -0700221 * - BLE scan result count
222 * - CPU frequency time per uid
223 * New in version 22:
224 * - BLE scan result background count, BLE unoptimized scan time
Bookatz98d4d5c2017-08-01 19:07:54 -0700225 * - Background partial wakelock time & count
226 * New in version 23:
227 * - Logging smeared power model values
228 * New in version 24:
229 * - Fixed bugs in background timers and BLE scan time
230 * New in version 25:
231 * - Package wakeup alarms are now on screen-off timebase
Bookatz50df7112017-08-04 14:53:26 -0700232 * New in version 26:
Bookatz82b341172017-09-07 19:06:08 -0700233 * - Resource power manager (rpm) states [but screenOffRpm is disabled from working properly]
Mike Mac2f518a2017-09-19 16:06:03 -0700234 * New in version 27:
235 * - Always On Display (screen doze mode) time and power
Mike Ma15313c92017-11-15 17:58:21 -0800236 * New in version 28:
237 * - Light/Deep Doze power
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700238 * - WiFi Multicast Wakelock statistics (count & duration)
Kweku Adamsa8943cb2017-12-22 13:21:06 -0800239 * New in version 29:
240 * - Process states re-ordered. TOP_SLEEPING now below BACKGROUND. HEAVY_WEIGHT introduced.
241 * - CPU times per UID process state
zhouwenjie46712bc2018-01-11 15:21:27 -0800242 * New in version 30:
243 * - Uid.PROCESS_STATE_FOREGROUND_SERVICE only tracks
244 * ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE.
Kweku Adamsb78430e2018-02-20 18:06:09 -0800245 * New in version 31:
246 * - New cellular network types.
247 * - Deferred job metrics.
Kweku Adams6fe1a5a2018-05-22 15:02:50 -0700248 * New in version 32:
249 * - Ambient display properly output in data dump.
zhouwenjied707da92018-07-06 10:23:00 -0700250 * New in version 33:
251 * - Fixed bug in min learned capacity updating process.
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700252 */
zhouwenjied707da92018-07-06 10:23:00 -0700253 static final int CHECKIN_VERSION = 33;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700254
255 /**
256 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700258 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700259
Evan Millar22ac0432009-03-31 11:33:18 -0700260 private static final long BYTES_PER_KB = 1024;
261 private static final long BYTES_PER_MB = 1048576; // 1024^2
262 private static final long BYTES_PER_GB = 1073741824; //1024^3
Bookatz506a8182017-05-01 14:18:42 -0700263
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700264 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800265 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700266 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700268 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700269 private static final String CPU_DATA = "cpu";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700270 private static final String GLOBAL_CPU_FREQ_DATA = "gcf";
271 private static final String CPU_TIMES_AT_FREQ_DATA = "ctf";
Bookatz50df7112017-08-04 14:53:26 -0700272 // rpm line is:
273 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "rpm", state/voter name, total time, total count,
274 // screen-off time, screen-off count
275 private static final String RESOURCE_POWER_MANAGER_DATA = "rpm";
Evan Millare84de8d2009-04-02 22:16:12 -0700276 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800277 private static final String VIBRATOR_DATA = "vib";
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700278 private static final String FOREGROUND_ACTIVITY_DATA = "fg";
279 // fgs line is:
280 // BATTERY_STATS_CHECKIN_VERSION, uid, category, "fgs",
281 // foreground service time, count
282 private static final String FOREGROUND_SERVICE_DATA = "fgs";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700283 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700284 // wl line is:
285 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
Bookatz5b5ec322017-05-26 09:40:38 -0700286 // full totalTime, 'f', count, current duration, max duration, total duration,
287 // partial totalTime, 'p', count, current duration, max duration, total duration,
288 // bg partial totalTime, 'bp', count, current duration, max duration, total duration,
289 // window totalTime, 'w', count, current duration, max duration, total duration
Bookatz506a8182017-05-01 14:18:42 -0700290 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700291 private static final String WAKELOCK_DATA = "wl";
Bookatzc8c44962017-05-11 12:12:54 -0700292 // awl line is:
293 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "awl",
294 // cumulative partial wakelock duration, cumulative background partial wakelock duration
295 private static final String AGGREGATED_WAKELOCK_DATA = "awl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700296 private static final String SYNC_DATA = "sy";
297 private static final String JOB_DATA = "jb";
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700298 private static final String JOB_COMPLETION_DATA = "jbc";
Amith Yamasani977e11f2018-02-16 11:29:54 -0800299
300 /**
301 * jbd line is:
302 * BATTERY_STATS_CHECKIN_VERSION, uid, which, "jbd",
Amith Yamasani0ca706b2018-03-01 17:28:59 -0800303 * jobsDeferredEventCount, jobsDeferredCount, totalLatencyMillis,
304 * 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 -0800305 * <p>
306 * @see #JOB_FRESHNESS_BUCKETS
307 */
308 private static final String JOBS_DEFERRED_DATA = "jbd";
Evan Millarc64edde2009-04-18 12:26:32 -0700309 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700310 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700311 private static final String NETWORK_DATA = "nt";
312 private static final String USER_ACTIVITY_DATA = "ua";
313 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800314 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700315 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700316 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700317 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800318 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
319 private static final String WIFI_CONTROLLER_DATA = "wfcd";
320 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
321 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700322 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700323 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800324 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800325 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
326 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800327 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700328 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700329 private static final String SCREEN_BRIGHTNESS_DATA = "br";
330 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700331 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700332 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
333 private static final String DATA_CONNECTION_TIME_DATA = "dct";
334 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800335 private static final String WIFI_STATE_TIME_DATA = "wst";
336 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700337 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
338 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
339 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
340 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800341 private static final String POWER_USE_SUMMARY_DATA = "pws";
342 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700343 private static final String DISCHARGE_STEP_DATA = "dsd";
344 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700345 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
346 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700347 private static final String FLASHLIGHT_DATA = "fla";
348 private static final String CAMERA_DATA = "cam";
349 private static final String VIDEO_DATA = "vid";
350 private static final String AUDIO_DATA = "aud";
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700351 private static final String WIFI_MULTICAST_TOTAL_DATA = "wmct";
352 private static final String WIFI_MULTICAST_DATA = "wmc";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353
Adam Lesinski010bf372016-04-11 12:18:18 -0700354 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
355
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700356 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 private final Formatter mFormatter = new Formatter(mFormatBuilder);
358
Siddharth Rayb50a6842017-12-14 15:15:28 -0800359 private static final String CELLULAR_CONTROLLER_NAME = "Cellular";
360 private static final String WIFI_CONTROLLER_NAME = "WiFi";
361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700363 * Indicates times spent by the uid at each cpu frequency in all process states.
364 *
365 * Other types might include times spent in foreground, background etc.
366 */
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800367 @VisibleForTesting
368 public static final String UID_TIMES_TYPE_ALL = "A";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700369
370 /**
Amith Yamasani977e11f2018-02-16 11:29:54 -0800371 * These are the thresholds for bucketing last time since a job was run for an app
372 * 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 -0800373 * than 1 hour ago, then it's reasonably fresh, 2 hours ago, not so fresh and so
Amith Yamasani977e11f2018-02-16 11:29:54 -0800374 * on.
375 */
376 public static final long[] JOB_FRESHNESS_BUCKETS = {
377 1 * 60 * 60 * 1000L,
378 2 * 60 * 60 * 1000L,
Amith Yamasani0ca706b2018-03-01 17:28:59 -0800379 4 * 60 * 60 * 1000L,
380 8 * 60 * 60 * 1000L,
Amith Yamasani977e11f2018-02-16 11:29:54 -0800381 Long.MAX_VALUE
382 };
383
384 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700385 * State for keeping track of counting information.
386 */
387 public static abstract class Counter {
388
389 /**
390 * Returns the count associated with this Counter for the
391 * selected type of statistics.
392 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700393 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700394 */
Evan Millarc64edde2009-04-18 12:26:32 -0700395 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700396
397 /**
398 * Temporary for debugging.
399 */
400 public abstract void logState(Printer pw, String prefix);
401 }
402
403 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700404 * State for keeping track of long counting information.
405 */
406 public static abstract class LongCounter {
407
408 /**
409 * Returns the count associated with this Counter for the
410 * selected type of statistics.
411 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700412 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700413 */
414 public abstract long getCountLocked(int which);
415
416 /**
417 * Temporary for debugging.
418 */
419 public abstract void logState(Printer pw, String prefix);
420 }
421
422 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700423 * State for keeping track of array of long counting information.
424 */
425 public static abstract class LongCounterArray {
426 /**
427 * Returns the counts associated with this Counter for the
428 * selected type of statistics.
429 *
430 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
431 */
432 public abstract long[] getCountsLocked(int which);
433
434 /**
435 * Temporary for debugging.
436 */
437 public abstract void logState(Printer pw, String prefix);
438 }
439
440 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800441 * Container class that aggregates counters for transmit, receive, and idle state of a
442 * radio controller.
443 */
444 public static abstract class ControllerActivityCounter {
445 /**
446 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
447 * idle state.
448 */
449 public abstract LongCounter getIdleTimeCounter();
450
451 /**
452 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
Siddharth Rayb50a6842017-12-14 15:15:28 -0800453 * scan state.
454 */
455 public abstract LongCounter getScanTimeCounter();
456
Siddharth Rayed754702018-02-15 12:44:37 -0800457 /**
458 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
459 * sleep state.
460 */
461 public abstract LongCounter getSleepTimeCounter();
Siddharth Rayb50a6842017-12-14 15:15:28 -0800462
463 /**
464 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800465 * receive state.
466 */
467 public abstract LongCounter getRxTimeCounter();
468
469 /**
470 * An array of {@link LongCounter}, representing various transmit levels, where each level
471 * may draw a different amount of power. The levels themselves are controller-specific.
472 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
473 * various transmit level states.
474 */
475 public abstract LongCounter[] getTxTimeCounters();
476
477 /**
478 * @return a non-null {@link LongCounter} representing the power consumed by the controller
479 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
480 * yield a value of 0 if the device doesn't support power calculations.
481 */
482 public abstract LongCounter getPowerCounter();
483 }
484
485 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 * State for keeping track of timing information.
487 */
488 public static abstract class Timer {
489
490 /**
491 * Returns the count associated with this Timer for the
492 * selected type of statistics.
493 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700494 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 */
Evan Millarc64edde2009-04-18 12:26:32 -0700496 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497
498 /**
499 * Returns the total time in microseconds associated with this Timer for the
500 * selected type of statistics.
501 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800502 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700503 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 * @return a time in microseconds
505 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800506 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700509 * Returns the total time in microseconds associated with this Timer since the
510 * 'mark' was last set.
511 *
512 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
513 * @return a time in microseconds
514 */
515 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
516
517 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700518 * Returns the max duration if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700519 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700520 */
521 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
522 return -1;
523 }
524
525 /**
526 * Returns the current time the timer has been active, if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700527 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700528 */
529 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
530 return -1;
531 }
532
533 /**
Kweku Adams103351f2017-10-16 14:39:34 -0700534 * Returns the total time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800535 *
536 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
537 * been on since reset.
538 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
539 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
540 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
541 * the actual total time.
Kweku Adams103351f2017-10-16 14:39:34 -0700542 * Not all Timer subclasses track the max, total, and current durations.
Bookatz867c0d72017-03-07 18:23:42 -0800543 */
544 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
545 return -1;
546 }
547
548 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700549 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
550 * used, for example, for tracking background usage. Secondary timers are never pooled.
551 *
552 * Not all Timer subclasses have a secondary timer; those that don't return null.
553 */
554 public Timer getSubTimer() {
555 return null;
556 }
557
558 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700559 * Returns whether the timer is currently running. Some types of timers
560 * (e.g. BatchTimers) don't know whether the event is currently active,
561 * and report false.
562 */
563 public boolean isRunningLocked() {
564 return false;
565 }
566
567 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 * Temporary for debugging.
569 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700570 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 }
572
573 /**
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800574 * Maps the ActivityManager procstate into corresponding BatteryStats procstate.
575 */
576 public static int mapToInternalProcessState(int procState) {
577 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
578 return ActivityManager.PROCESS_STATE_NONEXISTENT;
579 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
580 return Uid.PROCESS_STATE_TOP;
Dianne Hackborn10fc4fd2017-12-19 17:23:13 -0800581 } else if (procState == ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
582 // State when app has put itself in the foreground.
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800583 return Uid.PROCESS_STATE_FOREGROUND_SERVICE;
584 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
585 // Persistent and other foreground states go here.
586 return Uid.PROCESS_STATE_FOREGROUND;
587 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
588 return Uid.PROCESS_STATE_BACKGROUND;
589 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
590 return Uid.PROCESS_STATE_TOP_SLEEPING;
591 } else if (procState <= ActivityManager.PROCESS_STATE_HEAVY_WEIGHT) {
592 return Uid.PROCESS_STATE_HEAVY_WEIGHT;
593 } else {
594 return Uid.PROCESS_STATE_CACHED;
595 }
596 }
597
598 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 * The statistics associated with a particular uid.
600 */
601 public static abstract class Uid {
602
603 /**
604 * Returns a mapping containing wakelock statistics.
605 *
606 * @return a Map from Strings to Uid.Wakelock objects.
607 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700608 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609
610 /**
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700611 * Returns the WiFi Multicast Wakelock statistics.
612 *
613 * @return a Timer Object for the per uid Multicast statistics.
614 */
615 public abstract Timer getMulticastWakelockStats();
616
617 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700618 * Returns a mapping containing sync statistics.
619 *
620 * @return a Map from Strings to Timer objects.
621 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700622 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700623
624 /**
625 * Returns a mapping containing scheduled job statistics.
626 *
627 * @return a Map from Strings to Timer objects.
628 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700629 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700630
631 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700632 * Returns statistics about how jobs have completed.
633 *
634 * @return A Map of String job names to completion type -> count mapping.
635 */
636 public abstract ArrayMap<String, SparseIntArray> getJobCompletionStats();
637
638 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 * The statistics associated with a particular wake lock.
640 */
641 public static abstract class Wakelock {
642 public abstract Timer getWakeTime(int type);
643 }
644
645 /**
Bookatzc8c44962017-05-11 12:12:54 -0700646 * The cumulative time the uid spent holding any partial wakelocks. This will generally
647 * differ from summing over the Wakelocks in getWakelockStats since the latter may have
648 * wakelocks that overlap in time (and therefore over-counts).
649 */
650 public abstract Timer getAggregatedPartialWakelockTimer();
651
652 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 * Returns a mapping containing sensor statistics.
654 *
655 * @return a Map from Integer sensor ids to Uid.Sensor objects.
656 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700657 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658
659 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700660 * Returns a mapping containing active process data.
661 */
662 public abstract SparseArray<? extends Pid> getPidStats();
Bookatzc8c44962017-05-11 12:12:54 -0700663
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700664 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 * Returns a mapping containing process statistics.
666 *
667 * @return a Map from Strings to Uid.Proc objects.
668 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700669 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670
671 /**
672 * Returns a mapping containing package statistics.
673 *
674 * @return a Map from Strings to Uid.Pkg objects.
675 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700676 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700677
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800678 public abstract ControllerActivityCounter getWifiControllerActivity();
679 public abstract ControllerActivityCounter getBluetoothControllerActivity();
680 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800681
682 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 * {@hide}
684 */
685 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700686
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800687 public abstract void noteWifiRunningLocked(long elapsedRealtime);
688 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
689 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
690 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
691 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
692 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
693 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
694 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
695 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
696 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800697 public abstract void noteActivityResumedLocked(long elapsedRealtime);
698 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800699 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
700 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
701 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700702 public abstract int getWifiScanCount(int which);
Kweku Adams103351f2017-10-16 14:39:34 -0700703 /**
704 * Returns the timer keeping track of wifi scans.
705 */
706 public abstract Timer getWifiScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800707 public abstract int getWifiScanBackgroundCount(int which);
708 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
709 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -0700710 /**
711 * Returns the timer keeping track of background wifi scans.
712 */
713 public abstract Timer getWifiScanBackgroundTimer();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800714 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700715 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800716 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700717 public abstract Timer getAudioTurnedOnTimer();
718 public abstract Timer getVideoTurnedOnTimer();
719 public abstract Timer getFlashlightTurnedOnTimer();
720 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700721 public abstract Timer getForegroundActivityTimer();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700722
723 /**
724 * Returns the timer keeping track of Foreground Service time
725 */
726 public abstract Timer getForegroundServiceTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800727 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800728 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatzb1f04f32017-05-19 13:57:32 -0700729 public abstract Timer getBluetoothUnoptimizedScanTimer();
730 public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700731 public abstract Counter getBluetoothScanResultCounter();
Bookatzb1f04f32017-05-19 13:57:32 -0700732 public abstract Counter getBluetoothScanResultBgCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700733
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700734 public abstract long[] getCpuFreqTimes(int which);
735 public abstract long[] getScreenOffCpuFreqTimes(int which);
Mike Ma3d422c32017-10-25 11:08:57 -0700736 /**
737 * Returns cpu active time of an uid.
738 */
739 public abstract long getCpuActiveTime();
740 /**
741 * Returns cpu times of an uid on each cluster
742 */
743 public abstract long[] getCpuClusterTimes();
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700744
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800745 /**
746 * Returns cpu times of an uid at a particular process state.
747 */
748 public abstract long[] getCpuFreqTimes(int which, int procState);
749 /**
750 * Returns cpu times of an uid while the screen if off at a particular process state.
751 */
752 public abstract long[] getScreenOffCpuFreqTimes(int which, int procState);
753
Dianne Hackborna0200e32016-03-30 18:01:41 -0700754 // Note: the following times are disjoint. They can be added together to find the
755 // total time a uid has had any processes running at all.
756
757 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800758 * Time this uid has any processes in the top state.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700759 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800760 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700761 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800762 * Time this uid has any process with a started foreground service, but
Dianne Hackborna0200e32016-03-30 18:01:41 -0700763 * none in the "top" state.
764 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800765 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700766 /**
Dianne Hackborna0200e32016-03-30 18:01:41 -0700767 * Time this uid has any process in an active foreground state, but none in the
zhouwenjie46712bc2018-01-11 15:21:27 -0800768 * "foreground service" or better state. Persistent and other foreground states go here.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700769 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800770 public static final int PROCESS_STATE_FOREGROUND = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700771 /**
772 * Time this uid has any process in an active background state, but none in the
773 * "foreground" or better state.
774 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800775 public static final int PROCESS_STATE_BACKGROUND = 3;
776 /**
777 * Time this uid has any process that is top while the device is sleeping, but not
778 * active for any other reason. We kind-of consider it a kind of cached process
779 * for execution restrictions.
780 */
781 public static final int PROCESS_STATE_TOP_SLEEPING = 4;
782 /**
783 * Time this uid has any process that is in the background but it has an activity
784 * marked as "can't save state". This is essentially a cached process, though the
785 * system will try much harder than normal to avoid killing it.
786 */
787 public static final int PROCESS_STATE_HEAVY_WEIGHT = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700788 /**
789 * Time this uid has any processes that are sitting around cached, not in one of the
790 * other active states.
791 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800792 public static final int PROCESS_STATE_CACHED = 6;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700793 /**
794 * Total number of process states we track.
795 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800796 public static final int NUM_PROCESS_STATE = 7;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700797
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800798 // Used in dump
Dianne Hackborn61659e52014-07-09 16:13:01 -0700799 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800800 "Top", "Fg Service", "Foreground", "Background", "Top Sleeping", "Heavy Weight",
801 "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700802 };
803
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800804 // Used in checkin dump
805 @VisibleForTesting
806 public static final String[] UID_PROCESS_TYPES = {
807 "T", // TOP
808 "FS", // FOREGROUND_SERVICE
809 "F", // FOREGROUND
810 "B", // BACKGROUND
811 "TS", // TOP_SLEEPING
812 "HW", // HEAVY_WEIGHT
813 "C" // CACHED
814 };
815
816 /**
817 * When the process exits one of these states, we need to make sure cpu time in this state
818 * is not attributed to any non-critical process states.
819 */
820 public static final int[] CRITICAL_PROC_STATES = {
821 PROCESS_STATE_TOP, PROCESS_STATE_FOREGROUND_SERVICE, PROCESS_STATE_FOREGROUND
822 };
823
Dianne Hackborn61659e52014-07-09 16:13:01 -0700824 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800825 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700826
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800827 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828
Robert Greenwalta029ea12013-09-25 16:38:12 -0700829 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
830
Dianne Hackborn617f8772009-03-31 15:04:46 -0700831 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700832 * Note that these must match the constants in android.os.PowerManager.
833 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
834 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700835 */
836 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700837 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700838 };
Bookatzc8c44962017-05-11 12:12:54 -0700839
Phil Weaverda80d672016-03-15 16:25:46 -0700840 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700841
Dianne Hackborn617f8772009-03-31 15:04:46 -0700842 public abstract void noteUserActivityLocked(int type);
843 public abstract boolean hasUserActivity();
844 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700845
846 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800847 public abstract long getNetworkActivityBytes(int type, int which);
848 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800849 public abstract long getMobileRadioActiveTime(int which);
850 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700851
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700852 /**
853 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
854 */
855 public abstract long getUserCpuTimeUs(int which);
856
857 /**
858 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
859 */
860 public abstract long getSystemCpuTimeUs(int which);
861
862 /**
Sudheer Shanka71f34b32017-07-21 00:14:24 -0700863 * Returns the approximate cpu time (in microseconds) spent at a certain CPU speed for a
Adam Lesinski6832f392015-09-05 18:05:40 -0700864 * given CPU cluster.
865 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700866 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700867 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700868 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
869 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700870 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700871 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700872
Adam Lesinski5f056f62016-07-14 16:56:08 -0700873 /**
874 * Returns the number of times this UID woke up the Application Processor to
875 * process a mobile radio packet.
876 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
877 */
878 public abstract long getMobileRadioApWakeupCount(int which);
879
880 /**
881 * Returns the number of times this UID woke up the Application Processor to
882 * process a WiFi packet.
883 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
884 */
885 public abstract long getWifiRadioApWakeupCount(int which);
886
Amith Yamasani977e11f2018-02-16 11:29:54 -0800887 /**
888 * Appends the deferred jobs data to the StringBuilder passed in, in checkin format
889 * @param sb StringBuilder that can be overwritten with the deferred jobs data
890 * @param which one of STATS_*
891 */
892 public abstract void getDeferredJobsCheckinLineLocked(StringBuilder sb, int which);
893
894 /**
895 * Appends the deferred jobs data to the StringBuilder passed in
896 * @param sb StringBuilder that can be overwritten with the deferred jobs data
897 * @param which one of STATS_*
898 */
899 public abstract void getDeferredJobsLineLocked(StringBuilder sb, int which);
900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800902 /*
903 * FIXME: it's not correct to use this magic value because it
904 * could clash with a sensor handle (which are defined by
905 * the sensor HAL, and therefore out of our control
906 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 // Magic sensor number for the GPS.
908 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800913
Bookatz867c0d72017-03-07 18:23:42 -0800914 /** Returns a Timer for sensor usage when app is in the background. */
915 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 }
917
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700918 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800919 public int mWakeNesting;
920 public long mWakeSumMs;
921 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700922 }
923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 /**
925 * The statistics associated with a particular process.
926 */
927 public static abstract class Proc {
928
Dianne Hackborn287952c2010-09-22 22:34:31 -0700929 public static class ExcessivePower {
930 public static final int TYPE_WAKE = 1;
931 public static final int TYPE_CPU = 2;
932
933 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700934 public long overTime;
935 public long usedTime;
936 }
937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800939 * Returns true if this process is still active in the battery stats.
940 */
941 public abstract boolean isActive();
942
943 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700944 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700946 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 */
948 public abstract long getUserTime(int which);
949
950 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700951 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700953 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 */
955 public abstract long getSystemTime(int which);
956
957 /**
958 * Returns the number of times the process has been started.
959 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700960 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 */
962 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700963
964 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800965 * Returns the number of times the process has crashed.
966 *
967 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
968 */
969 public abstract int getNumCrashes(int which);
970
971 /**
972 * Returns the number of times the process has ANRed.
973 *
974 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
975 */
976 public abstract int getNumAnrs(int which);
977
978 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700979 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700980 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700981 * @return foreground cpu time in microseconds
982 */
983 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700984
Dianne Hackborn287952c2010-09-22 22:34:31 -0700985 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700986
Dianne Hackborn287952c2010-09-22 22:34:31 -0700987 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 }
989
990 /**
991 * The statistics associated with a particular package.
992 */
993 public static abstract class Pkg {
994
995 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700996 * Returns information about all wakeup alarms that have been triggered for this
997 * package. The mapping keys are tag names for the alarms, the counter contains
998 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001000 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001
1002 /**
1003 * Returns a mapping containing service statistics.
1004 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001005 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006
1007 /**
1008 * The statistics associated with a particular service.
1009 */
Joe Onoratoabded112016-02-08 16:49:39 -08001010 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011
1012 /**
1013 * Returns the amount of time spent started.
1014 *
1015 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001016 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 * @return
1018 */
1019 public abstract long getStartTime(long batteryUptime, int which);
1020
1021 /**
1022 * Returns the total number of times startService() has been called.
1023 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001024 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 */
1026 public abstract int getStarts(int which);
1027
1028 /**
1029 * Returns the total number times the service has been launched.
1030 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07001031 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 */
1033 public abstract int getLaunches(int which);
1034 }
1035 }
1036 }
1037
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001038 public static final class LevelStepTracker {
1039 public long mLastStepTime = -1;
1040 public int mNumStepDurations;
1041 public final long[] mStepDurations;
1042
1043 public LevelStepTracker(int maxLevelSteps) {
1044 mStepDurations = new long[maxLevelSteps];
1045 }
1046
1047 public LevelStepTracker(int numSteps, long[] steps) {
1048 mNumStepDurations = numSteps;
1049 mStepDurations = new long[numSteps];
1050 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
1051 }
1052
1053 public long getDurationAt(int index) {
1054 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
1055 }
1056
1057 public int getLevelAt(int index) {
1058 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
1059 >> STEP_LEVEL_LEVEL_SHIFT);
1060 }
1061
1062 public int getInitModeAt(int index) {
1063 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
1064 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1065 }
1066
1067 public int getModModeAt(int index) {
1068 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
1069 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1070 }
1071
1072 private void appendHex(long val, int topOffset, StringBuilder out) {
1073 boolean hasData = false;
1074 while (topOffset >= 0) {
1075 int digit = (int)( (val>>topOffset) & 0xf );
1076 topOffset -= 4;
1077 if (!hasData && digit == 0) {
1078 continue;
1079 }
1080 hasData = true;
1081 if (digit >= 0 && digit <= 9) {
1082 out.append((char)('0' + digit));
1083 } else {
1084 out.append((char)('a' + digit - 10));
1085 }
1086 }
1087 }
1088
1089 public void encodeEntryAt(int index, StringBuilder out) {
1090 long item = mStepDurations[index];
1091 long duration = item & STEP_LEVEL_TIME_MASK;
1092 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
1093 >> STEP_LEVEL_LEVEL_SHIFT);
1094 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
1095 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1096 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
1097 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1098 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1099 case Display.STATE_OFF: out.append('f'); break;
1100 case Display.STATE_ON: out.append('o'); break;
1101 case Display.STATE_DOZE: out.append('d'); break;
1102 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
1103 }
1104 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1105 out.append('p');
1106 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001107 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1108 out.append('i');
1109 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001110 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1111 case Display.STATE_OFF: out.append('F'); break;
1112 case Display.STATE_ON: out.append('O'); break;
1113 case Display.STATE_DOZE: out.append('D'); break;
1114 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
1115 }
1116 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1117 out.append('P');
1118 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001119 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1120 out.append('I');
1121 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001122 out.append('-');
1123 appendHex(level, 4, out);
1124 out.append('-');
1125 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
1126 }
1127
1128 public void decodeEntryAt(int index, String value) {
1129 final int N = value.length();
1130 int i = 0;
1131 char c;
1132 long out = 0;
1133 while (i < N && (c=value.charAt(i)) != '-') {
1134 i++;
1135 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001136 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001137 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001138 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001139 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001140 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001141 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001142 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1143 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1144 break;
1145 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1146 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1147 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001148 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1149 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1150 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001151 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1152 break;
1153 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1154 break;
1155 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1156 break;
1157 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1158 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1159 break;
1160 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1161 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001162 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001163 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1164 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1165 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001166 }
1167 }
1168 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001169 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001170 while (i < N && (c=value.charAt(i)) != '-') {
1171 i++;
1172 level <<= 4;
1173 if (c >= '0' && c <= '9') {
1174 level += c - '0';
1175 } else if (c >= 'a' && c <= 'f') {
1176 level += c - 'a' + 10;
1177 } else if (c >= 'A' && c <= 'F') {
1178 level += c - 'A' + 10;
1179 }
1180 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001181 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001182 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
1183 long duration = 0;
1184 while (i < N && (c=value.charAt(i)) != '-') {
1185 i++;
1186 duration <<= 4;
1187 if (c >= '0' && c <= '9') {
1188 duration += c - '0';
1189 } else if (c >= 'a' && c <= 'f') {
1190 duration += c - 'a' + 10;
1191 } else if (c >= 'A' && c <= 'F') {
1192 duration += c - 'A' + 10;
1193 }
1194 }
1195 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
1196 }
1197
1198 public void init() {
1199 mLastStepTime = -1;
1200 mNumStepDurations = 0;
1201 }
1202
1203 public void clearTime() {
1204 mLastStepTime = -1;
1205 }
1206
1207 public long computeTimePerLevel() {
1208 final long[] steps = mStepDurations;
1209 final int numSteps = mNumStepDurations;
1210
1211 // For now we'll do a simple average across all steps.
1212 if (numSteps <= 0) {
1213 return -1;
1214 }
1215 long total = 0;
1216 for (int i=0; i<numSteps; i++) {
1217 total += steps[i] & STEP_LEVEL_TIME_MASK;
1218 }
1219 return total / numSteps;
1220 /*
1221 long[] buckets = new long[numSteps];
1222 int numBuckets = 0;
1223 int numToAverage = 4;
1224 int i = 0;
1225 while (i < numSteps) {
1226 long totalTime = 0;
1227 int num = 0;
1228 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1229 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1230 num++;
1231 }
1232 buckets[numBuckets] = totalTime / num;
1233 numBuckets++;
1234 numToAverage *= 2;
1235 i += num;
1236 }
1237 if (numBuckets < 1) {
1238 return -1;
1239 }
1240 long averageTime = buckets[numBuckets-1];
1241 for (i=numBuckets-2; i>=0; i--) {
1242 averageTime = (averageTime + buckets[i]) / 2;
1243 }
1244 return averageTime;
1245 */
1246 }
1247
1248 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1249 int[] outNumOfInterest) {
1250 final long[] steps = mStepDurations;
1251 final int count = mNumStepDurations;
1252 if (count <= 0) {
1253 return -1;
1254 }
1255 long total = 0;
1256 int numOfInterest = 0;
1257 for (int i=0; i<count; i++) {
1258 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1259 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1260 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1261 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1262 // If the modes of interest didn't change during this step period...
1263 if ((modMode&modesOfInterest) == 0) {
1264 // And the mode values during this period match those we are measuring...
1265 if ((initMode&modesOfInterest) == modeValues) {
1266 // Then this can be used to estimate the total time!
1267 numOfInterest++;
1268 total += steps[i] & STEP_LEVEL_TIME_MASK;
1269 }
1270 }
1271 }
1272 if (numOfInterest <= 0) {
1273 return -1;
1274 }
1275
1276 if (outNumOfInterest != null) {
1277 outNumOfInterest[0] = numOfInterest;
1278 }
1279
1280 // The estimated time is the average time we spend in each level, multipled
1281 // by 100 -- the total number of battery levels
1282 return (total / numOfInterest) * 100;
1283 }
1284
1285 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1286 int stepCount = mNumStepDurations;
1287 final long lastStepTime = mLastStepTime;
1288 if (lastStepTime >= 0 && numStepLevels > 0) {
1289 final long[] steps = mStepDurations;
1290 long duration = elapsedRealtime - lastStepTime;
1291 for (int i=0; i<numStepLevels; i++) {
1292 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1293 long thisDuration = duration / (numStepLevels-i);
1294 duration -= thisDuration;
1295 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1296 thisDuration = STEP_LEVEL_TIME_MASK;
1297 }
1298 steps[0] = thisDuration | modeBits;
1299 }
1300 stepCount += numStepLevels;
1301 if (stepCount > steps.length) {
1302 stepCount = steps.length;
1303 }
1304 }
1305 mNumStepDurations = stepCount;
1306 mLastStepTime = elapsedRealtime;
1307 }
1308
1309 public void readFromParcel(Parcel in) {
1310 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001311 if (N > mStepDurations.length) {
1312 throw new ParcelFormatException("more step durations than available: " + N);
1313 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001314 mNumStepDurations = N;
1315 for (int i=0; i<N; i++) {
1316 mStepDurations[i] = in.readLong();
1317 }
1318 }
1319
1320 public void writeToParcel(Parcel out) {
1321 final int N = mNumStepDurations;
1322 out.writeInt(N);
1323 for (int i=0; i<N; i++) {
1324 out.writeLong(mStepDurations[i]);
1325 }
1326 }
1327 }
1328
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001329 public static final class PackageChange {
1330 public String mPackageName;
1331 public boolean mUpdate;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001332 public long mVersionCode;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001333 }
1334
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001335 public static final class DailyItem {
1336 public long mStartTime;
1337 public long mEndTime;
1338 public LevelStepTracker mDischargeSteps;
1339 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001340 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001341 }
1342
1343 public abstract DailyItem getDailyItemLocked(int daysAgo);
1344
1345 public abstract long getCurrentDailyStartTime();
1346
1347 public abstract long getNextMinDailyDeadline();
1348
1349 public abstract long getNextMaxDailyDeadline();
1350
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001351 public abstract long[] getCpuFreqs();
1352
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001353 public final static class HistoryTag {
1354 public String string;
1355 public int uid;
1356
1357 public int poolIdx;
1358
1359 public void setTo(HistoryTag o) {
1360 string = o.string;
1361 uid = o.uid;
1362 poolIdx = o.poolIdx;
1363 }
1364
1365 public void setTo(String _string, int _uid) {
1366 string = _string;
1367 uid = _uid;
1368 poolIdx = -1;
1369 }
1370
1371 public void writeToParcel(Parcel dest, int flags) {
1372 dest.writeString(string);
1373 dest.writeInt(uid);
1374 }
1375
1376 public void readFromParcel(Parcel src) {
1377 string = src.readString();
1378 uid = src.readInt();
1379 poolIdx = -1;
1380 }
1381
1382 @Override
1383 public boolean equals(Object o) {
1384 if (this == o) return true;
1385 if (o == null || getClass() != o.getClass()) return false;
1386
1387 HistoryTag that = (HistoryTag) o;
1388
1389 if (uid != that.uid) return false;
1390 if (!string.equals(that.string)) return false;
1391
1392 return true;
1393 }
1394
1395 @Override
1396 public int hashCode() {
1397 int result = string.hashCode();
1398 result = 31 * result + uid;
1399 return result;
1400 }
1401 }
1402
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001403 /**
1404 * Optional detailed information that can go into a history step. This is typically
1405 * generated each time the battery level changes.
1406 */
1407 public final static class HistoryStepDetails {
1408 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1409 public int userTime;
1410 public int systemTime;
1411
1412 // Top three apps using CPU in the last step, with times in 1/100 second.
1413 public int appCpuUid1;
1414 public int appCpuUTime1;
1415 public int appCpuSTime1;
1416 public int appCpuUid2;
1417 public int appCpuUTime2;
1418 public int appCpuSTime2;
1419 public int appCpuUid3;
1420 public int appCpuUTime3;
1421 public int appCpuSTime3;
1422
1423 // Information from /proc/stat
1424 public int statUserTime;
1425 public int statSystemTime;
1426 public int statIOWaitTime;
1427 public int statIrqTime;
1428 public int statSoftIrqTime;
1429 public int statIdlTime;
1430
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001431 // Platform-level low power state stats
1432 public String statPlatformIdleState;
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001433 public String statSubsystemPowerState;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001434
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001435 public HistoryStepDetails() {
1436 clear();
1437 }
1438
1439 public void clear() {
1440 userTime = systemTime = 0;
1441 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1442 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1443 = appCpuUTime3 = appCpuSTime3 = 0;
1444 }
1445
1446 public void writeToParcel(Parcel out) {
1447 out.writeInt(userTime);
1448 out.writeInt(systemTime);
1449 out.writeInt(appCpuUid1);
1450 out.writeInt(appCpuUTime1);
1451 out.writeInt(appCpuSTime1);
1452 out.writeInt(appCpuUid2);
1453 out.writeInt(appCpuUTime2);
1454 out.writeInt(appCpuSTime2);
1455 out.writeInt(appCpuUid3);
1456 out.writeInt(appCpuUTime3);
1457 out.writeInt(appCpuSTime3);
1458 out.writeInt(statUserTime);
1459 out.writeInt(statSystemTime);
1460 out.writeInt(statIOWaitTime);
1461 out.writeInt(statIrqTime);
1462 out.writeInt(statSoftIrqTime);
1463 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001464 out.writeString(statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001465 out.writeString(statSubsystemPowerState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001466 }
1467
1468 public void readFromParcel(Parcel in) {
1469 userTime = in.readInt();
1470 systemTime = in.readInt();
1471 appCpuUid1 = in.readInt();
1472 appCpuUTime1 = in.readInt();
1473 appCpuSTime1 = in.readInt();
1474 appCpuUid2 = in.readInt();
1475 appCpuUTime2 = in.readInt();
1476 appCpuSTime2 = in.readInt();
1477 appCpuUid3 = in.readInt();
1478 appCpuUTime3 = in.readInt();
1479 appCpuSTime3 = in.readInt();
1480 statUserTime = in.readInt();
1481 statSystemTime = in.readInt();
1482 statIOWaitTime = in.readInt();
1483 statIrqTime = in.readInt();
1484 statSoftIrqTime = in.readInt();
1485 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001486 statPlatformIdleState = in.readString();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001487 statSubsystemPowerState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001488 }
1489 }
1490
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001491 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001492 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001493
1494 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001495 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001496
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001497 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001498 public static final byte CMD_NULL = -1;
1499 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001500 public static final byte CMD_CURRENT_TIME = 5;
1501 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001502 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001503 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001504
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001505 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001506
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001507 /**
1508 * Return whether the command code is a delta data update.
1509 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001510 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001511 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001512 }
1513
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001514 public byte batteryLevel;
1515 public byte batteryStatus;
1516 public byte batteryHealth;
1517 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001518
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001519 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001520 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001521
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001522 // The charge of the battery in micro-Ampere-hours.
1523 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001524
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001525 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001526 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001527 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001528 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001529 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1530 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001531 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001532 public static final int STATE_PHONE_STATE_SHIFT = 6;
1533 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001534 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001535 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001536 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001537
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001538 // These states always appear directly in the first int token
1539 // of a delta change; they should be ones that change relatively
1540 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001541 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1542 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001543 public static final int STATE_GPS_ON_FLAG = 1<<29;
1544 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001545 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001546 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001547 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001548 // Do not use, this is used for coulomb delta count.
1549 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001550 // These are on the lower bits used for the command; if they change
1551 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001552 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001553 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001554 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001555 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1556 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
Mike Mac2f518a2017-09-19 16:06:03 -07001557 public static final int STATE_SCREEN_DOZE_FLAG = 1 << 18;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001558 // empty slot
1559 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001560
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001561 public static final int MOST_INTERESTING_STATES =
Mike Mac2f518a2017-09-19 16:06:03 -07001562 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG | STATE_SCREEN_DOZE_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001563
1564 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001565
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001566 public int states;
1567
Dianne Hackborn3251b902014-06-20 14:40:53 -07001568 // Constants from WIFI_SUPPL_STATE_*
1569 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1570 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1571 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1572 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1573 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1574 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
Siddharth Ray78ccaf52017-12-23 16:16:21 -08001575 // Values for NUM_GPS_SIGNAL_QUALITY_LEVELS
1576 public static final int STATE2_GPS_SIGNAL_QUALITY_SHIFT = 7;
1577 public static final int STATE2_GPS_SIGNAL_QUALITY_MASK =
1578 0x1 << STATE2_GPS_SIGNAL_QUALITY_SHIFT;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001579
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001580 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001581 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1582 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1583 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001584 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001585 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1586 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1587 public static final int STATE2_CHARGING_FLAG = 1<<24;
1588 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1589 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1590 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001591 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Siddharth Rayf5e796a2018-01-22 18:18:17 -08001592 public static final int STATE2_CELLULAR_HIGH_TX_POWER_FLAG = 1 << 19;
Mike Ma926a97c2018-03-25 02:32:35 -07001593 public static final int STATE2_USB_DATA_LINK_FLAG = 1 << 18;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001594
1595 public static final int MOST_INTERESTING_STATES2 =
Mike Mac2f518a2017-09-19 16:06:03 -07001596 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
1597 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001598
1599 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001600
Dianne Hackborn40c87252014-03-19 16:55:40 -07001601 public int states2;
1602
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001603 // The wake lock that was acquired at this point.
1604 public HistoryTag wakelockTag;
1605
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001606 // Kernel wakeup reason at this point.
1607 public HistoryTag wakeReasonTag;
1608
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001609 // Non-null when there is more detailed information at this step.
1610 public HistoryStepDetails stepDetails;
1611
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001612 public static final int EVENT_FLAG_START = 0x8000;
1613 public static final int EVENT_FLAG_FINISH = 0x4000;
1614
1615 // No event in this item.
1616 public static final int EVENT_NONE = 0x0000;
1617 // Event is about a process that is running.
1618 public static final int EVENT_PROC = 0x0001;
1619 // Event is about an application package that is in the foreground.
1620 public static final int EVENT_FOREGROUND = 0x0002;
1621 // Event is about an application package that is at the top of the screen.
1622 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001623 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001624 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001625 // Events for all additional wake locks aquired/release within a wake block.
1626 // These are not generated by default.
1627 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001628 // Event is about an application executing a scheduled job.
1629 public static final int EVENT_JOB = 0x0006;
1630 // Events for users running.
1631 public static final int EVENT_USER_RUNNING = 0x0007;
1632 // Events for foreground user.
1633 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001634 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001635 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001636 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001637 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001638 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001639 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001640 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001641 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001642 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001643 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001644 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001645 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001646 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001647 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001648 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001649 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001650 // Event for a package being on the temporary whitelist.
1651 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001652 // Event for the screen waking up.
1653 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001654 // Event for the UID that woke up the application processor.
1655 // Used for wakeups coming from WiFi, modem, etc.
1656 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001657 // Event for reporting that a specific partial wake lock has been held for a long duration.
1658 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001659
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001660 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001661 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001662 // Mask to extract out only the type part of the event.
1663 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001664
1665 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1666 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1667 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1668 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1669 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1670 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001671 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1672 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001673 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1674 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001675 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1676 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1677 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1678 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1679 public static final int EVENT_USER_FOREGROUND_START =
1680 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1681 public static final int EVENT_USER_FOREGROUND_FINISH =
1682 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001683 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1684 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001685 public static final int EVENT_TEMP_WHITELIST_START =
1686 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1687 public static final int EVENT_TEMP_WHITELIST_FINISH =
1688 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001689 public static final int EVENT_LONG_WAKE_LOCK_START =
1690 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1691 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1692 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001693
1694 // For CMD_EVENT.
1695 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001696 public HistoryTag eventTag;
1697
Dianne Hackborn9a755432014-05-15 17:05:22 -07001698 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001699 public long currentTime;
1700
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001701 // Meta-data when reading.
1702 public int numReadInts;
1703
1704 // Pre-allocated objects.
1705 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001706 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001707 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001708
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001709 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001710 }
Bookatzc8c44962017-05-11 12:12:54 -07001711
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001712 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001713 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001714 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001715 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001716 }
Bookatzc8c44962017-05-11 12:12:54 -07001717
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001718 public int describeContents() {
1719 return 0;
1720 }
1721
1722 public void writeToParcel(Parcel dest, int flags) {
1723 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001724 int bat = (((int)cmd)&0xff)
1725 | ((((int)batteryLevel)<<8)&0xff00)
1726 | ((((int)batteryStatus)<<16)&0xf0000)
1727 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001728 | ((((int)batteryPlugType)<<24)&0xf000000)
1729 | (wakelockTag != null ? 0x10000000 : 0)
1730 | (wakeReasonTag != null ? 0x20000000 : 0)
1731 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001732 dest.writeInt(bat);
1733 bat = (((int)batteryTemperature)&0xffff)
1734 | ((((int)batteryVoltage)<<16)&0xffff0000);
1735 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001736 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001737 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001738 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001739 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001740 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001741 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001742 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001743 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001744 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001745 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001746 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001747 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001748 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001749 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001750 dest.writeLong(currentTime);
1751 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001752 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001753
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001754 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001755 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001756 int bat = src.readInt();
1757 cmd = (byte)(bat&0xff);
1758 batteryLevel = (byte)((bat>>8)&0xff);
1759 batteryStatus = (byte)((bat>>16)&0xf);
1760 batteryHealth = (byte)((bat>>20)&0xf);
1761 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001762 int bat2 = src.readInt();
1763 batteryTemperature = (short)(bat2&0xffff);
1764 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001765 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001766 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001767 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001768 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001769 wakelockTag = localWakelockTag;
1770 wakelockTag.readFromParcel(src);
1771 } else {
1772 wakelockTag = null;
1773 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001774 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001775 wakeReasonTag = localWakeReasonTag;
1776 wakeReasonTag.readFromParcel(src);
1777 } else {
1778 wakeReasonTag = null;
1779 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001780 if ((bat&0x40000000) != 0) {
1781 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001782 eventTag = localEventTag;
1783 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001784 } else {
1785 eventCode = EVENT_NONE;
1786 eventTag = null;
1787 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001788 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001789 currentTime = src.readLong();
1790 } else {
1791 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001792 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001793 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001794 }
1795
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001796 public void clear() {
1797 time = 0;
1798 cmd = CMD_NULL;
1799 batteryLevel = 0;
1800 batteryStatus = 0;
1801 batteryHealth = 0;
1802 batteryPlugType = 0;
1803 batteryTemperature = 0;
1804 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001805 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001806 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001807 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001808 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001809 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001810 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001811 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001812 }
Bookatzc8c44962017-05-11 12:12:54 -07001813
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001814 public void setTo(HistoryItem o) {
1815 time = o.time;
1816 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001817 setToCommon(o);
1818 }
1819
1820 public void setTo(long time, byte cmd, HistoryItem o) {
1821 this.time = time;
1822 this.cmd = cmd;
1823 setToCommon(o);
1824 }
1825
1826 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001827 batteryLevel = o.batteryLevel;
1828 batteryStatus = o.batteryStatus;
1829 batteryHealth = o.batteryHealth;
1830 batteryPlugType = o.batteryPlugType;
1831 batteryTemperature = o.batteryTemperature;
1832 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001833 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001834 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001835 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001836 if (o.wakelockTag != null) {
1837 wakelockTag = localWakelockTag;
1838 wakelockTag.setTo(o.wakelockTag);
1839 } else {
1840 wakelockTag = null;
1841 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001842 if (o.wakeReasonTag != null) {
1843 wakeReasonTag = localWakeReasonTag;
1844 wakeReasonTag.setTo(o.wakeReasonTag);
1845 } else {
1846 wakeReasonTag = null;
1847 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001848 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001849 if (o.eventTag != null) {
1850 eventTag = localEventTag;
1851 eventTag.setTo(o.eventTag);
1852 } else {
1853 eventTag = null;
1854 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001855 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001856 }
1857
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001858 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001859 return batteryLevel == o.batteryLevel
1860 && batteryStatus == o.batteryStatus
1861 && batteryHealth == o.batteryHealth
1862 && batteryPlugType == o.batteryPlugType
1863 && batteryTemperature == o.batteryTemperature
1864 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001865 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001866 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001867 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001868 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001869 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001870
1871 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001872 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001873 return false;
1874 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001875 if (wakelockTag != o.wakelockTag) {
1876 if (wakelockTag == null || o.wakelockTag == null) {
1877 return false;
1878 }
1879 if (!wakelockTag.equals(o.wakelockTag)) {
1880 return false;
1881 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001882 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001883 if (wakeReasonTag != o.wakeReasonTag) {
1884 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1885 return false;
1886 }
1887 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1888 return false;
1889 }
1890 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001891 if (eventTag != o.eventTag) {
1892 if (eventTag == null || o.eventTag == null) {
1893 return false;
1894 }
1895 if (!eventTag.equals(o.eventTag)) {
1896 return false;
1897 }
1898 }
1899 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001900 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001901 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001902
1903 public final static class HistoryEventTracker {
1904 private final HashMap<String, SparseIntArray>[] mActiveEvents
1905 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1906
1907 public boolean updateState(int code, String name, int uid, int poolIdx) {
1908 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1909 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1910 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1911 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001912 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001913 mActiveEvents[idx] = active;
1914 }
1915 SparseIntArray uids = active.get(name);
1916 if (uids == null) {
1917 uids = new SparseIntArray();
1918 active.put(name, uids);
1919 }
1920 if (uids.indexOfKey(uid) >= 0) {
1921 // Already set, nothing to do!
1922 return false;
1923 }
1924 uids.put(uid, poolIdx);
1925 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1926 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1927 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1928 if (active == null) {
1929 // not currently active, nothing to do.
1930 return false;
1931 }
1932 SparseIntArray uids = active.get(name);
1933 if (uids == null) {
1934 // not currently active, nothing to do.
1935 return false;
1936 }
1937 idx = uids.indexOfKey(uid);
1938 if (idx < 0) {
1939 // not currently active, nothing to do.
1940 return false;
1941 }
1942 uids.removeAt(idx);
1943 if (uids.size() <= 0) {
1944 active.remove(name);
1945 }
1946 }
1947 return true;
1948 }
1949
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001950 public void removeEvents(int code) {
1951 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1952 mActiveEvents[idx] = null;
1953 }
1954
Dianne Hackborn37de0982014-05-09 09:32:18 -07001955 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1956 return mActiveEvents[code];
1957 }
1958 }
1959
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001960 public static final class BitDescription {
1961 public final int mask;
1962 public final int shift;
1963 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001964 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001965 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001966 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07001967
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001968 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001969 this.mask = mask;
1970 this.shift = -1;
1971 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001972 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001973 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001974 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001975 }
Bookatzc8c44962017-05-11 12:12:54 -07001976
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001977 public BitDescription(int mask, int shift, String name, String shortName,
1978 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001979 this.mask = mask;
1980 this.shift = shift;
1981 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001982 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001983 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001984 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001985 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001986 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001987
Dianne Hackbornfc064132014-06-02 12:42:12 -07001988 /**
1989 * Don't allow any more batching in to the current history event. This
1990 * is called when printing partial histories, so to ensure that the next
1991 * history event will go in to a new batch after what was printed in the
1992 * last partial history.
1993 */
1994 public abstract void commitCurrentHistoryBatchLocked();
1995
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001996 public abstract int getHistoryTotalSize();
1997
1998 public abstract int getHistoryUsedSize();
1999
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002000 public abstract boolean startIteratingHistoryLocked();
2001
Dianne Hackborn099bc622014-01-22 13:39:16 -08002002 public abstract int getHistoryStringPoolSize();
2003
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002004 public abstract int getHistoryStringPoolBytes();
2005
2006 public abstract String getHistoryTagPoolString(int index);
2007
2008 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002009
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002010 public abstract boolean getNextHistoryLocked(HistoryItem out);
2011
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002012 public abstract void finishIteratingHistoryLocked();
2013
2014 public abstract boolean startIteratingOldHistoryLocked();
2015
2016 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
2017
2018 public abstract void finishIteratingOldHistoryLocked();
2019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002021 * Return the base time offset for the battery history.
2022 */
2023 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07002024
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002025 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 * Returns the number of times the device has been started.
2027 */
2028 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07002029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002031 * 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 -08002032 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002033 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 * {@hide}
2035 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002036 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002037
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002038 /**
2039 * Returns the number of times the screen was turned on.
2040 *
2041 * {@hide}
2042 */
2043 public abstract int getScreenOnCount(int which);
2044
Mike Mac2f518a2017-09-19 16:06:03 -07002045 /**
2046 * Returns the time in microseconds that the screen has been dozing while the device was
2047 * running on battery.
2048 *
2049 * {@hide}
2050 */
2051 public abstract long getScreenDozeTime(long elapsedRealtimeUs, int which);
2052
2053 /**
2054 * Returns the number of times the screen was turned dozing.
2055 *
2056 * {@hide}
2057 */
2058 public abstract int getScreenDozeCount(int which);
2059
Jeff Browne95c3cd2014-05-02 16:59:26 -07002060 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
2061
Dianne Hackborn617f8772009-03-31 15:04:46 -07002062 public static final int SCREEN_BRIGHTNESS_DARK = 0;
2063 public static final int SCREEN_BRIGHTNESS_DIM = 1;
2064 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
2065 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
2066 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07002067
Dianne Hackborn617f8772009-03-31 15:04:46 -07002068 static final String[] SCREEN_BRIGHTNESS_NAMES = {
2069 "dark", "dim", "medium", "light", "bright"
2070 };
Bookatzc8c44962017-05-11 12:12:54 -07002071
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002072 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
2073 "0", "1", "2", "3", "4"
2074 };
2075
Dianne Hackborn617f8772009-03-31 15:04:46 -07002076 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07002077
Dianne Hackborn617f8772009-03-31 15:04:46 -07002078 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002079 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07002080 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07002081 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002082 * {@hide}
2083 */
2084 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002085 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002088 * Returns the {@link Timer} object that tracks the given screen brightness.
2089 *
2090 * {@hide}
2091 */
2092 public abstract Timer getScreenBrightnessTimer(int brightnessBin);
2093
2094 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002095 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002096 * running on battery.
2097 *
2098 * {@hide}
2099 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002100 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002101
2102 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002103 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002104 *
2105 * {@hide}
2106 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002107 public abstract int getPowerSaveModeEnabledCount(int which);
2108
2109 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002110 * Constant for device idle mode: not active.
2111 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002112 public static final int DEVICE_IDLE_MODE_OFF = ServerProtoEnums.DEVICE_IDLE_MODE_OFF; // 0
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002113
2114 /**
2115 * Constant for device idle mode: active in lightweight mode.
2116 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002117 public static final int DEVICE_IDLE_MODE_LIGHT = ServerProtoEnums.DEVICE_IDLE_MODE_LIGHT; // 1
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002118
2119 /**
2120 * Constant for device idle mode: active in full mode.
2121 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002122 public static final int DEVICE_IDLE_MODE_DEEP = ServerProtoEnums.DEVICE_IDLE_MODE_DEEP; // 2
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002123
2124 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002125 * Returns the time in microseconds that device has been in idle mode while
2126 * running on battery.
2127 *
2128 * {@hide}
2129 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002130 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002131
2132 /**
2133 * Returns the number of times that the devie has gone in to idle mode.
2134 *
2135 * {@hide}
2136 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002137 public abstract int getDeviceIdleModeCount(int mode, int which);
2138
2139 /**
2140 * Return the longest duration we spent in a particular device idle mode (fully in the
2141 * mode, not in idle maintenance etc).
2142 */
2143 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002144
2145 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002146 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002147 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002148 * counts all of the time that we consider the device to be idle, whether or not
2149 * it is currently in the actual device idle mode.
2150 *
2151 * {@hide}
2152 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002153 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002154
2155 /**
Bookatz8c6571b2017-10-24 15:04:41 -07002156 * Returns the number of times that the device has started idling.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002157 *
2158 * {@hide}
2159 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002160 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002161
2162 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002163 * Returns the number of times that connectivity state changed.
2164 *
2165 * {@hide}
2166 */
2167 public abstract int getNumConnectivityChange(int which);
2168
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002169
2170 /**
2171 * Returns the time in microseconds that the phone has been running with
2172 * the given GPS signal quality level
2173 *
2174 * {@hide}
2175 */
2176 public abstract long getGpsSignalQualityTime(int strengthBin,
2177 long elapsedRealtimeUs, int which);
2178
2179 /**
2180 * Returns the GPS battery drain in mA-ms
2181 *
2182 * {@hide}
2183 */
2184 public abstract long getGpsBatteryDrainMaMs();
2185
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002186 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002187 * 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 -08002188 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002189 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 * {@hide}
2191 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002192 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002193
Dianne Hackborn627bba72009-03-24 22:32:56 -07002194 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002195 * Returns the number of times a phone call was activated.
2196 *
2197 * {@hide}
2198 */
2199 public abstract int getPhoneOnCount(int which);
2200
2201 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002202 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002203 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002204 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002205 * {@hide}
2206 */
2207 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002208 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002209
Dianne Hackborn617f8772009-03-31 15:04:46 -07002210 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07002211 * Returns the time in microseconds that the phone has been trying to
2212 * acquire a signal.
2213 *
2214 * {@hide}
2215 */
2216 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002217 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002218
2219 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002220 * Returns the {@link Timer} object that tracks how much the phone has been trying to
2221 * acquire a signal.
2222 *
2223 * {@hide}
2224 */
2225 public abstract Timer getPhoneSignalScanningTimer();
2226
2227 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002228 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002229 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002230 * {@hide}
2231 */
2232 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
2233
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002234 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002235 * Return the {@link Timer} object used to track the given signal strength's duration and
2236 * counts.
2237 */
2238 protected abstract Timer getPhoneSignalStrengthTimer(int strengthBin);
2239
2240 /**
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002241 * Returns the time in microseconds that the mobile network has been active
2242 * (in a high power state).
2243 *
2244 * {@hide}
2245 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002246 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002247
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002248 /**
2249 * Returns the number of times that the mobile network has transitioned to the
2250 * active state.
2251 *
2252 * {@hide}
2253 */
2254 public abstract int getMobileRadioActiveCount(int which);
2255
2256 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002257 * Returns the time in microseconds that is the difference between the mobile radio
2258 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
2259 * from the radio.
2260 *
2261 * {@hide}
2262 */
2263 public abstract long getMobileRadioActiveAdjustedTime(int which);
2264
2265 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002266 * Returns the time in microseconds that the mobile network has been active
2267 * (in a high power state) but not being able to blame on an app.
2268 *
2269 * {@hide}
2270 */
2271 public abstract long getMobileRadioActiveUnknownTime(int which);
2272
2273 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002274 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002275 *
2276 * {@hide}
2277 */
2278 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002279
Tej Singheee317b2018-03-07 19:28:05 -08002280 public static final int DATA_CONNECTION_NONE = 0;
2281 public static final int DATA_CONNECTION_OTHER = TelephonyManager.MAX_NETWORK_TYPE + 1;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002282
Dianne Hackborn627bba72009-03-24 22:32:56 -07002283 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002284 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002285 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Siddharth Rayc72081d2018-02-13 11:31:54 -08002286 "ehrpd", "hspap", "gsm", "td_scdma", "iwlan", "lte_ca", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002287 };
Bookatzc8c44962017-05-11 12:12:54 -07002288
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002289 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002290
Dianne Hackborn627bba72009-03-24 22:32:56 -07002291 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002292 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002293 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002294 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002295 * {@hide}
2296 */
2297 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002298 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002300 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002301 * Returns the number of times the phone has entered the given data
2302 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002303 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002304 * {@hide}
2305 */
2306 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002307
Kweku Adams87b19ec2017-10-09 12:40:03 -07002308 /**
2309 * Returns the {@link Timer} object that tracks the phone's data connection type stats.
2310 */
2311 public abstract Timer getPhoneDataConnectionTimer(int dataType);
2312
Dianne Hackborn3251b902014-06-20 14:40:53 -07002313 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2314 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2315 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2316 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2317 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2318 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2319 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2320 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2321 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2322 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2323 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2324 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2325 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2326
2327 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2328
2329 static final String[] WIFI_SUPPL_STATE_NAMES = {
2330 "invalid", "disconn", "disabled", "inactive", "scanning",
2331 "authenticating", "associating", "associated", "4-way-handshake",
2332 "group-handshake", "completed", "dormant", "uninit"
2333 };
2334
2335 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2336 "inv", "dsc", "dis", "inact", "scan",
2337 "auth", "ascing", "asced", "4-way",
2338 "group", "compl", "dorm", "uninit"
2339 };
2340
Mike Mac2f518a2017-09-19 16:06:03 -07002341 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002342 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002343 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2344 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002345 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002346 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2347 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2348 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002349 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002350 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002351 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002352 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002353 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2354 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Mike Mac2f518a2017-09-19 16:06:03 -07002355 new BitDescription(HistoryItem.STATE_SCREEN_DOZE_FLAG, "screen_doze", "Sd"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002356 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2357 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2358 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2359 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2360 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2361 new String[] {"in", "out", "emergency", "off"},
2362 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002363 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2364 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2365 SignalStrength.SIGNAL_STRENGTH_NAMES,
2366 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002367 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2368 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2369 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002370 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002371
Mike Ma926a97c2018-03-25 02:32:35 -07002372 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002373 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002374 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002375 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002376 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002377 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002378 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2379 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2380 new String[] { "off", "light", "full", "???" },
2381 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002382 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
Mike Ma926a97c2018-03-25 02:32:35 -07002383 new BitDescription(HistoryItem.STATE2_USB_DATA_LINK_FLAG, "usb_data", "Ud"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002384 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2385 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002386 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2387 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2388 new String[] { "0", "1", "2", "3", "4" },
2389 new String[] { "0", "1", "2", "3", "4" }),
2390 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2391 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2392 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002393 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002394 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Siddharth Rayf5e796a2018-01-22 18:18:17 -08002395 new BitDescription(HistoryItem.STATE2_CELLULAR_HIGH_TX_POWER_FLAG,
2396 "cellular_high_tx_power", "Chtp"),
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002397 new BitDescription(HistoryItem.STATE2_GPS_SIGNAL_QUALITY_MASK,
2398 HistoryItem.STATE2_GPS_SIGNAL_QUALITY_SHIFT, "gps_signal_quality", "Gss",
Siddharth Rayf5e796a2018-01-22 18:18:17 -08002399 new String[] { "poor", "good"}, new String[] { "poor", "good"})
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002400 };
2401
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002402 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002403 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002404 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2405 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002406 };
2407
2408 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002409 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002410 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002411 "Esw", "Ewa", "Elw", "Eec"
2412 };
2413
2414 @FunctionalInterface
2415 public interface IntToString {
2416 String applyAsString(int val);
2417 }
2418
2419 private static final IntToString sUidToString = UserHandle::formatUid;
2420 private static final IntToString sIntToString = Integer::toString;
2421
2422 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2423 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2424 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2425 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2426 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002427 };
2428
Dianne Hackborn617f8772009-03-31 15:04:46 -07002429 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08002430 * Returns total time for WiFi Multicast Wakelock timer.
2431 * Note that this may be different from the sum of per uid timer values.
2432 *
2433 * {@hide}
2434 */
2435 public abstract long getWifiMulticastWakelockTime(long elapsedRealtimeUs, int which);
2436
2437 /**
2438 * Returns total time for WiFi Multicast Wakelock timer
2439 * Note that this may be different from the sum of per uid timer values.
2440 *
2441 * {@hide}
2442 */
2443 public abstract int getWifiMulticastWakelockCount(int which);
2444
2445 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002446 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002447 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002448 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002449 * {@hide}
2450 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002451 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002452
2453 /**
Siddharth Rayb50a6842017-12-14 15:15:28 -08002454 * Returns the time in microseconds that wifi has been active while the device was
2455 * running on battery.
2456 *
2457 * {@hide}
2458 */
2459 public abstract long getWifiActiveTime(long elapsedRealtimeUs, int which);
2460
2461 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002462 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002463 * been in the running state while the device was running on battery.
2464 *
2465 * {@hide}
2466 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002467 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002468
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002469 public static final int WIFI_STATE_OFF = 0;
2470 public static final int WIFI_STATE_OFF_SCANNING = 1;
2471 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2472 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2473 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2474 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2475 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2476 public static final int WIFI_STATE_SOFT_AP = 7;
2477
2478 static final String[] WIFI_STATE_NAMES = {
2479 "off", "scanning", "no_net", "disconn",
2480 "sta", "p2p", "sta_p2p", "soft_ap"
2481 };
2482
2483 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2484
2485 /**
2486 * Returns the time in microseconds that WiFi has been running in the given state.
2487 *
2488 * {@hide}
2489 */
2490 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002491 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002492
2493 /**
2494 * Returns the number of times that WiFi has entered the given state.
2495 *
2496 * {@hide}
2497 */
2498 public abstract int getWifiStateCount(int wifiState, int which);
2499
The Android Open Source Project10592532009-03-18 17:39:46 -07002500 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002501 * Returns the {@link Timer} object that tracks the given WiFi state.
2502 *
2503 * {@hide}
2504 */
2505 public abstract Timer getWifiStateTimer(int wifiState);
2506
2507 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002508 * Returns the time in microseconds that the wifi supplicant has been
2509 * in a given state.
2510 *
2511 * {@hide}
2512 */
2513 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2514
2515 /**
2516 * Returns the number of times that the wifi supplicant has transitioned
2517 * to a given state.
2518 *
2519 * {@hide}
2520 */
2521 public abstract int getWifiSupplStateCount(int state, int which);
2522
Kweku Adams87b19ec2017-10-09 12:40:03 -07002523 /**
2524 * Returns the {@link Timer} object that tracks the given wifi supplicant state.
2525 *
2526 * {@hide}
2527 */
2528 public abstract Timer getWifiSupplStateTimer(int state);
2529
Dianne Hackborn3251b902014-06-20 14:40:53 -07002530 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2531
2532 /**
2533 * Returns the time in microseconds that WIFI has been running with
2534 * the given signal strength.
2535 *
2536 * {@hide}
2537 */
2538 public abstract long getWifiSignalStrengthTime(int strengthBin,
2539 long elapsedRealtimeUs, int which);
2540
2541 /**
2542 * Returns the number of times WIFI has entered the given signal strength.
2543 *
2544 * {@hide}
2545 */
2546 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2547
2548 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002549 * Returns the {@link Timer} object that tracks the given WIFI signal strength.
2550 *
2551 * {@hide}
2552 */
2553 public abstract Timer getWifiSignalStrengthTimer(int strengthBin);
2554
2555 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002556 * Returns the time in microseconds that the flashlight has been on while the device was
2557 * running on battery.
2558 *
2559 * {@hide}
2560 */
2561 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2562
2563 /**
2564 * Returns the number of times that the flashlight has been turned on while the device was
2565 * running on battery.
2566 *
2567 * {@hide}
2568 */
2569 public abstract long getFlashlightOnCount(int which);
2570
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002571 /**
2572 * Returns the time in microseconds that the camera has been on while the device was
2573 * running on battery.
2574 *
2575 * {@hide}
2576 */
2577 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2578
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002579 /**
2580 * Returns the time in microseconds that bluetooth scans were running while the device was
2581 * on battery.
2582 *
2583 * {@hide}
2584 */
2585 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002586
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002587 public static final int NETWORK_MOBILE_RX_DATA = 0;
2588 public static final int NETWORK_MOBILE_TX_DATA = 1;
2589 public static final int NETWORK_WIFI_RX_DATA = 2;
2590 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002591 public static final int NETWORK_BT_RX_DATA = 4;
2592 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002593 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2594 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2595 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2596 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2597 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002598
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002599 public abstract long getNetworkActivityBytes(int type, int which);
2600 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002601
Adam Lesinskie08af192015-03-25 16:42:59 -07002602 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002603 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002604 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002605 * actual power data.
2606 */
2607 public abstract boolean hasWifiActivityReporting();
2608
2609 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002610 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2611 * in various radio controller states, such as transmit, receive, and idle.
2612 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002613 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002614 public abstract ControllerActivityCounter getWifiControllerActivity();
2615
2616 /**
2617 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2618 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2619 * actual power data.
2620 */
2621 public abstract boolean hasBluetoothActivityReporting();
2622
2623 /**
2624 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2625 * in various radio controller states, such as transmit, receive, and idle.
2626 * @return non-null {@link ControllerActivityCounter}
2627 */
2628 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2629
2630 /**
2631 * Returns true if the BatteryStats object has detailed modem power reports.
2632 * When true, calling {@link #getModemControllerActivity()} will yield the
2633 * actual power data.
2634 */
2635 public abstract boolean hasModemActivityReporting();
2636
2637 /**
2638 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2639 * in various radio controller states, such as transmit, receive, and idle.
2640 * @return non-null {@link ControllerActivityCounter}
2641 */
2642 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002643
The Android Open Source Project10592532009-03-18 17:39:46 -07002644 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002645 * Return the wall clock time when battery stats data collection started.
2646 */
2647 public abstract long getStartClockTime();
2648
2649 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002650 * Return platform version tag that we were running in when the battery stats started.
2651 */
2652 public abstract String getStartPlatformVersion();
2653
2654 /**
2655 * Return platform version tag that we were running in when the battery stats ended.
2656 */
2657 public abstract String getEndPlatformVersion();
2658
2659 /**
2660 * Return the internal version code of the parcelled format.
2661 */
2662 public abstract int getParcelVersion();
2663
2664 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 * Return whether we are currently running on battery.
2666 */
2667 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 /**
2670 * Returns a SparseArray containing the statistics for each uid.
2671 */
2672 public abstract SparseArray<? extends Uid> getUidStats();
2673
2674 /**
2675 * Returns the current battery uptime in microseconds.
2676 *
2677 * @param curTime the amount of elapsed realtime in microseconds.
2678 */
2679 public abstract long getBatteryUptime(long curTime);
2680
2681 /**
2682 * Returns the current battery realtime in microseconds.
2683 *
2684 * @param curTime the amount of elapsed realtime in microseconds.
2685 */
2686 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002687
The Android Open Source Project10592532009-03-18 17:39:46 -07002688 /**
Evan Millar633a1742009-04-02 16:36:33 -07002689 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002690 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002691 */
Evan Millar633a1742009-04-02 16:36:33 -07002692 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002693
The Android Open Source Project10592532009-03-18 17:39:46 -07002694 /**
Evan Millar633a1742009-04-02 16:36:33 -07002695 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2696 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002697 */
Evan Millar633a1742009-04-02 16:36:33 -07002698 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699
2700 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002701 * Get the amount the battery has discharged since the stats were
2702 * last reset after charging, as a lower-end approximation.
2703 */
2704 public abstract int getLowDischargeAmountSinceCharge();
2705
2706 /**
2707 * Get the amount the battery has discharged since the stats were
2708 * last reset after charging, as an upper-end approximation.
2709 */
2710 public abstract int getHighDischargeAmountSinceCharge();
2711
2712 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002713 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2714 */
2715 public abstract int getDischargeAmount(int which);
2716
2717 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002718 * Get the amount the battery has discharged while the screen was on,
2719 * since the last time power was unplugged.
2720 */
2721 public abstract int getDischargeAmountScreenOn();
2722
2723 /**
2724 * Get the amount the battery has discharged while the screen was on,
2725 * since the last time the device was charged.
2726 */
2727 public abstract int getDischargeAmountScreenOnSinceCharge();
2728
2729 /**
2730 * Get the amount the battery has discharged while the screen was off,
2731 * since the last time power was unplugged.
2732 */
2733 public abstract int getDischargeAmountScreenOff();
2734
2735 /**
2736 * Get the amount the battery has discharged while the screen was off,
2737 * since the last time the device was charged.
2738 */
2739 public abstract int getDischargeAmountScreenOffSinceCharge();
2740
2741 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002742 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002743 * since the last time power was unplugged.
2744 */
2745 public abstract int getDischargeAmountScreenDoze();
2746
2747 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002748 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002749 * since the last time the device was charged.
2750 */
2751 public abstract int getDischargeAmountScreenDozeSinceCharge();
2752
2753 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 * Returns the total, last, or current battery uptime in microseconds.
2755 *
2756 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002757 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 */
2759 public abstract long computeBatteryUptime(long curTime, int which);
2760
2761 /**
2762 * Returns the total, last, or current battery realtime in microseconds.
2763 *
2764 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002765 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 */
2767 public abstract long computeBatteryRealtime(long curTime, int which);
2768
2769 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002770 * Returns the total, last, or current battery screen off/doze uptime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002771 *
2772 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002773 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002774 */
2775 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2776
2777 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002778 * Returns the total, last, or current battery screen off/doze realtime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002779 *
2780 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002781 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002782 */
2783 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2784
2785 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 * Returns the total, last, or current uptime in microseconds.
2787 *
2788 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002789 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 */
2791 public abstract long computeUptime(long curTime, int which);
2792
2793 /**
2794 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002795 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002797 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 */
2799 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002800
2801 /**
2802 * Compute an approximation for how much run time (in microseconds) is remaining on
2803 * the battery. Returns -1 if no time can be computed: either there is not
2804 * enough current data to make a decision, or the battery is currently
2805 * charging.
2806 *
2807 * @param curTime The current elepsed realtime in microseconds.
2808 */
2809 public abstract long computeBatteryTimeRemaining(long curTime);
2810
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002811 // The part of a step duration that is the actual time.
2812 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2813
2814 // Bits in a step duration that are the new battery level we are at.
2815 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002816 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002817
2818 // Bits in a step duration that are the initial mode we were in at that step.
2819 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002820 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002821
2822 // Bits in a step duration that indicate which modes changed during that step.
2823 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002824 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002825
2826 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2827 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2828
Santos Cordone94f0502017-02-24 12:31:20 -08002829 // The largest value for screen state that is tracked in battery states. Any values above
2830 // this should be mapped back to one of the tracked values before being tracked here.
2831 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2832
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002833 // Step duration mode: power save is on.
2834 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2835
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002836 // Step duration mode: device is currently in idle mode.
2837 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2838
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002839 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2840 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002841 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2842 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002843 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2844 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2845 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2846 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2847 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002848 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2849 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002850 };
2851 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2852 (Display.STATE_OFF-1),
2853 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002854 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002855 (Display.STATE_ON-1),
2856 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2857 (Display.STATE_DOZE-1),
2858 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2859 (Display.STATE_DOZE_SUSPEND-1),
2860 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002861 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002862 };
2863 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2864 "screen off",
2865 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002866 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002867 "screen on",
2868 "screen on power save",
2869 "screen doze",
2870 "screen doze power save",
2871 "screen doze-suspend",
2872 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002873 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002874 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002875
2876 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002877 * Return the amount of battery discharge while the screen was off, measured in
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002878 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2879 * a coulomb counter.
2880 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002881 public abstract long getUahDischargeScreenOff(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002882
2883 /**
2884 * Return the amount of battery discharge while the screen was in doze mode, measured in
2885 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2886 * a coulomb counter.
2887 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002888 public abstract long getUahDischargeScreenDoze(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002889
2890 /**
2891 * Return the amount of battery discharge measured in micro-Ampere-hours. This will be
2892 * non-zero only if the device's battery has a coulomb counter.
2893 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002894 public abstract long getUahDischarge(int which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002895
2896 /**
Mike Ma15313c92017-11-15 17:58:21 -08002897 * @return the amount of battery discharge while the device is in light idle mode, measured in
2898 * micro-Ampere-hours.
2899 */
2900 public abstract long getUahDischargeLightDoze(int which);
2901
2902 /**
2903 * @return the amount of battery discharge while the device is in deep idle mode, measured in
2904 * micro-Ampere-hours.
2905 */
2906 public abstract long getUahDischargeDeepDoze(int which);
2907
2908 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002909 * Returns the estimated real battery capacity, which may be less than the capacity
2910 * declared by the PowerProfile.
2911 * @return The estimated battery capacity in mAh.
2912 */
2913 public abstract int getEstimatedBatteryCapacity();
2914
2915 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002916 * @return The minimum learned battery capacity in uAh.
2917 */
2918 public abstract int getMinLearnedBatteryCapacity();
2919
2920 /**
2921 * @return The maximum learned battery capacity in uAh.
2922 */
2923 public abstract int getMaxLearnedBatteryCapacity() ;
2924
2925 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002926 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002927 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002928 public abstract LevelStepTracker getDischargeLevelStepTracker();
2929
2930 /**
2931 * Return the array of daily discharge step durations.
2932 */
2933 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002934
2935 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002936 * Compute an approximation for how much time (in microseconds) remains until the battery
2937 * is fully charged. Returns -1 if no time can be computed: either there is not
2938 * enough current data to make a decision, or the battery is currently
2939 * discharging.
2940 *
2941 * @param curTime The current elepsed realtime in microseconds.
2942 */
2943 public abstract long computeChargeTimeRemaining(long curTime);
2944
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002945 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002946 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002947 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002948 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002949
2950 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002951 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002952 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002953 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002954
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002955 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2956
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002957 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002958
Evan Millarc64edde2009-04-18 12:26:32 -07002959 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960
Bookatz50df7112017-08-04 14:53:26 -07002961 /**
2962 * Returns Timers tracking the total time of each Resource Power Manager state and voter.
2963 */
2964 public abstract Map<String, ? extends Timer> getRpmStats();
2965 /**
2966 * Returns Timers tracking the screen-off time of each Resource Power Manager state and voter.
2967 */
2968 public abstract Map<String, ? extends Timer> getScreenOffRpmStats();
2969
2970
James Carr2dd7e5e2016-07-20 18:48:39 -07002971 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2972
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002973 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2974
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002975 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 long days = seconds / (60 * 60 * 24);
2977 if (days != 0) {
2978 out.append(days);
2979 out.append("d ");
2980 }
2981 long used = days * 60 * 60 * 24;
2982
2983 long hours = (seconds - used) / (60 * 60);
2984 if (hours != 0 || used != 0) {
2985 out.append(hours);
2986 out.append("h ");
2987 }
2988 used += hours * 60 * 60;
2989
2990 long mins = (seconds-used) / 60;
2991 if (mins != 0 || used != 0) {
2992 out.append(mins);
2993 out.append("m ");
2994 }
2995 used += mins * 60;
2996
2997 if (seconds != 0 || used != 0) {
2998 out.append(seconds-used);
2999 out.append("s ");
3000 }
3001 }
3002
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003003 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003005 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006 sb.append(time - (sec * 1000));
3007 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008 }
3009
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003010 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003011 long sec = time / 1000;
3012 formatTimeRaw(sb, sec);
3013 sb.append(time - (sec * 1000));
3014 sb.append("ms");
3015 }
3016
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003017 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003019 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 }
3021 float perc = ((float)num) / ((float)den) * 100;
3022 mFormatBuilder.setLength(0);
3023 mFormatter.format("%.1f%%", perc);
3024 return mFormatBuilder.toString();
3025 }
3026
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003027 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07003028 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07003029
Evan Millar22ac0432009-03-31 11:33:18 -07003030 if (bytes < BYTES_PER_KB) {
3031 return bytes + "B";
3032 } else if (bytes < BYTES_PER_MB) {
3033 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
3034 return mFormatBuilder.toString();
3035 } else if (bytes < BYTES_PER_GB){
3036 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
3037 return mFormatBuilder.toString();
3038 } else {
3039 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
3040 return mFormatBuilder.toString();
3041 }
3042 }
3043
Kweku Adams103351f2017-10-16 14:39:34 -07003044 private static long roundUsToMs(long timeUs) {
3045 return (timeUs + 500) / 1000;
3046 }
3047
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003048 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003049 if (timer != null) {
3050 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003051 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003052 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
3053 return totalTimeMillis;
3054 }
3055 return 0;
3056 }
3057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 /**
3059 *
3060 * @param sb a StringBuilder object.
3061 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003062 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003064 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 * @param linePrefix a String to be prepended to each line of output.
3066 * @return the line prefix
3067 */
3068 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003069 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003072 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003073
Evan Millarc64edde2009-04-18 12:26:32 -07003074 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075 if (totalTimeMillis != 0) {
3076 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003077 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08003078 if (name != null) {
3079 sb.append(name);
3080 sb.append(' ');
3081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 sb.append('(');
3083 sb.append(count);
3084 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003085 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
3086 if (maxDurationMs >= 0) {
3087 sb.append(" max=");
3088 sb.append(maxDurationMs);
3089 }
Bookatz506a8182017-05-01 14:18:42 -07003090 // Put actual time if it is available and different from totalTimeMillis.
3091 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
3092 if (totalDurMs > totalTimeMillis) {
3093 sb.append(" actual=");
3094 sb.append(totalDurMs);
3095 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07003096 if (timer.isRunningLocked()) {
3097 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3098 if (currentMs >= 0) {
3099 sb.append(" (running for ");
3100 sb.append(currentMs);
3101 sb.append("ms)");
3102 } else {
3103 sb.append(" (running)");
3104 }
3105 }
3106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 return ", ";
3108 }
3109 }
3110 return linePrefix;
3111 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003112
3113 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07003114 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003115 *
3116 * @param pw a PrintWriter object to print to.
3117 * @param sb a StringBuilder object.
3118 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08003119 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003120 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
3121 * @param prefix a String to be prepended to each line of output.
3122 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07003123 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003124 */
3125 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07003126 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003127 if (timer != null) {
3128 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07003129 final long totalTimeMs = (timer.getTotalTimeLocked(
3130 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003131 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003132 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003133 sb.setLength(0);
3134 sb.append(prefix);
3135 sb.append(" ");
3136 sb.append(type);
3137 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003138 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003139 sb.append("realtime (");
3140 sb.append(count);
3141 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003142 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
3143 if (maxDurationMs >= 0) {
3144 sb.append(" max=");
3145 sb.append(maxDurationMs);
3146 }
3147 if (timer.isRunningLocked()) {
3148 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
3149 if (currentMs >= 0) {
3150 sb.append(" (running for ");
3151 sb.append(currentMs);
3152 sb.append("ms)");
3153 } else {
3154 sb.append(" (running)");
3155 }
3156 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003157 pw.println(sb.toString());
3158 return true;
3159 }
3160 }
3161 return false;
3162 }
Bookatzc8c44962017-05-11 12:12:54 -07003163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003164 /**
3165 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07003166 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 * @param sb a StringBuilder object.
3168 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003169 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003171 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 * @param linePrefix a String to be prepended to each line of output.
3173 * @return the line prefix
3174 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003175 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
3176 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 long totalTimeMicros = 0;
3178 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07003179 long max = 0;
3180 long current = 0;
3181 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003183 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07003184 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003185 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3186 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07003187 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 }
3189 sb.append(linePrefix);
3190 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
3191 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07003192 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003194 sb.append(',');
3195 sb.append(current);
3196 sb.append(',');
3197 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07003198 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
3199 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
3200 // totalDuration independent of totalTimeMicros (since they are not pooled).
3201 if (name != null) {
3202 sb.append(',');
3203 sb.append(totalDuration);
3204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 return ",";
3206 }
Bookatz506a8182017-05-01 14:18:42 -07003207
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003208 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
3209 String type) {
3210 pw.print(BATTERY_STATS_CHECKIN_VERSION);
3211 pw.print(',');
3212 pw.print(uid);
3213 pw.print(',');
3214 pw.print(category);
3215 pw.print(',');
3216 pw.print(type);
3217 }
3218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219 /**
3220 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07003221 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 * @param pw the PageWriter to dump log to
3223 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3224 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3225 * @param args type-dependent data arguments
3226 */
Bookatzc8c44962017-05-11 12:12:54 -07003227 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003229 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003230 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003231 pw.print(',');
3232 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003234 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07003236
3237 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003238 * Dump a given timer stat for terse checkin mode.
3239 *
3240 * @param pw the PageWriter to dump log to
3241 * @param uid the UID to log
3242 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3243 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3244 * @param timer a {@link Timer} to dump stats for
3245 * @param rawRealtime the current elapsed realtime of the system in microseconds
3246 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3247 */
3248 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
3249 Timer timer, long rawRealtime, int which) {
3250 if (timer != null) {
3251 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003252 final long totalTime = roundUsToMs(timer.getTotalTimeLocked(rawRealtime, which));
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003253 final int count = timer.getCountLocked(which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003254 if (totalTime != 0 || count != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003255 dumpLine(pw, uid, category, type, totalTime, count);
3256 }
3257 }
3258 }
3259
3260 /**
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003261 * Dump a given timer stat to the proto stream.
3262 *
3263 * @param proto the ProtoOutputStream to log to
3264 * @param fieldId type of data, the field to save to (e.g. AggregatedBatteryStats.WAKELOCK)
3265 * @param timer a {@link Timer} to dump stats for
3266 * @param rawRealtimeUs the current elapsed realtime of the system in microseconds
3267 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3268 */
3269 private static void dumpTimer(ProtoOutputStream proto, long fieldId,
Kweku Adams87b19ec2017-10-09 12:40:03 -07003270 Timer timer, long rawRealtimeUs, int which) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003271 if (timer == null) {
3272 return;
3273 }
3274 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003275 final long timeMs = roundUsToMs(timer.getTotalTimeLocked(rawRealtimeUs, which));
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003276 final int count = timer.getCountLocked(which);
Kweku Adams103351f2017-10-16 14:39:34 -07003277 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs / 1000);
3278 final long curDurationMs = timer.getCurrentDurationMsLocked(rawRealtimeUs / 1000);
3279 final long totalDurationMs = timer.getTotalDurationMsLocked(rawRealtimeUs / 1000);
3280 if (timeMs != 0 || count != 0 || maxDurationMs != -1 || curDurationMs != -1
3281 || totalDurationMs != -1) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003282 final long token = proto.start(fieldId);
Kweku Adams103351f2017-10-16 14:39:34 -07003283 proto.write(TimerProto.DURATION_MS, timeMs);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003284 proto.write(TimerProto.COUNT, count);
Kweku Adams103351f2017-10-16 14:39:34 -07003285 // These values will be -1 for timers that don't implement the functionality.
3286 if (maxDurationMs != -1) {
3287 proto.write(TimerProto.MAX_DURATION_MS, maxDurationMs);
3288 }
3289 if (curDurationMs != -1) {
3290 proto.write(TimerProto.CURRENT_DURATION_MS, curDurationMs);
3291 }
3292 if (totalDurationMs != -1) {
3293 proto.write(TimerProto.TOTAL_DURATION_MS, totalDurationMs);
3294 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003295 proto.end(token);
3296 }
3297 }
3298
3299 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003300 * Checks if the ControllerActivityCounter has any data worth dumping.
3301 */
3302 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
3303 if (counter == null) {
3304 return false;
3305 }
3306
3307 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
3308 || counter.getRxTimeCounter().getCountLocked(which) != 0
3309 || counter.getPowerCounter().getCountLocked(which) != 0) {
3310 return true;
3311 }
3312
3313 for (LongCounter c : counter.getTxTimeCounters()) {
3314 if (c.getCountLocked(which) != 0) {
3315 return true;
3316 }
3317 }
3318 return false;
3319 }
3320
3321 /**
3322 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3323 * The order of the arguments in the final check in line is:
3324 *
3325 * idle, rx, power, tx...
3326 *
3327 * where tx... is one or more transmit level times.
3328 */
3329 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
3330 String type,
3331 ControllerActivityCounter counter,
3332 int which) {
3333 if (!controllerActivityHasData(counter, which)) {
3334 return;
3335 }
3336
3337 dumpLineHeader(pw, uid, category, type);
3338 pw.print(",");
3339 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
3340 pw.print(",");
3341 pw.print(counter.getRxTimeCounter().getCountLocked(which));
3342 pw.print(",");
3343 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3344 for (LongCounter c : counter.getTxTimeCounters()) {
3345 pw.print(",");
3346 pw.print(c.getCountLocked(which));
3347 }
3348 pw.println();
3349 }
3350
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003351 /**
3352 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3353 */
3354 private static void dumpControllerActivityProto(ProtoOutputStream proto, long fieldId,
3355 ControllerActivityCounter counter,
3356 int which) {
3357 if (!controllerActivityHasData(counter, which)) {
3358 return;
3359 }
3360
3361 final long cToken = proto.start(fieldId);
3362
3363 proto.write(ControllerActivityProto.IDLE_DURATION_MS,
3364 counter.getIdleTimeCounter().getCountLocked(which));
3365 proto.write(ControllerActivityProto.RX_DURATION_MS,
3366 counter.getRxTimeCounter().getCountLocked(which));
3367 proto.write(ControllerActivityProto.POWER_MAH,
3368 counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3369
3370 long tToken;
3371 LongCounter[] txCounters = counter.getTxTimeCounters();
3372 for (int i = 0; i < txCounters.length; ++i) {
3373 LongCounter c = txCounters[i];
3374 tToken = proto.start(ControllerActivityProto.TX);
3375 proto.write(ControllerActivityProto.TxLevel.LEVEL, i);
3376 proto.write(ControllerActivityProto.TxLevel.DURATION_MS, c.getCountLocked(which));
3377 proto.end(tToken);
3378 }
3379
3380 proto.end(cToken);
3381 }
3382
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003383 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
3384 String prefix, String controllerName,
3385 ControllerActivityCounter counter,
3386 int which) {
3387 if (controllerActivityHasData(counter, which)) {
3388 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
3389 }
3390 }
3391
3392 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
3393 String controllerName,
3394 ControllerActivityCounter counter, int which) {
3395 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
3396 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
3397 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003398 // Battery real time
3399 final long totalControllerActivityTimeMs
3400 = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003401 long totalTxTimeMs = 0;
3402 for (LongCounter txState : counter.getTxTimeCounters()) {
3403 totalTxTimeMs += txState.getCountLocked(which);
3404 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003405
Siddharth Rayb50a6842017-12-14 15:15:28 -08003406 if (controllerName.equals(WIFI_CONTROLLER_NAME)) {
3407 final long scanTimeMs = counter.getScanTimeCounter().getCountLocked(which);
3408 sb.setLength(0);
3409 sb.append(prefix);
3410 sb.append(" ");
3411 sb.append(controllerName);
3412 sb.append(" Scan time: ");
3413 formatTimeMs(sb, scanTimeMs);
3414 sb.append("(");
3415 sb.append(formatRatioLocked(scanTimeMs, totalControllerActivityTimeMs));
3416 sb.append(")");
3417 pw.println(sb.toString());
Siddharth Rayed754702018-02-15 12:44:37 -08003418
3419 final long sleepTimeMs
3420 = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + totalTxTimeMs);
3421 sb.setLength(0);
3422 sb.append(prefix);
3423 sb.append(" ");
3424 sb.append(controllerName);
3425 sb.append(" Sleep time: ");
3426 formatTimeMs(sb, sleepTimeMs);
3427 sb.append("(");
3428 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3429 sb.append(")");
3430 pw.println(sb.toString());
Siddharth Rayb50a6842017-12-14 15:15:28 -08003431 }
3432
Siddharth Rayed754702018-02-15 12:44:37 -08003433 if (controllerName.equals(CELLULAR_CONTROLLER_NAME)) {
3434 final long sleepTimeMs = counter.getSleepTimeCounter().getCountLocked(which);
3435 sb.setLength(0);
3436 sb.append(prefix);
3437 sb.append(" ");
3438 sb.append(controllerName);
3439 sb.append(" Sleep time: ");
3440 formatTimeMs(sb, sleepTimeMs);
3441 sb.append("(");
3442 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3443 sb.append(")");
3444 pw.println(sb.toString());
3445 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003446
3447 sb.setLength(0);
3448 sb.append(prefix);
3449 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003450 sb.append(controllerName);
3451 sb.append(" Idle time: ");
3452 formatTimeMs(sb, idleTimeMs);
3453 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003454 sb.append(formatRatioLocked(idleTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003455 sb.append(")");
3456 pw.println(sb.toString());
3457
3458 sb.setLength(0);
3459 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003460 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003461 sb.append(controllerName);
3462 sb.append(" Rx time: ");
3463 formatTimeMs(sb, rxTimeMs);
3464 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003465 sb.append(formatRatioLocked(rxTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003466 sb.append(")");
3467 pw.println(sb.toString());
3468
3469 sb.setLength(0);
3470 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003471 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003472 sb.append(controllerName);
3473 sb.append(" Tx time: ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003474
Siddharth Ray3c648c42017-10-02 17:30:58 -07003475 String [] powerLevel;
3476 switch(controllerName) {
Siddharth Rayb50a6842017-12-14 15:15:28 -08003477 case CELLULAR_CONTROLLER_NAME:
Siddharth Ray3c648c42017-10-02 17:30:58 -07003478 powerLevel = new String[] {
3479 " less than 0dBm: ",
3480 " 0dBm to 8dBm: ",
3481 " 8dBm to 15dBm: ",
3482 " 15dBm to 20dBm: ",
3483 " above 20dBm: "};
3484 break;
3485 default:
3486 powerLevel = new String[] {"[0]", "[1]", "[2]", "[3]", "[4]"};
3487 break;
3488 }
3489 final int numTxLvls = Math.min(counter.getTxTimeCounters().length, powerLevel.length);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003490 if (numTxLvls > 1) {
Siddharth Ray3c648c42017-10-02 17:30:58 -07003491 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003492 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3493 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3494 sb.setLength(0);
3495 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003496 sb.append(" ");
3497 sb.append(powerLevel[lvl]);
3498 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003499 formatTimeMs(sb, txLvlTimeMs);
3500 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003501 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003502 sb.append(")");
3503 pw.println(sb.toString());
3504 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003505 } else {
3506 final long txLvlTimeMs = counter.getTxTimeCounters()[0].getCountLocked(which);
3507 formatTimeMs(sb, txLvlTimeMs);
3508 sb.append("(");
3509 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
3510 sb.append(")");
3511 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003512 }
3513
Siddharth Ray3c648c42017-10-02 17:30:58 -07003514 if (powerDrainMaMs > 0) {
3515 sb.setLength(0);
3516 sb.append(prefix);
3517 sb.append(" ");
3518 sb.append(controllerName);
3519 sb.append(" Battery drain: ").append(
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003520 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
Siddharth Ray3c648c42017-10-02 17:30:58 -07003521 sb.append("mAh");
3522 pw.println(sb.toString());
3523 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003524 }
3525
3526 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003527 * Temporary for settings.
3528 */
3529 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3530 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3531 }
3532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003533 /**
3534 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003535 *
Kweku Adams87b19ec2017-10-09 12:40:03 -07003536 * NOTE: all times are expressed in microseconds, unless specified otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003538 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3539 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540 final long rawUptime = SystemClock.uptimeMillis() * 1000;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003541 final long rawRealtimeMs = SystemClock.elapsedRealtime();
3542 final long rawRealtime = rawRealtimeMs * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003543 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3545 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003546 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3547 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3548 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 final long totalRealtime = computeRealtime(rawRealtime, which);
3550 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003551 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Mike Mac2f518a2017-09-19 16:06:03 -07003552 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003553 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003554 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003555 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3556 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003557 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003558 rawRealtime, which);
3559 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3560 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003561 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003562 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003563 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003564 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003565 final long dischargeCount = getUahDischarge(which);
3566 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
3567 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Ma15313c92017-11-15 17:58:21 -08003568 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
3569 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003570
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003571 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003572
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003573 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003574 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003575
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003576 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003578 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003579 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003580 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003581 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003582 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003583 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003584 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003585 getEstimatedBatteryCapacity(),
Mike Mac2f518a2017-09-19 16:06:03 -07003586 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity(),
3587 screenDozeTime / 1000);
Adam Lesinski67c134f2016-06-10 15:15:08 -07003588
Bookatzc8c44962017-05-11 12:12:54 -07003589
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003590 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003591 long fullWakeLockTimeTotal = 0;
3592 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003593
Evan Millar22ac0432009-03-31 11:33:18 -07003594 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003595 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003596
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003597 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3598 = u.getWakelockStats();
3599 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3600 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003601
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003602 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3603 if (fullWakeTimer != null) {
3604 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3605 which);
3606 }
3607
3608 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3609 if (partialWakeTimer != null) {
3610 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3611 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003612 }
3613 }
3614 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003615
3616 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003617 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3618 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3619 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3620 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3621 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3622 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3623 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3624 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003625 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3626 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003627 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3628 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003629 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3630 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003631
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003632 // Dump Modem controller stats
3633 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3634 getModemControllerActivity(), which);
3635
Adam Lesinskie283d332015-04-16 12:29:25 -07003636 // Dump Wifi controller stats
3637 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3638 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003639 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003640 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003641
3642 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3643 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003644
3645 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003646 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3647 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003649 // Dump misc stats
3650 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003651 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003652 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003653 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003654 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003655 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003656 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3657 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003658 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003659 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3660 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3661 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3662 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003663 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003664
Dianne Hackborn617f8772009-03-31 15:04:46 -07003665 // Dump screen brightness stats
3666 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3667 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003668 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003669 }
3670 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003671
Dianne Hackborn627bba72009-03-24 22:32:56 -07003672 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003673 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3674 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003675 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003676 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003677 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003678 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003679 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003680 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003681 args[i] = getPhoneSignalStrengthCount(i, which);
3682 }
3683 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003684
Dianne Hackborn627bba72009-03-24 22:32:56 -07003685 // Dump network type stats
3686 args = new Object[NUM_DATA_CONNECTION_TYPES];
3687 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003688 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003689 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003690 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3691 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3692 args[i] = getPhoneDataConnectionCount(i, which);
3693 }
3694 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003695
3696 // Dump wifi state stats
3697 args = new Object[NUM_WIFI_STATES];
3698 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003699 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003700 }
3701 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3702 for (int i=0; i<NUM_WIFI_STATES; i++) {
3703 args[i] = getWifiStateCount(i, which);
3704 }
3705 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3706
Dianne Hackborn3251b902014-06-20 14:40:53 -07003707 // Dump wifi suppl state stats
3708 args = new Object[NUM_WIFI_SUPPL_STATES];
3709 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3710 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3711 }
3712 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3713 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3714 args[i] = getWifiSupplStateCount(i, which);
3715 }
3716 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3717
3718 // Dump wifi signal strength stats
3719 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3720 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3721 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3722 }
3723 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3724 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3725 args[i] = getWifiSignalStrengthCount(i, which);
3726 }
3727 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3728
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003729 // Dump Multicast total stats
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003730 final long multicastWakeLockTimeTotalMicros =
3731 getWifiMulticastWakelockTime(rawRealtime, which);
3732 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003733 dumpLine(pw, 0 /* uid */, category, WIFI_MULTICAST_TOTAL_DATA,
3734 multicastWakeLockTimeTotalMicros / 1000,
3735 multicastWakeLockCountTotal);
3736
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003737 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003738 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003739 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003740 }
Bookatzc8c44962017-05-11 12:12:54 -07003741
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003742 if (which == STATS_SINCE_UNPLUGGED) {
3743 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3744 getDischargeStartLevel()-getDischargeCurrentLevel(),
3745 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003746 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
Mike Mac2f518a2017-09-19 16:06:03 -07003747 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003748 getDischargeAmountScreenDoze(), dischargeScreenDozeCount / 1000,
3749 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003750 } else {
3751 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3752 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003753 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003754 getDischargeAmountScreenOffSinceCharge(),
Mike Mac2f518a2017-09-19 16:06:03 -07003755 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003756 getDischargeAmountScreenDozeSinceCharge(), dischargeScreenDozeCount / 1000,
3757 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003758 }
Bookatzc8c44962017-05-11 12:12:54 -07003759
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003760 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003761 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003762 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003763 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003764 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003765 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003766 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3767 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003768 }
Evan Millarc64edde2009-04-18 12:26:32 -07003769 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003770 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003771 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003772 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3773 // Not doing the regular wake lock formatting to remain compatible
3774 // with the old checkin format.
3775 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3776 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003777 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003778 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003779 }
3780 }
Evan Millarc64edde2009-04-18 12:26:32 -07003781 }
Bookatzc8c44962017-05-11 12:12:54 -07003782
Bookatz50df7112017-08-04 14:53:26 -07003783 final Map<String, ? extends Timer> rpmStats = getRpmStats();
3784 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
3785 if (rpmStats.size() > 0) {
3786 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
3787 sb.setLength(0);
3788 Timer totalTimer = ent.getValue();
3789 long timeMs = (totalTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3790 int count = totalTimer.getCountLocked(which);
3791 Timer screenOffTimer = screenOffRpmStats.get(ent.getKey());
3792 long screenOffTimeMs = screenOffTimer != null
3793 ? (screenOffTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3794 int screenOffCount = screenOffTimer != null
3795 ? screenOffTimer.getCountLocked(which) : 0;
Bookatz82b341172017-09-07 19:06:08 -07003796 if (SCREEN_OFF_RPM_STATS_ENABLED) {
3797 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3798 "\"" + ent.getKey() + "\"", timeMs, count, screenOffTimeMs,
3799 screenOffCount);
3800 } else {
3801 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3802 "\"" + ent.getKey() + "\"", timeMs, count);
3803 }
Bookatz50df7112017-08-04 14:53:26 -07003804 }
3805 }
3806
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003807 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003808 helper.create(this);
3809 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003810 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003811 if (sippers != null && sippers.size() > 0) {
3812 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3813 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003814 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003815 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3816 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
Kweku Adams87b19ec2017-10-09 12:40:03 -07003817 int uid = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003818 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003819 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003820 String label;
3821 switch (bs.drainType) {
Kweku Adamsef728952018-04-10 17:56:16 -07003822 case AMBIENT_DISPLAY:
3823 label = "ambi";
3824 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003825 case IDLE:
3826 label="idle";
3827 break;
3828 case CELL:
3829 label="cell";
3830 break;
3831 case PHONE:
3832 label="phone";
3833 break;
3834 case WIFI:
3835 label="wifi";
3836 break;
3837 case BLUETOOTH:
3838 label="blue";
3839 break;
3840 case SCREEN:
3841 label="scrn";
3842 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003843 case FLASHLIGHT:
3844 label="flashlight";
3845 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003846 case APP:
3847 uid = bs.uidObj.getUid();
3848 label = "uid";
3849 break;
3850 case USER:
3851 uid = UserHandle.getUid(bs.userId, 0);
3852 label = "user";
3853 break;
3854 case UNACCOUNTED:
3855 label = "unacc";
3856 break;
3857 case OVERCOUNTED:
3858 label = "over";
3859 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003860 case CAMERA:
3861 label = "camera";
3862 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003863 case MEMORY:
3864 label = "memory";
3865 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003866 default:
3867 label = "???";
3868 }
3869 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07003870 BatteryStatsHelper.makemAh(bs.totalPowerMah),
3871 bs.shouldHide ? 1 : 0,
3872 BatteryStatsHelper.makemAh(bs.screenPowerMah),
3873 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003874 }
3875 }
3876
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003877 final long[] cpuFreqs = getCpuFreqs();
3878 if (cpuFreqs != null) {
3879 sb.setLength(0);
3880 for (int i = 0; i < cpuFreqs.length; ++i) {
3881 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3882 }
3883 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3884 }
3885
Kweku Adams87b19ec2017-10-09 12:40:03 -07003886 // Dump stats per UID.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003887 for (int iu = 0; iu < NU; iu++) {
3888 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003889 if (reqUid >= 0 && uid != reqUid) {
3890 continue;
3891 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003892 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003895 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3896 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3897 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3898 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3899 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3900 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3901 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3902 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003903 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003904 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3905 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003906 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003907 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3908 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003909 // Background data transfers
3910 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3911 which);
3912 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3913 which);
3914 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3915 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3916 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3917 which);
3918 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3919 which);
3920 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3921 which);
3922 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3923 which);
3924
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003925 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3926 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003927 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003928 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3929 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3930 || wifiBytesBgTx > 0
3931 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3932 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003933 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3934 wifiBytesRx, wifiBytesTx,
3935 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003936 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003937 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003938 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3939 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3940 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3941 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003942 }
3943
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003944 // Dump modem controller data, per UID.
3945 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3946 u.getModemControllerActivity(), which);
3947
3948 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003949 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3950 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3951 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003952 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3953 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003954 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3955 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3956 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003957 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003958 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003959 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3960 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003961 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3962 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003963 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003964 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003966
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003967 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3968 u.getWifiControllerActivity(), which);
3969
Bookatz867c0d72017-03-07 18:23:42 -08003970 final Timer bleTimer = u.getBluetoothScanTimer();
3971 if (bleTimer != null) {
3972 // Convert from microseconds to milliseconds with rounding
3973 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3974 / 1000;
3975 if (totalTime != 0) {
3976 final int count = bleTimer.getCountLocked(which);
3977 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3978 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003979 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3980 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3981 final long actualTimeBg = bleTimerBg != null ?
3982 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003983 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003984 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3985 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003986 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3987 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3988 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3989 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3990 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
3991 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3992 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
3993 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3994 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
3995 final Timer unoptimizedScanTimerBg =
3996 u.getBluetoothUnoptimizedScanBackgroundTimer();
3997 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
3998 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3999 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
4000 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
4001
Bookatz867c0d72017-03-07 18:23:42 -08004002 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07004003 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
4004 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
4005 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08004006 }
4007 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004008
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004009 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
4010 u.getBluetoothControllerActivity(), which);
4011
Dianne Hackborn617f8772009-03-31 15:04:46 -07004012 if (u.hasUserActivity()) {
4013 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
4014 boolean hasData = false;
4015 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
4016 int val = u.getUserActivityCount(i, which);
4017 args[i] = val;
4018 if (val != 0) hasData = true;
4019 }
4020 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07004021 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004022 }
4023 }
Bookatzc8c44962017-05-11 12:12:54 -07004024
4025 if (u.getAggregatedPartialWakelockTimer() != null) {
4026 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07004027 // Times are since reset (regardless of 'which')
4028 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07004029 final Timer bgTimer = timer.getSubTimer();
4030 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004031 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07004032 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
4033 }
4034
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004035 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
4036 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4037 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4038 String linePrefix = "";
4039 sb.setLength(0);
4040 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
4041 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004042 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4043 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004044 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004045 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
4046 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004047 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
4048 rawRealtime, "w", which, linePrefix);
4049
Kweku Adams103351f2017-10-16 14:39:34 -07004050 // Only log if we had at least one wakelock...
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004051 if (sb.length() > 0) {
4052 String name = wakelocks.keyAt(iw);
4053 if (name.indexOf(',') >= 0) {
4054 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 }
Yi Jin02483362017-08-04 11:30:44 -07004056 if (name.indexOf('\n') >= 0) {
4057 name = name.replace('\n', '_');
4058 }
4059 if (name.indexOf('\r') >= 0) {
4060 name = name.replace('\r', '_');
4061 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004062 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 }
4064 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004065
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004066 // WiFi Multicast Wakelock Statistics
4067 final Timer mcTimer = u.getMulticastWakelockStats();
4068 if (mcTimer != null) {
4069 final long totalMcWakelockTimeMs =
4070 mcTimer.getTotalTimeLocked(rawRealtime, which) / 1000 ;
4071 final int countMcWakelock = mcTimer.getCountLocked(which);
4072 if(totalMcWakelockTimeMs > 0) {
4073 dumpLine(pw, uid, category, WIFI_MULTICAST_DATA,
4074 totalMcWakelockTimeMs, countMcWakelock);
4075 }
4076 }
4077
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004078 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4079 for (int isy=syncs.size()-1; isy>=0; isy--) {
4080 final Timer timer = syncs.valueAt(isy);
4081 // Convert from microseconds to milliseconds with rounding
4082 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4083 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07004084 final Timer bgTimer = timer.getSubTimer();
4085 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004086 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07004087 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004088 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004089 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07004090 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004091 }
4092 }
4093
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004094 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4095 for (int ij=jobs.size()-1; ij>=0; ij--) {
4096 final Timer timer = jobs.valueAt(ij);
4097 // Convert from microseconds to milliseconds with rounding
4098 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4099 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07004100 final Timer bgTimer = timer.getSubTimer();
4101 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004102 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07004103 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004104 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004105 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07004106 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004107 }
4108 }
4109
Dianne Hackborn94326cb2017-06-28 16:17:20 -07004110 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
4111 for (int ic=completions.size()-1; ic>=0; ic--) {
4112 SparseIntArray types = completions.valueAt(ic);
4113 if (types != null) {
4114 dumpLine(pw, uid, category, JOB_COMPLETION_DATA,
4115 "\"" + completions.keyAt(ic) + "\"",
4116 types.get(JobParameters.REASON_CANCELED, 0),
4117 types.get(JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, 0),
4118 types.get(JobParameters.REASON_PREEMPT, 0),
4119 types.get(JobParameters.REASON_TIMEOUT, 0),
4120 types.get(JobParameters.REASON_DEVICE_IDLE, 0));
4121 }
4122 }
4123
Amith Yamasani977e11f2018-02-16 11:29:54 -08004124 // Dump deferred jobs stats
4125 u.getDeferredJobsCheckinLineLocked(sb, which);
4126 if (sb.length() > 0) {
4127 dumpLine(pw, uid, category, JOBS_DEFERRED_DATA, sb.toString());
4128 }
4129
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004130 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
4131 rawRealtime, which);
4132 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
4133 rawRealtime, which);
4134 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
4135 rawRealtime, which);
4136 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
4137 rawRealtime, which);
4138
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004139 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4140 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004141 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004142 final Uid.Sensor se = sensors.valueAt(ise);
4143 final int sensorNumber = sensors.keyAt(ise);
4144 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004145 if (timer != null) {
4146 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004147 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4148 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004149 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004150 final int count = timer.getCountLocked(which);
4151 final Timer bgTimer = se.getSensorBackgroundTime();
4152 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004153 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4154 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4155 final long bgActualTime = bgTimer != null ?
4156 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4157 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
4158 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004160 }
4161 }
4162
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004163 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
4164 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004165
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07004166 dumpTimer(pw, uid, category, FOREGROUND_ACTIVITY_DATA, u.getForegroundActivityTimer(),
4167 rawRealtime, which);
4168
4169 dumpTimer(pw, uid, category, FOREGROUND_SERVICE_DATA, u.getForegroundServiceTimer(),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004170 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004171
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004172 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07004173 long totalStateTime = 0;
4174 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08004175 final long time = u.getProcessStateTime(ips, rawRealtime, which);
4176 totalStateTime += time;
4177 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004178 }
4179 if (totalStateTime > 0) {
4180 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
4181 }
4182
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004183 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4184 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004185 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004186 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004187 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004188 }
4189
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004190 // If the cpuFreqs is null, then don't bother checking for cpu freq times.
4191 if (cpuFreqs != null) {
4192 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
4193 // If total cpuFreqTimes is null, then we don't need to check for
4194 // screenOffCpuFreqTimes.
4195 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
4196 sb.setLength(0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004197 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004198 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004199 }
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004200 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
4201 if (screenOffCpuFreqTimeMs != null) {
4202 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
4203 sb.append("," + screenOffCpuFreqTimeMs[i]);
4204 }
4205 } else {
4206 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
4207 sb.append(",0");
4208 }
4209 }
4210 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
4211 cpuFreqTimeMs.length, sb.toString());
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004212 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -08004213
4214 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
4215 final long[] timesMs = u.getCpuFreqTimes(which, procState);
4216 if (timesMs != null && timesMs.length == cpuFreqs.length) {
4217 sb.setLength(0);
4218 for (int i = 0; i < timesMs.length; ++i) {
4219 sb.append((i == 0 ? "" : ",") + timesMs[i]);
4220 }
4221 final long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(
4222 which, procState);
4223 if (screenOffTimesMs != null) {
4224 for (int i = 0; i < screenOffTimesMs.length; ++i) {
4225 sb.append("," + screenOffTimesMs[i]);
4226 }
4227 } else {
4228 for (int i = 0; i < timesMs.length; ++i) {
4229 sb.append(",0");
4230 }
4231 }
4232 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA,
4233 Uid.UID_PROCESS_TYPES[procState], timesMs.length, sb.toString());
4234 }
4235 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004236 }
4237
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004238 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4239 = u.getProcessStats();
4240 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4241 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004242
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004243 final long userMillis = ps.getUserTime(which);
4244 final long systemMillis = ps.getSystemTime(which);
4245 final long foregroundMillis = ps.getForegroundTime(which);
4246 final int starts = ps.getStarts(which);
4247 final int numCrashes = ps.getNumCrashes(which);
4248 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004249
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004250 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
4251 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004252 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
4253 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254 }
4255 }
4256
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004257 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4258 = u.getPackageStats();
4259 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4260 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4261 int wakeups = 0;
4262 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4263 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07004264 int count = alarms.valueAt(iwa).getCountLocked(which);
4265 wakeups += count;
4266 String name = alarms.keyAt(iwa).replace(',', '_');
4267 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004268 }
4269 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4270 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4271 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4272 final long startTime = ss.getStartTime(batteryUptime, which);
4273 final int starts = ss.getStarts(which);
4274 final int launches = ss.getLaunches(which);
4275 if (startTime != 0 || starts != 0 || launches != 0) {
4276 dumpLine(pw, uid, category, APK_DATA,
4277 wakeups, // wakeup alarms
4278 packageStats.keyAt(ipkg), // Apk
4279 serviceStats.keyAt(isvc), // service
4280 startTime / 1000, // time spent started, in ms
4281 starts,
4282 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004283 }
4284 }
4285 }
4286 }
4287 }
4288
Dianne Hackborn81038902012-11-26 17:04:09 -08004289 static final class TimerEntry {
4290 final String mName;
4291 final int mId;
4292 final BatteryStats.Timer mTimer;
4293 final long mTime;
4294 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
4295 mName = name;
4296 mId = id;
4297 mTimer = timer;
4298 mTime = time;
4299 }
4300 }
4301
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004302 private void printmAh(PrintWriter printer, double power) {
4303 printer.print(BatteryStatsHelper.makemAh(power));
4304 }
4305
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004306 private void printmAh(StringBuilder sb, double power) {
4307 sb.append(BatteryStatsHelper.makemAh(power));
4308 }
4309
Dianne Hackbornd953c532014-08-16 18:17:38 -07004310 /**
4311 * Temporary for settings.
4312 */
4313 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
4314 int reqUid) {
4315 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
4316 }
4317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004318 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004319 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07004320 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 final long rawUptime = SystemClock.uptimeMillis() * 1000;
4322 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07004323 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004325
4326 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
4327 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
4328 final long totalRealtime = computeRealtime(rawRealtime, which);
4329 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004330 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
4331 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
4332 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004333 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
4334 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -07004335 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004336
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004337 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07004338
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004339 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07004340 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004341
Adam Lesinskif9b20a92016-06-17 17:30:01 -07004342 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
4343 if (estimatedBatteryCapacity > 0) {
4344 sb.setLength(0);
4345 sb.append(prefix);
4346 sb.append(" Estimated battery capacity: ");
4347 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
4348 sb.append(" mAh");
4349 pw.println(sb.toString());
4350 }
4351
Jocelyn Dangc627d102017-04-14 13:15:14 -07004352 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
4353 if (minLearnedBatteryCapacity > 0) {
4354 sb.setLength(0);
4355 sb.append(prefix);
4356 sb.append(" Min learned battery capacity: ");
4357 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
4358 sb.append(" mAh");
4359 pw.println(sb.toString());
4360 }
4361 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
4362 if (maxLearnedBatteryCapacity > 0) {
4363 sb.setLength(0);
4364 sb.append(prefix);
4365 sb.append(" Max learned battery capacity: ");
4366 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
4367 sb.append(" mAh");
4368 pw.println(sb.toString());
4369 }
4370
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004371 sb.setLength(0);
4372 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004373 sb.append(" Time on battery: ");
4374 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
4375 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
4376 sb.append(") realtime, ");
4377 formatTimeMs(sb, whichBatteryUptime / 1000);
4378 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, whichBatteryRealtime));
4379 sb.append(") uptime");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004380 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004381
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004382 sb.setLength(0);
4383 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004384 sb.append(" Time on battery screen off: ");
4385 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
4386 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, whichBatteryRealtime));
4387 sb.append(") realtime, ");
4388 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
4389 sb.append("(");
4390 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, whichBatteryRealtime));
4391 sb.append(") uptime");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004392 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004393
4394 sb.setLength(0);
4395 sb.append(prefix);
4396 sb.append(" Time on battery screen doze: ");
4397 formatTimeMs(sb, screenDozeTime / 1000); sb.append("(");
4398 sb.append(formatRatioLocked(screenDozeTime, whichBatteryRealtime));
4399 sb.append(")");
4400 pw.println(sb.toString());
4401
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004402 sb.setLength(0);
4403 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004404 sb.append(" Total run time: ");
4405 formatTimeMs(sb, totalRealtime / 1000);
4406 sb.append("realtime, ");
4407 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004408 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07004409 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004410 if (batteryTimeRemaining >= 0) {
4411 sb.setLength(0);
4412 sb.append(prefix);
4413 sb.append(" Battery time remaining: ");
4414 formatTimeMs(sb, batteryTimeRemaining / 1000);
4415 pw.println(sb.toString());
4416 }
4417 if (chargeTimeRemaining >= 0) {
4418 sb.setLength(0);
4419 sb.append(prefix);
4420 sb.append(" Charge time remaining: ");
4421 formatTimeMs(sb, chargeTimeRemaining / 1000);
4422 pw.println(sb.toString());
4423 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004424
Kweku Adams87b19ec2017-10-09 12:40:03 -07004425 final long dischargeCount = getUahDischarge(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004426 if (dischargeCount >= 0) {
4427 sb.setLength(0);
4428 sb.append(prefix);
4429 sb.append(" Discharge: ");
4430 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
4431 sb.append(" mAh");
4432 pw.println(sb.toString());
4433 }
4434
Kweku Adams87b19ec2017-10-09 12:40:03 -07004435 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004436 if (dischargeScreenOffCount >= 0) {
4437 sb.setLength(0);
4438 sb.append(prefix);
4439 sb.append(" Screen off discharge: ");
4440 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
4441 sb.append(" mAh");
4442 pw.println(sb.toString());
4443 }
4444
Kweku Adams87b19ec2017-10-09 12:40:03 -07004445 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Mac2f518a2017-09-19 16:06:03 -07004446 if (dischargeScreenDozeCount >= 0) {
4447 sb.setLength(0);
4448 sb.append(prefix);
4449 sb.append(" Screen doze discharge: ");
4450 sb.append(BatteryStatsHelper.makemAh(dischargeScreenDozeCount / 1000.0));
4451 sb.append(" mAh");
4452 pw.println(sb.toString());
4453 }
4454
Mike Ma90902652018-04-17 14:07:24 -07004455 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004456 if (dischargeScreenOnCount >= 0) {
4457 sb.setLength(0);
4458 sb.append(prefix);
4459 sb.append(" Screen on discharge: ");
4460 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
4461 sb.append(" mAh");
4462 pw.println(sb.toString());
4463 }
4464
Mike Ma15313c92017-11-15 17:58:21 -08004465 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
4466 if (dischargeLightDozeCount >= 0) {
4467 sb.setLength(0);
4468 sb.append(prefix);
4469 sb.append(" Device light doze discharge: ");
4470 sb.append(BatteryStatsHelper.makemAh(dischargeLightDozeCount / 1000.0));
4471 sb.append(" mAh");
4472 pw.println(sb.toString());
4473 }
4474
4475 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
4476 if (dischargeDeepDozeCount >= 0) {
4477 sb.setLength(0);
4478 sb.append(prefix);
4479 sb.append(" Device deep doze discharge: ");
4480 sb.append(BatteryStatsHelper.makemAh(dischargeDeepDozeCount / 1000.0));
4481 sb.append(" mAh");
4482 pw.println(sb.toString());
4483 }
4484
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004485 pw.print(" Start clock time: ");
4486 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
4487
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004488 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004489 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004490 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004491 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
4492 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004493 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004494 rawRealtime, which);
4495 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
4496 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004497 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004498 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004499 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
4500 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
4501 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004502 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004503 sb.append(prefix);
4504 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
4505 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004506 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004507 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
4508 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004509 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004510 pw.println(sb.toString());
4511 sb.setLength(0);
4512 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004513 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004514 boolean didOne = false;
4515 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004516 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004517 if (time == 0) {
4518 continue;
4519 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004520 sb.append("\n ");
4521 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004522 didOne = true;
4523 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
4524 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004525 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004526 sb.append("(");
4527 sb.append(formatRatioLocked(time, screenOnTime));
4528 sb.append(")");
4529 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004530 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004531 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004532 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004533 sb.setLength(0);
4534 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004535 sb.append(" Power save mode enabled: ");
4536 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004537 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004538 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004539 sb.append(")");
4540 pw.println(sb.toString());
4541 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004542 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004543 sb.setLength(0);
4544 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004545 sb.append(" Device light idling: ");
4546 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004547 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004548 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
4549 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004550 sb.append("x");
4551 pw.println(sb.toString());
4552 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004553 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004554 sb.setLength(0);
4555 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004556 sb.append(" Idle mode light time: ");
4557 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004558 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004559 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
4560 sb.append(") ");
4561 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004562 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004563 sb.append(" -- longest ");
4564 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
4565 pw.println(sb.toString());
4566 }
4567 if (deviceIdlingTime != 0) {
4568 sb.setLength(0);
4569 sb.append(prefix);
4570 sb.append(" Device full idling: ");
4571 formatTimeMs(sb, deviceIdlingTime / 1000);
4572 sb.append("(");
4573 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004574 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004575 sb.append("x");
4576 pw.println(sb.toString());
4577 }
4578 if (deviceIdleModeFullTime != 0) {
4579 sb.setLength(0);
4580 sb.append(prefix);
4581 sb.append(" Idle mode full time: ");
4582 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
4583 sb.append("(");
4584 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
4585 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004586 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004587 sb.append("x");
4588 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004589 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004590 pw.println(sb.toString());
4591 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004592 if (phoneOnTime != 0) {
4593 sb.setLength(0);
4594 sb.append(prefix);
4595 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
4596 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004597 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004598 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004599 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004600 if (connChanges != 0) {
4601 pw.print(prefix);
4602 pw.print(" Connectivity changes: "); pw.println(connChanges);
4603 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004604
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004605 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07004606 long fullWakeLockTimeTotalMicros = 0;
4607 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08004608
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004609 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08004610
Evan Millar22ac0432009-03-31 11:33:18 -07004611 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004612 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004613
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004614 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4615 = u.getWakelockStats();
4616 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4617 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07004618
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004619 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
4620 if (fullWakeTimer != null) {
4621 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
4622 rawRealtime, which);
4623 }
4624
4625 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4626 if (partialWakeTimer != null) {
4627 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
4628 rawRealtime, which);
4629 if (totalTimeMicros > 0) {
4630 if (reqUid < 0) {
4631 // Only show the ordered list of all wake
4632 // locks if the caller is not asking for data
4633 // about a specific uid.
4634 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
4635 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08004636 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004637 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07004638 }
4639 }
4640 }
4641 }
Bookatzc8c44962017-05-11 12:12:54 -07004642
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004643 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4644 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4645 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4646 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4647 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4648 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4649 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4650 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004651 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4652 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004653
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004654 if (fullWakeLockTimeTotalMicros != 0) {
4655 sb.setLength(0);
4656 sb.append(prefix);
4657 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4658 (fullWakeLockTimeTotalMicros + 500) / 1000);
4659 pw.println(sb.toString());
4660 }
4661
4662 if (partialWakeLockTimeTotalMicros != 0) {
4663 sb.setLength(0);
4664 sb.append(prefix);
4665 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4666 (partialWakeLockTimeTotalMicros + 500) / 1000);
4667 pw.println(sb.toString());
4668 }
4669
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004670 final long multicastWakeLockTimeTotalMicros =
4671 getWifiMulticastWakelockTime(rawRealtime, which);
4672 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004673 if (multicastWakeLockTimeTotalMicros != 0) {
4674 sb.setLength(0);
4675 sb.append(prefix);
4676 sb.append(" Total WiFi Multicast wakelock Count: ");
4677 sb.append(multicastWakeLockCountTotal);
4678 pw.println(sb.toString());
4679
4680 sb.setLength(0);
4681 sb.append(prefix);
4682 sb.append(" Total WiFi Multicast wakelock time: ");
4683 formatTimeMsNoSpace(sb, (multicastWakeLockTimeTotalMicros + 500) / 1000);
4684 pw.println(sb.toString());
4685 }
4686
Siddharth Ray3c648c42017-10-02 17:30:58 -07004687 pw.println("");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004688 pw.print(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004689 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004690 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004691 sb.append(" CONNECTIVITY POWER SUMMARY START");
4692 pw.println(sb.toString());
4693
4694 pw.print(prefix);
4695 sb.setLength(0);
4696 sb.append(prefix);
4697 sb.append(" Logging duration for connectivity statistics: ");
4698 formatTimeMs(sb, whichBatteryRealtime / 1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004699 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004700
4701 sb.setLength(0);
4702 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004703 sb.append(" Cellular Statistics:");
Amith Yamasanif37447b2009-10-08 18:28:01 -07004704 pw.println(sb.toString());
4705
Siddharth Ray3c648c42017-10-02 17:30:58 -07004706 pw.print(prefix);
4707 sb.setLength(0);
4708 sb.append(prefix);
4709 sb.append(" Cellular kernel active time: ");
4710 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
4711 formatTimeMs(sb, mobileActiveTime / 1000);
4712 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4713 sb.append(")");
4714 pw.println(sb.toString());
4715
4716 pw.print(" Cellular data received: "); pw.println(formatBytesLocked(mobileRxTotalBytes));
4717 pw.print(" Cellular data sent: "); pw.println(formatBytesLocked(mobileTxTotalBytes));
4718 pw.print(" Cellular packets received: "); pw.println(mobileRxTotalPackets);
4719 pw.print(" Cellular packets sent: "); pw.println(mobileTxTotalPackets);
4720
Dianne Hackborn627bba72009-03-24 22:32:56 -07004721 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004722 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004723 sb.append(" Cellular Radio Access Technology:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004724 didOne = false;
4725 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004726 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004727 if (time == 0) {
4728 continue;
4729 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004730 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004731 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004732 didOne = true;
4733 sb.append(DATA_CONNECTION_NAMES[i]);
4734 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004735 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004736 sb.append("(");
4737 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004738 sb.append(") ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004739 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004740 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004741 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004742
4743 sb.setLength(0);
4744 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004745 sb.append(" Cellular Rx signal strength (RSRP):");
4746 final String[] cellularRxSignalStrengthDescription = new String[]{
4747 "very poor (less than -128dBm): ",
4748 "poor (-128dBm to -118dBm): ",
4749 "moderate (-118dBm to -108dBm): ",
4750 "good (-108dBm to -98dBm): ",
4751 "great (greater than -98dBm): "};
4752 didOne = false;
4753 final int numCellularRxBins = Math.min(SignalStrength.NUM_SIGNAL_STRENGTH_BINS,
4754 cellularRxSignalStrengthDescription.length);
4755 for (int i=0; i<numCellularRxBins; i++) {
4756 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
4757 if (time == 0) {
4758 continue;
4759 }
4760 sb.append("\n ");
4761 sb.append(prefix);
4762 didOne = true;
4763 sb.append(cellularRxSignalStrengthDescription[i]);
4764 sb.append(" ");
4765 formatTimeMs(sb, time/1000);
4766 sb.append("(");
4767 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4768 sb.append(") ");
4769 }
4770 if (!didOne) sb.append(" (no activity)");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004771 pw.println(sb.toString());
4772
Siddharth Rayb50a6842017-12-14 15:15:28 -08004773 printControllerActivity(pw, sb, prefix, CELLULAR_CONTROLLER_NAME,
Siddharth Ray3c648c42017-10-02 17:30:58 -07004774 getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004775
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004776 pw.print(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004777 sb.setLength(0);
4778 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004779 sb.append(" Wifi Statistics:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004780 pw.println(sb.toString());
4781
Siddharth Rayb50a6842017-12-14 15:15:28 -08004782 pw.print(prefix);
4783 sb.setLength(0);
4784 sb.append(prefix);
4785 sb.append(" Wifi kernel active time: ");
4786 final long wifiActiveTime = getWifiActiveTime(rawRealtime, which);
4787 formatTimeMs(sb, wifiActiveTime / 1000);
4788 sb.append("("); sb.append(formatRatioLocked(wifiActiveTime, whichBatteryRealtime));
4789 sb.append(")");
4790 pw.println(sb.toString());
4791
Siddharth Ray3c648c42017-10-02 17:30:58 -07004792 pw.print(" Wifi data received: "); pw.println(formatBytesLocked(wifiRxTotalBytes));
4793 pw.print(" Wifi data sent: "); pw.println(formatBytesLocked(wifiTxTotalBytes));
4794 pw.print(" Wifi packets received: "); pw.println(wifiRxTotalPackets);
4795 pw.print(" Wifi packets sent: "); pw.println(wifiTxTotalPackets);
4796
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004797 sb.setLength(0);
4798 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004799 sb.append(" Wifi states:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004800 didOne = false;
4801 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004802 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004803 if (time == 0) {
4804 continue;
4805 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004806 sb.append("\n ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004807 didOne = true;
4808 sb.append(WIFI_STATE_NAMES[i]);
4809 sb.append(" ");
4810 formatTimeMs(sb, time/1000);
4811 sb.append("(");
4812 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4813 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004814 }
4815 if (!didOne) sb.append(" (no activity)");
4816 pw.println(sb.toString());
4817
4818 sb.setLength(0);
4819 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004820 sb.append(" Wifi supplicant states:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004821 didOne = false;
4822 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4823 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4824 if (time == 0) {
4825 continue;
4826 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004827 sb.append("\n ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004828 didOne = true;
4829 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4830 sb.append(" ");
4831 formatTimeMs(sb, time/1000);
4832 sb.append("(");
4833 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4834 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004835 }
4836 if (!didOne) sb.append(" (no activity)");
4837 pw.println(sb.toString());
4838
4839 sb.setLength(0);
4840 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004841 sb.append(" Wifi Rx signal strength (RSSI):");
4842 final String[] wifiRxSignalStrengthDescription = new String[]{
4843 "very poor (less than -88.75dBm): ",
4844 "poor (-88.75 to -77.5dBm): ",
4845 "moderate (-77.5dBm to -66.25dBm): ",
4846 "good (-66.25dBm to -55dBm): ",
4847 "great (greater than -55dBm): "};
Dianne Hackborn3251b902014-06-20 14:40:53 -07004848 didOne = false;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004849 final int numWifiRxBins = Math.min(NUM_WIFI_SIGNAL_STRENGTH_BINS,
4850 wifiRxSignalStrengthDescription.length);
4851 for (int i=0; i<numWifiRxBins; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004852 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4853 if (time == 0) {
4854 continue;
4855 }
4856 sb.append("\n ");
4857 sb.append(prefix);
4858 didOne = true;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004859 sb.append(" ");
4860 sb.append(wifiRxSignalStrengthDescription[i]);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004861 formatTimeMs(sb, time/1000);
4862 sb.append("(");
4863 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4864 sb.append(") ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004865 }
4866 if (!didOne) sb.append(" (no activity)");
4867 pw.println(sb.toString());
4868
Siddharth Rayb50a6842017-12-14 15:15:28 -08004869 printControllerActivity(pw, sb, prefix, WIFI_CONTROLLER_NAME,
4870 getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004871
Adam Lesinski50e47602015-12-04 17:04:54 -08004872 pw.print(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004873 sb.setLength(0);
4874 sb.append(prefix);
Siddharth Ray78ccaf52017-12-23 16:16:21 -08004875 sb.append(" GPS Statistics:");
4876 pw.println(sb.toString());
4877
4878 sb.setLength(0);
4879 sb.append(prefix);
4880 sb.append(" GPS signal quality (Top 4 Average CN0):");
4881 final String[] gpsSignalQualityDescription = new String[]{
4882 "poor (less than 20 dBHz): ",
4883 "good (greater than 20 dBHz): "};
4884 final int numGpsSignalQualityBins = Math.min(GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS,
4885 gpsSignalQualityDescription.length);
4886 for (int i=0; i<numGpsSignalQualityBins; i++) {
4887 final long time = getGpsSignalQualityTime(i, rawRealtime, which);
4888 sb.append("\n ");
4889 sb.append(prefix);
4890 sb.append(" ");
4891 sb.append(gpsSignalQualityDescription[i]);
4892 formatTimeMs(sb, time/1000);
4893 sb.append("(");
4894 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4895 sb.append(") ");
4896 }
4897 pw.println(sb.toString());
4898
4899 final long gpsBatteryDrainMaMs = getGpsBatteryDrainMaMs();
4900 if (gpsBatteryDrainMaMs > 0) {
4901 pw.print(prefix);
4902 sb.setLength(0);
4903 sb.append(prefix);
4904 sb.append(" Battery Drain (mAh): ");
4905 sb.append(Double.toString(((double) gpsBatteryDrainMaMs)/(3600 * 1000)));
4906 pw.println(sb.toString());
4907 }
4908
4909 pw.print(prefix);
4910 sb.setLength(0);
4911 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004912 sb.append(" CONNECTIVITY POWER SUMMARY END");
4913 pw.println(sb.toString());
4914 pw.println("");
4915
4916 pw.print(prefix);
Adam Lesinski50e47602015-12-04 17:04:54 -08004917 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4918 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4919
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004920 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4921 sb.setLength(0);
4922 sb.append(prefix);
4923 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4924 pw.println(sb.toString());
4925
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004926 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4927 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004928
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004929 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004930
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004931 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004932 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004933 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004934 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004935 pw.println(getDischargeStartLevel());
4936 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4937 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004938 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004939 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004940 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004941 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004942 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004943 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004944 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004945 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004946 pw.println(getDischargeAmountScreenOn());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004947 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004948 pw.println(getDischargeAmountScreenOff());
4949 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4950 pw.println(getDischargeAmountScreenDoze());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004951 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004952 } else {
4953 pw.print(prefix); pw.println(" Device battery use since last full charge");
4954 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004955 pw.println(getLowDischargeAmountSinceCharge());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004956 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004957 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004958 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004959 pw.println(getDischargeAmountScreenOnSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004960 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004961 pw.println(getDischargeAmountScreenOffSinceCharge());
4962 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4963 pw.println(getDischargeAmountScreenDozeSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004964 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004965 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004966
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004967 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004968 helper.create(this);
4969 helper.refreshStats(which, UserHandle.USER_ALL);
4970 List<BatterySipper> sippers = helper.getUsageList();
4971 if (sippers != null && sippers.size() > 0) {
4972 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4973 pw.print(prefix); pw.print(" Capacity: ");
4974 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004975 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004976 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4977 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4978 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4979 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004980 pw.println();
4981 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004982 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004983 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004984 switch (bs.drainType) {
Kweku Adamsef728952018-04-10 17:56:16 -07004985 case AMBIENT_DISPLAY:
4986 pw.print(" Ambient display: ");
4987 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004988 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004989 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004990 break;
4991 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004992 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004993 break;
4994 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004995 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004996 break;
4997 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004998 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004999 break;
5000 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005001 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005002 break;
5003 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005004 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005005 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005006 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005007 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005008 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005009 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005010 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005011 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005012 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005013 break;
5014 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005015 pw.print(" User "); pw.print(bs.userId);
5016 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005017 break;
5018 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005019 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005020 break;
5021 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005022 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005023 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005024 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005025 pw.print(" Camera: ");
5026 break;
5027 default:
5028 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005029 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005030 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005031 printmAh(pw, bs.totalPowerMah);
5032
Adam Lesinski57123002015-06-12 16:12:07 -07005033 if (bs.usagePowerMah != bs.totalPowerMah) {
5034 // If the usage (generic power) isn't the whole amount, we list out
5035 // what components are involved in the calculation.
5036
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005037 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07005038 if (bs.usagePowerMah != 0) {
5039 pw.print(" usage=");
5040 printmAh(pw, bs.usagePowerMah);
5041 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005042 if (bs.cpuPowerMah != 0) {
5043 pw.print(" cpu=");
5044 printmAh(pw, bs.cpuPowerMah);
5045 }
5046 if (bs.wakeLockPowerMah != 0) {
5047 pw.print(" wake=");
5048 printmAh(pw, bs.wakeLockPowerMah);
5049 }
5050 if (bs.mobileRadioPowerMah != 0) {
5051 pw.print(" radio=");
5052 printmAh(pw, bs.mobileRadioPowerMah);
5053 }
5054 if (bs.wifiPowerMah != 0) {
5055 pw.print(" wifi=");
5056 printmAh(pw, bs.wifiPowerMah);
5057 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005058 if (bs.bluetoothPowerMah != 0) {
5059 pw.print(" bt=");
5060 printmAh(pw, bs.bluetoothPowerMah);
5061 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005062 if (bs.gpsPowerMah != 0) {
5063 pw.print(" gps=");
5064 printmAh(pw, bs.gpsPowerMah);
5065 }
5066 if (bs.sensorPowerMah != 0) {
5067 pw.print(" sensor=");
5068 printmAh(pw, bs.sensorPowerMah);
5069 }
5070 if (bs.cameraPowerMah != 0) {
5071 pw.print(" camera=");
5072 printmAh(pw, bs.cameraPowerMah);
5073 }
5074 if (bs.flashlightPowerMah != 0) {
5075 pw.print(" flash=");
5076 printmAh(pw, bs.flashlightPowerMah);
5077 }
5078 pw.print(" )");
5079 }
Bookatz17d7d9d2017-06-08 14:50:46 -07005080
5081 // If there is additional smearing information, include it.
5082 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
5083 pw.print(" Including smearing: ");
5084 printmAh(pw, bs.totalSmearedPowerMah);
5085 pw.print(" (");
5086 if (bs.screenPowerMah != 0) {
5087 pw.print(" screen=");
5088 printmAh(pw, bs.screenPowerMah);
5089 }
5090 if (bs.proportionalSmearMah != 0) {
5091 pw.print(" proportional=");
5092 printmAh(pw, bs.proportionalSmearMah);
5093 }
5094 pw.print(" )");
5095 }
5096 if (bs.shouldHide) {
5097 pw.print(" Excluded from smearing");
5098 }
5099
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005100 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005101 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08005102 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005103 }
5104
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005105 sippers = helper.getMobilemsppList();
5106 if (sippers != null && sippers.size() > 0) {
5107 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005108 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005109 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005110 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005111 sb.setLength(0);
5112 sb.append(prefix); sb.append(" Uid ");
5113 UserHandle.formatUid(sb, bs.uidObj.getUid());
5114 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
5115 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
5116 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005117 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005118 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005119 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005120 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005121 sb.setLength(0);
5122 sb.append(prefix);
5123 sb.append(" TOTAL TIME: ");
5124 formatTimeMs(sb, totalTime);
5125 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
5126 sb.append(")");
5127 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005128 pw.println();
5129 }
5130
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005131 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
5132 @Override
5133 public int compare(TimerEntry lhs, TimerEntry rhs) {
5134 long lhsTime = lhs.mTime;
5135 long rhsTime = rhs.mTime;
5136 if (lhsTime < rhsTime) {
5137 return 1;
5138 }
5139 if (lhsTime > rhsTime) {
5140 return -1;
5141 }
5142 return 0;
5143 }
5144 };
5145
5146 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005147 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
5148 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005149 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005150 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
5151 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
5152 : kernelWakelocks.entrySet()) {
5153 final BatteryStats.Timer timer = ent.getValue();
5154 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005155 if (totalTimeMillis > 0) {
5156 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
5157 }
5158 }
5159 if (ktimers.size() > 0) {
5160 Collections.sort(ktimers, timerComparator);
5161 pw.print(prefix); pw.println(" All kernel wake locks:");
5162 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005163 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005164 String linePrefix = ": ";
5165 sb.setLength(0);
5166 sb.append(prefix);
5167 sb.append(" Kernel Wake lock ");
5168 sb.append(timer.mName);
5169 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
5170 which, linePrefix);
5171 if (!linePrefix.equals(": ")) {
5172 sb.append(" realtime");
5173 // Only print out wake locks that were held
5174 pw.println(sb.toString());
5175 }
5176 }
5177 pw.println();
5178 }
5179 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005180
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005181 if (timers.size() > 0) {
5182 Collections.sort(timers, timerComparator);
5183 pw.print(prefix); pw.println(" All partial wake locks:");
5184 for (int i=0; i<timers.size(); i++) {
5185 TimerEntry timer = timers.get(i);
5186 sb.setLength(0);
5187 sb.append(" Wake lock ");
5188 UserHandle.formatUid(sb, timer.mId);
5189 sb.append(" ");
5190 sb.append(timer.mName);
5191 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5192 sb.append(" realtime");
5193 pw.println(sb.toString());
5194 }
5195 timers.clear();
5196 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08005197 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005198
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005199 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005200 if (wakeupReasons.size() > 0) {
5201 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005202 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005203 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005204 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005205 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
5206 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005207 }
5208 Collections.sort(reasons, timerComparator);
5209 for (int i=0; i<reasons.size(); i++) {
5210 TimerEntry timer = reasons.get(i);
5211 String linePrefix = ": ";
5212 sb.setLength(0);
5213 sb.append(prefix);
5214 sb.append(" Wakeup reason ");
5215 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005216 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5217 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005218 pw.println(sb.toString());
5219 }
5220 pw.println();
5221 }
Dianne Hackborn81038902012-11-26 17:04:09 -08005222 }
Evan Millar22ac0432009-03-31 11:33:18 -07005223
James Carr2dd7e5e2016-07-20 18:48:39 -07005224 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
Bookatz50df7112017-08-04 14:53:26 -07005225 if (mMemoryStats.size() > 0) {
5226 pw.println(" Memory Stats");
5227 for (int i = 0; i < mMemoryStats.size(); i++) {
5228 sb.setLength(0);
5229 sb.append(" Bandwidth ");
5230 sb.append(mMemoryStats.keyAt(i));
5231 sb.append(" Time ");
5232 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
5233 pw.println(sb.toString());
5234 }
5235 pw.println();
5236 }
5237
5238 final Map<String, ? extends Timer> rpmStats = getRpmStats();
5239 if (rpmStats.size() > 0) {
5240 pw.print(prefix); pw.println(" Resource Power Manager Stats");
5241 if (rpmStats.size() > 0) {
5242 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
5243 final String timerName = ent.getKey();
5244 final Timer timer = ent.getValue();
5245 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5246 }
5247 }
5248 pw.println();
5249 }
Bookatz82b341172017-09-07 19:06:08 -07005250 if (SCREEN_OFF_RPM_STATS_ENABLED) {
5251 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
Bookatz50df7112017-08-04 14:53:26 -07005252 if (screenOffRpmStats.size() > 0) {
Bookatz82b341172017-09-07 19:06:08 -07005253 pw.print(prefix);
5254 pw.println(" Resource Power Manager Stats for when screen was off");
5255 if (screenOffRpmStats.size() > 0) {
5256 for (Map.Entry<String, ? extends Timer> ent : screenOffRpmStats.entrySet()) {
5257 final String timerName = ent.getKey();
5258 final Timer timer = ent.getValue();
5259 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5260 }
Bookatz50df7112017-08-04 14:53:26 -07005261 }
Bookatz82b341172017-09-07 19:06:08 -07005262 pw.println();
Bookatz50df7112017-08-04 14:53:26 -07005263 }
James Carr2dd7e5e2016-07-20 18:48:39 -07005264 }
5265
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005266 final long[] cpuFreqs = getCpuFreqs();
5267 if (cpuFreqs != null) {
5268 sb.setLength(0);
Bookatz50df7112017-08-04 14:53:26 -07005269 sb.append(" CPU freqs:");
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005270 for (int i = 0; i < cpuFreqs.length; ++i) {
5271 sb.append(" " + cpuFreqs[i]);
5272 }
5273 pw.println(sb.toString());
Bookatz50df7112017-08-04 14:53:26 -07005274 pw.println();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005275 }
5276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 for (int iu=0; iu<NU; iu++) {
5278 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005279 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005280 continue;
5281 }
Bookatzc8c44962017-05-11 12:12:54 -07005282
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005283 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07005284
5285 pw.print(prefix);
5286 pw.print(" ");
5287 UserHandle.formatUid(pw, uid);
5288 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005289 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005290
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005291 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
5292 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
5293 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
5294 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005295 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
5296 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
5297
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005298 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
5299 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005300 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
5301 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005302
5303 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
5304 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
5305
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005306 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
5307 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
5308 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08005309 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
5310 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5311 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
5312 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005313 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005314
Adam Lesinski5f056f62016-07-14 16:56:08 -07005315 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
5316 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
5317
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005318 if (mobileRxBytes > 0 || mobileTxBytes > 0
5319 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005320 pw.print(prefix); pw.print(" Mobile network: ");
5321 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005322 pw.print(formatBytesLocked(mobileTxBytes));
5323 pw.print(" sent (packets "); pw.print(mobileRxPackets);
5324 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005325 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005326 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
5327 sb.setLength(0);
5328 sb.append(prefix); sb.append(" Mobile radio active: ");
5329 formatTimeMs(sb, uidMobileActiveTime / 1000);
5330 sb.append("(");
5331 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
5332 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
5333 long packets = mobileRxPackets + mobileTxPackets;
5334 if (packets == 0) {
5335 packets = 1;
5336 }
5337 sb.append(" @ ");
5338 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
5339 sb.append(" mspp");
5340 pw.println(sb.toString());
5341 }
5342
Adam Lesinski5f056f62016-07-14 16:56:08 -07005343 if (mobileWakeup > 0) {
5344 sb.setLength(0);
5345 sb.append(prefix);
5346 sb.append(" Mobile radio AP wakeups: ");
5347 sb.append(mobileWakeup);
5348 pw.println(sb.toString());
5349 }
5350
Siddharth Rayb50a6842017-12-14 15:15:28 -08005351 printControllerActivityIfInteresting(pw, sb, prefix + " ",
5352 CELLULAR_CONTROLLER_NAME, u.getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005353
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005354 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005355 pw.print(prefix); pw.print(" Wi-Fi network: ");
5356 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005357 pw.print(formatBytesLocked(wifiTxBytes));
5358 pw.print(" sent (packets "); pw.print(wifiRxPackets);
5359 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005360 }
5361
Dianne Hackborn62793e42015-03-09 11:15:41 -07005362 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08005363 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005364 || uidWifiRunningTime != 0) {
5365 sb.setLength(0);
5366 sb.append(prefix); sb.append(" Wifi Running: ");
5367 formatTimeMs(sb, uidWifiRunningTime / 1000);
5368 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
5369 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07005370 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005371 formatTimeMs(sb, fullWifiLockOnTime / 1000);
5372 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
5373 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08005374 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005375 formatTimeMs(sb, wifiScanTime / 1000);
5376 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07005377 whichBatteryRealtime)); sb.append(") ");
5378 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08005379 sb.append("x\n");
5380 // actual and background times are unpooled and since reset (regardless of 'which')
5381 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
5382 formatTimeMs(sb, wifiScanActualTime / 1000);
5383 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
5384 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5385 sb.append(") ");
5386 sb.append(wifiScanCount);
5387 sb.append("x\n");
5388 sb.append(prefix); sb.append(" Background Wifi Scan: ");
5389 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
5390 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
5391 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5392 sb.append(") ");
5393 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07005394 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005395 pw.println(sb.toString());
5396 }
5397
Adam Lesinski5f056f62016-07-14 16:56:08 -07005398 if (wifiWakeup > 0) {
5399 sb.setLength(0);
5400 sb.append(prefix);
5401 sb.append(" WiFi AP wakeups: ");
5402 sb.append(wifiWakeup);
5403 pw.println(sb.toString());
5404 }
5405
Siddharth Rayb50a6842017-12-14 15:15:28 -08005406 printControllerActivityIfInteresting(pw, sb, prefix + " ", WIFI_CONTROLLER_NAME,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005407 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07005408
Adam Lesinski50e47602015-12-04 17:04:54 -08005409 if (btRxBytes > 0 || btTxBytes > 0) {
5410 pw.print(prefix); pw.print(" Bluetooth network: ");
5411 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
5412 pw.print(formatBytesLocked(btTxBytes));
5413 pw.println(" sent");
5414 }
5415
Bookatz867c0d72017-03-07 18:23:42 -08005416 final Timer bleTimer = u.getBluetoothScanTimer();
5417 if (bleTimer != null) {
5418 // Convert from microseconds to milliseconds with rounding
5419 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
5420 / 1000;
5421 if (totalTimeMs != 0) {
5422 final int count = bleTimer.getCountLocked(which);
5423 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
5424 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005425 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5426 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
5427 final long actualTimeMsBg = bleTimerBg != null ?
5428 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005429 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07005430 final int resultCount = u.getBluetoothScanResultCounter() != null ?
5431 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005432 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
5433 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
5434 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
5435 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
5436 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
5437 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5438 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
5439 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
5440 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
5441 final Timer unoptimizedScanTimerBg =
5442 u.getBluetoothUnoptimizedScanBackgroundTimer();
5443 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
5444 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5445 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
5446 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005447
5448 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08005449 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07005450 sb.append(prefix);
5451 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08005452 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07005453 sb.append(" (");
5454 sb.append(count);
5455 sb.append(" times)");
5456 if (bleTimer.isRunningLocked()) {
5457 sb.append(" (currently running)");
5458 }
5459 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005460 }
Bookatzb1f04f32017-05-19 13:57:32 -07005461
5462 sb.append(prefix);
5463 sb.append(" Bluetooth Scan (total actual realtime): ");
5464 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
5465 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005466 sb.append(count);
5467 sb.append(" times)");
5468 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07005469 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08005470 }
Bookatzb1f04f32017-05-19 13:57:32 -07005471 sb.append("\n");
5472 if (actualTimeMsBg > 0 || countBg > 0) {
5473 sb.append(prefix);
5474 sb.append(" Bluetooth Scan (background realtime): ");
5475 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
5476 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005477 sb.append(countBg);
5478 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07005479 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
5480 sb.append(" (currently running in background)");
5481 }
5482 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005483 }
Bookatzb1f04f32017-05-19 13:57:32 -07005484
5485 sb.append(prefix);
5486 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07005487 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07005488 sb.append(" (");
5489 sb.append(resultCountBg);
5490 sb.append(" in background)");
5491
5492 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
5493 sb.append("\n");
5494 sb.append(prefix);
5495 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
5496 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
5497 sb.append(" (max ");
5498 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
5499 sb.append(")");
5500 if (unoptimizedScanTimer != null
5501 && unoptimizedScanTimer.isRunningLocked()) {
5502 sb.append(" (currently running unoptimized)");
5503 }
5504 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
5505 sb.append("\n");
5506 sb.append(prefix);
5507 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
5508 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
5509 sb.append(" (max ");
5510 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
5511 sb.append(")");
5512 if (unoptimizedScanTimerBg.isRunningLocked()) {
5513 sb.append(" (currently running unoptimized in background)");
5514 }
5515 }
5516 }
Bookatz867c0d72017-03-07 18:23:42 -08005517 pw.println(sb.toString());
5518 uidActivity = true;
5519 }
5520 }
5521
5522
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005523
Dianne Hackborn617f8772009-03-31 15:04:46 -07005524 if (u.hasUserActivity()) {
5525 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07005526 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005527 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005528 if (val != 0) {
5529 if (!hasData) {
5530 sb.setLength(0);
5531 sb.append(" User activity: ");
5532 hasData = true;
5533 } else {
5534 sb.append(", ");
5535 }
5536 sb.append(val);
5537 sb.append(" ");
5538 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
5539 }
5540 }
5541 if (hasData) {
5542 pw.println(sb.toString());
5543 }
5544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005545
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005546 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
5547 = u.getWakelockStats();
5548 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005549 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005550 int countWakelock = 0;
5551 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
5552 final Uid.Wakelock wl = wakelocks.valueAt(iw);
5553 String linePrefix = ": ";
5554 sb.setLength(0);
5555 sb.append(prefix);
5556 sb.append(" Wake lock ");
5557 sb.append(wakelocks.keyAt(iw));
5558 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
5559 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005560 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
5561 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005562 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005563 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
5564 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005565 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
5566 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005567 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
5568 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07005569 sb.append(" realtime");
5570 pw.println(sb.toString());
5571 uidActivity = true;
5572 countWakelock++;
5573
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005574 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
5575 rawRealtime, which);
5576 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
5577 rawRealtime, which);
5578 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
5579 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005580 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07005581 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005582 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005583 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07005584 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
5585 // pooled and therefore just a lower bound)
5586 long actualTotalPartialWakelock = 0;
5587 long actualBgPartialWakelock = 0;
5588 if (u.getAggregatedPartialWakelockTimer() != null) {
5589 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
5590 // Convert from microseconds to milliseconds with rounding
5591 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07005592 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07005593 final Timer bgAggTimer = aggTimer.getSubTimer();
5594 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005595 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07005596 }
5597
5598 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
5599 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
5600 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005601 sb.setLength(0);
5602 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005603 sb.append(" TOTAL wake: ");
5604 boolean needComma = false;
5605 if (totalFullWakelock != 0) {
5606 needComma = true;
5607 formatTimeMs(sb, totalFullWakelock);
5608 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005609 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005610 if (totalPartialWakelock != 0) {
5611 if (needComma) {
5612 sb.append(", ");
5613 }
5614 needComma = true;
5615 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07005616 sb.append("blamed partial");
5617 }
5618 if (actualTotalPartialWakelock != 0) {
5619 if (needComma) {
5620 sb.append(", ");
5621 }
5622 needComma = true;
5623 formatTimeMs(sb, actualTotalPartialWakelock);
5624 sb.append("actual partial");
5625 }
5626 if (actualBgPartialWakelock != 0) {
5627 if (needComma) {
5628 sb.append(", ");
5629 }
5630 needComma = true;
5631 formatTimeMs(sb, actualBgPartialWakelock);
5632 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005633 }
5634 if (totalWindowWakelock != 0) {
5635 if (needComma) {
5636 sb.append(", ");
5637 }
5638 needComma = true;
5639 formatTimeMs(sb, totalWindowWakelock);
5640 sb.append("window");
5641 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005642 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07005643 if (needComma) {
5644 sb.append(",");
5645 }
5646 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005647 formatTimeMs(sb, totalDrawWakelock);
5648 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07005649 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005650 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005651 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005652 }
5653 }
5654
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07005655 // Calculate multicast wakelock stats
5656 final Timer mcTimer = u.getMulticastWakelockStats();
5657 if (mcTimer != null) {
5658 final long multicastWakeLockTimeMicros = mcTimer.getTotalTimeLocked(rawRealtime, which);
5659 final int multicastWakeLockCount = mcTimer.getCountLocked(which);
5660
5661 if (multicastWakeLockTimeMicros > 0) {
5662 sb.setLength(0);
5663 sb.append(prefix);
5664 sb.append(" WiFi Multicast Wakelock");
5665 sb.append(" count = ");
5666 sb.append(multicastWakeLockCount);
5667 sb.append(" time = ");
5668 formatTimeMsNoSpace(sb, (multicastWakeLockTimeMicros + 500) / 1000);
5669 pw.println(sb.toString());
5670 }
5671 }
5672
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005673 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
5674 for (int isy=syncs.size()-1; isy>=0; isy--) {
5675 final Timer timer = syncs.valueAt(isy);
5676 // Convert from microseconds to milliseconds with rounding
5677 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5678 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07005679 final Timer bgTimer = timer.getSubTimer();
5680 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005681 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07005682 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005683 sb.setLength(0);
5684 sb.append(prefix);
5685 sb.append(" Sync ");
5686 sb.append(syncs.keyAt(isy));
5687 sb.append(": ");
5688 if (totalTime != 0) {
5689 formatTimeMs(sb, totalTime);
5690 sb.append("realtime (");
5691 sb.append(count);
5692 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07005693 if (bgTime > 0) {
5694 sb.append(", ");
5695 formatTimeMs(sb, bgTime);
5696 sb.append("background (");
5697 sb.append(bgCount);
5698 sb.append(" times)");
5699 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005700 } else {
5701 sb.append("(not used)");
5702 }
5703 pw.println(sb.toString());
5704 uidActivity = true;
5705 }
5706
5707 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
5708 for (int ij=jobs.size()-1; ij>=0; ij--) {
5709 final Timer timer = jobs.valueAt(ij);
5710 // Convert from microseconds to milliseconds with rounding
5711 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5712 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07005713 final Timer bgTimer = timer.getSubTimer();
5714 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005715 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07005716 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005717 sb.setLength(0);
5718 sb.append(prefix);
5719 sb.append(" Job ");
5720 sb.append(jobs.keyAt(ij));
5721 sb.append(": ");
5722 if (totalTime != 0) {
5723 formatTimeMs(sb, totalTime);
5724 sb.append("realtime (");
5725 sb.append(count);
5726 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07005727 if (bgTime > 0) {
5728 sb.append(", ");
5729 formatTimeMs(sb, bgTime);
5730 sb.append("background (");
5731 sb.append(bgCount);
5732 sb.append(" times)");
5733 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005734 } else {
5735 sb.append("(not used)");
5736 }
5737 pw.println(sb.toString());
5738 uidActivity = true;
5739 }
5740
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005741 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
5742 for (int ic=completions.size()-1; ic>=0; ic--) {
5743 SparseIntArray types = completions.valueAt(ic);
5744 if (types != null) {
5745 pw.print(prefix);
5746 pw.print(" Job Completions ");
5747 pw.print(completions.keyAt(ic));
5748 pw.print(":");
5749 for (int it=0; it<types.size(); it++) {
5750 pw.print(" ");
5751 pw.print(JobParameters.getReasonName(types.keyAt(it)));
5752 pw.print("(");
5753 pw.print(types.valueAt(it));
5754 pw.print("x)");
5755 }
5756 pw.println();
5757 }
5758 }
5759
Amith Yamasani977e11f2018-02-16 11:29:54 -08005760 u.getDeferredJobsLineLocked(sb, which);
5761 if (sb.length() > 0) {
5762 pw.print(" Jobs deferred on launch "); pw.println(sb.toString());
5763 }
5764
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005765 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
5766 prefix, "Flashlight");
5767 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
5768 prefix, "Camera");
5769 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
5770 prefix, "Video");
5771 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
5772 prefix, "Audio");
5773
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005774 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
5775 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005776 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005777 final Uid.Sensor se = sensors.valueAt(ise);
5778 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005779 sb.setLength(0);
5780 sb.append(prefix);
5781 sb.append(" Sensor ");
5782 int handle = se.getHandle();
5783 if (handle == Uid.Sensor.GPS) {
5784 sb.append("GPS");
5785 } else {
5786 sb.append(handle);
5787 }
5788 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005789
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005790 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005791 if (timer != null) {
5792 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005793 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5794 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005795 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005796 final Timer bgTimer = se.getSensorBackgroundTime();
5797 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005798 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5799 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5800 final long bgActualTime = bgTimer != null ?
5801 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5802
Dianne Hackborn61659e52014-07-09 16:13:01 -07005803 //timer.logState();
5804 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005805 if (actualTime != totalTime) {
5806 formatTimeMs(sb, totalTime);
5807 sb.append("blamed realtime, ");
5808 }
5809
5810 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005811 sb.append("realtime (");
5812 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005813 sb.append(" times)");
5814
5815 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005816 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005817 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5818 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005819 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005820 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005822 } else {
5823 sb.append("(not used)");
5824 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005825 } else {
5826 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005827 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005828
5829 pw.println(sb.toString());
5830 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005831 }
5832
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005833 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5834 "Vibrator");
5835 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5836 prefix, "Foreground activities");
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005837 uidActivity |= printTimer(pw, sb, u.getForegroundServiceTimer(), rawRealtime, which,
5838 prefix, "Foreground services");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005839
Dianne Hackborn61659e52014-07-09 16:13:01 -07005840 long totalStateTime = 0;
5841 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5842 long time = u.getProcessStateTime(ips, rawRealtime, which);
5843 if (time > 0) {
5844 totalStateTime += time;
5845 sb.setLength(0);
5846 sb.append(prefix);
5847 sb.append(" ");
5848 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5849 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005850 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005851 pw.println(sb.toString());
5852 uidActivity = true;
5853 }
5854 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005855 if (totalStateTime > 0) {
5856 sb.setLength(0);
5857 sb.append(prefix);
5858 sb.append(" Total running: ");
5859 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5860 pw.println(sb.toString());
5861 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005862
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005863 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5864 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005865 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005866 sb.setLength(0);
5867 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005868 sb.append(" Total cpu time: u=");
5869 formatTimeMs(sb, userCpuTimeUs / 1000);
5870 sb.append("s=");
5871 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005872 pw.println(sb.toString());
5873 }
5874
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005875 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5876 if (cpuFreqTimes != null) {
5877 sb.setLength(0);
5878 sb.append(" Total cpu time per freq:");
5879 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5880 sb.append(" " + cpuFreqTimes[i]);
5881 }
5882 pw.println(sb.toString());
5883 }
5884 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5885 if (screenOffCpuFreqTimes != null) {
5886 sb.setLength(0);
5887 sb.append(" Total screen-off cpu time per freq:");
5888 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5889 sb.append(" " + screenOffCpuFreqTimes[i]);
5890 }
5891 pw.println(sb.toString());
5892 }
5893
Sudheer Shankab2f83c12017-11-13 19:25:01 -08005894 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
5895 final long[] cpuTimes = u.getCpuFreqTimes(which, procState);
5896 if (cpuTimes != null) {
5897 sb.setLength(0);
5898 sb.append(" Cpu times per freq at state "
5899 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5900 for (int i = 0; i < cpuTimes.length; ++i) {
5901 sb.append(" " + cpuTimes[i]);
5902 }
5903 pw.println(sb.toString());
5904 }
5905
5906 final long[] screenOffCpuTimes = u.getScreenOffCpuFreqTimes(which, procState);
5907 if (screenOffCpuTimes != null) {
5908 sb.setLength(0);
5909 sb.append(" Screen-off cpu times per freq at state "
5910 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5911 for (int i = 0; i < screenOffCpuTimes.length; ++i) {
5912 sb.append(" " + screenOffCpuTimes[i]);
5913 }
5914 pw.println(sb.toString());
5915 }
5916 }
5917
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005918 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5919 = u.getProcessStats();
5920 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5921 final Uid.Proc ps = processStats.valueAt(ipr);
5922 long userTime;
5923 long systemTime;
5924 long foregroundTime;
5925 int starts;
5926 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005927
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005928 userTime = ps.getUserTime(which);
5929 systemTime = ps.getSystemTime(which);
5930 foregroundTime = ps.getForegroundTime(which);
5931 starts = ps.getStarts(which);
5932 final int numCrashes = ps.getNumCrashes(which);
5933 final int numAnrs = ps.getNumAnrs(which);
5934 numExcessive = which == STATS_SINCE_CHARGED
5935 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005936
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005937 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5938 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5939 sb.setLength(0);
5940 sb.append(prefix); sb.append(" Proc ");
5941 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5942 sb.append(prefix); sb.append(" CPU: ");
5943 formatTimeMs(sb, userTime); sb.append("usr + ");
5944 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5945 formatTimeMs(sb, foregroundTime); sb.append("fg");
5946 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5947 sb.append("\n"); sb.append(prefix); sb.append(" ");
5948 boolean hasOne = false;
5949 if (starts != 0) {
5950 hasOne = true;
5951 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005952 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005953 if (numCrashes != 0) {
5954 if (hasOne) {
5955 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005956 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005957 hasOne = true;
5958 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005959 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005960 if (numAnrs != 0) {
5961 if (hasOne) {
5962 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005963 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005964 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005965 }
5966 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005967 pw.println(sb.toString());
5968 for (int e=0; e<numExcessive; e++) {
5969 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5970 if (ew != null) {
5971 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07005972 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005973 pw.print("cpu");
5974 } else {
5975 pw.print("unknown");
5976 }
5977 pw.print(" use: ");
5978 TimeUtils.formatDuration(ew.usedTime, pw);
5979 pw.print(" over ");
5980 TimeUtils.formatDuration(ew.overTime, pw);
5981 if (ew.overTime != 0) {
5982 pw.print(" (");
5983 pw.print((ew.usedTime*100)/ew.overTime);
5984 pw.println("%)");
5985 }
5986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005987 }
5988 uidActivity = true;
5989 }
5990 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005991
5992 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5993 = u.getPackageStats();
5994 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
5995 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
5996 pw.println(":");
5997 boolean apkActivity = false;
5998 final Uid.Pkg ps = packageStats.valueAt(ipkg);
5999 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
6000 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
6001 pw.print(prefix); pw.print(" Wakeup alarm ");
6002 pw.print(alarms.keyAt(iwa)); pw.print(": ");
6003 pw.print(alarms.valueAt(iwa).getCountLocked(which));
6004 pw.println(" times");
6005 apkActivity = true;
6006 }
6007 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
6008 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
6009 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
6010 final long startTime = ss.getStartTime(batteryUptime, which);
6011 final int starts = ss.getStarts(which);
6012 final int launches = ss.getLaunches(which);
6013 if (startTime != 0 || starts != 0 || launches != 0) {
6014 sb.setLength(0);
6015 sb.append(prefix); sb.append(" Service ");
6016 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
6017 sb.append(prefix); sb.append(" Created for: ");
6018 formatTimeMs(sb, startTime / 1000);
6019 sb.append("uptime\n");
6020 sb.append(prefix); sb.append(" Starts: ");
6021 sb.append(starts);
6022 sb.append(", launches: "); sb.append(launches);
6023 pw.println(sb.toString());
6024 apkActivity = true;
6025 }
6026 }
6027 if (!apkActivity) {
6028 pw.print(prefix); pw.println(" (nothing executed)");
6029 }
6030 uidActivity = true;
6031 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006032 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006033 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006034 }
6035 }
6036 }
6037
Kweku Adams71a95312018-04-16 16:54:24 -07006038 static void printBitDescriptions(StringBuilder sb, int oldval, int newval,
6039 HistoryTag wakelockTag, BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006040 int diff = oldval ^ newval;
6041 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006042 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006043 for (int i=0; i<descriptions.length; i++) {
6044 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006045 if ((diff&bd.mask) != 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006046 sb.append(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006047 if (bd.shift < 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006048 sb.append((newval & bd.mask) != 0 ? "+" : "-");
6049 sb.append(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006050 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
6051 didWake = true;
Kweku Adams71a95312018-04-16 16:54:24 -07006052 sb.append("=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006053 if (longNames) {
Kweku Adams71a95312018-04-16 16:54:24 -07006054 UserHandle.formatUid(sb, wakelockTag.uid);
6055 sb.append(":\"");
6056 sb.append(wakelockTag.string);
6057 sb.append("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006058 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006059 sb.append(wakelockTag.poolIdx);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006060 }
6061 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006062 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006063 sb.append(longNames ? bd.name : bd.shortName);
6064 sb.append("=");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006065 int val = (newval&bd.mask)>>bd.shift;
6066 if (bd.values != null && val >= 0 && val < bd.values.length) {
Kweku Adams71a95312018-04-16 16:54:24 -07006067 sb.append(longNames ? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006068 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006069 sb.append(val);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006070 }
6071 }
6072 }
6073 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006074 if (!didWake && wakelockTag != null) {
Kweku Adams71a95312018-04-16 16:54:24 -07006075 sb.append(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006076 if (longNames) {
Kweku Adams71a95312018-04-16 16:54:24 -07006077 UserHandle.formatUid(sb, wakelockTag.uid);
6078 sb.append(":\"");
6079 sb.append(wakelockTag.string);
6080 sb.append("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006081 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006082 sb.append(wakelockTag.poolIdx);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006083 }
6084 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006085 }
Mike Mac2f518a2017-09-19 16:06:03 -07006086
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006087 public void prepareForDumpLocked() {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006088 // We don't need to require subclasses implement this.
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006089 }
6090
6091 public static class HistoryPrinter {
6092 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006093 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006094 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006095 int oldStatus = -1;
6096 int oldHealth = -1;
6097 int oldPlug = -1;
6098 int oldTemp = -1;
6099 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006100 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006101 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006102
Dianne Hackborn3251b902014-06-20 14:40:53 -07006103 void reset() {
6104 oldState = oldState2 = 0;
6105 oldLevel = -1;
6106 oldStatus = -1;
6107 oldHealth = -1;
6108 oldPlug = -1;
6109 oldTemp = -1;
6110 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006111 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07006112 }
6113
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006114 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006115 boolean verbose) {
Kweku Adams71a95312018-04-16 16:54:24 -07006116 pw.print(printNextItem(rec, baseTime, checkin, verbose));
6117 }
6118
6119 /** Print the next history item to proto. */
6120 public void printNextItem(ProtoOutputStream proto, HistoryItem rec, long baseTime,
6121 boolean verbose) {
6122 String item = printNextItem(rec, baseTime, true, verbose);
6123 for (String line : item.split("\n")) {
6124 proto.write(BatteryStatsServiceDumpHistoryProto.CSV_LINES, line);
6125 }
6126 }
6127
6128 private String printNextItem(HistoryItem rec, long baseTime, boolean checkin,
6129 boolean verbose) {
6130 StringBuilder item = new StringBuilder();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006131 if (!checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006132 item.append(" ");
6133 TimeUtils.formatDuration(
6134 rec.time - baseTime, item, TimeUtils.HUNDRED_DAY_FIELD_LEN);
6135 item.append(" (");
6136 item.append(rec.numReadInts);
6137 item.append(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006138 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006139 item.append(BATTERY_STATS_CHECKIN_VERSION); item.append(',');
6140 item.append(HISTORY_DATA); item.append(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006141 if (lastTime < 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006142 item.append(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006143 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006144 item.append(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006145 }
6146 lastTime = rec.time;
6147 }
6148 if (rec.cmd == HistoryItem.CMD_START) {
6149 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006150 item.append(":");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006151 }
Kweku Adams71a95312018-04-16 16:54:24 -07006152 item.append("START\n");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006153 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006154 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
6155 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006156 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006157 item.append(":");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006158 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006159 if (rec.cmd == HistoryItem.CMD_RESET) {
Kweku Adams71a95312018-04-16 16:54:24 -07006160 item.append("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006161 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006162 }
Kweku Adams71a95312018-04-16 16:54:24 -07006163 item.append("TIME:");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006164 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006165 item.append(rec.currentTime);
6166 item.append("\n");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006167 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006168 item.append(" ");
6169 item.append(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006170 rec.currentTime).toString());
Kweku Adams71a95312018-04-16 16:54:24 -07006171 item.append("\n");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006172 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006173 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
6174 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006175 item.append(":");
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006176 }
Kweku Adams71a95312018-04-16 16:54:24 -07006177 item.append("SHUTDOWN\n");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006178 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
6179 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006180 item.append(":");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006181 }
Kweku Adams71a95312018-04-16 16:54:24 -07006182 item.append("*OVERFLOW*\n");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006183 } else {
6184 if (!checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006185 if (rec.batteryLevel < 10) item.append("00");
6186 else if (rec.batteryLevel < 100) item.append("0");
6187 item.append(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006188 if (verbose) {
Kweku Adams71a95312018-04-16 16:54:24 -07006189 item.append(" ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006190 if (rec.states < 0) ;
Kweku Adams71a95312018-04-16 16:54:24 -07006191 else if (rec.states < 0x10) item.append("0000000");
6192 else if (rec.states < 0x100) item.append("000000");
6193 else if (rec.states < 0x1000) item.append("00000");
6194 else if (rec.states < 0x10000) item.append("0000");
6195 else if (rec.states < 0x100000) item.append("000");
6196 else if (rec.states < 0x1000000) item.append("00");
6197 else if (rec.states < 0x10000000) item.append("0");
6198 item.append(Integer.toHexString(rec.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006199 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006200 } else {
6201 if (oldLevel != rec.batteryLevel) {
6202 oldLevel = rec.batteryLevel;
Kweku Adams71a95312018-04-16 16:54:24 -07006203 item.append(",Bl="); item.append(rec.batteryLevel);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006204 }
6205 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006206 if (oldStatus != rec.batteryStatus) {
6207 oldStatus = rec.batteryStatus;
Kweku Adams71a95312018-04-16 16:54:24 -07006208 item.append(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006209 switch (oldStatus) {
6210 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Kweku Adams71a95312018-04-16 16:54:24 -07006211 item.append(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006212 break;
6213 case BatteryManager.BATTERY_STATUS_CHARGING:
Kweku Adams71a95312018-04-16 16:54:24 -07006214 item.append(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006215 break;
6216 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Kweku Adams71a95312018-04-16 16:54:24 -07006217 item.append(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006218 break;
6219 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Kweku Adams71a95312018-04-16 16:54:24 -07006220 item.append(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006221 break;
6222 case BatteryManager.BATTERY_STATUS_FULL:
Kweku Adams71a95312018-04-16 16:54:24 -07006223 item.append(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006224 break;
6225 default:
Kweku Adams71a95312018-04-16 16:54:24 -07006226 item.append(oldStatus);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006227 break;
6228 }
6229 }
6230 if (oldHealth != rec.batteryHealth) {
6231 oldHealth = rec.batteryHealth;
Kweku Adams71a95312018-04-16 16:54:24 -07006232 item.append(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006233 switch (oldHealth) {
6234 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Kweku Adams71a95312018-04-16 16:54:24 -07006235 item.append(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006236 break;
6237 case BatteryManager.BATTERY_HEALTH_GOOD:
Kweku Adams71a95312018-04-16 16:54:24 -07006238 item.append(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006239 break;
6240 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Kweku Adams71a95312018-04-16 16:54:24 -07006241 item.append(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006242 break;
6243 case BatteryManager.BATTERY_HEALTH_DEAD:
Kweku Adams71a95312018-04-16 16:54:24 -07006244 item.append(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006245 break;
6246 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Kweku Adams71a95312018-04-16 16:54:24 -07006247 item.append(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006248 break;
6249 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Kweku Adams71a95312018-04-16 16:54:24 -07006250 item.append(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006251 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006252 case BatteryManager.BATTERY_HEALTH_COLD:
Kweku Adams71a95312018-04-16 16:54:24 -07006253 item.append(checkin ? "c" : "cold");
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006254 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006255 default:
Kweku Adams71a95312018-04-16 16:54:24 -07006256 item.append(oldHealth);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006257 break;
6258 }
6259 }
6260 if (oldPlug != rec.batteryPlugType) {
6261 oldPlug = rec.batteryPlugType;
Kweku Adams71a95312018-04-16 16:54:24 -07006262 item.append(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006263 switch (oldPlug) {
6264 case 0:
Kweku Adams71a95312018-04-16 16:54:24 -07006265 item.append(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006266 break;
6267 case BatteryManager.BATTERY_PLUGGED_AC:
Kweku Adams71a95312018-04-16 16:54:24 -07006268 item.append(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006269 break;
6270 case BatteryManager.BATTERY_PLUGGED_USB:
Kweku Adams71a95312018-04-16 16:54:24 -07006271 item.append(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006272 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006273 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Kweku Adams71a95312018-04-16 16:54:24 -07006274 item.append(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006275 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006276 default:
Kweku Adams71a95312018-04-16 16:54:24 -07006277 item.append(oldPlug);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006278 break;
6279 }
6280 }
6281 if (oldTemp != rec.batteryTemperature) {
6282 oldTemp = rec.batteryTemperature;
Kweku Adams71a95312018-04-16 16:54:24 -07006283 item.append(checkin ? ",Bt=" : " temp=");
6284 item.append(oldTemp);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006285 }
6286 if (oldVolt != rec.batteryVoltage) {
6287 oldVolt = rec.batteryVoltage;
Kweku Adams71a95312018-04-16 16:54:24 -07006288 item.append(checkin ? ",Bv=" : " volt=");
6289 item.append(oldVolt);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006290 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006291 final int chargeMAh = rec.batteryChargeUAh / 1000;
6292 if (oldChargeMAh != chargeMAh) {
6293 oldChargeMAh = chargeMAh;
Kweku Adams71a95312018-04-16 16:54:24 -07006294 item.append(checkin ? ",Bcc=" : " charge=");
6295 item.append(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07006296 }
Kweku Adams71a95312018-04-16 16:54:24 -07006297 printBitDescriptions(item, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006298 HISTORY_STATE_DESCRIPTIONS, !checkin);
Kweku Adams71a95312018-04-16 16:54:24 -07006299 printBitDescriptions(item, oldState2, rec.states2, null,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006300 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006301 if (rec.wakeReasonTag != null) {
6302 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006303 item.append(",wr=");
6304 item.append(rec.wakeReasonTag.poolIdx);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006305 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006306 item.append(" wake_reason=");
6307 item.append(rec.wakeReasonTag.uid);
6308 item.append(":\"");
6309 item.append(rec.wakeReasonTag.string);
6310 item.append("\"");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006311 }
6312 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08006313 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Kweku Adams71a95312018-04-16 16:54:24 -07006314 item.append(checkin ? "," : " ");
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006315 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006316 item.append("+");
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006317 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006318 item.append("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006319 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006320 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
6321 : HISTORY_EVENT_NAMES;
6322 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
6323 | HistoryItem.EVENT_FLAG_FINISH);
6324 if (idx >= 0 && idx < eventNames.length) {
Kweku Adams71a95312018-04-16 16:54:24 -07006325 item.append(eventNames[idx]);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006326 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006327 item.append(checkin ? "Ev" : "event");
6328 item.append(idx);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006329 }
Kweku Adams71a95312018-04-16 16:54:24 -07006330 item.append("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006331 if (checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006332 item.append(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08006333 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006334 item.append(HISTORY_EVENT_INT_FORMATTERS[idx]
Adam Lesinski041d9172016-12-12 12:03:56 -08006335 .applyAsString(rec.eventTag.uid));
Kweku Adams71a95312018-04-16 16:54:24 -07006336 item.append(":\"");
6337 item.append(rec.eventTag.string);
6338 item.append("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006339 }
6340 }
Kweku Adams71a95312018-04-16 16:54:24 -07006341 item.append("\n");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006342 if (rec.stepDetails != null) {
6343 if (!checkin) {
Kweku Adams71a95312018-04-16 16:54:24 -07006344 item.append(" Details: cpu=");
6345 item.append(rec.stepDetails.userTime);
6346 item.append("u+");
6347 item.append(rec.stepDetails.systemTime);
6348 item.append("s");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006349 if (rec.stepDetails.appCpuUid1 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006350 item.append(" (");
6351 printStepCpuUidDetails(item, rec.stepDetails.appCpuUid1,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006352 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6353 if (rec.stepDetails.appCpuUid2 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006354 item.append(", ");
6355 printStepCpuUidDetails(item, rec.stepDetails.appCpuUid2,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006356 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6357 }
6358 if (rec.stepDetails.appCpuUid3 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006359 item.append(", ");
6360 printStepCpuUidDetails(item, rec.stepDetails.appCpuUid3,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006361 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6362 }
Kweku Adams71a95312018-04-16 16:54:24 -07006363 item.append(')');
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006364 }
Kweku Adams71a95312018-04-16 16:54:24 -07006365 item.append("\n");
6366 item.append(" /proc/stat=");
6367 item.append(rec.stepDetails.statUserTime);
6368 item.append(" usr, ");
6369 item.append(rec.stepDetails.statSystemTime);
6370 item.append(" sys, ");
6371 item.append(rec.stepDetails.statIOWaitTime);
6372 item.append(" io, ");
6373 item.append(rec.stepDetails.statIrqTime);
6374 item.append(" irq, ");
6375 item.append(rec.stepDetails.statSoftIrqTime);
6376 item.append(" sirq, ");
6377 item.append(rec.stepDetails.statIdlTime);
6378 item.append(" idle");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006379 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
6380 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
6381 + rec.stepDetails.statSoftIrqTime;
6382 int total = totalRun + rec.stepDetails.statIdlTime;
6383 if (total > 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006384 item.append(" (");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006385 float perc = ((float)totalRun) / ((float)total) * 100;
Kweku Adams71a95312018-04-16 16:54:24 -07006386 item.append(String.format("%.1f%%", perc));
6387 item.append(" of ");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006388 StringBuilder sb = new StringBuilder(64);
6389 formatTimeMsNoSpace(sb, total*10);
Kweku Adams71a95312018-04-16 16:54:24 -07006390 item.append(sb);
6391 item.append(")");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006392 }
Kweku Adams71a95312018-04-16 16:54:24 -07006393 item.append(", PlatformIdleStat ");
6394 item.append(rec.stepDetails.statPlatformIdleState);
6395 item.append("\n");
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006396
Kweku Adams71a95312018-04-16 16:54:24 -07006397 item.append(", SubsystemPowerState ");
6398 item.append(rec.stepDetails.statSubsystemPowerState);
6399 item.append("\n");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006400 } else {
Kweku Adams71a95312018-04-16 16:54:24 -07006401 item.append(BATTERY_STATS_CHECKIN_VERSION); item.append(',');
6402 item.append(HISTORY_DATA); item.append(",0,Dcpu=");
6403 item.append(rec.stepDetails.userTime);
6404 item.append(":");
6405 item.append(rec.stepDetails.systemTime);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006406 if (rec.stepDetails.appCpuUid1 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006407 printStepCpuUidCheckinDetails(item, rec.stepDetails.appCpuUid1,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006408 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6409 if (rec.stepDetails.appCpuUid2 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006410 printStepCpuUidCheckinDetails(item, rec.stepDetails.appCpuUid2,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006411 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6412 }
6413 if (rec.stepDetails.appCpuUid3 >= 0) {
Kweku Adams71a95312018-04-16 16:54:24 -07006414 printStepCpuUidCheckinDetails(item, rec.stepDetails.appCpuUid3,
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006415 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6416 }
6417 }
Kweku Adams71a95312018-04-16 16:54:24 -07006418 item.append("\n");
6419 item.append(BATTERY_STATS_CHECKIN_VERSION); item.append(',');
6420 item.append(HISTORY_DATA); item.append(",0,Dpst=");
6421 item.append(rec.stepDetails.statUserTime);
6422 item.append(',');
6423 item.append(rec.stepDetails.statSystemTime);
6424 item.append(',');
6425 item.append(rec.stepDetails.statIOWaitTime);
6426 item.append(',');
6427 item.append(rec.stepDetails.statIrqTime);
6428 item.append(',');
6429 item.append(rec.stepDetails.statSoftIrqTime);
6430 item.append(',');
6431 item.append(rec.stepDetails.statIdlTime);
6432 item.append(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006433 if (rec.stepDetails.statPlatformIdleState != null) {
Kweku Adams71a95312018-04-16 16:54:24 -07006434 item.append(rec.stepDetails.statPlatformIdleState);
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006435 if (rec.stepDetails.statSubsystemPowerState != null) {
Kweku Adams71a95312018-04-16 16:54:24 -07006436 item.append(',');
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006437 }
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006438 }
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006439
6440 if (rec.stepDetails.statSubsystemPowerState != null) {
Kweku Adams71a95312018-04-16 16:54:24 -07006441 item.append(rec.stepDetails.statSubsystemPowerState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006442 }
Kweku Adams71a95312018-04-16 16:54:24 -07006443 item.append("\n");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006444 }
6445 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006446 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006447 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006448 }
Kweku Adams71a95312018-04-16 16:54:24 -07006449
6450 return item.toString();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006451 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006452
Kweku Adams71a95312018-04-16 16:54:24 -07006453 private void printStepCpuUidDetails(StringBuilder sb, int uid, int utime, int stime) {
6454 UserHandle.formatUid(sb, uid);
6455 sb.append("=");
6456 sb.append(utime);
6457 sb.append("u+");
6458 sb.append(stime);
6459 sb.append("s");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006460 }
6461
Kweku Adams71a95312018-04-16 16:54:24 -07006462 private void printStepCpuUidCheckinDetails(StringBuilder sb, int uid, int utime,
6463 int stime) {
6464 sb.append('/');
6465 sb.append(uid);
6466 sb.append(":");
6467 sb.append(utime);
6468 sb.append(":");
6469 sb.append(stime);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006470 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006471 }
6472
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006473 private void printSizeValue(PrintWriter pw, long size) {
6474 float result = size;
6475 String suffix = "";
6476 if (result >= 10*1024) {
6477 suffix = "KB";
6478 result = result / 1024;
6479 }
6480 if (result >= 10*1024) {
6481 suffix = "MB";
6482 result = result / 1024;
6483 }
6484 if (result >= 10*1024) {
6485 suffix = "GB";
6486 result = result / 1024;
6487 }
6488 if (result >= 10*1024) {
6489 suffix = "TB";
6490 result = result / 1024;
6491 }
6492 if (result >= 10*1024) {
6493 suffix = "PB";
6494 result = result / 1024;
6495 }
6496 pw.print((int)result);
6497 pw.print(suffix);
6498 }
6499
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006500 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
6501 String label3, long estimatedTime) {
6502 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006503 return false;
6504 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006505 pw.print(label1);
6506 pw.print(label2);
6507 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006508 StringBuilder sb = new StringBuilder(64);
6509 formatTimeMs(sb, estimatedTime);
6510 pw.print(sb);
6511 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006512 return true;
6513 }
6514
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006515 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
6516 LevelStepTracker steps, boolean checkin) {
6517 if (steps == null) {
6518 return false;
6519 }
6520 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006521 if (count <= 0) {
6522 return false;
6523 }
6524 if (!checkin) {
6525 pw.println(header);
6526 }
Kweku Adams030980a2015-04-01 16:07:48 -07006527 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006528 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006529 long duration = steps.getDurationAt(i);
6530 int level = steps.getLevelAt(i);
6531 long initMode = steps.getInitModeAt(i);
6532 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006533 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006534 lineArgs[0] = Long.toString(duration);
6535 lineArgs[1] = Integer.toString(level);
6536 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6537 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6538 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
6539 case Display.STATE_ON: lineArgs[2] = "s+"; break;
6540 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
6541 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07006542 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006543 }
6544 } else {
6545 lineArgs[2] = "";
6546 }
6547 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6548 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
6549 } else {
6550 lineArgs[3] = "";
6551 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006552 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07006553 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006554 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07006555 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006556 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006557 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
6558 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006559 pw.print(prefix);
6560 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006561 TimeUtils.formatDuration(duration, pw);
6562 pw.print(" to "); pw.print(level);
6563 boolean haveModes = false;
6564 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6565 pw.print(" (");
6566 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6567 case Display.STATE_OFF: pw.print("screen-off"); break;
6568 case Display.STATE_ON: pw.print("screen-on"); break;
6569 case Display.STATE_DOZE: pw.print("screen-doze"); break;
6570 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07006571 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006572 }
6573 haveModes = true;
6574 }
6575 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6576 pw.print(haveModes ? ", " : " (");
6577 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
6578 ? "power-save-on" : "power-save-off");
6579 haveModes = true;
6580 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006581 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6582 pw.print(haveModes ? ", " : " (");
6583 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6584 ? "device-idle-on" : "device-idle-off");
6585 haveModes = true;
6586 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006587 if (haveModes) {
6588 pw.print(")");
6589 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006590 pw.println();
6591 }
6592 }
6593 return true;
6594 }
6595
Kweku Adams87b19ec2017-10-09 12:40:03 -07006596 private static void dumpDurationSteps(ProtoOutputStream proto, long fieldId,
6597 LevelStepTracker steps) {
6598 if (steps == null) {
6599 return;
6600 }
6601 int count = steps.mNumStepDurations;
Kweku Adams87b19ec2017-10-09 12:40:03 -07006602 for (int i = 0; i < count; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07006603 long token = proto.start(fieldId);
Kweku Adams87b19ec2017-10-09 12:40:03 -07006604 proto.write(SystemProto.BatteryLevelStep.DURATION_MS, steps.getDurationAt(i));
6605 proto.write(SystemProto.BatteryLevelStep.LEVEL, steps.getLevelAt(i));
6606
6607 final long initMode = steps.getInitModeAt(i);
6608 final long modMode = steps.getModModeAt(i);
6609
6610 int ds = SystemProto.BatteryLevelStep.DS_MIXED;
6611 if ((modMode & STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6612 switch ((int) (initMode & STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6613 case Display.STATE_OFF:
6614 ds = SystemProto.BatteryLevelStep.DS_OFF;
6615 break;
6616 case Display.STATE_ON:
6617 ds = SystemProto.BatteryLevelStep.DS_ON;
6618 break;
6619 case Display.STATE_DOZE:
6620 ds = SystemProto.BatteryLevelStep.DS_DOZE;
6621 break;
6622 case Display.STATE_DOZE_SUSPEND:
6623 ds = SystemProto.BatteryLevelStep.DS_DOZE_SUSPEND;
6624 break;
6625 default:
6626 ds = SystemProto.BatteryLevelStep.DS_ERROR;
6627 break;
6628 }
6629 }
6630 proto.write(SystemProto.BatteryLevelStep.DISPLAY_STATE, ds);
6631
6632 int psm = SystemProto.BatteryLevelStep.PSM_MIXED;
6633 if ((modMode & STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6634 psm = (initMode & STEP_LEVEL_MODE_POWER_SAVE) != 0
6635 ? SystemProto.BatteryLevelStep.PSM_ON : SystemProto.BatteryLevelStep.PSM_OFF;
6636 }
6637 proto.write(SystemProto.BatteryLevelStep.POWER_SAVE_MODE, psm);
6638
6639 int im = SystemProto.BatteryLevelStep.IM_MIXED;
6640 if ((modMode & STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6641 im = (initMode & STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6642 ? SystemProto.BatteryLevelStep.IM_ON : SystemProto.BatteryLevelStep.IM_OFF;
6643 }
6644 proto.write(SystemProto.BatteryLevelStep.IDLE_MODE, im);
6645
6646 proto.end(token);
6647 }
6648 }
6649
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006650 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006651 public static final int DUMP_DAILY_ONLY = 1<<2;
6652 public static final int DUMP_HISTORY_ONLY = 1<<3;
6653 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
6654 public static final int DUMP_VERBOSE = 1<<5;
6655 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006656
Dianne Hackborn37de0982014-05-09 09:32:18 -07006657 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
6658 final HistoryPrinter hprinter = new HistoryPrinter();
6659 final HistoryItem rec = new HistoryItem();
6660 long lastTime = -1;
6661 long baseTime = -1;
6662 boolean printed = false;
6663 HistoryEventTracker tracker = null;
6664 while (getNextHistoryLocked(rec)) {
6665 lastTime = rec.time;
6666 if (baseTime < 0) {
6667 baseTime = lastTime;
6668 }
6669 if (rec.time >= histStart) {
6670 if (histStart >= 0 && !printed) {
6671 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07006672 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006673 || rec.cmd == HistoryItem.CMD_START
6674 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006675 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006676 hprinter.printNextItem(pw, rec, baseTime, checkin,
6677 (flags&DUMP_VERBOSE) != 0);
6678 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006679 } else if (rec.currentTime != 0) {
6680 printed = true;
6681 byte cmd = rec.cmd;
6682 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006683 hprinter.printNextItem(pw, rec, baseTime, checkin,
6684 (flags&DUMP_VERBOSE) != 0);
6685 rec.cmd = cmd;
6686 }
6687 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006688 if (rec.cmd != HistoryItem.CMD_UPDATE) {
6689 hprinter.printNextItem(pw, rec, baseTime, checkin,
6690 (flags&DUMP_VERBOSE) != 0);
6691 rec.cmd = HistoryItem.CMD_UPDATE;
6692 }
6693 int oldEventCode = rec.eventCode;
6694 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006695 rec.eventTag = new HistoryTag();
6696 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
6697 HashMap<String, SparseIntArray> active
6698 = tracker.getStateForEvent(i);
6699 if (active == null) {
6700 continue;
6701 }
6702 for (HashMap.Entry<String, SparseIntArray> ent
6703 : active.entrySet()) {
6704 SparseIntArray uids = ent.getValue();
6705 for (int j=0; j<uids.size(); j++) {
6706 rec.eventCode = i;
6707 rec.eventTag.string = ent.getKey();
6708 rec.eventTag.uid = uids.keyAt(j);
6709 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006710 hprinter.printNextItem(pw, rec, baseTime, checkin,
6711 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006712 rec.wakeReasonTag = null;
6713 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006714 }
6715 }
6716 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006717 rec.eventCode = oldEventCode;
6718 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006719 tracker = null;
6720 }
6721 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006722 hprinter.printNextItem(pw, rec, baseTime, checkin,
6723 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07006724 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
6725 // This is an attempt to aggregate the previous state and generate
6726 // fake events to reflect that state at the point where we start
6727 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07006728 if (tracker == null) {
6729 tracker = new HistoryEventTracker();
6730 }
6731 tracker.updateState(rec.eventCode, rec.eventTag.string,
6732 rec.eventTag.uid, rec.eventTag.poolIdx);
6733 }
6734 }
6735 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07006736 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006737 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
6738 }
6739 }
6740
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006741 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
6742 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
6743 if (steps == null) {
6744 return;
6745 }
6746 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
6747 if (timeRemaining >= 0) {
6748 pw.print(prefix); pw.print(label); pw.print(" total time: ");
6749 tmpSb.setLength(0);
6750 formatTimeMs(tmpSb, timeRemaining);
6751 pw.print(tmpSb);
6752 pw.print(" (from "); pw.print(tmpOutInt[0]);
6753 pw.println(" steps)");
6754 }
6755 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6756 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6757 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
6758 if (estimatedTime > 0) {
6759 pw.print(prefix); pw.print(label); pw.print(" ");
6760 pw.print(STEP_LEVEL_MODE_LABELS[i]);
6761 pw.print(" time: ");
6762 tmpSb.setLength(0);
6763 formatTimeMs(tmpSb, estimatedTime);
6764 pw.print(tmpSb);
6765 pw.print(" (from "); pw.print(tmpOutInt[0]);
6766 pw.println(" steps)");
6767 }
6768 }
6769 }
6770
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006771 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
6772 ArrayList<PackageChange> changes) {
6773 if (changes == null) {
6774 return;
6775 }
6776 pw.print(prefix); pw.println("Package changes:");
6777 for (int i=0; i<changes.size(); i++) {
6778 PackageChange pc = changes.get(i);
6779 if (pc.mUpdate) {
6780 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
6781 pw.print(" vers="); pw.println(pc.mVersionCode);
6782 } else {
6783 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
6784 }
6785 }
6786 }
6787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006788 /**
6789 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
6790 *
6791 * @param pw a Printer to receive the dump output.
6792 */
6793 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006794 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006795 prepareForDumpLocked();
6796
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006797 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006798 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006799
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006800 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006801 final long historyTotalSize = getHistoryTotalSize();
6802 final long historyUsedSize = getHistoryUsedSize();
6803 if (startIteratingHistoryLocked()) {
6804 try {
6805 pw.print("Battery History (");
6806 pw.print((100*historyUsedSize)/historyTotalSize);
6807 pw.print("% used, ");
6808 printSizeValue(pw, historyUsedSize);
6809 pw.print(" used of ");
6810 printSizeValue(pw, historyTotalSize);
6811 pw.print(", ");
6812 pw.print(getHistoryStringPoolSize());
6813 pw.print(" strings using ");
6814 printSizeValue(pw, getHistoryStringPoolBytes());
6815 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07006816 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006817 pw.println();
6818 } finally {
6819 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006820 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006821 }
6822
6823 if (startIteratingOldHistoryLocked()) {
6824 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006825 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006826 pw.println("Old battery History:");
6827 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006828 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006829 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006830 if (baseTime < 0) {
6831 baseTime = rec.time;
6832 }
6833 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006834 }
6835 pw.println();
6836 } finally {
6837 finishIteratingOldHistoryLocked();
6838 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006839 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006840 }
6841
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006842 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006843 return;
6844 }
6845
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006846 if (!filtering) {
6847 SparseArray<? extends Uid> uidStats = getUidStats();
6848 final int NU = uidStats.size();
6849 boolean didPid = false;
6850 long nowRealtime = SystemClock.elapsedRealtime();
6851 for (int i=0; i<NU; i++) {
6852 Uid uid = uidStats.valueAt(i);
6853 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
6854 if (pids != null) {
6855 for (int j=0; j<pids.size(); j++) {
6856 Uid.Pid pid = pids.valueAt(j);
6857 if (!didPid) {
6858 pw.println("Per-PID Stats:");
6859 didPid = true;
6860 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006861 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
6862 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006863 pw.print(" PID "); pw.print(pids.keyAt(j));
6864 pw.print(" wake time: ");
6865 TimeUtils.formatDuration(time, pw);
6866 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006867 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006868 }
6869 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006870 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006871 pw.println();
6872 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006873 }
6874
6875 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006876 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
6877 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006878 long timeRemaining = computeBatteryTimeRemaining(
6879 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006880 if (timeRemaining >= 0) {
6881 pw.print(" Estimated discharge time remaining: ");
6882 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6883 pw.println();
6884 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006885 final LevelStepTracker steps = getDischargeLevelStepTracker();
6886 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6887 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
6888 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6889 STEP_LEVEL_MODE_VALUES[i], null));
6890 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006891 pw.println();
6892 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006893 if (dumpDurationSteps(pw, " ", "Charge step durations:",
6894 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006895 long timeRemaining = computeChargeTimeRemaining(
6896 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006897 if (timeRemaining >= 0) {
6898 pw.print(" Estimated charge time remaining: ");
6899 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6900 pw.println();
6901 }
6902 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006903 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006904 }
Dianne Hackbornc81983a2017-10-20 16:16:32 -07006905 if (!filtering || (flags & DUMP_DAILY_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006906 pw.println("Daily stats:");
6907 pw.print(" Current start time: ");
6908 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6909 getCurrentDailyStartTime()).toString());
6910 pw.print(" Next min deadline: ");
6911 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6912 getNextMinDailyDeadline()).toString());
6913 pw.print(" Next max deadline: ");
6914 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6915 getNextMaxDailyDeadline()).toString());
6916 StringBuilder sb = new StringBuilder(64);
6917 int[] outInt = new int[1];
6918 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
6919 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006920 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
6921 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006922 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006923 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
6924 dsteps, false)) {
6925 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6926 sb, outInt);
6927 }
6928 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6929 csteps, false)) {
6930 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6931 sb, outInt);
6932 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006933 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006934 } else {
6935 pw.println(" Current daily steps:");
6936 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6937 sb, outInt);
6938 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6939 sb, outInt);
6940 }
6941 }
6942 DailyItem dit;
6943 int curIndex = 0;
6944 while ((dit=getDailyItemLocked(curIndex)) != null) {
6945 curIndex++;
6946 if ((flags&DUMP_DAILY_ONLY) != 0) {
6947 pw.println();
6948 }
6949 pw.print(" Daily from ");
6950 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6951 pw.print(" to ");
6952 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6953 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006954 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006955 if (dumpDurationSteps(pw, " ",
6956 " Discharge step durations:", dit.mDischargeSteps, false)) {
6957 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6958 sb, outInt);
6959 }
6960 if (dumpDurationSteps(pw, " ",
6961 " Charge step durations:", dit.mChargeSteps, false)) {
6962 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6963 sb, outInt);
6964 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006965 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006966 } else {
6967 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6968 sb, outInt);
6969 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6970 sb, outInt);
6971 }
6972 }
6973 pw.println();
6974 }
6975 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006976 pw.println("Statistics since last charge:");
6977 pw.println(" System starts: " + getStartCount()
6978 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006979 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6980 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006981 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006983 }
Mike Mac2f518a2017-09-19 16:06:03 -07006984
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006985 // This is called from BatteryStatsService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006986 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006987 public void dumpCheckinLocked(Context context, PrintWriter pw,
6988 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006989 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006990
6991 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006992 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6993 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006994
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006995 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6996
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006997 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07006998 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006999 try {
7000 for (int i=0; i<getHistoryStringPoolSize(); i++) {
7001 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
7002 pw.print(HISTORY_STRING_POOL); pw.print(',');
7003 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007004 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007005 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007006 pw.print(",\"");
7007 String str = getHistoryTagPoolString(i);
7008 str = str.replace("\\", "\\\\");
7009 str = str.replace("\"", "\\\"");
7010 pw.print(str);
7011 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007012 pw.println();
7013 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07007014 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007015 } finally {
7016 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08007017 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07007018 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07007019 }
7020
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007021 if ((flags & DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007022 return;
7023 }
7024
Dianne Hackborne4a59512010-12-07 11:08:07 -08007025 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007026 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08007027 for (int i=0; i<apps.size(); i++) {
7028 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007029 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
7030 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08007031 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007032 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
7033 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007034 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007035 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007036 }
7037 SparseArray<? extends Uid> uidStats = getUidStats();
7038 final int NU = uidStats.size();
7039 String[] lineArgs = new String[2];
7040 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007041 int uid = UserHandle.getAppId(uidStats.keyAt(i));
7042 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
7043 if (pkgs != null && !pkgs.second.value) {
7044 pkgs.second.value = true;
7045 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08007046 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07007047 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007048 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
7049 (Object[])lineArgs);
7050 }
7051 }
7052 }
7053 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007054 if ((flags & DUMP_DAILY_ONLY) == 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007055 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007056 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07007057 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007058 if (timeRemaining >= 0) {
7059 lineArgs[0] = Long.toString(timeRemaining);
7060 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
7061 (Object[])lineArgs);
7062 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007063 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07007064 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07007065 if (timeRemaining >= 0) {
7066 lineArgs[0] = Long.toString(timeRemaining);
7067 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
7068 (Object[])lineArgs);
7069 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007070 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
7071 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08007072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007073 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007074
Kweku Adams71a95312018-04-16 16:54:24 -07007075 /**
7076 * Dump #STATS_SINCE_CHARGED batterystats data to a proto. If the flags include
7077 * DUMP_INCLUDE_HISTORY or DUMP_HISTORY_ONLY, only the history will be dumped.
7078 * @hide
7079 */
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007080 public void dumpProtoLocked(Context context, FileDescriptor fd, List<ApplicationInfo> apps,
Kweku Adams71a95312018-04-16 16:54:24 -07007081 int flags, long histStart) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007082 final ProtoOutputStream proto = new ProtoOutputStream(fd);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007083 prepareForDumpLocked();
7084
Kweku Adams71a95312018-04-16 16:54:24 -07007085 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
7086 dumpProtoHistoryLocked(proto, flags, histStart);
7087 proto.flush();
7088 return;
7089 }
7090
7091 final long bToken = proto.start(BatteryStatsServiceDumpProto.BATTERYSTATS);
7092
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007093 proto.write(BatteryStatsProto.REPORT_VERSION, CHECKIN_VERSION);
7094 proto.write(BatteryStatsProto.PARCEL_VERSION, getParcelVersion());
7095 proto.write(BatteryStatsProto.START_PLATFORM_VERSION, getStartPlatformVersion());
7096 proto.write(BatteryStatsProto.END_PLATFORM_VERSION, getEndPlatformVersion());
7097
Kweku Adams71a95312018-04-16 16:54:24 -07007098 if ((flags & DUMP_DAILY_ONLY) == 0) {
Kweku Adams103351f2017-10-16 14:39:34 -07007099 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false,
7100 (flags & DUMP_DEVICE_WIFI_ONLY) != 0);
7101 helper.create(this);
7102 helper.refreshStats(STATS_SINCE_CHARGED, UserHandle.USER_ALL);
7103
7104 dumpProtoAppsLocked(proto, helper, apps);
7105 dumpProtoSystemLocked(proto, helper);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007106 }
7107
7108 proto.end(bToken);
7109 proto.flush();
7110 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07007111
Kweku Adams103351f2017-10-16 14:39:34 -07007112 private void dumpProtoAppsLocked(ProtoOutputStream proto, BatteryStatsHelper helper,
7113 List<ApplicationInfo> apps) {
7114 final int which = STATS_SINCE_CHARGED;
7115 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7116 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7117 final long rawRealtimeUs = rawRealtimeMs * 1000;
7118 final long batteryUptimeUs = getBatteryUptime(rawUptimeUs);
7119
7120 SparseArray<ArrayList<String>> aidToPackages = new SparseArray<>();
7121 if (apps != null) {
7122 for (int i = 0; i < apps.size(); ++i) {
7123 ApplicationInfo ai = apps.get(i);
7124 int aid = UserHandle.getAppId(ai.uid);
7125 ArrayList<String> pkgs = aidToPackages.get(aid);
7126 if (pkgs == null) {
7127 pkgs = new ArrayList<String>();
7128 aidToPackages.put(aid, pkgs);
7129 }
7130 pkgs.add(ai.packageName);
7131 }
7132 }
7133
7134 SparseArray<BatterySipper> uidToSipper = new SparseArray<>();
7135 final List<BatterySipper> sippers = helper.getUsageList();
7136 if (sippers != null) {
7137 for (int i = 0; i < sippers.size(); ++i) {
7138 final BatterySipper bs = sippers.get(i);
7139 if (bs.drainType != BatterySipper.DrainType.APP) {
7140 // Others are handled by dumpProtoSystemLocked()
7141 continue;
7142 }
7143 uidToSipper.put(bs.uidObj.getUid(), bs);
7144 }
7145 }
7146
7147 SparseArray<? extends Uid> uidStats = getUidStats();
7148 final int n = uidStats.size();
7149 for (int iu = 0; iu < n; ++iu) {
7150 final long uTkn = proto.start(BatteryStatsProto.UIDS);
7151 final Uid u = uidStats.valueAt(iu);
7152
7153 final int uid = uidStats.keyAt(iu);
7154 proto.write(UidProto.UID, uid);
7155
7156 // Print packages and apk stats (UID_DATA & APK_DATA)
7157 ArrayList<String> pkgs = aidToPackages.get(UserHandle.getAppId(uid));
7158 if (pkgs == null) {
7159 pkgs = new ArrayList<String>();
7160 }
7161 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats =
7162 u.getPackageStats();
7163 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7164 String pkg = packageStats.keyAt(ipkg);
7165 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats =
7166 packageStats.valueAt(ipkg).getServiceStats();
7167 if (serviceStats.size() == 0) {
7168 // Due to the way ActivityManagerService logs wakeup alarms, some packages (for
7169 // example, "android") may be included in the packageStats that aren't part of
7170 // the UID. If they don't have any services, then they shouldn't be listed here.
7171 // These packages won't be a part in the pkgs List.
7172 continue;
7173 }
7174
7175 final long pToken = proto.start(UidProto.PACKAGES);
7176 proto.write(UidProto.Package.NAME, pkg);
7177 // Remove from the packages list since we're logging it here.
7178 pkgs.remove(pkg);
7179
7180 for (int isvc = serviceStats.size() - 1; isvc >= 0; --isvc) {
7181 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
Kweku Adams14f3d222018-03-22 14:12:55 -07007182
7183 final long startTimeMs = roundUsToMs(ss.getStartTime(batteryUptimeUs, which));
7184 final int starts = ss.getStarts(which);
7185 final int launches = ss.getLaunches(which);
7186 if (startTimeMs == 0 && starts == 0 && launches == 0) {
7187 continue;
7188 }
7189
Kweku Adams103351f2017-10-16 14:39:34 -07007190 long sToken = proto.start(UidProto.Package.SERVICES);
7191
7192 proto.write(UidProto.Package.Service.NAME, serviceStats.keyAt(isvc));
Kweku Adams14f3d222018-03-22 14:12:55 -07007193 proto.write(UidProto.Package.Service.START_DURATION_MS, startTimeMs);
7194 proto.write(UidProto.Package.Service.START_COUNT, starts);
7195 proto.write(UidProto.Package.Service.LAUNCH_COUNT, launches);
Kweku Adams103351f2017-10-16 14:39:34 -07007196
7197 proto.end(sToken);
7198 }
7199 proto.end(pToken);
7200 }
7201 // Print any remaining packages that weren't in the packageStats map. pkgs is pulled
7202 // from PackageManager data. Packages are only included in packageStats if there was
7203 // specific data tracked for them (services and wakeup alarms, etc.).
7204 for (String p : pkgs) {
7205 final long pToken = proto.start(UidProto.PACKAGES);
7206 proto.write(UidProto.Package.NAME, p);
7207 proto.end(pToken);
7208 }
7209
7210 // Total wakelock data (AGGREGATED_WAKELOCK_DATA)
7211 if (u.getAggregatedPartialWakelockTimer() != null) {
7212 final Timer timer = u.getAggregatedPartialWakelockTimer();
7213 // Times are since reset (regardless of 'which')
7214 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
7215 final Timer bgTimer = timer.getSubTimer();
7216 final long bgTimeMs = bgTimer != null
7217 ? bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
7218 final long awToken = proto.start(UidProto.AGGREGATED_WAKELOCK);
7219 proto.write(UidProto.AggregatedWakelock.PARTIAL_DURATION_MS, totTimeMs);
7220 proto.write(UidProto.AggregatedWakelock.BACKGROUND_PARTIAL_DURATION_MS, bgTimeMs);
7221 proto.end(awToken);
7222 }
7223
7224 // Audio (AUDIO_DATA)
7225 dumpTimer(proto, UidProto.AUDIO, u.getAudioTurnedOnTimer(), rawRealtimeUs, which);
7226
7227 // Bluetooth Controller (BLUETOOTH_CONTROLLER_DATA)
7228 dumpControllerActivityProto(proto, UidProto.BLUETOOTH_CONTROLLER,
7229 u.getBluetoothControllerActivity(), which);
7230
7231 // BLE scans (BLUETOOTH_MISC_DATA) (uses totalDurationMsLocked and MaxDurationMsLocked)
7232 final Timer bleTimer = u.getBluetoothScanTimer();
7233 if (bleTimer != null) {
7234 final long bmToken = proto.start(UidProto.BLUETOOTH_MISC);
7235
7236 dumpTimer(proto, UidProto.BluetoothMisc.APPORTIONED_BLE_SCAN, bleTimer,
7237 rawRealtimeUs, which);
7238 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN,
7239 u.getBluetoothScanBackgroundTimer(), rawRealtimeUs, which);
7240 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
7241 dumpTimer(proto, UidProto.BluetoothMisc.UNOPTIMIZED_BLE_SCAN,
7242 u.getBluetoothUnoptimizedScanTimer(), rawRealtimeUs, which);
7243 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
7244 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_UNOPTIMIZED_BLE_SCAN,
7245 u.getBluetoothUnoptimizedScanBackgroundTimer(), rawRealtimeUs, which);
7246 // Result counters
7247 proto.write(UidProto.BluetoothMisc.BLE_SCAN_RESULT_COUNT,
7248 u.getBluetoothScanResultCounter() != null
7249 ? u.getBluetoothScanResultCounter().getCountLocked(which) : 0);
7250 proto.write(UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN_RESULT_COUNT,
7251 u.getBluetoothScanResultBgCounter() != null
7252 ? u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0);
7253
7254 proto.end(bmToken);
7255 }
7256
7257 // Camera (CAMERA_DATA)
7258 dumpTimer(proto, UidProto.CAMERA, u.getCameraTurnedOnTimer(), rawRealtimeUs, which);
7259
7260 // CPU stats (CPU_DATA & CPU_TIMES_AT_FREQ_DATA)
7261 final long cpuToken = proto.start(UidProto.CPU);
7262 proto.write(UidProto.Cpu.USER_DURATION_MS, roundUsToMs(u.getUserCpuTimeUs(which)));
7263 proto.write(UidProto.Cpu.SYSTEM_DURATION_MS, roundUsToMs(u.getSystemCpuTimeUs(which)));
7264
7265 final long[] cpuFreqs = getCpuFreqs();
7266 if (cpuFreqs != null) {
7267 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
7268 // If total cpuFreqTimes is null, then we don't need to check for
7269 // screenOffCpuFreqTimes.
7270 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
7271 long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
7272 if (screenOffCpuFreqTimeMs == null) {
7273 screenOffCpuFreqTimeMs = new long[cpuFreqTimeMs.length];
7274 }
7275 for (int ic = 0; ic < cpuFreqTimeMs.length; ++ic) {
7276 long cToken = proto.start(UidProto.Cpu.BY_FREQUENCY);
7277 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7278 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7279 cpuFreqTimeMs[ic]);
7280 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7281 screenOffCpuFreqTimeMs[ic]);
7282 proto.end(cToken);
7283 }
7284 }
7285 }
Sudheer Shanka6d658d72018-01-01 01:36:49 -08007286
7287 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
7288 final long[] timesMs = u.getCpuFreqTimes(which, procState);
7289 if (timesMs != null && timesMs.length == cpuFreqs.length) {
7290 long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(which, procState);
7291 if (screenOffTimesMs == null) {
7292 screenOffTimesMs = new long[timesMs.length];
7293 }
7294 final long procToken = proto.start(UidProto.Cpu.BY_PROCESS_STATE);
7295 proto.write(UidProto.Cpu.ByProcessState.PROCESS_STATE, procState);
7296 for (int ic = 0; ic < timesMs.length; ++ic) {
7297 long cToken = proto.start(UidProto.Cpu.ByProcessState.BY_FREQUENCY);
7298 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7299 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7300 timesMs[ic]);
7301 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7302 screenOffTimesMs[ic]);
7303 proto.end(cToken);
7304 }
7305 proto.end(procToken);
7306 }
7307 }
Kweku Adams103351f2017-10-16 14:39:34 -07007308 proto.end(cpuToken);
7309
7310 // Flashlight (FLASHLIGHT_DATA)
7311 dumpTimer(proto, UidProto.FLASHLIGHT, u.getFlashlightTurnedOnTimer(),
7312 rawRealtimeUs, which);
7313
7314 // Foreground activity (FOREGROUND_ACTIVITY_DATA)
7315 dumpTimer(proto, UidProto.FOREGROUND_ACTIVITY, u.getForegroundActivityTimer(),
7316 rawRealtimeUs, which);
7317
7318 // Foreground service (FOREGROUND_SERVICE_DATA)
7319 dumpTimer(proto, UidProto.FOREGROUND_SERVICE, u.getForegroundServiceTimer(),
7320 rawRealtimeUs, which);
7321
7322 // Job completion (JOB_COMPLETION_DATA)
7323 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
7324 final int[] reasons = new int[]{
7325 JobParameters.REASON_CANCELED,
7326 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED,
7327 JobParameters.REASON_PREEMPT,
7328 JobParameters.REASON_TIMEOUT,
7329 JobParameters.REASON_DEVICE_IDLE,
7330 };
7331 for (int ic = 0; ic < completions.size(); ++ic) {
7332 SparseIntArray types = completions.valueAt(ic);
7333 if (types != null) {
7334 final long jcToken = proto.start(UidProto.JOB_COMPLETION);
7335
7336 proto.write(UidProto.JobCompletion.NAME, completions.keyAt(ic));
7337
7338 for (int r : reasons) {
7339 long rToken = proto.start(UidProto.JobCompletion.REASON_COUNT);
7340 proto.write(UidProto.JobCompletion.ReasonCount.NAME, r);
7341 proto.write(UidProto.JobCompletion.ReasonCount.COUNT, types.get(r, 0));
7342 proto.end(rToken);
7343 }
7344
7345 proto.end(jcToken);
7346 }
7347 }
7348
7349 // Scheduled jobs (JOB_DATA)
7350 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
7351 for (int ij = jobs.size() - 1; ij >= 0; --ij) {
7352 final Timer timer = jobs.valueAt(ij);
7353 final Timer bgTimer = timer.getSubTimer();
7354 final long jToken = proto.start(UidProto.JOBS);
7355
7356 proto.write(UidProto.Job.NAME, jobs.keyAt(ij));
7357 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7358 dumpTimer(proto, UidProto.Job.TOTAL, timer, rawRealtimeUs, which);
7359 dumpTimer(proto, UidProto.Job.BACKGROUND, bgTimer, rawRealtimeUs, which);
7360
7361 proto.end(jToken);
7362 }
7363
7364 // Modem Controller (MODEM_CONTROLLER_DATA)
7365 dumpControllerActivityProto(proto, UidProto.MODEM_CONTROLLER,
7366 u.getModemControllerActivity(), which);
7367
7368 // Network stats (NETWORK_DATA)
7369 final long nToken = proto.start(UidProto.NETWORK);
7370 proto.write(UidProto.Network.MOBILE_BYTES_RX,
7371 u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7372 proto.write(UidProto.Network.MOBILE_BYTES_TX,
7373 u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7374 proto.write(UidProto.Network.WIFI_BYTES_RX,
7375 u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7376 proto.write(UidProto.Network.WIFI_BYTES_TX,
7377 u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7378 proto.write(UidProto.Network.BT_BYTES_RX,
7379 u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7380 proto.write(UidProto.Network.BT_BYTES_TX,
7381 u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
7382 proto.write(UidProto.Network.MOBILE_PACKETS_RX,
7383 u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7384 proto.write(UidProto.Network.MOBILE_PACKETS_TX,
7385 u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7386 proto.write(UidProto.Network.WIFI_PACKETS_RX,
7387 u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7388 proto.write(UidProto.Network.WIFI_PACKETS_TX,
7389 u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7390 proto.write(UidProto.Network.MOBILE_ACTIVE_DURATION_MS,
7391 roundUsToMs(u.getMobileRadioActiveTime(which)));
7392 proto.write(UidProto.Network.MOBILE_ACTIVE_COUNT,
7393 u.getMobileRadioActiveCount(which));
7394 proto.write(UidProto.Network.MOBILE_WAKEUP_COUNT,
7395 u.getMobileRadioApWakeupCount(which));
7396 proto.write(UidProto.Network.WIFI_WAKEUP_COUNT,
7397 u.getWifiRadioApWakeupCount(which));
7398 proto.write(UidProto.Network.MOBILE_BYTES_BG_RX,
7399 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA, which));
7400 proto.write(UidProto.Network.MOBILE_BYTES_BG_TX,
7401 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA, which));
7402 proto.write(UidProto.Network.WIFI_BYTES_BG_RX,
7403 u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which));
7404 proto.write(UidProto.Network.WIFI_BYTES_BG_TX,
7405 u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which));
7406 proto.write(UidProto.Network.MOBILE_PACKETS_BG_RX,
7407 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA, which));
7408 proto.write(UidProto.Network.MOBILE_PACKETS_BG_TX,
7409 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA, which));
7410 proto.write(UidProto.Network.WIFI_PACKETS_BG_RX,
7411 u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA, which));
7412 proto.write(UidProto.Network.WIFI_PACKETS_BG_TX,
7413 u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA, which));
7414 proto.end(nToken);
7415
7416 // Power use item (POWER_USE_ITEM_DATA)
7417 BatterySipper bs = uidToSipper.get(uid);
7418 if (bs != null) {
7419 final long bsToken = proto.start(UidProto.POWER_USE_ITEM);
7420 proto.write(UidProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7421 proto.write(UidProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7422 proto.write(UidProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7423 proto.write(UidProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7424 bs.proportionalSmearMah);
7425 proto.end(bsToken);
7426 }
7427
7428 // Processes (PROCESS_DATA)
7429 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats =
7430 u.getProcessStats();
7431 for (int ipr = processStats.size() - 1; ipr >= 0; --ipr) {
7432 final Uid.Proc ps = processStats.valueAt(ipr);
7433 final long prToken = proto.start(UidProto.PROCESS);
7434
7435 proto.write(UidProto.Process.NAME, processStats.keyAt(ipr));
7436 proto.write(UidProto.Process.USER_DURATION_MS, ps.getUserTime(which));
7437 proto.write(UidProto.Process.SYSTEM_DURATION_MS, ps.getSystemTime(which));
7438 proto.write(UidProto.Process.FOREGROUND_DURATION_MS, ps.getForegroundTime(which));
7439 proto.write(UidProto.Process.START_COUNT, ps.getStarts(which));
7440 proto.write(UidProto.Process.ANR_COUNT, ps.getNumAnrs(which));
7441 proto.write(UidProto.Process.CRASH_COUNT, ps.getNumCrashes(which));
7442
7443 proto.end(prToken);
7444 }
7445
7446 // Sensors (SENSOR_DATA)
7447 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
7448 for (int ise = 0; ise < sensors.size(); ++ise) {
7449 final Uid.Sensor se = sensors.valueAt(ise);
7450 final Timer timer = se.getSensorTime();
7451 if (timer == null) {
7452 continue;
7453 }
7454 final Timer bgTimer = se.getSensorBackgroundTime();
7455 final int sensorNumber = sensors.keyAt(ise);
7456 final long seToken = proto.start(UidProto.SENSORS);
7457
7458 proto.write(UidProto.Sensor.ID, sensorNumber);
7459 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7460 dumpTimer(proto, UidProto.Sensor.APPORTIONED, timer, rawRealtimeUs, which);
7461 dumpTimer(proto, UidProto.Sensor.BACKGROUND, bgTimer, rawRealtimeUs, which);
7462
7463 proto.end(seToken);
7464 }
7465
7466 // State times (STATE_TIME_DATA)
7467 for (int ips = 0; ips < Uid.NUM_PROCESS_STATE; ++ips) {
7468 long durMs = roundUsToMs(u.getProcessStateTime(ips, rawRealtimeUs, which));
7469 if (durMs == 0) {
7470 continue;
7471 }
7472 final long stToken = proto.start(UidProto.STATES);
7473 proto.write(UidProto.StateTime.STATE, ips);
7474 proto.write(UidProto.StateTime.DURATION_MS, durMs);
7475 proto.end(stToken);
7476 }
7477
7478 // Syncs (SYNC_DATA)
7479 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
7480 for (int isy = syncs.size() - 1; isy >= 0; --isy) {
7481 final Timer timer = syncs.valueAt(isy);
7482 final Timer bgTimer = timer.getSubTimer();
7483 final long syToken = proto.start(UidProto.SYNCS);
7484
7485 proto.write(UidProto.Sync.NAME, syncs.keyAt(isy));
7486 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7487 dumpTimer(proto, UidProto.Sync.TOTAL, timer, rawRealtimeUs, which);
7488 dumpTimer(proto, UidProto.Sync.BACKGROUND, bgTimer, rawRealtimeUs, which);
7489
7490 proto.end(syToken);
7491 }
7492
7493 // User activity (USER_ACTIVITY_DATA)
7494 if (u.hasUserActivity()) {
7495 for (int i = 0; i < Uid.NUM_USER_ACTIVITY_TYPES; ++i) {
7496 int val = u.getUserActivityCount(i, which);
7497 if (val != 0) {
7498 final long uaToken = proto.start(UidProto.USER_ACTIVITY);
7499 proto.write(UidProto.UserActivity.NAME, i);
7500 proto.write(UidProto.UserActivity.COUNT, val);
7501 proto.end(uaToken);
7502 }
7503 }
7504 }
7505
7506 // Vibrator (VIBRATOR_DATA)
7507 dumpTimer(proto, UidProto.VIBRATOR, u.getVibratorOnTimer(), rawRealtimeUs, which);
7508
7509 // Video (VIDEO_DATA)
7510 dumpTimer(proto, UidProto.VIDEO, u.getVideoTurnedOnTimer(), rawRealtimeUs, which);
7511
7512 // Wakelocks (WAKELOCK_DATA)
7513 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
7514 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7515 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7516 final long wToken = proto.start(UidProto.WAKELOCKS);
7517 proto.write(UidProto.Wakelock.NAME, wakelocks.keyAt(iw));
7518 dumpTimer(proto, UidProto.Wakelock.FULL, wl.getWakeTime(WAKE_TYPE_FULL),
7519 rawRealtimeUs, which);
7520 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7521 if (pTimer != null) {
7522 dumpTimer(proto, UidProto.Wakelock.PARTIAL, pTimer, rawRealtimeUs, which);
7523 dumpTimer(proto, UidProto.Wakelock.BACKGROUND_PARTIAL, pTimer.getSubTimer(),
7524 rawRealtimeUs, which);
7525 }
7526 dumpTimer(proto, UidProto.Wakelock.WINDOW, wl.getWakeTime(WAKE_TYPE_WINDOW),
7527 rawRealtimeUs, which);
7528 proto.end(wToken);
7529 }
7530
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007531 // Wifi Multicast Wakelock (WIFI_MULTICAST_WAKELOCK_DATA)
7532 dumpTimer(proto, UidProto.WIFI_MULTICAST_WAKELOCK, u.getMulticastWakelockStats(),
7533 rawRealtimeUs, which);
7534
Kweku Adams103351f2017-10-16 14:39:34 -07007535 // Wakeup alarms (WAKEUP_ALARM_DATA)
7536 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7537 final Uid.Pkg ps = packageStats.valueAt(ipkg);
7538 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
7539 for (int iwa = alarms.size() - 1; iwa >= 0; --iwa) {
7540 final long waToken = proto.start(UidProto.WAKEUP_ALARM);
7541 proto.write(UidProto.WakeupAlarm.NAME, alarms.keyAt(iwa));
7542 proto.write(UidProto.WakeupAlarm.COUNT,
7543 alarms.valueAt(iwa).getCountLocked(which));
7544 proto.end(waToken);
7545 }
7546 }
7547
7548 // Wifi Controller (WIFI_CONTROLLER_DATA)
7549 dumpControllerActivityProto(proto, UidProto.WIFI_CONTROLLER,
7550 u.getWifiControllerActivity(), which);
7551
7552 // Wifi data (WIFI_DATA)
7553 final long wToken = proto.start(UidProto.WIFI);
7554 proto.write(UidProto.Wifi.FULL_WIFI_LOCK_DURATION_MS,
7555 roundUsToMs(u.getFullWifiLockTime(rawRealtimeUs, which)));
7556 dumpTimer(proto, UidProto.Wifi.APPORTIONED_SCAN, u.getWifiScanTimer(),
7557 rawRealtimeUs, which);
7558 proto.write(UidProto.Wifi.RUNNING_DURATION_MS,
7559 roundUsToMs(u.getWifiRunningTime(rawRealtimeUs, which)));
7560 dumpTimer(proto, UidProto.Wifi.BACKGROUND_SCAN, u.getWifiScanBackgroundTimer(),
7561 rawRealtimeUs, which);
7562 proto.end(wToken);
7563
7564 proto.end(uTkn);
7565 }
7566 }
7567
Kweku Adams71a95312018-04-16 16:54:24 -07007568 private void dumpProtoHistoryLocked(ProtoOutputStream proto, int flags, long histStart) {
7569 if (!startIteratingHistoryLocked()) {
7570 return;
7571 }
7572
7573 proto.write(BatteryStatsServiceDumpHistoryProto.REPORT_VERSION, CHECKIN_VERSION);
7574 proto.write(BatteryStatsServiceDumpHistoryProto.PARCEL_VERSION, getParcelVersion());
7575 proto.write(BatteryStatsServiceDumpHistoryProto.START_PLATFORM_VERSION,
7576 getStartPlatformVersion());
7577 proto.write(BatteryStatsServiceDumpHistoryProto.END_PLATFORM_VERSION,
7578 getEndPlatformVersion());
7579 try {
7580 long token;
7581 // History string pool (HISTORY_STRING_POOL)
7582 for (int i = 0; i < getHistoryStringPoolSize(); ++i) {
7583 token = proto.start(BatteryStatsServiceDumpHistoryProto.KEYS);
7584 proto.write(BatteryStatsServiceDumpHistoryProto.Key.INDEX, i);
7585 proto.write(BatteryStatsServiceDumpHistoryProto.Key.UID, getHistoryTagPoolUid(i));
7586 proto.write(BatteryStatsServiceDumpHistoryProto.Key.TAG,
7587 getHistoryTagPoolString(i));
7588 proto.end(token);
7589 }
7590
7591 // History data (HISTORY_DATA)
7592 final HistoryPrinter hprinter = new HistoryPrinter();
7593 final HistoryItem rec = new HistoryItem();
7594 long lastTime = -1;
7595 long baseTime = -1;
7596 boolean printed = false;
7597 HistoryEventTracker tracker = null;
7598 while (getNextHistoryLocked(rec)) {
7599 lastTime = rec.time;
7600 if (baseTime < 0) {
7601 baseTime = lastTime;
7602 }
7603 if (rec.time >= histStart) {
7604 if (histStart >= 0 && !printed) {
7605 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
7606 || rec.cmd == HistoryItem.CMD_RESET
7607 || rec.cmd == HistoryItem.CMD_START
7608 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
7609 printed = true;
7610 hprinter.printNextItem(proto, rec, baseTime,
7611 (flags & DUMP_VERBOSE) != 0);
7612 rec.cmd = HistoryItem.CMD_UPDATE;
7613 } else if (rec.currentTime != 0) {
7614 printed = true;
7615 byte cmd = rec.cmd;
7616 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
7617 hprinter.printNextItem(proto, rec, baseTime,
7618 (flags & DUMP_VERBOSE) != 0);
7619 rec.cmd = cmd;
7620 }
7621 if (tracker != null) {
7622 if (rec.cmd != HistoryItem.CMD_UPDATE) {
7623 hprinter.printNextItem(proto, rec, baseTime,
7624 (flags & DUMP_VERBOSE) != 0);
7625 rec.cmd = HistoryItem.CMD_UPDATE;
7626 }
7627 int oldEventCode = rec.eventCode;
7628 HistoryTag oldEventTag = rec.eventTag;
7629 rec.eventTag = new HistoryTag();
7630 for (int i = 0; i < HistoryItem.EVENT_COUNT; i++) {
7631 HashMap<String, SparseIntArray> active =
7632 tracker.getStateForEvent(i);
7633 if (active == null) {
7634 continue;
7635 }
7636 for (HashMap.Entry<String, SparseIntArray> ent
7637 : active.entrySet()) {
7638 SparseIntArray uids = ent.getValue();
7639 for (int j = 0; j < uids.size(); j++) {
7640 rec.eventCode = i;
7641 rec.eventTag.string = ent.getKey();
7642 rec.eventTag.uid = uids.keyAt(j);
7643 rec.eventTag.poolIdx = uids.valueAt(j);
7644 hprinter.printNextItem(proto, rec, baseTime,
7645 (flags & DUMP_VERBOSE) != 0);
7646 rec.wakeReasonTag = null;
7647 rec.wakelockTag = null;
7648 }
7649 }
7650 }
7651 rec.eventCode = oldEventCode;
7652 rec.eventTag = oldEventTag;
7653 tracker = null;
7654 }
7655 }
7656 hprinter.printNextItem(proto, rec, baseTime,
7657 (flags & DUMP_VERBOSE) != 0);
7658 }
7659 }
7660 if (histStart >= 0) {
7661 commitCurrentHistoryBatchLocked();
7662 proto.write(BatteryStatsServiceDumpHistoryProto.CSV_LINES,
7663 "NEXT: " + (lastTime + 1));
7664 }
7665 } finally {
7666 finishIteratingHistoryLocked();
7667 }
7668 }
7669
Kweku Adams103351f2017-10-16 14:39:34 -07007670 private void dumpProtoSystemLocked(ProtoOutputStream proto, BatteryStatsHelper helper) {
Kweku Adams87b19ec2017-10-09 12:40:03 -07007671 final long sToken = proto.start(BatteryStatsProto.SYSTEM);
7672 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7673 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7674 final long rawRealtimeUs = rawRealtimeMs * 1000;
7675 final int which = STATS_SINCE_CHARGED;
7676
7677 // Battery data (BATTERY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007678 final long bToken = proto.start(SystemProto.BATTERY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007679 proto.write(SystemProto.Battery.START_CLOCK_TIME_MS, getStartClockTime());
7680 proto.write(SystemProto.Battery.START_COUNT, getStartCount());
7681 proto.write(SystemProto.Battery.TOTAL_REALTIME_MS,
7682 computeRealtime(rawRealtimeUs, which) / 1000);
7683 proto.write(SystemProto.Battery.TOTAL_UPTIME_MS,
7684 computeUptime(rawUptimeUs, which) / 1000);
7685 proto.write(SystemProto.Battery.BATTERY_REALTIME_MS,
7686 computeBatteryRealtime(rawRealtimeUs, which) / 1000);
7687 proto.write(SystemProto.Battery.BATTERY_UPTIME_MS,
7688 computeBatteryUptime(rawUptimeUs, which) / 1000);
7689 proto.write(SystemProto.Battery.SCREEN_OFF_REALTIME_MS,
7690 computeBatteryScreenOffRealtime(rawRealtimeUs, which) / 1000);
7691 proto.write(SystemProto.Battery.SCREEN_OFF_UPTIME_MS,
7692 computeBatteryScreenOffUptime(rawUptimeUs, which) / 1000);
7693 proto.write(SystemProto.Battery.SCREEN_DOZE_DURATION_MS,
7694 getScreenDozeTime(rawRealtimeUs, which) / 1000);
7695 proto.write(SystemProto.Battery.ESTIMATED_BATTERY_CAPACITY_MAH,
7696 getEstimatedBatteryCapacity());
7697 proto.write(SystemProto.Battery.MIN_LEARNED_BATTERY_CAPACITY_UAH,
7698 getMinLearnedBatteryCapacity());
7699 proto.write(SystemProto.Battery.MAX_LEARNED_BATTERY_CAPACITY_UAH,
7700 getMaxLearnedBatteryCapacity());
Kweku Adams103351f2017-10-16 14:39:34 -07007701 proto.end(bToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007702
7703 // Battery discharge (BATTERY_DISCHARGE_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007704 final long bdToken = proto.start(SystemProto.BATTERY_DISCHARGE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007705 proto.write(SystemProto.BatteryDischarge.LOWER_BOUND_SINCE_CHARGE,
7706 getLowDischargeAmountSinceCharge());
7707 proto.write(SystemProto.BatteryDischarge.UPPER_BOUND_SINCE_CHARGE,
7708 getHighDischargeAmountSinceCharge());
7709 proto.write(SystemProto.BatteryDischarge.SCREEN_ON_SINCE_CHARGE,
7710 getDischargeAmountScreenOnSinceCharge());
7711 proto.write(SystemProto.BatteryDischarge.SCREEN_OFF_SINCE_CHARGE,
7712 getDischargeAmountScreenOffSinceCharge());
7713 proto.write(SystemProto.BatteryDischarge.SCREEN_DOZE_SINCE_CHARGE,
7714 getDischargeAmountScreenDozeSinceCharge());
7715 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH,
7716 getUahDischarge(which) / 1000);
7717 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_OFF,
7718 getUahDischargeScreenOff(which) / 1000);
7719 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_DOZE,
7720 getUahDischargeScreenDoze(which) / 1000);
Mike Ma15313c92017-11-15 17:58:21 -08007721 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_LIGHT_DOZE,
7722 getUahDischargeLightDoze(which) / 1000);
7723 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_DEEP_DOZE,
7724 getUahDischargeDeepDoze(which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007725 proto.end(bdToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007726
7727 // Time remaining
7728 long timeRemainingUs = computeChargeTimeRemaining(rawRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -07007729 // These are part of a oneof, so we should only set one of them.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007730 if (timeRemainingUs >= 0) {
7731 // Charge time remaining (CHARGE_TIME_REMAIN_DATA)
7732 proto.write(SystemProto.CHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7733 } else {
7734 timeRemainingUs = computeBatteryTimeRemaining(rawRealtimeUs);
7735 // Discharge time remaining (DISCHARGE_TIME_REMAIN_DATA)
7736 if (timeRemainingUs >= 0) {
7737 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7738 } else {
7739 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, -1);
7740 }
7741 }
7742
7743 // Charge step (CHARGE_STEP_DATA)
7744 dumpDurationSteps(proto, SystemProto.CHARGE_STEP, getChargeLevelStepTracker());
7745
7746 // Phone data connection (DATA_CONNECTION_TIME_DATA and DATA_CONNECTION_COUNT_DATA)
7747 for (int i = 0; i < NUM_DATA_CONNECTION_TYPES; ++i) {
Tej Singheee317b2018-03-07 19:28:05 -08007748 // Map OTHER to TelephonyManager.NETWORK_TYPE_UNKNOWN and mark NONE as a boolean.
7749 boolean isNone = (i == DATA_CONNECTION_NONE);
7750 int telephonyNetworkType = i;
7751 if (i == DATA_CONNECTION_OTHER) {
7752 telephonyNetworkType = TelephonyManager.NETWORK_TYPE_UNKNOWN;
7753 }
Kweku Adams103351f2017-10-16 14:39:34 -07007754 final long pdcToken = proto.start(SystemProto.DATA_CONNECTION);
Tej Singheee317b2018-03-07 19:28:05 -08007755 if (isNone) {
7756 proto.write(SystemProto.DataConnection.IS_NONE, isNone);
7757 } else {
7758 proto.write(SystemProto.DataConnection.NAME, telephonyNetworkType);
7759 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07007760 dumpTimer(proto, SystemProto.DataConnection.TOTAL, getPhoneDataConnectionTimer(i),
7761 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007762 proto.end(pdcToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007763 }
7764
7765 // Discharge step (DISCHARGE_STEP_DATA)
7766 dumpDurationSteps(proto, SystemProto.DISCHARGE_STEP, getDischargeLevelStepTracker());
7767
7768 // CPU frequencies (GLOBAL_CPU_FREQ_DATA)
7769 final long[] cpuFreqs = getCpuFreqs();
7770 if (cpuFreqs != null) {
7771 for (long i : cpuFreqs) {
7772 proto.write(SystemProto.CPU_FREQUENCY, i);
7773 }
7774 }
7775
7776 // Bluetooth controller (GLOBAL_BLUETOOTH_CONTROLLER_DATA)
7777 dumpControllerActivityProto(proto, SystemProto.GLOBAL_BLUETOOTH_CONTROLLER,
7778 getBluetoothControllerActivity(), which);
7779
7780 // Modem controller (GLOBAL_MODEM_CONTROLLER_DATA)
7781 dumpControllerActivityProto(proto, SystemProto.GLOBAL_MODEM_CONTROLLER,
7782 getModemControllerActivity(), which);
7783
7784 // Global network data (GLOBAL_NETWORK_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007785 final long gnToken = proto.start(SystemProto.GLOBAL_NETWORK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007786 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_RX,
7787 getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7788 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_TX,
7789 getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7790 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_RX,
7791 getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7792 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_TX,
7793 getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7794 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_RX,
7795 getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7796 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_TX,
7797 getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7798 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_RX,
7799 getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7800 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_TX,
7801 getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7802 proto.write(SystemProto.GlobalNetwork.BT_BYTES_RX,
7803 getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7804 proto.write(SystemProto.GlobalNetwork.BT_BYTES_TX,
7805 getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
Kweku Adams103351f2017-10-16 14:39:34 -07007806 proto.end(gnToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007807
7808 // Wifi controller (GLOBAL_WIFI_CONTROLLER_DATA)
7809 dumpControllerActivityProto(proto, SystemProto.GLOBAL_WIFI_CONTROLLER,
7810 getWifiControllerActivity(), which);
7811
7812
7813 // Global wifi (GLOBAL_WIFI_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007814 final long gwToken = proto.start(SystemProto.GLOBAL_WIFI);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007815 proto.write(SystemProto.GlobalWifi.ON_DURATION_MS,
7816 getWifiOnTime(rawRealtimeUs, which) / 1000);
7817 proto.write(SystemProto.GlobalWifi.RUNNING_DURATION_MS,
7818 getGlobalWifiRunningTime(rawRealtimeUs, which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007819 proto.end(gwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007820
7821 // Kernel wakelock (KERNEL_WAKELOCK_DATA)
7822 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
7823 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007824 final long kwToken = proto.start(SystemProto.KERNEL_WAKELOCK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007825 proto.write(SystemProto.KernelWakelock.NAME, ent.getKey());
7826 dumpTimer(proto, SystemProto.KernelWakelock.TOTAL, ent.getValue(),
7827 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007828 proto.end(kwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007829 }
7830
7831 // Misc (MISC_DATA)
7832 // Calculate wakelock times across all uids.
7833 long fullWakeLockTimeTotalUs = 0;
7834 long partialWakeLockTimeTotalUs = 0;
7835
7836 final SparseArray<? extends Uid> uidStats = getUidStats();
7837 for (int iu = 0; iu < uidStats.size(); iu++) {
7838 final Uid u = uidStats.valueAt(iu);
7839
7840 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks =
7841 u.getWakelockStats();
7842 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7843 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7844
7845 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
7846 if (fullWakeTimer != null) {
7847 fullWakeLockTimeTotalUs += fullWakeTimer.getTotalTimeLocked(rawRealtimeUs,
7848 which);
7849 }
7850
7851 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7852 if (partialWakeTimer != null) {
7853 partialWakeLockTimeTotalUs += partialWakeTimer.getTotalTimeLocked(
7854 rawRealtimeUs, which);
7855 }
7856 }
7857 }
Kweku Adams103351f2017-10-16 14:39:34 -07007858 final long mToken = proto.start(SystemProto.MISC);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007859 proto.write(SystemProto.Misc.SCREEN_ON_DURATION_MS,
7860 getScreenOnTime(rawRealtimeUs, which) / 1000);
7861 proto.write(SystemProto.Misc.PHONE_ON_DURATION_MS,
7862 getPhoneOnTime(rawRealtimeUs, which) / 1000);
7863 proto.write(SystemProto.Misc.FULL_WAKELOCK_TOTAL_DURATION_MS,
7864 fullWakeLockTimeTotalUs / 1000);
7865 proto.write(SystemProto.Misc.PARTIAL_WAKELOCK_TOTAL_DURATION_MS,
7866 partialWakeLockTimeTotalUs / 1000);
7867 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_DURATION_MS,
7868 getMobileRadioActiveTime(rawRealtimeUs, which) / 1000);
7869 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_ADJUSTED_TIME_MS,
7870 getMobileRadioActiveAdjustedTime(which) / 1000);
7871 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_COUNT,
7872 getMobileRadioActiveCount(which));
7873 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_UNKNOWN_DURATION_MS,
7874 getMobileRadioActiveUnknownTime(which) / 1000);
7875 proto.write(SystemProto.Misc.INTERACTIVE_DURATION_MS,
7876 getInteractiveTime(rawRealtimeUs, which) / 1000);
7877 proto.write(SystemProto.Misc.BATTERY_SAVER_MODE_ENABLED_DURATION_MS,
7878 getPowerSaveModeEnabledTime(rawRealtimeUs, which) / 1000);
7879 proto.write(SystemProto.Misc.NUM_CONNECTIVITY_CHANGES,
7880 getNumConnectivityChange(which));
7881 proto.write(SystemProto.Misc.DEEP_DOZE_ENABLED_DURATION_MS,
7882 getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7883 proto.write(SystemProto.Misc.DEEP_DOZE_COUNT,
7884 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
7885 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_DURATION_MS,
7886 getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7887 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_COUNT,
7888 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
7889 proto.write(SystemProto.Misc.LONGEST_DEEP_DOZE_DURATION_MS,
7890 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
7891 proto.write(SystemProto.Misc.LIGHT_DOZE_ENABLED_DURATION_MS,
7892 getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7893 proto.write(SystemProto.Misc.LIGHT_DOZE_COUNT,
7894 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
7895 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_DURATION_MS,
7896 getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7897 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_COUNT,
7898 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
7899 proto.write(SystemProto.Misc.LONGEST_LIGHT_DOZE_DURATION_MS,
7900 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
Kweku Adams103351f2017-10-16 14:39:34 -07007901 proto.end(mToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007902
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007903 // Wifi multicast wakelock total stats (WIFI_MULTICAST_WAKELOCK_TOTAL_DATA)
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08007904 final long multicastWakeLockTimeTotalUs =
7905 getWifiMulticastWakelockTime(rawRealtimeUs, which);
7906 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007907 final long wmctToken = proto.start(SystemProto.WIFI_MULTICAST_WAKELOCK_TOTAL);
7908 proto.write(SystemProto.WifiMulticastWakelockTotal.DURATION_MS,
7909 multicastWakeLockTimeTotalUs / 1000);
7910 proto.write(SystemProto.WifiMulticastWakelockTotal.COUNT,
7911 multicastWakeLockCountTotal);
7912 proto.end(wmctToken);
7913
Kweku Adams87b19ec2017-10-09 12:40:03 -07007914 // Power use item (POWER_USE_ITEM_DATA)
7915 final List<BatterySipper> sippers = helper.getUsageList();
7916 if (sippers != null) {
7917 for (int i = 0; i < sippers.size(); ++i) {
7918 final BatterySipper bs = sippers.get(i);
7919 int n = SystemProto.PowerUseItem.UNKNOWN_SIPPER;
7920 int uid = 0;
7921 switch (bs.drainType) {
Kweku Adamsef728952018-04-10 17:56:16 -07007922 case AMBIENT_DISPLAY:
7923 n = SystemProto.PowerUseItem.AMBIENT_DISPLAY;
7924 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07007925 case IDLE:
7926 n = SystemProto.PowerUseItem.IDLE;
7927 break;
7928 case CELL:
7929 n = SystemProto.PowerUseItem.CELL;
7930 break;
7931 case PHONE:
7932 n = SystemProto.PowerUseItem.PHONE;
7933 break;
7934 case WIFI:
7935 n = SystemProto.PowerUseItem.WIFI;
7936 break;
7937 case BLUETOOTH:
7938 n = SystemProto.PowerUseItem.BLUETOOTH;
7939 break;
7940 case SCREEN:
7941 n = SystemProto.PowerUseItem.SCREEN;
7942 break;
7943 case FLASHLIGHT:
7944 n = SystemProto.PowerUseItem.FLASHLIGHT;
7945 break;
7946 case APP:
Kweku Adams103351f2017-10-16 14:39:34 -07007947 // dumpProtoAppsLocked will handle this.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007948 continue;
7949 case USER:
7950 n = SystemProto.PowerUseItem.USER;
7951 uid = UserHandle.getUid(bs.userId, 0);
7952 break;
7953 case UNACCOUNTED:
7954 n = SystemProto.PowerUseItem.UNACCOUNTED;
7955 break;
7956 case OVERCOUNTED:
7957 n = SystemProto.PowerUseItem.OVERCOUNTED;
7958 break;
7959 case CAMERA:
7960 n = SystemProto.PowerUseItem.CAMERA;
7961 break;
7962 case MEMORY:
7963 n = SystemProto.PowerUseItem.MEMORY;
7964 break;
7965 }
Kweku Adams103351f2017-10-16 14:39:34 -07007966 final long puiToken = proto.start(SystemProto.POWER_USE_ITEM);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007967 proto.write(SystemProto.PowerUseItem.NAME, n);
7968 proto.write(SystemProto.PowerUseItem.UID, uid);
7969 proto.write(SystemProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7970 proto.write(SystemProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7971 proto.write(SystemProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7972 proto.write(SystemProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7973 bs.proportionalSmearMah);
Kweku Adams103351f2017-10-16 14:39:34 -07007974 proto.end(puiToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007975 }
7976 }
7977
7978 // Power use summary (POWER_USE_SUMMARY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007979 final long pusToken = proto.start(SystemProto.POWER_USE_SUMMARY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007980 proto.write(SystemProto.PowerUseSummary.BATTERY_CAPACITY_MAH,
7981 helper.getPowerProfile().getBatteryCapacity());
7982 proto.write(SystemProto.PowerUseSummary.COMPUTED_POWER_MAH, helper.getComputedPower());
7983 proto.write(SystemProto.PowerUseSummary.MIN_DRAINED_POWER_MAH, helper.getMinDrainedPower());
7984 proto.write(SystemProto.PowerUseSummary.MAX_DRAINED_POWER_MAH, helper.getMaxDrainedPower());
Kweku Adams103351f2017-10-16 14:39:34 -07007985 proto.end(pusToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007986
7987 // RPM stats (RESOURCE_POWER_MANAGER_DATA)
7988 final Map<String, ? extends Timer> rpmStats = getRpmStats();
7989 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
7990 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007991 final long rpmToken = proto.start(SystemProto.RESOURCE_POWER_MANAGER);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007992 proto.write(SystemProto.ResourcePowerManager.NAME, ent.getKey());
7993 dumpTimer(proto, SystemProto.ResourcePowerManager.TOTAL,
7994 ent.getValue(), rawRealtimeUs, which);
7995 dumpTimer(proto, SystemProto.ResourcePowerManager.SCREEN_OFF,
7996 screenOffRpmStats.get(ent.getKey()), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007997 proto.end(rpmToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007998 }
7999
8000 // Screen brightness (SCREEN_BRIGHTNESS_DATA)
8001 for (int i = 0; i < NUM_SCREEN_BRIGHTNESS_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008002 final long sbToken = proto.start(SystemProto.SCREEN_BRIGHTNESS);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008003 proto.write(SystemProto.ScreenBrightness.NAME, i);
8004 dumpTimer(proto, SystemProto.ScreenBrightness.TOTAL, getScreenBrightnessTimer(i),
8005 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008006 proto.end(sbToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008007 }
8008
8009 // Signal scanning time (SIGNAL_SCANNING_TIME_DATA)
8010 dumpTimer(proto, SystemProto.SIGNAL_SCANNING, getPhoneSignalScanningTimer(), rawRealtimeUs,
8011 which);
8012
8013 // Phone signal strength (SIGNAL_STRENGTH_TIME_DATA and SIGNAL_STRENGTH_COUNT_DATA)
8014 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008015 final long pssToken = proto.start(SystemProto.PHONE_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008016 proto.write(SystemProto.PhoneSignalStrength.NAME, i);
8017 dumpTimer(proto, SystemProto.PhoneSignalStrength.TOTAL, getPhoneSignalStrengthTimer(i),
8018 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008019 proto.end(pssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008020 }
8021
8022 // Wakeup reasons (WAKEUP_REASON_DATA)
8023 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
8024 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07008025 final long wrToken = proto.start(SystemProto.WAKEUP_REASON);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008026 proto.write(SystemProto.WakeupReason.NAME, ent.getKey());
8027 dumpTimer(proto, SystemProto.WakeupReason.TOTAL, ent.getValue(), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008028 proto.end(wrToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008029 }
8030
8031 // Wifi signal strength (WIFI_SIGNAL_STRENGTH_TIME_DATA and WIFI_SIGNAL_STRENGTH_COUNT_DATA)
8032 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008033 final long wssToken = proto.start(SystemProto.WIFI_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008034 proto.write(SystemProto.WifiSignalStrength.NAME, i);
8035 dumpTimer(proto, SystemProto.WifiSignalStrength.TOTAL, getWifiSignalStrengthTimer(i),
8036 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008037 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008038 }
8039
8040 // Wifi state (WIFI_STATE_TIME_DATA and WIFI_STATE_COUNT_DATA)
8041 for (int i = 0; i < NUM_WIFI_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008042 final long wsToken = proto.start(SystemProto.WIFI_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008043 proto.write(SystemProto.WifiState.NAME, i);
8044 dumpTimer(proto, SystemProto.WifiState.TOTAL, getWifiStateTimer(i),
8045 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008046 proto.end(wsToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008047 }
8048
8049 // Wifi supplicant state (WIFI_SUPPL_STATE_TIME_DATA and WIFI_SUPPL_STATE_COUNT_DATA)
8050 for (int i = 0; i < NUM_WIFI_SUPPL_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07008051 final long wssToken = proto.start(SystemProto.WIFI_SUPPLICANT_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008052 proto.write(SystemProto.WifiSupplicantState.NAME, i);
8053 dumpTimer(proto, SystemProto.WifiSupplicantState.TOTAL, getWifiSupplStateTimer(i),
8054 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07008055 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07008056 }
8057
8058 proto.end(sToken);
8059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008060}