blob: 77e4808bf5c3ebc857d90764306b3efe651f0c28 [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 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -0800183 * A constant indicating an aggregate wifi multicast timer
184 */
185 public static final int WIFI_AGGREGATE_MULTICAST_ENABLED = 23;
186
187 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 * Include all of the data in the stats, including previously saved data.
189 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700190 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191
192 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * Include only the current run in the stats.
194 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700195 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196
197 /**
198 * Include only the run since the last time the device was unplugged in the stats.
199 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700200 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700201
202 // NOTE: Update this list if you add/change any stats above.
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700203 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700204 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700205 private static final String[] STAT_NAMES = { "l", "c", "u" };
206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700208 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700209 *
210 * New in version 19:
211 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800212 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700213 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Bookatz506a8182017-05-01 14:18:42 -0700214 * New in version 21:
215 * - Actual (not just apportioned) Wakelock time is also recorded.
Bookatzc8c44962017-05-11 12:12:54 -0700216 * - Aggregated partial wakelock time (per uid, instead of per wakelock) is recorded.
Bookatzb1f04f32017-05-19 13:57:32 -0700217 * - BLE scan result count
218 * - CPU frequency time per uid
219 * New in version 22:
220 * - BLE scan result background count, BLE unoptimized scan time
Bookatz98d4d5c2017-08-01 19:07:54 -0700221 * - Background partial wakelock time & count
222 * New in version 23:
223 * - Logging smeared power model values
224 * New in version 24:
225 * - Fixed bugs in background timers and BLE scan time
226 * New in version 25:
227 * - Package wakeup alarms are now on screen-off timebase
Bookatz50df7112017-08-04 14:53:26 -0700228 * New in version 26:
Bookatz82b341172017-09-07 19:06:08 -0700229 * - Resource power manager (rpm) states [but screenOffRpm is disabled from working properly]
Mike Mac2f518a2017-09-19 16:06:03 -0700230 * New in version 27:
231 * - Always On Display (screen doze mode) time and power
Mike Ma15313c92017-11-15 17:58:21 -0800232 * New in version 28:
233 * - Light/Deep Doze power
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700234 * - WiFi Multicast Wakelock statistics (count & duration)
Kweku Adamsa8943cb2017-12-22 13:21:06 -0800235 * New in version 29:
236 * - Process states re-ordered. TOP_SLEEPING now below BACKGROUND. HEAVY_WEIGHT introduced.
237 * - CPU times per UID process state
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700238 */
Kweku Adamsa8943cb2017-12-22 13:21:06 -0800239 static final int CHECKIN_VERSION = 29;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700240
241 /**
242 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700244 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700245
Evan Millar22ac0432009-03-31 11:33:18 -0700246 private static final long BYTES_PER_KB = 1024;
247 private static final long BYTES_PER_MB = 1048576; // 1024^2
248 private static final long BYTES_PER_GB = 1073741824; //1024^3
Bookatz506a8182017-05-01 14:18:42 -0700249
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700250 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800251 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700252 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700254 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700255 private static final String CPU_DATA = "cpu";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700256 private static final String GLOBAL_CPU_FREQ_DATA = "gcf";
257 private static final String CPU_TIMES_AT_FREQ_DATA = "ctf";
Bookatz50df7112017-08-04 14:53:26 -0700258 // rpm line is:
259 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "rpm", state/voter name, total time, total count,
260 // screen-off time, screen-off count
261 private static final String RESOURCE_POWER_MANAGER_DATA = "rpm";
Evan Millare84de8d2009-04-02 22:16:12 -0700262 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800263 private static final String VIBRATOR_DATA = "vib";
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700264 private static final String FOREGROUND_ACTIVITY_DATA = "fg";
265 // fgs line is:
266 // BATTERY_STATS_CHECKIN_VERSION, uid, category, "fgs",
267 // foreground service time, count
268 private static final String FOREGROUND_SERVICE_DATA = "fgs";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700269 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700270 // wl line is:
271 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
Bookatz5b5ec322017-05-26 09:40:38 -0700272 // full totalTime, 'f', count, current duration, max duration, total duration,
273 // partial totalTime, 'p', count, current duration, max duration, total duration,
274 // bg partial totalTime, 'bp', count, current duration, max duration, total duration,
275 // window totalTime, 'w', count, current duration, max duration, total duration
Bookatz506a8182017-05-01 14:18:42 -0700276 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700277 private static final String WAKELOCK_DATA = "wl";
Bookatzc8c44962017-05-11 12:12:54 -0700278 // awl line is:
279 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "awl",
280 // cumulative partial wakelock duration, cumulative background partial wakelock duration
281 private static final String AGGREGATED_WAKELOCK_DATA = "awl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700282 private static final String SYNC_DATA = "sy";
283 private static final String JOB_DATA = "jb";
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700284 private static final String JOB_COMPLETION_DATA = "jbc";
Evan Millarc64edde2009-04-18 12:26:32 -0700285 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700286 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700287 private static final String NETWORK_DATA = "nt";
288 private static final String USER_ACTIVITY_DATA = "ua";
289 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800290 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700291 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700292 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700293 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800294 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
295 private static final String WIFI_CONTROLLER_DATA = "wfcd";
296 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
297 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700298 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700299 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800300 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800301 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
302 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800303 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700304 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700305 private static final String SCREEN_BRIGHTNESS_DATA = "br";
306 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700307 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700308 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
309 private static final String DATA_CONNECTION_TIME_DATA = "dct";
310 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800311 private static final String WIFI_STATE_TIME_DATA = "wst";
312 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700313 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
314 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
315 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
316 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800317 private static final String POWER_USE_SUMMARY_DATA = "pws";
318 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700319 private static final String DISCHARGE_STEP_DATA = "dsd";
320 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700321 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
322 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700323 private static final String FLASHLIGHT_DATA = "fla";
324 private static final String CAMERA_DATA = "cam";
325 private static final String VIDEO_DATA = "vid";
326 private static final String AUDIO_DATA = "aud";
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700327 private static final String WIFI_MULTICAST_TOTAL_DATA = "wmct";
328 private static final String WIFI_MULTICAST_DATA = "wmc";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329
Adam Lesinski010bf372016-04-11 12:18:18 -0700330 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
331
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700332 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 private final Formatter mFormatter = new Formatter(mFormatBuilder);
334
335 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700336 * Indicates times spent by the uid at each cpu frequency in all process states.
337 *
338 * Other types might include times spent in foreground, background etc.
339 */
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800340 @VisibleForTesting
341 public static final String UID_TIMES_TYPE_ALL = "A";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700342
343 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700344 * State for keeping track of counting information.
345 */
346 public static abstract class Counter {
347
348 /**
349 * Returns the count associated with this Counter for the
350 * selected type of statistics.
351 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700352 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700353 */
Evan Millarc64edde2009-04-18 12:26:32 -0700354 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700355
356 /**
357 * Temporary for debugging.
358 */
359 public abstract void logState(Printer pw, String prefix);
360 }
361
362 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700363 * State for keeping track of long counting information.
364 */
365 public static abstract class LongCounter {
366
367 /**
368 * Returns the count associated with this Counter for the
369 * selected type of statistics.
370 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700371 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700372 */
373 public abstract long getCountLocked(int which);
374
375 /**
376 * Temporary for debugging.
377 */
378 public abstract void logState(Printer pw, String prefix);
379 }
380
381 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700382 * State for keeping track of array of long counting information.
383 */
384 public static abstract class LongCounterArray {
385 /**
386 * Returns the counts associated with this Counter for the
387 * selected type of statistics.
388 *
389 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
390 */
391 public abstract long[] getCountsLocked(int which);
392
393 /**
394 * Temporary for debugging.
395 */
396 public abstract void logState(Printer pw, String prefix);
397 }
398
399 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800400 * Container class that aggregates counters for transmit, receive, and idle state of a
401 * radio controller.
402 */
403 public static abstract class ControllerActivityCounter {
404 /**
405 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
406 * idle state.
407 */
408 public abstract LongCounter getIdleTimeCounter();
409
410 /**
411 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
412 * receive state.
413 */
414 public abstract LongCounter getRxTimeCounter();
415
416 /**
417 * An array of {@link LongCounter}, representing various transmit levels, where each level
418 * may draw a different amount of power. The levels themselves are controller-specific.
419 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
420 * various transmit level states.
421 */
422 public abstract LongCounter[] getTxTimeCounters();
423
424 /**
425 * @return a non-null {@link LongCounter} representing the power consumed by the controller
426 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
427 * yield a value of 0 if the device doesn't support power calculations.
428 */
429 public abstract LongCounter getPowerCounter();
430 }
431
432 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 * State for keeping track of timing information.
434 */
435 public static abstract class Timer {
436
437 /**
438 * Returns the count associated with this Timer for the
439 * selected type of statistics.
440 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700441 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 */
Evan Millarc64edde2009-04-18 12:26:32 -0700443 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444
445 /**
446 * Returns the total time in microseconds associated with this Timer for the
447 * selected type of statistics.
448 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800449 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700450 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 * @return a time in microseconds
452 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800453 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700456 * Returns the total time in microseconds associated with this Timer since the
457 * 'mark' was last set.
458 *
459 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
460 * @return a time in microseconds
461 */
462 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
463
464 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700465 * Returns the max duration if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700466 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700467 */
468 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
469 return -1;
470 }
471
472 /**
473 * Returns the current time the timer has been active, if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700474 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700475 */
476 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
477 return -1;
478 }
479
480 /**
Kweku Adams103351f2017-10-16 14:39:34 -0700481 * Returns the total time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800482 *
483 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
484 * been on since reset.
485 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
486 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
487 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
488 * the actual total time.
Kweku Adams103351f2017-10-16 14:39:34 -0700489 * Not all Timer subclasses track the max, total, and current durations.
Bookatz867c0d72017-03-07 18:23:42 -0800490 */
491 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
492 return -1;
493 }
494
495 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700496 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
497 * used, for example, for tracking background usage. Secondary timers are never pooled.
498 *
499 * Not all Timer subclasses have a secondary timer; those that don't return null.
500 */
501 public Timer getSubTimer() {
502 return null;
503 }
504
505 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700506 * Returns whether the timer is currently running. Some types of timers
507 * (e.g. BatchTimers) don't know whether the event is currently active,
508 * and report false.
509 */
510 public boolean isRunningLocked() {
511 return false;
512 }
513
514 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 * Temporary for debugging.
516 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700517 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 }
519
520 /**
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800521 * Maps the ActivityManager procstate into corresponding BatteryStats procstate.
522 */
523 public static int mapToInternalProcessState(int procState) {
524 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
525 return ActivityManager.PROCESS_STATE_NONEXISTENT;
526 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
527 return Uid.PROCESS_STATE_TOP;
Dianne Hackborn10fc4fd2017-12-19 17:23:13 -0800528 } else if (procState == ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
529 // State when app has put itself in the foreground.
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800530 return Uid.PROCESS_STATE_FOREGROUND_SERVICE;
531 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
532 // Persistent and other foreground states go here.
533 return Uid.PROCESS_STATE_FOREGROUND;
534 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
535 return Uid.PROCESS_STATE_BACKGROUND;
536 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
537 return Uid.PROCESS_STATE_TOP_SLEEPING;
538 } else if (procState <= ActivityManager.PROCESS_STATE_HEAVY_WEIGHT) {
539 return Uid.PROCESS_STATE_HEAVY_WEIGHT;
540 } else {
541 return Uid.PROCESS_STATE_CACHED;
542 }
543 }
544
545 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 * The statistics associated with a particular uid.
547 */
548 public static abstract class Uid {
549
550 /**
551 * Returns a mapping containing wakelock statistics.
552 *
553 * @return a Map from Strings to Uid.Wakelock objects.
554 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700555 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556
557 /**
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700558 * Returns the WiFi Multicast Wakelock statistics.
559 *
560 * @return a Timer Object for the per uid Multicast statistics.
561 */
562 public abstract Timer getMulticastWakelockStats();
563
564 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700565 * Returns a mapping containing sync statistics.
566 *
567 * @return a Map from Strings to Timer objects.
568 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700569 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700570
571 /**
572 * Returns a mapping containing scheduled job statistics.
573 *
574 * @return a Map from Strings to Timer objects.
575 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700576 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700577
578 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700579 * Returns statistics about how jobs have completed.
580 *
581 * @return A Map of String job names to completion type -> count mapping.
582 */
583 public abstract ArrayMap<String, SparseIntArray> getJobCompletionStats();
584
585 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 * The statistics associated with a particular wake lock.
587 */
588 public static abstract class Wakelock {
589 public abstract Timer getWakeTime(int type);
590 }
591
592 /**
Bookatzc8c44962017-05-11 12:12:54 -0700593 * The cumulative time the uid spent holding any partial wakelocks. This will generally
594 * differ from summing over the Wakelocks in getWakelockStats since the latter may have
595 * wakelocks that overlap in time (and therefore over-counts).
596 */
597 public abstract Timer getAggregatedPartialWakelockTimer();
598
599 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 * Returns a mapping containing sensor statistics.
601 *
602 * @return a Map from Integer sensor ids to Uid.Sensor objects.
603 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700604 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605
606 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700607 * Returns a mapping containing active process data.
608 */
609 public abstract SparseArray<? extends Pid> getPidStats();
Bookatzc8c44962017-05-11 12:12:54 -0700610
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700611 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 * Returns a mapping containing process statistics.
613 *
614 * @return a Map from Strings to Uid.Proc objects.
615 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700616 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617
618 /**
619 * Returns a mapping containing package statistics.
620 *
621 * @return a Map from Strings to Uid.Pkg objects.
622 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700623 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700624
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800625 public abstract ControllerActivityCounter getWifiControllerActivity();
626 public abstract ControllerActivityCounter getBluetoothControllerActivity();
627 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800628
629 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 * {@hide}
631 */
632 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700633
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800634 public abstract void noteWifiRunningLocked(long elapsedRealtime);
635 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
636 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
637 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
638 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
639 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
640 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
641 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
642 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
643 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800644 public abstract void noteActivityResumedLocked(long elapsedRealtime);
645 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800646 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
647 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
648 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700649 public abstract int getWifiScanCount(int which);
Kweku Adams103351f2017-10-16 14:39:34 -0700650 /**
651 * Returns the timer keeping track of wifi scans.
652 */
653 public abstract Timer getWifiScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800654 public abstract int getWifiScanBackgroundCount(int which);
655 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
656 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -0700657 /**
658 * Returns the timer keeping track of background wifi scans.
659 */
660 public abstract Timer getWifiScanBackgroundTimer();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800661 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700662 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800663 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700664 public abstract Timer getAudioTurnedOnTimer();
665 public abstract Timer getVideoTurnedOnTimer();
666 public abstract Timer getFlashlightTurnedOnTimer();
667 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700668 public abstract Timer getForegroundActivityTimer();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700669
670 /**
671 * Returns the timer keeping track of Foreground Service time
672 */
673 public abstract Timer getForegroundServiceTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800674 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800675 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatzb1f04f32017-05-19 13:57:32 -0700676 public abstract Timer getBluetoothUnoptimizedScanTimer();
677 public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700678 public abstract Counter getBluetoothScanResultCounter();
Bookatzb1f04f32017-05-19 13:57:32 -0700679 public abstract Counter getBluetoothScanResultBgCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700680
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700681 public abstract long[] getCpuFreqTimes(int which);
682 public abstract long[] getScreenOffCpuFreqTimes(int which);
683
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800684 /**
685 * Returns cpu times of an uid at a particular process state.
686 */
687 public abstract long[] getCpuFreqTimes(int which, int procState);
688 /**
689 * Returns cpu times of an uid while the screen if off at a particular process state.
690 */
691 public abstract long[] getScreenOffCpuFreqTimes(int which, int procState);
692
Dianne Hackborna0200e32016-03-30 18:01:41 -0700693 // Note: the following times are disjoint. They can be added together to find the
694 // total time a uid has had any processes running at all.
695
696 /**
697 * Time this uid has any processes in the top state (or above such as persistent).
698 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800699 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700700 /**
701 * Time this uid has any process with a started out bound foreground service, but
702 * none in the "top" state.
703 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800704 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700705 /**
Dianne Hackborna0200e32016-03-30 18:01:41 -0700706 * Time this uid has any process in an active foreground state, but none in the
707 * "top sleeping" or better state.
708 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800709 public static final int PROCESS_STATE_FOREGROUND = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700710 /**
711 * Time this uid has any process in an active background state, but none in the
712 * "foreground" or better state.
713 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800714 public static final int PROCESS_STATE_BACKGROUND = 3;
715 /**
716 * Time this uid has any process that is top while the device is sleeping, but not
717 * active for any other reason. We kind-of consider it a kind of cached process
718 * for execution restrictions.
719 */
720 public static final int PROCESS_STATE_TOP_SLEEPING = 4;
721 /**
722 * Time this uid has any process that is in the background but it has an activity
723 * marked as "can't save state". This is essentially a cached process, though the
724 * system will try much harder than normal to avoid killing it.
725 */
726 public static final int PROCESS_STATE_HEAVY_WEIGHT = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700727 /**
728 * Time this uid has any processes that are sitting around cached, not in one of the
729 * other active states.
730 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800731 public static final int PROCESS_STATE_CACHED = 6;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700732 /**
733 * Total number of process states we track.
734 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800735 public static final int NUM_PROCESS_STATE = 7;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700736
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800737 // Used in dump
Dianne Hackborn61659e52014-07-09 16:13:01 -0700738 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800739 "Top", "Fg Service", "Foreground", "Background", "Top Sleeping", "Heavy Weight",
740 "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700741 };
742
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800743 // Used in checkin dump
744 @VisibleForTesting
745 public static final String[] UID_PROCESS_TYPES = {
746 "T", // TOP
747 "FS", // FOREGROUND_SERVICE
748 "F", // FOREGROUND
749 "B", // BACKGROUND
750 "TS", // TOP_SLEEPING
751 "HW", // HEAVY_WEIGHT
752 "C" // CACHED
753 };
754
755 /**
756 * When the process exits one of these states, we need to make sure cpu time in this state
757 * is not attributed to any non-critical process states.
758 */
759 public static final int[] CRITICAL_PROC_STATES = {
760 PROCESS_STATE_TOP, PROCESS_STATE_FOREGROUND_SERVICE, PROCESS_STATE_FOREGROUND
761 };
762
Dianne Hackborn61659e52014-07-09 16:13:01 -0700763 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800764 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700765
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800766 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767
Robert Greenwalta029ea12013-09-25 16:38:12 -0700768 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
769
Dianne Hackborn617f8772009-03-31 15:04:46 -0700770 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700771 * Note that these must match the constants in android.os.PowerManager.
772 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
773 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700774 */
775 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700776 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700777 };
Bookatzc8c44962017-05-11 12:12:54 -0700778
Phil Weaverda80d672016-03-15 16:25:46 -0700779 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700780
Dianne Hackborn617f8772009-03-31 15:04:46 -0700781 public abstract void noteUserActivityLocked(int type);
782 public abstract boolean hasUserActivity();
783 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700784
785 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800786 public abstract long getNetworkActivityBytes(int type, int which);
787 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800788 public abstract long getMobileRadioActiveTime(int which);
789 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700790
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700791 /**
792 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
793 */
794 public abstract long getUserCpuTimeUs(int which);
795
796 /**
797 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
798 */
799 public abstract long getSystemCpuTimeUs(int which);
800
801 /**
Sudheer Shanka71f34b32017-07-21 00:14:24 -0700802 * Returns the approximate cpu time (in microseconds) spent at a certain CPU speed for a
Adam Lesinski6832f392015-09-05 18:05:40 -0700803 * given CPU cluster.
804 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700805 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700806 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700807 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
808 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700809 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700810 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700811
Adam Lesinski5f056f62016-07-14 16:56:08 -0700812 /**
813 * Returns the number of times this UID woke up the Application Processor to
814 * process a mobile radio packet.
815 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
816 */
817 public abstract long getMobileRadioApWakeupCount(int which);
818
819 /**
820 * Returns the number of times this UID woke up the Application Processor to
821 * process a WiFi packet.
822 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
823 */
824 public abstract long getWifiRadioApWakeupCount(int which);
825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800827 /*
828 * FIXME: it's not correct to use this magic value because it
829 * could clash with a sensor handle (which are defined by
830 * the sensor HAL, and therefore out of our control
831 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 // Magic sensor number for the GPS.
833 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800838
Bookatz867c0d72017-03-07 18:23:42 -0800839 /** Returns a Timer for sensor usage when app is in the background. */
840 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 }
842
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700843 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800844 public int mWakeNesting;
845 public long mWakeSumMs;
846 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700847 }
848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 /**
850 * The statistics associated with a particular process.
851 */
852 public static abstract class Proc {
853
Dianne Hackborn287952c2010-09-22 22:34:31 -0700854 public static class ExcessivePower {
855 public static final int TYPE_WAKE = 1;
856 public static final int TYPE_CPU = 2;
857
858 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700859 public long overTime;
860 public long usedTime;
861 }
862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800864 * Returns true if this process is still active in the battery stats.
865 */
866 public abstract boolean isActive();
867
868 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700869 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700871 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 */
873 public abstract long getUserTime(int which);
874
875 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700876 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700878 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 */
880 public abstract long getSystemTime(int which);
881
882 /**
883 * Returns the number of times the process has been started.
884 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700885 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 */
887 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700888
889 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800890 * Returns the number of times the process has crashed.
891 *
892 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
893 */
894 public abstract int getNumCrashes(int which);
895
896 /**
897 * Returns the number of times the process has ANRed.
898 *
899 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
900 */
901 public abstract int getNumAnrs(int which);
902
903 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700904 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700905 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700906 * @return foreground cpu time in microseconds
907 */
908 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700909
Dianne Hackborn287952c2010-09-22 22:34:31 -0700910 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700911
Dianne Hackborn287952c2010-09-22 22:34:31 -0700912 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 }
914
915 /**
916 * The statistics associated with a particular package.
917 */
918 public static abstract class Pkg {
919
920 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700921 * Returns information about all wakeup alarms that have been triggered for this
922 * package. The mapping keys are tag names for the alarms, the counter contains
923 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700925 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926
927 /**
928 * Returns a mapping containing service statistics.
929 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700930 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931
932 /**
933 * The statistics associated with a particular service.
934 */
Joe Onoratoabded112016-02-08 16:49:39 -0800935 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936
937 /**
938 * Returns the amount of time spent started.
939 *
940 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700941 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 * @return
943 */
944 public abstract long getStartTime(long batteryUptime, int which);
945
946 /**
947 * Returns the total number of times startService() has been called.
948 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700949 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 */
951 public abstract int getStarts(int which);
952
953 /**
954 * Returns the total number times the service has been launched.
955 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700956 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 */
958 public abstract int getLaunches(int which);
959 }
960 }
961 }
962
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800963 public static final class LevelStepTracker {
964 public long mLastStepTime = -1;
965 public int mNumStepDurations;
966 public final long[] mStepDurations;
967
968 public LevelStepTracker(int maxLevelSteps) {
969 mStepDurations = new long[maxLevelSteps];
970 }
971
972 public LevelStepTracker(int numSteps, long[] steps) {
973 mNumStepDurations = numSteps;
974 mStepDurations = new long[numSteps];
975 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
976 }
977
978 public long getDurationAt(int index) {
979 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
980 }
981
982 public int getLevelAt(int index) {
983 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
984 >> STEP_LEVEL_LEVEL_SHIFT);
985 }
986
987 public int getInitModeAt(int index) {
988 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
989 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
990 }
991
992 public int getModModeAt(int index) {
993 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
994 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
995 }
996
997 private void appendHex(long val, int topOffset, StringBuilder out) {
998 boolean hasData = false;
999 while (topOffset >= 0) {
1000 int digit = (int)( (val>>topOffset) & 0xf );
1001 topOffset -= 4;
1002 if (!hasData && digit == 0) {
1003 continue;
1004 }
1005 hasData = true;
1006 if (digit >= 0 && digit <= 9) {
1007 out.append((char)('0' + digit));
1008 } else {
1009 out.append((char)('a' + digit - 10));
1010 }
1011 }
1012 }
1013
1014 public void encodeEntryAt(int index, StringBuilder out) {
1015 long item = mStepDurations[index];
1016 long duration = item & STEP_LEVEL_TIME_MASK;
1017 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
1018 >> STEP_LEVEL_LEVEL_SHIFT);
1019 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
1020 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1021 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
1022 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1023 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1024 case Display.STATE_OFF: out.append('f'); break;
1025 case Display.STATE_ON: out.append('o'); break;
1026 case Display.STATE_DOZE: out.append('d'); break;
1027 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
1028 }
1029 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1030 out.append('p');
1031 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001032 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1033 out.append('i');
1034 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001035 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1036 case Display.STATE_OFF: out.append('F'); break;
1037 case Display.STATE_ON: out.append('O'); break;
1038 case Display.STATE_DOZE: out.append('D'); break;
1039 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
1040 }
1041 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1042 out.append('P');
1043 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001044 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1045 out.append('I');
1046 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001047 out.append('-');
1048 appendHex(level, 4, out);
1049 out.append('-');
1050 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
1051 }
1052
1053 public void decodeEntryAt(int index, String value) {
1054 final int N = value.length();
1055 int i = 0;
1056 char c;
1057 long out = 0;
1058 while (i < N && (c=value.charAt(i)) != '-') {
1059 i++;
1060 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001061 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001062 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001063 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001064 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001065 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001066 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001067 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1068 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1069 break;
1070 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1071 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1072 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001073 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1074 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1075 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001076 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1077 break;
1078 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1079 break;
1080 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1081 break;
1082 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1083 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1084 break;
1085 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1086 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001087 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001088 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1089 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1090 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001091 }
1092 }
1093 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001094 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001095 while (i < N && (c=value.charAt(i)) != '-') {
1096 i++;
1097 level <<= 4;
1098 if (c >= '0' && c <= '9') {
1099 level += c - '0';
1100 } else if (c >= 'a' && c <= 'f') {
1101 level += c - 'a' + 10;
1102 } else if (c >= 'A' && c <= 'F') {
1103 level += c - 'A' + 10;
1104 }
1105 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001106 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001107 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
1108 long duration = 0;
1109 while (i < N && (c=value.charAt(i)) != '-') {
1110 i++;
1111 duration <<= 4;
1112 if (c >= '0' && c <= '9') {
1113 duration += c - '0';
1114 } else if (c >= 'a' && c <= 'f') {
1115 duration += c - 'a' + 10;
1116 } else if (c >= 'A' && c <= 'F') {
1117 duration += c - 'A' + 10;
1118 }
1119 }
1120 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
1121 }
1122
1123 public void init() {
1124 mLastStepTime = -1;
1125 mNumStepDurations = 0;
1126 }
1127
1128 public void clearTime() {
1129 mLastStepTime = -1;
1130 }
1131
1132 public long computeTimePerLevel() {
1133 final long[] steps = mStepDurations;
1134 final int numSteps = mNumStepDurations;
1135
1136 // For now we'll do a simple average across all steps.
1137 if (numSteps <= 0) {
1138 return -1;
1139 }
1140 long total = 0;
1141 for (int i=0; i<numSteps; i++) {
1142 total += steps[i] & STEP_LEVEL_TIME_MASK;
1143 }
1144 return total / numSteps;
1145 /*
1146 long[] buckets = new long[numSteps];
1147 int numBuckets = 0;
1148 int numToAverage = 4;
1149 int i = 0;
1150 while (i < numSteps) {
1151 long totalTime = 0;
1152 int num = 0;
1153 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1154 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1155 num++;
1156 }
1157 buckets[numBuckets] = totalTime / num;
1158 numBuckets++;
1159 numToAverage *= 2;
1160 i += num;
1161 }
1162 if (numBuckets < 1) {
1163 return -1;
1164 }
1165 long averageTime = buckets[numBuckets-1];
1166 for (i=numBuckets-2; i>=0; i--) {
1167 averageTime = (averageTime + buckets[i]) / 2;
1168 }
1169 return averageTime;
1170 */
1171 }
1172
1173 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1174 int[] outNumOfInterest) {
1175 final long[] steps = mStepDurations;
1176 final int count = mNumStepDurations;
1177 if (count <= 0) {
1178 return -1;
1179 }
1180 long total = 0;
1181 int numOfInterest = 0;
1182 for (int i=0; i<count; i++) {
1183 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1184 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1185 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1186 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1187 // If the modes of interest didn't change during this step period...
1188 if ((modMode&modesOfInterest) == 0) {
1189 // And the mode values during this period match those we are measuring...
1190 if ((initMode&modesOfInterest) == modeValues) {
1191 // Then this can be used to estimate the total time!
1192 numOfInterest++;
1193 total += steps[i] & STEP_LEVEL_TIME_MASK;
1194 }
1195 }
1196 }
1197 if (numOfInterest <= 0) {
1198 return -1;
1199 }
1200
1201 if (outNumOfInterest != null) {
1202 outNumOfInterest[0] = numOfInterest;
1203 }
1204
1205 // The estimated time is the average time we spend in each level, multipled
1206 // by 100 -- the total number of battery levels
1207 return (total / numOfInterest) * 100;
1208 }
1209
1210 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1211 int stepCount = mNumStepDurations;
1212 final long lastStepTime = mLastStepTime;
1213 if (lastStepTime >= 0 && numStepLevels > 0) {
1214 final long[] steps = mStepDurations;
1215 long duration = elapsedRealtime - lastStepTime;
1216 for (int i=0; i<numStepLevels; i++) {
1217 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1218 long thisDuration = duration / (numStepLevels-i);
1219 duration -= thisDuration;
1220 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1221 thisDuration = STEP_LEVEL_TIME_MASK;
1222 }
1223 steps[0] = thisDuration | modeBits;
1224 }
1225 stepCount += numStepLevels;
1226 if (stepCount > steps.length) {
1227 stepCount = steps.length;
1228 }
1229 }
1230 mNumStepDurations = stepCount;
1231 mLastStepTime = elapsedRealtime;
1232 }
1233
1234 public void readFromParcel(Parcel in) {
1235 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001236 if (N > mStepDurations.length) {
1237 throw new ParcelFormatException("more step durations than available: " + N);
1238 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001239 mNumStepDurations = N;
1240 for (int i=0; i<N; i++) {
1241 mStepDurations[i] = in.readLong();
1242 }
1243 }
1244
1245 public void writeToParcel(Parcel out) {
1246 final int N = mNumStepDurations;
1247 out.writeInt(N);
1248 for (int i=0; i<N; i++) {
1249 out.writeLong(mStepDurations[i]);
1250 }
1251 }
1252 }
1253
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001254 public static final class PackageChange {
1255 public String mPackageName;
1256 public boolean mUpdate;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001257 public long mVersionCode;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001258 }
1259
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001260 public static final class DailyItem {
1261 public long mStartTime;
1262 public long mEndTime;
1263 public LevelStepTracker mDischargeSteps;
1264 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001265 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001266 }
1267
1268 public abstract DailyItem getDailyItemLocked(int daysAgo);
1269
1270 public abstract long getCurrentDailyStartTime();
1271
1272 public abstract long getNextMinDailyDeadline();
1273
1274 public abstract long getNextMaxDailyDeadline();
1275
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001276 public abstract long[] getCpuFreqs();
1277
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001278 public final static class HistoryTag {
1279 public String string;
1280 public int uid;
1281
1282 public int poolIdx;
1283
1284 public void setTo(HistoryTag o) {
1285 string = o.string;
1286 uid = o.uid;
1287 poolIdx = o.poolIdx;
1288 }
1289
1290 public void setTo(String _string, int _uid) {
1291 string = _string;
1292 uid = _uid;
1293 poolIdx = -1;
1294 }
1295
1296 public void writeToParcel(Parcel dest, int flags) {
1297 dest.writeString(string);
1298 dest.writeInt(uid);
1299 }
1300
1301 public void readFromParcel(Parcel src) {
1302 string = src.readString();
1303 uid = src.readInt();
1304 poolIdx = -1;
1305 }
1306
1307 @Override
1308 public boolean equals(Object o) {
1309 if (this == o) return true;
1310 if (o == null || getClass() != o.getClass()) return false;
1311
1312 HistoryTag that = (HistoryTag) o;
1313
1314 if (uid != that.uid) return false;
1315 if (!string.equals(that.string)) return false;
1316
1317 return true;
1318 }
1319
1320 @Override
1321 public int hashCode() {
1322 int result = string.hashCode();
1323 result = 31 * result + uid;
1324 return result;
1325 }
1326 }
1327
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001328 /**
1329 * Optional detailed information that can go into a history step. This is typically
1330 * generated each time the battery level changes.
1331 */
1332 public final static class HistoryStepDetails {
1333 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1334 public int userTime;
1335 public int systemTime;
1336
1337 // Top three apps using CPU in the last step, with times in 1/100 second.
1338 public int appCpuUid1;
1339 public int appCpuUTime1;
1340 public int appCpuSTime1;
1341 public int appCpuUid2;
1342 public int appCpuUTime2;
1343 public int appCpuSTime2;
1344 public int appCpuUid3;
1345 public int appCpuUTime3;
1346 public int appCpuSTime3;
1347
1348 // Information from /proc/stat
1349 public int statUserTime;
1350 public int statSystemTime;
1351 public int statIOWaitTime;
1352 public int statIrqTime;
1353 public int statSoftIrqTime;
1354 public int statIdlTime;
1355
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001356 // Platform-level low power state stats
1357 public String statPlatformIdleState;
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001358 public String statSubsystemPowerState;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001359
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001360 public HistoryStepDetails() {
1361 clear();
1362 }
1363
1364 public void clear() {
1365 userTime = systemTime = 0;
1366 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1367 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1368 = appCpuUTime3 = appCpuSTime3 = 0;
1369 }
1370
1371 public void writeToParcel(Parcel out) {
1372 out.writeInt(userTime);
1373 out.writeInt(systemTime);
1374 out.writeInt(appCpuUid1);
1375 out.writeInt(appCpuUTime1);
1376 out.writeInt(appCpuSTime1);
1377 out.writeInt(appCpuUid2);
1378 out.writeInt(appCpuUTime2);
1379 out.writeInt(appCpuSTime2);
1380 out.writeInt(appCpuUid3);
1381 out.writeInt(appCpuUTime3);
1382 out.writeInt(appCpuSTime3);
1383 out.writeInt(statUserTime);
1384 out.writeInt(statSystemTime);
1385 out.writeInt(statIOWaitTime);
1386 out.writeInt(statIrqTime);
1387 out.writeInt(statSoftIrqTime);
1388 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001389 out.writeString(statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001390 out.writeString(statSubsystemPowerState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001391 }
1392
1393 public void readFromParcel(Parcel in) {
1394 userTime = in.readInt();
1395 systemTime = in.readInt();
1396 appCpuUid1 = in.readInt();
1397 appCpuUTime1 = in.readInt();
1398 appCpuSTime1 = in.readInt();
1399 appCpuUid2 = in.readInt();
1400 appCpuUTime2 = in.readInt();
1401 appCpuSTime2 = in.readInt();
1402 appCpuUid3 = in.readInt();
1403 appCpuUTime3 = in.readInt();
1404 appCpuSTime3 = in.readInt();
1405 statUserTime = in.readInt();
1406 statSystemTime = in.readInt();
1407 statIOWaitTime = in.readInt();
1408 statIrqTime = in.readInt();
1409 statSoftIrqTime = in.readInt();
1410 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001411 statPlatformIdleState = in.readString();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001412 statSubsystemPowerState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001413 }
1414 }
1415
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001416 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001417 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001418
1419 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001420 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001421
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001422 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001423 public static final byte CMD_NULL = -1;
1424 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001425 public static final byte CMD_CURRENT_TIME = 5;
1426 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001427 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001428 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001429
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001430 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001431
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001432 /**
1433 * Return whether the command code is a delta data update.
1434 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001435 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001436 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001437 }
1438
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001439 public byte batteryLevel;
1440 public byte batteryStatus;
1441 public byte batteryHealth;
1442 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001443
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001444 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001445 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001446
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001447 // The charge of the battery in micro-Ampere-hours.
1448 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001449
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001450 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001451 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001452 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001453 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001454 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1455 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001456 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001457 public static final int STATE_PHONE_STATE_SHIFT = 6;
1458 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001459 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001460 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001461 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001462
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001463 // These states always appear directly in the first int token
1464 // of a delta change; they should be ones that change relatively
1465 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001466 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1467 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001468 public static final int STATE_GPS_ON_FLAG = 1<<29;
1469 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001470 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001471 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001472 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001473 // Do not use, this is used for coulomb delta count.
1474 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001475 // These are on the lower bits used for the command; if they change
1476 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001477 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001478 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001479 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001480 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1481 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
Mike Mac2f518a2017-09-19 16:06:03 -07001482 public static final int STATE_SCREEN_DOZE_FLAG = 1 << 18;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001483 // empty slot
1484 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001485
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001486 public static final int MOST_INTERESTING_STATES =
Mike Mac2f518a2017-09-19 16:06:03 -07001487 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG | STATE_SCREEN_DOZE_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001488
1489 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001490
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001491 public int states;
1492
Dianne Hackborn3251b902014-06-20 14:40:53 -07001493 // Constants from WIFI_SUPPL_STATE_*
1494 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1495 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1496 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1497 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1498 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1499 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1500
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001501 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001502 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1503 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1504 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001505 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001506 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1507 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1508 public static final int STATE2_CHARGING_FLAG = 1<<24;
1509 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1510 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1511 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001512 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001513
1514 public static final int MOST_INTERESTING_STATES2 =
Mike Mac2f518a2017-09-19 16:06:03 -07001515 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
1516 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001517
1518 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001519
Dianne Hackborn40c87252014-03-19 16:55:40 -07001520 public int states2;
1521
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001522 // The wake lock that was acquired at this point.
1523 public HistoryTag wakelockTag;
1524
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001525 // Kernel wakeup reason at this point.
1526 public HistoryTag wakeReasonTag;
1527
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001528 // Non-null when there is more detailed information at this step.
1529 public HistoryStepDetails stepDetails;
1530
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001531 public static final int EVENT_FLAG_START = 0x8000;
1532 public static final int EVENT_FLAG_FINISH = 0x4000;
1533
1534 // No event in this item.
1535 public static final int EVENT_NONE = 0x0000;
1536 // Event is about a process that is running.
1537 public static final int EVENT_PROC = 0x0001;
1538 // Event is about an application package that is in the foreground.
1539 public static final int EVENT_FOREGROUND = 0x0002;
1540 // Event is about an application package that is at the top of the screen.
1541 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001542 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001543 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001544 // Events for all additional wake locks aquired/release within a wake block.
1545 // These are not generated by default.
1546 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001547 // Event is about an application executing a scheduled job.
1548 public static final int EVENT_JOB = 0x0006;
1549 // Events for users running.
1550 public static final int EVENT_USER_RUNNING = 0x0007;
1551 // Events for foreground user.
1552 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001553 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001554 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001555 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001556 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001557 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001558 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001559 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001560 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001561 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001562 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001563 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001564 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001565 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001566 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001567 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001568 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001569 // Event for a package being on the temporary whitelist.
1570 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001571 // Event for the screen waking up.
1572 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001573 // Event for the UID that woke up the application processor.
1574 // Used for wakeups coming from WiFi, modem, etc.
1575 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001576 // Event for reporting that a specific partial wake lock has been held for a long duration.
1577 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001578
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001579 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001580 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001581 // Mask to extract out only the type part of the event.
1582 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001583
1584 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1585 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1586 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1587 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1588 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1589 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001590 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1591 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001592 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1593 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001594 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1595 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1596 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1597 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1598 public static final int EVENT_USER_FOREGROUND_START =
1599 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1600 public static final int EVENT_USER_FOREGROUND_FINISH =
1601 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001602 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1603 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001604 public static final int EVENT_TEMP_WHITELIST_START =
1605 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1606 public static final int EVENT_TEMP_WHITELIST_FINISH =
1607 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001608 public static final int EVENT_LONG_WAKE_LOCK_START =
1609 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1610 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1611 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001612
1613 // For CMD_EVENT.
1614 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001615 public HistoryTag eventTag;
1616
Dianne Hackborn9a755432014-05-15 17:05:22 -07001617 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001618 public long currentTime;
1619
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001620 // Meta-data when reading.
1621 public int numReadInts;
1622
1623 // Pre-allocated objects.
1624 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001625 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001626 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001627
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001628 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001629 }
Bookatzc8c44962017-05-11 12:12:54 -07001630
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001631 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001632 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001633 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001634 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001635 }
Bookatzc8c44962017-05-11 12:12:54 -07001636
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001637 public int describeContents() {
1638 return 0;
1639 }
1640
1641 public void writeToParcel(Parcel dest, int flags) {
1642 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001643 int bat = (((int)cmd)&0xff)
1644 | ((((int)batteryLevel)<<8)&0xff00)
1645 | ((((int)batteryStatus)<<16)&0xf0000)
1646 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001647 | ((((int)batteryPlugType)<<24)&0xf000000)
1648 | (wakelockTag != null ? 0x10000000 : 0)
1649 | (wakeReasonTag != null ? 0x20000000 : 0)
1650 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001651 dest.writeInt(bat);
1652 bat = (((int)batteryTemperature)&0xffff)
1653 | ((((int)batteryVoltage)<<16)&0xffff0000);
1654 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001655 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001656 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001657 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001658 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001659 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001660 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001661 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001662 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001663 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001664 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001665 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001666 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001667 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001668 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001669 dest.writeLong(currentTime);
1670 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001671 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001672
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001673 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001674 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001675 int bat = src.readInt();
1676 cmd = (byte)(bat&0xff);
1677 batteryLevel = (byte)((bat>>8)&0xff);
1678 batteryStatus = (byte)((bat>>16)&0xf);
1679 batteryHealth = (byte)((bat>>20)&0xf);
1680 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001681 int bat2 = src.readInt();
1682 batteryTemperature = (short)(bat2&0xffff);
1683 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001684 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001685 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001686 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001687 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001688 wakelockTag = localWakelockTag;
1689 wakelockTag.readFromParcel(src);
1690 } else {
1691 wakelockTag = null;
1692 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001693 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001694 wakeReasonTag = localWakeReasonTag;
1695 wakeReasonTag.readFromParcel(src);
1696 } else {
1697 wakeReasonTag = null;
1698 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001699 if ((bat&0x40000000) != 0) {
1700 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001701 eventTag = localEventTag;
1702 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001703 } else {
1704 eventCode = EVENT_NONE;
1705 eventTag = null;
1706 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001707 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001708 currentTime = src.readLong();
1709 } else {
1710 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001711 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001712 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001713 }
1714
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001715 public void clear() {
1716 time = 0;
1717 cmd = CMD_NULL;
1718 batteryLevel = 0;
1719 batteryStatus = 0;
1720 batteryHealth = 0;
1721 batteryPlugType = 0;
1722 batteryTemperature = 0;
1723 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001724 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001725 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001726 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001727 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001728 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001729 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001730 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001731 }
Bookatzc8c44962017-05-11 12:12:54 -07001732
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001733 public void setTo(HistoryItem o) {
1734 time = o.time;
1735 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001736 setToCommon(o);
1737 }
1738
1739 public void setTo(long time, byte cmd, HistoryItem o) {
1740 this.time = time;
1741 this.cmd = cmd;
1742 setToCommon(o);
1743 }
1744
1745 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001746 batteryLevel = o.batteryLevel;
1747 batteryStatus = o.batteryStatus;
1748 batteryHealth = o.batteryHealth;
1749 batteryPlugType = o.batteryPlugType;
1750 batteryTemperature = o.batteryTemperature;
1751 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001752 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001753 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001754 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001755 if (o.wakelockTag != null) {
1756 wakelockTag = localWakelockTag;
1757 wakelockTag.setTo(o.wakelockTag);
1758 } else {
1759 wakelockTag = null;
1760 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001761 if (o.wakeReasonTag != null) {
1762 wakeReasonTag = localWakeReasonTag;
1763 wakeReasonTag.setTo(o.wakeReasonTag);
1764 } else {
1765 wakeReasonTag = null;
1766 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001767 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001768 if (o.eventTag != null) {
1769 eventTag = localEventTag;
1770 eventTag.setTo(o.eventTag);
1771 } else {
1772 eventTag = null;
1773 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001774 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001775 }
1776
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001777 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001778 return batteryLevel == o.batteryLevel
1779 && batteryStatus == o.batteryStatus
1780 && batteryHealth == o.batteryHealth
1781 && batteryPlugType == o.batteryPlugType
1782 && batteryTemperature == o.batteryTemperature
1783 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001784 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001785 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001786 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001787 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001788 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001789
1790 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001791 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001792 return false;
1793 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001794 if (wakelockTag != o.wakelockTag) {
1795 if (wakelockTag == null || o.wakelockTag == null) {
1796 return false;
1797 }
1798 if (!wakelockTag.equals(o.wakelockTag)) {
1799 return false;
1800 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001801 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001802 if (wakeReasonTag != o.wakeReasonTag) {
1803 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1804 return false;
1805 }
1806 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1807 return false;
1808 }
1809 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001810 if (eventTag != o.eventTag) {
1811 if (eventTag == null || o.eventTag == null) {
1812 return false;
1813 }
1814 if (!eventTag.equals(o.eventTag)) {
1815 return false;
1816 }
1817 }
1818 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001819 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001820 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001821
1822 public final static class HistoryEventTracker {
1823 private final HashMap<String, SparseIntArray>[] mActiveEvents
1824 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1825
1826 public boolean updateState(int code, String name, int uid, int poolIdx) {
1827 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1828 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1829 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1830 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001831 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001832 mActiveEvents[idx] = active;
1833 }
1834 SparseIntArray uids = active.get(name);
1835 if (uids == null) {
1836 uids = new SparseIntArray();
1837 active.put(name, uids);
1838 }
1839 if (uids.indexOfKey(uid) >= 0) {
1840 // Already set, nothing to do!
1841 return false;
1842 }
1843 uids.put(uid, poolIdx);
1844 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1845 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1846 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1847 if (active == null) {
1848 // not currently active, nothing to do.
1849 return false;
1850 }
1851 SparseIntArray uids = active.get(name);
1852 if (uids == null) {
1853 // not currently active, nothing to do.
1854 return false;
1855 }
1856 idx = uids.indexOfKey(uid);
1857 if (idx < 0) {
1858 // not currently active, nothing to do.
1859 return false;
1860 }
1861 uids.removeAt(idx);
1862 if (uids.size() <= 0) {
1863 active.remove(name);
1864 }
1865 }
1866 return true;
1867 }
1868
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001869 public void removeEvents(int code) {
1870 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1871 mActiveEvents[idx] = null;
1872 }
1873
Dianne Hackborn37de0982014-05-09 09:32:18 -07001874 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1875 return mActiveEvents[code];
1876 }
1877 }
1878
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001879 public static final class BitDescription {
1880 public final int mask;
1881 public final int shift;
1882 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001883 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001884 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001885 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07001886
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001887 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001888 this.mask = mask;
1889 this.shift = -1;
1890 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001891 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001892 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001893 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001894 }
Bookatzc8c44962017-05-11 12:12:54 -07001895
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001896 public BitDescription(int mask, int shift, String name, String shortName,
1897 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001898 this.mask = mask;
1899 this.shift = shift;
1900 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001901 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001902 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001903 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001904 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001905 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001906
Dianne Hackbornfc064132014-06-02 12:42:12 -07001907 /**
1908 * Don't allow any more batching in to the current history event. This
1909 * is called when printing partial histories, so to ensure that the next
1910 * history event will go in to a new batch after what was printed in the
1911 * last partial history.
1912 */
1913 public abstract void commitCurrentHistoryBatchLocked();
1914
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001915 public abstract int getHistoryTotalSize();
1916
1917 public abstract int getHistoryUsedSize();
1918
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001919 public abstract boolean startIteratingHistoryLocked();
1920
Dianne Hackborn099bc622014-01-22 13:39:16 -08001921 public abstract int getHistoryStringPoolSize();
1922
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001923 public abstract int getHistoryStringPoolBytes();
1924
1925 public abstract String getHistoryTagPoolString(int index);
1926
1927 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001928
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001929 public abstract boolean getNextHistoryLocked(HistoryItem out);
1930
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001931 public abstract void finishIteratingHistoryLocked();
1932
1933 public abstract boolean startIteratingOldHistoryLocked();
1934
1935 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1936
1937 public abstract void finishIteratingOldHistoryLocked();
1938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001940 * Return the base time offset for the battery history.
1941 */
1942 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07001943
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001944 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 * Returns the number of times the device has been started.
1946 */
1947 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07001948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001950 * 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 -08001951 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07001952 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 * {@hide}
1954 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001955 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07001956
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001957 /**
1958 * Returns the number of times the screen was turned on.
1959 *
1960 * {@hide}
1961 */
1962 public abstract int getScreenOnCount(int which);
1963
Mike Mac2f518a2017-09-19 16:06:03 -07001964 /**
1965 * Returns the time in microseconds that the screen has been dozing while the device was
1966 * running on battery.
1967 *
1968 * {@hide}
1969 */
1970 public abstract long getScreenDozeTime(long elapsedRealtimeUs, int which);
1971
1972 /**
1973 * Returns the number of times the screen was turned dozing.
1974 *
1975 * {@hide}
1976 */
1977 public abstract int getScreenDozeCount(int which);
1978
Jeff Browne95c3cd2014-05-02 16:59:26 -07001979 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1980
Dianne Hackborn617f8772009-03-31 15:04:46 -07001981 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1982 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1983 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1984 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1985 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07001986
Dianne Hackborn617f8772009-03-31 15:04:46 -07001987 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1988 "dark", "dim", "medium", "light", "bright"
1989 };
Bookatzc8c44962017-05-11 12:12:54 -07001990
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001991 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1992 "0", "1", "2", "3", "4"
1993 };
1994
Dianne Hackborn617f8772009-03-31 15:04:46 -07001995 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001996
Dianne Hackborn617f8772009-03-31 15:04:46 -07001997 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001998 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001999 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07002000 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002001 * {@hide}
2002 */
2003 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002004 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002007 * Returns the {@link Timer} object that tracks the given screen brightness.
2008 *
2009 * {@hide}
2010 */
2011 public abstract Timer getScreenBrightnessTimer(int brightnessBin);
2012
2013 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002014 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002015 * running on battery.
2016 *
2017 * {@hide}
2018 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002019 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002020
2021 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002022 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002023 *
2024 * {@hide}
2025 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002026 public abstract int getPowerSaveModeEnabledCount(int which);
2027
2028 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002029 * Constant for device idle mode: not active.
2030 */
2031 public static final int DEVICE_IDLE_MODE_OFF = 0;
2032
2033 /**
2034 * Constant for device idle mode: active in lightweight mode.
2035 */
2036 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
2037
2038 /**
2039 * Constant for device idle mode: active in full mode.
2040 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002041 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002042
2043 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002044 * Returns the time in microseconds that device has been in idle mode while
2045 * running on battery.
2046 *
2047 * {@hide}
2048 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002049 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002050
2051 /**
2052 * Returns the number of times that the devie has gone in to idle mode.
2053 *
2054 * {@hide}
2055 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002056 public abstract int getDeviceIdleModeCount(int mode, int which);
2057
2058 /**
2059 * Return the longest duration we spent in a particular device idle mode (fully in the
2060 * mode, not in idle maintenance etc).
2061 */
2062 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002063
2064 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002065 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002066 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002067 * counts all of the time that we consider the device to be idle, whether or not
2068 * it is currently in the actual device idle mode.
2069 *
2070 * {@hide}
2071 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002072 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002073
2074 /**
Bookatz8c6571b2017-10-24 15:04:41 -07002075 * Returns the number of times that the device has started idling.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002076 *
2077 * {@hide}
2078 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002079 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002080
2081 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002082 * Returns the number of times that connectivity state changed.
2083 *
2084 * {@hide}
2085 */
2086 public abstract int getNumConnectivityChange(int which);
2087
2088 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002089 * 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 -08002090 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002091 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 * {@hide}
2093 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002094 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002095
Dianne Hackborn627bba72009-03-24 22:32:56 -07002096 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002097 * Returns the number of times a phone call was activated.
2098 *
2099 * {@hide}
2100 */
2101 public abstract int getPhoneOnCount(int which);
2102
2103 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002104 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002105 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002106 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002107 * {@hide}
2108 */
2109 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002110 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002111
Dianne Hackborn617f8772009-03-31 15:04:46 -07002112 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07002113 * Returns the time in microseconds that the phone has been trying to
2114 * acquire a signal.
2115 *
2116 * {@hide}
2117 */
2118 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002119 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002120
2121 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002122 * Returns the {@link Timer} object that tracks how much the phone has been trying to
2123 * acquire a signal.
2124 *
2125 * {@hide}
2126 */
2127 public abstract Timer getPhoneSignalScanningTimer();
2128
2129 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002130 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002131 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002132 * {@hide}
2133 */
2134 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
2135
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002136 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002137 * Return the {@link Timer} object used to track the given signal strength's duration and
2138 * counts.
2139 */
2140 protected abstract Timer getPhoneSignalStrengthTimer(int strengthBin);
2141
2142 /**
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002143 * Returns the time in microseconds that the mobile network has been active
2144 * (in a high power state).
2145 *
2146 * {@hide}
2147 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002148 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002149
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002150 /**
2151 * Returns the number of times that the mobile network has transitioned to the
2152 * active state.
2153 *
2154 * {@hide}
2155 */
2156 public abstract int getMobileRadioActiveCount(int which);
2157
2158 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002159 * Returns the time in microseconds that is the difference between the mobile radio
2160 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
2161 * from the radio.
2162 *
2163 * {@hide}
2164 */
2165 public abstract long getMobileRadioActiveAdjustedTime(int which);
2166
2167 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002168 * Returns the time in microseconds that the mobile network has been active
2169 * (in a high power state) but not being able to blame on an app.
2170 *
2171 * {@hide}
2172 */
2173 public abstract long getMobileRadioActiveUnknownTime(int which);
2174
2175 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002176 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002177 *
2178 * {@hide}
2179 */
2180 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002181
Dianne Hackborn627bba72009-03-24 22:32:56 -07002182 public static final int DATA_CONNECTION_NONE = 0;
2183 public static final int DATA_CONNECTION_GPRS = 1;
2184 public static final int DATA_CONNECTION_EDGE = 2;
2185 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002186 public static final int DATA_CONNECTION_CDMA = 4;
2187 public static final int DATA_CONNECTION_EVDO_0 = 5;
2188 public static final int DATA_CONNECTION_EVDO_A = 6;
2189 public static final int DATA_CONNECTION_1xRTT = 7;
2190 public static final int DATA_CONNECTION_HSDPA = 8;
2191 public static final int DATA_CONNECTION_HSUPA = 9;
2192 public static final int DATA_CONNECTION_HSPA = 10;
2193 public static final int DATA_CONNECTION_IDEN = 11;
2194 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002195 public static final int DATA_CONNECTION_LTE = 13;
2196 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08002197 public static final int DATA_CONNECTION_HSPAP = 15;
2198 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002199
Dianne Hackborn627bba72009-03-24 22:32:56 -07002200 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002201 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002202 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08002203 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002204 };
Bookatzc8c44962017-05-11 12:12:54 -07002205
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002206 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002207
Dianne Hackborn627bba72009-03-24 22:32:56 -07002208 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002209 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002210 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002211 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002212 * {@hide}
2213 */
2214 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002215 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002218 * Returns the number of times the phone has entered the given data
2219 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002220 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002221 * {@hide}
2222 */
2223 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002224
Kweku Adams87b19ec2017-10-09 12:40:03 -07002225 /**
2226 * Returns the {@link Timer} object that tracks the phone's data connection type stats.
2227 */
2228 public abstract Timer getPhoneDataConnectionTimer(int dataType);
2229
Dianne Hackborn3251b902014-06-20 14:40:53 -07002230 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2231 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2232 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2233 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2234 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2235 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2236 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2237 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2238 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2239 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2240 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2241 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2242 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2243
2244 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2245
2246 static final String[] WIFI_SUPPL_STATE_NAMES = {
2247 "invalid", "disconn", "disabled", "inactive", "scanning",
2248 "authenticating", "associating", "associated", "4-way-handshake",
2249 "group-handshake", "completed", "dormant", "uninit"
2250 };
2251
2252 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2253 "inv", "dsc", "dis", "inact", "scan",
2254 "auth", "ascing", "asced", "4-way",
2255 "group", "compl", "dorm", "uninit"
2256 };
2257
Mike Mac2f518a2017-09-19 16:06:03 -07002258 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002259 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002260 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2261 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002262 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002263 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2264 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2265 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002266 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002267 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002268 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002269 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002270 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2271 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Mike Mac2f518a2017-09-19 16:06:03 -07002272 new BitDescription(HistoryItem.STATE_SCREEN_DOZE_FLAG, "screen_doze", "Sd"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002273 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2274 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2275 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2276 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2277 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2278 new String[] {"in", "out", "emergency", "off"},
2279 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002280 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2281 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2282 SignalStrength.SIGNAL_STRENGTH_NAMES,
2283 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002284 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2285 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2286 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002287 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002288
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002289 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2290 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002291 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002292 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002293 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002294 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002295 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002296 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2297 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2298 new String[] { "off", "light", "full", "???" },
2299 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002300 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2301 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2302 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002303 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2304 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2305 new String[] { "0", "1", "2", "3", "4" },
2306 new String[] { "0", "1", "2", "3", "4" }),
2307 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2308 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2309 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002310 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002311 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002312 };
2313
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002314 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002315 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002316 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2317 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002318 };
2319
2320 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002321 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002322 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002323 "Esw", "Ewa", "Elw", "Eec"
2324 };
2325
2326 @FunctionalInterface
2327 public interface IntToString {
2328 String applyAsString(int val);
2329 }
2330
2331 private static final IntToString sUidToString = UserHandle::formatUid;
2332 private static final IntToString sIntToString = Integer::toString;
2333
2334 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2335 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2336 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2337 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2338 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002339 };
2340
Dianne Hackborn617f8772009-03-31 15:04:46 -07002341 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08002342 * Returns total time for WiFi Multicast Wakelock timer.
2343 * Note that this may be different from the sum of per uid timer values.
2344 *
2345 * {@hide}
2346 */
2347 public abstract long getWifiMulticastWakelockTime(long elapsedRealtimeUs, int which);
2348
2349 /**
2350 * Returns total time for WiFi Multicast Wakelock timer
2351 * Note that this may be different from the sum of per uid timer values.
2352 *
2353 * {@hide}
2354 */
2355 public abstract int getWifiMulticastWakelockCount(int which);
2356
2357 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002358 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002359 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002360 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002361 * {@hide}
2362 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002363 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002364
2365 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002366 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002367 * been in the running state while the device was running on battery.
2368 *
2369 * {@hide}
2370 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002371 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002372
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002373 public static final int WIFI_STATE_OFF = 0;
2374 public static final int WIFI_STATE_OFF_SCANNING = 1;
2375 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2376 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2377 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2378 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2379 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2380 public static final int WIFI_STATE_SOFT_AP = 7;
2381
2382 static final String[] WIFI_STATE_NAMES = {
2383 "off", "scanning", "no_net", "disconn",
2384 "sta", "p2p", "sta_p2p", "soft_ap"
2385 };
2386
2387 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2388
2389 /**
2390 * Returns the time in microseconds that WiFi has been running in the given state.
2391 *
2392 * {@hide}
2393 */
2394 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002395 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002396
2397 /**
2398 * Returns the number of times that WiFi has entered the given state.
2399 *
2400 * {@hide}
2401 */
2402 public abstract int getWifiStateCount(int wifiState, int which);
2403
The Android Open Source Project10592532009-03-18 17:39:46 -07002404 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002405 * Returns the {@link Timer} object that tracks the given WiFi state.
2406 *
2407 * {@hide}
2408 */
2409 public abstract Timer getWifiStateTimer(int wifiState);
2410
2411 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002412 * Returns the time in microseconds that the wifi supplicant has been
2413 * in a given state.
2414 *
2415 * {@hide}
2416 */
2417 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2418
2419 /**
2420 * Returns the number of times that the wifi supplicant has transitioned
2421 * to a given state.
2422 *
2423 * {@hide}
2424 */
2425 public abstract int getWifiSupplStateCount(int state, int which);
2426
Kweku Adams87b19ec2017-10-09 12:40:03 -07002427 /**
2428 * Returns the {@link Timer} object that tracks the given wifi supplicant state.
2429 *
2430 * {@hide}
2431 */
2432 public abstract Timer getWifiSupplStateTimer(int state);
2433
Dianne Hackborn3251b902014-06-20 14:40:53 -07002434 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2435
2436 /**
2437 * Returns the time in microseconds that WIFI has been running with
2438 * the given signal strength.
2439 *
2440 * {@hide}
2441 */
2442 public abstract long getWifiSignalStrengthTime(int strengthBin,
2443 long elapsedRealtimeUs, int which);
2444
2445 /**
2446 * Returns the number of times WIFI has entered the given signal strength.
2447 *
2448 * {@hide}
2449 */
2450 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2451
2452 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002453 * Returns the {@link Timer} object that tracks the given WIFI signal strength.
2454 *
2455 * {@hide}
2456 */
2457 public abstract Timer getWifiSignalStrengthTimer(int strengthBin);
2458
2459 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002460 * Returns the time in microseconds that the flashlight has been on while the device was
2461 * running on battery.
2462 *
2463 * {@hide}
2464 */
2465 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2466
2467 /**
2468 * Returns the number of times that the flashlight has been turned on while the device was
2469 * running on battery.
2470 *
2471 * {@hide}
2472 */
2473 public abstract long getFlashlightOnCount(int which);
2474
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002475 /**
2476 * Returns the time in microseconds that the camera has been on while the device was
2477 * running on battery.
2478 *
2479 * {@hide}
2480 */
2481 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2482
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002483 /**
2484 * Returns the time in microseconds that bluetooth scans were running while the device was
2485 * on battery.
2486 *
2487 * {@hide}
2488 */
2489 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002490
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002491 public static final int NETWORK_MOBILE_RX_DATA = 0;
2492 public static final int NETWORK_MOBILE_TX_DATA = 1;
2493 public static final int NETWORK_WIFI_RX_DATA = 2;
2494 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002495 public static final int NETWORK_BT_RX_DATA = 4;
2496 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002497 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2498 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2499 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2500 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2501 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002502
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002503 public abstract long getNetworkActivityBytes(int type, int which);
2504 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002505
Adam Lesinskie08af192015-03-25 16:42:59 -07002506 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002507 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002508 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002509 * actual power data.
2510 */
2511 public abstract boolean hasWifiActivityReporting();
2512
2513 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002514 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2515 * in various radio controller states, such as transmit, receive, and idle.
2516 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002517 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002518 public abstract ControllerActivityCounter getWifiControllerActivity();
2519
2520 /**
2521 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2522 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2523 * actual power data.
2524 */
2525 public abstract boolean hasBluetoothActivityReporting();
2526
2527 /**
2528 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2529 * in various radio controller states, such as transmit, receive, and idle.
2530 * @return non-null {@link ControllerActivityCounter}
2531 */
2532 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2533
2534 /**
2535 * Returns true if the BatteryStats object has detailed modem power reports.
2536 * When true, calling {@link #getModemControllerActivity()} will yield the
2537 * actual power data.
2538 */
2539 public abstract boolean hasModemActivityReporting();
2540
2541 /**
2542 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2543 * in various radio controller states, such as transmit, receive, and idle.
2544 * @return non-null {@link ControllerActivityCounter}
2545 */
2546 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002547
The Android Open Source Project10592532009-03-18 17:39:46 -07002548 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002549 * Return the wall clock time when battery stats data collection started.
2550 */
2551 public abstract long getStartClockTime();
2552
2553 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002554 * Return platform version tag that we were running in when the battery stats started.
2555 */
2556 public abstract String getStartPlatformVersion();
2557
2558 /**
2559 * Return platform version tag that we were running in when the battery stats ended.
2560 */
2561 public abstract String getEndPlatformVersion();
2562
2563 /**
2564 * Return the internal version code of the parcelled format.
2565 */
2566 public abstract int getParcelVersion();
2567
2568 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 * Return whether we are currently running on battery.
2570 */
2571 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 /**
2574 * Returns a SparseArray containing the statistics for each uid.
2575 */
2576 public abstract SparseArray<? extends Uid> getUidStats();
2577
2578 /**
2579 * Returns the current battery uptime in microseconds.
2580 *
2581 * @param curTime the amount of elapsed realtime in microseconds.
2582 */
2583 public abstract long getBatteryUptime(long curTime);
2584
2585 /**
2586 * Returns the current battery realtime in microseconds.
2587 *
2588 * @param curTime the amount of elapsed realtime in microseconds.
2589 */
2590 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002591
The Android Open Source Project10592532009-03-18 17:39:46 -07002592 /**
Evan Millar633a1742009-04-02 16:36:33 -07002593 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002594 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002595 */
Evan Millar633a1742009-04-02 16:36:33 -07002596 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002597
The Android Open Source Project10592532009-03-18 17:39:46 -07002598 /**
Evan Millar633a1742009-04-02 16:36:33 -07002599 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2600 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002601 */
Evan Millar633a1742009-04-02 16:36:33 -07002602 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603
2604 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002605 * Get the amount the battery has discharged since the stats were
2606 * last reset after charging, as a lower-end approximation.
2607 */
2608 public abstract int getLowDischargeAmountSinceCharge();
2609
2610 /**
2611 * Get the amount the battery has discharged since the stats were
2612 * last reset after charging, as an upper-end approximation.
2613 */
2614 public abstract int getHighDischargeAmountSinceCharge();
2615
2616 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002617 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2618 */
2619 public abstract int getDischargeAmount(int which);
2620
2621 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002622 * Get the amount the battery has discharged while the screen was on,
2623 * since the last time power was unplugged.
2624 */
2625 public abstract int getDischargeAmountScreenOn();
2626
2627 /**
2628 * Get the amount the battery has discharged while the screen was on,
2629 * since the last time the device was charged.
2630 */
2631 public abstract int getDischargeAmountScreenOnSinceCharge();
2632
2633 /**
2634 * Get the amount the battery has discharged while the screen was off,
2635 * since the last time power was unplugged.
2636 */
2637 public abstract int getDischargeAmountScreenOff();
2638
2639 /**
2640 * Get the amount the battery has discharged while the screen was off,
2641 * since the last time the device was charged.
2642 */
2643 public abstract int getDischargeAmountScreenOffSinceCharge();
2644
2645 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002646 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002647 * since the last time power was unplugged.
2648 */
2649 public abstract int getDischargeAmountScreenDoze();
2650
2651 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002652 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002653 * since the last time the device was charged.
2654 */
2655 public abstract int getDischargeAmountScreenDozeSinceCharge();
2656
2657 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 * Returns the total, last, or current battery uptime in microseconds.
2659 *
2660 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002661 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 */
2663 public abstract long computeBatteryUptime(long curTime, int which);
2664
2665 /**
2666 * Returns the total, last, or current battery realtime in microseconds.
2667 *
2668 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002669 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 */
2671 public abstract long computeBatteryRealtime(long curTime, int which);
2672
2673 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002674 * Returns the total, last, or current battery screen off/doze uptime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002675 *
2676 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002677 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002678 */
2679 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2680
2681 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002682 * Returns the total, last, or current battery screen off/doze realtime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002683 *
2684 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002685 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002686 */
2687 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2688
2689 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 * Returns the total, last, or current uptime in microseconds.
2691 *
2692 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002693 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 */
2695 public abstract long computeUptime(long curTime, int which);
2696
2697 /**
2698 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002699 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002701 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 */
2703 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002704
2705 /**
2706 * Compute an approximation for how much run time (in microseconds) is remaining on
2707 * the battery. Returns -1 if no time can be computed: either there is not
2708 * enough current data to make a decision, or the battery is currently
2709 * charging.
2710 *
2711 * @param curTime The current elepsed realtime in microseconds.
2712 */
2713 public abstract long computeBatteryTimeRemaining(long curTime);
2714
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002715 // The part of a step duration that is the actual time.
2716 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2717
2718 // Bits in a step duration that are the new battery level we are at.
2719 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002720 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002721
2722 // Bits in a step duration that are the initial mode we were in at that step.
2723 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002724 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002725
2726 // Bits in a step duration that indicate which modes changed during that step.
2727 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002728 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002729
2730 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2731 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2732
Santos Cordone94f0502017-02-24 12:31:20 -08002733 // The largest value for screen state that is tracked in battery states. Any values above
2734 // this should be mapped back to one of the tracked values before being tracked here.
2735 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2736
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002737 // Step duration mode: power save is on.
2738 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2739
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002740 // Step duration mode: device is currently in idle mode.
2741 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2742
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002743 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2744 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002745 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2746 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002747 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2748 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2749 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2750 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2751 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002752 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2753 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002754 };
2755 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2756 (Display.STATE_OFF-1),
2757 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002758 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002759 (Display.STATE_ON-1),
2760 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2761 (Display.STATE_DOZE-1),
2762 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2763 (Display.STATE_DOZE_SUSPEND-1),
2764 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002765 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002766 };
2767 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2768 "screen off",
2769 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002770 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002771 "screen on",
2772 "screen on power save",
2773 "screen doze",
2774 "screen doze power save",
2775 "screen doze-suspend",
2776 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002777 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002778 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002779
2780 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002781 * Return the amount of battery discharge while the screen was off, measured in
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002782 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2783 * a coulomb counter.
2784 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002785 public abstract long getUahDischargeScreenOff(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002786
2787 /**
2788 * Return the amount of battery discharge while the screen was in doze mode, measured in
2789 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2790 * a coulomb counter.
2791 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002792 public abstract long getUahDischargeScreenDoze(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002793
2794 /**
2795 * Return the amount of battery discharge measured in micro-Ampere-hours. This will be
2796 * non-zero only if the device's battery has a coulomb counter.
2797 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002798 public abstract long getUahDischarge(int which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002799
2800 /**
Mike Ma15313c92017-11-15 17:58:21 -08002801 * @return the amount of battery discharge while the device is in light idle mode, measured in
2802 * micro-Ampere-hours.
2803 */
2804 public abstract long getUahDischargeLightDoze(int which);
2805
2806 /**
2807 * @return the amount of battery discharge while the device is in deep idle mode, measured in
2808 * micro-Ampere-hours.
2809 */
2810 public abstract long getUahDischargeDeepDoze(int which);
2811
2812 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002813 * Returns the estimated real battery capacity, which may be less than the capacity
2814 * declared by the PowerProfile.
2815 * @return The estimated battery capacity in mAh.
2816 */
2817 public abstract int getEstimatedBatteryCapacity();
2818
2819 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002820 * @return The minimum learned battery capacity in uAh.
2821 */
2822 public abstract int getMinLearnedBatteryCapacity();
2823
2824 /**
2825 * @return The maximum learned battery capacity in uAh.
2826 */
2827 public abstract int getMaxLearnedBatteryCapacity() ;
2828
2829 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002830 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002831 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002832 public abstract LevelStepTracker getDischargeLevelStepTracker();
2833
2834 /**
2835 * Return the array of daily discharge step durations.
2836 */
2837 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002838
2839 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002840 * Compute an approximation for how much time (in microseconds) remains until the battery
2841 * is fully charged. Returns -1 if no time can be computed: either there is not
2842 * enough current data to make a decision, or the battery is currently
2843 * discharging.
2844 *
2845 * @param curTime The current elepsed realtime in microseconds.
2846 */
2847 public abstract long computeChargeTimeRemaining(long curTime);
2848
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002849 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002850 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002851 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002852 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002853
2854 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002855 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002856 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002857 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002858
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002859 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2860
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002861 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002862
Evan Millarc64edde2009-04-18 12:26:32 -07002863 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864
Bookatz50df7112017-08-04 14:53:26 -07002865 /**
2866 * Returns Timers tracking the total time of each Resource Power Manager state and voter.
2867 */
2868 public abstract Map<String, ? extends Timer> getRpmStats();
2869 /**
2870 * Returns Timers tracking the screen-off time of each Resource Power Manager state and voter.
2871 */
2872 public abstract Map<String, ? extends Timer> getScreenOffRpmStats();
2873
2874
James Carr2dd7e5e2016-07-20 18:48:39 -07002875 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2876
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002877 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2878
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002879 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 long days = seconds / (60 * 60 * 24);
2881 if (days != 0) {
2882 out.append(days);
2883 out.append("d ");
2884 }
2885 long used = days * 60 * 60 * 24;
2886
2887 long hours = (seconds - used) / (60 * 60);
2888 if (hours != 0 || used != 0) {
2889 out.append(hours);
2890 out.append("h ");
2891 }
2892 used += hours * 60 * 60;
2893
2894 long mins = (seconds-used) / 60;
2895 if (mins != 0 || used != 0) {
2896 out.append(mins);
2897 out.append("m ");
2898 }
2899 used += mins * 60;
2900
2901 if (seconds != 0 || used != 0) {
2902 out.append(seconds-used);
2903 out.append("s ");
2904 }
2905 }
2906
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002907 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002909 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 sb.append(time - (sec * 1000));
2911 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 }
2913
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002914 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002915 long sec = time / 1000;
2916 formatTimeRaw(sb, sec);
2917 sb.append(time - (sec * 1000));
2918 sb.append("ms");
2919 }
2920
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002921 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002923 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 }
2925 float perc = ((float)num) / ((float)den) * 100;
2926 mFormatBuilder.setLength(0);
2927 mFormatter.format("%.1f%%", perc);
2928 return mFormatBuilder.toString();
2929 }
2930
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002931 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002932 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07002933
Evan Millar22ac0432009-03-31 11:33:18 -07002934 if (bytes < BYTES_PER_KB) {
2935 return bytes + "B";
2936 } else if (bytes < BYTES_PER_MB) {
2937 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2938 return mFormatBuilder.toString();
2939 } else if (bytes < BYTES_PER_GB){
2940 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2941 return mFormatBuilder.toString();
2942 } else {
2943 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2944 return mFormatBuilder.toString();
2945 }
2946 }
2947
Kweku Adams103351f2017-10-16 14:39:34 -07002948 private static long roundUsToMs(long timeUs) {
2949 return (timeUs + 500) / 1000;
2950 }
2951
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002952 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002953 if (timer != null) {
2954 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002955 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002956 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2957 return totalTimeMillis;
2958 }
2959 return 0;
2960 }
2961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 /**
2963 *
2964 * @param sb a StringBuilder object.
2965 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002966 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002968 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 * @param linePrefix a String to be prepended to each line of output.
2970 * @return the line prefix
2971 */
2972 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002973 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002976 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002977
Evan Millarc64edde2009-04-18 12:26:32 -07002978 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 if (totalTimeMillis != 0) {
2980 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002981 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002982 if (name != null) {
2983 sb.append(name);
2984 sb.append(' ');
2985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 sb.append('(');
2987 sb.append(count);
2988 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002989 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2990 if (maxDurationMs >= 0) {
2991 sb.append(" max=");
2992 sb.append(maxDurationMs);
2993 }
Bookatz506a8182017-05-01 14:18:42 -07002994 // Put actual time if it is available and different from totalTimeMillis.
2995 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
2996 if (totalDurMs > totalTimeMillis) {
2997 sb.append(" actual=");
2998 sb.append(totalDurMs);
2999 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07003000 if (timer.isRunningLocked()) {
3001 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3002 if (currentMs >= 0) {
3003 sb.append(" (running for ");
3004 sb.append(currentMs);
3005 sb.append("ms)");
3006 } else {
3007 sb.append(" (running)");
3008 }
3009 }
3010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 return ", ";
3012 }
3013 }
3014 return linePrefix;
3015 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003016
3017 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07003018 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003019 *
3020 * @param pw a PrintWriter object to print to.
3021 * @param sb a StringBuilder object.
3022 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08003023 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003024 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
3025 * @param prefix a String to be prepended to each line of output.
3026 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07003027 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003028 */
3029 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07003030 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003031 if (timer != null) {
3032 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07003033 final long totalTimeMs = (timer.getTotalTimeLocked(
3034 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003035 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003036 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003037 sb.setLength(0);
3038 sb.append(prefix);
3039 sb.append(" ");
3040 sb.append(type);
3041 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003042 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003043 sb.append("realtime (");
3044 sb.append(count);
3045 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003046 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
3047 if (maxDurationMs >= 0) {
3048 sb.append(" max=");
3049 sb.append(maxDurationMs);
3050 }
3051 if (timer.isRunningLocked()) {
3052 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
3053 if (currentMs >= 0) {
3054 sb.append(" (running for ");
3055 sb.append(currentMs);
3056 sb.append("ms)");
3057 } else {
3058 sb.append(" (running)");
3059 }
3060 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003061 pw.println(sb.toString());
3062 return true;
3063 }
3064 }
3065 return false;
3066 }
Bookatzc8c44962017-05-11 12:12:54 -07003067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003068 /**
3069 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07003070 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 * @param sb a StringBuilder object.
3072 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003073 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003075 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 * @param linePrefix a String to be prepended to each line of output.
3077 * @return the line prefix
3078 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003079 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
3080 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081 long totalTimeMicros = 0;
3082 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07003083 long max = 0;
3084 long current = 0;
3085 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003087 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07003088 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003089 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3090 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07003091 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 }
3093 sb.append(linePrefix);
3094 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
3095 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07003096 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003098 sb.append(',');
3099 sb.append(current);
3100 sb.append(',');
3101 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07003102 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
3103 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
3104 // totalDuration independent of totalTimeMicros (since they are not pooled).
3105 if (name != null) {
3106 sb.append(',');
3107 sb.append(totalDuration);
3108 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 return ",";
3110 }
Bookatz506a8182017-05-01 14:18:42 -07003111
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003112 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
3113 String type) {
3114 pw.print(BATTERY_STATS_CHECKIN_VERSION);
3115 pw.print(',');
3116 pw.print(uid);
3117 pw.print(',');
3118 pw.print(category);
3119 pw.print(',');
3120 pw.print(type);
3121 }
3122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 /**
3124 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07003125 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 * @param pw the PageWriter to dump log to
3127 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3128 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3129 * @param args type-dependent data arguments
3130 */
Bookatzc8c44962017-05-11 12:12:54 -07003131 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003133 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003134 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003135 pw.print(',');
3136 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003138 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07003140
3141 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003142 * Dump a given timer stat for terse checkin mode.
3143 *
3144 * @param pw the PageWriter to dump log to
3145 * @param uid the UID to log
3146 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3147 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3148 * @param timer a {@link Timer} to dump stats for
3149 * @param rawRealtime 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 final void dumpTimer(PrintWriter pw, int uid, String category, String type,
3153 Timer timer, long rawRealtime, int which) {
3154 if (timer != null) {
3155 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003156 final long totalTime = roundUsToMs(timer.getTotalTimeLocked(rawRealtime, which));
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003157 final int count = timer.getCountLocked(which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003158 if (totalTime != 0 || count != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003159 dumpLine(pw, uid, category, type, totalTime, count);
3160 }
3161 }
3162 }
3163
3164 /**
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003165 * Dump a given timer stat to the proto stream.
3166 *
3167 * @param proto the ProtoOutputStream to log to
3168 * @param fieldId type of data, the field to save to (e.g. AggregatedBatteryStats.WAKELOCK)
3169 * @param timer a {@link Timer} to dump stats for
3170 * @param rawRealtimeUs the current elapsed realtime of the system in microseconds
3171 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3172 */
3173 private static void dumpTimer(ProtoOutputStream proto, long fieldId,
Kweku Adams87b19ec2017-10-09 12:40:03 -07003174 Timer timer, long rawRealtimeUs, int which) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003175 if (timer == null) {
3176 return;
3177 }
3178 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003179 final long timeMs = roundUsToMs(timer.getTotalTimeLocked(rawRealtimeUs, which));
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003180 final int count = timer.getCountLocked(which);
Kweku Adams103351f2017-10-16 14:39:34 -07003181 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs / 1000);
3182 final long curDurationMs = timer.getCurrentDurationMsLocked(rawRealtimeUs / 1000);
3183 final long totalDurationMs = timer.getTotalDurationMsLocked(rawRealtimeUs / 1000);
3184 if (timeMs != 0 || count != 0 || maxDurationMs != -1 || curDurationMs != -1
3185 || totalDurationMs != -1) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003186 final long token = proto.start(fieldId);
Kweku Adams103351f2017-10-16 14:39:34 -07003187 proto.write(TimerProto.DURATION_MS, timeMs);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003188 proto.write(TimerProto.COUNT, count);
Kweku Adams103351f2017-10-16 14:39:34 -07003189 // These values will be -1 for timers that don't implement the functionality.
3190 if (maxDurationMs != -1) {
3191 proto.write(TimerProto.MAX_DURATION_MS, maxDurationMs);
3192 }
3193 if (curDurationMs != -1) {
3194 proto.write(TimerProto.CURRENT_DURATION_MS, curDurationMs);
3195 }
3196 if (totalDurationMs != -1) {
3197 proto.write(TimerProto.TOTAL_DURATION_MS, totalDurationMs);
3198 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003199 proto.end(token);
3200 }
3201 }
3202
3203 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003204 * Checks if the ControllerActivityCounter has any data worth dumping.
3205 */
3206 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
3207 if (counter == null) {
3208 return false;
3209 }
3210
3211 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
3212 || counter.getRxTimeCounter().getCountLocked(which) != 0
3213 || counter.getPowerCounter().getCountLocked(which) != 0) {
3214 return true;
3215 }
3216
3217 for (LongCounter c : counter.getTxTimeCounters()) {
3218 if (c.getCountLocked(which) != 0) {
3219 return true;
3220 }
3221 }
3222 return false;
3223 }
3224
3225 /**
3226 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3227 * The order of the arguments in the final check in line is:
3228 *
3229 * idle, rx, power, tx...
3230 *
3231 * where tx... is one or more transmit level times.
3232 */
3233 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
3234 String type,
3235 ControllerActivityCounter counter,
3236 int which) {
3237 if (!controllerActivityHasData(counter, which)) {
3238 return;
3239 }
3240
3241 dumpLineHeader(pw, uid, category, type);
3242 pw.print(",");
3243 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
3244 pw.print(",");
3245 pw.print(counter.getRxTimeCounter().getCountLocked(which));
3246 pw.print(",");
3247 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3248 for (LongCounter c : counter.getTxTimeCounters()) {
3249 pw.print(",");
3250 pw.print(c.getCountLocked(which));
3251 }
3252 pw.println();
3253 }
3254
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003255 /**
3256 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3257 */
3258 private static void dumpControllerActivityProto(ProtoOutputStream proto, long fieldId,
3259 ControllerActivityCounter counter,
3260 int which) {
3261 if (!controllerActivityHasData(counter, which)) {
3262 return;
3263 }
3264
3265 final long cToken = proto.start(fieldId);
3266
3267 proto.write(ControllerActivityProto.IDLE_DURATION_MS,
3268 counter.getIdleTimeCounter().getCountLocked(which));
3269 proto.write(ControllerActivityProto.RX_DURATION_MS,
3270 counter.getRxTimeCounter().getCountLocked(which));
3271 proto.write(ControllerActivityProto.POWER_MAH,
3272 counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3273
3274 long tToken;
3275 LongCounter[] txCounters = counter.getTxTimeCounters();
3276 for (int i = 0; i < txCounters.length; ++i) {
3277 LongCounter c = txCounters[i];
3278 tToken = proto.start(ControllerActivityProto.TX);
3279 proto.write(ControllerActivityProto.TxLevel.LEVEL, i);
3280 proto.write(ControllerActivityProto.TxLevel.DURATION_MS, c.getCountLocked(which));
3281 proto.end(tToken);
3282 }
3283
3284 proto.end(cToken);
3285 }
3286
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003287 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
3288 String prefix, String controllerName,
3289 ControllerActivityCounter counter,
3290 int which) {
3291 if (controllerActivityHasData(counter, which)) {
3292 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
3293 }
3294 }
3295
3296 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
3297 String controllerName,
3298 ControllerActivityCounter counter, int which) {
3299 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
3300 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
3301 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003302 // Battery real time
3303 final long totalControllerActivityTimeMs
3304 = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003305 long totalTxTimeMs = 0;
3306 for (LongCounter txState : counter.getTxTimeCounters()) {
3307 totalTxTimeMs += txState.getCountLocked(which);
3308 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003309 final long sleepTimeMs
3310 = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + totalTxTimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003311
3312 sb.setLength(0);
3313 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003314 sb.append(" ");
3315 sb.append(controllerName);
3316 sb.append(" Sleep time: ");
3317 formatTimeMs(sb, sleepTimeMs);
3318 sb.append("(");
3319 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3320 sb.append(")");
3321 pw.println(sb.toString());
3322
3323 sb.setLength(0);
3324 sb.append(prefix);
3325 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003326 sb.append(controllerName);
3327 sb.append(" Idle time: ");
3328 formatTimeMs(sb, idleTimeMs);
3329 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003330 sb.append(formatRatioLocked(idleTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003331 sb.append(")");
3332 pw.println(sb.toString());
3333
3334 sb.setLength(0);
3335 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003336 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003337 sb.append(controllerName);
3338 sb.append(" Rx time: ");
3339 formatTimeMs(sb, rxTimeMs);
3340 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003341 sb.append(formatRatioLocked(rxTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003342 sb.append(")");
3343 pw.println(sb.toString());
3344
3345 sb.setLength(0);
3346 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003347 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003348 sb.append(controllerName);
3349 sb.append(" Tx time: ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003350
Siddharth Ray3c648c42017-10-02 17:30:58 -07003351 String [] powerLevel;
3352 switch(controllerName) {
3353 case "Cellular":
3354 powerLevel = new String[] {
3355 " less than 0dBm: ",
3356 " 0dBm to 8dBm: ",
3357 " 8dBm to 15dBm: ",
3358 " 15dBm to 20dBm: ",
3359 " above 20dBm: "};
3360 break;
3361 default:
3362 powerLevel = new String[] {"[0]", "[1]", "[2]", "[3]", "[4]"};
3363 break;
3364 }
3365 final int numTxLvls = Math.min(counter.getTxTimeCounters().length, powerLevel.length);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003366 if (numTxLvls > 1) {
Siddharth Ray3c648c42017-10-02 17:30:58 -07003367 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003368 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3369 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3370 sb.setLength(0);
3371 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003372 sb.append(" ");
3373 sb.append(powerLevel[lvl]);
3374 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003375 formatTimeMs(sb, txLvlTimeMs);
3376 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003377 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003378 sb.append(")");
3379 pw.println(sb.toString());
3380 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003381 } else {
3382 final long txLvlTimeMs = counter.getTxTimeCounters()[0].getCountLocked(which);
3383 formatTimeMs(sb, txLvlTimeMs);
3384 sb.append("(");
3385 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
3386 sb.append(")");
3387 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003388 }
3389
Siddharth Ray3c648c42017-10-02 17:30:58 -07003390 if (powerDrainMaMs > 0) {
3391 sb.setLength(0);
3392 sb.append(prefix);
3393 sb.append(" ");
3394 sb.append(controllerName);
3395 sb.append(" Battery drain: ").append(
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003396 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
Siddharth Ray3c648c42017-10-02 17:30:58 -07003397 sb.append("mAh");
3398 pw.println(sb.toString());
3399 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003400 }
3401
3402 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003403 * Temporary for settings.
3404 */
3405 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3406 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3407 }
3408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 /**
3410 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003411 *
Kweku Adams87b19ec2017-10-09 12:40:03 -07003412 * NOTE: all times are expressed in microseconds, unless specified otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003414 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3415 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416 final long rawUptime = SystemClock.uptimeMillis() * 1000;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003417 final long rawRealtimeMs = SystemClock.elapsedRealtime();
3418 final long rawRealtime = rawRealtimeMs * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003419 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003420 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3421 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003422 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3423 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3424 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003425 final long totalRealtime = computeRealtime(rawRealtime, which);
3426 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003427 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Mike Mac2f518a2017-09-19 16:06:03 -07003428 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003429 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003430 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003431 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3432 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003433 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003434 rawRealtime, which);
3435 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3436 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003437 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003438 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003439 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003440 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003441 final long dischargeCount = getUahDischarge(which);
3442 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
3443 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Ma15313c92017-11-15 17:58:21 -08003444 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
3445 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003446
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003447 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003448
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003449 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003450 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003451
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003452 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003454 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003455 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003456 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003457 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003458 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003459 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003460 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003461 getEstimatedBatteryCapacity(),
Mike Mac2f518a2017-09-19 16:06:03 -07003462 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity(),
3463 screenDozeTime / 1000);
Adam Lesinski67c134f2016-06-10 15:15:08 -07003464
Bookatzc8c44962017-05-11 12:12:54 -07003465
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003466 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003467 long fullWakeLockTimeTotal = 0;
3468 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003469
Evan Millar22ac0432009-03-31 11:33:18 -07003470 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003471 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003472
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003473 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3474 = u.getWakelockStats();
3475 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3476 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003477
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003478 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3479 if (fullWakeTimer != null) {
3480 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3481 which);
3482 }
3483
3484 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3485 if (partialWakeTimer != null) {
3486 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3487 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003488 }
3489 }
3490 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003491
3492 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003493 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3494 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3495 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3496 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3497 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3498 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3499 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3500 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003501 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3502 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003503 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3504 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003505 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3506 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003507
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003508 // Dump Modem controller stats
3509 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3510 getModemControllerActivity(), which);
3511
Adam Lesinskie283d332015-04-16 12:29:25 -07003512 // Dump Wifi controller stats
3513 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3514 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003515 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003516 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003517
3518 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3519 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003520
3521 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003522 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3523 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 // Dump misc stats
3526 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003527 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003528 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003529 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003530 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003531 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003532 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3533 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003534 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003535 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3536 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3537 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3538 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003539 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003540
Dianne Hackborn617f8772009-03-31 15:04:46 -07003541 // Dump screen brightness stats
3542 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3543 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003544 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003545 }
3546 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003547
Dianne Hackborn627bba72009-03-24 22:32:56 -07003548 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003549 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3550 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003551 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003552 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003553 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003554 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003555 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003556 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003557 args[i] = getPhoneSignalStrengthCount(i, which);
3558 }
3559 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003560
Dianne Hackborn627bba72009-03-24 22:32:56 -07003561 // Dump network type stats
3562 args = new Object[NUM_DATA_CONNECTION_TYPES];
3563 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003564 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003565 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003566 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3567 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3568 args[i] = getPhoneDataConnectionCount(i, which);
3569 }
3570 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003571
3572 // Dump wifi state stats
3573 args = new Object[NUM_WIFI_STATES];
3574 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003575 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003576 }
3577 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3578 for (int i=0; i<NUM_WIFI_STATES; i++) {
3579 args[i] = getWifiStateCount(i, which);
3580 }
3581 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3582
Dianne Hackborn3251b902014-06-20 14:40:53 -07003583 // Dump wifi suppl state stats
3584 args = new Object[NUM_WIFI_SUPPL_STATES];
3585 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3586 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3587 }
3588 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3589 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3590 args[i] = getWifiSupplStateCount(i, which);
3591 }
3592 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3593
3594 // Dump wifi signal strength stats
3595 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3596 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3597 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3598 }
3599 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3600 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3601 args[i] = getWifiSignalStrengthCount(i, which);
3602 }
3603 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3604
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003605 // Dump Multicast total stats
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003606 final long multicastWakeLockTimeTotalMicros =
3607 getWifiMulticastWakelockTime(rawRealtime, which);
3608 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003609 dumpLine(pw, 0 /* uid */, category, WIFI_MULTICAST_TOTAL_DATA,
3610 multicastWakeLockTimeTotalMicros / 1000,
3611 multicastWakeLockCountTotal);
3612
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003613 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003614 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003615 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003616 }
Bookatzc8c44962017-05-11 12:12:54 -07003617
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003618 if (which == STATS_SINCE_UNPLUGGED) {
3619 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3620 getDischargeStartLevel()-getDischargeCurrentLevel(),
3621 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003622 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
Mike Mac2f518a2017-09-19 16:06:03 -07003623 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003624 getDischargeAmountScreenDoze(), dischargeScreenDozeCount / 1000,
3625 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003626 } else {
3627 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3628 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003629 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003630 getDischargeAmountScreenOffSinceCharge(),
Mike Mac2f518a2017-09-19 16:06:03 -07003631 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003632 getDischargeAmountScreenDozeSinceCharge(), dischargeScreenDozeCount / 1000,
3633 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003634 }
Bookatzc8c44962017-05-11 12:12:54 -07003635
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003636 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003637 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003638 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003639 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003640 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003641 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003642 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3643 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003644 }
Evan Millarc64edde2009-04-18 12:26:32 -07003645 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003646 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003647 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003648 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3649 // Not doing the regular wake lock formatting to remain compatible
3650 // with the old checkin format.
3651 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3652 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003653 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003654 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003655 }
3656 }
Evan Millarc64edde2009-04-18 12:26:32 -07003657 }
Bookatzc8c44962017-05-11 12:12:54 -07003658
Bookatz50df7112017-08-04 14:53:26 -07003659 final Map<String, ? extends Timer> rpmStats = getRpmStats();
3660 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
3661 if (rpmStats.size() > 0) {
3662 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
3663 sb.setLength(0);
3664 Timer totalTimer = ent.getValue();
3665 long timeMs = (totalTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3666 int count = totalTimer.getCountLocked(which);
3667 Timer screenOffTimer = screenOffRpmStats.get(ent.getKey());
3668 long screenOffTimeMs = screenOffTimer != null
3669 ? (screenOffTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3670 int screenOffCount = screenOffTimer != null
3671 ? screenOffTimer.getCountLocked(which) : 0;
Bookatz82b341172017-09-07 19:06:08 -07003672 if (SCREEN_OFF_RPM_STATS_ENABLED) {
3673 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3674 "\"" + ent.getKey() + "\"", timeMs, count, screenOffTimeMs,
3675 screenOffCount);
3676 } else {
3677 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3678 "\"" + ent.getKey() + "\"", timeMs, count);
3679 }
Bookatz50df7112017-08-04 14:53:26 -07003680 }
3681 }
3682
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003683 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003684 helper.create(this);
3685 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003686 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003687 if (sippers != null && sippers.size() > 0) {
3688 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3689 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003690 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003691 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3692 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
Kweku Adams87b19ec2017-10-09 12:40:03 -07003693 int uid = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003694 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003695 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003696 String label;
3697 switch (bs.drainType) {
3698 case IDLE:
3699 label="idle";
3700 break;
3701 case CELL:
3702 label="cell";
3703 break;
3704 case PHONE:
3705 label="phone";
3706 break;
3707 case WIFI:
3708 label="wifi";
3709 break;
3710 case BLUETOOTH:
3711 label="blue";
3712 break;
3713 case SCREEN:
3714 label="scrn";
3715 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003716 case FLASHLIGHT:
3717 label="flashlight";
3718 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003719 case APP:
3720 uid = bs.uidObj.getUid();
3721 label = "uid";
3722 break;
3723 case USER:
3724 uid = UserHandle.getUid(bs.userId, 0);
3725 label = "user";
3726 break;
3727 case UNACCOUNTED:
3728 label = "unacc";
3729 break;
3730 case OVERCOUNTED:
3731 label = "over";
3732 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003733 case CAMERA:
3734 label = "camera";
3735 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003736 case MEMORY:
3737 label = "memory";
3738 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003739 default:
3740 label = "???";
3741 }
3742 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07003743 BatteryStatsHelper.makemAh(bs.totalPowerMah),
3744 bs.shouldHide ? 1 : 0,
3745 BatteryStatsHelper.makemAh(bs.screenPowerMah),
3746 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003747 }
3748 }
3749
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003750 final long[] cpuFreqs = getCpuFreqs();
3751 if (cpuFreqs != null) {
3752 sb.setLength(0);
3753 for (int i = 0; i < cpuFreqs.length; ++i) {
3754 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3755 }
3756 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3757 }
3758
Kweku Adams87b19ec2017-10-09 12:40:03 -07003759 // Dump stats per UID.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003760 for (int iu = 0; iu < NU; iu++) {
3761 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003762 if (reqUid >= 0 && uid != reqUid) {
3763 continue;
3764 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003765 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003767 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003768 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3769 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3770 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3771 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3772 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3773 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3774 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3775 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003776 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003777 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3778 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003779 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003780 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3781 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003782 // Background data transfers
3783 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3784 which);
3785 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3786 which);
3787 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3788 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3789 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3790 which);
3791 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3792 which);
3793 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3794 which);
3795 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3796 which);
3797
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003798 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3799 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003800 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003801 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3802 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3803 || wifiBytesBgTx > 0
3804 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3805 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003806 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3807 wifiBytesRx, wifiBytesTx,
3808 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003809 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003810 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003811 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3812 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3813 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3814 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003815 }
3816
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003817 // Dump modem controller data, per UID.
3818 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3819 u.getModemControllerActivity(), which);
3820
3821 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003822 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3823 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3824 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003825 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3826 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003827 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3828 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3829 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003830 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003831 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003832 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3833 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003834 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3835 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003836 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003837 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003840 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3841 u.getWifiControllerActivity(), which);
3842
Bookatz867c0d72017-03-07 18:23:42 -08003843 final Timer bleTimer = u.getBluetoothScanTimer();
3844 if (bleTimer != null) {
3845 // Convert from microseconds to milliseconds with rounding
3846 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3847 / 1000;
3848 if (totalTime != 0) {
3849 final int count = bleTimer.getCountLocked(which);
3850 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3851 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003852 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3853 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3854 final long actualTimeBg = bleTimerBg != null ?
3855 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003856 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003857 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3858 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003859 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3860 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3861 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3862 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3863 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
3864 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3865 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
3866 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3867 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
3868 final Timer unoptimizedScanTimerBg =
3869 u.getBluetoothUnoptimizedScanBackgroundTimer();
3870 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
3871 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3872 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
3873 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3874
Bookatz867c0d72017-03-07 18:23:42 -08003875 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07003876 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
3877 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
3878 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08003879 }
3880 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003881
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003882 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3883 u.getBluetoothControllerActivity(), which);
3884
Dianne Hackborn617f8772009-03-31 15:04:46 -07003885 if (u.hasUserActivity()) {
3886 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3887 boolean hasData = false;
3888 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3889 int val = u.getUserActivityCount(i, which);
3890 args[i] = val;
3891 if (val != 0) hasData = true;
3892 }
3893 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003894 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003895 }
3896 }
Bookatzc8c44962017-05-11 12:12:54 -07003897
3898 if (u.getAggregatedPartialWakelockTimer() != null) {
3899 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07003900 // Times are since reset (regardless of 'which')
3901 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07003902 final Timer bgTimer = timer.getSubTimer();
3903 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003904 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07003905 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
3906 }
3907
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003908 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3909 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3910 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3911 String linePrefix = "";
3912 sb.setLength(0);
3913 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3914 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003915 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3916 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003917 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003918 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
3919 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003920 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3921 rawRealtime, "w", which, linePrefix);
3922
Kweku Adams103351f2017-10-16 14:39:34 -07003923 // Only log if we had at least one wakelock...
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003924 if (sb.length() > 0) {
3925 String name = wakelocks.keyAt(iw);
3926 if (name.indexOf(',') >= 0) {
3927 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003928 }
Yi Jin02483362017-08-04 11:30:44 -07003929 if (name.indexOf('\n') >= 0) {
3930 name = name.replace('\n', '_');
3931 }
3932 if (name.indexOf('\r') >= 0) {
3933 name = name.replace('\r', '_');
3934 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003935 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 }
3937 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003938
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003939 // WiFi Multicast Wakelock Statistics
3940 final Timer mcTimer = u.getMulticastWakelockStats();
3941 if (mcTimer != null) {
3942 final long totalMcWakelockTimeMs =
3943 mcTimer.getTotalTimeLocked(rawRealtime, which) / 1000 ;
3944 final int countMcWakelock = mcTimer.getCountLocked(which);
3945 if(totalMcWakelockTimeMs > 0) {
3946 dumpLine(pw, uid, category, WIFI_MULTICAST_DATA,
3947 totalMcWakelockTimeMs, countMcWakelock);
3948 }
3949 }
3950
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003951 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3952 for (int isy=syncs.size()-1; isy>=0; isy--) {
3953 final Timer timer = syncs.valueAt(isy);
3954 // Convert from microseconds to milliseconds with rounding
3955 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3956 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07003957 final Timer bgTimer = timer.getSubTimer();
3958 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003959 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07003960 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003961 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003962 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07003963 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003964 }
3965 }
3966
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003967 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3968 for (int ij=jobs.size()-1; ij>=0; ij--) {
3969 final Timer timer = jobs.valueAt(ij);
3970 // Convert from microseconds to milliseconds with rounding
3971 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3972 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07003973 final Timer bgTimer = timer.getSubTimer();
3974 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003975 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07003976 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003977 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003978 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07003979 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003980 }
3981 }
3982
Dianne Hackborn94326cb2017-06-28 16:17:20 -07003983 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
3984 for (int ic=completions.size()-1; ic>=0; ic--) {
3985 SparseIntArray types = completions.valueAt(ic);
3986 if (types != null) {
3987 dumpLine(pw, uid, category, JOB_COMPLETION_DATA,
3988 "\"" + completions.keyAt(ic) + "\"",
3989 types.get(JobParameters.REASON_CANCELED, 0),
3990 types.get(JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, 0),
3991 types.get(JobParameters.REASON_PREEMPT, 0),
3992 types.get(JobParameters.REASON_TIMEOUT, 0),
3993 types.get(JobParameters.REASON_DEVICE_IDLE, 0));
3994 }
3995 }
3996
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003997 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3998 rawRealtime, which);
3999 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
4000 rawRealtime, which);
4001 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
4002 rawRealtime, which);
4003 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
4004 rawRealtime, which);
4005
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004006 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4007 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004008 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004009 final Uid.Sensor se = sensors.valueAt(ise);
4010 final int sensorNumber = sensors.keyAt(ise);
4011 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004012 if (timer != null) {
4013 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004014 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4015 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004016 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004017 final int count = timer.getCountLocked(which);
4018 final Timer bgTimer = se.getSensorBackgroundTime();
4019 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004020 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4021 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4022 final long bgActualTime = bgTimer != null ?
4023 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4024 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
4025 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004026 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004027 }
4028 }
4029
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004030 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
4031 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004032
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07004033 dumpTimer(pw, uid, category, FOREGROUND_ACTIVITY_DATA, u.getForegroundActivityTimer(),
4034 rawRealtime, which);
4035
4036 dumpTimer(pw, uid, category, FOREGROUND_SERVICE_DATA, u.getForegroundServiceTimer(),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004037 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004038
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004039 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07004040 long totalStateTime = 0;
4041 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08004042 final long time = u.getProcessStateTime(ips, rawRealtime, which);
4043 totalStateTime += time;
4044 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004045 }
4046 if (totalStateTime > 0) {
4047 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
4048 }
4049
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004050 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4051 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004052 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004053 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004054 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004055 }
4056
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004057 // If the cpuFreqs is null, then don't bother checking for cpu freq times.
4058 if (cpuFreqs != null) {
4059 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
4060 // If total cpuFreqTimes is null, then we don't need to check for
4061 // screenOffCpuFreqTimes.
4062 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
4063 sb.setLength(0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004064 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004065 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004066 }
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004067 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
4068 if (screenOffCpuFreqTimeMs != null) {
4069 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
4070 sb.append("," + screenOffCpuFreqTimeMs[i]);
4071 }
4072 } else {
4073 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
4074 sb.append(",0");
4075 }
4076 }
4077 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
4078 cpuFreqTimeMs.length, sb.toString());
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004079 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -08004080
4081 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
4082 final long[] timesMs = u.getCpuFreqTimes(which, procState);
4083 if (timesMs != null && timesMs.length == cpuFreqs.length) {
4084 sb.setLength(0);
4085 for (int i = 0; i < timesMs.length; ++i) {
4086 sb.append((i == 0 ? "" : ",") + timesMs[i]);
4087 }
4088 final long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(
4089 which, procState);
4090 if (screenOffTimesMs != null) {
4091 for (int i = 0; i < screenOffTimesMs.length; ++i) {
4092 sb.append("," + screenOffTimesMs[i]);
4093 }
4094 } else {
4095 for (int i = 0; i < timesMs.length; ++i) {
4096 sb.append(",0");
4097 }
4098 }
4099 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA,
4100 Uid.UID_PROCESS_TYPES[procState], timesMs.length, sb.toString());
4101 }
4102 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004103 }
4104
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004105 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4106 = u.getProcessStats();
4107 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4108 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004109
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004110 final long userMillis = ps.getUserTime(which);
4111 final long systemMillis = ps.getSystemTime(which);
4112 final long foregroundMillis = ps.getForegroundTime(which);
4113 final int starts = ps.getStarts(which);
4114 final int numCrashes = ps.getNumCrashes(which);
4115 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004116
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004117 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
4118 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004119 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
4120 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004121 }
4122 }
4123
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004124 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4125 = u.getPackageStats();
4126 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4127 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4128 int wakeups = 0;
4129 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4130 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07004131 int count = alarms.valueAt(iwa).getCountLocked(which);
4132 wakeups += count;
4133 String name = alarms.keyAt(iwa).replace(',', '_');
4134 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004135 }
4136 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4137 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4138 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4139 final long startTime = ss.getStartTime(batteryUptime, which);
4140 final int starts = ss.getStarts(which);
4141 final int launches = ss.getLaunches(which);
4142 if (startTime != 0 || starts != 0 || launches != 0) {
4143 dumpLine(pw, uid, category, APK_DATA,
4144 wakeups, // wakeup alarms
4145 packageStats.keyAt(ipkg), // Apk
4146 serviceStats.keyAt(isvc), // service
4147 startTime / 1000, // time spent started, in ms
4148 starts,
4149 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 }
4151 }
4152 }
4153 }
4154 }
4155
Dianne Hackborn81038902012-11-26 17:04:09 -08004156 static final class TimerEntry {
4157 final String mName;
4158 final int mId;
4159 final BatteryStats.Timer mTimer;
4160 final long mTime;
4161 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
4162 mName = name;
4163 mId = id;
4164 mTimer = timer;
4165 mTime = time;
4166 }
4167 }
4168
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004169 private void printmAh(PrintWriter printer, double power) {
4170 printer.print(BatteryStatsHelper.makemAh(power));
4171 }
4172
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004173 private void printmAh(StringBuilder sb, double power) {
4174 sb.append(BatteryStatsHelper.makemAh(power));
4175 }
4176
Dianne Hackbornd953c532014-08-16 18:17:38 -07004177 /**
4178 * Temporary for settings.
4179 */
4180 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
4181 int reqUid) {
4182 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
4183 }
4184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004185 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004186 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07004187 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004188 final long rawUptime = SystemClock.uptimeMillis() * 1000;
4189 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07004190 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004191 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004192
4193 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
4194 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
4195 final long totalRealtime = computeRealtime(rawRealtime, which);
4196 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004197 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
4198 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
4199 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004200 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
4201 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -07004202 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004203
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004204 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07004205
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004206 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07004207 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004208
Adam Lesinskif9b20a92016-06-17 17:30:01 -07004209 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
4210 if (estimatedBatteryCapacity > 0) {
4211 sb.setLength(0);
4212 sb.append(prefix);
4213 sb.append(" Estimated battery capacity: ");
4214 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
4215 sb.append(" mAh");
4216 pw.println(sb.toString());
4217 }
4218
Jocelyn Dangc627d102017-04-14 13:15:14 -07004219 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
4220 if (minLearnedBatteryCapacity > 0) {
4221 sb.setLength(0);
4222 sb.append(prefix);
4223 sb.append(" Min learned battery capacity: ");
4224 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
4225 sb.append(" mAh");
4226 pw.println(sb.toString());
4227 }
4228 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
4229 if (maxLearnedBatteryCapacity > 0) {
4230 sb.setLength(0);
4231 sb.append(prefix);
4232 sb.append(" Max learned battery capacity: ");
4233 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
4234 sb.append(" mAh");
4235 pw.println(sb.toString());
4236 }
4237
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004238 sb.setLength(0);
4239 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004240 sb.append(" Time on battery: ");
4241 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
4242 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
4243 sb.append(") realtime, ");
4244 formatTimeMs(sb, whichBatteryUptime / 1000);
4245 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, whichBatteryRealtime));
4246 sb.append(") uptime");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004247 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004248
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004249 sb.setLength(0);
4250 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004251 sb.append(" Time on battery screen off: ");
4252 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
4253 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, whichBatteryRealtime));
4254 sb.append(") realtime, ");
4255 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
4256 sb.append("(");
4257 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, whichBatteryRealtime));
4258 sb.append(") uptime");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004259 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004260
4261 sb.setLength(0);
4262 sb.append(prefix);
4263 sb.append(" Time on battery screen doze: ");
4264 formatTimeMs(sb, screenDozeTime / 1000); sb.append("(");
4265 sb.append(formatRatioLocked(screenDozeTime, whichBatteryRealtime));
4266 sb.append(")");
4267 pw.println(sb.toString());
4268
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004269 sb.setLength(0);
4270 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004271 sb.append(" Total run time: ");
4272 formatTimeMs(sb, totalRealtime / 1000);
4273 sb.append("realtime, ");
4274 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004275 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07004276 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004277 if (batteryTimeRemaining >= 0) {
4278 sb.setLength(0);
4279 sb.append(prefix);
4280 sb.append(" Battery time remaining: ");
4281 formatTimeMs(sb, batteryTimeRemaining / 1000);
4282 pw.println(sb.toString());
4283 }
4284 if (chargeTimeRemaining >= 0) {
4285 sb.setLength(0);
4286 sb.append(prefix);
4287 sb.append(" Charge time remaining: ");
4288 formatTimeMs(sb, chargeTimeRemaining / 1000);
4289 pw.println(sb.toString());
4290 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004291
Kweku Adams87b19ec2017-10-09 12:40:03 -07004292 final long dischargeCount = getUahDischarge(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004293 if (dischargeCount >= 0) {
4294 sb.setLength(0);
4295 sb.append(prefix);
4296 sb.append(" Discharge: ");
4297 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
4298 sb.append(" mAh");
4299 pw.println(sb.toString());
4300 }
4301
Kweku Adams87b19ec2017-10-09 12:40:03 -07004302 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004303 if (dischargeScreenOffCount >= 0) {
4304 sb.setLength(0);
4305 sb.append(prefix);
4306 sb.append(" Screen off discharge: ");
4307 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
4308 sb.append(" mAh");
4309 pw.println(sb.toString());
4310 }
4311
Kweku Adams87b19ec2017-10-09 12:40:03 -07004312 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Mac2f518a2017-09-19 16:06:03 -07004313 if (dischargeScreenDozeCount >= 0) {
4314 sb.setLength(0);
4315 sb.append(prefix);
4316 sb.append(" Screen doze discharge: ");
4317 sb.append(BatteryStatsHelper.makemAh(dischargeScreenDozeCount / 1000.0));
4318 sb.append(" mAh");
4319 pw.println(sb.toString());
4320 }
4321
4322 final long dischargeScreenOnCount =
4323 dischargeCount - dischargeScreenOffCount - dischargeScreenDozeCount;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004324 if (dischargeScreenOnCount >= 0) {
4325 sb.setLength(0);
4326 sb.append(prefix);
4327 sb.append(" Screen on discharge: ");
4328 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
4329 sb.append(" mAh");
4330 pw.println(sb.toString());
4331 }
4332
Mike Ma15313c92017-11-15 17:58:21 -08004333 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
4334 if (dischargeLightDozeCount >= 0) {
4335 sb.setLength(0);
4336 sb.append(prefix);
4337 sb.append(" Device light doze discharge: ");
4338 sb.append(BatteryStatsHelper.makemAh(dischargeLightDozeCount / 1000.0));
4339 sb.append(" mAh");
4340 pw.println(sb.toString());
4341 }
4342
4343 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
4344 if (dischargeDeepDozeCount >= 0) {
4345 sb.setLength(0);
4346 sb.append(prefix);
4347 sb.append(" Device deep doze discharge: ");
4348 sb.append(BatteryStatsHelper.makemAh(dischargeDeepDozeCount / 1000.0));
4349 sb.append(" mAh");
4350 pw.println(sb.toString());
4351 }
4352
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004353 pw.print(" Start clock time: ");
4354 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
4355
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004356 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004357 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004358 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004359 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
4360 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004361 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004362 rawRealtime, which);
4363 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
4364 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004365 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004366 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004367 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
4368 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
4369 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004370 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004371 sb.append(prefix);
4372 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
4373 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004374 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004375 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
4376 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004377 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004378 pw.println(sb.toString());
4379 sb.setLength(0);
4380 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004381 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004382 boolean didOne = false;
4383 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004384 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004385 if (time == 0) {
4386 continue;
4387 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004388 sb.append("\n ");
4389 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004390 didOne = true;
4391 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
4392 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004393 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004394 sb.append("(");
4395 sb.append(formatRatioLocked(time, screenOnTime));
4396 sb.append(")");
4397 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004398 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004399 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004400 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004401 sb.setLength(0);
4402 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004403 sb.append(" Power save mode enabled: ");
4404 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004405 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004406 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004407 sb.append(")");
4408 pw.println(sb.toString());
4409 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004410 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004411 sb.setLength(0);
4412 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004413 sb.append(" Device light idling: ");
4414 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004415 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004416 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
4417 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004418 sb.append("x");
4419 pw.println(sb.toString());
4420 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004421 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004422 sb.setLength(0);
4423 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004424 sb.append(" Idle mode light time: ");
4425 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004426 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004427 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
4428 sb.append(") ");
4429 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004430 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004431 sb.append(" -- longest ");
4432 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
4433 pw.println(sb.toString());
4434 }
4435 if (deviceIdlingTime != 0) {
4436 sb.setLength(0);
4437 sb.append(prefix);
4438 sb.append(" Device full idling: ");
4439 formatTimeMs(sb, deviceIdlingTime / 1000);
4440 sb.append("(");
4441 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004442 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004443 sb.append("x");
4444 pw.println(sb.toString());
4445 }
4446 if (deviceIdleModeFullTime != 0) {
4447 sb.setLength(0);
4448 sb.append(prefix);
4449 sb.append(" Idle mode full time: ");
4450 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
4451 sb.append("(");
4452 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
4453 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004454 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004455 sb.append("x");
4456 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004457 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004458 pw.println(sb.toString());
4459 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004460 if (phoneOnTime != 0) {
4461 sb.setLength(0);
4462 sb.append(prefix);
4463 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
4464 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004465 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004466 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004467 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004468 if (connChanges != 0) {
4469 pw.print(prefix);
4470 pw.print(" Connectivity changes: "); pw.println(connChanges);
4471 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004472
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004473 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07004474 long fullWakeLockTimeTotalMicros = 0;
4475 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08004476
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004477 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08004478
Evan Millar22ac0432009-03-31 11:33:18 -07004479 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004480 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004481
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004482 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4483 = u.getWakelockStats();
4484 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4485 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07004486
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004487 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
4488 if (fullWakeTimer != null) {
4489 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
4490 rawRealtime, which);
4491 }
4492
4493 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4494 if (partialWakeTimer != null) {
4495 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
4496 rawRealtime, which);
4497 if (totalTimeMicros > 0) {
4498 if (reqUid < 0) {
4499 // Only show the ordered list of all wake
4500 // locks if the caller is not asking for data
4501 // about a specific uid.
4502 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
4503 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08004504 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004505 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07004506 }
4507 }
4508 }
4509 }
Bookatzc8c44962017-05-11 12:12:54 -07004510
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004511 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4512 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4513 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4514 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4515 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4516 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4517 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4518 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004519 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4520 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004521
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004522 if (fullWakeLockTimeTotalMicros != 0) {
4523 sb.setLength(0);
4524 sb.append(prefix);
4525 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4526 (fullWakeLockTimeTotalMicros + 500) / 1000);
4527 pw.println(sb.toString());
4528 }
4529
4530 if (partialWakeLockTimeTotalMicros != 0) {
4531 sb.setLength(0);
4532 sb.append(prefix);
4533 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4534 (partialWakeLockTimeTotalMicros + 500) / 1000);
4535 pw.println(sb.toString());
4536 }
4537
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004538 final long multicastWakeLockTimeTotalMicros =
4539 getWifiMulticastWakelockTime(rawRealtime, which);
4540 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004541 if (multicastWakeLockTimeTotalMicros != 0) {
4542 sb.setLength(0);
4543 sb.append(prefix);
4544 sb.append(" Total WiFi Multicast wakelock Count: ");
4545 sb.append(multicastWakeLockCountTotal);
4546 pw.println(sb.toString());
4547
4548 sb.setLength(0);
4549 sb.append(prefix);
4550 sb.append(" Total WiFi Multicast wakelock time: ");
4551 formatTimeMsNoSpace(sb, (multicastWakeLockTimeTotalMicros + 500) / 1000);
4552 pw.println(sb.toString());
4553 }
4554
Siddharth Ray3c648c42017-10-02 17:30:58 -07004555 pw.println("");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004556 pw.print(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004557 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004558 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004559 sb.append(" CONNECTIVITY POWER SUMMARY START");
4560 pw.println(sb.toString());
4561
4562 pw.print(prefix);
4563 sb.setLength(0);
4564 sb.append(prefix);
4565 sb.append(" Logging duration for connectivity statistics: ");
4566 formatTimeMs(sb, whichBatteryRealtime / 1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004567 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004568
4569 sb.setLength(0);
4570 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004571 sb.append(" Cellular Statistics:");
Amith Yamasanif37447b2009-10-08 18:28:01 -07004572 pw.println(sb.toString());
4573
Siddharth Ray3c648c42017-10-02 17:30:58 -07004574 pw.print(prefix);
4575 sb.setLength(0);
4576 sb.append(prefix);
4577 sb.append(" Cellular kernel active time: ");
4578 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
4579 formatTimeMs(sb, mobileActiveTime / 1000);
4580 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4581 sb.append(")");
4582 pw.println(sb.toString());
4583
4584 pw.print(" Cellular data received: "); pw.println(formatBytesLocked(mobileRxTotalBytes));
4585 pw.print(" Cellular data sent: "); pw.println(formatBytesLocked(mobileTxTotalBytes));
4586 pw.print(" Cellular packets received: "); pw.println(mobileRxTotalPackets);
4587 pw.print(" Cellular packets sent: "); pw.println(mobileTxTotalPackets);
4588
Dianne Hackborn627bba72009-03-24 22:32:56 -07004589 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004590 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004591 sb.append(" Cellular Radio Access Technology:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004592 didOne = false;
4593 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004594 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004595 if (time == 0) {
4596 continue;
4597 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004598 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004599 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004600 didOne = true;
4601 sb.append(DATA_CONNECTION_NAMES[i]);
4602 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004603 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004604 sb.append("(");
4605 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004606 sb.append(") ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004607 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004608 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004609 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004610
4611 sb.setLength(0);
4612 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004613 sb.append(" Cellular Rx signal strength (RSRP):");
4614 final String[] cellularRxSignalStrengthDescription = new String[]{
4615 "very poor (less than -128dBm): ",
4616 "poor (-128dBm to -118dBm): ",
4617 "moderate (-118dBm to -108dBm): ",
4618 "good (-108dBm to -98dBm): ",
4619 "great (greater than -98dBm): "};
4620 didOne = false;
4621 final int numCellularRxBins = Math.min(SignalStrength.NUM_SIGNAL_STRENGTH_BINS,
4622 cellularRxSignalStrengthDescription.length);
4623 for (int i=0; i<numCellularRxBins; i++) {
4624 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
4625 if (time == 0) {
4626 continue;
4627 }
4628 sb.append("\n ");
4629 sb.append(prefix);
4630 didOne = true;
4631 sb.append(cellularRxSignalStrengthDescription[i]);
4632 sb.append(" ");
4633 formatTimeMs(sb, time/1000);
4634 sb.append("(");
4635 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4636 sb.append(") ");
4637 }
4638 if (!didOne) sb.append(" (no activity)");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004639 pw.println(sb.toString());
4640
Siddharth Ray3c648c42017-10-02 17:30:58 -07004641 printControllerActivity(pw, sb, prefix, "Cellular",
4642 getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004643
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004644 pw.print(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004645 sb.setLength(0);
4646 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004647 sb.append(" Wifi Statistics:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004648 pw.println(sb.toString());
4649
Siddharth Ray3c648c42017-10-02 17:30:58 -07004650 pw.print(" Wifi data received: "); pw.println(formatBytesLocked(wifiRxTotalBytes));
4651 pw.print(" Wifi data sent: "); pw.println(formatBytesLocked(wifiTxTotalBytes));
4652 pw.print(" Wifi packets received: "); pw.println(wifiRxTotalPackets);
4653 pw.print(" Wifi packets sent: "); pw.println(wifiTxTotalPackets);
4654
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004655 sb.setLength(0);
4656 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004657 sb.append(" Wifi states:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004658 didOne = false;
4659 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004660 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004661 if (time == 0) {
4662 continue;
4663 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004664 sb.append("\n ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004665 didOne = true;
4666 sb.append(WIFI_STATE_NAMES[i]);
4667 sb.append(" ");
4668 formatTimeMs(sb, time/1000);
4669 sb.append("(");
4670 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4671 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004672 }
4673 if (!didOne) sb.append(" (no activity)");
4674 pw.println(sb.toString());
4675
4676 sb.setLength(0);
4677 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004678 sb.append(" Wifi supplicant states:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004679 didOne = false;
4680 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4681 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4682 if (time == 0) {
4683 continue;
4684 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004685 sb.append("\n ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004686 didOne = true;
4687 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4688 sb.append(" ");
4689 formatTimeMs(sb, time/1000);
4690 sb.append("(");
4691 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4692 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004693 }
4694 if (!didOne) sb.append(" (no activity)");
4695 pw.println(sb.toString());
4696
4697 sb.setLength(0);
4698 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004699 sb.append(" Wifi Rx signal strength (RSSI):");
4700 final String[] wifiRxSignalStrengthDescription = new String[]{
4701 "very poor (less than -88.75dBm): ",
4702 "poor (-88.75 to -77.5dBm): ",
4703 "moderate (-77.5dBm to -66.25dBm): ",
4704 "good (-66.25dBm to -55dBm): ",
4705 "great (greater than -55dBm): "};
Dianne Hackborn3251b902014-06-20 14:40:53 -07004706 didOne = false;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004707 final int numWifiRxBins = Math.min(NUM_WIFI_SIGNAL_STRENGTH_BINS,
4708 wifiRxSignalStrengthDescription.length);
4709 for (int i=0; i<numWifiRxBins; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004710 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4711 if (time == 0) {
4712 continue;
4713 }
4714 sb.append("\n ");
4715 sb.append(prefix);
4716 didOne = true;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004717 sb.append(" ");
4718 sb.append(wifiRxSignalStrengthDescription[i]);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004719 formatTimeMs(sb, time/1000);
4720 sb.append("(");
4721 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4722 sb.append(") ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004723 }
4724 if (!didOne) sb.append(" (no activity)");
4725 pw.println(sb.toString());
4726
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004727 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004728
Adam Lesinski50e47602015-12-04 17:04:54 -08004729 pw.print(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004730 sb.setLength(0);
4731 sb.append(prefix);
4732 sb.append(" CONNECTIVITY POWER SUMMARY END");
4733 pw.println(sb.toString());
4734 pw.println("");
4735
4736 pw.print(prefix);
Adam Lesinski50e47602015-12-04 17:04:54 -08004737 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4738 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4739
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004740 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4741 sb.setLength(0);
4742 sb.append(prefix);
4743 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4744 pw.println(sb.toString());
4745
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004746 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4747 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004748
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004749 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004750
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004751 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004752 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004753 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004754 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004755 pw.println(getDischargeStartLevel());
4756 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4757 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004758 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004759 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004760 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004761 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004762 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004763 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004764 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004765 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004766 pw.println(getDischargeAmountScreenOn());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004767 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004768 pw.println(getDischargeAmountScreenOff());
4769 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4770 pw.println(getDischargeAmountScreenDoze());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004771 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004772 } else {
4773 pw.print(prefix); pw.println(" Device battery use since last full charge");
4774 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004775 pw.println(getLowDischargeAmountSinceCharge());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004776 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004777 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004778 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004779 pw.println(getDischargeAmountScreenOnSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004780 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004781 pw.println(getDischargeAmountScreenOffSinceCharge());
4782 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4783 pw.println(getDischargeAmountScreenDozeSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004784 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004785 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004786
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004787 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004788 helper.create(this);
4789 helper.refreshStats(which, UserHandle.USER_ALL);
4790 List<BatterySipper> sippers = helper.getUsageList();
4791 if (sippers != null && sippers.size() > 0) {
4792 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4793 pw.print(prefix); pw.print(" Capacity: ");
4794 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004795 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004796 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4797 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4798 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4799 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004800 pw.println();
4801 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004802 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004803 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004804 switch (bs.drainType) {
4805 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004806 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004807 break;
4808 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004809 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004810 break;
4811 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004812 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004813 break;
4814 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004815 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004816 break;
4817 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004818 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004819 break;
4820 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004821 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004822 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004823 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004824 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004825 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004826 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004827 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004828 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004829 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004830 break;
4831 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004832 pw.print(" User "); pw.print(bs.userId);
4833 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004834 break;
4835 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004836 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004837 break;
4838 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004839 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004840 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004841 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004842 pw.print(" Camera: ");
4843 break;
4844 default:
4845 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004846 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004847 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004848 printmAh(pw, bs.totalPowerMah);
4849
Adam Lesinski57123002015-06-12 16:12:07 -07004850 if (bs.usagePowerMah != bs.totalPowerMah) {
4851 // If the usage (generic power) isn't the whole amount, we list out
4852 // what components are involved in the calculation.
4853
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004854 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004855 if (bs.usagePowerMah != 0) {
4856 pw.print(" usage=");
4857 printmAh(pw, bs.usagePowerMah);
4858 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004859 if (bs.cpuPowerMah != 0) {
4860 pw.print(" cpu=");
4861 printmAh(pw, bs.cpuPowerMah);
4862 }
4863 if (bs.wakeLockPowerMah != 0) {
4864 pw.print(" wake=");
4865 printmAh(pw, bs.wakeLockPowerMah);
4866 }
4867 if (bs.mobileRadioPowerMah != 0) {
4868 pw.print(" radio=");
4869 printmAh(pw, bs.mobileRadioPowerMah);
4870 }
4871 if (bs.wifiPowerMah != 0) {
4872 pw.print(" wifi=");
4873 printmAh(pw, bs.wifiPowerMah);
4874 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004875 if (bs.bluetoothPowerMah != 0) {
4876 pw.print(" bt=");
4877 printmAh(pw, bs.bluetoothPowerMah);
4878 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004879 if (bs.gpsPowerMah != 0) {
4880 pw.print(" gps=");
4881 printmAh(pw, bs.gpsPowerMah);
4882 }
4883 if (bs.sensorPowerMah != 0) {
4884 pw.print(" sensor=");
4885 printmAh(pw, bs.sensorPowerMah);
4886 }
4887 if (bs.cameraPowerMah != 0) {
4888 pw.print(" camera=");
4889 printmAh(pw, bs.cameraPowerMah);
4890 }
4891 if (bs.flashlightPowerMah != 0) {
4892 pw.print(" flash=");
4893 printmAh(pw, bs.flashlightPowerMah);
4894 }
4895 pw.print(" )");
4896 }
Bookatz17d7d9d2017-06-08 14:50:46 -07004897
4898 // If there is additional smearing information, include it.
4899 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
4900 pw.print(" Including smearing: ");
4901 printmAh(pw, bs.totalSmearedPowerMah);
4902 pw.print(" (");
4903 if (bs.screenPowerMah != 0) {
4904 pw.print(" screen=");
4905 printmAh(pw, bs.screenPowerMah);
4906 }
4907 if (bs.proportionalSmearMah != 0) {
4908 pw.print(" proportional=");
4909 printmAh(pw, bs.proportionalSmearMah);
4910 }
4911 pw.print(" )");
4912 }
4913 if (bs.shouldHide) {
4914 pw.print(" Excluded from smearing");
4915 }
4916
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004917 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004918 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004919 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004920 }
4921
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004922 sippers = helper.getMobilemsppList();
4923 if (sippers != null && sippers.size() > 0) {
4924 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004925 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004926 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004927 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004928 sb.setLength(0);
4929 sb.append(prefix); sb.append(" Uid ");
4930 UserHandle.formatUid(sb, bs.uidObj.getUid());
4931 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4932 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4933 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004934 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004935 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004936 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004937 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004938 sb.setLength(0);
4939 sb.append(prefix);
4940 sb.append(" TOTAL TIME: ");
4941 formatTimeMs(sb, totalTime);
4942 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4943 sb.append(")");
4944 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004945 pw.println();
4946 }
4947
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004948 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4949 @Override
4950 public int compare(TimerEntry lhs, TimerEntry rhs) {
4951 long lhsTime = lhs.mTime;
4952 long rhsTime = rhs.mTime;
4953 if (lhsTime < rhsTime) {
4954 return 1;
4955 }
4956 if (lhsTime > rhsTime) {
4957 return -1;
4958 }
4959 return 0;
4960 }
4961 };
4962
4963 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004964 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4965 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004966 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004967 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4968 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4969 : kernelWakelocks.entrySet()) {
4970 final BatteryStats.Timer timer = ent.getValue();
4971 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004972 if (totalTimeMillis > 0) {
4973 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4974 }
4975 }
4976 if (ktimers.size() > 0) {
4977 Collections.sort(ktimers, timerComparator);
4978 pw.print(prefix); pw.println(" All kernel wake locks:");
4979 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004980 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004981 String linePrefix = ": ";
4982 sb.setLength(0);
4983 sb.append(prefix);
4984 sb.append(" Kernel Wake lock ");
4985 sb.append(timer.mName);
4986 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4987 which, linePrefix);
4988 if (!linePrefix.equals(": ")) {
4989 sb.append(" realtime");
4990 // Only print out wake locks that were held
4991 pw.println(sb.toString());
4992 }
4993 }
4994 pw.println();
4995 }
4996 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004997
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004998 if (timers.size() > 0) {
4999 Collections.sort(timers, timerComparator);
5000 pw.print(prefix); pw.println(" All partial wake locks:");
5001 for (int i=0; i<timers.size(); i++) {
5002 TimerEntry timer = timers.get(i);
5003 sb.setLength(0);
5004 sb.append(" Wake lock ");
5005 UserHandle.formatUid(sb, timer.mId);
5006 sb.append(" ");
5007 sb.append(timer.mName);
5008 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5009 sb.append(" realtime");
5010 pw.println(sb.toString());
5011 }
5012 timers.clear();
5013 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08005014 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005015
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005016 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005017 if (wakeupReasons.size() > 0) {
5018 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005019 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005020 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005021 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005022 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
5023 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005024 }
5025 Collections.sort(reasons, timerComparator);
5026 for (int i=0; i<reasons.size(); i++) {
5027 TimerEntry timer = reasons.get(i);
5028 String linePrefix = ": ";
5029 sb.setLength(0);
5030 sb.append(prefix);
5031 sb.append(" Wakeup reason ");
5032 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005033 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5034 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005035 pw.println(sb.toString());
5036 }
5037 pw.println();
5038 }
Dianne Hackborn81038902012-11-26 17:04:09 -08005039 }
Evan Millar22ac0432009-03-31 11:33:18 -07005040
James Carr2dd7e5e2016-07-20 18:48:39 -07005041 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
Bookatz50df7112017-08-04 14:53:26 -07005042 if (mMemoryStats.size() > 0) {
5043 pw.println(" Memory Stats");
5044 for (int i = 0; i < mMemoryStats.size(); i++) {
5045 sb.setLength(0);
5046 sb.append(" Bandwidth ");
5047 sb.append(mMemoryStats.keyAt(i));
5048 sb.append(" Time ");
5049 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
5050 pw.println(sb.toString());
5051 }
5052 pw.println();
5053 }
5054
5055 final Map<String, ? extends Timer> rpmStats = getRpmStats();
5056 if (rpmStats.size() > 0) {
5057 pw.print(prefix); pw.println(" Resource Power Manager Stats");
5058 if (rpmStats.size() > 0) {
5059 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
5060 final String timerName = ent.getKey();
5061 final Timer timer = ent.getValue();
5062 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5063 }
5064 }
5065 pw.println();
5066 }
Bookatz82b341172017-09-07 19:06:08 -07005067 if (SCREEN_OFF_RPM_STATS_ENABLED) {
5068 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
Bookatz50df7112017-08-04 14:53:26 -07005069 if (screenOffRpmStats.size() > 0) {
Bookatz82b341172017-09-07 19:06:08 -07005070 pw.print(prefix);
5071 pw.println(" Resource Power Manager Stats for when screen was off");
5072 if (screenOffRpmStats.size() > 0) {
5073 for (Map.Entry<String, ? extends Timer> ent : screenOffRpmStats.entrySet()) {
5074 final String timerName = ent.getKey();
5075 final Timer timer = ent.getValue();
5076 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5077 }
Bookatz50df7112017-08-04 14:53:26 -07005078 }
Bookatz82b341172017-09-07 19:06:08 -07005079 pw.println();
Bookatz50df7112017-08-04 14:53:26 -07005080 }
James Carr2dd7e5e2016-07-20 18:48:39 -07005081 }
5082
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005083 final long[] cpuFreqs = getCpuFreqs();
5084 if (cpuFreqs != null) {
5085 sb.setLength(0);
Bookatz50df7112017-08-04 14:53:26 -07005086 sb.append(" CPU freqs:");
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005087 for (int i = 0; i < cpuFreqs.length; ++i) {
5088 sb.append(" " + cpuFreqs[i]);
5089 }
5090 pw.println(sb.toString());
Bookatz50df7112017-08-04 14:53:26 -07005091 pw.println();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005092 }
5093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005094 for (int iu=0; iu<NU; iu++) {
5095 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005096 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005097 continue;
5098 }
Bookatzc8c44962017-05-11 12:12:54 -07005099
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005100 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07005101
5102 pw.print(prefix);
5103 pw.print(" ");
5104 UserHandle.formatUid(pw, uid);
5105 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005106 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005107
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005108 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
5109 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
5110 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
5111 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005112 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
5113 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
5114
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005115 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
5116 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005117 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
5118 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005119
5120 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
5121 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
5122
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005123 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
5124 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
5125 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08005126 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
5127 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5128 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
5129 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005130 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005131
Adam Lesinski5f056f62016-07-14 16:56:08 -07005132 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
5133 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
5134
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005135 if (mobileRxBytes > 0 || mobileTxBytes > 0
5136 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005137 pw.print(prefix); pw.print(" Mobile network: ");
5138 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005139 pw.print(formatBytesLocked(mobileTxBytes));
5140 pw.print(" sent (packets "); pw.print(mobileRxPackets);
5141 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005142 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005143 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
5144 sb.setLength(0);
5145 sb.append(prefix); sb.append(" Mobile radio active: ");
5146 formatTimeMs(sb, uidMobileActiveTime / 1000);
5147 sb.append("(");
5148 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
5149 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
5150 long packets = mobileRxPackets + mobileTxPackets;
5151 if (packets == 0) {
5152 packets = 1;
5153 }
5154 sb.append(" @ ");
5155 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
5156 sb.append(" mspp");
5157 pw.println(sb.toString());
5158 }
5159
Adam Lesinski5f056f62016-07-14 16:56:08 -07005160 if (mobileWakeup > 0) {
5161 sb.setLength(0);
5162 sb.append(prefix);
5163 sb.append(" Mobile radio AP wakeups: ");
5164 sb.append(mobileWakeup);
5165 pw.println(sb.toString());
5166 }
5167
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005168 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
5169 u.getModemControllerActivity(), which);
5170
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005171 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005172 pw.print(prefix); pw.print(" Wi-Fi network: ");
5173 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005174 pw.print(formatBytesLocked(wifiTxBytes));
5175 pw.print(" sent (packets "); pw.print(wifiRxPackets);
5176 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005177 }
5178
Dianne Hackborn62793e42015-03-09 11:15:41 -07005179 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08005180 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005181 || uidWifiRunningTime != 0) {
5182 sb.setLength(0);
5183 sb.append(prefix); sb.append(" Wifi Running: ");
5184 formatTimeMs(sb, uidWifiRunningTime / 1000);
5185 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
5186 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07005187 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005188 formatTimeMs(sb, fullWifiLockOnTime / 1000);
5189 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
5190 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08005191 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005192 formatTimeMs(sb, wifiScanTime / 1000);
5193 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07005194 whichBatteryRealtime)); sb.append(") ");
5195 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08005196 sb.append("x\n");
5197 // actual and background times are unpooled and since reset (regardless of 'which')
5198 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
5199 formatTimeMs(sb, wifiScanActualTime / 1000);
5200 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
5201 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5202 sb.append(") ");
5203 sb.append(wifiScanCount);
5204 sb.append("x\n");
5205 sb.append(prefix); sb.append(" Background Wifi Scan: ");
5206 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
5207 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
5208 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5209 sb.append(") ");
5210 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07005211 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005212 pw.println(sb.toString());
5213 }
5214
Adam Lesinski5f056f62016-07-14 16:56:08 -07005215 if (wifiWakeup > 0) {
5216 sb.setLength(0);
5217 sb.append(prefix);
5218 sb.append(" WiFi AP wakeups: ");
5219 sb.append(wifiWakeup);
5220 pw.println(sb.toString());
5221 }
5222
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005223 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
5224 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07005225
Adam Lesinski50e47602015-12-04 17:04:54 -08005226 if (btRxBytes > 0 || btTxBytes > 0) {
5227 pw.print(prefix); pw.print(" Bluetooth network: ");
5228 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
5229 pw.print(formatBytesLocked(btTxBytes));
5230 pw.println(" sent");
5231 }
5232
Bookatz867c0d72017-03-07 18:23:42 -08005233 final Timer bleTimer = u.getBluetoothScanTimer();
5234 if (bleTimer != null) {
5235 // Convert from microseconds to milliseconds with rounding
5236 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
5237 / 1000;
5238 if (totalTimeMs != 0) {
5239 final int count = bleTimer.getCountLocked(which);
5240 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
5241 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005242 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5243 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
5244 final long actualTimeMsBg = bleTimerBg != null ?
5245 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005246 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07005247 final int resultCount = u.getBluetoothScanResultCounter() != null ?
5248 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005249 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
5250 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
5251 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
5252 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
5253 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
5254 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5255 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
5256 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
5257 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
5258 final Timer unoptimizedScanTimerBg =
5259 u.getBluetoothUnoptimizedScanBackgroundTimer();
5260 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
5261 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5262 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
5263 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005264
5265 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08005266 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07005267 sb.append(prefix);
5268 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08005269 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07005270 sb.append(" (");
5271 sb.append(count);
5272 sb.append(" times)");
5273 if (bleTimer.isRunningLocked()) {
5274 sb.append(" (currently running)");
5275 }
5276 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005277 }
Bookatzb1f04f32017-05-19 13:57:32 -07005278
5279 sb.append(prefix);
5280 sb.append(" Bluetooth Scan (total actual realtime): ");
5281 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
5282 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005283 sb.append(count);
5284 sb.append(" times)");
5285 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07005286 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08005287 }
Bookatzb1f04f32017-05-19 13:57:32 -07005288 sb.append("\n");
5289 if (actualTimeMsBg > 0 || countBg > 0) {
5290 sb.append(prefix);
5291 sb.append(" Bluetooth Scan (background realtime): ");
5292 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
5293 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005294 sb.append(countBg);
5295 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07005296 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
5297 sb.append(" (currently running in background)");
5298 }
5299 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005300 }
Bookatzb1f04f32017-05-19 13:57:32 -07005301
5302 sb.append(prefix);
5303 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07005304 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07005305 sb.append(" (");
5306 sb.append(resultCountBg);
5307 sb.append(" in background)");
5308
5309 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
5310 sb.append("\n");
5311 sb.append(prefix);
5312 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
5313 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
5314 sb.append(" (max ");
5315 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
5316 sb.append(")");
5317 if (unoptimizedScanTimer != null
5318 && unoptimizedScanTimer.isRunningLocked()) {
5319 sb.append(" (currently running unoptimized)");
5320 }
5321 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
5322 sb.append("\n");
5323 sb.append(prefix);
5324 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
5325 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
5326 sb.append(" (max ");
5327 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
5328 sb.append(")");
5329 if (unoptimizedScanTimerBg.isRunningLocked()) {
5330 sb.append(" (currently running unoptimized in background)");
5331 }
5332 }
5333 }
Bookatz867c0d72017-03-07 18:23:42 -08005334 pw.println(sb.toString());
5335 uidActivity = true;
5336 }
5337 }
5338
5339
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005340
Dianne Hackborn617f8772009-03-31 15:04:46 -07005341 if (u.hasUserActivity()) {
5342 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07005343 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005344 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005345 if (val != 0) {
5346 if (!hasData) {
5347 sb.setLength(0);
5348 sb.append(" User activity: ");
5349 hasData = true;
5350 } else {
5351 sb.append(", ");
5352 }
5353 sb.append(val);
5354 sb.append(" ");
5355 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
5356 }
5357 }
5358 if (hasData) {
5359 pw.println(sb.toString());
5360 }
5361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005362
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005363 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
5364 = u.getWakelockStats();
5365 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005366 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005367 int countWakelock = 0;
5368 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
5369 final Uid.Wakelock wl = wakelocks.valueAt(iw);
5370 String linePrefix = ": ";
5371 sb.setLength(0);
5372 sb.append(prefix);
5373 sb.append(" Wake lock ");
5374 sb.append(wakelocks.keyAt(iw));
5375 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
5376 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005377 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
5378 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005379 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005380 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
5381 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005382 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
5383 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005384 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
5385 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07005386 sb.append(" realtime");
5387 pw.println(sb.toString());
5388 uidActivity = true;
5389 countWakelock++;
5390
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005391 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
5392 rawRealtime, which);
5393 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
5394 rawRealtime, which);
5395 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
5396 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005397 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07005398 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005399 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005400 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07005401 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
5402 // pooled and therefore just a lower bound)
5403 long actualTotalPartialWakelock = 0;
5404 long actualBgPartialWakelock = 0;
5405 if (u.getAggregatedPartialWakelockTimer() != null) {
5406 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
5407 // Convert from microseconds to milliseconds with rounding
5408 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07005409 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07005410 final Timer bgAggTimer = aggTimer.getSubTimer();
5411 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005412 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07005413 }
5414
5415 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
5416 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
5417 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005418 sb.setLength(0);
5419 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005420 sb.append(" TOTAL wake: ");
5421 boolean needComma = false;
5422 if (totalFullWakelock != 0) {
5423 needComma = true;
5424 formatTimeMs(sb, totalFullWakelock);
5425 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005426 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005427 if (totalPartialWakelock != 0) {
5428 if (needComma) {
5429 sb.append(", ");
5430 }
5431 needComma = true;
5432 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07005433 sb.append("blamed partial");
5434 }
5435 if (actualTotalPartialWakelock != 0) {
5436 if (needComma) {
5437 sb.append(", ");
5438 }
5439 needComma = true;
5440 formatTimeMs(sb, actualTotalPartialWakelock);
5441 sb.append("actual partial");
5442 }
5443 if (actualBgPartialWakelock != 0) {
5444 if (needComma) {
5445 sb.append(", ");
5446 }
5447 needComma = true;
5448 formatTimeMs(sb, actualBgPartialWakelock);
5449 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005450 }
5451 if (totalWindowWakelock != 0) {
5452 if (needComma) {
5453 sb.append(", ");
5454 }
5455 needComma = true;
5456 formatTimeMs(sb, totalWindowWakelock);
5457 sb.append("window");
5458 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005459 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07005460 if (needComma) {
5461 sb.append(",");
5462 }
5463 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005464 formatTimeMs(sb, totalDrawWakelock);
5465 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07005466 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005467 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005468 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005469 }
5470 }
5471
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07005472 // Calculate multicast wakelock stats
5473 final Timer mcTimer = u.getMulticastWakelockStats();
5474 if (mcTimer != null) {
5475 final long multicastWakeLockTimeMicros = mcTimer.getTotalTimeLocked(rawRealtime, which);
5476 final int multicastWakeLockCount = mcTimer.getCountLocked(which);
5477
5478 if (multicastWakeLockTimeMicros > 0) {
5479 sb.setLength(0);
5480 sb.append(prefix);
5481 sb.append(" WiFi Multicast Wakelock");
5482 sb.append(" count = ");
5483 sb.append(multicastWakeLockCount);
5484 sb.append(" time = ");
5485 formatTimeMsNoSpace(sb, (multicastWakeLockTimeMicros + 500) / 1000);
5486 pw.println(sb.toString());
5487 }
5488 }
5489
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005490 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
5491 for (int isy=syncs.size()-1; isy>=0; isy--) {
5492 final Timer timer = syncs.valueAt(isy);
5493 // Convert from microseconds to milliseconds with rounding
5494 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5495 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07005496 final Timer bgTimer = timer.getSubTimer();
5497 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005498 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07005499 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005500 sb.setLength(0);
5501 sb.append(prefix);
5502 sb.append(" Sync ");
5503 sb.append(syncs.keyAt(isy));
5504 sb.append(": ");
5505 if (totalTime != 0) {
5506 formatTimeMs(sb, totalTime);
5507 sb.append("realtime (");
5508 sb.append(count);
5509 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07005510 if (bgTime > 0) {
5511 sb.append(", ");
5512 formatTimeMs(sb, bgTime);
5513 sb.append("background (");
5514 sb.append(bgCount);
5515 sb.append(" times)");
5516 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005517 } else {
5518 sb.append("(not used)");
5519 }
5520 pw.println(sb.toString());
5521 uidActivity = true;
5522 }
5523
5524 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
5525 for (int ij=jobs.size()-1; ij>=0; ij--) {
5526 final Timer timer = jobs.valueAt(ij);
5527 // Convert from microseconds to milliseconds with rounding
5528 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5529 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07005530 final Timer bgTimer = timer.getSubTimer();
5531 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005532 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07005533 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005534 sb.setLength(0);
5535 sb.append(prefix);
5536 sb.append(" Job ");
5537 sb.append(jobs.keyAt(ij));
5538 sb.append(": ");
5539 if (totalTime != 0) {
5540 formatTimeMs(sb, totalTime);
5541 sb.append("realtime (");
5542 sb.append(count);
5543 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07005544 if (bgTime > 0) {
5545 sb.append(", ");
5546 formatTimeMs(sb, bgTime);
5547 sb.append("background (");
5548 sb.append(bgCount);
5549 sb.append(" times)");
5550 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005551 } else {
5552 sb.append("(not used)");
5553 }
5554 pw.println(sb.toString());
5555 uidActivity = true;
5556 }
5557
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005558 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
5559 for (int ic=completions.size()-1; ic>=0; ic--) {
5560 SparseIntArray types = completions.valueAt(ic);
5561 if (types != null) {
5562 pw.print(prefix);
5563 pw.print(" Job Completions ");
5564 pw.print(completions.keyAt(ic));
5565 pw.print(":");
5566 for (int it=0; it<types.size(); it++) {
5567 pw.print(" ");
5568 pw.print(JobParameters.getReasonName(types.keyAt(it)));
5569 pw.print("(");
5570 pw.print(types.valueAt(it));
5571 pw.print("x)");
5572 }
5573 pw.println();
5574 }
5575 }
5576
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005577 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
5578 prefix, "Flashlight");
5579 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
5580 prefix, "Camera");
5581 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
5582 prefix, "Video");
5583 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
5584 prefix, "Audio");
5585
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005586 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
5587 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005588 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005589 final Uid.Sensor se = sensors.valueAt(ise);
5590 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005591 sb.setLength(0);
5592 sb.append(prefix);
5593 sb.append(" Sensor ");
5594 int handle = se.getHandle();
5595 if (handle == Uid.Sensor.GPS) {
5596 sb.append("GPS");
5597 } else {
5598 sb.append(handle);
5599 }
5600 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005601
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005602 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005603 if (timer != null) {
5604 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005605 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5606 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005607 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005608 final Timer bgTimer = se.getSensorBackgroundTime();
5609 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005610 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5611 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5612 final long bgActualTime = bgTimer != null ?
5613 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5614
Dianne Hackborn61659e52014-07-09 16:13:01 -07005615 //timer.logState();
5616 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005617 if (actualTime != totalTime) {
5618 formatTimeMs(sb, totalTime);
5619 sb.append("blamed realtime, ");
5620 }
5621
5622 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005623 sb.append("realtime (");
5624 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005625 sb.append(" times)");
5626
5627 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005628 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005629 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5630 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005631 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005632 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005633 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005634 } else {
5635 sb.append("(not used)");
5636 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005637 } else {
5638 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005639 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005640
5641 pw.println(sb.toString());
5642 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005643 }
5644
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005645 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5646 "Vibrator");
5647 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5648 prefix, "Foreground activities");
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005649 uidActivity |= printTimer(pw, sb, u.getForegroundServiceTimer(), rawRealtime, which,
5650 prefix, "Foreground services");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005651
Dianne Hackborn61659e52014-07-09 16:13:01 -07005652 long totalStateTime = 0;
5653 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5654 long time = u.getProcessStateTime(ips, rawRealtime, which);
5655 if (time > 0) {
5656 totalStateTime += time;
5657 sb.setLength(0);
5658 sb.append(prefix);
5659 sb.append(" ");
5660 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5661 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005662 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005663 pw.println(sb.toString());
5664 uidActivity = true;
5665 }
5666 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005667 if (totalStateTime > 0) {
5668 sb.setLength(0);
5669 sb.append(prefix);
5670 sb.append(" Total running: ");
5671 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5672 pw.println(sb.toString());
5673 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005674
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005675 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5676 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005677 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005678 sb.setLength(0);
5679 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005680 sb.append(" Total cpu time: u=");
5681 formatTimeMs(sb, userCpuTimeUs / 1000);
5682 sb.append("s=");
5683 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005684 pw.println(sb.toString());
5685 }
5686
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005687 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5688 if (cpuFreqTimes != null) {
5689 sb.setLength(0);
5690 sb.append(" Total cpu time per freq:");
5691 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5692 sb.append(" " + cpuFreqTimes[i]);
5693 }
5694 pw.println(sb.toString());
5695 }
5696 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5697 if (screenOffCpuFreqTimes != null) {
5698 sb.setLength(0);
5699 sb.append(" Total screen-off cpu time per freq:");
5700 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5701 sb.append(" " + screenOffCpuFreqTimes[i]);
5702 }
5703 pw.println(sb.toString());
5704 }
5705
Sudheer Shankab2f83c12017-11-13 19:25:01 -08005706 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
5707 final long[] cpuTimes = u.getCpuFreqTimes(which, procState);
5708 if (cpuTimes != null) {
5709 sb.setLength(0);
5710 sb.append(" Cpu times per freq at state "
5711 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5712 for (int i = 0; i < cpuTimes.length; ++i) {
5713 sb.append(" " + cpuTimes[i]);
5714 }
5715 pw.println(sb.toString());
5716 }
5717
5718 final long[] screenOffCpuTimes = u.getScreenOffCpuFreqTimes(which, procState);
5719 if (screenOffCpuTimes != null) {
5720 sb.setLength(0);
5721 sb.append(" Screen-off cpu times per freq at state "
5722 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5723 for (int i = 0; i < screenOffCpuTimes.length; ++i) {
5724 sb.append(" " + screenOffCpuTimes[i]);
5725 }
5726 pw.println(sb.toString());
5727 }
5728 }
5729
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005730 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5731 = u.getProcessStats();
5732 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5733 final Uid.Proc ps = processStats.valueAt(ipr);
5734 long userTime;
5735 long systemTime;
5736 long foregroundTime;
5737 int starts;
5738 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005739
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005740 userTime = ps.getUserTime(which);
5741 systemTime = ps.getSystemTime(which);
5742 foregroundTime = ps.getForegroundTime(which);
5743 starts = ps.getStarts(which);
5744 final int numCrashes = ps.getNumCrashes(which);
5745 final int numAnrs = ps.getNumAnrs(which);
5746 numExcessive = which == STATS_SINCE_CHARGED
5747 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005748
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005749 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5750 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5751 sb.setLength(0);
5752 sb.append(prefix); sb.append(" Proc ");
5753 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5754 sb.append(prefix); sb.append(" CPU: ");
5755 formatTimeMs(sb, userTime); sb.append("usr + ");
5756 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5757 formatTimeMs(sb, foregroundTime); sb.append("fg");
5758 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5759 sb.append("\n"); sb.append(prefix); sb.append(" ");
5760 boolean hasOne = false;
5761 if (starts != 0) {
5762 hasOne = true;
5763 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005764 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005765 if (numCrashes != 0) {
5766 if (hasOne) {
5767 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005768 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005769 hasOne = true;
5770 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005771 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005772 if (numAnrs != 0) {
5773 if (hasOne) {
5774 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005775 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005776 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005777 }
5778 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005779 pw.println(sb.toString());
5780 for (int e=0; e<numExcessive; e++) {
5781 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5782 if (ew != null) {
5783 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07005784 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005785 pw.print("cpu");
5786 } else {
5787 pw.print("unknown");
5788 }
5789 pw.print(" use: ");
5790 TimeUtils.formatDuration(ew.usedTime, pw);
5791 pw.print(" over ");
5792 TimeUtils.formatDuration(ew.overTime, pw);
5793 if (ew.overTime != 0) {
5794 pw.print(" (");
5795 pw.print((ew.usedTime*100)/ew.overTime);
5796 pw.println("%)");
5797 }
5798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005799 }
5800 uidActivity = true;
5801 }
5802 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005803
5804 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5805 = u.getPackageStats();
5806 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
5807 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
5808 pw.println(":");
5809 boolean apkActivity = false;
5810 final Uid.Pkg ps = packageStats.valueAt(ipkg);
5811 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
5812 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
5813 pw.print(prefix); pw.print(" Wakeup alarm ");
5814 pw.print(alarms.keyAt(iwa)); pw.print(": ");
5815 pw.print(alarms.valueAt(iwa).getCountLocked(which));
5816 pw.println(" times");
5817 apkActivity = true;
5818 }
5819 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
5820 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
5821 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
5822 final long startTime = ss.getStartTime(batteryUptime, which);
5823 final int starts = ss.getStarts(which);
5824 final int launches = ss.getLaunches(which);
5825 if (startTime != 0 || starts != 0 || launches != 0) {
5826 sb.setLength(0);
5827 sb.append(prefix); sb.append(" Service ");
5828 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
5829 sb.append(prefix); sb.append(" Created for: ");
5830 formatTimeMs(sb, startTime / 1000);
5831 sb.append("uptime\n");
5832 sb.append(prefix); sb.append(" Starts: ");
5833 sb.append(starts);
5834 sb.append(", launches: "); sb.append(launches);
5835 pw.println(sb.toString());
5836 apkActivity = true;
5837 }
5838 }
5839 if (!apkActivity) {
5840 pw.print(prefix); pw.println(" (nothing executed)");
5841 }
5842 uidActivity = true;
5843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005844 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005845 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005846 }
5847 }
5848 }
5849
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005850 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005851 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005852 int diff = oldval ^ newval;
5853 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005854 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005855 for (int i=0; i<descriptions.length; i++) {
5856 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005857 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005858 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005859 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005860 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005861 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005862 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
5863 didWake = true;
5864 pw.print("=");
5865 if (longNames) {
5866 UserHandle.formatUid(pw, wakelockTag.uid);
5867 pw.print(":\"");
5868 pw.print(wakelockTag.string);
5869 pw.print("\"");
5870 } else {
5871 pw.print(wakelockTag.poolIdx);
5872 }
5873 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005874 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005875 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005876 pw.print("=");
5877 int val = (newval&bd.mask)>>bd.shift;
5878 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005879 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005880 } else {
5881 pw.print(val);
5882 }
5883 }
5884 }
5885 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005886 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07005887 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005888 if (longNames) {
5889 UserHandle.formatUid(pw, wakelockTag.uid);
5890 pw.print(":\"");
5891 pw.print(wakelockTag.string);
5892 pw.print("\"");
5893 } else {
5894 pw.print(wakelockTag.poolIdx);
5895 }
5896 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005897 }
Mike Mac2f518a2017-09-19 16:06:03 -07005898
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005899 public void prepareForDumpLocked() {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07005900 // We don't need to require subclasses implement this.
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005901 }
5902
5903 public static class HistoryPrinter {
5904 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005905 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005906 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005907 int oldStatus = -1;
5908 int oldHealth = -1;
5909 int oldPlug = -1;
5910 int oldTemp = -1;
5911 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005912 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005913 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005914
Dianne Hackborn3251b902014-06-20 14:40:53 -07005915 void reset() {
5916 oldState = oldState2 = 0;
5917 oldLevel = -1;
5918 oldStatus = -1;
5919 oldHealth = -1;
5920 oldPlug = -1;
5921 oldTemp = -1;
5922 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005923 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07005924 }
5925
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005926 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005927 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005928 if (!checkin) {
5929 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005930 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005931 pw.print(" (");
5932 pw.print(rec.numReadInts);
5933 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005934 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005935 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5936 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005937 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005938 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005939 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005940 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005941 }
5942 lastTime = rec.time;
5943 }
5944 if (rec.cmd == HistoryItem.CMD_START) {
5945 if (checkin) {
5946 pw.print(":");
5947 }
5948 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005949 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005950 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
5951 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005952 if (checkin) {
5953 pw.print(":");
5954 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005955 if (rec.cmd == HistoryItem.CMD_RESET) {
5956 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005957 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005958 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005959 pw.print("TIME:");
5960 if (checkin) {
5961 pw.println(rec.currentTime);
5962 } else {
5963 pw.print(" ");
5964 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5965 rec.currentTime).toString());
5966 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005967 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
5968 if (checkin) {
5969 pw.print(":");
5970 }
5971 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005972 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
5973 if (checkin) {
5974 pw.print(":");
5975 }
5976 pw.println("*OVERFLOW*");
5977 } else {
5978 if (!checkin) {
5979 if (rec.batteryLevel < 10) pw.print("00");
5980 else if (rec.batteryLevel < 100) pw.print("0");
5981 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005982 if (verbose) {
5983 pw.print(" ");
5984 if (rec.states < 0) ;
5985 else if (rec.states < 0x10) pw.print("0000000");
5986 else if (rec.states < 0x100) pw.print("000000");
5987 else if (rec.states < 0x1000) pw.print("00000");
5988 else if (rec.states < 0x10000) pw.print("0000");
5989 else if (rec.states < 0x100000) pw.print("000");
5990 else if (rec.states < 0x1000000) pw.print("00");
5991 else if (rec.states < 0x10000000) pw.print("0");
5992 pw.print(Integer.toHexString(rec.states));
5993 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005994 } else {
5995 if (oldLevel != rec.batteryLevel) {
5996 oldLevel = rec.batteryLevel;
5997 pw.print(",Bl="); pw.print(rec.batteryLevel);
5998 }
5999 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006000 if (oldStatus != rec.batteryStatus) {
6001 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006002 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006003 switch (oldStatus) {
6004 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006005 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006006 break;
6007 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006008 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006009 break;
6010 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006011 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006012 break;
6013 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006014 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006015 break;
6016 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006017 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006018 break;
6019 default:
6020 pw.print(oldStatus);
6021 break;
6022 }
6023 }
6024 if (oldHealth != rec.batteryHealth) {
6025 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006026 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006027 switch (oldHealth) {
6028 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006029 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006030 break;
6031 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006032 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006033 break;
6034 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006035 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006036 break;
6037 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006038 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006039 break;
6040 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006041 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006042 break;
6043 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006044 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006045 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006046 case BatteryManager.BATTERY_HEALTH_COLD:
6047 pw.print(checkin ? "c" : "cold");
6048 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006049 default:
6050 pw.print(oldHealth);
6051 break;
6052 }
6053 }
6054 if (oldPlug != rec.batteryPlugType) {
6055 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006056 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006057 switch (oldPlug) {
6058 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006059 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006060 break;
6061 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006062 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006063 break;
6064 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006065 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006066 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006067 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006068 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006069 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006070 default:
6071 pw.print(oldPlug);
6072 break;
6073 }
6074 }
6075 if (oldTemp != rec.batteryTemperature) {
6076 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006077 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006078 pw.print(oldTemp);
6079 }
6080 if (oldVolt != rec.batteryVoltage) {
6081 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006082 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006083 pw.print(oldVolt);
6084 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006085 final int chargeMAh = rec.batteryChargeUAh / 1000;
6086 if (oldChargeMAh != chargeMAh) {
6087 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07006088 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006089 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07006090 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006091 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006092 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006093 printBitDescriptions(pw, oldState2, rec.states2, null,
6094 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006095 if (rec.wakeReasonTag != null) {
6096 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006097 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006098 pw.print(rec.wakeReasonTag.poolIdx);
6099 } else {
6100 pw.print(" wake_reason=");
6101 pw.print(rec.wakeReasonTag.uid);
6102 pw.print(":\"");
6103 pw.print(rec.wakeReasonTag.string);
6104 pw.print("\"");
6105 }
6106 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08006107 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006108 pw.print(checkin ? "," : " ");
6109 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
6110 pw.print("+");
6111 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
6112 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006113 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006114 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
6115 : HISTORY_EVENT_NAMES;
6116 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
6117 | HistoryItem.EVENT_FLAG_FINISH);
6118 if (idx >= 0 && idx < eventNames.length) {
6119 pw.print(eventNames[idx]);
6120 } else {
6121 pw.print(checkin ? "Ev" : "event");
6122 pw.print(idx);
6123 }
6124 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006125 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006126 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08006127 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08006128 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
6129 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006130 pw.print(":\"");
6131 pw.print(rec.eventTag.string);
6132 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006133 }
6134 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006135 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006136 if (rec.stepDetails != null) {
6137 if (!checkin) {
6138 pw.print(" Details: cpu=");
6139 pw.print(rec.stepDetails.userTime);
6140 pw.print("u+");
6141 pw.print(rec.stepDetails.systemTime);
6142 pw.print("s");
6143 if (rec.stepDetails.appCpuUid1 >= 0) {
6144 pw.print(" (");
6145 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
6146 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6147 if (rec.stepDetails.appCpuUid2 >= 0) {
6148 pw.print(", ");
6149 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
6150 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6151 }
6152 if (rec.stepDetails.appCpuUid3 >= 0) {
6153 pw.print(", ");
6154 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
6155 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6156 }
6157 pw.print(')');
6158 }
6159 pw.println();
6160 pw.print(" /proc/stat=");
6161 pw.print(rec.stepDetails.statUserTime);
6162 pw.print(" usr, ");
6163 pw.print(rec.stepDetails.statSystemTime);
6164 pw.print(" sys, ");
6165 pw.print(rec.stepDetails.statIOWaitTime);
6166 pw.print(" io, ");
6167 pw.print(rec.stepDetails.statIrqTime);
6168 pw.print(" irq, ");
6169 pw.print(rec.stepDetails.statSoftIrqTime);
6170 pw.print(" sirq, ");
6171 pw.print(rec.stepDetails.statIdlTime);
6172 pw.print(" idle");
6173 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
6174 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
6175 + rec.stepDetails.statSoftIrqTime;
6176 int total = totalRun + rec.stepDetails.statIdlTime;
6177 if (total > 0) {
6178 pw.print(" (");
6179 float perc = ((float)totalRun) / ((float)total) * 100;
6180 pw.print(String.format("%.1f%%", perc));
6181 pw.print(" of ");
6182 StringBuilder sb = new StringBuilder(64);
6183 formatTimeMsNoSpace(sb, total*10);
6184 pw.print(sb);
6185 pw.print(")");
6186 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006187 pw.print(", PlatformIdleStat ");
6188 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006189 pw.println();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006190
6191 pw.print(", SubsystemPowerState ");
6192 pw.print(rec.stepDetails.statSubsystemPowerState);
6193 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006194 } else {
6195 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6196 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
6197 pw.print(rec.stepDetails.userTime);
6198 pw.print(":");
6199 pw.print(rec.stepDetails.systemTime);
6200 if (rec.stepDetails.appCpuUid1 >= 0) {
6201 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
6202 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6203 if (rec.stepDetails.appCpuUid2 >= 0) {
6204 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
6205 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6206 }
6207 if (rec.stepDetails.appCpuUid3 >= 0) {
6208 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
6209 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6210 }
6211 }
6212 pw.println();
6213 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6214 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
6215 pw.print(rec.stepDetails.statUserTime);
6216 pw.print(',');
6217 pw.print(rec.stepDetails.statSystemTime);
6218 pw.print(',');
6219 pw.print(rec.stepDetails.statIOWaitTime);
6220 pw.print(',');
6221 pw.print(rec.stepDetails.statIrqTime);
6222 pw.print(',');
6223 pw.print(rec.stepDetails.statSoftIrqTime);
6224 pw.print(',');
6225 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006226 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006227 if (rec.stepDetails.statPlatformIdleState != null) {
6228 pw.print(rec.stepDetails.statPlatformIdleState);
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006229 if (rec.stepDetails.statSubsystemPowerState != null) {
6230 pw.print(',');
6231 }
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006232 }
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006233
6234 if (rec.stepDetails.statSubsystemPowerState != null) {
6235 pw.print(rec.stepDetails.statSubsystemPowerState);
6236 }
6237 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006238 }
6239 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006240 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006241 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006242 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006243 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006244
6245 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
6246 UserHandle.formatUid(pw, uid);
6247 pw.print("=");
6248 pw.print(utime);
6249 pw.print("u+");
6250 pw.print(stime);
6251 pw.print("s");
6252 }
6253
6254 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
6255 pw.print('/');
6256 pw.print(uid);
6257 pw.print(":");
6258 pw.print(utime);
6259 pw.print(":");
6260 pw.print(stime);
6261 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006262 }
6263
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006264 private void printSizeValue(PrintWriter pw, long size) {
6265 float result = size;
6266 String suffix = "";
6267 if (result >= 10*1024) {
6268 suffix = "KB";
6269 result = result / 1024;
6270 }
6271 if (result >= 10*1024) {
6272 suffix = "MB";
6273 result = result / 1024;
6274 }
6275 if (result >= 10*1024) {
6276 suffix = "GB";
6277 result = result / 1024;
6278 }
6279 if (result >= 10*1024) {
6280 suffix = "TB";
6281 result = result / 1024;
6282 }
6283 if (result >= 10*1024) {
6284 suffix = "PB";
6285 result = result / 1024;
6286 }
6287 pw.print((int)result);
6288 pw.print(suffix);
6289 }
6290
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006291 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
6292 String label3, long estimatedTime) {
6293 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006294 return false;
6295 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006296 pw.print(label1);
6297 pw.print(label2);
6298 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006299 StringBuilder sb = new StringBuilder(64);
6300 formatTimeMs(sb, estimatedTime);
6301 pw.print(sb);
6302 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006303 return true;
6304 }
6305
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006306 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
6307 LevelStepTracker steps, boolean checkin) {
6308 if (steps == null) {
6309 return false;
6310 }
6311 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006312 if (count <= 0) {
6313 return false;
6314 }
6315 if (!checkin) {
6316 pw.println(header);
6317 }
Kweku Adams030980a2015-04-01 16:07:48 -07006318 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006319 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006320 long duration = steps.getDurationAt(i);
6321 int level = steps.getLevelAt(i);
6322 long initMode = steps.getInitModeAt(i);
6323 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006324 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006325 lineArgs[0] = Long.toString(duration);
6326 lineArgs[1] = Integer.toString(level);
6327 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6328 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6329 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
6330 case Display.STATE_ON: lineArgs[2] = "s+"; break;
6331 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
6332 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07006333 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006334 }
6335 } else {
6336 lineArgs[2] = "";
6337 }
6338 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6339 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
6340 } else {
6341 lineArgs[3] = "";
6342 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006343 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07006344 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006345 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07006346 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006347 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006348 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
6349 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006350 pw.print(prefix);
6351 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006352 TimeUtils.formatDuration(duration, pw);
6353 pw.print(" to "); pw.print(level);
6354 boolean haveModes = false;
6355 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6356 pw.print(" (");
6357 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6358 case Display.STATE_OFF: pw.print("screen-off"); break;
6359 case Display.STATE_ON: pw.print("screen-on"); break;
6360 case Display.STATE_DOZE: pw.print("screen-doze"); break;
6361 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07006362 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006363 }
6364 haveModes = true;
6365 }
6366 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6367 pw.print(haveModes ? ", " : " (");
6368 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
6369 ? "power-save-on" : "power-save-off");
6370 haveModes = true;
6371 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006372 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6373 pw.print(haveModes ? ", " : " (");
6374 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6375 ? "device-idle-on" : "device-idle-off");
6376 haveModes = true;
6377 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006378 if (haveModes) {
6379 pw.print(")");
6380 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006381 pw.println();
6382 }
6383 }
6384 return true;
6385 }
6386
Kweku Adams87b19ec2017-10-09 12:40:03 -07006387 private static void dumpDurationSteps(ProtoOutputStream proto, long fieldId,
6388 LevelStepTracker steps) {
6389 if (steps == null) {
6390 return;
6391 }
6392 int count = steps.mNumStepDurations;
Kweku Adams87b19ec2017-10-09 12:40:03 -07006393 for (int i = 0; i < count; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07006394 long token = proto.start(fieldId);
Kweku Adams87b19ec2017-10-09 12:40:03 -07006395 proto.write(SystemProto.BatteryLevelStep.DURATION_MS, steps.getDurationAt(i));
6396 proto.write(SystemProto.BatteryLevelStep.LEVEL, steps.getLevelAt(i));
6397
6398 final long initMode = steps.getInitModeAt(i);
6399 final long modMode = steps.getModModeAt(i);
6400
6401 int ds = SystemProto.BatteryLevelStep.DS_MIXED;
6402 if ((modMode & STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6403 switch ((int) (initMode & STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6404 case Display.STATE_OFF:
6405 ds = SystemProto.BatteryLevelStep.DS_OFF;
6406 break;
6407 case Display.STATE_ON:
6408 ds = SystemProto.BatteryLevelStep.DS_ON;
6409 break;
6410 case Display.STATE_DOZE:
6411 ds = SystemProto.BatteryLevelStep.DS_DOZE;
6412 break;
6413 case Display.STATE_DOZE_SUSPEND:
6414 ds = SystemProto.BatteryLevelStep.DS_DOZE_SUSPEND;
6415 break;
6416 default:
6417 ds = SystemProto.BatteryLevelStep.DS_ERROR;
6418 break;
6419 }
6420 }
6421 proto.write(SystemProto.BatteryLevelStep.DISPLAY_STATE, ds);
6422
6423 int psm = SystemProto.BatteryLevelStep.PSM_MIXED;
6424 if ((modMode & STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6425 psm = (initMode & STEP_LEVEL_MODE_POWER_SAVE) != 0
6426 ? SystemProto.BatteryLevelStep.PSM_ON : SystemProto.BatteryLevelStep.PSM_OFF;
6427 }
6428 proto.write(SystemProto.BatteryLevelStep.POWER_SAVE_MODE, psm);
6429
6430 int im = SystemProto.BatteryLevelStep.IM_MIXED;
6431 if ((modMode & STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6432 im = (initMode & STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6433 ? SystemProto.BatteryLevelStep.IM_ON : SystemProto.BatteryLevelStep.IM_OFF;
6434 }
6435 proto.write(SystemProto.BatteryLevelStep.IDLE_MODE, im);
6436
6437 proto.end(token);
6438 }
6439 }
6440
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006441 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006442 public static final int DUMP_DAILY_ONLY = 1<<2;
6443 public static final int DUMP_HISTORY_ONLY = 1<<3;
6444 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
6445 public static final int DUMP_VERBOSE = 1<<5;
6446 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006447
Dianne Hackborn37de0982014-05-09 09:32:18 -07006448 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
6449 final HistoryPrinter hprinter = new HistoryPrinter();
6450 final HistoryItem rec = new HistoryItem();
6451 long lastTime = -1;
6452 long baseTime = -1;
6453 boolean printed = false;
6454 HistoryEventTracker tracker = null;
6455 while (getNextHistoryLocked(rec)) {
6456 lastTime = rec.time;
6457 if (baseTime < 0) {
6458 baseTime = lastTime;
6459 }
6460 if (rec.time >= histStart) {
6461 if (histStart >= 0 && !printed) {
6462 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07006463 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006464 || rec.cmd == HistoryItem.CMD_START
6465 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006466 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006467 hprinter.printNextItem(pw, rec, baseTime, checkin,
6468 (flags&DUMP_VERBOSE) != 0);
6469 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006470 } else if (rec.currentTime != 0) {
6471 printed = true;
6472 byte cmd = rec.cmd;
6473 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006474 hprinter.printNextItem(pw, rec, baseTime, checkin,
6475 (flags&DUMP_VERBOSE) != 0);
6476 rec.cmd = cmd;
6477 }
6478 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006479 if (rec.cmd != HistoryItem.CMD_UPDATE) {
6480 hprinter.printNextItem(pw, rec, baseTime, checkin,
6481 (flags&DUMP_VERBOSE) != 0);
6482 rec.cmd = HistoryItem.CMD_UPDATE;
6483 }
6484 int oldEventCode = rec.eventCode;
6485 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006486 rec.eventTag = new HistoryTag();
6487 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
6488 HashMap<String, SparseIntArray> active
6489 = tracker.getStateForEvent(i);
6490 if (active == null) {
6491 continue;
6492 }
6493 for (HashMap.Entry<String, SparseIntArray> ent
6494 : active.entrySet()) {
6495 SparseIntArray uids = ent.getValue();
6496 for (int j=0; j<uids.size(); j++) {
6497 rec.eventCode = i;
6498 rec.eventTag.string = ent.getKey();
6499 rec.eventTag.uid = uids.keyAt(j);
6500 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006501 hprinter.printNextItem(pw, rec, baseTime, checkin,
6502 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006503 rec.wakeReasonTag = null;
6504 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006505 }
6506 }
6507 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006508 rec.eventCode = oldEventCode;
6509 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006510 tracker = null;
6511 }
6512 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006513 hprinter.printNextItem(pw, rec, baseTime, checkin,
6514 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07006515 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
6516 // This is an attempt to aggregate the previous state and generate
6517 // fake events to reflect that state at the point where we start
6518 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07006519 if (tracker == null) {
6520 tracker = new HistoryEventTracker();
6521 }
6522 tracker.updateState(rec.eventCode, rec.eventTag.string,
6523 rec.eventTag.uid, rec.eventTag.poolIdx);
6524 }
6525 }
6526 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07006527 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006528 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
6529 }
6530 }
6531
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006532 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
6533 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
6534 if (steps == null) {
6535 return;
6536 }
6537 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
6538 if (timeRemaining >= 0) {
6539 pw.print(prefix); pw.print(label); pw.print(" total time: ");
6540 tmpSb.setLength(0);
6541 formatTimeMs(tmpSb, timeRemaining);
6542 pw.print(tmpSb);
6543 pw.print(" (from "); pw.print(tmpOutInt[0]);
6544 pw.println(" steps)");
6545 }
6546 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6547 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6548 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
6549 if (estimatedTime > 0) {
6550 pw.print(prefix); pw.print(label); pw.print(" ");
6551 pw.print(STEP_LEVEL_MODE_LABELS[i]);
6552 pw.print(" time: ");
6553 tmpSb.setLength(0);
6554 formatTimeMs(tmpSb, estimatedTime);
6555 pw.print(tmpSb);
6556 pw.print(" (from "); pw.print(tmpOutInt[0]);
6557 pw.println(" steps)");
6558 }
6559 }
6560 }
6561
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006562 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
6563 ArrayList<PackageChange> changes) {
6564 if (changes == null) {
6565 return;
6566 }
6567 pw.print(prefix); pw.println("Package changes:");
6568 for (int i=0; i<changes.size(); i++) {
6569 PackageChange pc = changes.get(i);
6570 if (pc.mUpdate) {
6571 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
6572 pw.print(" vers="); pw.println(pc.mVersionCode);
6573 } else {
6574 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
6575 }
6576 }
6577 }
6578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006579 /**
6580 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
6581 *
6582 * @param pw a Printer to receive the dump output.
6583 */
6584 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006585 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006586 prepareForDumpLocked();
6587
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006588 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006589 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006590
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006591 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006592 final long historyTotalSize = getHistoryTotalSize();
6593 final long historyUsedSize = getHistoryUsedSize();
6594 if (startIteratingHistoryLocked()) {
6595 try {
6596 pw.print("Battery History (");
6597 pw.print((100*historyUsedSize)/historyTotalSize);
6598 pw.print("% used, ");
6599 printSizeValue(pw, historyUsedSize);
6600 pw.print(" used of ");
6601 printSizeValue(pw, historyTotalSize);
6602 pw.print(", ");
6603 pw.print(getHistoryStringPoolSize());
6604 pw.print(" strings using ");
6605 printSizeValue(pw, getHistoryStringPoolBytes());
6606 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07006607 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006608 pw.println();
6609 } finally {
6610 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006611 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006612 }
6613
6614 if (startIteratingOldHistoryLocked()) {
6615 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006616 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006617 pw.println("Old battery History:");
6618 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006619 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006620 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006621 if (baseTime < 0) {
6622 baseTime = rec.time;
6623 }
6624 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006625 }
6626 pw.println();
6627 } finally {
6628 finishIteratingOldHistoryLocked();
6629 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006630 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006631 }
6632
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006633 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006634 return;
6635 }
6636
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006637 if (!filtering) {
6638 SparseArray<? extends Uid> uidStats = getUidStats();
6639 final int NU = uidStats.size();
6640 boolean didPid = false;
6641 long nowRealtime = SystemClock.elapsedRealtime();
6642 for (int i=0; i<NU; i++) {
6643 Uid uid = uidStats.valueAt(i);
6644 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
6645 if (pids != null) {
6646 for (int j=0; j<pids.size(); j++) {
6647 Uid.Pid pid = pids.valueAt(j);
6648 if (!didPid) {
6649 pw.println("Per-PID Stats:");
6650 didPid = true;
6651 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006652 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
6653 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006654 pw.print(" PID "); pw.print(pids.keyAt(j));
6655 pw.print(" wake time: ");
6656 TimeUtils.formatDuration(time, pw);
6657 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006658 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006659 }
6660 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006661 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006662 pw.println();
6663 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006664 }
6665
6666 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006667 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
6668 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006669 long timeRemaining = computeBatteryTimeRemaining(
6670 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006671 if (timeRemaining >= 0) {
6672 pw.print(" Estimated discharge time remaining: ");
6673 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6674 pw.println();
6675 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006676 final LevelStepTracker steps = getDischargeLevelStepTracker();
6677 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6678 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
6679 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6680 STEP_LEVEL_MODE_VALUES[i], null));
6681 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006682 pw.println();
6683 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006684 if (dumpDurationSteps(pw, " ", "Charge step durations:",
6685 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006686 long timeRemaining = computeChargeTimeRemaining(
6687 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006688 if (timeRemaining >= 0) {
6689 pw.print(" Estimated charge time remaining: ");
6690 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6691 pw.println();
6692 }
6693 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006694 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006695 }
Dianne Hackbornc81983a2017-10-20 16:16:32 -07006696 if (!filtering || (flags & DUMP_DAILY_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006697 pw.println("Daily stats:");
6698 pw.print(" Current start time: ");
6699 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6700 getCurrentDailyStartTime()).toString());
6701 pw.print(" Next min deadline: ");
6702 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6703 getNextMinDailyDeadline()).toString());
6704 pw.print(" Next max deadline: ");
6705 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6706 getNextMaxDailyDeadline()).toString());
6707 StringBuilder sb = new StringBuilder(64);
6708 int[] outInt = new int[1];
6709 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
6710 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006711 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
6712 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006713 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006714 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
6715 dsteps, false)) {
6716 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6717 sb, outInt);
6718 }
6719 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6720 csteps, false)) {
6721 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6722 sb, outInt);
6723 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006724 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006725 } else {
6726 pw.println(" Current daily steps:");
6727 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6728 sb, outInt);
6729 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6730 sb, outInt);
6731 }
6732 }
6733 DailyItem dit;
6734 int curIndex = 0;
6735 while ((dit=getDailyItemLocked(curIndex)) != null) {
6736 curIndex++;
6737 if ((flags&DUMP_DAILY_ONLY) != 0) {
6738 pw.println();
6739 }
6740 pw.print(" Daily from ");
6741 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6742 pw.print(" to ");
6743 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6744 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006745 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006746 if (dumpDurationSteps(pw, " ",
6747 " Discharge step durations:", dit.mDischargeSteps, false)) {
6748 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6749 sb, outInt);
6750 }
6751 if (dumpDurationSteps(pw, " ",
6752 " Charge step durations:", dit.mChargeSteps, false)) {
6753 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6754 sb, outInt);
6755 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006756 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006757 } else {
6758 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6759 sb, outInt);
6760 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6761 sb, outInt);
6762 }
6763 }
6764 pw.println();
6765 }
6766 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006767 pw.println("Statistics since last charge:");
6768 pw.println(" System starts: " + getStartCount()
6769 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006770 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6771 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006772 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006773 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006774 }
Mike Mac2f518a2017-09-19 16:06:03 -07006775
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006776 // This is called from BatteryStatsService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006777 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006778 public void dumpCheckinLocked(Context context, PrintWriter pw,
6779 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006780 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006781
6782 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006783 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6784 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006785
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006786 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6787
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006788 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07006789 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006790 try {
6791 for (int i=0; i<getHistoryStringPoolSize(); i++) {
6792 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6793 pw.print(HISTORY_STRING_POOL); pw.print(',');
6794 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006795 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006796 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006797 pw.print(",\"");
6798 String str = getHistoryTagPoolString(i);
6799 str = str.replace("\\", "\\\\");
6800 str = str.replace("\"", "\\\"");
6801 pw.print(str);
6802 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006803 pw.println();
6804 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006805 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006806 } finally {
6807 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08006808 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006809 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006810 }
6811
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006812 if ((flags & DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006813 return;
6814 }
6815
Dianne Hackborne4a59512010-12-07 11:08:07 -08006816 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006817 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08006818 for (int i=0; i<apps.size(); i++) {
6819 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006820 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
6821 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08006822 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006823 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
6824 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006825 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006826 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006827 }
6828 SparseArray<? extends Uid> uidStats = getUidStats();
6829 final int NU = uidStats.size();
6830 String[] lineArgs = new String[2];
6831 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006832 int uid = UserHandle.getAppId(uidStats.keyAt(i));
6833 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
6834 if (pkgs != null && !pkgs.second.value) {
6835 pkgs.second.value = true;
6836 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08006837 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006838 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006839 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
6840 (Object[])lineArgs);
6841 }
6842 }
6843 }
6844 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006845 if ((flags & DUMP_DAILY_ONLY) == 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006846 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006847 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07006848 long timeRemaining = computeBatteryTimeRemaining(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 */, DISCHARGE_TIME_REMAIN_DATA,
6852 (Object[])lineArgs);
6853 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006854 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07006855 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006856 if (timeRemaining >= 0) {
6857 lineArgs[0] = Long.toString(timeRemaining);
6858 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
6859 (Object[])lineArgs);
6860 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006861 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
6862 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006864 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006865
Kweku Adams87b19ec2017-10-09 12:40:03 -07006866 /** Dump #STATS_SINCE_CHARGED batterystats data to a proto. @hide */
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006867 public void dumpProtoLocked(Context context, FileDescriptor fd, List<ApplicationInfo> apps,
Kweku Adams6ccebf22017-12-11 12:30:35 -08006868 int flags) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006869 final ProtoOutputStream proto = new ProtoOutputStream(fd);
6870 final long bToken = proto.start(BatteryStatsServiceDumpProto.BATTERYSTATS);
6871 prepareForDumpLocked();
6872
6873 proto.write(BatteryStatsProto.REPORT_VERSION, CHECKIN_VERSION);
6874 proto.write(BatteryStatsProto.PARCEL_VERSION, getParcelVersion());
6875 proto.write(BatteryStatsProto.START_PLATFORM_VERSION, getStartPlatformVersion());
6876 proto.write(BatteryStatsProto.END_PLATFORM_VERSION, getEndPlatformVersion());
6877
Kweku Adams6ccebf22017-12-11 12:30:35 -08006878 // History intentionally not included in proto dump.
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006879
6880 if ((flags & (DUMP_HISTORY_ONLY | DUMP_DAILY_ONLY)) == 0) {
Kweku Adams103351f2017-10-16 14:39:34 -07006881 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false,
6882 (flags & DUMP_DEVICE_WIFI_ONLY) != 0);
6883 helper.create(this);
6884 helper.refreshStats(STATS_SINCE_CHARGED, UserHandle.USER_ALL);
6885
6886 dumpProtoAppsLocked(proto, helper, apps);
6887 dumpProtoSystemLocked(proto, helper);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006888 }
6889
6890 proto.end(bToken);
6891 proto.flush();
6892 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07006893
Kweku Adams103351f2017-10-16 14:39:34 -07006894 private void dumpProtoAppsLocked(ProtoOutputStream proto, BatteryStatsHelper helper,
6895 List<ApplicationInfo> apps) {
6896 final int which = STATS_SINCE_CHARGED;
6897 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
6898 final long rawRealtimeMs = SystemClock.elapsedRealtime();
6899 final long rawRealtimeUs = rawRealtimeMs * 1000;
6900 final long batteryUptimeUs = getBatteryUptime(rawUptimeUs);
6901
6902 SparseArray<ArrayList<String>> aidToPackages = new SparseArray<>();
6903 if (apps != null) {
6904 for (int i = 0; i < apps.size(); ++i) {
6905 ApplicationInfo ai = apps.get(i);
6906 int aid = UserHandle.getAppId(ai.uid);
6907 ArrayList<String> pkgs = aidToPackages.get(aid);
6908 if (pkgs == null) {
6909 pkgs = new ArrayList<String>();
6910 aidToPackages.put(aid, pkgs);
6911 }
6912 pkgs.add(ai.packageName);
6913 }
6914 }
6915
6916 SparseArray<BatterySipper> uidToSipper = new SparseArray<>();
6917 final List<BatterySipper> sippers = helper.getUsageList();
6918 if (sippers != null) {
6919 for (int i = 0; i < sippers.size(); ++i) {
6920 final BatterySipper bs = sippers.get(i);
6921 if (bs.drainType != BatterySipper.DrainType.APP) {
6922 // Others are handled by dumpProtoSystemLocked()
6923 continue;
6924 }
6925 uidToSipper.put(bs.uidObj.getUid(), bs);
6926 }
6927 }
6928
6929 SparseArray<? extends Uid> uidStats = getUidStats();
6930 final int n = uidStats.size();
6931 for (int iu = 0; iu < n; ++iu) {
6932 final long uTkn = proto.start(BatteryStatsProto.UIDS);
6933 final Uid u = uidStats.valueAt(iu);
6934
6935 final int uid = uidStats.keyAt(iu);
6936 proto.write(UidProto.UID, uid);
6937
6938 // Print packages and apk stats (UID_DATA & APK_DATA)
6939 ArrayList<String> pkgs = aidToPackages.get(UserHandle.getAppId(uid));
6940 if (pkgs == null) {
6941 pkgs = new ArrayList<String>();
6942 }
6943 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats =
6944 u.getPackageStats();
6945 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
6946 String pkg = packageStats.keyAt(ipkg);
6947 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats =
6948 packageStats.valueAt(ipkg).getServiceStats();
6949 if (serviceStats.size() == 0) {
6950 // Due to the way ActivityManagerService logs wakeup alarms, some packages (for
6951 // example, "android") may be included in the packageStats that aren't part of
6952 // the UID. If they don't have any services, then they shouldn't be listed here.
6953 // These packages won't be a part in the pkgs List.
6954 continue;
6955 }
6956
6957 final long pToken = proto.start(UidProto.PACKAGES);
6958 proto.write(UidProto.Package.NAME, pkg);
6959 // Remove from the packages list since we're logging it here.
6960 pkgs.remove(pkg);
6961
6962 for (int isvc = serviceStats.size() - 1; isvc >= 0; --isvc) {
6963 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
6964 long sToken = proto.start(UidProto.Package.SERVICES);
6965
6966 proto.write(UidProto.Package.Service.NAME, serviceStats.keyAt(isvc));
6967 proto.write(UidProto.Package.Service.START_DURATION_MS,
6968 roundUsToMs(ss.getStartTime(batteryUptimeUs, which)));
6969 proto.write(UidProto.Package.Service.START_COUNT, ss.getStarts(which));
6970 proto.write(UidProto.Package.Service.LAUNCH_COUNT, ss.getLaunches(which));
6971
6972 proto.end(sToken);
6973 }
6974 proto.end(pToken);
6975 }
6976 // Print any remaining packages that weren't in the packageStats map. pkgs is pulled
6977 // from PackageManager data. Packages are only included in packageStats if there was
6978 // specific data tracked for them (services and wakeup alarms, etc.).
6979 for (String p : pkgs) {
6980 final long pToken = proto.start(UidProto.PACKAGES);
6981 proto.write(UidProto.Package.NAME, p);
6982 proto.end(pToken);
6983 }
6984
6985 // Total wakelock data (AGGREGATED_WAKELOCK_DATA)
6986 if (u.getAggregatedPartialWakelockTimer() != null) {
6987 final Timer timer = u.getAggregatedPartialWakelockTimer();
6988 // Times are since reset (regardless of 'which')
6989 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
6990 final Timer bgTimer = timer.getSubTimer();
6991 final long bgTimeMs = bgTimer != null
6992 ? bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
6993 final long awToken = proto.start(UidProto.AGGREGATED_WAKELOCK);
6994 proto.write(UidProto.AggregatedWakelock.PARTIAL_DURATION_MS, totTimeMs);
6995 proto.write(UidProto.AggregatedWakelock.BACKGROUND_PARTIAL_DURATION_MS, bgTimeMs);
6996 proto.end(awToken);
6997 }
6998
6999 // Audio (AUDIO_DATA)
7000 dumpTimer(proto, UidProto.AUDIO, u.getAudioTurnedOnTimer(), rawRealtimeUs, which);
7001
7002 // Bluetooth Controller (BLUETOOTH_CONTROLLER_DATA)
7003 dumpControllerActivityProto(proto, UidProto.BLUETOOTH_CONTROLLER,
7004 u.getBluetoothControllerActivity(), which);
7005
7006 // BLE scans (BLUETOOTH_MISC_DATA) (uses totalDurationMsLocked and MaxDurationMsLocked)
7007 final Timer bleTimer = u.getBluetoothScanTimer();
7008 if (bleTimer != null) {
7009 final long bmToken = proto.start(UidProto.BLUETOOTH_MISC);
7010
7011 dumpTimer(proto, UidProto.BluetoothMisc.APPORTIONED_BLE_SCAN, bleTimer,
7012 rawRealtimeUs, which);
7013 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN,
7014 u.getBluetoothScanBackgroundTimer(), rawRealtimeUs, which);
7015 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
7016 dumpTimer(proto, UidProto.BluetoothMisc.UNOPTIMIZED_BLE_SCAN,
7017 u.getBluetoothUnoptimizedScanTimer(), rawRealtimeUs, which);
7018 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
7019 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_UNOPTIMIZED_BLE_SCAN,
7020 u.getBluetoothUnoptimizedScanBackgroundTimer(), rawRealtimeUs, which);
7021 // Result counters
7022 proto.write(UidProto.BluetoothMisc.BLE_SCAN_RESULT_COUNT,
7023 u.getBluetoothScanResultCounter() != null
7024 ? u.getBluetoothScanResultCounter().getCountLocked(which) : 0);
7025 proto.write(UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN_RESULT_COUNT,
7026 u.getBluetoothScanResultBgCounter() != null
7027 ? u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0);
7028
7029 proto.end(bmToken);
7030 }
7031
7032 // Camera (CAMERA_DATA)
7033 dumpTimer(proto, UidProto.CAMERA, u.getCameraTurnedOnTimer(), rawRealtimeUs, which);
7034
7035 // CPU stats (CPU_DATA & CPU_TIMES_AT_FREQ_DATA)
7036 final long cpuToken = proto.start(UidProto.CPU);
7037 proto.write(UidProto.Cpu.USER_DURATION_MS, roundUsToMs(u.getUserCpuTimeUs(which)));
7038 proto.write(UidProto.Cpu.SYSTEM_DURATION_MS, roundUsToMs(u.getSystemCpuTimeUs(which)));
7039
7040 final long[] cpuFreqs = getCpuFreqs();
7041 if (cpuFreqs != null) {
7042 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
7043 // If total cpuFreqTimes is null, then we don't need to check for
7044 // screenOffCpuFreqTimes.
7045 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
7046 long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
7047 if (screenOffCpuFreqTimeMs == null) {
7048 screenOffCpuFreqTimeMs = new long[cpuFreqTimeMs.length];
7049 }
7050 for (int ic = 0; ic < cpuFreqTimeMs.length; ++ic) {
7051 long cToken = proto.start(UidProto.Cpu.BY_FREQUENCY);
7052 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7053 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7054 cpuFreqTimeMs[ic]);
7055 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7056 screenOffCpuFreqTimeMs[ic]);
7057 proto.end(cToken);
7058 }
7059 }
7060 }
Sudheer Shanka6d658d72018-01-01 01:36:49 -08007061
7062 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
7063 final long[] timesMs = u.getCpuFreqTimes(which, procState);
7064 if (timesMs != null && timesMs.length == cpuFreqs.length) {
7065 long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(which, procState);
7066 if (screenOffTimesMs == null) {
7067 screenOffTimesMs = new long[timesMs.length];
7068 }
7069 final long procToken = proto.start(UidProto.Cpu.BY_PROCESS_STATE);
7070 proto.write(UidProto.Cpu.ByProcessState.PROCESS_STATE, procState);
7071 for (int ic = 0; ic < timesMs.length; ++ic) {
7072 long cToken = proto.start(UidProto.Cpu.ByProcessState.BY_FREQUENCY);
7073 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7074 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7075 timesMs[ic]);
7076 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7077 screenOffTimesMs[ic]);
7078 proto.end(cToken);
7079 }
7080 proto.end(procToken);
7081 }
7082 }
Kweku Adams103351f2017-10-16 14:39:34 -07007083 proto.end(cpuToken);
7084
7085 // Flashlight (FLASHLIGHT_DATA)
7086 dumpTimer(proto, UidProto.FLASHLIGHT, u.getFlashlightTurnedOnTimer(),
7087 rawRealtimeUs, which);
7088
7089 // Foreground activity (FOREGROUND_ACTIVITY_DATA)
7090 dumpTimer(proto, UidProto.FOREGROUND_ACTIVITY, u.getForegroundActivityTimer(),
7091 rawRealtimeUs, which);
7092
7093 // Foreground service (FOREGROUND_SERVICE_DATA)
7094 dumpTimer(proto, UidProto.FOREGROUND_SERVICE, u.getForegroundServiceTimer(),
7095 rawRealtimeUs, which);
7096
7097 // Job completion (JOB_COMPLETION_DATA)
7098 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
7099 final int[] reasons = new int[]{
7100 JobParameters.REASON_CANCELED,
7101 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED,
7102 JobParameters.REASON_PREEMPT,
7103 JobParameters.REASON_TIMEOUT,
7104 JobParameters.REASON_DEVICE_IDLE,
7105 };
7106 for (int ic = 0; ic < completions.size(); ++ic) {
7107 SparseIntArray types = completions.valueAt(ic);
7108 if (types != null) {
7109 final long jcToken = proto.start(UidProto.JOB_COMPLETION);
7110
7111 proto.write(UidProto.JobCompletion.NAME, completions.keyAt(ic));
7112
7113 for (int r : reasons) {
7114 long rToken = proto.start(UidProto.JobCompletion.REASON_COUNT);
7115 proto.write(UidProto.JobCompletion.ReasonCount.NAME, r);
7116 proto.write(UidProto.JobCompletion.ReasonCount.COUNT, types.get(r, 0));
7117 proto.end(rToken);
7118 }
7119
7120 proto.end(jcToken);
7121 }
7122 }
7123
7124 // Scheduled jobs (JOB_DATA)
7125 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
7126 for (int ij = jobs.size() - 1; ij >= 0; --ij) {
7127 final Timer timer = jobs.valueAt(ij);
7128 final Timer bgTimer = timer.getSubTimer();
7129 final long jToken = proto.start(UidProto.JOBS);
7130
7131 proto.write(UidProto.Job.NAME, jobs.keyAt(ij));
7132 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7133 dumpTimer(proto, UidProto.Job.TOTAL, timer, rawRealtimeUs, which);
7134 dumpTimer(proto, UidProto.Job.BACKGROUND, bgTimer, rawRealtimeUs, which);
7135
7136 proto.end(jToken);
7137 }
7138
7139 // Modem Controller (MODEM_CONTROLLER_DATA)
7140 dumpControllerActivityProto(proto, UidProto.MODEM_CONTROLLER,
7141 u.getModemControllerActivity(), which);
7142
7143 // Network stats (NETWORK_DATA)
7144 final long nToken = proto.start(UidProto.NETWORK);
7145 proto.write(UidProto.Network.MOBILE_BYTES_RX,
7146 u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7147 proto.write(UidProto.Network.MOBILE_BYTES_TX,
7148 u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7149 proto.write(UidProto.Network.WIFI_BYTES_RX,
7150 u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7151 proto.write(UidProto.Network.WIFI_BYTES_TX,
7152 u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7153 proto.write(UidProto.Network.BT_BYTES_RX,
7154 u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7155 proto.write(UidProto.Network.BT_BYTES_TX,
7156 u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
7157 proto.write(UidProto.Network.MOBILE_PACKETS_RX,
7158 u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7159 proto.write(UidProto.Network.MOBILE_PACKETS_TX,
7160 u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7161 proto.write(UidProto.Network.WIFI_PACKETS_RX,
7162 u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7163 proto.write(UidProto.Network.WIFI_PACKETS_TX,
7164 u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7165 proto.write(UidProto.Network.MOBILE_ACTIVE_DURATION_MS,
7166 roundUsToMs(u.getMobileRadioActiveTime(which)));
7167 proto.write(UidProto.Network.MOBILE_ACTIVE_COUNT,
7168 u.getMobileRadioActiveCount(which));
7169 proto.write(UidProto.Network.MOBILE_WAKEUP_COUNT,
7170 u.getMobileRadioApWakeupCount(which));
7171 proto.write(UidProto.Network.WIFI_WAKEUP_COUNT,
7172 u.getWifiRadioApWakeupCount(which));
7173 proto.write(UidProto.Network.MOBILE_BYTES_BG_RX,
7174 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA, which));
7175 proto.write(UidProto.Network.MOBILE_BYTES_BG_TX,
7176 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA, which));
7177 proto.write(UidProto.Network.WIFI_BYTES_BG_RX,
7178 u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which));
7179 proto.write(UidProto.Network.WIFI_BYTES_BG_TX,
7180 u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which));
7181 proto.write(UidProto.Network.MOBILE_PACKETS_BG_RX,
7182 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA, which));
7183 proto.write(UidProto.Network.MOBILE_PACKETS_BG_TX,
7184 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA, which));
7185 proto.write(UidProto.Network.WIFI_PACKETS_BG_RX,
7186 u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA, which));
7187 proto.write(UidProto.Network.WIFI_PACKETS_BG_TX,
7188 u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA, which));
7189 proto.end(nToken);
7190
7191 // Power use item (POWER_USE_ITEM_DATA)
7192 BatterySipper bs = uidToSipper.get(uid);
7193 if (bs != null) {
7194 final long bsToken = proto.start(UidProto.POWER_USE_ITEM);
7195 proto.write(UidProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7196 proto.write(UidProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7197 proto.write(UidProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7198 proto.write(UidProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7199 bs.proportionalSmearMah);
7200 proto.end(bsToken);
7201 }
7202
7203 // Processes (PROCESS_DATA)
7204 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats =
7205 u.getProcessStats();
7206 for (int ipr = processStats.size() - 1; ipr >= 0; --ipr) {
7207 final Uid.Proc ps = processStats.valueAt(ipr);
7208 final long prToken = proto.start(UidProto.PROCESS);
7209
7210 proto.write(UidProto.Process.NAME, processStats.keyAt(ipr));
7211 proto.write(UidProto.Process.USER_DURATION_MS, ps.getUserTime(which));
7212 proto.write(UidProto.Process.SYSTEM_DURATION_MS, ps.getSystemTime(which));
7213 proto.write(UidProto.Process.FOREGROUND_DURATION_MS, ps.getForegroundTime(which));
7214 proto.write(UidProto.Process.START_COUNT, ps.getStarts(which));
7215 proto.write(UidProto.Process.ANR_COUNT, ps.getNumAnrs(which));
7216 proto.write(UidProto.Process.CRASH_COUNT, ps.getNumCrashes(which));
7217
7218 proto.end(prToken);
7219 }
7220
7221 // Sensors (SENSOR_DATA)
7222 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
7223 for (int ise = 0; ise < sensors.size(); ++ise) {
7224 final Uid.Sensor se = sensors.valueAt(ise);
7225 final Timer timer = se.getSensorTime();
7226 if (timer == null) {
7227 continue;
7228 }
7229 final Timer bgTimer = se.getSensorBackgroundTime();
7230 final int sensorNumber = sensors.keyAt(ise);
7231 final long seToken = proto.start(UidProto.SENSORS);
7232
7233 proto.write(UidProto.Sensor.ID, sensorNumber);
7234 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7235 dumpTimer(proto, UidProto.Sensor.APPORTIONED, timer, rawRealtimeUs, which);
7236 dumpTimer(proto, UidProto.Sensor.BACKGROUND, bgTimer, rawRealtimeUs, which);
7237
7238 proto.end(seToken);
7239 }
7240
7241 // State times (STATE_TIME_DATA)
7242 for (int ips = 0; ips < Uid.NUM_PROCESS_STATE; ++ips) {
7243 long durMs = roundUsToMs(u.getProcessStateTime(ips, rawRealtimeUs, which));
7244 if (durMs == 0) {
7245 continue;
7246 }
7247 final long stToken = proto.start(UidProto.STATES);
7248 proto.write(UidProto.StateTime.STATE, ips);
7249 proto.write(UidProto.StateTime.DURATION_MS, durMs);
7250 proto.end(stToken);
7251 }
7252
7253 // Syncs (SYNC_DATA)
7254 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
7255 for (int isy = syncs.size() - 1; isy >= 0; --isy) {
7256 final Timer timer = syncs.valueAt(isy);
7257 final Timer bgTimer = timer.getSubTimer();
7258 final long syToken = proto.start(UidProto.SYNCS);
7259
7260 proto.write(UidProto.Sync.NAME, syncs.keyAt(isy));
7261 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7262 dumpTimer(proto, UidProto.Sync.TOTAL, timer, rawRealtimeUs, which);
7263 dumpTimer(proto, UidProto.Sync.BACKGROUND, bgTimer, rawRealtimeUs, which);
7264
7265 proto.end(syToken);
7266 }
7267
7268 // User activity (USER_ACTIVITY_DATA)
7269 if (u.hasUserActivity()) {
7270 for (int i = 0; i < Uid.NUM_USER_ACTIVITY_TYPES; ++i) {
7271 int val = u.getUserActivityCount(i, which);
7272 if (val != 0) {
7273 final long uaToken = proto.start(UidProto.USER_ACTIVITY);
7274 proto.write(UidProto.UserActivity.NAME, i);
7275 proto.write(UidProto.UserActivity.COUNT, val);
7276 proto.end(uaToken);
7277 }
7278 }
7279 }
7280
7281 // Vibrator (VIBRATOR_DATA)
7282 dumpTimer(proto, UidProto.VIBRATOR, u.getVibratorOnTimer(), rawRealtimeUs, which);
7283
7284 // Video (VIDEO_DATA)
7285 dumpTimer(proto, UidProto.VIDEO, u.getVideoTurnedOnTimer(), rawRealtimeUs, which);
7286
7287 // Wakelocks (WAKELOCK_DATA)
7288 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
7289 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7290 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7291 final long wToken = proto.start(UidProto.WAKELOCKS);
7292 proto.write(UidProto.Wakelock.NAME, wakelocks.keyAt(iw));
7293 dumpTimer(proto, UidProto.Wakelock.FULL, wl.getWakeTime(WAKE_TYPE_FULL),
7294 rawRealtimeUs, which);
7295 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7296 if (pTimer != null) {
7297 dumpTimer(proto, UidProto.Wakelock.PARTIAL, pTimer, rawRealtimeUs, which);
7298 dumpTimer(proto, UidProto.Wakelock.BACKGROUND_PARTIAL, pTimer.getSubTimer(),
7299 rawRealtimeUs, which);
7300 }
7301 dumpTimer(proto, UidProto.Wakelock.WINDOW, wl.getWakeTime(WAKE_TYPE_WINDOW),
7302 rawRealtimeUs, which);
7303 proto.end(wToken);
7304 }
7305
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007306 // Wifi Multicast Wakelock (WIFI_MULTICAST_WAKELOCK_DATA)
7307 dumpTimer(proto, UidProto.WIFI_MULTICAST_WAKELOCK, u.getMulticastWakelockStats(),
7308 rawRealtimeUs, which);
7309
Kweku Adams103351f2017-10-16 14:39:34 -07007310 // Wakeup alarms (WAKEUP_ALARM_DATA)
7311 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7312 final Uid.Pkg ps = packageStats.valueAt(ipkg);
7313 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
7314 for (int iwa = alarms.size() - 1; iwa >= 0; --iwa) {
7315 final long waToken = proto.start(UidProto.WAKEUP_ALARM);
7316 proto.write(UidProto.WakeupAlarm.NAME, alarms.keyAt(iwa));
7317 proto.write(UidProto.WakeupAlarm.COUNT,
7318 alarms.valueAt(iwa).getCountLocked(which));
7319 proto.end(waToken);
7320 }
7321 }
7322
7323 // Wifi Controller (WIFI_CONTROLLER_DATA)
7324 dumpControllerActivityProto(proto, UidProto.WIFI_CONTROLLER,
7325 u.getWifiControllerActivity(), which);
7326
7327 // Wifi data (WIFI_DATA)
7328 final long wToken = proto.start(UidProto.WIFI);
7329 proto.write(UidProto.Wifi.FULL_WIFI_LOCK_DURATION_MS,
7330 roundUsToMs(u.getFullWifiLockTime(rawRealtimeUs, which)));
7331 dumpTimer(proto, UidProto.Wifi.APPORTIONED_SCAN, u.getWifiScanTimer(),
7332 rawRealtimeUs, which);
7333 proto.write(UidProto.Wifi.RUNNING_DURATION_MS,
7334 roundUsToMs(u.getWifiRunningTime(rawRealtimeUs, which)));
7335 dumpTimer(proto, UidProto.Wifi.BACKGROUND_SCAN, u.getWifiScanBackgroundTimer(),
7336 rawRealtimeUs, which);
7337 proto.end(wToken);
7338
7339 proto.end(uTkn);
7340 }
7341 }
7342
7343 private void dumpProtoSystemLocked(ProtoOutputStream proto, BatteryStatsHelper helper) {
Kweku Adams87b19ec2017-10-09 12:40:03 -07007344 final long sToken = proto.start(BatteryStatsProto.SYSTEM);
7345 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7346 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7347 final long rawRealtimeUs = rawRealtimeMs * 1000;
7348 final int which = STATS_SINCE_CHARGED;
7349
7350 // Battery data (BATTERY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007351 final long bToken = proto.start(SystemProto.BATTERY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007352 proto.write(SystemProto.Battery.START_CLOCK_TIME_MS, getStartClockTime());
7353 proto.write(SystemProto.Battery.START_COUNT, getStartCount());
7354 proto.write(SystemProto.Battery.TOTAL_REALTIME_MS,
7355 computeRealtime(rawRealtimeUs, which) / 1000);
7356 proto.write(SystemProto.Battery.TOTAL_UPTIME_MS,
7357 computeUptime(rawUptimeUs, which) / 1000);
7358 proto.write(SystemProto.Battery.BATTERY_REALTIME_MS,
7359 computeBatteryRealtime(rawRealtimeUs, which) / 1000);
7360 proto.write(SystemProto.Battery.BATTERY_UPTIME_MS,
7361 computeBatteryUptime(rawUptimeUs, which) / 1000);
7362 proto.write(SystemProto.Battery.SCREEN_OFF_REALTIME_MS,
7363 computeBatteryScreenOffRealtime(rawRealtimeUs, which) / 1000);
7364 proto.write(SystemProto.Battery.SCREEN_OFF_UPTIME_MS,
7365 computeBatteryScreenOffUptime(rawUptimeUs, which) / 1000);
7366 proto.write(SystemProto.Battery.SCREEN_DOZE_DURATION_MS,
7367 getScreenDozeTime(rawRealtimeUs, which) / 1000);
7368 proto.write(SystemProto.Battery.ESTIMATED_BATTERY_CAPACITY_MAH,
7369 getEstimatedBatteryCapacity());
7370 proto.write(SystemProto.Battery.MIN_LEARNED_BATTERY_CAPACITY_UAH,
7371 getMinLearnedBatteryCapacity());
7372 proto.write(SystemProto.Battery.MAX_LEARNED_BATTERY_CAPACITY_UAH,
7373 getMaxLearnedBatteryCapacity());
Kweku Adams103351f2017-10-16 14:39:34 -07007374 proto.end(bToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007375
7376 // Battery discharge (BATTERY_DISCHARGE_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007377 final long bdToken = proto.start(SystemProto.BATTERY_DISCHARGE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007378 proto.write(SystemProto.BatteryDischarge.LOWER_BOUND_SINCE_CHARGE,
7379 getLowDischargeAmountSinceCharge());
7380 proto.write(SystemProto.BatteryDischarge.UPPER_BOUND_SINCE_CHARGE,
7381 getHighDischargeAmountSinceCharge());
7382 proto.write(SystemProto.BatteryDischarge.SCREEN_ON_SINCE_CHARGE,
7383 getDischargeAmountScreenOnSinceCharge());
7384 proto.write(SystemProto.BatteryDischarge.SCREEN_OFF_SINCE_CHARGE,
7385 getDischargeAmountScreenOffSinceCharge());
7386 proto.write(SystemProto.BatteryDischarge.SCREEN_DOZE_SINCE_CHARGE,
7387 getDischargeAmountScreenDozeSinceCharge());
7388 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH,
7389 getUahDischarge(which) / 1000);
7390 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_OFF,
7391 getUahDischargeScreenOff(which) / 1000);
7392 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_DOZE,
7393 getUahDischargeScreenDoze(which) / 1000);
Mike Ma15313c92017-11-15 17:58:21 -08007394 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_LIGHT_DOZE,
7395 getUahDischargeLightDoze(which) / 1000);
7396 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_DEEP_DOZE,
7397 getUahDischargeDeepDoze(which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007398 proto.end(bdToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007399
7400 // Time remaining
7401 long timeRemainingUs = computeChargeTimeRemaining(rawRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -07007402 // These are part of a oneof, so we should only set one of them.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007403 if (timeRemainingUs >= 0) {
7404 // Charge time remaining (CHARGE_TIME_REMAIN_DATA)
7405 proto.write(SystemProto.CHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7406 } else {
7407 timeRemainingUs = computeBatteryTimeRemaining(rawRealtimeUs);
7408 // Discharge time remaining (DISCHARGE_TIME_REMAIN_DATA)
7409 if (timeRemainingUs >= 0) {
7410 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7411 } else {
7412 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, -1);
7413 }
7414 }
7415
7416 // Charge step (CHARGE_STEP_DATA)
7417 dumpDurationSteps(proto, SystemProto.CHARGE_STEP, getChargeLevelStepTracker());
7418
7419 // Phone data connection (DATA_CONNECTION_TIME_DATA and DATA_CONNECTION_COUNT_DATA)
7420 for (int i = 0; i < NUM_DATA_CONNECTION_TYPES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007421 final long pdcToken = proto.start(SystemProto.DATA_CONNECTION);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007422 proto.write(SystemProto.DataConnection.NAME, i);
7423 dumpTimer(proto, SystemProto.DataConnection.TOTAL, getPhoneDataConnectionTimer(i),
7424 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007425 proto.end(pdcToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007426 }
7427
7428 // Discharge step (DISCHARGE_STEP_DATA)
7429 dumpDurationSteps(proto, SystemProto.DISCHARGE_STEP, getDischargeLevelStepTracker());
7430
7431 // CPU frequencies (GLOBAL_CPU_FREQ_DATA)
7432 final long[] cpuFreqs = getCpuFreqs();
7433 if (cpuFreqs != null) {
7434 for (long i : cpuFreqs) {
7435 proto.write(SystemProto.CPU_FREQUENCY, i);
7436 }
7437 }
7438
7439 // Bluetooth controller (GLOBAL_BLUETOOTH_CONTROLLER_DATA)
7440 dumpControllerActivityProto(proto, SystemProto.GLOBAL_BLUETOOTH_CONTROLLER,
7441 getBluetoothControllerActivity(), which);
7442
7443 // Modem controller (GLOBAL_MODEM_CONTROLLER_DATA)
7444 dumpControllerActivityProto(proto, SystemProto.GLOBAL_MODEM_CONTROLLER,
7445 getModemControllerActivity(), which);
7446
7447 // Global network data (GLOBAL_NETWORK_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007448 final long gnToken = proto.start(SystemProto.GLOBAL_NETWORK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007449 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_RX,
7450 getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7451 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_TX,
7452 getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7453 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_RX,
7454 getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7455 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_TX,
7456 getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7457 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_RX,
7458 getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7459 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_TX,
7460 getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7461 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_RX,
7462 getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7463 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_TX,
7464 getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7465 proto.write(SystemProto.GlobalNetwork.BT_BYTES_RX,
7466 getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7467 proto.write(SystemProto.GlobalNetwork.BT_BYTES_TX,
7468 getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
Kweku Adams103351f2017-10-16 14:39:34 -07007469 proto.end(gnToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007470
7471 // Wifi controller (GLOBAL_WIFI_CONTROLLER_DATA)
7472 dumpControllerActivityProto(proto, SystemProto.GLOBAL_WIFI_CONTROLLER,
7473 getWifiControllerActivity(), which);
7474
7475
7476 // Global wifi (GLOBAL_WIFI_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007477 final long gwToken = proto.start(SystemProto.GLOBAL_WIFI);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007478 proto.write(SystemProto.GlobalWifi.ON_DURATION_MS,
7479 getWifiOnTime(rawRealtimeUs, which) / 1000);
7480 proto.write(SystemProto.GlobalWifi.RUNNING_DURATION_MS,
7481 getGlobalWifiRunningTime(rawRealtimeUs, which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007482 proto.end(gwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007483
7484 // Kernel wakelock (KERNEL_WAKELOCK_DATA)
7485 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
7486 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007487 final long kwToken = proto.start(SystemProto.KERNEL_WAKELOCK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007488 proto.write(SystemProto.KernelWakelock.NAME, ent.getKey());
7489 dumpTimer(proto, SystemProto.KernelWakelock.TOTAL, ent.getValue(),
7490 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007491 proto.end(kwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007492 }
7493
7494 // Misc (MISC_DATA)
7495 // Calculate wakelock times across all uids.
7496 long fullWakeLockTimeTotalUs = 0;
7497 long partialWakeLockTimeTotalUs = 0;
7498
7499 final SparseArray<? extends Uid> uidStats = getUidStats();
7500 for (int iu = 0; iu < uidStats.size(); iu++) {
7501 final Uid u = uidStats.valueAt(iu);
7502
7503 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks =
7504 u.getWakelockStats();
7505 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7506 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7507
7508 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
7509 if (fullWakeTimer != null) {
7510 fullWakeLockTimeTotalUs += fullWakeTimer.getTotalTimeLocked(rawRealtimeUs,
7511 which);
7512 }
7513
7514 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7515 if (partialWakeTimer != null) {
7516 partialWakeLockTimeTotalUs += partialWakeTimer.getTotalTimeLocked(
7517 rawRealtimeUs, which);
7518 }
7519 }
7520 }
Kweku Adams103351f2017-10-16 14:39:34 -07007521 final long mToken = proto.start(SystemProto.MISC);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007522 proto.write(SystemProto.Misc.SCREEN_ON_DURATION_MS,
7523 getScreenOnTime(rawRealtimeUs, which) / 1000);
7524 proto.write(SystemProto.Misc.PHONE_ON_DURATION_MS,
7525 getPhoneOnTime(rawRealtimeUs, which) / 1000);
7526 proto.write(SystemProto.Misc.FULL_WAKELOCK_TOTAL_DURATION_MS,
7527 fullWakeLockTimeTotalUs / 1000);
7528 proto.write(SystemProto.Misc.PARTIAL_WAKELOCK_TOTAL_DURATION_MS,
7529 partialWakeLockTimeTotalUs / 1000);
7530 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_DURATION_MS,
7531 getMobileRadioActiveTime(rawRealtimeUs, which) / 1000);
7532 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_ADJUSTED_TIME_MS,
7533 getMobileRadioActiveAdjustedTime(which) / 1000);
7534 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_COUNT,
7535 getMobileRadioActiveCount(which));
7536 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_UNKNOWN_DURATION_MS,
7537 getMobileRadioActiveUnknownTime(which) / 1000);
7538 proto.write(SystemProto.Misc.INTERACTIVE_DURATION_MS,
7539 getInteractiveTime(rawRealtimeUs, which) / 1000);
7540 proto.write(SystemProto.Misc.BATTERY_SAVER_MODE_ENABLED_DURATION_MS,
7541 getPowerSaveModeEnabledTime(rawRealtimeUs, which) / 1000);
7542 proto.write(SystemProto.Misc.NUM_CONNECTIVITY_CHANGES,
7543 getNumConnectivityChange(which));
7544 proto.write(SystemProto.Misc.DEEP_DOZE_ENABLED_DURATION_MS,
7545 getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7546 proto.write(SystemProto.Misc.DEEP_DOZE_COUNT,
7547 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
7548 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_DURATION_MS,
7549 getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7550 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_COUNT,
7551 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
7552 proto.write(SystemProto.Misc.LONGEST_DEEP_DOZE_DURATION_MS,
7553 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
7554 proto.write(SystemProto.Misc.LIGHT_DOZE_ENABLED_DURATION_MS,
7555 getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7556 proto.write(SystemProto.Misc.LIGHT_DOZE_COUNT,
7557 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
7558 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_DURATION_MS,
7559 getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7560 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_COUNT,
7561 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
7562 proto.write(SystemProto.Misc.LONGEST_LIGHT_DOZE_DURATION_MS,
7563 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
Kweku Adams103351f2017-10-16 14:39:34 -07007564 proto.end(mToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007565
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007566 // Wifi multicast wakelock total stats (WIFI_MULTICAST_WAKELOCK_TOTAL_DATA)
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08007567 final long multicastWakeLockTimeTotalUs =
7568 getWifiMulticastWakelockTime(rawRealtimeUs, which);
7569 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007570 final long wmctToken = proto.start(SystemProto.WIFI_MULTICAST_WAKELOCK_TOTAL);
7571 proto.write(SystemProto.WifiMulticastWakelockTotal.DURATION_MS,
7572 multicastWakeLockTimeTotalUs / 1000);
7573 proto.write(SystemProto.WifiMulticastWakelockTotal.COUNT,
7574 multicastWakeLockCountTotal);
7575 proto.end(wmctToken);
7576
Kweku Adams87b19ec2017-10-09 12:40:03 -07007577 // Power use item (POWER_USE_ITEM_DATA)
7578 final List<BatterySipper> sippers = helper.getUsageList();
7579 if (sippers != null) {
7580 for (int i = 0; i < sippers.size(); ++i) {
7581 final BatterySipper bs = sippers.get(i);
7582 int n = SystemProto.PowerUseItem.UNKNOWN_SIPPER;
7583 int uid = 0;
7584 switch (bs.drainType) {
7585 case IDLE:
7586 n = SystemProto.PowerUseItem.IDLE;
7587 break;
7588 case CELL:
7589 n = SystemProto.PowerUseItem.CELL;
7590 break;
7591 case PHONE:
7592 n = SystemProto.PowerUseItem.PHONE;
7593 break;
7594 case WIFI:
7595 n = SystemProto.PowerUseItem.WIFI;
7596 break;
7597 case BLUETOOTH:
7598 n = SystemProto.PowerUseItem.BLUETOOTH;
7599 break;
7600 case SCREEN:
7601 n = SystemProto.PowerUseItem.SCREEN;
7602 break;
7603 case FLASHLIGHT:
7604 n = SystemProto.PowerUseItem.FLASHLIGHT;
7605 break;
7606 case APP:
Kweku Adams103351f2017-10-16 14:39:34 -07007607 // dumpProtoAppsLocked will handle this.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007608 continue;
7609 case USER:
7610 n = SystemProto.PowerUseItem.USER;
7611 uid = UserHandle.getUid(bs.userId, 0);
7612 break;
7613 case UNACCOUNTED:
7614 n = SystemProto.PowerUseItem.UNACCOUNTED;
7615 break;
7616 case OVERCOUNTED:
7617 n = SystemProto.PowerUseItem.OVERCOUNTED;
7618 break;
7619 case CAMERA:
7620 n = SystemProto.PowerUseItem.CAMERA;
7621 break;
7622 case MEMORY:
7623 n = SystemProto.PowerUseItem.MEMORY;
7624 break;
7625 }
Kweku Adams103351f2017-10-16 14:39:34 -07007626 final long puiToken = proto.start(SystemProto.POWER_USE_ITEM);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007627 proto.write(SystemProto.PowerUseItem.NAME, n);
7628 proto.write(SystemProto.PowerUseItem.UID, uid);
7629 proto.write(SystemProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7630 proto.write(SystemProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7631 proto.write(SystemProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7632 proto.write(SystemProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7633 bs.proportionalSmearMah);
Kweku Adams103351f2017-10-16 14:39:34 -07007634 proto.end(puiToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007635 }
7636 }
7637
7638 // Power use summary (POWER_USE_SUMMARY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007639 final long pusToken = proto.start(SystemProto.POWER_USE_SUMMARY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007640 proto.write(SystemProto.PowerUseSummary.BATTERY_CAPACITY_MAH,
7641 helper.getPowerProfile().getBatteryCapacity());
7642 proto.write(SystemProto.PowerUseSummary.COMPUTED_POWER_MAH, helper.getComputedPower());
7643 proto.write(SystemProto.PowerUseSummary.MIN_DRAINED_POWER_MAH, helper.getMinDrainedPower());
7644 proto.write(SystemProto.PowerUseSummary.MAX_DRAINED_POWER_MAH, helper.getMaxDrainedPower());
Kweku Adams103351f2017-10-16 14:39:34 -07007645 proto.end(pusToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007646
7647 // RPM stats (RESOURCE_POWER_MANAGER_DATA)
7648 final Map<String, ? extends Timer> rpmStats = getRpmStats();
7649 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
7650 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007651 final long rpmToken = proto.start(SystemProto.RESOURCE_POWER_MANAGER);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007652 proto.write(SystemProto.ResourcePowerManager.NAME, ent.getKey());
7653 dumpTimer(proto, SystemProto.ResourcePowerManager.TOTAL,
7654 ent.getValue(), rawRealtimeUs, which);
7655 dumpTimer(proto, SystemProto.ResourcePowerManager.SCREEN_OFF,
7656 screenOffRpmStats.get(ent.getKey()), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007657 proto.end(rpmToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007658 }
7659
7660 // Screen brightness (SCREEN_BRIGHTNESS_DATA)
7661 for (int i = 0; i < NUM_SCREEN_BRIGHTNESS_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007662 final long sbToken = proto.start(SystemProto.SCREEN_BRIGHTNESS);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007663 proto.write(SystemProto.ScreenBrightness.NAME, i);
7664 dumpTimer(proto, SystemProto.ScreenBrightness.TOTAL, getScreenBrightnessTimer(i),
7665 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007666 proto.end(sbToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007667 }
7668
7669 // Signal scanning time (SIGNAL_SCANNING_TIME_DATA)
7670 dumpTimer(proto, SystemProto.SIGNAL_SCANNING, getPhoneSignalScanningTimer(), rawRealtimeUs,
7671 which);
7672
7673 // Phone signal strength (SIGNAL_STRENGTH_TIME_DATA and SIGNAL_STRENGTH_COUNT_DATA)
7674 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007675 final long pssToken = proto.start(SystemProto.PHONE_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007676 proto.write(SystemProto.PhoneSignalStrength.NAME, i);
7677 dumpTimer(proto, SystemProto.PhoneSignalStrength.TOTAL, getPhoneSignalStrengthTimer(i),
7678 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007679 proto.end(pssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007680 }
7681
7682 // Wakeup reasons (WAKEUP_REASON_DATA)
7683 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
7684 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007685 final long wrToken = proto.start(SystemProto.WAKEUP_REASON);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007686 proto.write(SystemProto.WakeupReason.NAME, ent.getKey());
7687 dumpTimer(proto, SystemProto.WakeupReason.TOTAL, ent.getValue(), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007688 proto.end(wrToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007689 }
7690
7691 // Wifi signal strength (WIFI_SIGNAL_STRENGTH_TIME_DATA and WIFI_SIGNAL_STRENGTH_COUNT_DATA)
7692 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007693 final long wssToken = proto.start(SystemProto.WIFI_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007694 proto.write(SystemProto.WifiSignalStrength.NAME, i);
7695 dumpTimer(proto, SystemProto.WifiSignalStrength.TOTAL, getWifiSignalStrengthTimer(i),
7696 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007697 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007698 }
7699
7700 // Wifi state (WIFI_STATE_TIME_DATA and WIFI_STATE_COUNT_DATA)
7701 for (int i = 0; i < NUM_WIFI_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007702 final long wsToken = proto.start(SystemProto.WIFI_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007703 proto.write(SystemProto.WifiState.NAME, i);
7704 dumpTimer(proto, SystemProto.WifiState.TOTAL, getWifiStateTimer(i),
7705 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007706 proto.end(wsToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007707 }
7708
7709 // Wifi supplicant state (WIFI_SUPPL_STATE_TIME_DATA and WIFI_SUPPL_STATE_COUNT_DATA)
7710 for (int i = 0; i < NUM_WIFI_SUPPL_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007711 final long wssToken = proto.start(SystemProto.WIFI_SUPPLICANT_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007712 proto.write(SystemProto.WifiSupplicantState.NAME, i);
7713 dumpTimer(proto, SystemProto.WifiSupplicantState.TOTAL, getWifiSupplStateTimer(i),
7714 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007715 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007716 }
7717
7718 proto.end(sToken);
7719 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007720}