blob: 781fada131c62f5ed76101ccad4da4c14336ece3 [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;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070023import android.service.batterystats.BatteryStatsServiceDumpProto;
Wink Saville52840902011-02-18 12:40:47 -080024import android.telephony.SignalStrength;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080025import android.text.format.DateFormat;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070026import android.util.ArrayMap;
James Carr2dd7e5e2016-07-20 18:48:39 -070027import android.util.LongSparseArray;
Dianne Hackborn9cfba352016-03-24 17:31:28 -070028import android.util.MutableBoolean;
29import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.util.Printer;
31import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070032import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070033import android.util.TimeUtils;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070034import android.util.proto.ProtoOutputStream;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070035import android.view.Display;
Amith Yamasaniab9ad192016-12-06 12:46:59 -080036
Sudheer Shankab2f83c12017-11-13 19:25:01 -080037import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080038import com.android.internal.os.BatterySipper;
39import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Kweku Adams2f73ecd2017-09-27 16:59:19 -070041import java.io.FileDescriptor;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070042import java.io.PrintWriter;
43import java.util.ArrayList;
44import java.util.Collections;
45import java.util.Comparator;
46import java.util.Formatter;
47import java.util.HashMap;
48import java.util.List;
49import java.util.Map;
50
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051/**
52 * A class providing access to battery usage statistics, including information on
53 * wakelocks, processes, packages, and services. All times are represented in microseconds
54 * except where indicated otherwise.
55 * @hide
56 */
57public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070058 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
60 private static final boolean LOCAL_LOGV = false;
Bookatz82b341172017-09-07 19:06:08 -070061 /** Fetching RPM stats is too slow to do each time screen changes, so disable it. */
62 protected static final boolean SCREEN_OFF_RPM_STATS_ENABLED = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070063
64 /** @hide */
65 public static final String SERVICE_NAME = "batterystats";
66
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 /**
68 * A constant indicating a partial wake lock timer.
69 */
70 public static final int WAKE_TYPE_PARTIAL = 0;
71
72 /**
73 * A constant indicating a full wake lock timer.
74 */
75 public static final int WAKE_TYPE_FULL = 1;
76
77 /**
78 * A constant indicating a window wake lock timer.
79 */
80 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 /**
83 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 */
85 public static final int SENSOR = 3;
Mike Mac2f518a2017-09-19 16:06:03 -070086
The Android Open Source Project10592532009-03-18 17:39:46 -070087 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070088 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070089 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070090 public static final int WIFI_RUNNING = 4;
Mike Mac2f518a2017-09-19 16:06:03 -070091
Dianne Hackborn617f8772009-03-31 15:04:46 -070092 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070093 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070094 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070095 public static final int FULL_WIFI_LOCK = 5;
Mike Mac2f518a2017-09-19 16:06:03 -070096
The Android Open Source Project10592532009-03-18 17:39:46 -070097 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070098 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070099 */
Nick Pelly6ccaa542012-06-15 15:22:47 -0700100 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
Dianne Hackborn62793e42015-03-09 11:15:41 -0700102 /**
103 * A constant indicating a wifi multicast timer
104 */
105 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700108 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700109 */
110 public static final int VIDEO_TURNED_ON = 8;
111
112 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800113 * A constant indicating a vibrator on timer
114 */
115 public static final int VIBRATOR_ON = 9;
116
117 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700118 * A constant indicating a foreground activity timer
119 */
120 public static final int FOREGROUND_ACTIVITY = 10;
121
122 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700123 * A constant indicating a wifi batched scan is active
124 */
125 public static final int WIFI_BATCHED_SCAN = 11;
126
127 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700128 * A constant indicating a process state timer
129 */
130 public static final int PROCESS_STATE = 12;
131
132 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700133 * A constant indicating a sync timer
134 */
135 public static final int SYNC = 13;
136
137 /**
138 * A constant indicating a job timer
139 */
140 public static final int JOB = 14;
141
142 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800143 * A constant indicating an audio turn on timer
144 */
145 public static final int AUDIO_TURNED_ON = 15;
146
147 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700148 * A constant indicating a flashlight turn on timer
149 */
150 public static final int FLASHLIGHT_TURNED_ON = 16;
151
152 /**
153 * A constant indicating a camera turn on timer
154 */
155 public static final int CAMERA_TURNED_ON = 17;
156
157 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700158 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700159 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700160 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700161
162 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800163 * A constant indicating a bluetooth scan timer.
164 */
165 public static final int BLUETOOTH_SCAN_ON = 19;
166
167 /**
Bookatzc8c44962017-05-11 12:12:54 -0700168 * A constant indicating an aggregated partial wake lock timer.
169 */
170 public static final int AGGREGATED_WAKE_TYPE_PARTIAL = 20;
171
172 /**
Bookatzb1f04f32017-05-19 13:57:32 -0700173 * A constant indicating a bluetooth scan timer for unoptimized scans.
174 */
175 public static final int BLUETOOTH_UNOPTIMIZED_SCAN_ON = 21;
176
177 /**
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700178 * A constant indicating a foreground service timer
179 */
180 public static final int FOREGROUND_SERVICE = 22;
181
182 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 * Include all of the data in the stats, including previously saved data.
184 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700185 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186
187 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 * Include only the current run in the stats.
189 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700190 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191
192 /**
193 * Include only the run since the last time the device was unplugged in the stats.
194 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700195 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700196
197 // NOTE: Update this list if you add/change any stats above.
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700198 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700199 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700200 private static final String[] STAT_NAMES = { "l", "c", "u" };
201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700203 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700204 *
205 * New in version 19:
206 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800207 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700208 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Bookatz506a8182017-05-01 14:18:42 -0700209 * New in version 21:
210 * - Actual (not just apportioned) Wakelock time is also recorded.
Bookatzc8c44962017-05-11 12:12:54 -0700211 * - Aggregated partial wakelock time (per uid, instead of per wakelock) is recorded.
Bookatzb1f04f32017-05-19 13:57:32 -0700212 * - BLE scan result count
213 * - CPU frequency time per uid
214 * New in version 22:
215 * - BLE scan result background count, BLE unoptimized scan time
Bookatz98d4d5c2017-08-01 19:07:54 -0700216 * - Background partial wakelock time & count
217 * New in version 23:
218 * - Logging smeared power model values
219 * New in version 24:
220 * - Fixed bugs in background timers and BLE scan time
221 * New in version 25:
222 * - Package wakeup alarms are now on screen-off timebase
Bookatz50df7112017-08-04 14:53:26 -0700223 * New in version 26:
Bookatz82b341172017-09-07 19:06:08 -0700224 * - Resource power manager (rpm) states [but screenOffRpm is disabled from working properly]
Mike Mac2f518a2017-09-19 16:06:03 -0700225 * New in version 27:
226 * - Always On Display (screen doze mode) time and power
Mike Ma15313c92017-11-15 17:58:21 -0800227 * New in version 28:
228 * - Light/Deep Doze power
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700229 * - WiFi Multicast Wakelock statistics (count & duration)
Kweku Adamsa8943cb2017-12-22 13:21:06 -0800230 * New in version 29:
231 * - Process states re-ordered. TOP_SLEEPING now below BACKGROUND. HEAVY_WEIGHT introduced.
232 * - CPU times per UID process state
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700233 */
Kweku Adamsa8943cb2017-12-22 13:21:06 -0800234 static final int CHECKIN_VERSION = 29;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700235
236 /**
237 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700239 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700240
Evan Millar22ac0432009-03-31 11:33:18 -0700241 private static final long BYTES_PER_KB = 1024;
242 private static final long BYTES_PER_MB = 1048576; // 1024^2
243 private static final long BYTES_PER_GB = 1073741824; //1024^3
Bookatz506a8182017-05-01 14:18:42 -0700244
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700245 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800246 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700247 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700249 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700250 private static final String CPU_DATA = "cpu";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700251 private static final String GLOBAL_CPU_FREQ_DATA = "gcf";
252 private static final String CPU_TIMES_AT_FREQ_DATA = "ctf";
Bookatz50df7112017-08-04 14:53:26 -0700253 // rpm line is:
254 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "rpm", state/voter name, total time, total count,
255 // screen-off time, screen-off count
256 private static final String RESOURCE_POWER_MANAGER_DATA = "rpm";
Evan Millare84de8d2009-04-02 22:16:12 -0700257 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800258 private static final String VIBRATOR_DATA = "vib";
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700259 private static final String FOREGROUND_ACTIVITY_DATA = "fg";
260 // fgs line is:
261 // BATTERY_STATS_CHECKIN_VERSION, uid, category, "fgs",
262 // foreground service time, count
263 private static final String FOREGROUND_SERVICE_DATA = "fgs";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700264 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700265 // wl line is:
266 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
Bookatz5b5ec322017-05-26 09:40:38 -0700267 // full totalTime, 'f', count, current duration, max duration, total duration,
268 // partial totalTime, 'p', count, current duration, max duration, total duration,
269 // bg partial totalTime, 'bp', count, current duration, max duration, total duration,
270 // window totalTime, 'w', count, current duration, max duration, total duration
Bookatz506a8182017-05-01 14:18:42 -0700271 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700272 private static final String WAKELOCK_DATA = "wl";
Bookatzc8c44962017-05-11 12:12:54 -0700273 // awl line is:
274 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "awl",
275 // cumulative partial wakelock duration, cumulative background partial wakelock duration
276 private static final String AGGREGATED_WAKELOCK_DATA = "awl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700277 private static final String SYNC_DATA = "sy";
278 private static final String JOB_DATA = "jb";
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700279 private static final String JOB_COMPLETION_DATA = "jbc";
Evan Millarc64edde2009-04-18 12:26:32 -0700280 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700281 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700282 private static final String NETWORK_DATA = "nt";
283 private static final String USER_ACTIVITY_DATA = "ua";
284 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800285 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700286 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700287 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700288 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800289 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
290 private static final String WIFI_CONTROLLER_DATA = "wfcd";
291 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
292 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700293 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700294 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800295 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800296 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
297 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800298 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700299 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700300 private static final String SCREEN_BRIGHTNESS_DATA = "br";
301 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700302 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700303 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
304 private static final String DATA_CONNECTION_TIME_DATA = "dct";
305 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800306 private static final String WIFI_STATE_TIME_DATA = "wst";
307 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700308 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
309 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
310 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
311 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800312 private static final String POWER_USE_SUMMARY_DATA = "pws";
313 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700314 private static final String DISCHARGE_STEP_DATA = "dsd";
315 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700316 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
317 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700318 private static final String FLASHLIGHT_DATA = "fla";
319 private static final String CAMERA_DATA = "cam";
320 private static final String VIDEO_DATA = "vid";
321 private static final String AUDIO_DATA = "aud";
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700322 private static final String WIFI_MULTICAST_TOTAL_DATA = "wmct";
323 private static final String WIFI_MULTICAST_DATA = "wmc";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324
Adam Lesinski010bf372016-04-11 12:18:18 -0700325 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
326
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700327 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 private final Formatter mFormatter = new Formatter(mFormatBuilder);
329
330 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700331 * Indicates times spent by the uid at each cpu frequency in all process states.
332 *
333 * Other types might include times spent in foreground, background etc.
334 */
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800335 @VisibleForTesting
336 public static final String UID_TIMES_TYPE_ALL = "A";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700337
338 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700339 * State for keeping track of counting information.
340 */
341 public static abstract class Counter {
342
343 /**
344 * Returns the count associated with this Counter for the
345 * selected type of statistics.
346 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700347 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700348 */
Evan Millarc64edde2009-04-18 12:26:32 -0700349 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700350
351 /**
352 * Temporary for debugging.
353 */
354 public abstract void logState(Printer pw, String prefix);
355 }
356
357 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700358 * State for keeping track of long counting information.
359 */
360 public static abstract class LongCounter {
361
362 /**
363 * Returns the count associated with this Counter for the
364 * selected type of statistics.
365 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700366 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700367 */
368 public abstract long getCountLocked(int which);
369
370 /**
371 * Temporary for debugging.
372 */
373 public abstract void logState(Printer pw, String prefix);
374 }
375
376 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700377 * State for keeping track of array of long counting information.
378 */
379 public static abstract class LongCounterArray {
380 /**
381 * Returns the counts associated with this Counter for the
382 * selected type of statistics.
383 *
384 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
385 */
386 public abstract long[] getCountsLocked(int which);
387
388 /**
389 * Temporary for debugging.
390 */
391 public abstract void logState(Printer pw, String prefix);
392 }
393
394 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800395 * Container class that aggregates counters for transmit, receive, and idle state of a
396 * radio controller.
397 */
398 public static abstract class ControllerActivityCounter {
399 /**
400 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
401 * idle state.
402 */
403 public abstract LongCounter getIdleTimeCounter();
404
405 /**
406 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
407 * receive state.
408 */
409 public abstract LongCounter getRxTimeCounter();
410
411 /**
412 * An array of {@link LongCounter}, representing various transmit levels, where each level
413 * may draw a different amount of power. The levels themselves are controller-specific.
414 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
415 * various transmit level states.
416 */
417 public abstract LongCounter[] getTxTimeCounters();
418
419 /**
420 * @return a non-null {@link LongCounter} representing the power consumed by the controller
421 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
422 * yield a value of 0 if the device doesn't support power calculations.
423 */
424 public abstract LongCounter getPowerCounter();
425 }
426
427 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 * State for keeping track of timing information.
429 */
430 public static abstract class Timer {
431
432 /**
433 * Returns the count associated with this Timer for the
434 * selected type of statistics.
435 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700436 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 */
Evan Millarc64edde2009-04-18 12:26:32 -0700438 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439
440 /**
441 * Returns the total time in microseconds associated with this Timer for the
442 * selected type of statistics.
443 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800444 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700445 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 * @return a time in microseconds
447 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800448 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700451 * Returns the total time in microseconds associated with this Timer since the
452 * 'mark' was last set.
453 *
454 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
455 * @return a time in microseconds
456 */
457 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
458
459 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700460 * Returns the max duration if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700461 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700462 */
463 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
464 return -1;
465 }
466
467 /**
468 * Returns the current time the timer has been active, if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700469 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700470 */
471 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
472 return -1;
473 }
474
475 /**
Kweku Adams103351f2017-10-16 14:39:34 -0700476 * Returns the total time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800477 *
478 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
479 * been on since reset.
480 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
481 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
482 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
483 * the actual total time.
Kweku Adams103351f2017-10-16 14:39:34 -0700484 * Not all Timer subclasses track the max, total, and current durations.
Bookatz867c0d72017-03-07 18:23:42 -0800485 */
486 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
487 return -1;
488 }
489
490 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700491 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
492 * used, for example, for tracking background usage. Secondary timers are never pooled.
493 *
494 * Not all Timer subclasses have a secondary timer; those that don't return null.
495 */
496 public Timer getSubTimer() {
497 return null;
498 }
499
500 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700501 * Returns whether the timer is currently running. Some types of timers
502 * (e.g. BatchTimers) don't know whether the event is currently active,
503 * and report false.
504 */
505 public boolean isRunningLocked() {
506 return false;
507 }
508
509 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 * Temporary for debugging.
511 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700512 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 }
514
515 /**
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800516 * Maps the ActivityManager procstate into corresponding BatteryStats procstate.
517 */
518 public static int mapToInternalProcessState(int procState) {
519 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
520 return ActivityManager.PROCESS_STATE_NONEXISTENT;
521 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
522 return Uid.PROCESS_STATE_TOP;
523 } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
524 // Persistent and other foreground states go here.
525 return Uid.PROCESS_STATE_FOREGROUND_SERVICE;
526 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
527 // Persistent and other foreground states go here.
528 return Uid.PROCESS_STATE_FOREGROUND;
529 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
530 return Uid.PROCESS_STATE_BACKGROUND;
531 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
532 return Uid.PROCESS_STATE_TOP_SLEEPING;
533 } else if (procState <= ActivityManager.PROCESS_STATE_HEAVY_WEIGHT) {
534 return Uid.PROCESS_STATE_HEAVY_WEIGHT;
535 } else {
536 return Uid.PROCESS_STATE_CACHED;
537 }
538 }
539
540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 * The statistics associated with a particular uid.
542 */
543 public static abstract class Uid {
544
545 /**
546 * Returns a mapping containing wakelock statistics.
547 *
548 * @return a Map from Strings to Uid.Wakelock objects.
549 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700550 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551
552 /**
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700553 * Returns the WiFi Multicast Wakelock statistics.
554 *
555 * @return a Timer Object for the per uid Multicast statistics.
556 */
557 public abstract Timer getMulticastWakelockStats();
558
559 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700560 * Returns a mapping containing sync statistics.
561 *
562 * @return a Map from Strings to Timer objects.
563 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700564 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700565
566 /**
567 * Returns a mapping containing scheduled job statistics.
568 *
569 * @return a Map from Strings to Timer objects.
570 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700571 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700572
573 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700574 * Returns statistics about how jobs have completed.
575 *
576 * @return A Map of String job names to completion type -> count mapping.
577 */
578 public abstract ArrayMap<String, SparseIntArray> getJobCompletionStats();
579
580 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 * The statistics associated with a particular wake lock.
582 */
583 public static abstract class Wakelock {
584 public abstract Timer getWakeTime(int type);
585 }
586
587 /**
Bookatzc8c44962017-05-11 12:12:54 -0700588 * The cumulative time the uid spent holding any partial wakelocks. This will generally
589 * differ from summing over the Wakelocks in getWakelockStats since the latter may have
590 * wakelocks that overlap in time (and therefore over-counts).
591 */
592 public abstract Timer getAggregatedPartialWakelockTimer();
593
594 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 * Returns a mapping containing sensor statistics.
596 *
597 * @return a Map from Integer sensor ids to Uid.Sensor objects.
598 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700599 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600
601 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700602 * Returns a mapping containing active process data.
603 */
604 public abstract SparseArray<? extends Pid> getPidStats();
Bookatzc8c44962017-05-11 12:12:54 -0700605
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700606 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 * Returns a mapping containing process statistics.
608 *
609 * @return a Map from Strings to Uid.Proc objects.
610 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700611 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612
613 /**
614 * Returns a mapping containing package statistics.
615 *
616 * @return a Map from Strings to Uid.Pkg objects.
617 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700618 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700619
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800620 public abstract ControllerActivityCounter getWifiControllerActivity();
621 public abstract ControllerActivityCounter getBluetoothControllerActivity();
622 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800623
624 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 * {@hide}
626 */
627 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700628
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800629 public abstract void noteWifiRunningLocked(long elapsedRealtime);
630 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
631 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
632 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
633 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
634 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
635 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
636 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
637 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
638 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800639 public abstract void noteActivityResumedLocked(long elapsedRealtime);
640 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800641 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
642 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
643 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700644 public abstract int getWifiScanCount(int which);
Kweku Adams103351f2017-10-16 14:39:34 -0700645 /**
646 * Returns the timer keeping track of wifi scans.
647 */
648 public abstract Timer getWifiScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800649 public abstract int getWifiScanBackgroundCount(int which);
650 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
651 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -0700652 /**
653 * Returns the timer keeping track of background wifi scans.
654 */
655 public abstract Timer getWifiScanBackgroundTimer();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800656 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700657 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800658 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700659 public abstract Timer getAudioTurnedOnTimer();
660 public abstract Timer getVideoTurnedOnTimer();
661 public abstract Timer getFlashlightTurnedOnTimer();
662 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700663 public abstract Timer getForegroundActivityTimer();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700664
665 /**
666 * Returns the timer keeping track of Foreground Service time
667 */
668 public abstract Timer getForegroundServiceTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800669 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800670 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatzb1f04f32017-05-19 13:57:32 -0700671 public abstract Timer getBluetoothUnoptimizedScanTimer();
672 public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700673 public abstract Counter getBluetoothScanResultCounter();
Bookatzb1f04f32017-05-19 13:57:32 -0700674 public abstract Counter getBluetoothScanResultBgCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700675
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700676 public abstract long[] getCpuFreqTimes(int which);
677 public abstract long[] getScreenOffCpuFreqTimes(int which);
678
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800679 /**
680 * Returns cpu times of an uid at a particular process state.
681 */
682 public abstract long[] getCpuFreqTimes(int which, int procState);
683 /**
684 * Returns cpu times of an uid while the screen if off at a particular process state.
685 */
686 public abstract long[] getScreenOffCpuFreqTimes(int which, int procState);
687
Dianne Hackborna0200e32016-03-30 18:01:41 -0700688 // Note: the following times are disjoint. They can be added together to find the
689 // total time a uid has had any processes running at all.
690
691 /**
692 * Time this uid has any processes in the top state (or above such as persistent).
693 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800694 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700695 /**
696 * Time this uid has any process with a started out bound foreground service, but
697 * none in the "top" state.
698 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800699 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700700 /**
Dianne Hackborna0200e32016-03-30 18:01:41 -0700701 * Time this uid has any process in an active foreground state, but none in the
702 * "top sleeping" or better state.
703 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800704 public static final int PROCESS_STATE_FOREGROUND = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700705 /**
706 * Time this uid has any process in an active background state, but none in the
707 * "foreground" or better state.
708 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800709 public static final int PROCESS_STATE_BACKGROUND = 3;
710 /**
711 * Time this uid has any process that is top while the device is sleeping, but not
712 * active for any other reason. We kind-of consider it a kind of cached process
713 * for execution restrictions.
714 */
715 public static final int PROCESS_STATE_TOP_SLEEPING = 4;
716 /**
717 * Time this uid has any process that is in the background but it has an activity
718 * marked as "can't save state". This is essentially a cached process, though the
719 * system will try much harder than normal to avoid killing it.
720 */
721 public static final int PROCESS_STATE_HEAVY_WEIGHT = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700722 /**
723 * Time this uid has any processes that are sitting around cached, not in one of the
724 * other active states.
725 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800726 public static final int PROCESS_STATE_CACHED = 6;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700727 /**
728 * Total number of process states we track.
729 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800730 public static final int NUM_PROCESS_STATE = 7;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700731
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800732 // Used in dump
Dianne Hackborn61659e52014-07-09 16:13:01 -0700733 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800734 "Top", "Fg Service", "Foreground", "Background", "Top Sleeping", "Heavy Weight",
735 "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700736 };
737
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800738 // Used in checkin dump
739 @VisibleForTesting
740 public static final String[] UID_PROCESS_TYPES = {
741 "T", // TOP
742 "FS", // FOREGROUND_SERVICE
743 "F", // FOREGROUND
744 "B", // BACKGROUND
745 "TS", // TOP_SLEEPING
746 "HW", // HEAVY_WEIGHT
747 "C" // CACHED
748 };
749
750 /**
751 * When the process exits one of these states, we need to make sure cpu time in this state
752 * is not attributed to any non-critical process states.
753 */
754 public static final int[] CRITICAL_PROC_STATES = {
755 PROCESS_STATE_TOP, PROCESS_STATE_FOREGROUND_SERVICE, PROCESS_STATE_FOREGROUND
756 };
757
Dianne Hackborn61659e52014-07-09 16:13:01 -0700758 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800759 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700760
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800761 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762
Robert Greenwalta029ea12013-09-25 16:38:12 -0700763 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
764
Dianne Hackborn617f8772009-03-31 15:04:46 -0700765 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700766 * Note that these must match the constants in android.os.PowerManager.
767 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
768 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700769 */
770 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700771 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700772 };
Bookatzc8c44962017-05-11 12:12:54 -0700773
Phil Weaverda80d672016-03-15 16:25:46 -0700774 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700775
Dianne Hackborn617f8772009-03-31 15:04:46 -0700776 public abstract void noteUserActivityLocked(int type);
777 public abstract boolean hasUserActivity();
778 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700779
780 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800781 public abstract long getNetworkActivityBytes(int type, int which);
782 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800783 public abstract long getMobileRadioActiveTime(int which);
784 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700785
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700786 /**
787 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
788 */
789 public abstract long getUserCpuTimeUs(int which);
790
791 /**
792 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
793 */
794 public abstract long getSystemCpuTimeUs(int which);
795
796 /**
Sudheer Shanka71f34b32017-07-21 00:14:24 -0700797 * Returns the approximate cpu time (in microseconds) spent at a certain CPU speed for a
Adam Lesinski6832f392015-09-05 18:05:40 -0700798 * given CPU cluster.
799 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700800 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700801 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700802 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
803 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700804 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700805 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700806
Adam Lesinski5f056f62016-07-14 16:56:08 -0700807 /**
808 * Returns the number of times this UID woke up the Application Processor to
809 * process a mobile radio packet.
810 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
811 */
812 public abstract long getMobileRadioApWakeupCount(int which);
813
814 /**
815 * Returns the number of times this UID woke up the Application Processor to
816 * process a WiFi packet.
817 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
818 */
819 public abstract long getWifiRadioApWakeupCount(int which);
820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800822 /*
823 * FIXME: it's not correct to use this magic value because it
824 * could clash with a sensor handle (which are defined by
825 * the sensor HAL, and therefore out of our control
826 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 // Magic sensor number for the GPS.
828 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800833
Bookatz867c0d72017-03-07 18:23:42 -0800834 /** Returns a Timer for sensor usage when app is in the background. */
835 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 }
837
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700838 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800839 public int mWakeNesting;
840 public long mWakeSumMs;
841 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700842 }
843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 /**
845 * The statistics associated with a particular process.
846 */
847 public static abstract class Proc {
848
Dianne Hackborn287952c2010-09-22 22:34:31 -0700849 public static class ExcessivePower {
850 public static final int TYPE_WAKE = 1;
851 public static final int TYPE_CPU = 2;
852
853 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700854 public long overTime;
855 public long usedTime;
856 }
857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800859 * Returns true if this process is still active in the battery stats.
860 */
861 public abstract boolean isActive();
862
863 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700864 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700866 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 */
868 public abstract long getUserTime(int which);
869
870 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700871 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700873 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 */
875 public abstract long getSystemTime(int which);
876
877 /**
878 * Returns the number of times the process has been started.
879 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700880 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 */
882 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700883
884 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800885 * Returns the number of times the process has crashed.
886 *
887 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
888 */
889 public abstract int getNumCrashes(int which);
890
891 /**
892 * Returns the number of times the process has ANRed.
893 *
894 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
895 */
896 public abstract int getNumAnrs(int which);
897
898 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700899 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700900 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700901 * @return foreground cpu time in microseconds
902 */
903 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700904
Dianne Hackborn287952c2010-09-22 22:34:31 -0700905 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700906
Dianne Hackborn287952c2010-09-22 22:34:31 -0700907 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 }
909
910 /**
911 * The statistics associated with a particular package.
912 */
913 public static abstract class Pkg {
914
915 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700916 * Returns information about all wakeup alarms that have been triggered for this
917 * package. The mapping keys are tag names for the alarms, the counter contains
918 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700920 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921
922 /**
923 * Returns a mapping containing service statistics.
924 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700925 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926
927 /**
928 * The statistics associated with a particular service.
929 */
Joe Onoratoabded112016-02-08 16:49:39 -0800930 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931
932 /**
933 * Returns the amount of time spent started.
934 *
935 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700936 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 * @return
938 */
939 public abstract long getStartTime(long batteryUptime, int which);
940
941 /**
942 * Returns the total number of times startService() has been called.
943 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700944 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 */
946 public abstract int getStarts(int which);
947
948 /**
949 * Returns the total number times the service has been launched.
950 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700951 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 */
953 public abstract int getLaunches(int which);
954 }
955 }
956 }
957
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800958 public static final class LevelStepTracker {
959 public long mLastStepTime = -1;
960 public int mNumStepDurations;
961 public final long[] mStepDurations;
962
963 public LevelStepTracker(int maxLevelSteps) {
964 mStepDurations = new long[maxLevelSteps];
965 }
966
967 public LevelStepTracker(int numSteps, long[] steps) {
968 mNumStepDurations = numSteps;
969 mStepDurations = new long[numSteps];
970 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
971 }
972
973 public long getDurationAt(int index) {
974 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
975 }
976
977 public int getLevelAt(int index) {
978 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
979 >> STEP_LEVEL_LEVEL_SHIFT);
980 }
981
982 public int getInitModeAt(int index) {
983 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
984 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
985 }
986
987 public int getModModeAt(int index) {
988 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
989 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
990 }
991
992 private void appendHex(long val, int topOffset, StringBuilder out) {
993 boolean hasData = false;
994 while (topOffset >= 0) {
995 int digit = (int)( (val>>topOffset) & 0xf );
996 topOffset -= 4;
997 if (!hasData && digit == 0) {
998 continue;
999 }
1000 hasData = true;
1001 if (digit >= 0 && digit <= 9) {
1002 out.append((char)('0' + digit));
1003 } else {
1004 out.append((char)('a' + digit - 10));
1005 }
1006 }
1007 }
1008
1009 public void encodeEntryAt(int index, StringBuilder out) {
1010 long item = mStepDurations[index];
1011 long duration = item & STEP_LEVEL_TIME_MASK;
1012 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
1013 >> STEP_LEVEL_LEVEL_SHIFT);
1014 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
1015 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1016 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
1017 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1018 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1019 case Display.STATE_OFF: out.append('f'); break;
1020 case Display.STATE_ON: out.append('o'); break;
1021 case Display.STATE_DOZE: out.append('d'); break;
1022 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
1023 }
1024 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1025 out.append('p');
1026 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001027 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1028 out.append('i');
1029 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001030 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1031 case Display.STATE_OFF: out.append('F'); break;
1032 case Display.STATE_ON: out.append('O'); break;
1033 case Display.STATE_DOZE: out.append('D'); break;
1034 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
1035 }
1036 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1037 out.append('P');
1038 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001039 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1040 out.append('I');
1041 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001042 out.append('-');
1043 appendHex(level, 4, out);
1044 out.append('-');
1045 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
1046 }
1047
1048 public void decodeEntryAt(int index, String value) {
1049 final int N = value.length();
1050 int i = 0;
1051 char c;
1052 long out = 0;
1053 while (i < N && (c=value.charAt(i)) != '-') {
1054 i++;
1055 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001056 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001057 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001058 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001059 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001060 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001061 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001062 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1063 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1064 break;
1065 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1066 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1067 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001068 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1069 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1070 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001071 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1072 break;
1073 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1074 break;
1075 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1076 break;
1077 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1078 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1079 break;
1080 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1081 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001082 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001083 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1084 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1085 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001086 }
1087 }
1088 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001089 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001090 while (i < N && (c=value.charAt(i)) != '-') {
1091 i++;
1092 level <<= 4;
1093 if (c >= '0' && c <= '9') {
1094 level += c - '0';
1095 } else if (c >= 'a' && c <= 'f') {
1096 level += c - 'a' + 10;
1097 } else if (c >= 'A' && c <= 'F') {
1098 level += c - 'A' + 10;
1099 }
1100 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001101 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001102 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
1103 long duration = 0;
1104 while (i < N && (c=value.charAt(i)) != '-') {
1105 i++;
1106 duration <<= 4;
1107 if (c >= '0' && c <= '9') {
1108 duration += c - '0';
1109 } else if (c >= 'a' && c <= 'f') {
1110 duration += c - 'a' + 10;
1111 } else if (c >= 'A' && c <= 'F') {
1112 duration += c - 'A' + 10;
1113 }
1114 }
1115 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
1116 }
1117
1118 public void init() {
1119 mLastStepTime = -1;
1120 mNumStepDurations = 0;
1121 }
1122
1123 public void clearTime() {
1124 mLastStepTime = -1;
1125 }
1126
1127 public long computeTimePerLevel() {
1128 final long[] steps = mStepDurations;
1129 final int numSteps = mNumStepDurations;
1130
1131 // For now we'll do a simple average across all steps.
1132 if (numSteps <= 0) {
1133 return -1;
1134 }
1135 long total = 0;
1136 for (int i=0; i<numSteps; i++) {
1137 total += steps[i] & STEP_LEVEL_TIME_MASK;
1138 }
1139 return total / numSteps;
1140 /*
1141 long[] buckets = new long[numSteps];
1142 int numBuckets = 0;
1143 int numToAverage = 4;
1144 int i = 0;
1145 while (i < numSteps) {
1146 long totalTime = 0;
1147 int num = 0;
1148 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1149 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1150 num++;
1151 }
1152 buckets[numBuckets] = totalTime / num;
1153 numBuckets++;
1154 numToAverage *= 2;
1155 i += num;
1156 }
1157 if (numBuckets < 1) {
1158 return -1;
1159 }
1160 long averageTime = buckets[numBuckets-1];
1161 for (i=numBuckets-2; i>=0; i--) {
1162 averageTime = (averageTime + buckets[i]) / 2;
1163 }
1164 return averageTime;
1165 */
1166 }
1167
1168 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1169 int[] outNumOfInterest) {
1170 final long[] steps = mStepDurations;
1171 final int count = mNumStepDurations;
1172 if (count <= 0) {
1173 return -1;
1174 }
1175 long total = 0;
1176 int numOfInterest = 0;
1177 for (int i=0; i<count; i++) {
1178 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1179 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1180 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1181 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1182 // If the modes of interest didn't change during this step period...
1183 if ((modMode&modesOfInterest) == 0) {
1184 // And the mode values during this period match those we are measuring...
1185 if ((initMode&modesOfInterest) == modeValues) {
1186 // Then this can be used to estimate the total time!
1187 numOfInterest++;
1188 total += steps[i] & STEP_LEVEL_TIME_MASK;
1189 }
1190 }
1191 }
1192 if (numOfInterest <= 0) {
1193 return -1;
1194 }
1195
1196 if (outNumOfInterest != null) {
1197 outNumOfInterest[0] = numOfInterest;
1198 }
1199
1200 // The estimated time is the average time we spend in each level, multipled
1201 // by 100 -- the total number of battery levels
1202 return (total / numOfInterest) * 100;
1203 }
1204
1205 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1206 int stepCount = mNumStepDurations;
1207 final long lastStepTime = mLastStepTime;
1208 if (lastStepTime >= 0 && numStepLevels > 0) {
1209 final long[] steps = mStepDurations;
1210 long duration = elapsedRealtime - lastStepTime;
1211 for (int i=0; i<numStepLevels; i++) {
1212 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1213 long thisDuration = duration / (numStepLevels-i);
1214 duration -= thisDuration;
1215 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1216 thisDuration = STEP_LEVEL_TIME_MASK;
1217 }
1218 steps[0] = thisDuration | modeBits;
1219 }
1220 stepCount += numStepLevels;
1221 if (stepCount > steps.length) {
1222 stepCount = steps.length;
1223 }
1224 }
1225 mNumStepDurations = stepCount;
1226 mLastStepTime = elapsedRealtime;
1227 }
1228
1229 public void readFromParcel(Parcel in) {
1230 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001231 if (N > mStepDurations.length) {
1232 throw new ParcelFormatException("more step durations than available: " + N);
1233 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001234 mNumStepDurations = N;
1235 for (int i=0; i<N; i++) {
1236 mStepDurations[i] = in.readLong();
1237 }
1238 }
1239
1240 public void writeToParcel(Parcel out) {
1241 final int N = mNumStepDurations;
1242 out.writeInt(N);
1243 for (int i=0; i<N; i++) {
1244 out.writeLong(mStepDurations[i]);
1245 }
1246 }
1247 }
1248
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001249 public static final class PackageChange {
1250 public String mPackageName;
1251 public boolean mUpdate;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001252 public long mVersionCode;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001253 }
1254
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001255 public static final class DailyItem {
1256 public long mStartTime;
1257 public long mEndTime;
1258 public LevelStepTracker mDischargeSteps;
1259 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001260 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001261 }
1262
1263 public abstract DailyItem getDailyItemLocked(int daysAgo);
1264
1265 public abstract long getCurrentDailyStartTime();
1266
1267 public abstract long getNextMinDailyDeadline();
1268
1269 public abstract long getNextMaxDailyDeadline();
1270
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001271 public abstract long[] getCpuFreqs();
1272
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001273 public final static class HistoryTag {
1274 public String string;
1275 public int uid;
1276
1277 public int poolIdx;
1278
1279 public void setTo(HistoryTag o) {
1280 string = o.string;
1281 uid = o.uid;
1282 poolIdx = o.poolIdx;
1283 }
1284
1285 public void setTo(String _string, int _uid) {
1286 string = _string;
1287 uid = _uid;
1288 poolIdx = -1;
1289 }
1290
1291 public void writeToParcel(Parcel dest, int flags) {
1292 dest.writeString(string);
1293 dest.writeInt(uid);
1294 }
1295
1296 public void readFromParcel(Parcel src) {
1297 string = src.readString();
1298 uid = src.readInt();
1299 poolIdx = -1;
1300 }
1301
1302 @Override
1303 public boolean equals(Object o) {
1304 if (this == o) return true;
1305 if (o == null || getClass() != o.getClass()) return false;
1306
1307 HistoryTag that = (HistoryTag) o;
1308
1309 if (uid != that.uid) return false;
1310 if (!string.equals(that.string)) return false;
1311
1312 return true;
1313 }
1314
1315 @Override
1316 public int hashCode() {
1317 int result = string.hashCode();
1318 result = 31 * result + uid;
1319 return result;
1320 }
1321 }
1322
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001323 /**
1324 * Optional detailed information that can go into a history step. This is typically
1325 * generated each time the battery level changes.
1326 */
1327 public final static class HistoryStepDetails {
1328 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1329 public int userTime;
1330 public int systemTime;
1331
1332 // Top three apps using CPU in the last step, with times in 1/100 second.
1333 public int appCpuUid1;
1334 public int appCpuUTime1;
1335 public int appCpuSTime1;
1336 public int appCpuUid2;
1337 public int appCpuUTime2;
1338 public int appCpuSTime2;
1339 public int appCpuUid3;
1340 public int appCpuUTime3;
1341 public int appCpuSTime3;
1342
1343 // Information from /proc/stat
1344 public int statUserTime;
1345 public int statSystemTime;
1346 public int statIOWaitTime;
1347 public int statIrqTime;
1348 public int statSoftIrqTime;
1349 public int statIdlTime;
1350
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001351 // Platform-level low power state stats
1352 public String statPlatformIdleState;
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001353 public String statSubsystemPowerState;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001354
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001355 public HistoryStepDetails() {
1356 clear();
1357 }
1358
1359 public void clear() {
1360 userTime = systemTime = 0;
1361 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1362 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1363 = appCpuUTime3 = appCpuSTime3 = 0;
1364 }
1365
1366 public void writeToParcel(Parcel out) {
1367 out.writeInt(userTime);
1368 out.writeInt(systemTime);
1369 out.writeInt(appCpuUid1);
1370 out.writeInt(appCpuUTime1);
1371 out.writeInt(appCpuSTime1);
1372 out.writeInt(appCpuUid2);
1373 out.writeInt(appCpuUTime2);
1374 out.writeInt(appCpuSTime2);
1375 out.writeInt(appCpuUid3);
1376 out.writeInt(appCpuUTime3);
1377 out.writeInt(appCpuSTime3);
1378 out.writeInt(statUserTime);
1379 out.writeInt(statSystemTime);
1380 out.writeInt(statIOWaitTime);
1381 out.writeInt(statIrqTime);
1382 out.writeInt(statSoftIrqTime);
1383 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001384 out.writeString(statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001385 out.writeString(statSubsystemPowerState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001386 }
1387
1388 public void readFromParcel(Parcel in) {
1389 userTime = in.readInt();
1390 systemTime = in.readInt();
1391 appCpuUid1 = in.readInt();
1392 appCpuUTime1 = in.readInt();
1393 appCpuSTime1 = in.readInt();
1394 appCpuUid2 = in.readInt();
1395 appCpuUTime2 = in.readInt();
1396 appCpuSTime2 = in.readInt();
1397 appCpuUid3 = in.readInt();
1398 appCpuUTime3 = in.readInt();
1399 appCpuSTime3 = in.readInt();
1400 statUserTime = in.readInt();
1401 statSystemTime = in.readInt();
1402 statIOWaitTime = in.readInt();
1403 statIrqTime = in.readInt();
1404 statSoftIrqTime = in.readInt();
1405 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001406 statPlatformIdleState = in.readString();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001407 statSubsystemPowerState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001408 }
1409 }
1410
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001411 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001412 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001413
1414 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001415 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001416
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001417 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001418 public static final byte CMD_NULL = -1;
1419 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001420 public static final byte CMD_CURRENT_TIME = 5;
1421 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001422 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001423 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001424
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001425 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001426
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001427 /**
1428 * Return whether the command code is a delta data update.
1429 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001430 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001431 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001432 }
1433
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001434 public byte batteryLevel;
1435 public byte batteryStatus;
1436 public byte batteryHealth;
1437 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001438
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001439 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001440 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001441
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001442 // The charge of the battery in micro-Ampere-hours.
1443 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001444
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001445 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001446 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001447 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001448 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001449 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1450 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001451 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001452 public static final int STATE_PHONE_STATE_SHIFT = 6;
1453 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001454 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001455 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001456 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001457
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001458 // These states always appear directly in the first int token
1459 // of a delta change; they should be ones that change relatively
1460 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001461 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1462 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001463 public static final int STATE_GPS_ON_FLAG = 1<<29;
1464 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001465 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001466 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001467 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001468 // Do not use, this is used for coulomb delta count.
1469 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001470 // These are on the lower bits used for the command; if they change
1471 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001472 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001473 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001474 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001475 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1476 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
Mike Mac2f518a2017-09-19 16:06:03 -07001477 public static final int STATE_SCREEN_DOZE_FLAG = 1 << 18;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001478 // empty slot
1479 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001480
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001481 public static final int MOST_INTERESTING_STATES =
Mike Mac2f518a2017-09-19 16:06:03 -07001482 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG | STATE_SCREEN_DOZE_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001483
1484 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001485
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001486 public int states;
1487
Dianne Hackborn3251b902014-06-20 14:40:53 -07001488 // Constants from WIFI_SUPPL_STATE_*
1489 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1490 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1491 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1492 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1493 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1494 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1495
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001496 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001497 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1498 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1499 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001500 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001501 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1502 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1503 public static final int STATE2_CHARGING_FLAG = 1<<24;
1504 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1505 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1506 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001507 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001508
1509 public static final int MOST_INTERESTING_STATES2 =
Mike Mac2f518a2017-09-19 16:06:03 -07001510 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
1511 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001512
1513 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001514
Dianne Hackborn40c87252014-03-19 16:55:40 -07001515 public int states2;
1516
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001517 // The wake lock that was acquired at this point.
1518 public HistoryTag wakelockTag;
1519
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001520 // Kernel wakeup reason at this point.
1521 public HistoryTag wakeReasonTag;
1522
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001523 // Non-null when there is more detailed information at this step.
1524 public HistoryStepDetails stepDetails;
1525
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001526 public static final int EVENT_FLAG_START = 0x8000;
1527 public static final int EVENT_FLAG_FINISH = 0x4000;
1528
1529 // No event in this item.
1530 public static final int EVENT_NONE = 0x0000;
1531 // Event is about a process that is running.
1532 public static final int EVENT_PROC = 0x0001;
1533 // Event is about an application package that is in the foreground.
1534 public static final int EVENT_FOREGROUND = 0x0002;
1535 // Event is about an application package that is at the top of the screen.
1536 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001537 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001538 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001539 // Events for all additional wake locks aquired/release within a wake block.
1540 // These are not generated by default.
1541 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001542 // Event is about an application executing a scheduled job.
1543 public static final int EVENT_JOB = 0x0006;
1544 // Events for users running.
1545 public static final int EVENT_USER_RUNNING = 0x0007;
1546 // Events for foreground user.
1547 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001548 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001549 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001550 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001551 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001552 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001553 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001554 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001555 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001556 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001557 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001558 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001559 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001560 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001561 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001562 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001563 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001564 // Event for a package being on the temporary whitelist.
1565 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001566 // Event for the screen waking up.
1567 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001568 // Event for the UID that woke up the application processor.
1569 // Used for wakeups coming from WiFi, modem, etc.
1570 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001571 // Event for reporting that a specific partial wake lock has been held for a long duration.
1572 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001573
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001574 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001575 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001576 // Mask to extract out only the type part of the event.
1577 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001578
1579 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1580 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1581 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1582 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1583 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1584 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001585 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1586 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001587 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1588 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001589 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1590 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1591 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1592 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1593 public static final int EVENT_USER_FOREGROUND_START =
1594 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1595 public static final int EVENT_USER_FOREGROUND_FINISH =
1596 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001597 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1598 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001599 public static final int EVENT_TEMP_WHITELIST_START =
1600 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1601 public static final int EVENT_TEMP_WHITELIST_FINISH =
1602 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001603 public static final int EVENT_LONG_WAKE_LOCK_START =
1604 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1605 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1606 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001607
1608 // For CMD_EVENT.
1609 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001610 public HistoryTag eventTag;
1611
Dianne Hackborn9a755432014-05-15 17:05:22 -07001612 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001613 public long currentTime;
1614
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001615 // Meta-data when reading.
1616 public int numReadInts;
1617
1618 // Pre-allocated objects.
1619 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001620 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001621 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001622
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001623 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001624 }
Bookatzc8c44962017-05-11 12:12:54 -07001625
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001626 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001627 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001628 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001629 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001630 }
Bookatzc8c44962017-05-11 12:12:54 -07001631
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001632 public int describeContents() {
1633 return 0;
1634 }
1635
1636 public void writeToParcel(Parcel dest, int flags) {
1637 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001638 int bat = (((int)cmd)&0xff)
1639 | ((((int)batteryLevel)<<8)&0xff00)
1640 | ((((int)batteryStatus)<<16)&0xf0000)
1641 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001642 | ((((int)batteryPlugType)<<24)&0xf000000)
1643 | (wakelockTag != null ? 0x10000000 : 0)
1644 | (wakeReasonTag != null ? 0x20000000 : 0)
1645 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001646 dest.writeInt(bat);
1647 bat = (((int)batteryTemperature)&0xffff)
1648 | ((((int)batteryVoltage)<<16)&0xffff0000);
1649 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001650 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001651 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001652 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001653 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001654 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001655 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001656 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001657 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001658 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001659 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001660 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001661 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001662 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001663 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001664 dest.writeLong(currentTime);
1665 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001666 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001667
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001668 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001669 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001670 int bat = src.readInt();
1671 cmd = (byte)(bat&0xff);
1672 batteryLevel = (byte)((bat>>8)&0xff);
1673 batteryStatus = (byte)((bat>>16)&0xf);
1674 batteryHealth = (byte)((bat>>20)&0xf);
1675 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001676 int bat2 = src.readInt();
1677 batteryTemperature = (short)(bat2&0xffff);
1678 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001679 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001680 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001681 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001682 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001683 wakelockTag = localWakelockTag;
1684 wakelockTag.readFromParcel(src);
1685 } else {
1686 wakelockTag = null;
1687 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001688 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001689 wakeReasonTag = localWakeReasonTag;
1690 wakeReasonTag.readFromParcel(src);
1691 } else {
1692 wakeReasonTag = null;
1693 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001694 if ((bat&0x40000000) != 0) {
1695 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001696 eventTag = localEventTag;
1697 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001698 } else {
1699 eventCode = EVENT_NONE;
1700 eventTag = null;
1701 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001702 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001703 currentTime = src.readLong();
1704 } else {
1705 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001706 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001707 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001708 }
1709
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001710 public void clear() {
1711 time = 0;
1712 cmd = CMD_NULL;
1713 batteryLevel = 0;
1714 batteryStatus = 0;
1715 batteryHealth = 0;
1716 batteryPlugType = 0;
1717 batteryTemperature = 0;
1718 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001719 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001720 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001721 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001722 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001723 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001724 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001725 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001726 }
Bookatzc8c44962017-05-11 12:12:54 -07001727
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001728 public void setTo(HistoryItem o) {
1729 time = o.time;
1730 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001731 setToCommon(o);
1732 }
1733
1734 public void setTo(long time, byte cmd, HistoryItem o) {
1735 this.time = time;
1736 this.cmd = cmd;
1737 setToCommon(o);
1738 }
1739
1740 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001741 batteryLevel = o.batteryLevel;
1742 batteryStatus = o.batteryStatus;
1743 batteryHealth = o.batteryHealth;
1744 batteryPlugType = o.batteryPlugType;
1745 batteryTemperature = o.batteryTemperature;
1746 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001747 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001748 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001749 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001750 if (o.wakelockTag != null) {
1751 wakelockTag = localWakelockTag;
1752 wakelockTag.setTo(o.wakelockTag);
1753 } else {
1754 wakelockTag = null;
1755 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001756 if (o.wakeReasonTag != null) {
1757 wakeReasonTag = localWakeReasonTag;
1758 wakeReasonTag.setTo(o.wakeReasonTag);
1759 } else {
1760 wakeReasonTag = null;
1761 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001762 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001763 if (o.eventTag != null) {
1764 eventTag = localEventTag;
1765 eventTag.setTo(o.eventTag);
1766 } else {
1767 eventTag = null;
1768 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001769 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001770 }
1771
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001772 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001773 return batteryLevel == o.batteryLevel
1774 && batteryStatus == o.batteryStatus
1775 && batteryHealth == o.batteryHealth
1776 && batteryPlugType == o.batteryPlugType
1777 && batteryTemperature == o.batteryTemperature
1778 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001779 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001780 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001781 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001782 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001783 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001784
1785 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001786 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001787 return false;
1788 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001789 if (wakelockTag != o.wakelockTag) {
1790 if (wakelockTag == null || o.wakelockTag == null) {
1791 return false;
1792 }
1793 if (!wakelockTag.equals(o.wakelockTag)) {
1794 return false;
1795 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001796 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001797 if (wakeReasonTag != o.wakeReasonTag) {
1798 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1799 return false;
1800 }
1801 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1802 return false;
1803 }
1804 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001805 if (eventTag != o.eventTag) {
1806 if (eventTag == null || o.eventTag == null) {
1807 return false;
1808 }
1809 if (!eventTag.equals(o.eventTag)) {
1810 return false;
1811 }
1812 }
1813 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001814 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001815 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001816
1817 public final static class HistoryEventTracker {
1818 private final HashMap<String, SparseIntArray>[] mActiveEvents
1819 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1820
1821 public boolean updateState(int code, String name, int uid, int poolIdx) {
1822 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1823 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1824 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1825 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001826 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001827 mActiveEvents[idx] = active;
1828 }
1829 SparseIntArray uids = active.get(name);
1830 if (uids == null) {
1831 uids = new SparseIntArray();
1832 active.put(name, uids);
1833 }
1834 if (uids.indexOfKey(uid) >= 0) {
1835 // Already set, nothing to do!
1836 return false;
1837 }
1838 uids.put(uid, poolIdx);
1839 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1840 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1841 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1842 if (active == null) {
1843 // not currently active, nothing to do.
1844 return false;
1845 }
1846 SparseIntArray uids = active.get(name);
1847 if (uids == null) {
1848 // not currently active, nothing to do.
1849 return false;
1850 }
1851 idx = uids.indexOfKey(uid);
1852 if (idx < 0) {
1853 // not currently active, nothing to do.
1854 return false;
1855 }
1856 uids.removeAt(idx);
1857 if (uids.size() <= 0) {
1858 active.remove(name);
1859 }
1860 }
1861 return true;
1862 }
1863
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001864 public void removeEvents(int code) {
1865 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1866 mActiveEvents[idx] = null;
1867 }
1868
Dianne Hackborn37de0982014-05-09 09:32:18 -07001869 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1870 return mActiveEvents[code];
1871 }
1872 }
1873
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001874 public static final class BitDescription {
1875 public final int mask;
1876 public final int shift;
1877 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001878 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001879 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001880 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07001881
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001882 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001883 this.mask = mask;
1884 this.shift = -1;
1885 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001886 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001887 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001888 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001889 }
Bookatzc8c44962017-05-11 12:12:54 -07001890
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001891 public BitDescription(int mask, int shift, String name, String shortName,
1892 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001893 this.mask = mask;
1894 this.shift = shift;
1895 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001896 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001897 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001898 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001899 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001900 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001901
Dianne Hackbornfc064132014-06-02 12:42:12 -07001902 /**
1903 * Don't allow any more batching in to the current history event. This
1904 * is called when printing partial histories, so to ensure that the next
1905 * history event will go in to a new batch after what was printed in the
1906 * last partial history.
1907 */
1908 public abstract void commitCurrentHistoryBatchLocked();
1909
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001910 public abstract int getHistoryTotalSize();
1911
1912 public abstract int getHistoryUsedSize();
1913
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001914 public abstract boolean startIteratingHistoryLocked();
1915
Dianne Hackborn099bc622014-01-22 13:39:16 -08001916 public abstract int getHistoryStringPoolSize();
1917
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001918 public abstract int getHistoryStringPoolBytes();
1919
1920 public abstract String getHistoryTagPoolString(int index);
1921
1922 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001923
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001924 public abstract boolean getNextHistoryLocked(HistoryItem out);
1925
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001926 public abstract void finishIteratingHistoryLocked();
1927
1928 public abstract boolean startIteratingOldHistoryLocked();
1929
1930 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1931
1932 public abstract void finishIteratingOldHistoryLocked();
1933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001935 * Return the base time offset for the battery history.
1936 */
1937 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07001938
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001939 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 * Returns the number of times the device has been started.
1941 */
1942 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07001943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001945 * 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 -08001946 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07001947 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 * {@hide}
1949 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001950 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07001951
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001952 /**
1953 * Returns the number of times the screen was turned on.
1954 *
1955 * {@hide}
1956 */
1957 public abstract int getScreenOnCount(int which);
1958
Mike Mac2f518a2017-09-19 16:06:03 -07001959 /**
1960 * Returns the time in microseconds that the screen has been dozing while the device was
1961 * running on battery.
1962 *
1963 * {@hide}
1964 */
1965 public abstract long getScreenDozeTime(long elapsedRealtimeUs, int which);
1966
1967 /**
1968 * Returns the number of times the screen was turned dozing.
1969 *
1970 * {@hide}
1971 */
1972 public abstract int getScreenDozeCount(int which);
1973
Jeff Browne95c3cd2014-05-02 16:59:26 -07001974 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1975
Dianne Hackborn617f8772009-03-31 15:04:46 -07001976 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1977 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1978 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1979 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1980 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07001981
Dianne Hackborn617f8772009-03-31 15:04:46 -07001982 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1983 "dark", "dim", "medium", "light", "bright"
1984 };
Bookatzc8c44962017-05-11 12:12:54 -07001985
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001986 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1987 "0", "1", "2", "3", "4"
1988 };
1989
Dianne Hackborn617f8772009-03-31 15:04:46 -07001990 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001991
Dianne Hackborn617f8772009-03-31 15:04:46 -07001992 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001993 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001994 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07001995 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07001996 * {@hide}
1997 */
1998 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001999 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002002 * Returns the {@link Timer} object that tracks the given screen brightness.
2003 *
2004 * {@hide}
2005 */
2006 public abstract Timer getScreenBrightnessTimer(int brightnessBin);
2007
2008 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002009 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002010 * running on battery.
2011 *
2012 * {@hide}
2013 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002014 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002015
2016 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002017 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002018 *
2019 * {@hide}
2020 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002021 public abstract int getPowerSaveModeEnabledCount(int which);
2022
2023 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002024 * Constant for device idle mode: not active.
2025 */
2026 public static final int DEVICE_IDLE_MODE_OFF = 0;
2027
2028 /**
2029 * Constant for device idle mode: active in lightweight mode.
2030 */
2031 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
2032
2033 /**
2034 * Constant for device idle mode: active in full mode.
2035 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002036 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002037
2038 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002039 * Returns the time in microseconds that device has been in idle mode while
2040 * running on battery.
2041 *
2042 * {@hide}
2043 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002044 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002045
2046 /**
2047 * Returns the number of times that the devie has gone in to idle mode.
2048 *
2049 * {@hide}
2050 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002051 public abstract int getDeviceIdleModeCount(int mode, int which);
2052
2053 /**
2054 * Return the longest duration we spent in a particular device idle mode (fully in the
2055 * mode, not in idle maintenance etc).
2056 */
2057 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002058
2059 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002060 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002061 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002062 * counts all of the time that we consider the device to be idle, whether or not
2063 * it is currently in the actual device idle mode.
2064 *
2065 * {@hide}
2066 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002067 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002068
2069 /**
Bookatz8c6571b2017-10-24 15:04:41 -07002070 * Returns the number of times that the device has started idling.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002071 *
2072 * {@hide}
2073 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002074 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002075
2076 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002077 * Returns the number of times that connectivity state changed.
2078 *
2079 * {@hide}
2080 */
2081 public abstract int getNumConnectivityChange(int which);
2082
2083 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002084 * 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 -08002085 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002086 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 * {@hide}
2088 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002089 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002090
Dianne Hackborn627bba72009-03-24 22:32:56 -07002091 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002092 * Returns the number of times a phone call was activated.
2093 *
2094 * {@hide}
2095 */
2096 public abstract int getPhoneOnCount(int which);
2097
2098 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002099 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002100 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002101 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002102 * {@hide}
2103 */
2104 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002105 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002106
Dianne Hackborn617f8772009-03-31 15:04:46 -07002107 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07002108 * Returns the time in microseconds that the phone has been trying to
2109 * acquire a signal.
2110 *
2111 * {@hide}
2112 */
2113 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002114 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002115
2116 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002117 * Returns the {@link Timer} object that tracks how much the phone has been trying to
2118 * acquire a signal.
2119 *
2120 * {@hide}
2121 */
2122 public abstract Timer getPhoneSignalScanningTimer();
2123
2124 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002125 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002126 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002127 * {@hide}
2128 */
2129 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
2130
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002131 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002132 * Return the {@link Timer} object used to track the given signal strength's duration and
2133 * counts.
2134 */
2135 protected abstract Timer getPhoneSignalStrengthTimer(int strengthBin);
2136
2137 /**
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002138 * Returns the time in microseconds that the mobile network has been active
2139 * (in a high power state).
2140 *
2141 * {@hide}
2142 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002143 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002144
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002145 /**
2146 * Returns the number of times that the mobile network has transitioned to the
2147 * active state.
2148 *
2149 * {@hide}
2150 */
2151 public abstract int getMobileRadioActiveCount(int which);
2152
2153 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002154 * Returns the time in microseconds that is the difference between the mobile radio
2155 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
2156 * from the radio.
2157 *
2158 * {@hide}
2159 */
2160 public abstract long getMobileRadioActiveAdjustedTime(int which);
2161
2162 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002163 * Returns the time in microseconds that the mobile network has been active
2164 * (in a high power state) but not being able to blame on an app.
2165 *
2166 * {@hide}
2167 */
2168 public abstract long getMobileRadioActiveUnknownTime(int which);
2169
2170 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002171 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002172 *
2173 * {@hide}
2174 */
2175 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002176
Dianne Hackborn627bba72009-03-24 22:32:56 -07002177 public static final int DATA_CONNECTION_NONE = 0;
2178 public static final int DATA_CONNECTION_GPRS = 1;
2179 public static final int DATA_CONNECTION_EDGE = 2;
2180 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002181 public static final int DATA_CONNECTION_CDMA = 4;
2182 public static final int DATA_CONNECTION_EVDO_0 = 5;
2183 public static final int DATA_CONNECTION_EVDO_A = 6;
2184 public static final int DATA_CONNECTION_1xRTT = 7;
2185 public static final int DATA_CONNECTION_HSDPA = 8;
2186 public static final int DATA_CONNECTION_HSUPA = 9;
2187 public static final int DATA_CONNECTION_HSPA = 10;
2188 public static final int DATA_CONNECTION_IDEN = 11;
2189 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002190 public static final int DATA_CONNECTION_LTE = 13;
2191 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08002192 public static final int DATA_CONNECTION_HSPAP = 15;
2193 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002194
Dianne Hackborn627bba72009-03-24 22:32:56 -07002195 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002196 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002197 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08002198 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002199 };
Bookatzc8c44962017-05-11 12:12:54 -07002200
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002201 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002202
Dianne Hackborn627bba72009-03-24 22:32:56 -07002203 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002204 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002205 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002206 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002207 * {@hide}
2208 */
2209 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002210 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002213 * Returns the number of times the phone has entered the given data
2214 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002215 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002216 * {@hide}
2217 */
2218 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002219
Kweku Adams87b19ec2017-10-09 12:40:03 -07002220 /**
2221 * Returns the {@link Timer} object that tracks the phone's data connection type stats.
2222 */
2223 public abstract Timer getPhoneDataConnectionTimer(int dataType);
2224
Dianne Hackborn3251b902014-06-20 14:40:53 -07002225 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2226 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2227 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2228 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2229 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2230 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2231 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2232 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2233 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2234 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2235 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2236 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2237 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2238
2239 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2240
2241 static final String[] WIFI_SUPPL_STATE_NAMES = {
2242 "invalid", "disconn", "disabled", "inactive", "scanning",
2243 "authenticating", "associating", "associated", "4-way-handshake",
2244 "group-handshake", "completed", "dormant", "uninit"
2245 };
2246
2247 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2248 "inv", "dsc", "dis", "inact", "scan",
2249 "auth", "ascing", "asced", "4-way",
2250 "group", "compl", "dorm", "uninit"
2251 };
2252
Mike Mac2f518a2017-09-19 16:06:03 -07002253 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002254 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002255 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2256 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002257 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002258 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2259 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2260 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002261 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002262 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002263 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002264 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002265 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2266 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Mike Mac2f518a2017-09-19 16:06:03 -07002267 new BitDescription(HistoryItem.STATE_SCREEN_DOZE_FLAG, "screen_doze", "Sd"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002268 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2269 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2270 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2271 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2272 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2273 new String[] {"in", "out", "emergency", "off"},
2274 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002275 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2276 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2277 SignalStrength.SIGNAL_STRENGTH_NAMES,
2278 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002279 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2280 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2281 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002282 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002283
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002284 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2285 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002286 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002287 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002288 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002289 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002290 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002291 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2292 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2293 new String[] { "off", "light", "full", "???" },
2294 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002295 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2296 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2297 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002298 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2299 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2300 new String[] { "0", "1", "2", "3", "4" },
2301 new String[] { "0", "1", "2", "3", "4" }),
2302 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2303 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2304 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002305 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002306 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002307 };
2308
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002309 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002310 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002311 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2312 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002313 };
2314
2315 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002316 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002317 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002318 "Esw", "Ewa", "Elw", "Eec"
2319 };
2320
2321 @FunctionalInterface
2322 public interface IntToString {
2323 String applyAsString(int val);
2324 }
2325
2326 private static final IntToString sUidToString = UserHandle::formatUid;
2327 private static final IntToString sIntToString = Integer::toString;
2328
2329 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2330 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2331 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2332 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2333 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002334 };
2335
Dianne Hackborn617f8772009-03-31 15:04:46 -07002336 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002337 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002338 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002339 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002340 * {@hide}
2341 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002342 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002343
2344 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002345 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002346 * been in the running state while the device was running on battery.
2347 *
2348 * {@hide}
2349 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002350 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002351
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002352 public static final int WIFI_STATE_OFF = 0;
2353 public static final int WIFI_STATE_OFF_SCANNING = 1;
2354 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2355 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2356 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2357 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2358 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2359 public static final int WIFI_STATE_SOFT_AP = 7;
2360
2361 static final String[] WIFI_STATE_NAMES = {
2362 "off", "scanning", "no_net", "disconn",
2363 "sta", "p2p", "sta_p2p", "soft_ap"
2364 };
2365
2366 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2367
2368 /**
2369 * Returns the time in microseconds that WiFi has been running in the given state.
2370 *
2371 * {@hide}
2372 */
2373 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002374 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002375
2376 /**
2377 * Returns the number of times that WiFi has entered the given state.
2378 *
2379 * {@hide}
2380 */
2381 public abstract int getWifiStateCount(int wifiState, int which);
2382
The Android Open Source Project10592532009-03-18 17:39:46 -07002383 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002384 * Returns the {@link Timer} object that tracks the given WiFi state.
2385 *
2386 * {@hide}
2387 */
2388 public abstract Timer getWifiStateTimer(int wifiState);
2389
2390 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002391 * Returns the time in microseconds that the wifi supplicant has been
2392 * in a given state.
2393 *
2394 * {@hide}
2395 */
2396 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2397
2398 /**
2399 * Returns the number of times that the wifi supplicant has transitioned
2400 * to a given state.
2401 *
2402 * {@hide}
2403 */
2404 public abstract int getWifiSupplStateCount(int state, int which);
2405
Kweku Adams87b19ec2017-10-09 12:40:03 -07002406 /**
2407 * Returns the {@link Timer} object that tracks the given wifi supplicant state.
2408 *
2409 * {@hide}
2410 */
2411 public abstract Timer getWifiSupplStateTimer(int state);
2412
Dianne Hackborn3251b902014-06-20 14:40:53 -07002413 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2414
2415 /**
2416 * Returns the time in microseconds that WIFI has been running with
2417 * the given signal strength.
2418 *
2419 * {@hide}
2420 */
2421 public abstract long getWifiSignalStrengthTime(int strengthBin,
2422 long elapsedRealtimeUs, int which);
2423
2424 /**
2425 * Returns the number of times WIFI has entered the given signal strength.
2426 *
2427 * {@hide}
2428 */
2429 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2430
2431 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002432 * Returns the {@link Timer} object that tracks the given WIFI signal strength.
2433 *
2434 * {@hide}
2435 */
2436 public abstract Timer getWifiSignalStrengthTimer(int strengthBin);
2437
2438 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002439 * Returns the time in microseconds that the flashlight has been on while the device was
2440 * running on battery.
2441 *
2442 * {@hide}
2443 */
2444 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2445
2446 /**
2447 * Returns the number of times that the flashlight has been turned on while the device was
2448 * running on battery.
2449 *
2450 * {@hide}
2451 */
2452 public abstract long getFlashlightOnCount(int which);
2453
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002454 /**
2455 * Returns the time in microseconds that the camera has been on while the device was
2456 * running on battery.
2457 *
2458 * {@hide}
2459 */
2460 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2461
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002462 /**
2463 * Returns the time in microseconds that bluetooth scans were running while the device was
2464 * on battery.
2465 *
2466 * {@hide}
2467 */
2468 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002469
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002470 public static final int NETWORK_MOBILE_RX_DATA = 0;
2471 public static final int NETWORK_MOBILE_TX_DATA = 1;
2472 public static final int NETWORK_WIFI_RX_DATA = 2;
2473 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002474 public static final int NETWORK_BT_RX_DATA = 4;
2475 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002476 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2477 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2478 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2479 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2480 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002481
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002482 public abstract long getNetworkActivityBytes(int type, int which);
2483 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002484
Adam Lesinskie08af192015-03-25 16:42:59 -07002485 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002486 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002487 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002488 * actual power data.
2489 */
2490 public abstract boolean hasWifiActivityReporting();
2491
2492 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002493 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2494 * in various radio controller states, such as transmit, receive, and idle.
2495 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002496 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002497 public abstract ControllerActivityCounter getWifiControllerActivity();
2498
2499 /**
2500 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2501 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2502 * actual power data.
2503 */
2504 public abstract boolean hasBluetoothActivityReporting();
2505
2506 /**
2507 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2508 * in various radio controller states, such as transmit, receive, and idle.
2509 * @return non-null {@link ControllerActivityCounter}
2510 */
2511 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2512
2513 /**
2514 * Returns true if the BatteryStats object has detailed modem power reports.
2515 * When true, calling {@link #getModemControllerActivity()} will yield the
2516 * actual power data.
2517 */
2518 public abstract boolean hasModemActivityReporting();
2519
2520 /**
2521 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2522 * in various radio controller states, such as transmit, receive, and idle.
2523 * @return non-null {@link ControllerActivityCounter}
2524 */
2525 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002526
The Android Open Source Project10592532009-03-18 17:39:46 -07002527 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002528 * Return the wall clock time when battery stats data collection started.
2529 */
2530 public abstract long getStartClockTime();
2531
2532 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002533 * Return platform version tag that we were running in when the battery stats started.
2534 */
2535 public abstract String getStartPlatformVersion();
2536
2537 /**
2538 * Return platform version tag that we were running in when the battery stats ended.
2539 */
2540 public abstract String getEndPlatformVersion();
2541
2542 /**
2543 * Return the internal version code of the parcelled format.
2544 */
2545 public abstract int getParcelVersion();
2546
2547 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 * Return whether we are currently running on battery.
2549 */
2550 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 /**
2553 * Returns a SparseArray containing the statistics for each uid.
2554 */
2555 public abstract SparseArray<? extends Uid> getUidStats();
2556
2557 /**
2558 * Returns the current battery uptime in microseconds.
2559 *
2560 * @param curTime the amount of elapsed realtime in microseconds.
2561 */
2562 public abstract long getBatteryUptime(long curTime);
2563
2564 /**
2565 * Returns the current battery realtime in microseconds.
2566 *
2567 * @param curTime the amount of elapsed realtime in microseconds.
2568 */
2569 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002570
The Android Open Source Project10592532009-03-18 17:39:46 -07002571 /**
Evan Millar633a1742009-04-02 16:36:33 -07002572 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002573 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002574 */
Evan Millar633a1742009-04-02 16:36:33 -07002575 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002576
The Android Open Source Project10592532009-03-18 17:39:46 -07002577 /**
Evan Millar633a1742009-04-02 16:36:33 -07002578 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2579 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002580 */
Evan Millar633a1742009-04-02 16:36:33 -07002581 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582
2583 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002584 * Get the amount the battery has discharged since the stats were
2585 * last reset after charging, as a lower-end approximation.
2586 */
2587 public abstract int getLowDischargeAmountSinceCharge();
2588
2589 /**
2590 * Get the amount the battery has discharged since the stats were
2591 * last reset after charging, as an upper-end approximation.
2592 */
2593 public abstract int getHighDischargeAmountSinceCharge();
2594
2595 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002596 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2597 */
2598 public abstract int getDischargeAmount(int which);
2599
2600 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002601 * Get the amount the battery has discharged while the screen was on,
2602 * since the last time power was unplugged.
2603 */
2604 public abstract int getDischargeAmountScreenOn();
2605
2606 /**
2607 * Get the amount the battery has discharged while the screen was on,
2608 * since the last time the device was charged.
2609 */
2610 public abstract int getDischargeAmountScreenOnSinceCharge();
2611
2612 /**
2613 * Get the amount the battery has discharged while the screen was off,
2614 * since the last time power was unplugged.
2615 */
2616 public abstract int getDischargeAmountScreenOff();
2617
2618 /**
2619 * Get the amount the battery has discharged while the screen was off,
2620 * since the last time the device was charged.
2621 */
2622 public abstract int getDischargeAmountScreenOffSinceCharge();
2623
2624 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002625 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002626 * since the last time power was unplugged.
2627 */
2628 public abstract int getDischargeAmountScreenDoze();
2629
2630 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002631 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002632 * since the last time the device was charged.
2633 */
2634 public abstract int getDischargeAmountScreenDozeSinceCharge();
2635
2636 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637 * Returns the total, last, or current battery uptime in microseconds.
2638 *
2639 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002640 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641 */
2642 public abstract long computeBatteryUptime(long curTime, int which);
2643
2644 /**
2645 * Returns the total, last, or current battery realtime in microseconds.
2646 *
2647 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002648 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 */
2650 public abstract long computeBatteryRealtime(long curTime, int which);
2651
2652 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002653 * Returns the total, last, or current battery screen off/doze uptime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002654 *
2655 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002656 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002657 */
2658 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2659
2660 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002661 * Returns the total, last, or current battery screen off/doze realtime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002662 *
2663 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002664 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002665 */
2666 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2667
2668 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 * Returns the total, last, or current uptime in microseconds.
2670 *
2671 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002672 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 */
2674 public abstract long computeUptime(long curTime, int which);
2675
2676 /**
2677 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002678 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002680 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 */
2682 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002683
2684 /**
2685 * Compute an approximation for how much run time (in microseconds) is remaining on
2686 * the battery. Returns -1 if no time can be computed: either there is not
2687 * enough current data to make a decision, or the battery is currently
2688 * charging.
2689 *
2690 * @param curTime The current elepsed realtime in microseconds.
2691 */
2692 public abstract long computeBatteryTimeRemaining(long curTime);
2693
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002694 // The part of a step duration that is the actual time.
2695 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2696
2697 // Bits in a step duration that are the new battery level we are at.
2698 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002699 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002700
2701 // Bits in a step duration that are the initial mode we were in at that step.
2702 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002703 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002704
2705 // Bits in a step duration that indicate which modes changed during that step.
2706 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002707 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002708
2709 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2710 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2711
Santos Cordone94f0502017-02-24 12:31:20 -08002712 // The largest value for screen state that is tracked in battery states. Any values above
2713 // this should be mapped back to one of the tracked values before being tracked here.
2714 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2715
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002716 // Step duration mode: power save is on.
2717 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2718
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002719 // Step duration mode: device is currently in idle mode.
2720 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2721
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002722 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2723 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002724 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2725 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002726 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2727 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2728 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2729 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2730 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002731 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2732 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002733 };
2734 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2735 (Display.STATE_OFF-1),
2736 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002737 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002738 (Display.STATE_ON-1),
2739 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2740 (Display.STATE_DOZE-1),
2741 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2742 (Display.STATE_DOZE_SUSPEND-1),
2743 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002744 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002745 };
2746 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2747 "screen off",
2748 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002749 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002750 "screen on",
2751 "screen on power save",
2752 "screen doze",
2753 "screen doze power save",
2754 "screen doze-suspend",
2755 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002756 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002757 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002758
2759 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002760 * Return the amount of battery discharge while the screen was off, measured in
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002761 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2762 * a coulomb counter.
2763 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002764 public abstract long getUahDischargeScreenOff(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002765
2766 /**
2767 * Return the amount of battery discharge while the screen was in doze mode, measured in
2768 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2769 * a coulomb counter.
2770 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002771 public abstract long getUahDischargeScreenDoze(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002772
2773 /**
2774 * Return the amount of battery discharge measured in micro-Ampere-hours. This will be
2775 * non-zero only if the device's battery has a coulomb counter.
2776 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002777 public abstract long getUahDischarge(int which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002778
2779 /**
Mike Ma15313c92017-11-15 17:58:21 -08002780 * @return the amount of battery discharge while the device is in light idle mode, measured in
2781 * micro-Ampere-hours.
2782 */
2783 public abstract long getUahDischargeLightDoze(int which);
2784
2785 /**
2786 * @return the amount of battery discharge while the device is in deep idle mode, measured in
2787 * micro-Ampere-hours.
2788 */
2789 public abstract long getUahDischargeDeepDoze(int which);
2790
2791 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002792 * Returns the estimated real battery capacity, which may be less than the capacity
2793 * declared by the PowerProfile.
2794 * @return The estimated battery capacity in mAh.
2795 */
2796 public abstract int getEstimatedBatteryCapacity();
2797
2798 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002799 * @return The minimum learned battery capacity in uAh.
2800 */
2801 public abstract int getMinLearnedBatteryCapacity();
2802
2803 /**
2804 * @return The maximum learned battery capacity in uAh.
2805 */
2806 public abstract int getMaxLearnedBatteryCapacity() ;
2807
2808 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002809 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002810 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002811 public abstract LevelStepTracker getDischargeLevelStepTracker();
2812
2813 /**
2814 * Return the array of daily discharge step durations.
2815 */
2816 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002817
2818 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002819 * Compute an approximation for how much time (in microseconds) remains until the battery
2820 * is fully charged. Returns -1 if no time can be computed: either there is not
2821 * enough current data to make a decision, or the battery is currently
2822 * discharging.
2823 *
2824 * @param curTime The current elepsed realtime in microseconds.
2825 */
2826 public abstract long computeChargeTimeRemaining(long curTime);
2827
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002828 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002829 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002830 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002831 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002832
2833 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002834 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002835 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002836 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002837
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002838 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2839
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002840 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002841
Evan Millarc64edde2009-04-18 12:26:32 -07002842 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843
Bookatz50df7112017-08-04 14:53:26 -07002844 /**
2845 * Returns Timers tracking the total time of each Resource Power Manager state and voter.
2846 */
2847 public abstract Map<String, ? extends Timer> getRpmStats();
2848 /**
2849 * Returns Timers tracking the screen-off time of each Resource Power Manager state and voter.
2850 */
2851 public abstract Map<String, ? extends Timer> getScreenOffRpmStats();
2852
2853
James Carr2dd7e5e2016-07-20 18:48:39 -07002854 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2855
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002856 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2857
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002858 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 long days = seconds / (60 * 60 * 24);
2860 if (days != 0) {
2861 out.append(days);
2862 out.append("d ");
2863 }
2864 long used = days * 60 * 60 * 24;
2865
2866 long hours = (seconds - used) / (60 * 60);
2867 if (hours != 0 || used != 0) {
2868 out.append(hours);
2869 out.append("h ");
2870 }
2871 used += hours * 60 * 60;
2872
2873 long mins = (seconds-used) / 60;
2874 if (mins != 0 || used != 0) {
2875 out.append(mins);
2876 out.append("m ");
2877 }
2878 used += mins * 60;
2879
2880 if (seconds != 0 || used != 0) {
2881 out.append(seconds-used);
2882 out.append("s ");
2883 }
2884 }
2885
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002886 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002888 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 sb.append(time - (sec * 1000));
2890 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 }
2892
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002893 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002894 long sec = time / 1000;
2895 formatTimeRaw(sb, sec);
2896 sb.append(time - (sec * 1000));
2897 sb.append("ms");
2898 }
2899
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002900 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002902 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 }
2904 float perc = ((float)num) / ((float)den) * 100;
2905 mFormatBuilder.setLength(0);
2906 mFormatter.format("%.1f%%", perc);
2907 return mFormatBuilder.toString();
2908 }
2909
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002910 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002911 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07002912
Evan Millar22ac0432009-03-31 11:33:18 -07002913 if (bytes < BYTES_PER_KB) {
2914 return bytes + "B";
2915 } else if (bytes < BYTES_PER_MB) {
2916 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2917 return mFormatBuilder.toString();
2918 } else if (bytes < BYTES_PER_GB){
2919 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2920 return mFormatBuilder.toString();
2921 } else {
2922 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2923 return mFormatBuilder.toString();
2924 }
2925 }
2926
Kweku Adams103351f2017-10-16 14:39:34 -07002927 private static long roundUsToMs(long timeUs) {
2928 return (timeUs + 500) / 1000;
2929 }
2930
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002931 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002932 if (timer != null) {
2933 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002934 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002935 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2936 return totalTimeMillis;
2937 }
2938 return 0;
2939 }
2940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 /**
2942 *
2943 * @param sb a StringBuilder object.
2944 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002945 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002947 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002948 * @param linePrefix a String to be prepended to each line of output.
2949 * @return the line prefix
2950 */
2951 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002952 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002955 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002956
Evan Millarc64edde2009-04-18 12:26:32 -07002957 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 if (totalTimeMillis != 0) {
2959 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002960 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002961 if (name != null) {
2962 sb.append(name);
2963 sb.append(' ');
2964 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 sb.append('(');
2966 sb.append(count);
2967 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002968 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2969 if (maxDurationMs >= 0) {
2970 sb.append(" max=");
2971 sb.append(maxDurationMs);
2972 }
Bookatz506a8182017-05-01 14:18:42 -07002973 // Put actual time if it is available and different from totalTimeMillis.
2974 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
2975 if (totalDurMs > totalTimeMillis) {
2976 sb.append(" actual=");
2977 sb.append(totalDurMs);
2978 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07002979 if (timer.isRunningLocked()) {
2980 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2981 if (currentMs >= 0) {
2982 sb.append(" (running for ");
2983 sb.append(currentMs);
2984 sb.append("ms)");
2985 } else {
2986 sb.append(" (running)");
2987 }
2988 }
2989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990 return ", ";
2991 }
2992 }
2993 return linePrefix;
2994 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002995
2996 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07002997 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002998 *
2999 * @param pw a PrintWriter object to print to.
3000 * @param sb a StringBuilder object.
3001 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08003002 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003003 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
3004 * @param prefix a String to be prepended to each line of output.
3005 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07003006 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003007 */
3008 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07003009 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003010 if (timer != null) {
3011 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07003012 final long totalTimeMs = (timer.getTotalTimeLocked(
3013 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003014 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003015 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003016 sb.setLength(0);
3017 sb.append(prefix);
3018 sb.append(" ");
3019 sb.append(type);
3020 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003021 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003022 sb.append("realtime (");
3023 sb.append(count);
3024 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003025 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
3026 if (maxDurationMs >= 0) {
3027 sb.append(" max=");
3028 sb.append(maxDurationMs);
3029 }
3030 if (timer.isRunningLocked()) {
3031 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
3032 if (currentMs >= 0) {
3033 sb.append(" (running for ");
3034 sb.append(currentMs);
3035 sb.append("ms)");
3036 } else {
3037 sb.append(" (running)");
3038 }
3039 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003040 pw.println(sb.toString());
3041 return true;
3042 }
3043 }
3044 return false;
3045 }
Bookatzc8c44962017-05-11 12:12:54 -07003046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047 /**
3048 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07003049 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003050 * @param sb a StringBuilder object.
3051 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003052 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003053 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003054 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 * @param linePrefix a String to be prepended to each line of output.
3056 * @return the line prefix
3057 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003058 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
3059 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 long totalTimeMicros = 0;
3061 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07003062 long max = 0;
3063 long current = 0;
3064 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003066 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07003067 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003068 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3069 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07003070 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 }
3072 sb.append(linePrefix);
3073 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
3074 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07003075 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003077 sb.append(',');
3078 sb.append(current);
3079 sb.append(',');
3080 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07003081 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
3082 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
3083 // totalDuration independent of totalTimeMicros (since they are not pooled).
3084 if (name != null) {
3085 sb.append(',');
3086 sb.append(totalDuration);
3087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 return ",";
3089 }
Bookatz506a8182017-05-01 14:18:42 -07003090
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003091 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
3092 String type) {
3093 pw.print(BATTERY_STATS_CHECKIN_VERSION);
3094 pw.print(',');
3095 pw.print(uid);
3096 pw.print(',');
3097 pw.print(category);
3098 pw.print(',');
3099 pw.print(type);
3100 }
3101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 /**
3103 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07003104 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003105 * @param pw the PageWriter to dump log to
3106 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3107 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3108 * @param args type-dependent data arguments
3109 */
Bookatzc8c44962017-05-11 12:12:54 -07003110 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003112 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003113 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003114 pw.print(',');
3115 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003117 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07003119
3120 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003121 * Dump a given timer stat for terse checkin mode.
3122 *
3123 * @param pw the PageWriter to dump log to
3124 * @param uid the UID to log
3125 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3126 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3127 * @param timer a {@link Timer} to dump stats for
3128 * @param rawRealtime the current elapsed realtime of the system in microseconds
3129 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3130 */
3131 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
3132 Timer timer, long rawRealtime, int which) {
3133 if (timer != null) {
3134 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003135 final long totalTime = roundUsToMs(timer.getTotalTimeLocked(rawRealtime, which));
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003136 final int count = timer.getCountLocked(which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003137 if (totalTime != 0 || count != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003138 dumpLine(pw, uid, category, type, totalTime, count);
3139 }
3140 }
3141 }
3142
3143 /**
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003144 * Dump a given timer stat to the proto stream.
3145 *
3146 * @param proto the ProtoOutputStream to log to
3147 * @param fieldId type of data, the field to save to (e.g. AggregatedBatteryStats.WAKELOCK)
3148 * @param timer a {@link Timer} to dump stats for
3149 * @param rawRealtimeUs the current elapsed realtime of the system in microseconds
3150 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3151 */
3152 private static void dumpTimer(ProtoOutputStream proto, long fieldId,
Kweku Adams87b19ec2017-10-09 12:40:03 -07003153 Timer timer, long rawRealtimeUs, int which) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003154 if (timer == null) {
3155 return;
3156 }
3157 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003158 final long timeMs = roundUsToMs(timer.getTotalTimeLocked(rawRealtimeUs, which));
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003159 final int count = timer.getCountLocked(which);
Kweku Adams103351f2017-10-16 14:39:34 -07003160 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs / 1000);
3161 final long curDurationMs = timer.getCurrentDurationMsLocked(rawRealtimeUs / 1000);
3162 final long totalDurationMs = timer.getTotalDurationMsLocked(rawRealtimeUs / 1000);
3163 if (timeMs != 0 || count != 0 || maxDurationMs != -1 || curDurationMs != -1
3164 || totalDurationMs != -1) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003165 final long token = proto.start(fieldId);
Kweku Adams103351f2017-10-16 14:39:34 -07003166 proto.write(TimerProto.DURATION_MS, timeMs);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003167 proto.write(TimerProto.COUNT, count);
Kweku Adams103351f2017-10-16 14:39:34 -07003168 // These values will be -1 for timers that don't implement the functionality.
3169 if (maxDurationMs != -1) {
3170 proto.write(TimerProto.MAX_DURATION_MS, maxDurationMs);
3171 }
3172 if (curDurationMs != -1) {
3173 proto.write(TimerProto.CURRENT_DURATION_MS, curDurationMs);
3174 }
3175 if (totalDurationMs != -1) {
3176 proto.write(TimerProto.TOTAL_DURATION_MS, totalDurationMs);
3177 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003178 proto.end(token);
3179 }
3180 }
3181
3182 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003183 * Checks if the ControllerActivityCounter has any data worth dumping.
3184 */
3185 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
3186 if (counter == null) {
3187 return false;
3188 }
3189
3190 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
3191 || counter.getRxTimeCounter().getCountLocked(which) != 0
3192 || counter.getPowerCounter().getCountLocked(which) != 0) {
3193 return true;
3194 }
3195
3196 for (LongCounter c : counter.getTxTimeCounters()) {
3197 if (c.getCountLocked(which) != 0) {
3198 return true;
3199 }
3200 }
3201 return false;
3202 }
3203
3204 /**
3205 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3206 * The order of the arguments in the final check in line is:
3207 *
3208 * idle, rx, power, tx...
3209 *
3210 * where tx... is one or more transmit level times.
3211 */
3212 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
3213 String type,
3214 ControllerActivityCounter counter,
3215 int which) {
3216 if (!controllerActivityHasData(counter, which)) {
3217 return;
3218 }
3219
3220 dumpLineHeader(pw, uid, category, type);
3221 pw.print(",");
3222 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
3223 pw.print(",");
3224 pw.print(counter.getRxTimeCounter().getCountLocked(which));
3225 pw.print(",");
3226 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3227 for (LongCounter c : counter.getTxTimeCounters()) {
3228 pw.print(",");
3229 pw.print(c.getCountLocked(which));
3230 }
3231 pw.println();
3232 }
3233
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003234 /**
3235 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3236 */
3237 private static void dumpControllerActivityProto(ProtoOutputStream proto, long fieldId,
3238 ControllerActivityCounter counter,
3239 int which) {
3240 if (!controllerActivityHasData(counter, which)) {
3241 return;
3242 }
3243
3244 final long cToken = proto.start(fieldId);
3245
3246 proto.write(ControllerActivityProto.IDLE_DURATION_MS,
3247 counter.getIdleTimeCounter().getCountLocked(which));
3248 proto.write(ControllerActivityProto.RX_DURATION_MS,
3249 counter.getRxTimeCounter().getCountLocked(which));
3250 proto.write(ControllerActivityProto.POWER_MAH,
3251 counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3252
3253 long tToken;
3254 LongCounter[] txCounters = counter.getTxTimeCounters();
3255 for (int i = 0; i < txCounters.length; ++i) {
3256 LongCounter c = txCounters[i];
3257 tToken = proto.start(ControllerActivityProto.TX);
3258 proto.write(ControllerActivityProto.TxLevel.LEVEL, i);
3259 proto.write(ControllerActivityProto.TxLevel.DURATION_MS, c.getCountLocked(which));
3260 proto.end(tToken);
3261 }
3262
3263 proto.end(cToken);
3264 }
3265
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003266 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
3267 String prefix, String controllerName,
3268 ControllerActivityCounter counter,
3269 int which) {
3270 if (controllerActivityHasData(counter, which)) {
3271 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
3272 }
3273 }
3274
3275 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
3276 String controllerName,
3277 ControllerActivityCounter counter, int which) {
3278 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
3279 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
3280 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003281 // Battery real time
3282 final long totalControllerActivityTimeMs
3283 = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003284 long totalTxTimeMs = 0;
3285 for (LongCounter txState : counter.getTxTimeCounters()) {
3286 totalTxTimeMs += txState.getCountLocked(which);
3287 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003288 final long sleepTimeMs
3289 = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + totalTxTimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003290
3291 sb.setLength(0);
3292 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003293 sb.append(" ");
3294 sb.append(controllerName);
3295 sb.append(" Sleep time: ");
3296 formatTimeMs(sb, sleepTimeMs);
3297 sb.append("(");
3298 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3299 sb.append(")");
3300 pw.println(sb.toString());
3301
3302 sb.setLength(0);
3303 sb.append(prefix);
3304 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003305 sb.append(controllerName);
3306 sb.append(" Idle time: ");
3307 formatTimeMs(sb, idleTimeMs);
3308 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003309 sb.append(formatRatioLocked(idleTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003310 sb.append(")");
3311 pw.println(sb.toString());
3312
3313 sb.setLength(0);
3314 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003315 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003316 sb.append(controllerName);
3317 sb.append(" Rx time: ");
3318 formatTimeMs(sb, rxTimeMs);
3319 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003320 sb.append(formatRatioLocked(rxTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003321 sb.append(")");
3322 pw.println(sb.toString());
3323
3324 sb.setLength(0);
3325 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003326 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003327 sb.append(controllerName);
3328 sb.append(" Tx time: ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003329
Siddharth Ray3c648c42017-10-02 17:30:58 -07003330 String [] powerLevel;
3331 switch(controllerName) {
3332 case "Cellular":
3333 powerLevel = new String[] {
3334 " less than 0dBm: ",
3335 " 0dBm to 8dBm: ",
3336 " 8dBm to 15dBm: ",
3337 " 15dBm to 20dBm: ",
3338 " above 20dBm: "};
3339 break;
3340 default:
3341 powerLevel = new String[] {"[0]", "[1]", "[2]", "[3]", "[4]"};
3342 break;
3343 }
3344 final int numTxLvls = Math.min(counter.getTxTimeCounters().length, powerLevel.length);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003345 if (numTxLvls > 1) {
Siddharth Ray3c648c42017-10-02 17:30:58 -07003346 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003347 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3348 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3349 sb.setLength(0);
3350 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003351 sb.append(" ");
3352 sb.append(powerLevel[lvl]);
3353 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003354 formatTimeMs(sb, txLvlTimeMs);
3355 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003356 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003357 sb.append(")");
3358 pw.println(sb.toString());
3359 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003360 } else {
3361 final long txLvlTimeMs = counter.getTxTimeCounters()[0].getCountLocked(which);
3362 formatTimeMs(sb, txLvlTimeMs);
3363 sb.append("(");
3364 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
3365 sb.append(")");
3366 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003367 }
3368
Siddharth Ray3c648c42017-10-02 17:30:58 -07003369 if (powerDrainMaMs > 0) {
3370 sb.setLength(0);
3371 sb.append(prefix);
3372 sb.append(" ");
3373 sb.append(controllerName);
3374 sb.append(" Battery drain: ").append(
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003375 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
Siddharth Ray3c648c42017-10-02 17:30:58 -07003376 sb.append("mAh");
3377 pw.println(sb.toString());
3378 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003379 }
3380
3381 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003382 * Temporary for settings.
3383 */
3384 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3385 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3386 }
3387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 /**
3389 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003390 *
Kweku Adams87b19ec2017-10-09 12:40:03 -07003391 * NOTE: all times are expressed in microseconds, unless specified otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003393 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3394 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003395 final long rawUptime = SystemClock.uptimeMillis() * 1000;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003396 final long rawRealtimeMs = SystemClock.elapsedRealtime();
3397 final long rawRealtime = rawRealtimeMs * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003399 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3400 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003401 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3402 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3403 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 final long totalRealtime = computeRealtime(rawRealtime, which);
3405 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003406 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Mike Mac2f518a2017-09-19 16:06:03 -07003407 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003408 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003409 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003410 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3411 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003412 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003413 rawRealtime, which);
3414 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3415 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003416 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003417 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003418 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003419 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003420 final long dischargeCount = getUahDischarge(which);
3421 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
3422 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Ma15313c92017-11-15 17:58:21 -08003423 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
3424 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003425
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003426 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003427
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003428 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003429 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003430
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003431 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003434 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003435 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003436 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003437 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003438 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003439 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003440 getEstimatedBatteryCapacity(),
Mike Mac2f518a2017-09-19 16:06:03 -07003441 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity(),
3442 screenDozeTime / 1000);
Adam Lesinski67c134f2016-06-10 15:15:08 -07003443
Bookatzc8c44962017-05-11 12:12:54 -07003444
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003445 // Calculate both wakelock and wifi multicast wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003446 long fullWakeLockTimeTotal = 0;
3447 long partialWakeLockTimeTotal = 0;
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003448 long multicastWakeLockTimeTotalMicros = 0;
3449 int multicastWakeLockCountTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003450
Evan Millar22ac0432009-03-31 11:33:18 -07003451 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003452 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003453
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003454 // First calculating the wakelock stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003455 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3456 = u.getWakelockStats();
3457 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3458 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003459
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003460 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3461 if (fullWakeTimer != null) {
3462 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3463 which);
3464 }
3465
3466 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3467 if (partialWakeTimer != null) {
3468 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3469 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003470 }
3471 }
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003472
3473 // Now calculating the wifi multicast wakelock stats
3474 final Timer mcTimer = u.getMulticastWakelockStats();
3475 if (mcTimer != null) {
3476 multicastWakeLockTimeTotalMicros += mcTimer.getTotalTimeLocked(rawRealtime, which);
3477 multicastWakeLockCountTotal += mcTimer.getCountLocked(which);
3478 }
Evan Millar22ac0432009-03-31 11:33:18 -07003479 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003480
3481 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003482 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3483 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3484 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3485 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3486 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3487 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3488 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3489 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003490 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3491 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003492 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3493 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003494 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3495 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003496
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003497 // Dump Modem controller stats
3498 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3499 getModemControllerActivity(), which);
3500
Adam Lesinskie283d332015-04-16 12:29:25 -07003501 // Dump Wifi controller stats
3502 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3503 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003504 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003505 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003506
3507 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3508 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003509
3510 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003511 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3512 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003514 // Dump misc stats
3515 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003516 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003517 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003518 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003519 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003520 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003521 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3522 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003523 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003524 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3525 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3526 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3527 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003528 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003529
Dianne Hackborn617f8772009-03-31 15:04:46 -07003530 // Dump screen brightness stats
3531 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3532 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003533 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003534 }
3535 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003536
Dianne Hackborn627bba72009-03-24 22:32:56 -07003537 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003538 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3539 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003540 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003541 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003542 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003543 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003544 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003545 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003546 args[i] = getPhoneSignalStrengthCount(i, which);
3547 }
3548 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003549
Dianne Hackborn627bba72009-03-24 22:32:56 -07003550 // Dump network type stats
3551 args = new Object[NUM_DATA_CONNECTION_TYPES];
3552 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003553 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003554 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003555 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3556 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3557 args[i] = getPhoneDataConnectionCount(i, which);
3558 }
3559 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003560
3561 // Dump wifi state stats
3562 args = new Object[NUM_WIFI_STATES];
3563 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003564 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003565 }
3566 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3567 for (int i=0; i<NUM_WIFI_STATES; i++) {
3568 args[i] = getWifiStateCount(i, which);
3569 }
3570 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3571
Dianne Hackborn3251b902014-06-20 14:40:53 -07003572 // Dump wifi suppl state stats
3573 args = new Object[NUM_WIFI_SUPPL_STATES];
3574 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3575 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3576 }
3577 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3578 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3579 args[i] = getWifiSupplStateCount(i, which);
3580 }
3581 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3582
3583 // Dump wifi signal strength stats
3584 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3585 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3586 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3587 }
3588 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3589 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3590 args[i] = getWifiSignalStrengthCount(i, which);
3591 }
3592 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3593
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003594 // Dump Multicast total stats
3595 dumpLine(pw, 0 /* uid */, category, WIFI_MULTICAST_TOTAL_DATA,
3596 multicastWakeLockTimeTotalMicros / 1000,
3597 multicastWakeLockCountTotal);
3598
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003599 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003600 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003601 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003602 }
Bookatzc8c44962017-05-11 12:12:54 -07003603
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003604 if (which == STATS_SINCE_UNPLUGGED) {
3605 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3606 getDischargeStartLevel()-getDischargeCurrentLevel(),
3607 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003608 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
Mike Mac2f518a2017-09-19 16:06:03 -07003609 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003610 getDischargeAmountScreenDoze(), dischargeScreenDozeCount / 1000,
3611 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003612 } else {
3613 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3614 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003615 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003616 getDischargeAmountScreenOffSinceCharge(),
Mike Mac2f518a2017-09-19 16:06:03 -07003617 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003618 getDischargeAmountScreenDozeSinceCharge(), dischargeScreenDozeCount / 1000,
3619 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003620 }
Bookatzc8c44962017-05-11 12:12:54 -07003621
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003622 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003623 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003624 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003625 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003626 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003627 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003628 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3629 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003630 }
Evan Millarc64edde2009-04-18 12:26:32 -07003631 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003632 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003633 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003634 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3635 // Not doing the regular wake lock formatting to remain compatible
3636 // with the old checkin format.
3637 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3638 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003639 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003640 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003641 }
3642 }
Evan Millarc64edde2009-04-18 12:26:32 -07003643 }
Bookatzc8c44962017-05-11 12:12:54 -07003644
Bookatz50df7112017-08-04 14:53:26 -07003645 final Map<String, ? extends Timer> rpmStats = getRpmStats();
3646 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
3647 if (rpmStats.size() > 0) {
3648 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
3649 sb.setLength(0);
3650 Timer totalTimer = ent.getValue();
3651 long timeMs = (totalTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3652 int count = totalTimer.getCountLocked(which);
3653 Timer screenOffTimer = screenOffRpmStats.get(ent.getKey());
3654 long screenOffTimeMs = screenOffTimer != null
3655 ? (screenOffTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3656 int screenOffCount = screenOffTimer != null
3657 ? screenOffTimer.getCountLocked(which) : 0;
Bookatz82b341172017-09-07 19:06:08 -07003658 if (SCREEN_OFF_RPM_STATS_ENABLED) {
3659 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3660 "\"" + ent.getKey() + "\"", timeMs, count, screenOffTimeMs,
3661 screenOffCount);
3662 } else {
3663 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3664 "\"" + ent.getKey() + "\"", timeMs, count);
3665 }
Bookatz50df7112017-08-04 14:53:26 -07003666 }
3667 }
3668
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003669 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003670 helper.create(this);
3671 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003672 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003673 if (sippers != null && sippers.size() > 0) {
3674 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3675 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003676 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003677 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3678 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
Kweku Adams87b19ec2017-10-09 12:40:03 -07003679 int uid = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003680 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003681 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003682 String label;
3683 switch (bs.drainType) {
3684 case IDLE:
3685 label="idle";
3686 break;
3687 case CELL:
3688 label="cell";
3689 break;
3690 case PHONE:
3691 label="phone";
3692 break;
3693 case WIFI:
3694 label="wifi";
3695 break;
3696 case BLUETOOTH:
3697 label="blue";
3698 break;
3699 case SCREEN:
3700 label="scrn";
3701 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003702 case FLASHLIGHT:
3703 label="flashlight";
3704 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003705 case APP:
3706 uid = bs.uidObj.getUid();
3707 label = "uid";
3708 break;
3709 case USER:
3710 uid = UserHandle.getUid(bs.userId, 0);
3711 label = "user";
3712 break;
3713 case UNACCOUNTED:
3714 label = "unacc";
3715 break;
3716 case OVERCOUNTED:
3717 label = "over";
3718 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003719 case CAMERA:
3720 label = "camera";
3721 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003722 case MEMORY:
3723 label = "memory";
3724 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003725 default:
3726 label = "???";
3727 }
3728 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07003729 BatteryStatsHelper.makemAh(bs.totalPowerMah),
3730 bs.shouldHide ? 1 : 0,
3731 BatteryStatsHelper.makemAh(bs.screenPowerMah),
3732 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003733 }
3734 }
3735
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003736 final long[] cpuFreqs = getCpuFreqs();
3737 if (cpuFreqs != null) {
3738 sb.setLength(0);
3739 for (int i = 0; i < cpuFreqs.length; ++i) {
3740 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3741 }
3742 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3743 }
3744
Kweku Adams87b19ec2017-10-09 12:40:03 -07003745 // Dump stats per UID.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003746 for (int iu = 0; iu < NU; iu++) {
3747 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003748 if (reqUid >= 0 && uid != reqUid) {
3749 continue;
3750 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003751 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003753 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003754 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3755 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3756 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3757 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3758 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3759 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3760 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3761 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003762 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003763 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3764 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003765 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003766 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3767 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003768 // Background data transfers
3769 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3770 which);
3771 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3772 which);
3773 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3774 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3775 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3776 which);
3777 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3778 which);
3779 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3780 which);
3781 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3782 which);
3783
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003784 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3785 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003786 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003787 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3788 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3789 || wifiBytesBgTx > 0
3790 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3791 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003792 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3793 wifiBytesRx, wifiBytesTx,
3794 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003795 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003796 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003797 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3798 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3799 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3800 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003801 }
3802
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003803 // Dump modem controller data, per UID.
3804 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3805 u.getModemControllerActivity(), which);
3806
3807 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003808 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3809 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3810 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003811 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3812 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003813 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3814 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3815 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003816 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003817 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003818 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3819 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003820 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3821 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003822 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003823 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003825
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003826 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3827 u.getWifiControllerActivity(), which);
3828
Bookatz867c0d72017-03-07 18:23:42 -08003829 final Timer bleTimer = u.getBluetoothScanTimer();
3830 if (bleTimer != null) {
3831 // Convert from microseconds to milliseconds with rounding
3832 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3833 / 1000;
3834 if (totalTime != 0) {
3835 final int count = bleTimer.getCountLocked(which);
3836 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3837 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003838 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3839 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3840 final long actualTimeBg = bleTimerBg != null ?
3841 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003842 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003843 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3844 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003845 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3846 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3847 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3848 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3849 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
3850 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3851 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
3852 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3853 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
3854 final Timer unoptimizedScanTimerBg =
3855 u.getBluetoothUnoptimizedScanBackgroundTimer();
3856 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
3857 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3858 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
3859 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3860
Bookatz867c0d72017-03-07 18:23:42 -08003861 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07003862 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
3863 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
3864 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08003865 }
3866 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003867
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003868 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3869 u.getBluetoothControllerActivity(), which);
3870
Dianne Hackborn617f8772009-03-31 15:04:46 -07003871 if (u.hasUserActivity()) {
3872 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3873 boolean hasData = false;
3874 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3875 int val = u.getUserActivityCount(i, which);
3876 args[i] = val;
3877 if (val != 0) hasData = true;
3878 }
3879 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003880 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003881 }
3882 }
Bookatzc8c44962017-05-11 12:12:54 -07003883
3884 if (u.getAggregatedPartialWakelockTimer() != null) {
3885 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07003886 // Times are since reset (regardless of 'which')
3887 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07003888 final Timer bgTimer = timer.getSubTimer();
3889 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003890 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07003891 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
3892 }
3893
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003894 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3895 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3896 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3897 String linePrefix = "";
3898 sb.setLength(0);
3899 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3900 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003901 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3902 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003903 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003904 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
3905 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003906 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3907 rawRealtime, "w", which, linePrefix);
3908
Kweku Adams103351f2017-10-16 14:39:34 -07003909 // Only log if we had at least one wakelock...
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003910 if (sb.length() > 0) {
3911 String name = wakelocks.keyAt(iw);
3912 if (name.indexOf(',') >= 0) {
3913 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003914 }
Yi Jin02483362017-08-04 11:30:44 -07003915 if (name.indexOf('\n') >= 0) {
3916 name = name.replace('\n', '_');
3917 }
3918 if (name.indexOf('\r') >= 0) {
3919 name = name.replace('\r', '_');
3920 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003921 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003922 }
3923 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003924
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003925 // WiFi Multicast Wakelock Statistics
3926 final Timer mcTimer = u.getMulticastWakelockStats();
3927 if (mcTimer != null) {
3928 final long totalMcWakelockTimeMs =
3929 mcTimer.getTotalTimeLocked(rawRealtime, which) / 1000 ;
3930 final int countMcWakelock = mcTimer.getCountLocked(which);
3931 if(totalMcWakelockTimeMs > 0) {
3932 dumpLine(pw, uid, category, WIFI_MULTICAST_DATA,
3933 totalMcWakelockTimeMs, countMcWakelock);
3934 }
3935 }
3936
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003937 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3938 for (int isy=syncs.size()-1; isy>=0; isy--) {
3939 final Timer timer = syncs.valueAt(isy);
3940 // Convert from microseconds to milliseconds with rounding
3941 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3942 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07003943 final Timer bgTimer = timer.getSubTimer();
3944 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003945 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07003946 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003947 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003948 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07003949 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003950 }
3951 }
3952
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003953 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3954 for (int ij=jobs.size()-1; ij>=0; ij--) {
3955 final Timer timer = jobs.valueAt(ij);
3956 // Convert from microseconds to milliseconds with rounding
3957 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3958 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07003959 final Timer bgTimer = timer.getSubTimer();
3960 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003961 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07003962 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003963 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003964 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07003965 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003966 }
3967 }
3968
Dianne Hackborn94326cb2017-06-28 16:17:20 -07003969 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
3970 for (int ic=completions.size()-1; ic>=0; ic--) {
3971 SparseIntArray types = completions.valueAt(ic);
3972 if (types != null) {
3973 dumpLine(pw, uid, category, JOB_COMPLETION_DATA,
3974 "\"" + completions.keyAt(ic) + "\"",
3975 types.get(JobParameters.REASON_CANCELED, 0),
3976 types.get(JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, 0),
3977 types.get(JobParameters.REASON_PREEMPT, 0),
3978 types.get(JobParameters.REASON_TIMEOUT, 0),
3979 types.get(JobParameters.REASON_DEVICE_IDLE, 0));
3980 }
3981 }
3982
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003983 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3984 rawRealtime, which);
3985 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3986 rawRealtime, which);
3987 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3988 rawRealtime, which);
3989 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3990 rawRealtime, which);
3991
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003992 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3993 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003994 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003995 final Uid.Sensor se = sensors.valueAt(ise);
3996 final int sensorNumber = sensors.keyAt(ise);
3997 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003998 if (timer != null) {
3999 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004000 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4001 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004002 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004003 final int count = timer.getCountLocked(which);
4004 final Timer bgTimer = se.getSensorBackgroundTime();
4005 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004006 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4007 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4008 final long bgActualTime = bgTimer != null ?
4009 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4010 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
4011 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004013 }
4014 }
4015
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004016 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
4017 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004018
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07004019 dumpTimer(pw, uid, category, FOREGROUND_ACTIVITY_DATA, u.getForegroundActivityTimer(),
4020 rawRealtime, which);
4021
4022 dumpTimer(pw, uid, category, FOREGROUND_SERVICE_DATA, u.getForegroundServiceTimer(),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004023 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004024
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004025 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07004026 long totalStateTime = 0;
4027 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08004028 final long time = u.getProcessStateTime(ips, rawRealtime, which);
4029 totalStateTime += time;
4030 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004031 }
4032 if (totalStateTime > 0) {
4033 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
4034 }
4035
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004036 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4037 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004038 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004039 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004040 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004041 }
4042
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004043 // If the cpuFreqs is null, then don't bother checking for cpu freq times.
4044 if (cpuFreqs != null) {
4045 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
4046 // If total cpuFreqTimes is null, then we don't need to check for
4047 // screenOffCpuFreqTimes.
4048 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
4049 sb.setLength(0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004050 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004051 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004052 }
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004053 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
4054 if (screenOffCpuFreqTimeMs != null) {
4055 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
4056 sb.append("," + screenOffCpuFreqTimeMs[i]);
4057 }
4058 } else {
4059 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
4060 sb.append(",0");
4061 }
4062 }
4063 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
4064 cpuFreqTimeMs.length, sb.toString());
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004065 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -08004066
4067 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
4068 final long[] timesMs = u.getCpuFreqTimes(which, procState);
4069 if (timesMs != null && timesMs.length == cpuFreqs.length) {
4070 sb.setLength(0);
4071 for (int i = 0; i < timesMs.length; ++i) {
4072 sb.append((i == 0 ? "" : ",") + timesMs[i]);
4073 }
4074 final long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(
4075 which, procState);
4076 if (screenOffTimesMs != null) {
4077 for (int i = 0; i < screenOffTimesMs.length; ++i) {
4078 sb.append("," + screenOffTimesMs[i]);
4079 }
4080 } else {
4081 for (int i = 0; i < timesMs.length; ++i) {
4082 sb.append(",0");
4083 }
4084 }
4085 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA,
4086 Uid.UID_PROCESS_TYPES[procState], timesMs.length, sb.toString());
4087 }
4088 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004089 }
4090
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004091 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4092 = u.getProcessStats();
4093 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4094 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004095
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004096 final long userMillis = ps.getUserTime(which);
4097 final long systemMillis = ps.getSystemTime(which);
4098 final long foregroundMillis = ps.getForegroundTime(which);
4099 final int starts = ps.getStarts(which);
4100 final int numCrashes = ps.getNumCrashes(which);
4101 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004102
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004103 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
4104 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004105 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
4106 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004107 }
4108 }
4109
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004110 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4111 = u.getPackageStats();
4112 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4113 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4114 int wakeups = 0;
4115 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4116 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07004117 int count = alarms.valueAt(iwa).getCountLocked(which);
4118 wakeups += count;
4119 String name = alarms.keyAt(iwa).replace(',', '_');
4120 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004121 }
4122 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4123 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4124 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4125 final long startTime = ss.getStartTime(batteryUptime, which);
4126 final int starts = ss.getStarts(which);
4127 final int launches = ss.getLaunches(which);
4128 if (startTime != 0 || starts != 0 || launches != 0) {
4129 dumpLine(pw, uid, category, APK_DATA,
4130 wakeups, // wakeup alarms
4131 packageStats.keyAt(ipkg), // Apk
4132 serviceStats.keyAt(isvc), // service
4133 startTime / 1000, // time spent started, in ms
4134 starts,
4135 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004136 }
4137 }
4138 }
4139 }
4140 }
4141
Dianne Hackborn81038902012-11-26 17:04:09 -08004142 static final class TimerEntry {
4143 final String mName;
4144 final int mId;
4145 final BatteryStats.Timer mTimer;
4146 final long mTime;
4147 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
4148 mName = name;
4149 mId = id;
4150 mTimer = timer;
4151 mTime = time;
4152 }
4153 }
4154
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004155 private void printmAh(PrintWriter printer, double power) {
4156 printer.print(BatteryStatsHelper.makemAh(power));
4157 }
4158
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004159 private void printmAh(StringBuilder sb, double power) {
4160 sb.append(BatteryStatsHelper.makemAh(power));
4161 }
4162
Dianne Hackbornd953c532014-08-16 18:17:38 -07004163 /**
4164 * Temporary for settings.
4165 */
4166 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
4167 int reqUid) {
4168 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
4169 }
4170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004171 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004172 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07004173 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 final long rawUptime = SystemClock.uptimeMillis() * 1000;
4175 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07004176 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004177 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178
4179 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
4180 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
4181 final long totalRealtime = computeRealtime(rawRealtime, which);
4182 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004183 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
4184 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
4185 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004186 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
4187 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -07004188 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004189
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004190 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07004191
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004192 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07004193 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004194
Adam Lesinskif9b20a92016-06-17 17:30:01 -07004195 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
4196 if (estimatedBatteryCapacity > 0) {
4197 sb.setLength(0);
4198 sb.append(prefix);
4199 sb.append(" Estimated battery capacity: ");
4200 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
4201 sb.append(" mAh");
4202 pw.println(sb.toString());
4203 }
4204
Jocelyn Dangc627d102017-04-14 13:15:14 -07004205 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
4206 if (minLearnedBatteryCapacity > 0) {
4207 sb.setLength(0);
4208 sb.append(prefix);
4209 sb.append(" Min learned battery capacity: ");
4210 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
4211 sb.append(" mAh");
4212 pw.println(sb.toString());
4213 }
4214 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
4215 if (maxLearnedBatteryCapacity > 0) {
4216 sb.setLength(0);
4217 sb.append(prefix);
4218 sb.append(" Max learned battery capacity: ");
4219 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
4220 sb.append(" mAh");
4221 pw.println(sb.toString());
4222 }
4223
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004224 sb.setLength(0);
4225 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004226 sb.append(" Time on battery: ");
4227 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
4228 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
4229 sb.append(") realtime, ");
4230 formatTimeMs(sb, whichBatteryUptime / 1000);
4231 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, whichBatteryRealtime));
4232 sb.append(") uptime");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004233 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004234
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004235 sb.setLength(0);
4236 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004237 sb.append(" Time on battery screen off: ");
4238 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
4239 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, whichBatteryRealtime));
4240 sb.append(") realtime, ");
4241 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
4242 sb.append("(");
4243 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, whichBatteryRealtime));
4244 sb.append(") uptime");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004245 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004246
4247 sb.setLength(0);
4248 sb.append(prefix);
4249 sb.append(" Time on battery screen doze: ");
4250 formatTimeMs(sb, screenDozeTime / 1000); sb.append("(");
4251 sb.append(formatRatioLocked(screenDozeTime, whichBatteryRealtime));
4252 sb.append(")");
4253 pw.println(sb.toString());
4254
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004255 sb.setLength(0);
4256 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004257 sb.append(" Total run time: ");
4258 formatTimeMs(sb, totalRealtime / 1000);
4259 sb.append("realtime, ");
4260 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004261 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07004262 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004263 if (batteryTimeRemaining >= 0) {
4264 sb.setLength(0);
4265 sb.append(prefix);
4266 sb.append(" Battery time remaining: ");
4267 formatTimeMs(sb, batteryTimeRemaining / 1000);
4268 pw.println(sb.toString());
4269 }
4270 if (chargeTimeRemaining >= 0) {
4271 sb.setLength(0);
4272 sb.append(prefix);
4273 sb.append(" Charge time remaining: ");
4274 formatTimeMs(sb, chargeTimeRemaining / 1000);
4275 pw.println(sb.toString());
4276 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004277
Kweku Adams87b19ec2017-10-09 12:40:03 -07004278 final long dischargeCount = getUahDischarge(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004279 if (dischargeCount >= 0) {
4280 sb.setLength(0);
4281 sb.append(prefix);
4282 sb.append(" Discharge: ");
4283 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
4284 sb.append(" mAh");
4285 pw.println(sb.toString());
4286 }
4287
Kweku Adams87b19ec2017-10-09 12:40:03 -07004288 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004289 if (dischargeScreenOffCount >= 0) {
4290 sb.setLength(0);
4291 sb.append(prefix);
4292 sb.append(" Screen off discharge: ");
4293 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
4294 sb.append(" mAh");
4295 pw.println(sb.toString());
4296 }
4297
Kweku Adams87b19ec2017-10-09 12:40:03 -07004298 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Mac2f518a2017-09-19 16:06:03 -07004299 if (dischargeScreenDozeCount >= 0) {
4300 sb.setLength(0);
4301 sb.append(prefix);
4302 sb.append(" Screen doze discharge: ");
4303 sb.append(BatteryStatsHelper.makemAh(dischargeScreenDozeCount / 1000.0));
4304 sb.append(" mAh");
4305 pw.println(sb.toString());
4306 }
4307
4308 final long dischargeScreenOnCount =
4309 dischargeCount - dischargeScreenOffCount - dischargeScreenDozeCount;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004310 if (dischargeScreenOnCount >= 0) {
4311 sb.setLength(0);
4312 sb.append(prefix);
4313 sb.append(" Screen on discharge: ");
4314 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
4315 sb.append(" mAh");
4316 pw.println(sb.toString());
4317 }
4318
Mike Ma15313c92017-11-15 17:58:21 -08004319 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
4320 if (dischargeLightDozeCount >= 0) {
4321 sb.setLength(0);
4322 sb.append(prefix);
4323 sb.append(" Device light doze discharge: ");
4324 sb.append(BatteryStatsHelper.makemAh(dischargeLightDozeCount / 1000.0));
4325 sb.append(" mAh");
4326 pw.println(sb.toString());
4327 }
4328
4329 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
4330 if (dischargeDeepDozeCount >= 0) {
4331 sb.setLength(0);
4332 sb.append(prefix);
4333 sb.append(" Device deep doze discharge: ");
4334 sb.append(BatteryStatsHelper.makemAh(dischargeDeepDozeCount / 1000.0));
4335 sb.append(" mAh");
4336 pw.println(sb.toString());
4337 }
4338
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004339 pw.print(" Start clock time: ");
4340 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
4341
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004342 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004343 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004344 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004345 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
4346 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004347 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004348 rawRealtime, which);
4349 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
4350 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004351 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004352 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004353 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
4354 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
4355 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004356 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004357 sb.append(prefix);
4358 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
4359 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004360 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004361 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
4362 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004363 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004364 pw.println(sb.toString());
4365 sb.setLength(0);
4366 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004367 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004368 boolean didOne = false;
4369 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004370 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004371 if (time == 0) {
4372 continue;
4373 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004374 sb.append("\n ");
4375 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004376 didOne = true;
4377 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
4378 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004379 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004380 sb.append("(");
4381 sb.append(formatRatioLocked(time, screenOnTime));
4382 sb.append(")");
4383 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004384 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004385 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004386 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004387 sb.setLength(0);
4388 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004389 sb.append(" Power save mode enabled: ");
4390 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004391 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004392 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004393 sb.append(")");
4394 pw.println(sb.toString());
4395 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004396 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004397 sb.setLength(0);
4398 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004399 sb.append(" Device light idling: ");
4400 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004401 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004402 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
4403 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004404 sb.append("x");
4405 pw.println(sb.toString());
4406 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004407 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004408 sb.setLength(0);
4409 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004410 sb.append(" Idle mode light time: ");
4411 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004412 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004413 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
4414 sb.append(") ");
4415 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004416 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004417 sb.append(" -- longest ");
4418 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
4419 pw.println(sb.toString());
4420 }
4421 if (deviceIdlingTime != 0) {
4422 sb.setLength(0);
4423 sb.append(prefix);
4424 sb.append(" Device full idling: ");
4425 formatTimeMs(sb, deviceIdlingTime / 1000);
4426 sb.append("(");
4427 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004428 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004429 sb.append("x");
4430 pw.println(sb.toString());
4431 }
4432 if (deviceIdleModeFullTime != 0) {
4433 sb.setLength(0);
4434 sb.append(prefix);
4435 sb.append(" Idle mode full time: ");
4436 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
4437 sb.append("(");
4438 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
4439 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004440 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004441 sb.append("x");
4442 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004443 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004444 pw.println(sb.toString());
4445 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004446 if (phoneOnTime != 0) {
4447 sb.setLength(0);
4448 sb.append(prefix);
4449 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
4450 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004451 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004452 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004453 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004454 if (connChanges != 0) {
4455 pw.print(prefix);
4456 pw.print(" Connectivity changes: "); pw.println(connChanges);
4457 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004458
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004459 // Calculate both wakelock and wifi multicast wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07004460 long fullWakeLockTimeTotalMicros = 0;
4461 long partialWakeLockTimeTotalMicros = 0;
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004462 long multicastWakeLockTimeTotalMicros = 0;
4463 int multicastWakeLockCountTotal = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08004464
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004465 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08004466
Evan Millar22ac0432009-03-31 11:33:18 -07004467 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004468 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004469
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004470 // First calculate wakelock statistics
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004471 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4472 = u.getWakelockStats();
4473 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4474 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07004475
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004476 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
4477 if (fullWakeTimer != null) {
4478 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
4479 rawRealtime, which);
4480 }
4481
4482 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4483 if (partialWakeTimer != null) {
4484 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
4485 rawRealtime, which);
4486 if (totalTimeMicros > 0) {
4487 if (reqUid < 0) {
4488 // Only show the ordered list of all wake
4489 // locks if the caller is not asking for data
4490 // about a specific uid.
4491 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
4492 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08004493 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004494 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07004495 }
4496 }
4497 }
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004498
4499 // Next calculate wifi multicast wakelock statistics
4500 final Timer mcTimer = u.getMulticastWakelockStats();
4501 if (mcTimer != null) {
4502 multicastWakeLockTimeTotalMicros += mcTimer.getTotalTimeLocked(rawRealtime, which);
4503 multicastWakeLockCountTotal += mcTimer.getCountLocked(which);
4504 }
Evan Millar22ac0432009-03-31 11:33:18 -07004505 }
Bookatzc8c44962017-05-11 12:12:54 -07004506
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004507 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4508 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4509 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4510 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4511 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4512 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4513 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4514 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004515 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4516 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004517
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004518 if (fullWakeLockTimeTotalMicros != 0) {
4519 sb.setLength(0);
4520 sb.append(prefix);
4521 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4522 (fullWakeLockTimeTotalMicros + 500) / 1000);
4523 pw.println(sb.toString());
4524 }
4525
4526 if (partialWakeLockTimeTotalMicros != 0) {
4527 sb.setLength(0);
4528 sb.append(prefix);
4529 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4530 (partialWakeLockTimeTotalMicros + 500) / 1000);
4531 pw.println(sb.toString());
4532 }
4533
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004534 if (multicastWakeLockTimeTotalMicros != 0) {
4535 sb.setLength(0);
4536 sb.append(prefix);
4537 sb.append(" Total WiFi Multicast wakelock Count: ");
4538 sb.append(multicastWakeLockCountTotal);
4539 pw.println(sb.toString());
4540
4541 sb.setLength(0);
4542 sb.append(prefix);
4543 sb.append(" Total WiFi Multicast wakelock time: ");
4544 formatTimeMsNoSpace(sb, (multicastWakeLockTimeTotalMicros + 500) / 1000);
4545 pw.println(sb.toString());
4546 }
4547
Siddharth Ray3c648c42017-10-02 17:30:58 -07004548 pw.println("");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004549 pw.print(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004550 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004551 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004552 sb.append(" CONNECTIVITY POWER SUMMARY START");
4553 pw.println(sb.toString());
4554
4555 pw.print(prefix);
4556 sb.setLength(0);
4557 sb.append(prefix);
4558 sb.append(" Logging duration for connectivity statistics: ");
4559 formatTimeMs(sb, whichBatteryRealtime / 1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004560 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004561
4562 sb.setLength(0);
4563 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004564 sb.append(" Cellular Statistics:");
Amith Yamasanif37447b2009-10-08 18:28:01 -07004565 pw.println(sb.toString());
4566
Siddharth Ray3c648c42017-10-02 17:30:58 -07004567 pw.print(prefix);
4568 sb.setLength(0);
4569 sb.append(prefix);
4570 sb.append(" Cellular kernel active time: ");
4571 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
4572 formatTimeMs(sb, mobileActiveTime / 1000);
4573 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4574 sb.append(")");
4575 pw.println(sb.toString());
4576
4577 pw.print(" Cellular data received: "); pw.println(formatBytesLocked(mobileRxTotalBytes));
4578 pw.print(" Cellular data sent: "); pw.println(formatBytesLocked(mobileTxTotalBytes));
4579 pw.print(" Cellular packets received: "); pw.println(mobileRxTotalPackets);
4580 pw.print(" Cellular packets sent: "); pw.println(mobileTxTotalPackets);
4581
Dianne Hackborn627bba72009-03-24 22:32:56 -07004582 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004583 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004584 sb.append(" Cellular Radio Access Technology:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004585 didOne = false;
4586 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004587 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004588 if (time == 0) {
4589 continue;
4590 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004591 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004592 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004593 didOne = true;
4594 sb.append(DATA_CONNECTION_NAMES[i]);
4595 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004596 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004597 sb.append("(");
4598 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004599 sb.append(") ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004600 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004601 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004602 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004603
4604 sb.setLength(0);
4605 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004606 sb.append(" Cellular Rx signal strength (RSRP):");
4607 final String[] cellularRxSignalStrengthDescription = new String[]{
4608 "very poor (less than -128dBm): ",
4609 "poor (-128dBm to -118dBm): ",
4610 "moderate (-118dBm to -108dBm): ",
4611 "good (-108dBm to -98dBm): ",
4612 "great (greater than -98dBm): "};
4613 didOne = false;
4614 final int numCellularRxBins = Math.min(SignalStrength.NUM_SIGNAL_STRENGTH_BINS,
4615 cellularRxSignalStrengthDescription.length);
4616 for (int i=0; i<numCellularRxBins; i++) {
4617 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
4618 if (time == 0) {
4619 continue;
4620 }
4621 sb.append("\n ");
4622 sb.append(prefix);
4623 didOne = true;
4624 sb.append(cellularRxSignalStrengthDescription[i]);
4625 sb.append(" ");
4626 formatTimeMs(sb, time/1000);
4627 sb.append("(");
4628 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4629 sb.append(") ");
4630 }
4631 if (!didOne) sb.append(" (no activity)");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004632 pw.println(sb.toString());
4633
Siddharth Ray3c648c42017-10-02 17:30:58 -07004634 printControllerActivity(pw, sb, prefix, "Cellular",
4635 getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004636
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004637 pw.print(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004638 sb.setLength(0);
4639 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004640 sb.append(" Wifi Statistics:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004641 pw.println(sb.toString());
4642
Siddharth Ray3c648c42017-10-02 17:30:58 -07004643 pw.print(" Wifi data received: "); pw.println(formatBytesLocked(wifiRxTotalBytes));
4644 pw.print(" Wifi data sent: "); pw.println(formatBytesLocked(wifiTxTotalBytes));
4645 pw.print(" Wifi packets received: "); pw.println(wifiRxTotalPackets);
4646 pw.print(" Wifi packets sent: "); pw.println(wifiTxTotalPackets);
4647
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004648 sb.setLength(0);
4649 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004650 sb.append(" Wifi states:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004651 didOne = false;
4652 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004653 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004654 if (time == 0) {
4655 continue;
4656 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004657 sb.append("\n ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004658 didOne = true;
4659 sb.append(WIFI_STATE_NAMES[i]);
4660 sb.append(" ");
4661 formatTimeMs(sb, time/1000);
4662 sb.append("(");
4663 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4664 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004665 }
4666 if (!didOne) sb.append(" (no activity)");
4667 pw.println(sb.toString());
4668
4669 sb.setLength(0);
4670 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004671 sb.append(" Wifi supplicant states:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004672 didOne = false;
4673 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4674 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4675 if (time == 0) {
4676 continue;
4677 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004678 sb.append("\n ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004679 didOne = true;
4680 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4681 sb.append(" ");
4682 formatTimeMs(sb, time/1000);
4683 sb.append("(");
4684 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4685 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004686 }
4687 if (!didOne) sb.append(" (no activity)");
4688 pw.println(sb.toString());
4689
4690 sb.setLength(0);
4691 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004692 sb.append(" Wifi Rx signal strength (RSSI):");
4693 final String[] wifiRxSignalStrengthDescription = new String[]{
4694 "very poor (less than -88.75dBm): ",
4695 "poor (-88.75 to -77.5dBm): ",
4696 "moderate (-77.5dBm to -66.25dBm): ",
4697 "good (-66.25dBm to -55dBm): ",
4698 "great (greater than -55dBm): "};
Dianne Hackborn3251b902014-06-20 14:40:53 -07004699 didOne = false;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004700 final int numWifiRxBins = Math.min(NUM_WIFI_SIGNAL_STRENGTH_BINS,
4701 wifiRxSignalStrengthDescription.length);
4702 for (int i=0; i<numWifiRxBins; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004703 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4704 if (time == 0) {
4705 continue;
4706 }
4707 sb.append("\n ");
4708 sb.append(prefix);
4709 didOne = true;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004710 sb.append(" ");
4711 sb.append(wifiRxSignalStrengthDescription[i]);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004712 formatTimeMs(sb, time/1000);
4713 sb.append("(");
4714 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4715 sb.append(") ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004716 }
4717 if (!didOne) sb.append(" (no activity)");
4718 pw.println(sb.toString());
4719
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004720 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004721
Adam Lesinski50e47602015-12-04 17:04:54 -08004722 pw.print(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004723 sb.setLength(0);
4724 sb.append(prefix);
4725 sb.append(" CONNECTIVITY POWER SUMMARY END");
4726 pw.println(sb.toString());
4727 pw.println("");
4728
4729 pw.print(prefix);
Adam Lesinski50e47602015-12-04 17:04:54 -08004730 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4731 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4732
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004733 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4734 sb.setLength(0);
4735 sb.append(prefix);
4736 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4737 pw.println(sb.toString());
4738
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004739 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4740 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004741
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004742 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004743
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004744 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004745 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004746 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004747 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004748 pw.println(getDischargeStartLevel());
4749 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4750 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004751 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004752 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004753 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004754 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004755 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004756 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004757 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004758 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004759 pw.println(getDischargeAmountScreenOn());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004760 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004761 pw.println(getDischargeAmountScreenOff());
4762 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4763 pw.println(getDischargeAmountScreenDoze());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004764 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004765 } else {
4766 pw.print(prefix); pw.println(" Device battery use since last full charge");
4767 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004768 pw.println(getLowDischargeAmountSinceCharge());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004769 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004770 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004771 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004772 pw.println(getDischargeAmountScreenOnSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004773 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004774 pw.println(getDischargeAmountScreenOffSinceCharge());
4775 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4776 pw.println(getDischargeAmountScreenDozeSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004777 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004778 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004779
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004780 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004781 helper.create(this);
4782 helper.refreshStats(which, UserHandle.USER_ALL);
4783 List<BatterySipper> sippers = helper.getUsageList();
4784 if (sippers != null && sippers.size() > 0) {
4785 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4786 pw.print(prefix); pw.print(" Capacity: ");
4787 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004788 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004789 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4790 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4791 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4792 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004793 pw.println();
4794 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004795 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004796 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004797 switch (bs.drainType) {
4798 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004799 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004800 break;
4801 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004802 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004803 break;
4804 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004805 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004806 break;
4807 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004808 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004809 break;
4810 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004811 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004812 break;
4813 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004814 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004815 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004816 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004817 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004818 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004819 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004820 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004821 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004822 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004823 break;
4824 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004825 pw.print(" User "); pw.print(bs.userId);
4826 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004827 break;
4828 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004829 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004830 break;
4831 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004832 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004833 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004834 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004835 pw.print(" Camera: ");
4836 break;
4837 default:
4838 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004839 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004840 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004841 printmAh(pw, bs.totalPowerMah);
4842
Adam Lesinski57123002015-06-12 16:12:07 -07004843 if (bs.usagePowerMah != bs.totalPowerMah) {
4844 // If the usage (generic power) isn't the whole amount, we list out
4845 // what components are involved in the calculation.
4846
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004847 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004848 if (bs.usagePowerMah != 0) {
4849 pw.print(" usage=");
4850 printmAh(pw, bs.usagePowerMah);
4851 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004852 if (bs.cpuPowerMah != 0) {
4853 pw.print(" cpu=");
4854 printmAh(pw, bs.cpuPowerMah);
4855 }
4856 if (bs.wakeLockPowerMah != 0) {
4857 pw.print(" wake=");
4858 printmAh(pw, bs.wakeLockPowerMah);
4859 }
4860 if (bs.mobileRadioPowerMah != 0) {
4861 pw.print(" radio=");
4862 printmAh(pw, bs.mobileRadioPowerMah);
4863 }
4864 if (bs.wifiPowerMah != 0) {
4865 pw.print(" wifi=");
4866 printmAh(pw, bs.wifiPowerMah);
4867 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004868 if (bs.bluetoothPowerMah != 0) {
4869 pw.print(" bt=");
4870 printmAh(pw, bs.bluetoothPowerMah);
4871 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004872 if (bs.gpsPowerMah != 0) {
4873 pw.print(" gps=");
4874 printmAh(pw, bs.gpsPowerMah);
4875 }
4876 if (bs.sensorPowerMah != 0) {
4877 pw.print(" sensor=");
4878 printmAh(pw, bs.sensorPowerMah);
4879 }
4880 if (bs.cameraPowerMah != 0) {
4881 pw.print(" camera=");
4882 printmAh(pw, bs.cameraPowerMah);
4883 }
4884 if (bs.flashlightPowerMah != 0) {
4885 pw.print(" flash=");
4886 printmAh(pw, bs.flashlightPowerMah);
4887 }
4888 pw.print(" )");
4889 }
Bookatz17d7d9d2017-06-08 14:50:46 -07004890
4891 // If there is additional smearing information, include it.
4892 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
4893 pw.print(" Including smearing: ");
4894 printmAh(pw, bs.totalSmearedPowerMah);
4895 pw.print(" (");
4896 if (bs.screenPowerMah != 0) {
4897 pw.print(" screen=");
4898 printmAh(pw, bs.screenPowerMah);
4899 }
4900 if (bs.proportionalSmearMah != 0) {
4901 pw.print(" proportional=");
4902 printmAh(pw, bs.proportionalSmearMah);
4903 }
4904 pw.print(" )");
4905 }
4906 if (bs.shouldHide) {
4907 pw.print(" Excluded from smearing");
4908 }
4909
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004910 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004911 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004912 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004913 }
4914
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004915 sippers = helper.getMobilemsppList();
4916 if (sippers != null && sippers.size() > 0) {
4917 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004918 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004919 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004920 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004921 sb.setLength(0);
4922 sb.append(prefix); sb.append(" Uid ");
4923 UserHandle.formatUid(sb, bs.uidObj.getUid());
4924 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4925 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4926 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004927 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004928 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004929 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004930 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004931 sb.setLength(0);
4932 sb.append(prefix);
4933 sb.append(" TOTAL TIME: ");
4934 formatTimeMs(sb, totalTime);
4935 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4936 sb.append(")");
4937 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004938 pw.println();
4939 }
4940
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004941 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4942 @Override
4943 public int compare(TimerEntry lhs, TimerEntry rhs) {
4944 long lhsTime = lhs.mTime;
4945 long rhsTime = rhs.mTime;
4946 if (lhsTime < rhsTime) {
4947 return 1;
4948 }
4949 if (lhsTime > rhsTime) {
4950 return -1;
4951 }
4952 return 0;
4953 }
4954 };
4955
4956 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004957 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4958 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004959 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004960 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4961 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4962 : kernelWakelocks.entrySet()) {
4963 final BatteryStats.Timer timer = ent.getValue();
4964 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004965 if (totalTimeMillis > 0) {
4966 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4967 }
4968 }
4969 if (ktimers.size() > 0) {
4970 Collections.sort(ktimers, timerComparator);
4971 pw.print(prefix); pw.println(" All kernel wake locks:");
4972 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004973 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004974 String linePrefix = ": ";
4975 sb.setLength(0);
4976 sb.append(prefix);
4977 sb.append(" Kernel Wake lock ");
4978 sb.append(timer.mName);
4979 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4980 which, linePrefix);
4981 if (!linePrefix.equals(": ")) {
4982 sb.append(" realtime");
4983 // Only print out wake locks that were held
4984 pw.println(sb.toString());
4985 }
4986 }
4987 pw.println();
4988 }
4989 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004990
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004991 if (timers.size() > 0) {
4992 Collections.sort(timers, timerComparator);
4993 pw.print(prefix); pw.println(" All partial wake locks:");
4994 for (int i=0; i<timers.size(); i++) {
4995 TimerEntry timer = timers.get(i);
4996 sb.setLength(0);
4997 sb.append(" Wake lock ");
4998 UserHandle.formatUid(sb, timer.mId);
4999 sb.append(" ");
5000 sb.append(timer.mName);
5001 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5002 sb.append(" realtime");
5003 pw.println(sb.toString());
5004 }
5005 timers.clear();
5006 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08005007 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005008
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005009 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005010 if (wakeupReasons.size() > 0) {
5011 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005012 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005013 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005014 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005015 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
5016 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005017 }
5018 Collections.sort(reasons, timerComparator);
5019 for (int i=0; i<reasons.size(); i++) {
5020 TimerEntry timer = reasons.get(i);
5021 String linePrefix = ": ";
5022 sb.setLength(0);
5023 sb.append(prefix);
5024 sb.append(" Wakeup reason ");
5025 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005026 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5027 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005028 pw.println(sb.toString());
5029 }
5030 pw.println();
5031 }
Dianne Hackborn81038902012-11-26 17:04:09 -08005032 }
Evan Millar22ac0432009-03-31 11:33:18 -07005033
James Carr2dd7e5e2016-07-20 18:48:39 -07005034 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
Bookatz50df7112017-08-04 14:53:26 -07005035 if (mMemoryStats.size() > 0) {
5036 pw.println(" Memory Stats");
5037 for (int i = 0; i < mMemoryStats.size(); i++) {
5038 sb.setLength(0);
5039 sb.append(" Bandwidth ");
5040 sb.append(mMemoryStats.keyAt(i));
5041 sb.append(" Time ");
5042 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
5043 pw.println(sb.toString());
5044 }
5045 pw.println();
5046 }
5047
5048 final Map<String, ? extends Timer> rpmStats = getRpmStats();
5049 if (rpmStats.size() > 0) {
5050 pw.print(prefix); pw.println(" Resource Power Manager Stats");
5051 if (rpmStats.size() > 0) {
5052 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
5053 final String timerName = ent.getKey();
5054 final Timer timer = ent.getValue();
5055 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5056 }
5057 }
5058 pw.println();
5059 }
Bookatz82b341172017-09-07 19:06:08 -07005060 if (SCREEN_OFF_RPM_STATS_ENABLED) {
5061 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
Bookatz50df7112017-08-04 14:53:26 -07005062 if (screenOffRpmStats.size() > 0) {
Bookatz82b341172017-09-07 19:06:08 -07005063 pw.print(prefix);
5064 pw.println(" Resource Power Manager Stats for when screen was off");
5065 if (screenOffRpmStats.size() > 0) {
5066 for (Map.Entry<String, ? extends Timer> ent : screenOffRpmStats.entrySet()) {
5067 final String timerName = ent.getKey();
5068 final Timer timer = ent.getValue();
5069 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5070 }
Bookatz50df7112017-08-04 14:53:26 -07005071 }
Bookatz82b341172017-09-07 19:06:08 -07005072 pw.println();
Bookatz50df7112017-08-04 14:53:26 -07005073 }
James Carr2dd7e5e2016-07-20 18:48:39 -07005074 }
5075
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005076 final long[] cpuFreqs = getCpuFreqs();
5077 if (cpuFreqs != null) {
5078 sb.setLength(0);
Bookatz50df7112017-08-04 14:53:26 -07005079 sb.append(" CPU freqs:");
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005080 for (int i = 0; i < cpuFreqs.length; ++i) {
5081 sb.append(" " + cpuFreqs[i]);
5082 }
5083 pw.println(sb.toString());
Bookatz50df7112017-08-04 14:53:26 -07005084 pw.println();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005085 }
5086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087 for (int iu=0; iu<NU; iu++) {
5088 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005089 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005090 continue;
5091 }
Bookatzc8c44962017-05-11 12:12:54 -07005092
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005093 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07005094
5095 pw.print(prefix);
5096 pw.print(" ");
5097 UserHandle.formatUid(pw, uid);
5098 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005099 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005100
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005101 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
5102 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
5103 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
5104 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005105 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
5106 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
5107
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005108 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
5109 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005110 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
5111 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005112
5113 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
5114 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
5115
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005116 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
5117 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
5118 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08005119 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
5120 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5121 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
5122 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005123 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005124
Adam Lesinski5f056f62016-07-14 16:56:08 -07005125 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
5126 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
5127
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005128 if (mobileRxBytes > 0 || mobileTxBytes > 0
5129 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005130 pw.print(prefix); pw.print(" Mobile network: ");
5131 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005132 pw.print(formatBytesLocked(mobileTxBytes));
5133 pw.print(" sent (packets "); pw.print(mobileRxPackets);
5134 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005135 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005136 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
5137 sb.setLength(0);
5138 sb.append(prefix); sb.append(" Mobile radio active: ");
5139 formatTimeMs(sb, uidMobileActiveTime / 1000);
5140 sb.append("(");
5141 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
5142 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
5143 long packets = mobileRxPackets + mobileTxPackets;
5144 if (packets == 0) {
5145 packets = 1;
5146 }
5147 sb.append(" @ ");
5148 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
5149 sb.append(" mspp");
5150 pw.println(sb.toString());
5151 }
5152
Adam Lesinski5f056f62016-07-14 16:56:08 -07005153 if (mobileWakeup > 0) {
5154 sb.setLength(0);
5155 sb.append(prefix);
5156 sb.append(" Mobile radio AP wakeups: ");
5157 sb.append(mobileWakeup);
5158 pw.println(sb.toString());
5159 }
5160
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005161 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
5162 u.getModemControllerActivity(), which);
5163
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005164 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005165 pw.print(prefix); pw.print(" Wi-Fi network: ");
5166 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005167 pw.print(formatBytesLocked(wifiTxBytes));
5168 pw.print(" sent (packets "); pw.print(wifiRxPackets);
5169 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005170 }
5171
Dianne Hackborn62793e42015-03-09 11:15:41 -07005172 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08005173 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005174 || uidWifiRunningTime != 0) {
5175 sb.setLength(0);
5176 sb.append(prefix); sb.append(" Wifi Running: ");
5177 formatTimeMs(sb, uidWifiRunningTime / 1000);
5178 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
5179 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07005180 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005181 formatTimeMs(sb, fullWifiLockOnTime / 1000);
5182 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
5183 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08005184 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005185 formatTimeMs(sb, wifiScanTime / 1000);
5186 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07005187 whichBatteryRealtime)); sb.append(") ");
5188 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08005189 sb.append("x\n");
5190 // actual and background times are unpooled and since reset (regardless of 'which')
5191 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
5192 formatTimeMs(sb, wifiScanActualTime / 1000);
5193 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
5194 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5195 sb.append(") ");
5196 sb.append(wifiScanCount);
5197 sb.append("x\n");
5198 sb.append(prefix); sb.append(" Background Wifi Scan: ");
5199 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
5200 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
5201 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5202 sb.append(") ");
5203 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07005204 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005205 pw.println(sb.toString());
5206 }
5207
Adam Lesinski5f056f62016-07-14 16:56:08 -07005208 if (wifiWakeup > 0) {
5209 sb.setLength(0);
5210 sb.append(prefix);
5211 sb.append(" WiFi AP wakeups: ");
5212 sb.append(wifiWakeup);
5213 pw.println(sb.toString());
5214 }
5215
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005216 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
5217 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07005218
Adam Lesinski50e47602015-12-04 17:04:54 -08005219 if (btRxBytes > 0 || btTxBytes > 0) {
5220 pw.print(prefix); pw.print(" Bluetooth network: ");
5221 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
5222 pw.print(formatBytesLocked(btTxBytes));
5223 pw.println(" sent");
5224 }
5225
Bookatz867c0d72017-03-07 18:23:42 -08005226 final Timer bleTimer = u.getBluetoothScanTimer();
5227 if (bleTimer != null) {
5228 // Convert from microseconds to milliseconds with rounding
5229 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
5230 / 1000;
5231 if (totalTimeMs != 0) {
5232 final int count = bleTimer.getCountLocked(which);
5233 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
5234 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005235 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5236 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
5237 final long actualTimeMsBg = bleTimerBg != null ?
5238 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005239 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07005240 final int resultCount = u.getBluetoothScanResultCounter() != null ?
5241 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005242 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
5243 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
5244 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
5245 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
5246 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
5247 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5248 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
5249 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
5250 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
5251 final Timer unoptimizedScanTimerBg =
5252 u.getBluetoothUnoptimizedScanBackgroundTimer();
5253 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
5254 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5255 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
5256 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005257
5258 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08005259 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07005260 sb.append(prefix);
5261 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08005262 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07005263 sb.append(" (");
5264 sb.append(count);
5265 sb.append(" times)");
5266 if (bleTimer.isRunningLocked()) {
5267 sb.append(" (currently running)");
5268 }
5269 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005270 }
Bookatzb1f04f32017-05-19 13:57:32 -07005271
5272 sb.append(prefix);
5273 sb.append(" Bluetooth Scan (total actual realtime): ");
5274 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
5275 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005276 sb.append(count);
5277 sb.append(" times)");
5278 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07005279 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08005280 }
Bookatzb1f04f32017-05-19 13:57:32 -07005281 sb.append("\n");
5282 if (actualTimeMsBg > 0 || countBg > 0) {
5283 sb.append(prefix);
5284 sb.append(" Bluetooth Scan (background realtime): ");
5285 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
5286 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005287 sb.append(countBg);
5288 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07005289 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
5290 sb.append(" (currently running in background)");
5291 }
5292 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005293 }
Bookatzb1f04f32017-05-19 13:57:32 -07005294
5295 sb.append(prefix);
5296 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07005297 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07005298 sb.append(" (");
5299 sb.append(resultCountBg);
5300 sb.append(" in background)");
5301
5302 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
5303 sb.append("\n");
5304 sb.append(prefix);
5305 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
5306 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
5307 sb.append(" (max ");
5308 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
5309 sb.append(")");
5310 if (unoptimizedScanTimer != null
5311 && unoptimizedScanTimer.isRunningLocked()) {
5312 sb.append(" (currently running unoptimized)");
5313 }
5314 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
5315 sb.append("\n");
5316 sb.append(prefix);
5317 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
5318 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
5319 sb.append(" (max ");
5320 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
5321 sb.append(")");
5322 if (unoptimizedScanTimerBg.isRunningLocked()) {
5323 sb.append(" (currently running unoptimized in background)");
5324 }
5325 }
5326 }
Bookatz867c0d72017-03-07 18:23:42 -08005327 pw.println(sb.toString());
5328 uidActivity = true;
5329 }
5330 }
5331
5332
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005333
Dianne Hackborn617f8772009-03-31 15:04:46 -07005334 if (u.hasUserActivity()) {
5335 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07005336 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005337 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005338 if (val != 0) {
5339 if (!hasData) {
5340 sb.setLength(0);
5341 sb.append(" User activity: ");
5342 hasData = true;
5343 } else {
5344 sb.append(", ");
5345 }
5346 sb.append(val);
5347 sb.append(" ");
5348 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
5349 }
5350 }
5351 if (hasData) {
5352 pw.println(sb.toString());
5353 }
5354 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005355
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005356 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
5357 = u.getWakelockStats();
5358 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005359 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005360 int countWakelock = 0;
5361 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
5362 final Uid.Wakelock wl = wakelocks.valueAt(iw);
5363 String linePrefix = ": ";
5364 sb.setLength(0);
5365 sb.append(prefix);
5366 sb.append(" Wake lock ");
5367 sb.append(wakelocks.keyAt(iw));
5368 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
5369 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005370 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
5371 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005372 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005373 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
5374 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005375 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
5376 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005377 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
5378 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07005379 sb.append(" realtime");
5380 pw.println(sb.toString());
5381 uidActivity = true;
5382 countWakelock++;
5383
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005384 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
5385 rawRealtime, which);
5386 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
5387 rawRealtime, which);
5388 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
5389 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005390 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07005391 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005392 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005393 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07005394 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
5395 // pooled and therefore just a lower bound)
5396 long actualTotalPartialWakelock = 0;
5397 long actualBgPartialWakelock = 0;
5398 if (u.getAggregatedPartialWakelockTimer() != null) {
5399 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
5400 // Convert from microseconds to milliseconds with rounding
5401 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07005402 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07005403 final Timer bgAggTimer = aggTimer.getSubTimer();
5404 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005405 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07005406 }
5407
5408 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
5409 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
5410 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005411 sb.setLength(0);
5412 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005413 sb.append(" TOTAL wake: ");
5414 boolean needComma = false;
5415 if (totalFullWakelock != 0) {
5416 needComma = true;
5417 formatTimeMs(sb, totalFullWakelock);
5418 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005419 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005420 if (totalPartialWakelock != 0) {
5421 if (needComma) {
5422 sb.append(", ");
5423 }
5424 needComma = true;
5425 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07005426 sb.append("blamed partial");
5427 }
5428 if (actualTotalPartialWakelock != 0) {
5429 if (needComma) {
5430 sb.append(", ");
5431 }
5432 needComma = true;
5433 formatTimeMs(sb, actualTotalPartialWakelock);
5434 sb.append("actual partial");
5435 }
5436 if (actualBgPartialWakelock != 0) {
5437 if (needComma) {
5438 sb.append(", ");
5439 }
5440 needComma = true;
5441 formatTimeMs(sb, actualBgPartialWakelock);
5442 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005443 }
5444 if (totalWindowWakelock != 0) {
5445 if (needComma) {
5446 sb.append(", ");
5447 }
5448 needComma = true;
5449 formatTimeMs(sb, totalWindowWakelock);
5450 sb.append("window");
5451 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005452 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07005453 if (needComma) {
5454 sb.append(",");
5455 }
5456 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005457 formatTimeMs(sb, totalDrawWakelock);
5458 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07005459 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005460 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005461 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005462 }
5463 }
5464
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07005465 // Calculate multicast wakelock stats
5466 final Timer mcTimer = u.getMulticastWakelockStats();
5467 if (mcTimer != null) {
5468 final long multicastWakeLockTimeMicros = mcTimer.getTotalTimeLocked(rawRealtime, which);
5469 final int multicastWakeLockCount = mcTimer.getCountLocked(which);
5470
5471 if (multicastWakeLockTimeMicros > 0) {
5472 sb.setLength(0);
5473 sb.append(prefix);
5474 sb.append(" WiFi Multicast Wakelock");
5475 sb.append(" count = ");
5476 sb.append(multicastWakeLockCount);
5477 sb.append(" time = ");
5478 formatTimeMsNoSpace(sb, (multicastWakeLockTimeMicros + 500) / 1000);
5479 pw.println(sb.toString());
5480 }
5481 }
5482
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005483 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
5484 for (int isy=syncs.size()-1; isy>=0; isy--) {
5485 final Timer timer = syncs.valueAt(isy);
5486 // Convert from microseconds to milliseconds with rounding
5487 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5488 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07005489 final Timer bgTimer = timer.getSubTimer();
5490 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005491 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07005492 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005493 sb.setLength(0);
5494 sb.append(prefix);
5495 sb.append(" Sync ");
5496 sb.append(syncs.keyAt(isy));
5497 sb.append(": ");
5498 if (totalTime != 0) {
5499 formatTimeMs(sb, totalTime);
5500 sb.append("realtime (");
5501 sb.append(count);
5502 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07005503 if (bgTime > 0) {
5504 sb.append(", ");
5505 formatTimeMs(sb, bgTime);
5506 sb.append("background (");
5507 sb.append(bgCount);
5508 sb.append(" times)");
5509 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005510 } else {
5511 sb.append("(not used)");
5512 }
5513 pw.println(sb.toString());
5514 uidActivity = true;
5515 }
5516
5517 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
5518 for (int ij=jobs.size()-1; ij>=0; ij--) {
5519 final Timer timer = jobs.valueAt(ij);
5520 // Convert from microseconds to milliseconds with rounding
5521 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5522 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07005523 final Timer bgTimer = timer.getSubTimer();
5524 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005525 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07005526 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005527 sb.setLength(0);
5528 sb.append(prefix);
5529 sb.append(" Job ");
5530 sb.append(jobs.keyAt(ij));
5531 sb.append(": ");
5532 if (totalTime != 0) {
5533 formatTimeMs(sb, totalTime);
5534 sb.append("realtime (");
5535 sb.append(count);
5536 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07005537 if (bgTime > 0) {
5538 sb.append(", ");
5539 formatTimeMs(sb, bgTime);
5540 sb.append("background (");
5541 sb.append(bgCount);
5542 sb.append(" times)");
5543 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005544 } else {
5545 sb.append("(not used)");
5546 }
5547 pw.println(sb.toString());
5548 uidActivity = true;
5549 }
5550
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005551 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
5552 for (int ic=completions.size()-1; ic>=0; ic--) {
5553 SparseIntArray types = completions.valueAt(ic);
5554 if (types != null) {
5555 pw.print(prefix);
5556 pw.print(" Job Completions ");
5557 pw.print(completions.keyAt(ic));
5558 pw.print(":");
5559 for (int it=0; it<types.size(); it++) {
5560 pw.print(" ");
5561 pw.print(JobParameters.getReasonName(types.keyAt(it)));
5562 pw.print("(");
5563 pw.print(types.valueAt(it));
5564 pw.print("x)");
5565 }
5566 pw.println();
5567 }
5568 }
5569
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005570 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
5571 prefix, "Flashlight");
5572 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
5573 prefix, "Camera");
5574 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
5575 prefix, "Video");
5576 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
5577 prefix, "Audio");
5578
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005579 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
5580 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005581 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005582 final Uid.Sensor se = sensors.valueAt(ise);
5583 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005584 sb.setLength(0);
5585 sb.append(prefix);
5586 sb.append(" Sensor ");
5587 int handle = se.getHandle();
5588 if (handle == Uid.Sensor.GPS) {
5589 sb.append("GPS");
5590 } else {
5591 sb.append(handle);
5592 }
5593 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005594
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005595 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005596 if (timer != null) {
5597 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005598 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5599 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005600 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005601 final Timer bgTimer = se.getSensorBackgroundTime();
5602 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005603 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5604 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5605 final long bgActualTime = bgTimer != null ?
5606 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5607
Dianne Hackborn61659e52014-07-09 16:13:01 -07005608 //timer.logState();
5609 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005610 if (actualTime != totalTime) {
5611 formatTimeMs(sb, totalTime);
5612 sb.append("blamed realtime, ");
5613 }
5614
5615 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005616 sb.append("realtime (");
5617 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005618 sb.append(" times)");
5619
5620 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005621 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005622 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5623 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005624 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005625 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005627 } else {
5628 sb.append("(not used)");
5629 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005630 } else {
5631 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005632 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005633
5634 pw.println(sb.toString());
5635 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005636 }
5637
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005638 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5639 "Vibrator");
5640 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5641 prefix, "Foreground activities");
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005642 uidActivity |= printTimer(pw, sb, u.getForegroundServiceTimer(), rawRealtime, which,
5643 prefix, "Foreground services");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005644
Dianne Hackborn61659e52014-07-09 16:13:01 -07005645 long totalStateTime = 0;
5646 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5647 long time = u.getProcessStateTime(ips, rawRealtime, which);
5648 if (time > 0) {
5649 totalStateTime += time;
5650 sb.setLength(0);
5651 sb.append(prefix);
5652 sb.append(" ");
5653 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5654 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005655 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005656 pw.println(sb.toString());
5657 uidActivity = true;
5658 }
5659 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005660 if (totalStateTime > 0) {
5661 sb.setLength(0);
5662 sb.append(prefix);
5663 sb.append(" Total running: ");
5664 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5665 pw.println(sb.toString());
5666 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005667
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005668 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5669 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005670 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005671 sb.setLength(0);
5672 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005673 sb.append(" Total cpu time: u=");
5674 formatTimeMs(sb, userCpuTimeUs / 1000);
5675 sb.append("s=");
5676 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005677 pw.println(sb.toString());
5678 }
5679
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005680 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5681 if (cpuFreqTimes != null) {
5682 sb.setLength(0);
5683 sb.append(" Total cpu time per freq:");
5684 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5685 sb.append(" " + cpuFreqTimes[i]);
5686 }
5687 pw.println(sb.toString());
5688 }
5689 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5690 if (screenOffCpuFreqTimes != null) {
5691 sb.setLength(0);
5692 sb.append(" Total screen-off cpu time per freq:");
5693 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5694 sb.append(" " + screenOffCpuFreqTimes[i]);
5695 }
5696 pw.println(sb.toString());
5697 }
5698
Sudheer Shankab2f83c12017-11-13 19:25:01 -08005699 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
5700 final long[] cpuTimes = u.getCpuFreqTimes(which, procState);
5701 if (cpuTimes != null) {
5702 sb.setLength(0);
5703 sb.append(" Cpu times per freq at state "
5704 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5705 for (int i = 0; i < cpuTimes.length; ++i) {
5706 sb.append(" " + cpuTimes[i]);
5707 }
5708 pw.println(sb.toString());
5709 }
5710
5711 final long[] screenOffCpuTimes = u.getScreenOffCpuFreqTimes(which, procState);
5712 if (screenOffCpuTimes != null) {
5713 sb.setLength(0);
5714 sb.append(" Screen-off cpu times per freq at state "
5715 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5716 for (int i = 0; i < screenOffCpuTimes.length; ++i) {
5717 sb.append(" " + screenOffCpuTimes[i]);
5718 }
5719 pw.println(sb.toString());
5720 }
5721 }
5722
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005723 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5724 = u.getProcessStats();
5725 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5726 final Uid.Proc ps = processStats.valueAt(ipr);
5727 long userTime;
5728 long systemTime;
5729 long foregroundTime;
5730 int starts;
5731 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005732
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005733 userTime = ps.getUserTime(which);
5734 systemTime = ps.getSystemTime(which);
5735 foregroundTime = ps.getForegroundTime(which);
5736 starts = ps.getStarts(which);
5737 final int numCrashes = ps.getNumCrashes(which);
5738 final int numAnrs = ps.getNumAnrs(which);
5739 numExcessive = which == STATS_SINCE_CHARGED
5740 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005741
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005742 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5743 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5744 sb.setLength(0);
5745 sb.append(prefix); sb.append(" Proc ");
5746 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5747 sb.append(prefix); sb.append(" CPU: ");
5748 formatTimeMs(sb, userTime); sb.append("usr + ");
5749 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5750 formatTimeMs(sb, foregroundTime); sb.append("fg");
5751 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5752 sb.append("\n"); sb.append(prefix); sb.append(" ");
5753 boolean hasOne = false;
5754 if (starts != 0) {
5755 hasOne = true;
5756 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005757 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005758 if (numCrashes != 0) {
5759 if (hasOne) {
5760 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005761 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005762 hasOne = true;
5763 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005764 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005765 if (numAnrs != 0) {
5766 if (hasOne) {
5767 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005768 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005769 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005770 }
5771 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005772 pw.println(sb.toString());
5773 for (int e=0; e<numExcessive; e++) {
5774 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5775 if (ew != null) {
5776 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07005777 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005778 pw.print("cpu");
5779 } else {
5780 pw.print("unknown");
5781 }
5782 pw.print(" use: ");
5783 TimeUtils.formatDuration(ew.usedTime, pw);
5784 pw.print(" over ");
5785 TimeUtils.formatDuration(ew.overTime, pw);
5786 if (ew.overTime != 0) {
5787 pw.print(" (");
5788 pw.print((ew.usedTime*100)/ew.overTime);
5789 pw.println("%)");
5790 }
5791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005792 }
5793 uidActivity = true;
5794 }
5795 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005796
5797 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5798 = u.getPackageStats();
5799 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
5800 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
5801 pw.println(":");
5802 boolean apkActivity = false;
5803 final Uid.Pkg ps = packageStats.valueAt(ipkg);
5804 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
5805 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
5806 pw.print(prefix); pw.print(" Wakeup alarm ");
5807 pw.print(alarms.keyAt(iwa)); pw.print(": ");
5808 pw.print(alarms.valueAt(iwa).getCountLocked(which));
5809 pw.println(" times");
5810 apkActivity = true;
5811 }
5812 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
5813 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
5814 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
5815 final long startTime = ss.getStartTime(batteryUptime, which);
5816 final int starts = ss.getStarts(which);
5817 final int launches = ss.getLaunches(which);
5818 if (startTime != 0 || starts != 0 || launches != 0) {
5819 sb.setLength(0);
5820 sb.append(prefix); sb.append(" Service ");
5821 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
5822 sb.append(prefix); sb.append(" Created for: ");
5823 formatTimeMs(sb, startTime / 1000);
5824 sb.append("uptime\n");
5825 sb.append(prefix); sb.append(" Starts: ");
5826 sb.append(starts);
5827 sb.append(", launches: "); sb.append(launches);
5828 pw.println(sb.toString());
5829 apkActivity = true;
5830 }
5831 }
5832 if (!apkActivity) {
5833 pw.print(prefix); pw.println(" (nothing executed)");
5834 }
5835 uidActivity = true;
5836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005837 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005838 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005839 }
5840 }
5841 }
5842
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005843 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005844 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005845 int diff = oldval ^ newval;
5846 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005847 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005848 for (int i=0; i<descriptions.length; i++) {
5849 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005850 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005851 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005852 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005853 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005854 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005855 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
5856 didWake = true;
5857 pw.print("=");
5858 if (longNames) {
5859 UserHandle.formatUid(pw, wakelockTag.uid);
5860 pw.print(":\"");
5861 pw.print(wakelockTag.string);
5862 pw.print("\"");
5863 } else {
5864 pw.print(wakelockTag.poolIdx);
5865 }
5866 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005867 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005868 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005869 pw.print("=");
5870 int val = (newval&bd.mask)>>bd.shift;
5871 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005872 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005873 } else {
5874 pw.print(val);
5875 }
5876 }
5877 }
5878 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005879 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07005880 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005881 if (longNames) {
5882 UserHandle.formatUid(pw, wakelockTag.uid);
5883 pw.print(":\"");
5884 pw.print(wakelockTag.string);
5885 pw.print("\"");
5886 } else {
5887 pw.print(wakelockTag.poolIdx);
5888 }
5889 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005890 }
Mike Mac2f518a2017-09-19 16:06:03 -07005891
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005892 public void prepareForDumpLocked() {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07005893 // We don't need to require subclasses implement this.
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005894 }
5895
5896 public static class HistoryPrinter {
5897 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005898 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005899 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005900 int oldStatus = -1;
5901 int oldHealth = -1;
5902 int oldPlug = -1;
5903 int oldTemp = -1;
5904 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005905 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005906 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005907
Dianne Hackborn3251b902014-06-20 14:40:53 -07005908 void reset() {
5909 oldState = oldState2 = 0;
5910 oldLevel = -1;
5911 oldStatus = -1;
5912 oldHealth = -1;
5913 oldPlug = -1;
5914 oldTemp = -1;
5915 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005916 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07005917 }
5918
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005919 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005920 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005921 if (!checkin) {
5922 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005923 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005924 pw.print(" (");
5925 pw.print(rec.numReadInts);
5926 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005927 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005928 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5929 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005930 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005931 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005932 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005933 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005934 }
5935 lastTime = rec.time;
5936 }
5937 if (rec.cmd == HistoryItem.CMD_START) {
5938 if (checkin) {
5939 pw.print(":");
5940 }
5941 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005942 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005943 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
5944 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005945 if (checkin) {
5946 pw.print(":");
5947 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005948 if (rec.cmd == HistoryItem.CMD_RESET) {
5949 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005950 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005951 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005952 pw.print("TIME:");
5953 if (checkin) {
5954 pw.println(rec.currentTime);
5955 } else {
5956 pw.print(" ");
5957 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5958 rec.currentTime).toString());
5959 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005960 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
5961 if (checkin) {
5962 pw.print(":");
5963 }
5964 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005965 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
5966 if (checkin) {
5967 pw.print(":");
5968 }
5969 pw.println("*OVERFLOW*");
5970 } else {
5971 if (!checkin) {
5972 if (rec.batteryLevel < 10) pw.print("00");
5973 else if (rec.batteryLevel < 100) pw.print("0");
5974 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005975 if (verbose) {
5976 pw.print(" ");
5977 if (rec.states < 0) ;
5978 else if (rec.states < 0x10) pw.print("0000000");
5979 else if (rec.states < 0x100) pw.print("000000");
5980 else if (rec.states < 0x1000) pw.print("00000");
5981 else if (rec.states < 0x10000) pw.print("0000");
5982 else if (rec.states < 0x100000) pw.print("000");
5983 else if (rec.states < 0x1000000) pw.print("00");
5984 else if (rec.states < 0x10000000) pw.print("0");
5985 pw.print(Integer.toHexString(rec.states));
5986 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005987 } else {
5988 if (oldLevel != rec.batteryLevel) {
5989 oldLevel = rec.batteryLevel;
5990 pw.print(",Bl="); pw.print(rec.batteryLevel);
5991 }
5992 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005993 if (oldStatus != rec.batteryStatus) {
5994 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005995 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005996 switch (oldStatus) {
5997 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005998 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005999 break;
6000 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006001 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006002 break;
6003 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006004 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006005 break;
6006 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006007 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006008 break;
6009 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006010 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006011 break;
6012 default:
6013 pw.print(oldStatus);
6014 break;
6015 }
6016 }
6017 if (oldHealth != rec.batteryHealth) {
6018 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006019 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006020 switch (oldHealth) {
6021 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006022 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006023 break;
6024 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006025 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006026 break;
6027 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006028 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006029 break;
6030 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006031 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006032 break;
6033 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006034 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006035 break;
6036 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006037 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006038 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006039 case BatteryManager.BATTERY_HEALTH_COLD:
6040 pw.print(checkin ? "c" : "cold");
6041 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006042 default:
6043 pw.print(oldHealth);
6044 break;
6045 }
6046 }
6047 if (oldPlug != rec.batteryPlugType) {
6048 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006049 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006050 switch (oldPlug) {
6051 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006052 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006053 break;
6054 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006055 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006056 break;
6057 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006058 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006059 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006060 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006061 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006062 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006063 default:
6064 pw.print(oldPlug);
6065 break;
6066 }
6067 }
6068 if (oldTemp != rec.batteryTemperature) {
6069 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006070 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006071 pw.print(oldTemp);
6072 }
6073 if (oldVolt != rec.batteryVoltage) {
6074 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006075 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006076 pw.print(oldVolt);
6077 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006078 final int chargeMAh = rec.batteryChargeUAh / 1000;
6079 if (oldChargeMAh != chargeMAh) {
6080 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07006081 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006082 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07006083 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006084 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006085 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006086 printBitDescriptions(pw, oldState2, rec.states2, null,
6087 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006088 if (rec.wakeReasonTag != null) {
6089 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006090 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006091 pw.print(rec.wakeReasonTag.poolIdx);
6092 } else {
6093 pw.print(" wake_reason=");
6094 pw.print(rec.wakeReasonTag.uid);
6095 pw.print(":\"");
6096 pw.print(rec.wakeReasonTag.string);
6097 pw.print("\"");
6098 }
6099 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08006100 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006101 pw.print(checkin ? "," : " ");
6102 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
6103 pw.print("+");
6104 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
6105 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006106 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006107 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
6108 : HISTORY_EVENT_NAMES;
6109 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
6110 | HistoryItem.EVENT_FLAG_FINISH);
6111 if (idx >= 0 && idx < eventNames.length) {
6112 pw.print(eventNames[idx]);
6113 } else {
6114 pw.print(checkin ? "Ev" : "event");
6115 pw.print(idx);
6116 }
6117 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006118 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006119 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08006120 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08006121 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
6122 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006123 pw.print(":\"");
6124 pw.print(rec.eventTag.string);
6125 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006126 }
6127 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006128 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006129 if (rec.stepDetails != null) {
6130 if (!checkin) {
6131 pw.print(" Details: cpu=");
6132 pw.print(rec.stepDetails.userTime);
6133 pw.print("u+");
6134 pw.print(rec.stepDetails.systemTime);
6135 pw.print("s");
6136 if (rec.stepDetails.appCpuUid1 >= 0) {
6137 pw.print(" (");
6138 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
6139 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6140 if (rec.stepDetails.appCpuUid2 >= 0) {
6141 pw.print(", ");
6142 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
6143 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6144 }
6145 if (rec.stepDetails.appCpuUid3 >= 0) {
6146 pw.print(", ");
6147 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
6148 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6149 }
6150 pw.print(')');
6151 }
6152 pw.println();
6153 pw.print(" /proc/stat=");
6154 pw.print(rec.stepDetails.statUserTime);
6155 pw.print(" usr, ");
6156 pw.print(rec.stepDetails.statSystemTime);
6157 pw.print(" sys, ");
6158 pw.print(rec.stepDetails.statIOWaitTime);
6159 pw.print(" io, ");
6160 pw.print(rec.stepDetails.statIrqTime);
6161 pw.print(" irq, ");
6162 pw.print(rec.stepDetails.statSoftIrqTime);
6163 pw.print(" sirq, ");
6164 pw.print(rec.stepDetails.statIdlTime);
6165 pw.print(" idle");
6166 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
6167 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
6168 + rec.stepDetails.statSoftIrqTime;
6169 int total = totalRun + rec.stepDetails.statIdlTime;
6170 if (total > 0) {
6171 pw.print(" (");
6172 float perc = ((float)totalRun) / ((float)total) * 100;
6173 pw.print(String.format("%.1f%%", perc));
6174 pw.print(" of ");
6175 StringBuilder sb = new StringBuilder(64);
6176 formatTimeMsNoSpace(sb, total*10);
6177 pw.print(sb);
6178 pw.print(")");
6179 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006180 pw.print(", PlatformIdleStat ");
6181 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006182 pw.println();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006183
6184 pw.print(", SubsystemPowerState ");
6185 pw.print(rec.stepDetails.statSubsystemPowerState);
6186 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006187 } else {
6188 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6189 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
6190 pw.print(rec.stepDetails.userTime);
6191 pw.print(":");
6192 pw.print(rec.stepDetails.systemTime);
6193 if (rec.stepDetails.appCpuUid1 >= 0) {
6194 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
6195 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6196 if (rec.stepDetails.appCpuUid2 >= 0) {
6197 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
6198 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6199 }
6200 if (rec.stepDetails.appCpuUid3 >= 0) {
6201 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
6202 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6203 }
6204 }
6205 pw.println();
6206 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6207 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
6208 pw.print(rec.stepDetails.statUserTime);
6209 pw.print(',');
6210 pw.print(rec.stepDetails.statSystemTime);
6211 pw.print(',');
6212 pw.print(rec.stepDetails.statIOWaitTime);
6213 pw.print(',');
6214 pw.print(rec.stepDetails.statIrqTime);
6215 pw.print(',');
6216 pw.print(rec.stepDetails.statSoftIrqTime);
6217 pw.print(',');
6218 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006219 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006220 if (rec.stepDetails.statPlatformIdleState != null) {
6221 pw.print(rec.stepDetails.statPlatformIdleState);
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006222 if (rec.stepDetails.statSubsystemPowerState != null) {
6223 pw.print(',');
6224 }
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006225 }
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006226
6227 if (rec.stepDetails.statSubsystemPowerState != null) {
6228 pw.print(rec.stepDetails.statSubsystemPowerState);
6229 }
6230 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006231 }
6232 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006233 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006234 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006235 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006236 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006237
6238 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
6239 UserHandle.formatUid(pw, uid);
6240 pw.print("=");
6241 pw.print(utime);
6242 pw.print("u+");
6243 pw.print(stime);
6244 pw.print("s");
6245 }
6246
6247 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
6248 pw.print('/');
6249 pw.print(uid);
6250 pw.print(":");
6251 pw.print(utime);
6252 pw.print(":");
6253 pw.print(stime);
6254 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006255 }
6256
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006257 private void printSizeValue(PrintWriter pw, long size) {
6258 float result = size;
6259 String suffix = "";
6260 if (result >= 10*1024) {
6261 suffix = "KB";
6262 result = result / 1024;
6263 }
6264 if (result >= 10*1024) {
6265 suffix = "MB";
6266 result = result / 1024;
6267 }
6268 if (result >= 10*1024) {
6269 suffix = "GB";
6270 result = result / 1024;
6271 }
6272 if (result >= 10*1024) {
6273 suffix = "TB";
6274 result = result / 1024;
6275 }
6276 if (result >= 10*1024) {
6277 suffix = "PB";
6278 result = result / 1024;
6279 }
6280 pw.print((int)result);
6281 pw.print(suffix);
6282 }
6283
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006284 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
6285 String label3, long estimatedTime) {
6286 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006287 return false;
6288 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006289 pw.print(label1);
6290 pw.print(label2);
6291 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006292 StringBuilder sb = new StringBuilder(64);
6293 formatTimeMs(sb, estimatedTime);
6294 pw.print(sb);
6295 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006296 return true;
6297 }
6298
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006299 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
6300 LevelStepTracker steps, boolean checkin) {
6301 if (steps == null) {
6302 return false;
6303 }
6304 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006305 if (count <= 0) {
6306 return false;
6307 }
6308 if (!checkin) {
6309 pw.println(header);
6310 }
Kweku Adams030980a2015-04-01 16:07:48 -07006311 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006312 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006313 long duration = steps.getDurationAt(i);
6314 int level = steps.getLevelAt(i);
6315 long initMode = steps.getInitModeAt(i);
6316 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006317 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006318 lineArgs[0] = Long.toString(duration);
6319 lineArgs[1] = Integer.toString(level);
6320 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6321 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6322 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
6323 case Display.STATE_ON: lineArgs[2] = "s+"; break;
6324 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
6325 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07006326 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006327 }
6328 } else {
6329 lineArgs[2] = "";
6330 }
6331 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6332 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
6333 } else {
6334 lineArgs[3] = "";
6335 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006336 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07006337 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006338 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07006339 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006340 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006341 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
6342 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006343 pw.print(prefix);
6344 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006345 TimeUtils.formatDuration(duration, pw);
6346 pw.print(" to "); pw.print(level);
6347 boolean haveModes = false;
6348 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6349 pw.print(" (");
6350 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6351 case Display.STATE_OFF: pw.print("screen-off"); break;
6352 case Display.STATE_ON: pw.print("screen-on"); break;
6353 case Display.STATE_DOZE: pw.print("screen-doze"); break;
6354 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07006355 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006356 }
6357 haveModes = true;
6358 }
6359 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6360 pw.print(haveModes ? ", " : " (");
6361 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
6362 ? "power-save-on" : "power-save-off");
6363 haveModes = true;
6364 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006365 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6366 pw.print(haveModes ? ", " : " (");
6367 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6368 ? "device-idle-on" : "device-idle-off");
6369 haveModes = true;
6370 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006371 if (haveModes) {
6372 pw.print(")");
6373 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006374 pw.println();
6375 }
6376 }
6377 return true;
6378 }
6379
Kweku Adams87b19ec2017-10-09 12:40:03 -07006380 private static void dumpDurationSteps(ProtoOutputStream proto, long fieldId,
6381 LevelStepTracker steps) {
6382 if (steps == null) {
6383 return;
6384 }
6385 int count = steps.mNumStepDurations;
Kweku Adams87b19ec2017-10-09 12:40:03 -07006386 for (int i = 0; i < count; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07006387 long token = proto.start(fieldId);
Kweku Adams87b19ec2017-10-09 12:40:03 -07006388 proto.write(SystemProto.BatteryLevelStep.DURATION_MS, steps.getDurationAt(i));
6389 proto.write(SystemProto.BatteryLevelStep.LEVEL, steps.getLevelAt(i));
6390
6391 final long initMode = steps.getInitModeAt(i);
6392 final long modMode = steps.getModModeAt(i);
6393
6394 int ds = SystemProto.BatteryLevelStep.DS_MIXED;
6395 if ((modMode & STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6396 switch ((int) (initMode & STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6397 case Display.STATE_OFF:
6398 ds = SystemProto.BatteryLevelStep.DS_OFF;
6399 break;
6400 case Display.STATE_ON:
6401 ds = SystemProto.BatteryLevelStep.DS_ON;
6402 break;
6403 case Display.STATE_DOZE:
6404 ds = SystemProto.BatteryLevelStep.DS_DOZE;
6405 break;
6406 case Display.STATE_DOZE_SUSPEND:
6407 ds = SystemProto.BatteryLevelStep.DS_DOZE_SUSPEND;
6408 break;
6409 default:
6410 ds = SystemProto.BatteryLevelStep.DS_ERROR;
6411 break;
6412 }
6413 }
6414 proto.write(SystemProto.BatteryLevelStep.DISPLAY_STATE, ds);
6415
6416 int psm = SystemProto.BatteryLevelStep.PSM_MIXED;
6417 if ((modMode & STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6418 psm = (initMode & STEP_LEVEL_MODE_POWER_SAVE) != 0
6419 ? SystemProto.BatteryLevelStep.PSM_ON : SystemProto.BatteryLevelStep.PSM_OFF;
6420 }
6421 proto.write(SystemProto.BatteryLevelStep.POWER_SAVE_MODE, psm);
6422
6423 int im = SystemProto.BatteryLevelStep.IM_MIXED;
6424 if ((modMode & STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6425 im = (initMode & STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6426 ? SystemProto.BatteryLevelStep.IM_ON : SystemProto.BatteryLevelStep.IM_OFF;
6427 }
6428 proto.write(SystemProto.BatteryLevelStep.IDLE_MODE, im);
6429
6430 proto.end(token);
6431 }
6432 }
6433
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006434 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006435 public static final int DUMP_DAILY_ONLY = 1<<2;
6436 public static final int DUMP_HISTORY_ONLY = 1<<3;
6437 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
6438 public static final int DUMP_VERBOSE = 1<<5;
6439 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006440
Dianne Hackborn37de0982014-05-09 09:32:18 -07006441 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
6442 final HistoryPrinter hprinter = new HistoryPrinter();
6443 final HistoryItem rec = new HistoryItem();
6444 long lastTime = -1;
6445 long baseTime = -1;
6446 boolean printed = false;
6447 HistoryEventTracker tracker = null;
6448 while (getNextHistoryLocked(rec)) {
6449 lastTime = rec.time;
6450 if (baseTime < 0) {
6451 baseTime = lastTime;
6452 }
6453 if (rec.time >= histStart) {
6454 if (histStart >= 0 && !printed) {
6455 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07006456 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006457 || rec.cmd == HistoryItem.CMD_START
6458 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006459 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006460 hprinter.printNextItem(pw, rec, baseTime, checkin,
6461 (flags&DUMP_VERBOSE) != 0);
6462 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006463 } else if (rec.currentTime != 0) {
6464 printed = true;
6465 byte cmd = rec.cmd;
6466 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006467 hprinter.printNextItem(pw, rec, baseTime, checkin,
6468 (flags&DUMP_VERBOSE) != 0);
6469 rec.cmd = cmd;
6470 }
6471 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006472 if (rec.cmd != HistoryItem.CMD_UPDATE) {
6473 hprinter.printNextItem(pw, rec, baseTime, checkin,
6474 (flags&DUMP_VERBOSE) != 0);
6475 rec.cmd = HistoryItem.CMD_UPDATE;
6476 }
6477 int oldEventCode = rec.eventCode;
6478 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006479 rec.eventTag = new HistoryTag();
6480 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
6481 HashMap<String, SparseIntArray> active
6482 = tracker.getStateForEvent(i);
6483 if (active == null) {
6484 continue;
6485 }
6486 for (HashMap.Entry<String, SparseIntArray> ent
6487 : active.entrySet()) {
6488 SparseIntArray uids = ent.getValue();
6489 for (int j=0; j<uids.size(); j++) {
6490 rec.eventCode = i;
6491 rec.eventTag.string = ent.getKey();
6492 rec.eventTag.uid = uids.keyAt(j);
6493 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006494 hprinter.printNextItem(pw, rec, baseTime, checkin,
6495 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006496 rec.wakeReasonTag = null;
6497 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006498 }
6499 }
6500 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006501 rec.eventCode = oldEventCode;
6502 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006503 tracker = null;
6504 }
6505 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006506 hprinter.printNextItem(pw, rec, baseTime, checkin,
6507 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07006508 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
6509 // This is an attempt to aggregate the previous state and generate
6510 // fake events to reflect that state at the point where we start
6511 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07006512 if (tracker == null) {
6513 tracker = new HistoryEventTracker();
6514 }
6515 tracker.updateState(rec.eventCode, rec.eventTag.string,
6516 rec.eventTag.uid, rec.eventTag.poolIdx);
6517 }
6518 }
6519 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07006520 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006521 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
6522 }
6523 }
6524
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006525 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
6526 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
6527 if (steps == null) {
6528 return;
6529 }
6530 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
6531 if (timeRemaining >= 0) {
6532 pw.print(prefix); pw.print(label); pw.print(" total time: ");
6533 tmpSb.setLength(0);
6534 formatTimeMs(tmpSb, timeRemaining);
6535 pw.print(tmpSb);
6536 pw.print(" (from "); pw.print(tmpOutInt[0]);
6537 pw.println(" steps)");
6538 }
6539 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6540 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6541 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
6542 if (estimatedTime > 0) {
6543 pw.print(prefix); pw.print(label); pw.print(" ");
6544 pw.print(STEP_LEVEL_MODE_LABELS[i]);
6545 pw.print(" time: ");
6546 tmpSb.setLength(0);
6547 formatTimeMs(tmpSb, estimatedTime);
6548 pw.print(tmpSb);
6549 pw.print(" (from "); pw.print(tmpOutInt[0]);
6550 pw.println(" steps)");
6551 }
6552 }
6553 }
6554
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006555 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
6556 ArrayList<PackageChange> changes) {
6557 if (changes == null) {
6558 return;
6559 }
6560 pw.print(prefix); pw.println("Package changes:");
6561 for (int i=0; i<changes.size(); i++) {
6562 PackageChange pc = changes.get(i);
6563 if (pc.mUpdate) {
6564 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
6565 pw.print(" vers="); pw.println(pc.mVersionCode);
6566 } else {
6567 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
6568 }
6569 }
6570 }
6571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006572 /**
6573 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
6574 *
6575 * @param pw a Printer to receive the dump output.
6576 */
6577 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006578 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006579 prepareForDumpLocked();
6580
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006581 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006582 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006583
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006584 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006585 final long historyTotalSize = getHistoryTotalSize();
6586 final long historyUsedSize = getHistoryUsedSize();
6587 if (startIteratingHistoryLocked()) {
6588 try {
6589 pw.print("Battery History (");
6590 pw.print((100*historyUsedSize)/historyTotalSize);
6591 pw.print("% used, ");
6592 printSizeValue(pw, historyUsedSize);
6593 pw.print(" used of ");
6594 printSizeValue(pw, historyTotalSize);
6595 pw.print(", ");
6596 pw.print(getHistoryStringPoolSize());
6597 pw.print(" strings using ");
6598 printSizeValue(pw, getHistoryStringPoolBytes());
6599 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07006600 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006601 pw.println();
6602 } finally {
6603 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006604 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006605 }
6606
6607 if (startIteratingOldHistoryLocked()) {
6608 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006609 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006610 pw.println("Old battery History:");
6611 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006612 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006613 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006614 if (baseTime < 0) {
6615 baseTime = rec.time;
6616 }
6617 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006618 }
6619 pw.println();
6620 } finally {
6621 finishIteratingOldHistoryLocked();
6622 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006623 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006624 }
6625
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006626 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006627 return;
6628 }
6629
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006630 if (!filtering) {
6631 SparseArray<? extends Uid> uidStats = getUidStats();
6632 final int NU = uidStats.size();
6633 boolean didPid = false;
6634 long nowRealtime = SystemClock.elapsedRealtime();
6635 for (int i=0; i<NU; i++) {
6636 Uid uid = uidStats.valueAt(i);
6637 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
6638 if (pids != null) {
6639 for (int j=0; j<pids.size(); j++) {
6640 Uid.Pid pid = pids.valueAt(j);
6641 if (!didPid) {
6642 pw.println("Per-PID Stats:");
6643 didPid = true;
6644 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006645 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
6646 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006647 pw.print(" PID "); pw.print(pids.keyAt(j));
6648 pw.print(" wake time: ");
6649 TimeUtils.formatDuration(time, pw);
6650 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006651 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006652 }
6653 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006654 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006655 pw.println();
6656 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006657 }
6658
6659 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006660 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
6661 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006662 long timeRemaining = computeBatteryTimeRemaining(
6663 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006664 if (timeRemaining >= 0) {
6665 pw.print(" Estimated discharge time remaining: ");
6666 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6667 pw.println();
6668 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006669 final LevelStepTracker steps = getDischargeLevelStepTracker();
6670 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6671 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
6672 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6673 STEP_LEVEL_MODE_VALUES[i], null));
6674 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006675 pw.println();
6676 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006677 if (dumpDurationSteps(pw, " ", "Charge step durations:",
6678 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006679 long timeRemaining = computeChargeTimeRemaining(
6680 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006681 if (timeRemaining >= 0) {
6682 pw.print(" Estimated charge time remaining: ");
6683 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6684 pw.println();
6685 }
6686 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006687 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006688 }
Dianne Hackbornc81983a2017-10-20 16:16:32 -07006689 if (!filtering || (flags & DUMP_DAILY_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006690 pw.println("Daily stats:");
6691 pw.print(" Current start time: ");
6692 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6693 getCurrentDailyStartTime()).toString());
6694 pw.print(" Next min deadline: ");
6695 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6696 getNextMinDailyDeadline()).toString());
6697 pw.print(" Next max deadline: ");
6698 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6699 getNextMaxDailyDeadline()).toString());
6700 StringBuilder sb = new StringBuilder(64);
6701 int[] outInt = new int[1];
6702 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
6703 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006704 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
6705 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006706 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006707 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
6708 dsteps, false)) {
6709 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6710 sb, outInt);
6711 }
6712 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6713 csteps, false)) {
6714 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6715 sb, outInt);
6716 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006717 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006718 } else {
6719 pw.println(" Current daily steps:");
6720 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6721 sb, outInt);
6722 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6723 sb, outInt);
6724 }
6725 }
6726 DailyItem dit;
6727 int curIndex = 0;
6728 while ((dit=getDailyItemLocked(curIndex)) != null) {
6729 curIndex++;
6730 if ((flags&DUMP_DAILY_ONLY) != 0) {
6731 pw.println();
6732 }
6733 pw.print(" Daily from ");
6734 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6735 pw.print(" to ");
6736 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6737 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006738 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006739 if (dumpDurationSteps(pw, " ",
6740 " Discharge step durations:", dit.mDischargeSteps, false)) {
6741 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6742 sb, outInt);
6743 }
6744 if (dumpDurationSteps(pw, " ",
6745 " Charge step durations:", dit.mChargeSteps, false)) {
6746 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6747 sb, outInt);
6748 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006749 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006750 } else {
6751 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6752 sb, outInt);
6753 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6754 sb, outInt);
6755 }
6756 }
6757 pw.println();
6758 }
6759 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006760 pw.println("Statistics since last charge:");
6761 pw.println(" System starts: " + getStartCount()
6762 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006763 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6764 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006765 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006766 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006767 }
Mike Mac2f518a2017-09-19 16:06:03 -07006768
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006769 // This is called from BatteryStatsService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006770 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006771 public void dumpCheckinLocked(Context context, PrintWriter pw,
6772 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006773 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006774
6775 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006776 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6777 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006778
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006779 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6780
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006781 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07006782 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006783 try {
6784 for (int i=0; i<getHistoryStringPoolSize(); i++) {
6785 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6786 pw.print(HISTORY_STRING_POOL); pw.print(',');
6787 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006788 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006789 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006790 pw.print(",\"");
6791 String str = getHistoryTagPoolString(i);
6792 str = str.replace("\\", "\\\\");
6793 str = str.replace("\"", "\\\"");
6794 pw.print(str);
6795 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006796 pw.println();
6797 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006798 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006799 } finally {
6800 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08006801 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006802 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006803 }
6804
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006805 if ((flags & DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006806 return;
6807 }
6808
Dianne Hackborne4a59512010-12-07 11:08:07 -08006809 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006810 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08006811 for (int i=0; i<apps.size(); i++) {
6812 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006813 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
6814 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08006815 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006816 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
6817 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006818 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006819 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006820 }
6821 SparseArray<? extends Uid> uidStats = getUidStats();
6822 final int NU = uidStats.size();
6823 String[] lineArgs = new String[2];
6824 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006825 int uid = UserHandle.getAppId(uidStats.keyAt(i));
6826 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
6827 if (pkgs != null && !pkgs.second.value) {
6828 pkgs.second.value = true;
6829 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08006830 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006831 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006832 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
6833 (Object[])lineArgs);
6834 }
6835 }
6836 }
6837 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006838 if ((flags & DUMP_DAILY_ONLY) == 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006839 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006840 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07006841 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006842 if (timeRemaining >= 0) {
6843 lineArgs[0] = Long.toString(timeRemaining);
6844 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
6845 (Object[])lineArgs);
6846 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006847 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07006848 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006849 if (timeRemaining >= 0) {
6850 lineArgs[0] = Long.toString(timeRemaining);
6851 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
6852 (Object[])lineArgs);
6853 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006854 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
6855 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006856 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006857 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006858
Kweku Adams87b19ec2017-10-09 12:40:03 -07006859 /** Dump #STATS_SINCE_CHARGED batterystats data to a proto. @hide */
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006860 public void dumpProtoLocked(Context context, FileDescriptor fd, List<ApplicationInfo> apps,
Kweku Adams6ccebf22017-12-11 12:30:35 -08006861 int flags) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006862 final ProtoOutputStream proto = new ProtoOutputStream(fd);
6863 final long bToken = proto.start(BatteryStatsServiceDumpProto.BATTERYSTATS);
6864 prepareForDumpLocked();
6865
6866 proto.write(BatteryStatsProto.REPORT_VERSION, CHECKIN_VERSION);
6867 proto.write(BatteryStatsProto.PARCEL_VERSION, getParcelVersion());
6868 proto.write(BatteryStatsProto.START_PLATFORM_VERSION, getStartPlatformVersion());
6869 proto.write(BatteryStatsProto.END_PLATFORM_VERSION, getEndPlatformVersion());
6870
Kweku Adams6ccebf22017-12-11 12:30:35 -08006871 // History intentionally not included in proto dump.
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006872
6873 if ((flags & (DUMP_HISTORY_ONLY | DUMP_DAILY_ONLY)) == 0) {
Kweku Adams103351f2017-10-16 14:39:34 -07006874 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false,
6875 (flags & DUMP_DEVICE_WIFI_ONLY) != 0);
6876 helper.create(this);
6877 helper.refreshStats(STATS_SINCE_CHARGED, UserHandle.USER_ALL);
6878
6879 dumpProtoAppsLocked(proto, helper, apps);
6880 dumpProtoSystemLocked(proto, helper);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006881 }
6882
6883 proto.end(bToken);
6884 proto.flush();
6885 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07006886
Kweku Adams103351f2017-10-16 14:39:34 -07006887 private void dumpProtoAppsLocked(ProtoOutputStream proto, BatteryStatsHelper helper,
6888 List<ApplicationInfo> apps) {
6889 final int which = STATS_SINCE_CHARGED;
6890 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
6891 final long rawRealtimeMs = SystemClock.elapsedRealtime();
6892 final long rawRealtimeUs = rawRealtimeMs * 1000;
6893 final long batteryUptimeUs = getBatteryUptime(rawUptimeUs);
6894
6895 SparseArray<ArrayList<String>> aidToPackages = new SparseArray<>();
6896 if (apps != null) {
6897 for (int i = 0; i < apps.size(); ++i) {
6898 ApplicationInfo ai = apps.get(i);
6899 int aid = UserHandle.getAppId(ai.uid);
6900 ArrayList<String> pkgs = aidToPackages.get(aid);
6901 if (pkgs == null) {
6902 pkgs = new ArrayList<String>();
6903 aidToPackages.put(aid, pkgs);
6904 }
6905 pkgs.add(ai.packageName);
6906 }
6907 }
6908
6909 SparseArray<BatterySipper> uidToSipper = new SparseArray<>();
6910 final List<BatterySipper> sippers = helper.getUsageList();
6911 if (sippers != null) {
6912 for (int i = 0; i < sippers.size(); ++i) {
6913 final BatterySipper bs = sippers.get(i);
6914 if (bs.drainType != BatterySipper.DrainType.APP) {
6915 // Others are handled by dumpProtoSystemLocked()
6916 continue;
6917 }
6918 uidToSipper.put(bs.uidObj.getUid(), bs);
6919 }
6920 }
6921
6922 SparseArray<? extends Uid> uidStats = getUidStats();
6923 final int n = uidStats.size();
6924 for (int iu = 0; iu < n; ++iu) {
6925 final long uTkn = proto.start(BatteryStatsProto.UIDS);
6926 final Uid u = uidStats.valueAt(iu);
6927
6928 final int uid = uidStats.keyAt(iu);
6929 proto.write(UidProto.UID, uid);
6930
6931 // Print packages and apk stats (UID_DATA & APK_DATA)
6932 ArrayList<String> pkgs = aidToPackages.get(UserHandle.getAppId(uid));
6933 if (pkgs == null) {
6934 pkgs = new ArrayList<String>();
6935 }
6936 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats =
6937 u.getPackageStats();
6938 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
6939 String pkg = packageStats.keyAt(ipkg);
6940 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats =
6941 packageStats.valueAt(ipkg).getServiceStats();
6942 if (serviceStats.size() == 0) {
6943 // Due to the way ActivityManagerService logs wakeup alarms, some packages (for
6944 // example, "android") may be included in the packageStats that aren't part of
6945 // the UID. If they don't have any services, then they shouldn't be listed here.
6946 // These packages won't be a part in the pkgs List.
6947 continue;
6948 }
6949
6950 final long pToken = proto.start(UidProto.PACKAGES);
6951 proto.write(UidProto.Package.NAME, pkg);
6952 // Remove from the packages list since we're logging it here.
6953 pkgs.remove(pkg);
6954
6955 for (int isvc = serviceStats.size() - 1; isvc >= 0; --isvc) {
6956 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
6957 long sToken = proto.start(UidProto.Package.SERVICES);
6958
6959 proto.write(UidProto.Package.Service.NAME, serviceStats.keyAt(isvc));
6960 proto.write(UidProto.Package.Service.START_DURATION_MS,
6961 roundUsToMs(ss.getStartTime(batteryUptimeUs, which)));
6962 proto.write(UidProto.Package.Service.START_COUNT, ss.getStarts(which));
6963 proto.write(UidProto.Package.Service.LAUNCH_COUNT, ss.getLaunches(which));
6964
6965 proto.end(sToken);
6966 }
6967 proto.end(pToken);
6968 }
6969 // Print any remaining packages that weren't in the packageStats map. pkgs is pulled
6970 // from PackageManager data. Packages are only included in packageStats if there was
6971 // specific data tracked for them (services and wakeup alarms, etc.).
6972 for (String p : pkgs) {
6973 final long pToken = proto.start(UidProto.PACKAGES);
6974 proto.write(UidProto.Package.NAME, p);
6975 proto.end(pToken);
6976 }
6977
6978 // Total wakelock data (AGGREGATED_WAKELOCK_DATA)
6979 if (u.getAggregatedPartialWakelockTimer() != null) {
6980 final Timer timer = u.getAggregatedPartialWakelockTimer();
6981 // Times are since reset (regardless of 'which')
6982 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
6983 final Timer bgTimer = timer.getSubTimer();
6984 final long bgTimeMs = bgTimer != null
6985 ? bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
6986 final long awToken = proto.start(UidProto.AGGREGATED_WAKELOCK);
6987 proto.write(UidProto.AggregatedWakelock.PARTIAL_DURATION_MS, totTimeMs);
6988 proto.write(UidProto.AggregatedWakelock.BACKGROUND_PARTIAL_DURATION_MS, bgTimeMs);
6989 proto.end(awToken);
6990 }
6991
6992 // Audio (AUDIO_DATA)
6993 dumpTimer(proto, UidProto.AUDIO, u.getAudioTurnedOnTimer(), rawRealtimeUs, which);
6994
6995 // Bluetooth Controller (BLUETOOTH_CONTROLLER_DATA)
6996 dumpControllerActivityProto(proto, UidProto.BLUETOOTH_CONTROLLER,
6997 u.getBluetoothControllerActivity(), which);
6998
6999 // BLE scans (BLUETOOTH_MISC_DATA) (uses totalDurationMsLocked and MaxDurationMsLocked)
7000 final Timer bleTimer = u.getBluetoothScanTimer();
7001 if (bleTimer != null) {
7002 final long bmToken = proto.start(UidProto.BLUETOOTH_MISC);
7003
7004 dumpTimer(proto, UidProto.BluetoothMisc.APPORTIONED_BLE_SCAN, bleTimer,
7005 rawRealtimeUs, which);
7006 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN,
7007 u.getBluetoothScanBackgroundTimer(), rawRealtimeUs, which);
7008 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
7009 dumpTimer(proto, UidProto.BluetoothMisc.UNOPTIMIZED_BLE_SCAN,
7010 u.getBluetoothUnoptimizedScanTimer(), rawRealtimeUs, which);
7011 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
7012 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_UNOPTIMIZED_BLE_SCAN,
7013 u.getBluetoothUnoptimizedScanBackgroundTimer(), rawRealtimeUs, which);
7014 // Result counters
7015 proto.write(UidProto.BluetoothMisc.BLE_SCAN_RESULT_COUNT,
7016 u.getBluetoothScanResultCounter() != null
7017 ? u.getBluetoothScanResultCounter().getCountLocked(which) : 0);
7018 proto.write(UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN_RESULT_COUNT,
7019 u.getBluetoothScanResultBgCounter() != null
7020 ? u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0);
7021
7022 proto.end(bmToken);
7023 }
7024
7025 // Camera (CAMERA_DATA)
7026 dumpTimer(proto, UidProto.CAMERA, u.getCameraTurnedOnTimer(), rawRealtimeUs, which);
7027
7028 // CPU stats (CPU_DATA & CPU_TIMES_AT_FREQ_DATA)
7029 final long cpuToken = proto.start(UidProto.CPU);
7030 proto.write(UidProto.Cpu.USER_DURATION_MS, roundUsToMs(u.getUserCpuTimeUs(which)));
7031 proto.write(UidProto.Cpu.SYSTEM_DURATION_MS, roundUsToMs(u.getSystemCpuTimeUs(which)));
7032
7033 final long[] cpuFreqs = getCpuFreqs();
7034 if (cpuFreqs != null) {
7035 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
7036 // If total cpuFreqTimes is null, then we don't need to check for
7037 // screenOffCpuFreqTimes.
7038 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
7039 long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
7040 if (screenOffCpuFreqTimeMs == null) {
7041 screenOffCpuFreqTimeMs = new long[cpuFreqTimeMs.length];
7042 }
7043 for (int ic = 0; ic < cpuFreqTimeMs.length; ++ic) {
7044 long cToken = proto.start(UidProto.Cpu.BY_FREQUENCY);
7045 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7046 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7047 cpuFreqTimeMs[ic]);
7048 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7049 screenOffCpuFreqTimeMs[ic]);
7050 proto.end(cToken);
7051 }
7052 }
7053 }
Sudheer Shanka6d658d72018-01-01 01:36:49 -08007054
7055 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
7056 final long[] timesMs = u.getCpuFreqTimes(which, procState);
7057 if (timesMs != null && timesMs.length == cpuFreqs.length) {
7058 long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(which, procState);
7059 if (screenOffTimesMs == null) {
7060 screenOffTimesMs = new long[timesMs.length];
7061 }
7062 final long procToken = proto.start(UidProto.Cpu.BY_PROCESS_STATE);
7063 proto.write(UidProto.Cpu.ByProcessState.PROCESS_STATE, procState);
7064 for (int ic = 0; ic < timesMs.length; ++ic) {
7065 long cToken = proto.start(UidProto.Cpu.ByProcessState.BY_FREQUENCY);
7066 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7067 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7068 timesMs[ic]);
7069 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7070 screenOffTimesMs[ic]);
7071 proto.end(cToken);
7072 }
7073 proto.end(procToken);
7074 }
7075 }
Kweku Adams103351f2017-10-16 14:39:34 -07007076 proto.end(cpuToken);
7077
7078 // Flashlight (FLASHLIGHT_DATA)
7079 dumpTimer(proto, UidProto.FLASHLIGHT, u.getFlashlightTurnedOnTimer(),
7080 rawRealtimeUs, which);
7081
7082 // Foreground activity (FOREGROUND_ACTIVITY_DATA)
7083 dumpTimer(proto, UidProto.FOREGROUND_ACTIVITY, u.getForegroundActivityTimer(),
7084 rawRealtimeUs, which);
7085
7086 // Foreground service (FOREGROUND_SERVICE_DATA)
7087 dumpTimer(proto, UidProto.FOREGROUND_SERVICE, u.getForegroundServiceTimer(),
7088 rawRealtimeUs, which);
7089
7090 // Job completion (JOB_COMPLETION_DATA)
7091 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
7092 final int[] reasons = new int[]{
7093 JobParameters.REASON_CANCELED,
7094 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED,
7095 JobParameters.REASON_PREEMPT,
7096 JobParameters.REASON_TIMEOUT,
7097 JobParameters.REASON_DEVICE_IDLE,
7098 };
7099 for (int ic = 0; ic < completions.size(); ++ic) {
7100 SparseIntArray types = completions.valueAt(ic);
7101 if (types != null) {
7102 final long jcToken = proto.start(UidProto.JOB_COMPLETION);
7103
7104 proto.write(UidProto.JobCompletion.NAME, completions.keyAt(ic));
7105
7106 for (int r : reasons) {
7107 long rToken = proto.start(UidProto.JobCompletion.REASON_COUNT);
7108 proto.write(UidProto.JobCompletion.ReasonCount.NAME, r);
7109 proto.write(UidProto.JobCompletion.ReasonCount.COUNT, types.get(r, 0));
7110 proto.end(rToken);
7111 }
7112
7113 proto.end(jcToken);
7114 }
7115 }
7116
7117 // Scheduled jobs (JOB_DATA)
7118 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
7119 for (int ij = jobs.size() - 1; ij >= 0; --ij) {
7120 final Timer timer = jobs.valueAt(ij);
7121 final Timer bgTimer = timer.getSubTimer();
7122 final long jToken = proto.start(UidProto.JOBS);
7123
7124 proto.write(UidProto.Job.NAME, jobs.keyAt(ij));
7125 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7126 dumpTimer(proto, UidProto.Job.TOTAL, timer, rawRealtimeUs, which);
7127 dumpTimer(proto, UidProto.Job.BACKGROUND, bgTimer, rawRealtimeUs, which);
7128
7129 proto.end(jToken);
7130 }
7131
7132 // Modem Controller (MODEM_CONTROLLER_DATA)
7133 dumpControllerActivityProto(proto, UidProto.MODEM_CONTROLLER,
7134 u.getModemControllerActivity(), which);
7135
7136 // Network stats (NETWORK_DATA)
7137 final long nToken = proto.start(UidProto.NETWORK);
7138 proto.write(UidProto.Network.MOBILE_BYTES_RX,
7139 u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7140 proto.write(UidProto.Network.MOBILE_BYTES_TX,
7141 u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7142 proto.write(UidProto.Network.WIFI_BYTES_RX,
7143 u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7144 proto.write(UidProto.Network.WIFI_BYTES_TX,
7145 u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7146 proto.write(UidProto.Network.BT_BYTES_RX,
7147 u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7148 proto.write(UidProto.Network.BT_BYTES_TX,
7149 u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
7150 proto.write(UidProto.Network.MOBILE_PACKETS_RX,
7151 u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7152 proto.write(UidProto.Network.MOBILE_PACKETS_TX,
7153 u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7154 proto.write(UidProto.Network.WIFI_PACKETS_RX,
7155 u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7156 proto.write(UidProto.Network.WIFI_PACKETS_TX,
7157 u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7158 proto.write(UidProto.Network.MOBILE_ACTIVE_DURATION_MS,
7159 roundUsToMs(u.getMobileRadioActiveTime(which)));
7160 proto.write(UidProto.Network.MOBILE_ACTIVE_COUNT,
7161 u.getMobileRadioActiveCount(which));
7162 proto.write(UidProto.Network.MOBILE_WAKEUP_COUNT,
7163 u.getMobileRadioApWakeupCount(which));
7164 proto.write(UidProto.Network.WIFI_WAKEUP_COUNT,
7165 u.getWifiRadioApWakeupCount(which));
7166 proto.write(UidProto.Network.MOBILE_BYTES_BG_RX,
7167 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA, which));
7168 proto.write(UidProto.Network.MOBILE_BYTES_BG_TX,
7169 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA, which));
7170 proto.write(UidProto.Network.WIFI_BYTES_BG_RX,
7171 u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which));
7172 proto.write(UidProto.Network.WIFI_BYTES_BG_TX,
7173 u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which));
7174 proto.write(UidProto.Network.MOBILE_PACKETS_BG_RX,
7175 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA, which));
7176 proto.write(UidProto.Network.MOBILE_PACKETS_BG_TX,
7177 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA, which));
7178 proto.write(UidProto.Network.WIFI_PACKETS_BG_RX,
7179 u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA, which));
7180 proto.write(UidProto.Network.WIFI_PACKETS_BG_TX,
7181 u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA, which));
7182 proto.end(nToken);
7183
7184 // Power use item (POWER_USE_ITEM_DATA)
7185 BatterySipper bs = uidToSipper.get(uid);
7186 if (bs != null) {
7187 final long bsToken = proto.start(UidProto.POWER_USE_ITEM);
7188 proto.write(UidProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7189 proto.write(UidProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7190 proto.write(UidProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7191 proto.write(UidProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7192 bs.proportionalSmearMah);
7193 proto.end(bsToken);
7194 }
7195
7196 // Processes (PROCESS_DATA)
7197 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats =
7198 u.getProcessStats();
7199 for (int ipr = processStats.size() - 1; ipr >= 0; --ipr) {
7200 final Uid.Proc ps = processStats.valueAt(ipr);
7201 final long prToken = proto.start(UidProto.PROCESS);
7202
7203 proto.write(UidProto.Process.NAME, processStats.keyAt(ipr));
7204 proto.write(UidProto.Process.USER_DURATION_MS, ps.getUserTime(which));
7205 proto.write(UidProto.Process.SYSTEM_DURATION_MS, ps.getSystemTime(which));
7206 proto.write(UidProto.Process.FOREGROUND_DURATION_MS, ps.getForegroundTime(which));
7207 proto.write(UidProto.Process.START_COUNT, ps.getStarts(which));
7208 proto.write(UidProto.Process.ANR_COUNT, ps.getNumAnrs(which));
7209 proto.write(UidProto.Process.CRASH_COUNT, ps.getNumCrashes(which));
7210
7211 proto.end(prToken);
7212 }
7213
7214 // Sensors (SENSOR_DATA)
7215 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
7216 for (int ise = 0; ise < sensors.size(); ++ise) {
7217 final Uid.Sensor se = sensors.valueAt(ise);
7218 final Timer timer = se.getSensorTime();
7219 if (timer == null) {
7220 continue;
7221 }
7222 final Timer bgTimer = se.getSensorBackgroundTime();
7223 final int sensorNumber = sensors.keyAt(ise);
7224 final long seToken = proto.start(UidProto.SENSORS);
7225
7226 proto.write(UidProto.Sensor.ID, sensorNumber);
7227 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7228 dumpTimer(proto, UidProto.Sensor.APPORTIONED, timer, rawRealtimeUs, which);
7229 dumpTimer(proto, UidProto.Sensor.BACKGROUND, bgTimer, rawRealtimeUs, which);
7230
7231 proto.end(seToken);
7232 }
7233
7234 // State times (STATE_TIME_DATA)
7235 for (int ips = 0; ips < Uid.NUM_PROCESS_STATE; ++ips) {
7236 long durMs = roundUsToMs(u.getProcessStateTime(ips, rawRealtimeUs, which));
7237 if (durMs == 0) {
7238 continue;
7239 }
7240 final long stToken = proto.start(UidProto.STATES);
7241 proto.write(UidProto.StateTime.STATE, ips);
7242 proto.write(UidProto.StateTime.DURATION_MS, durMs);
7243 proto.end(stToken);
7244 }
7245
7246 // Syncs (SYNC_DATA)
7247 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
7248 for (int isy = syncs.size() - 1; isy >= 0; --isy) {
7249 final Timer timer = syncs.valueAt(isy);
7250 final Timer bgTimer = timer.getSubTimer();
7251 final long syToken = proto.start(UidProto.SYNCS);
7252
7253 proto.write(UidProto.Sync.NAME, syncs.keyAt(isy));
7254 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7255 dumpTimer(proto, UidProto.Sync.TOTAL, timer, rawRealtimeUs, which);
7256 dumpTimer(proto, UidProto.Sync.BACKGROUND, bgTimer, rawRealtimeUs, which);
7257
7258 proto.end(syToken);
7259 }
7260
7261 // User activity (USER_ACTIVITY_DATA)
7262 if (u.hasUserActivity()) {
7263 for (int i = 0; i < Uid.NUM_USER_ACTIVITY_TYPES; ++i) {
7264 int val = u.getUserActivityCount(i, which);
7265 if (val != 0) {
7266 final long uaToken = proto.start(UidProto.USER_ACTIVITY);
7267 proto.write(UidProto.UserActivity.NAME, i);
7268 proto.write(UidProto.UserActivity.COUNT, val);
7269 proto.end(uaToken);
7270 }
7271 }
7272 }
7273
7274 // Vibrator (VIBRATOR_DATA)
7275 dumpTimer(proto, UidProto.VIBRATOR, u.getVibratorOnTimer(), rawRealtimeUs, which);
7276
7277 // Video (VIDEO_DATA)
7278 dumpTimer(proto, UidProto.VIDEO, u.getVideoTurnedOnTimer(), rawRealtimeUs, which);
7279
7280 // Wakelocks (WAKELOCK_DATA)
7281 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
7282 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7283 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7284 final long wToken = proto.start(UidProto.WAKELOCKS);
7285 proto.write(UidProto.Wakelock.NAME, wakelocks.keyAt(iw));
7286 dumpTimer(proto, UidProto.Wakelock.FULL, wl.getWakeTime(WAKE_TYPE_FULL),
7287 rawRealtimeUs, which);
7288 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7289 if (pTimer != null) {
7290 dumpTimer(proto, UidProto.Wakelock.PARTIAL, pTimer, rawRealtimeUs, which);
7291 dumpTimer(proto, UidProto.Wakelock.BACKGROUND_PARTIAL, pTimer.getSubTimer(),
7292 rawRealtimeUs, which);
7293 }
7294 dumpTimer(proto, UidProto.Wakelock.WINDOW, wl.getWakeTime(WAKE_TYPE_WINDOW),
7295 rawRealtimeUs, which);
7296 proto.end(wToken);
7297 }
7298
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007299 // Wifi Multicast Wakelock (WIFI_MULTICAST_WAKELOCK_DATA)
7300 dumpTimer(proto, UidProto.WIFI_MULTICAST_WAKELOCK, u.getMulticastWakelockStats(),
7301 rawRealtimeUs, which);
7302
Kweku Adams103351f2017-10-16 14:39:34 -07007303 // Wakeup alarms (WAKEUP_ALARM_DATA)
7304 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7305 final Uid.Pkg ps = packageStats.valueAt(ipkg);
7306 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
7307 for (int iwa = alarms.size() - 1; iwa >= 0; --iwa) {
7308 final long waToken = proto.start(UidProto.WAKEUP_ALARM);
7309 proto.write(UidProto.WakeupAlarm.NAME, alarms.keyAt(iwa));
7310 proto.write(UidProto.WakeupAlarm.COUNT,
7311 alarms.valueAt(iwa).getCountLocked(which));
7312 proto.end(waToken);
7313 }
7314 }
7315
7316 // Wifi Controller (WIFI_CONTROLLER_DATA)
7317 dumpControllerActivityProto(proto, UidProto.WIFI_CONTROLLER,
7318 u.getWifiControllerActivity(), which);
7319
7320 // Wifi data (WIFI_DATA)
7321 final long wToken = proto.start(UidProto.WIFI);
7322 proto.write(UidProto.Wifi.FULL_WIFI_LOCK_DURATION_MS,
7323 roundUsToMs(u.getFullWifiLockTime(rawRealtimeUs, which)));
7324 dumpTimer(proto, UidProto.Wifi.APPORTIONED_SCAN, u.getWifiScanTimer(),
7325 rawRealtimeUs, which);
7326 proto.write(UidProto.Wifi.RUNNING_DURATION_MS,
7327 roundUsToMs(u.getWifiRunningTime(rawRealtimeUs, which)));
7328 dumpTimer(proto, UidProto.Wifi.BACKGROUND_SCAN, u.getWifiScanBackgroundTimer(),
7329 rawRealtimeUs, which);
7330 proto.end(wToken);
7331
7332 proto.end(uTkn);
7333 }
7334 }
7335
7336 private void dumpProtoSystemLocked(ProtoOutputStream proto, BatteryStatsHelper helper) {
Kweku Adams87b19ec2017-10-09 12:40:03 -07007337 final long sToken = proto.start(BatteryStatsProto.SYSTEM);
7338 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7339 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7340 final long rawRealtimeUs = rawRealtimeMs * 1000;
7341 final int which = STATS_SINCE_CHARGED;
7342
7343 // Battery data (BATTERY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007344 final long bToken = proto.start(SystemProto.BATTERY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007345 proto.write(SystemProto.Battery.START_CLOCK_TIME_MS, getStartClockTime());
7346 proto.write(SystemProto.Battery.START_COUNT, getStartCount());
7347 proto.write(SystemProto.Battery.TOTAL_REALTIME_MS,
7348 computeRealtime(rawRealtimeUs, which) / 1000);
7349 proto.write(SystemProto.Battery.TOTAL_UPTIME_MS,
7350 computeUptime(rawUptimeUs, which) / 1000);
7351 proto.write(SystemProto.Battery.BATTERY_REALTIME_MS,
7352 computeBatteryRealtime(rawRealtimeUs, which) / 1000);
7353 proto.write(SystemProto.Battery.BATTERY_UPTIME_MS,
7354 computeBatteryUptime(rawUptimeUs, which) / 1000);
7355 proto.write(SystemProto.Battery.SCREEN_OFF_REALTIME_MS,
7356 computeBatteryScreenOffRealtime(rawRealtimeUs, which) / 1000);
7357 proto.write(SystemProto.Battery.SCREEN_OFF_UPTIME_MS,
7358 computeBatteryScreenOffUptime(rawUptimeUs, which) / 1000);
7359 proto.write(SystemProto.Battery.SCREEN_DOZE_DURATION_MS,
7360 getScreenDozeTime(rawRealtimeUs, which) / 1000);
7361 proto.write(SystemProto.Battery.ESTIMATED_BATTERY_CAPACITY_MAH,
7362 getEstimatedBatteryCapacity());
7363 proto.write(SystemProto.Battery.MIN_LEARNED_BATTERY_CAPACITY_UAH,
7364 getMinLearnedBatteryCapacity());
7365 proto.write(SystemProto.Battery.MAX_LEARNED_BATTERY_CAPACITY_UAH,
7366 getMaxLearnedBatteryCapacity());
Kweku Adams103351f2017-10-16 14:39:34 -07007367 proto.end(bToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007368
7369 // Battery discharge (BATTERY_DISCHARGE_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007370 final long bdToken = proto.start(SystemProto.BATTERY_DISCHARGE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007371 proto.write(SystemProto.BatteryDischarge.LOWER_BOUND_SINCE_CHARGE,
7372 getLowDischargeAmountSinceCharge());
7373 proto.write(SystemProto.BatteryDischarge.UPPER_BOUND_SINCE_CHARGE,
7374 getHighDischargeAmountSinceCharge());
7375 proto.write(SystemProto.BatteryDischarge.SCREEN_ON_SINCE_CHARGE,
7376 getDischargeAmountScreenOnSinceCharge());
7377 proto.write(SystemProto.BatteryDischarge.SCREEN_OFF_SINCE_CHARGE,
7378 getDischargeAmountScreenOffSinceCharge());
7379 proto.write(SystemProto.BatteryDischarge.SCREEN_DOZE_SINCE_CHARGE,
7380 getDischargeAmountScreenDozeSinceCharge());
7381 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH,
7382 getUahDischarge(which) / 1000);
7383 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_OFF,
7384 getUahDischargeScreenOff(which) / 1000);
7385 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_DOZE,
7386 getUahDischargeScreenDoze(which) / 1000);
Mike Ma15313c92017-11-15 17:58:21 -08007387 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_LIGHT_DOZE,
7388 getUahDischargeLightDoze(which) / 1000);
7389 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_DEEP_DOZE,
7390 getUahDischargeDeepDoze(which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007391 proto.end(bdToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007392
7393 // Time remaining
7394 long timeRemainingUs = computeChargeTimeRemaining(rawRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -07007395 // These are part of a oneof, so we should only set one of them.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007396 if (timeRemainingUs >= 0) {
7397 // Charge time remaining (CHARGE_TIME_REMAIN_DATA)
7398 proto.write(SystemProto.CHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7399 } else {
7400 timeRemainingUs = computeBatteryTimeRemaining(rawRealtimeUs);
7401 // Discharge time remaining (DISCHARGE_TIME_REMAIN_DATA)
7402 if (timeRemainingUs >= 0) {
7403 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7404 } else {
7405 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, -1);
7406 }
7407 }
7408
7409 // Charge step (CHARGE_STEP_DATA)
7410 dumpDurationSteps(proto, SystemProto.CHARGE_STEP, getChargeLevelStepTracker());
7411
7412 // Phone data connection (DATA_CONNECTION_TIME_DATA and DATA_CONNECTION_COUNT_DATA)
7413 for (int i = 0; i < NUM_DATA_CONNECTION_TYPES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007414 final long pdcToken = proto.start(SystemProto.DATA_CONNECTION);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007415 proto.write(SystemProto.DataConnection.NAME, i);
7416 dumpTimer(proto, SystemProto.DataConnection.TOTAL, getPhoneDataConnectionTimer(i),
7417 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007418 proto.end(pdcToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007419 }
7420
7421 // Discharge step (DISCHARGE_STEP_DATA)
7422 dumpDurationSteps(proto, SystemProto.DISCHARGE_STEP, getDischargeLevelStepTracker());
7423
7424 // CPU frequencies (GLOBAL_CPU_FREQ_DATA)
7425 final long[] cpuFreqs = getCpuFreqs();
7426 if (cpuFreqs != null) {
7427 for (long i : cpuFreqs) {
7428 proto.write(SystemProto.CPU_FREQUENCY, i);
7429 }
7430 }
7431
7432 // Bluetooth controller (GLOBAL_BLUETOOTH_CONTROLLER_DATA)
7433 dumpControllerActivityProto(proto, SystemProto.GLOBAL_BLUETOOTH_CONTROLLER,
7434 getBluetoothControllerActivity(), which);
7435
7436 // Modem controller (GLOBAL_MODEM_CONTROLLER_DATA)
7437 dumpControllerActivityProto(proto, SystemProto.GLOBAL_MODEM_CONTROLLER,
7438 getModemControllerActivity(), which);
7439
7440 // Global network data (GLOBAL_NETWORK_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007441 final long gnToken = proto.start(SystemProto.GLOBAL_NETWORK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007442 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_RX,
7443 getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7444 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_TX,
7445 getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7446 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_RX,
7447 getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7448 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_TX,
7449 getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7450 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_RX,
7451 getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7452 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_TX,
7453 getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7454 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_RX,
7455 getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7456 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_TX,
7457 getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7458 proto.write(SystemProto.GlobalNetwork.BT_BYTES_RX,
7459 getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7460 proto.write(SystemProto.GlobalNetwork.BT_BYTES_TX,
7461 getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
Kweku Adams103351f2017-10-16 14:39:34 -07007462 proto.end(gnToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007463
7464 // Wifi controller (GLOBAL_WIFI_CONTROLLER_DATA)
7465 dumpControllerActivityProto(proto, SystemProto.GLOBAL_WIFI_CONTROLLER,
7466 getWifiControllerActivity(), which);
7467
7468
7469 // Global wifi (GLOBAL_WIFI_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007470 final long gwToken = proto.start(SystemProto.GLOBAL_WIFI);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007471 proto.write(SystemProto.GlobalWifi.ON_DURATION_MS,
7472 getWifiOnTime(rawRealtimeUs, which) / 1000);
7473 proto.write(SystemProto.GlobalWifi.RUNNING_DURATION_MS,
7474 getGlobalWifiRunningTime(rawRealtimeUs, which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007475 proto.end(gwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007476
7477 // Kernel wakelock (KERNEL_WAKELOCK_DATA)
7478 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
7479 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007480 final long kwToken = proto.start(SystemProto.KERNEL_WAKELOCK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007481 proto.write(SystemProto.KernelWakelock.NAME, ent.getKey());
7482 dumpTimer(proto, SystemProto.KernelWakelock.TOTAL, ent.getValue(),
7483 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007484 proto.end(kwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007485 }
7486
7487 // Misc (MISC_DATA)
7488 // Calculate wakelock times across all uids.
7489 long fullWakeLockTimeTotalUs = 0;
7490 long partialWakeLockTimeTotalUs = 0;
7491
7492 final SparseArray<? extends Uid> uidStats = getUidStats();
7493 for (int iu = 0; iu < uidStats.size(); iu++) {
7494 final Uid u = uidStats.valueAt(iu);
7495
7496 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks =
7497 u.getWakelockStats();
7498 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7499 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7500
7501 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
7502 if (fullWakeTimer != null) {
7503 fullWakeLockTimeTotalUs += fullWakeTimer.getTotalTimeLocked(rawRealtimeUs,
7504 which);
7505 }
7506
7507 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7508 if (partialWakeTimer != null) {
7509 partialWakeLockTimeTotalUs += partialWakeTimer.getTotalTimeLocked(
7510 rawRealtimeUs, which);
7511 }
7512 }
7513 }
Kweku Adams103351f2017-10-16 14:39:34 -07007514 final long mToken = proto.start(SystemProto.MISC);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007515 proto.write(SystemProto.Misc.SCREEN_ON_DURATION_MS,
7516 getScreenOnTime(rawRealtimeUs, which) / 1000);
7517 proto.write(SystemProto.Misc.PHONE_ON_DURATION_MS,
7518 getPhoneOnTime(rawRealtimeUs, which) / 1000);
7519 proto.write(SystemProto.Misc.FULL_WAKELOCK_TOTAL_DURATION_MS,
7520 fullWakeLockTimeTotalUs / 1000);
7521 proto.write(SystemProto.Misc.PARTIAL_WAKELOCK_TOTAL_DURATION_MS,
7522 partialWakeLockTimeTotalUs / 1000);
7523 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_DURATION_MS,
7524 getMobileRadioActiveTime(rawRealtimeUs, which) / 1000);
7525 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_ADJUSTED_TIME_MS,
7526 getMobileRadioActiveAdjustedTime(which) / 1000);
7527 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_COUNT,
7528 getMobileRadioActiveCount(which));
7529 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_UNKNOWN_DURATION_MS,
7530 getMobileRadioActiveUnknownTime(which) / 1000);
7531 proto.write(SystemProto.Misc.INTERACTIVE_DURATION_MS,
7532 getInteractiveTime(rawRealtimeUs, which) / 1000);
7533 proto.write(SystemProto.Misc.BATTERY_SAVER_MODE_ENABLED_DURATION_MS,
7534 getPowerSaveModeEnabledTime(rawRealtimeUs, which) / 1000);
7535 proto.write(SystemProto.Misc.NUM_CONNECTIVITY_CHANGES,
7536 getNumConnectivityChange(which));
7537 proto.write(SystemProto.Misc.DEEP_DOZE_ENABLED_DURATION_MS,
7538 getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7539 proto.write(SystemProto.Misc.DEEP_DOZE_COUNT,
7540 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
7541 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_DURATION_MS,
7542 getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7543 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_COUNT,
7544 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
7545 proto.write(SystemProto.Misc.LONGEST_DEEP_DOZE_DURATION_MS,
7546 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
7547 proto.write(SystemProto.Misc.LIGHT_DOZE_ENABLED_DURATION_MS,
7548 getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7549 proto.write(SystemProto.Misc.LIGHT_DOZE_COUNT,
7550 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
7551 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_DURATION_MS,
7552 getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7553 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_COUNT,
7554 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
7555 proto.write(SystemProto.Misc.LONGEST_LIGHT_DOZE_DURATION_MS,
7556 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
Kweku Adams103351f2017-10-16 14:39:34 -07007557 proto.end(mToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007558
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007559 // Wifi multicast wakelock total stats (WIFI_MULTICAST_WAKELOCK_TOTAL_DATA)
7560 // Calculate multicast wakelock stats across all uids.
7561 long multicastWakeLockTimeTotalUs = 0;
7562 int multicastWakeLockCountTotal = 0;
7563
7564 for (int iu = 0; iu < uidStats.size(); iu++) {
7565 final Uid u = uidStats.valueAt(iu);
7566
7567 final Timer mcTimer = u.getMulticastWakelockStats();
7568
7569 if (mcTimer != null) {
7570 multicastWakeLockTimeTotalUs +=
7571 mcTimer.getTotalTimeLocked(rawRealtimeUs, which);
7572 multicastWakeLockCountTotal += mcTimer.getCountLocked(which);
7573 }
7574 }
7575
7576 final long wmctToken = proto.start(SystemProto.WIFI_MULTICAST_WAKELOCK_TOTAL);
7577 proto.write(SystemProto.WifiMulticastWakelockTotal.DURATION_MS,
7578 multicastWakeLockTimeTotalUs / 1000);
7579 proto.write(SystemProto.WifiMulticastWakelockTotal.COUNT,
7580 multicastWakeLockCountTotal);
7581 proto.end(wmctToken);
7582
Kweku Adams87b19ec2017-10-09 12:40:03 -07007583 // Power use item (POWER_USE_ITEM_DATA)
7584 final List<BatterySipper> sippers = helper.getUsageList();
7585 if (sippers != null) {
7586 for (int i = 0; i < sippers.size(); ++i) {
7587 final BatterySipper bs = sippers.get(i);
7588 int n = SystemProto.PowerUseItem.UNKNOWN_SIPPER;
7589 int uid = 0;
7590 switch (bs.drainType) {
7591 case IDLE:
7592 n = SystemProto.PowerUseItem.IDLE;
7593 break;
7594 case CELL:
7595 n = SystemProto.PowerUseItem.CELL;
7596 break;
7597 case PHONE:
7598 n = SystemProto.PowerUseItem.PHONE;
7599 break;
7600 case WIFI:
7601 n = SystemProto.PowerUseItem.WIFI;
7602 break;
7603 case BLUETOOTH:
7604 n = SystemProto.PowerUseItem.BLUETOOTH;
7605 break;
7606 case SCREEN:
7607 n = SystemProto.PowerUseItem.SCREEN;
7608 break;
7609 case FLASHLIGHT:
7610 n = SystemProto.PowerUseItem.FLASHLIGHT;
7611 break;
7612 case APP:
Kweku Adams103351f2017-10-16 14:39:34 -07007613 // dumpProtoAppsLocked will handle this.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007614 continue;
7615 case USER:
7616 n = SystemProto.PowerUseItem.USER;
7617 uid = UserHandle.getUid(bs.userId, 0);
7618 break;
7619 case UNACCOUNTED:
7620 n = SystemProto.PowerUseItem.UNACCOUNTED;
7621 break;
7622 case OVERCOUNTED:
7623 n = SystemProto.PowerUseItem.OVERCOUNTED;
7624 break;
7625 case CAMERA:
7626 n = SystemProto.PowerUseItem.CAMERA;
7627 break;
7628 case MEMORY:
7629 n = SystemProto.PowerUseItem.MEMORY;
7630 break;
7631 }
Kweku Adams103351f2017-10-16 14:39:34 -07007632 final long puiToken = proto.start(SystemProto.POWER_USE_ITEM);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007633 proto.write(SystemProto.PowerUseItem.NAME, n);
7634 proto.write(SystemProto.PowerUseItem.UID, uid);
7635 proto.write(SystemProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7636 proto.write(SystemProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7637 proto.write(SystemProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7638 proto.write(SystemProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7639 bs.proportionalSmearMah);
Kweku Adams103351f2017-10-16 14:39:34 -07007640 proto.end(puiToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007641 }
7642 }
7643
7644 // Power use summary (POWER_USE_SUMMARY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007645 final long pusToken = proto.start(SystemProto.POWER_USE_SUMMARY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007646 proto.write(SystemProto.PowerUseSummary.BATTERY_CAPACITY_MAH,
7647 helper.getPowerProfile().getBatteryCapacity());
7648 proto.write(SystemProto.PowerUseSummary.COMPUTED_POWER_MAH, helper.getComputedPower());
7649 proto.write(SystemProto.PowerUseSummary.MIN_DRAINED_POWER_MAH, helper.getMinDrainedPower());
7650 proto.write(SystemProto.PowerUseSummary.MAX_DRAINED_POWER_MAH, helper.getMaxDrainedPower());
Kweku Adams103351f2017-10-16 14:39:34 -07007651 proto.end(pusToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007652
7653 // RPM stats (RESOURCE_POWER_MANAGER_DATA)
7654 final Map<String, ? extends Timer> rpmStats = getRpmStats();
7655 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
7656 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007657 final long rpmToken = proto.start(SystemProto.RESOURCE_POWER_MANAGER);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007658 proto.write(SystemProto.ResourcePowerManager.NAME, ent.getKey());
7659 dumpTimer(proto, SystemProto.ResourcePowerManager.TOTAL,
7660 ent.getValue(), rawRealtimeUs, which);
7661 dumpTimer(proto, SystemProto.ResourcePowerManager.SCREEN_OFF,
7662 screenOffRpmStats.get(ent.getKey()), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007663 proto.end(rpmToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007664 }
7665
7666 // Screen brightness (SCREEN_BRIGHTNESS_DATA)
7667 for (int i = 0; i < NUM_SCREEN_BRIGHTNESS_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007668 final long sbToken = proto.start(SystemProto.SCREEN_BRIGHTNESS);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007669 proto.write(SystemProto.ScreenBrightness.NAME, i);
7670 dumpTimer(proto, SystemProto.ScreenBrightness.TOTAL, getScreenBrightnessTimer(i),
7671 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007672 proto.end(sbToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007673 }
7674
7675 // Signal scanning time (SIGNAL_SCANNING_TIME_DATA)
7676 dumpTimer(proto, SystemProto.SIGNAL_SCANNING, getPhoneSignalScanningTimer(), rawRealtimeUs,
7677 which);
7678
7679 // Phone signal strength (SIGNAL_STRENGTH_TIME_DATA and SIGNAL_STRENGTH_COUNT_DATA)
7680 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007681 final long pssToken = proto.start(SystemProto.PHONE_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007682 proto.write(SystemProto.PhoneSignalStrength.NAME, i);
7683 dumpTimer(proto, SystemProto.PhoneSignalStrength.TOTAL, getPhoneSignalStrengthTimer(i),
7684 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007685 proto.end(pssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007686 }
7687
7688 // Wakeup reasons (WAKEUP_REASON_DATA)
7689 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
7690 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007691 final long wrToken = proto.start(SystemProto.WAKEUP_REASON);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007692 proto.write(SystemProto.WakeupReason.NAME, ent.getKey());
7693 dumpTimer(proto, SystemProto.WakeupReason.TOTAL, ent.getValue(), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007694 proto.end(wrToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007695 }
7696
7697 // Wifi signal strength (WIFI_SIGNAL_STRENGTH_TIME_DATA and WIFI_SIGNAL_STRENGTH_COUNT_DATA)
7698 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007699 final long wssToken = proto.start(SystemProto.WIFI_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007700 proto.write(SystemProto.WifiSignalStrength.NAME, i);
7701 dumpTimer(proto, SystemProto.WifiSignalStrength.TOTAL, getWifiSignalStrengthTimer(i),
7702 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007703 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007704 }
7705
7706 // Wifi state (WIFI_STATE_TIME_DATA and WIFI_STATE_COUNT_DATA)
7707 for (int i = 0; i < NUM_WIFI_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007708 final long wsToken = proto.start(SystemProto.WIFI_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007709 proto.write(SystemProto.WifiState.NAME, i);
7710 dumpTimer(proto, SystemProto.WifiState.TOTAL, getWifiStateTimer(i),
7711 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007712 proto.end(wsToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007713 }
7714
7715 // Wifi supplicant state (WIFI_SUPPL_STATE_TIME_DATA and WIFI_SUPPL_STATE_COUNT_DATA)
7716 for (int i = 0; i < NUM_WIFI_SUPPL_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007717 final long wssToken = proto.start(SystemProto.WIFI_SUPPLICANT_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007718 proto.write(SystemProto.WifiSupplicantState.NAME, i);
7719 dumpTimer(proto, SystemProto.WifiSupplicantState.TOTAL, getWifiSupplStateTimer(i),
7720 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007721 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007722 }
7723
7724 proto.end(sToken);
7725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007726}