blob: 430c28b73b13ead6d9d97e7e9f3218ef370b6be9 [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.os;
18
Sudheer Shankab2f83c12017-11-13 19:25:01 -080019import android.app.ActivityManager;
Dianne Hackborn94326cb2017-06-28 16:17:20 -070020import android.app.job.JobParameters;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080021import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080022import android.content.pm.ApplicationInfo;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070023import android.service.batterystats.BatteryStatsServiceDumpProto;
Wink Saville52840902011-02-18 12:40:47 -080024import android.telephony.SignalStrength;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080025import android.text.format.DateFormat;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070026import android.util.ArrayMap;
James Carr2dd7e5e2016-07-20 18:48:39 -070027import android.util.LongSparseArray;
Dianne Hackborn9cfba352016-03-24 17:31:28 -070028import android.util.MutableBoolean;
29import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.util.Printer;
31import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070032import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070033import android.util.TimeUtils;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070034import android.util.proto.ProtoOutputStream;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070035import android.view.Display;
Amith Yamasaniab9ad192016-12-06 12:46:59 -080036
Sudheer Shankab2f83c12017-11-13 19:25:01 -080037import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080038import com.android.internal.os.BatterySipper;
39import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Kweku Adams2f73ecd2017-09-27 16:59:19 -070041import java.io.FileDescriptor;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070042import java.io.PrintWriter;
43import java.util.ArrayList;
44import java.util.Collections;
45import java.util.Comparator;
46import java.util.Formatter;
47import java.util.HashMap;
48import java.util.List;
49import java.util.Map;
50
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051/**
52 * A class providing access to battery usage statistics, including information on
53 * wakelocks, processes, packages, and services. All times are represented in microseconds
54 * except where indicated otherwise.
55 * @hide
56 */
57public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070058 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
60 private static final boolean LOCAL_LOGV = false;
Bookatz82b341172017-09-07 19:06:08 -070061 /** Fetching RPM stats is too slow to do each time screen changes, so disable it. */
62 protected static final boolean SCREEN_OFF_RPM_STATS_ENABLED = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070063
64 /** @hide */
65 public static final String SERVICE_NAME = "batterystats";
66
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 /**
68 * A constant indicating a partial wake lock timer.
69 */
70 public static final int WAKE_TYPE_PARTIAL = 0;
71
72 /**
73 * A constant indicating a full wake lock timer.
74 */
75 public static final int WAKE_TYPE_FULL = 1;
76
77 /**
78 * A constant indicating a window wake lock timer.
79 */
80 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 /**
83 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 */
85 public static final int SENSOR = 3;
Mike Mac2f518a2017-09-19 16:06:03 -070086
The Android Open Source Project10592532009-03-18 17:39:46 -070087 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070088 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070089 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070090 public static final int WIFI_RUNNING = 4;
Mike Mac2f518a2017-09-19 16:06:03 -070091
Dianne Hackborn617f8772009-03-31 15:04:46 -070092 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070093 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070094 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070095 public static final int FULL_WIFI_LOCK = 5;
Mike Mac2f518a2017-09-19 16:06:03 -070096
The Android Open Source Project10592532009-03-18 17:39:46 -070097 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070098 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070099 */
Nick Pelly6ccaa542012-06-15 15:22:47 -0700100 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
Dianne Hackborn62793e42015-03-09 11:15:41 -0700102 /**
103 * A constant indicating a wifi multicast timer
104 */
105 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700108 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700109 */
110 public static final int VIDEO_TURNED_ON = 8;
111
112 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800113 * A constant indicating a vibrator on timer
114 */
115 public static final int VIBRATOR_ON = 9;
116
117 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700118 * A constant indicating a foreground activity timer
119 */
120 public static final int FOREGROUND_ACTIVITY = 10;
121
122 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700123 * A constant indicating a wifi batched scan is active
124 */
125 public static final int WIFI_BATCHED_SCAN = 11;
126
127 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700128 * A constant indicating a process state timer
129 */
130 public static final int PROCESS_STATE = 12;
131
132 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700133 * A constant indicating a sync timer
134 */
135 public static final int SYNC = 13;
136
137 /**
138 * A constant indicating a job timer
139 */
140 public static final int JOB = 14;
141
142 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800143 * A constant indicating an audio turn on timer
144 */
145 public static final int AUDIO_TURNED_ON = 15;
146
147 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700148 * A constant indicating a flashlight turn on timer
149 */
150 public static final int FLASHLIGHT_TURNED_ON = 16;
151
152 /**
153 * A constant indicating a camera turn on timer
154 */
155 public static final int CAMERA_TURNED_ON = 17;
156
157 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700158 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700159 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700160 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700161
162 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800163 * A constant indicating a bluetooth scan timer.
164 */
165 public static final int BLUETOOTH_SCAN_ON = 19;
166
167 /**
Bookatzc8c44962017-05-11 12:12:54 -0700168 * A constant indicating an aggregated partial wake lock timer.
169 */
170 public static final int AGGREGATED_WAKE_TYPE_PARTIAL = 20;
171
172 /**
Bookatzb1f04f32017-05-19 13:57:32 -0700173 * A constant indicating a bluetooth scan timer for unoptimized scans.
174 */
175 public static final int BLUETOOTH_UNOPTIMIZED_SCAN_ON = 21;
176
177 /**
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700178 * A constant indicating a foreground service timer
179 */
180 public static final int FOREGROUND_SERVICE = 22;
181
182 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 * Include all of the data in the stats, including previously saved data.
184 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700185 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186
187 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 * Include only the current run in the stats.
189 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700190 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191
192 /**
193 * Include only the run since the last time the device was unplugged in the stats.
194 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700195 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700196
197 // NOTE: Update this list if you add/change any stats above.
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700198 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700199 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700200 private static final String[] STAT_NAMES = { "l", "c", "u" };
201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700203 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700204 *
205 * New in version 19:
206 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800207 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700208 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Bookatz506a8182017-05-01 14:18:42 -0700209 * New in version 21:
210 * - Actual (not just apportioned) Wakelock time is also recorded.
Bookatzc8c44962017-05-11 12:12:54 -0700211 * - Aggregated partial wakelock time (per uid, instead of per wakelock) is recorded.
Bookatzb1f04f32017-05-19 13:57:32 -0700212 * - BLE scan result count
213 * - CPU frequency time per uid
214 * New in version 22:
215 * - BLE scan result background count, BLE unoptimized scan time
Bookatz98d4d5c2017-08-01 19:07:54 -0700216 * - Background partial wakelock time & count
217 * New in version 23:
218 * - Logging smeared power model values
219 * New in version 24:
220 * - Fixed bugs in background timers and BLE scan time
221 * New in version 25:
222 * - Package wakeup alarms are now on screen-off timebase
Bookatz50df7112017-08-04 14:53:26 -0700223 * New in version 26:
Bookatz82b341172017-09-07 19:06:08 -0700224 * - Resource power manager (rpm) states [but screenOffRpm is disabled from working properly]
Mike Mac2f518a2017-09-19 16:06:03 -0700225 * New in version 27:
226 * - Always On Display (screen doze mode) time and power
Mike Ma15313c92017-11-15 17:58:21 -0800227 * New in version 28:
228 * - Light/Deep Doze power
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700229 * - WiFi Multicast Wakelock statistics (count & duration)
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700230 */
Mike Ma15313c92017-11-15 17:58:21 -0800231 static final int CHECKIN_VERSION = 28;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700232
233 /**
234 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700236 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700237
Evan Millar22ac0432009-03-31 11:33:18 -0700238 private static final long BYTES_PER_KB = 1024;
239 private static final long BYTES_PER_MB = 1048576; // 1024^2
240 private static final long BYTES_PER_GB = 1073741824; //1024^3
Bookatz506a8182017-05-01 14:18:42 -0700241
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700242 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800243 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700244 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700246 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700247 private static final String CPU_DATA = "cpu";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700248 private static final String GLOBAL_CPU_FREQ_DATA = "gcf";
249 private static final String CPU_TIMES_AT_FREQ_DATA = "ctf";
Bookatz50df7112017-08-04 14:53:26 -0700250 // rpm line is:
251 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "rpm", state/voter name, total time, total count,
252 // screen-off time, screen-off count
253 private static final String RESOURCE_POWER_MANAGER_DATA = "rpm";
Evan Millare84de8d2009-04-02 22:16:12 -0700254 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800255 private static final String VIBRATOR_DATA = "vib";
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700256 private static final String FOREGROUND_ACTIVITY_DATA = "fg";
257 // fgs line is:
258 // BATTERY_STATS_CHECKIN_VERSION, uid, category, "fgs",
259 // foreground service time, count
260 private static final String FOREGROUND_SERVICE_DATA = "fgs";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700261 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700262 // wl line is:
263 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
Bookatz5b5ec322017-05-26 09:40:38 -0700264 // full totalTime, 'f', count, current duration, max duration, total duration,
265 // partial totalTime, 'p', count, current duration, max duration, total duration,
266 // bg partial totalTime, 'bp', count, current duration, max duration, total duration,
267 // window totalTime, 'w', count, current duration, max duration, total duration
Bookatz506a8182017-05-01 14:18:42 -0700268 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700269 private static final String WAKELOCK_DATA = "wl";
Bookatzc8c44962017-05-11 12:12:54 -0700270 // awl line is:
271 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "awl",
272 // cumulative partial wakelock duration, cumulative background partial wakelock duration
273 private static final String AGGREGATED_WAKELOCK_DATA = "awl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700274 private static final String SYNC_DATA = "sy";
275 private static final String JOB_DATA = "jb";
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700276 private static final String JOB_COMPLETION_DATA = "jbc";
Evan Millarc64edde2009-04-18 12:26:32 -0700277 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700278 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700279 private static final String NETWORK_DATA = "nt";
280 private static final String USER_ACTIVITY_DATA = "ua";
281 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800282 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700283 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700284 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700285 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800286 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
287 private static final String WIFI_CONTROLLER_DATA = "wfcd";
288 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
289 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700290 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700291 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800292 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800293 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
294 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800295 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700296 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700297 private static final String SCREEN_BRIGHTNESS_DATA = "br";
298 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700299 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700300 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
301 private static final String DATA_CONNECTION_TIME_DATA = "dct";
302 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800303 private static final String WIFI_STATE_TIME_DATA = "wst";
304 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700305 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
306 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
307 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
308 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800309 private static final String POWER_USE_SUMMARY_DATA = "pws";
310 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700311 private static final String DISCHARGE_STEP_DATA = "dsd";
312 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700313 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
314 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700315 private static final String FLASHLIGHT_DATA = "fla";
316 private static final String CAMERA_DATA = "cam";
317 private static final String VIDEO_DATA = "vid";
318 private static final String AUDIO_DATA = "aud";
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700319 private static final String WIFI_MULTICAST_TOTAL_DATA = "wmct";
320 private static final String WIFI_MULTICAST_DATA = "wmc";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321
Adam Lesinski010bf372016-04-11 12:18:18 -0700322 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
323
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700324 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 private final Formatter mFormatter = new Formatter(mFormatBuilder);
326
327 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700328 * Indicates times spent by the uid at each cpu frequency in all process states.
329 *
330 * Other types might include times spent in foreground, background etc.
331 */
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800332 @VisibleForTesting
333 public static final String UID_TIMES_TYPE_ALL = "A";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700334
335 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700336 * State for keeping track of counting information.
337 */
338 public static abstract class Counter {
339
340 /**
341 * Returns the count associated with this Counter for the
342 * selected type of statistics.
343 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700344 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700345 */
Evan Millarc64edde2009-04-18 12:26:32 -0700346 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700347
348 /**
349 * Temporary for debugging.
350 */
351 public abstract void logState(Printer pw, String prefix);
352 }
353
354 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700355 * State for keeping track of long counting information.
356 */
357 public static abstract class LongCounter {
358
359 /**
360 * Returns the count associated with this Counter for the
361 * selected type of statistics.
362 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700363 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700364 */
365 public abstract long getCountLocked(int which);
366
367 /**
368 * Temporary for debugging.
369 */
370 public abstract void logState(Printer pw, String prefix);
371 }
372
373 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700374 * State for keeping track of array of long counting information.
375 */
376 public static abstract class LongCounterArray {
377 /**
378 * Returns the counts associated with this Counter for the
379 * selected type of statistics.
380 *
381 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
382 */
383 public abstract long[] getCountsLocked(int which);
384
385 /**
386 * Temporary for debugging.
387 */
388 public abstract void logState(Printer pw, String prefix);
389 }
390
391 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800392 * Container class that aggregates counters for transmit, receive, and idle state of a
393 * radio controller.
394 */
395 public static abstract class ControllerActivityCounter {
396 /**
397 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
398 * idle state.
399 */
400 public abstract LongCounter getIdleTimeCounter();
401
402 /**
403 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
404 * receive state.
405 */
406 public abstract LongCounter getRxTimeCounter();
407
408 /**
409 * An array of {@link LongCounter}, representing various transmit levels, where each level
410 * may draw a different amount of power. The levels themselves are controller-specific.
411 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
412 * various transmit level states.
413 */
414 public abstract LongCounter[] getTxTimeCounters();
415
416 /**
417 * @return a non-null {@link LongCounter} representing the power consumed by the controller
418 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
419 * yield a value of 0 if the device doesn't support power calculations.
420 */
421 public abstract LongCounter getPowerCounter();
422 }
423
424 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 * State for keeping track of timing information.
426 */
427 public static abstract class Timer {
428
429 /**
430 * Returns the count associated with this Timer for the
431 * selected type of statistics.
432 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700433 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 */
Evan Millarc64edde2009-04-18 12:26:32 -0700435 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436
437 /**
438 * Returns the total time in microseconds associated with this Timer for the
439 * selected type of statistics.
440 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800441 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700442 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 * @return a time in microseconds
444 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800445 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700448 * Returns the total time in microseconds associated with this Timer since the
449 * 'mark' was last set.
450 *
451 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
452 * @return a time in microseconds
453 */
454 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
455
456 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700457 * Returns the max duration if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700458 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700459 */
460 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
461 return -1;
462 }
463
464 /**
465 * Returns the current time the timer has been active, 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 getCurrentDurationMsLocked(long elapsedRealtimeMs) {
469 return -1;
470 }
471
472 /**
Kweku Adams103351f2017-10-16 14:39:34 -0700473 * Returns the total time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800474 *
475 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
476 * been on since reset.
477 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
478 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
479 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
480 * the actual total time.
Kweku Adams103351f2017-10-16 14:39:34 -0700481 * Not all Timer subclasses track the max, total, and current durations.
Bookatz867c0d72017-03-07 18:23:42 -0800482 */
483 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
484 return -1;
485 }
486
487 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700488 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
489 * used, for example, for tracking background usage. Secondary timers are never pooled.
490 *
491 * Not all Timer subclasses have a secondary timer; those that don't return null.
492 */
493 public Timer getSubTimer() {
494 return null;
495 }
496
497 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700498 * Returns whether the timer is currently running. Some types of timers
499 * (e.g. BatchTimers) don't know whether the event is currently active,
500 * and report false.
501 */
502 public boolean isRunningLocked() {
503 return false;
504 }
505
506 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 * Temporary for debugging.
508 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700509 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 }
511
512 /**
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800513 * Maps the ActivityManager procstate into corresponding BatteryStats procstate.
514 */
515 public static int mapToInternalProcessState(int procState) {
516 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
517 return ActivityManager.PROCESS_STATE_NONEXISTENT;
518 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
519 return Uid.PROCESS_STATE_TOP;
520 } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
521 // Persistent and other foreground states go here.
522 return Uid.PROCESS_STATE_FOREGROUND_SERVICE;
523 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
524 // Persistent and other foreground states go here.
525 return Uid.PROCESS_STATE_FOREGROUND;
526 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
527 return Uid.PROCESS_STATE_BACKGROUND;
528 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
529 return Uid.PROCESS_STATE_TOP_SLEEPING;
530 } else if (procState <= ActivityManager.PROCESS_STATE_HEAVY_WEIGHT) {
531 return Uid.PROCESS_STATE_HEAVY_WEIGHT;
532 } else {
533 return Uid.PROCESS_STATE_CACHED;
534 }
535 }
536
537 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 * The statistics associated with a particular uid.
539 */
540 public static abstract class Uid {
541
542 /**
543 * Returns a mapping containing wakelock statistics.
544 *
545 * @return a Map from Strings to Uid.Wakelock objects.
546 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700547 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548
549 /**
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700550 * Returns the WiFi Multicast Wakelock statistics.
551 *
552 * @return a Timer Object for the per uid Multicast statistics.
553 */
554 public abstract Timer getMulticastWakelockStats();
555
556 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700557 * Returns a mapping containing sync statistics.
558 *
559 * @return a Map from Strings to Timer objects.
560 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700561 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700562
563 /**
564 * Returns a mapping containing scheduled job statistics.
565 *
566 * @return a Map from Strings to Timer objects.
567 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700568 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700569
570 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700571 * Returns statistics about how jobs have completed.
572 *
573 * @return A Map of String job names to completion type -> count mapping.
574 */
575 public abstract ArrayMap<String, SparseIntArray> getJobCompletionStats();
576
577 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 * The statistics associated with a particular wake lock.
579 */
580 public static abstract class Wakelock {
581 public abstract Timer getWakeTime(int type);
582 }
583
584 /**
Bookatzc8c44962017-05-11 12:12:54 -0700585 * The cumulative time the uid spent holding any partial wakelocks. This will generally
586 * differ from summing over the Wakelocks in getWakelockStats since the latter may have
587 * wakelocks that overlap in time (and therefore over-counts).
588 */
589 public abstract Timer getAggregatedPartialWakelockTimer();
590
591 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 * Returns a mapping containing sensor statistics.
593 *
594 * @return a Map from Integer sensor ids to Uid.Sensor objects.
595 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700596 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597
598 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700599 * Returns a mapping containing active process data.
600 */
601 public abstract SparseArray<? extends Pid> getPidStats();
Bookatzc8c44962017-05-11 12:12:54 -0700602
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700603 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 * Returns a mapping containing process statistics.
605 *
606 * @return a Map from Strings to Uid.Proc objects.
607 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700608 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609
610 /**
611 * Returns a mapping containing package statistics.
612 *
613 * @return a Map from Strings to Uid.Pkg objects.
614 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700615 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700616
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800617 public abstract ControllerActivityCounter getWifiControllerActivity();
618 public abstract ControllerActivityCounter getBluetoothControllerActivity();
619 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800620
621 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 * {@hide}
623 */
624 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700625
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800626 public abstract void noteWifiRunningLocked(long elapsedRealtime);
627 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
628 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
629 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
630 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
631 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
632 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
633 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
634 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
635 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800636 public abstract void noteActivityResumedLocked(long elapsedRealtime);
637 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800638 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
639 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
640 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700641 public abstract int getWifiScanCount(int which);
Kweku Adams103351f2017-10-16 14:39:34 -0700642 /**
643 * Returns the timer keeping track of wifi scans.
644 */
645 public abstract Timer getWifiScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800646 public abstract int getWifiScanBackgroundCount(int which);
647 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
648 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -0700649 /**
650 * Returns the timer keeping track of background wifi scans.
651 */
652 public abstract Timer getWifiScanBackgroundTimer();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800653 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700654 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800655 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700656 public abstract Timer getAudioTurnedOnTimer();
657 public abstract Timer getVideoTurnedOnTimer();
658 public abstract Timer getFlashlightTurnedOnTimer();
659 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700660 public abstract Timer getForegroundActivityTimer();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700661
662 /**
663 * Returns the timer keeping track of Foreground Service time
664 */
665 public abstract Timer getForegroundServiceTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800666 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800667 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatzb1f04f32017-05-19 13:57:32 -0700668 public abstract Timer getBluetoothUnoptimizedScanTimer();
669 public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700670 public abstract Counter getBluetoothScanResultCounter();
Bookatzb1f04f32017-05-19 13:57:32 -0700671 public abstract Counter getBluetoothScanResultBgCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700672
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700673 public abstract long[] getCpuFreqTimes(int which);
674 public abstract long[] getScreenOffCpuFreqTimes(int which);
675
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800676 /**
677 * Returns cpu times of an uid at a particular process state.
678 */
679 public abstract long[] getCpuFreqTimes(int which, int procState);
680 /**
681 * Returns cpu times of an uid while the screen if off at a particular process state.
682 */
683 public abstract long[] getScreenOffCpuFreqTimes(int which, int procState);
684
Dianne Hackborna0200e32016-03-30 18:01:41 -0700685 // Note: the following times are disjoint. They can be added together to find the
686 // total time a uid has had any processes running at all.
687
688 /**
689 * Time this uid has any processes in the top state (or above such as persistent).
690 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800691 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700692 /**
693 * Time this uid has any process with a started out bound foreground service, but
694 * none in the "top" state.
695 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800696 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700697 /**
Dianne Hackborna0200e32016-03-30 18:01:41 -0700698 * Time this uid has any process in an active foreground state, but none in the
699 * "top sleeping" or better state.
700 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800701 public static final int PROCESS_STATE_FOREGROUND = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700702 /**
703 * Time this uid has any process in an active background state, but none in the
704 * "foreground" or better state.
705 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800706 public static final int PROCESS_STATE_BACKGROUND = 3;
707 /**
708 * Time this uid has any process that is top while the device is sleeping, but not
709 * active for any other reason. We kind-of consider it a kind of cached process
710 * for execution restrictions.
711 */
712 public static final int PROCESS_STATE_TOP_SLEEPING = 4;
713 /**
714 * Time this uid has any process that is in the background but it has an activity
715 * marked as "can't save state". This is essentially a cached process, though the
716 * system will try much harder than normal to avoid killing it.
717 */
718 public static final int PROCESS_STATE_HEAVY_WEIGHT = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700719 /**
720 * Time this uid has any processes that are sitting around cached, not in one of the
721 * other active states.
722 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800723 public static final int PROCESS_STATE_CACHED = 6;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700724 /**
725 * Total number of process states we track.
726 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800727 public static final int NUM_PROCESS_STATE = 7;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700728
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800729 // Used in dump
Dianne Hackborn61659e52014-07-09 16:13:01 -0700730 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800731 "Top", "Fg Service", "Foreground", "Background", "Top Sleeping", "Heavy Weight",
732 "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700733 };
734
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800735 // Used in checkin dump
736 @VisibleForTesting
737 public static final String[] UID_PROCESS_TYPES = {
738 "T", // TOP
739 "FS", // FOREGROUND_SERVICE
740 "F", // FOREGROUND
741 "B", // BACKGROUND
742 "TS", // TOP_SLEEPING
743 "HW", // HEAVY_WEIGHT
744 "C" // CACHED
745 };
746
747 /**
748 * When the process exits one of these states, we need to make sure cpu time in this state
749 * is not attributed to any non-critical process states.
750 */
751 public static final int[] CRITICAL_PROC_STATES = {
752 PROCESS_STATE_TOP, PROCESS_STATE_FOREGROUND_SERVICE, PROCESS_STATE_FOREGROUND
753 };
754
Dianne Hackborn61659e52014-07-09 16:13:01 -0700755 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800756 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700757
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800758 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759
Robert Greenwalta029ea12013-09-25 16:38:12 -0700760 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
761
Dianne Hackborn617f8772009-03-31 15:04:46 -0700762 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700763 * Note that these must match the constants in android.os.PowerManager.
764 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
765 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700766 */
767 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700768 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700769 };
Bookatzc8c44962017-05-11 12:12:54 -0700770
Phil Weaverda80d672016-03-15 16:25:46 -0700771 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700772
Dianne Hackborn617f8772009-03-31 15:04:46 -0700773 public abstract void noteUserActivityLocked(int type);
774 public abstract boolean hasUserActivity();
775 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700776
777 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800778 public abstract long getNetworkActivityBytes(int type, int which);
779 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800780 public abstract long getMobileRadioActiveTime(int which);
781 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700782
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700783 /**
784 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
785 */
786 public abstract long getUserCpuTimeUs(int which);
787
788 /**
789 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
790 */
791 public abstract long getSystemCpuTimeUs(int which);
792
793 /**
Sudheer Shanka71f34b32017-07-21 00:14:24 -0700794 * Returns the approximate cpu time (in microseconds) spent at a certain CPU speed for a
Adam Lesinski6832f392015-09-05 18:05:40 -0700795 * given CPU cluster.
796 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700797 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700798 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700799 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
800 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700801 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700802 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700803
Adam Lesinski5f056f62016-07-14 16:56:08 -0700804 /**
805 * Returns the number of times this UID woke up the Application Processor to
806 * process a mobile radio packet.
807 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
808 */
809 public abstract long getMobileRadioApWakeupCount(int which);
810
811 /**
812 * Returns the number of times this UID woke up the Application Processor to
813 * process a WiFi packet.
814 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
815 */
816 public abstract long getWifiRadioApWakeupCount(int which);
817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800819 /*
820 * FIXME: it's not correct to use this magic value because it
821 * could clash with a sensor handle (which are defined by
822 * the sensor HAL, and therefore out of our control
823 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 // Magic sensor number for the GPS.
825 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800830
Bookatz867c0d72017-03-07 18:23:42 -0800831 /** Returns a Timer for sensor usage when app is in the background. */
832 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 }
834
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700835 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800836 public int mWakeNesting;
837 public long mWakeSumMs;
838 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700839 }
840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 /**
842 * The statistics associated with a particular process.
843 */
844 public static abstract class Proc {
845
Dianne Hackborn287952c2010-09-22 22:34:31 -0700846 public static class ExcessivePower {
847 public static final int TYPE_WAKE = 1;
848 public static final int TYPE_CPU = 2;
849
850 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700851 public long overTime;
852 public long usedTime;
853 }
854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800856 * Returns true if this process is still active in the battery stats.
857 */
858 public abstract boolean isActive();
859
860 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700861 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700863 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 */
865 public abstract long getUserTime(int which);
866
867 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700868 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700870 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 */
872 public abstract long getSystemTime(int which);
873
874 /**
875 * Returns the number of times the process has been started.
876 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700877 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 */
879 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700880
881 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800882 * Returns the number of times the process has crashed.
883 *
884 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
885 */
886 public abstract int getNumCrashes(int which);
887
888 /**
889 * Returns the number of times the process has ANRed.
890 *
891 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
892 */
893 public abstract int getNumAnrs(int which);
894
895 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700896 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700897 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700898 * @return foreground cpu time in microseconds
899 */
900 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700901
Dianne Hackborn287952c2010-09-22 22:34:31 -0700902 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700903
Dianne Hackborn287952c2010-09-22 22:34:31 -0700904 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 }
906
907 /**
908 * The statistics associated with a particular package.
909 */
910 public static abstract class Pkg {
911
912 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700913 * Returns information about all wakeup alarms that have been triggered for this
914 * package. The mapping keys are tag names for the alarms, the counter contains
915 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700917 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918
919 /**
920 * Returns a mapping containing service statistics.
921 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700922 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923
924 /**
925 * The statistics associated with a particular service.
926 */
Joe Onoratoabded112016-02-08 16:49:39 -0800927 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928
929 /**
930 * Returns the amount of time spent started.
931 *
932 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700933 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 * @return
935 */
936 public abstract long getStartTime(long batteryUptime, int which);
937
938 /**
939 * Returns the total number of times startService() has been called.
940 *
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 */
943 public abstract int getStarts(int which);
944
945 /**
946 * Returns the total number times the service has been launched.
947 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700948 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 */
950 public abstract int getLaunches(int which);
951 }
952 }
953 }
954
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800955 public static final class LevelStepTracker {
956 public long mLastStepTime = -1;
957 public int mNumStepDurations;
958 public final long[] mStepDurations;
959
960 public LevelStepTracker(int maxLevelSteps) {
961 mStepDurations = new long[maxLevelSteps];
962 }
963
964 public LevelStepTracker(int numSteps, long[] steps) {
965 mNumStepDurations = numSteps;
966 mStepDurations = new long[numSteps];
967 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
968 }
969
970 public long getDurationAt(int index) {
971 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
972 }
973
974 public int getLevelAt(int index) {
975 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
976 >> STEP_LEVEL_LEVEL_SHIFT);
977 }
978
979 public int getInitModeAt(int index) {
980 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
981 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
982 }
983
984 public int getModModeAt(int index) {
985 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
986 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
987 }
988
989 private void appendHex(long val, int topOffset, StringBuilder out) {
990 boolean hasData = false;
991 while (topOffset >= 0) {
992 int digit = (int)( (val>>topOffset) & 0xf );
993 topOffset -= 4;
994 if (!hasData && digit == 0) {
995 continue;
996 }
997 hasData = true;
998 if (digit >= 0 && digit <= 9) {
999 out.append((char)('0' + digit));
1000 } else {
1001 out.append((char)('a' + digit - 10));
1002 }
1003 }
1004 }
1005
1006 public void encodeEntryAt(int index, StringBuilder out) {
1007 long item = mStepDurations[index];
1008 long duration = item & STEP_LEVEL_TIME_MASK;
1009 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
1010 >> STEP_LEVEL_LEVEL_SHIFT);
1011 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
1012 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1013 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
1014 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1015 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1016 case Display.STATE_OFF: out.append('f'); break;
1017 case Display.STATE_ON: out.append('o'); break;
1018 case Display.STATE_DOZE: out.append('d'); break;
1019 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
1020 }
1021 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1022 out.append('p');
1023 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001024 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1025 out.append('i');
1026 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001027 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1028 case Display.STATE_OFF: out.append('F'); break;
1029 case Display.STATE_ON: out.append('O'); break;
1030 case Display.STATE_DOZE: out.append('D'); break;
1031 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
1032 }
1033 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1034 out.append('P');
1035 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001036 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1037 out.append('I');
1038 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001039 out.append('-');
1040 appendHex(level, 4, out);
1041 out.append('-');
1042 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
1043 }
1044
1045 public void decodeEntryAt(int index, String value) {
1046 final int N = value.length();
1047 int i = 0;
1048 char c;
1049 long out = 0;
1050 while (i < N && (c=value.charAt(i)) != '-') {
1051 i++;
1052 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001053 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001054 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001055 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001056 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001057 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001058 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001059 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1060 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1061 break;
1062 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1063 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1064 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001065 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1066 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1067 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001068 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1069 break;
1070 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1071 break;
1072 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1073 break;
1074 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1075 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1076 break;
1077 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1078 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001079 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001080 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1081 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1082 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001083 }
1084 }
1085 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001086 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001087 while (i < N && (c=value.charAt(i)) != '-') {
1088 i++;
1089 level <<= 4;
1090 if (c >= '0' && c <= '9') {
1091 level += c - '0';
1092 } else if (c >= 'a' && c <= 'f') {
1093 level += c - 'a' + 10;
1094 } else if (c >= 'A' && c <= 'F') {
1095 level += c - 'A' + 10;
1096 }
1097 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001098 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001099 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
1100 long duration = 0;
1101 while (i < N && (c=value.charAt(i)) != '-') {
1102 i++;
1103 duration <<= 4;
1104 if (c >= '0' && c <= '9') {
1105 duration += c - '0';
1106 } else if (c >= 'a' && c <= 'f') {
1107 duration += c - 'a' + 10;
1108 } else if (c >= 'A' && c <= 'F') {
1109 duration += c - 'A' + 10;
1110 }
1111 }
1112 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
1113 }
1114
1115 public void init() {
1116 mLastStepTime = -1;
1117 mNumStepDurations = 0;
1118 }
1119
1120 public void clearTime() {
1121 mLastStepTime = -1;
1122 }
1123
1124 public long computeTimePerLevel() {
1125 final long[] steps = mStepDurations;
1126 final int numSteps = mNumStepDurations;
1127
1128 // For now we'll do a simple average across all steps.
1129 if (numSteps <= 0) {
1130 return -1;
1131 }
1132 long total = 0;
1133 for (int i=0; i<numSteps; i++) {
1134 total += steps[i] & STEP_LEVEL_TIME_MASK;
1135 }
1136 return total / numSteps;
1137 /*
1138 long[] buckets = new long[numSteps];
1139 int numBuckets = 0;
1140 int numToAverage = 4;
1141 int i = 0;
1142 while (i < numSteps) {
1143 long totalTime = 0;
1144 int num = 0;
1145 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1146 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1147 num++;
1148 }
1149 buckets[numBuckets] = totalTime / num;
1150 numBuckets++;
1151 numToAverage *= 2;
1152 i += num;
1153 }
1154 if (numBuckets < 1) {
1155 return -1;
1156 }
1157 long averageTime = buckets[numBuckets-1];
1158 for (i=numBuckets-2; i>=0; i--) {
1159 averageTime = (averageTime + buckets[i]) / 2;
1160 }
1161 return averageTime;
1162 */
1163 }
1164
1165 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1166 int[] outNumOfInterest) {
1167 final long[] steps = mStepDurations;
1168 final int count = mNumStepDurations;
1169 if (count <= 0) {
1170 return -1;
1171 }
1172 long total = 0;
1173 int numOfInterest = 0;
1174 for (int i=0; i<count; i++) {
1175 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1176 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1177 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1178 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1179 // If the modes of interest didn't change during this step period...
1180 if ((modMode&modesOfInterest) == 0) {
1181 // And the mode values during this period match those we are measuring...
1182 if ((initMode&modesOfInterest) == modeValues) {
1183 // Then this can be used to estimate the total time!
1184 numOfInterest++;
1185 total += steps[i] & STEP_LEVEL_TIME_MASK;
1186 }
1187 }
1188 }
1189 if (numOfInterest <= 0) {
1190 return -1;
1191 }
1192
1193 if (outNumOfInterest != null) {
1194 outNumOfInterest[0] = numOfInterest;
1195 }
1196
1197 // The estimated time is the average time we spend in each level, multipled
1198 // by 100 -- the total number of battery levels
1199 return (total / numOfInterest) * 100;
1200 }
1201
1202 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1203 int stepCount = mNumStepDurations;
1204 final long lastStepTime = mLastStepTime;
1205 if (lastStepTime >= 0 && numStepLevels > 0) {
1206 final long[] steps = mStepDurations;
1207 long duration = elapsedRealtime - lastStepTime;
1208 for (int i=0; i<numStepLevels; i++) {
1209 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1210 long thisDuration = duration / (numStepLevels-i);
1211 duration -= thisDuration;
1212 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1213 thisDuration = STEP_LEVEL_TIME_MASK;
1214 }
1215 steps[0] = thisDuration | modeBits;
1216 }
1217 stepCount += numStepLevels;
1218 if (stepCount > steps.length) {
1219 stepCount = steps.length;
1220 }
1221 }
1222 mNumStepDurations = stepCount;
1223 mLastStepTime = elapsedRealtime;
1224 }
1225
1226 public void readFromParcel(Parcel in) {
1227 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001228 if (N > mStepDurations.length) {
1229 throw new ParcelFormatException("more step durations than available: " + N);
1230 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001231 mNumStepDurations = N;
1232 for (int i=0; i<N; i++) {
1233 mStepDurations[i] = in.readLong();
1234 }
1235 }
1236
1237 public void writeToParcel(Parcel out) {
1238 final int N = mNumStepDurations;
1239 out.writeInt(N);
1240 for (int i=0; i<N; i++) {
1241 out.writeLong(mStepDurations[i]);
1242 }
1243 }
1244 }
1245
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001246 public static final class PackageChange {
1247 public String mPackageName;
1248 public boolean mUpdate;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001249 public long mVersionCode;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001250 }
1251
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001252 public static final class DailyItem {
1253 public long mStartTime;
1254 public long mEndTime;
1255 public LevelStepTracker mDischargeSteps;
1256 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001257 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001258 }
1259
1260 public abstract DailyItem getDailyItemLocked(int daysAgo);
1261
1262 public abstract long getCurrentDailyStartTime();
1263
1264 public abstract long getNextMinDailyDeadline();
1265
1266 public abstract long getNextMaxDailyDeadline();
1267
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001268 public abstract long[] getCpuFreqs();
1269
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001270 public final static class HistoryTag {
1271 public String string;
1272 public int uid;
1273
1274 public int poolIdx;
1275
1276 public void setTo(HistoryTag o) {
1277 string = o.string;
1278 uid = o.uid;
1279 poolIdx = o.poolIdx;
1280 }
1281
1282 public void setTo(String _string, int _uid) {
1283 string = _string;
1284 uid = _uid;
1285 poolIdx = -1;
1286 }
1287
1288 public void writeToParcel(Parcel dest, int flags) {
1289 dest.writeString(string);
1290 dest.writeInt(uid);
1291 }
1292
1293 public void readFromParcel(Parcel src) {
1294 string = src.readString();
1295 uid = src.readInt();
1296 poolIdx = -1;
1297 }
1298
1299 @Override
1300 public boolean equals(Object o) {
1301 if (this == o) return true;
1302 if (o == null || getClass() != o.getClass()) return false;
1303
1304 HistoryTag that = (HistoryTag) o;
1305
1306 if (uid != that.uid) return false;
1307 if (!string.equals(that.string)) return false;
1308
1309 return true;
1310 }
1311
1312 @Override
1313 public int hashCode() {
1314 int result = string.hashCode();
1315 result = 31 * result + uid;
1316 return result;
1317 }
1318 }
1319
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001320 /**
1321 * Optional detailed information that can go into a history step. This is typically
1322 * generated each time the battery level changes.
1323 */
1324 public final static class HistoryStepDetails {
1325 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1326 public int userTime;
1327 public int systemTime;
1328
1329 // Top three apps using CPU in the last step, with times in 1/100 second.
1330 public int appCpuUid1;
1331 public int appCpuUTime1;
1332 public int appCpuSTime1;
1333 public int appCpuUid2;
1334 public int appCpuUTime2;
1335 public int appCpuSTime2;
1336 public int appCpuUid3;
1337 public int appCpuUTime3;
1338 public int appCpuSTime3;
1339
1340 // Information from /proc/stat
1341 public int statUserTime;
1342 public int statSystemTime;
1343 public int statIOWaitTime;
1344 public int statIrqTime;
1345 public int statSoftIrqTime;
1346 public int statIdlTime;
1347
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001348 // Platform-level low power state stats
1349 public String statPlatformIdleState;
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001350 public String statSubsystemPowerState;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001351
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001352 public HistoryStepDetails() {
1353 clear();
1354 }
1355
1356 public void clear() {
1357 userTime = systemTime = 0;
1358 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1359 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1360 = appCpuUTime3 = appCpuSTime3 = 0;
1361 }
1362
1363 public void writeToParcel(Parcel out) {
1364 out.writeInt(userTime);
1365 out.writeInt(systemTime);
1366 out.writeInt(appCpuUid1);
1367 out.writeInt(appCpuUTime1);
1368 out.writeInt(appCpuSTime1);
1369 out.writeInt(appCpuUid2);
1370 out.writeInt(appCpuUTime2);
1371 out.writeInt(appCpuSTime2);
1372 out.writeInt(appCpuUid3);
1373 out.writeInt(appCpuUTime3);
1374 out.writeInt(appCpuSTime3);
1375 out.writeInt(statUserTime);
1376 out.writeInt(statSystemTime);
1377 out.writeInt(statIOWaitTime);
1378 out.writeInt(statIrqTime);
1379 out.writeInt(statSoftIrqTime);
1380 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001381 out.writeString(statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001382 out.writeString(statSubsystemPowerState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001383 }
1384
1385 public void readFromParcel(Parcel in) {
1386 userTime = in.readInt();
1387 systemTime = in.readInt();
1388 appCpuUid1 = in.readInt();
1389 appCpuUTime1 = in.readInt();
1390 appCpuSTime1 = in.readInt();
1391 appCpuUid2 = in.readInt();
1392 appCpuUTime2 = in.readInt();
1393 appCpuSTime2 = in.readInt();
1394 appCpuUid3 = in.readInt();
1395 appCpuUTime3 = in.readInt();
1396 appCpuSTime3 = in.readInt();
1397 statUserTime = in.readInt();
1398 statSystemTime = in.readInt();
1399 statIOWaitTime = in.readInt();
1400 statIrqTime = in.readInt();
1401 statSoftIrqTime = in.readInt();
1402 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001403 statPlatformIdleState = in.readString();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001404 statSubsystemPowerState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001405 }
1406 }
1407
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001408 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001409 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001410
1411 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001412 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001413
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001414 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001415 public static final byte CMD_NULL = -1;
1416 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001417 public static final byte CMD_CURRENT_TIME = 5;
1418 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001419 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001420 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001421
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001422 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001423
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001424 /**
1425 * Return whether the command code is a delta data update.
1426 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001427 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001428 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001429 }
1430
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001431 public byte batteryLevel;
1432 public byte batteryStatus;
1433 public byte batteryHealth;
1434 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001435
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001436 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001437 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001438
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001439 // The charge of the battery in micro-Ampere-hours.
1440 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001441
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001442 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001443 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001444 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001445 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001446 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1447 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001448 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001449 public static final int STATE_PHONE_STATE_SHIFT = 6;
1450 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001451 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001452 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001453 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001454
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001455 // These states always appear directly in the first int token
1456 // of a delta change; they should be ones that change relatively
1457 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001458 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1459 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001460 public static final int STATE_GPS_ON_FLAG = 1<<29;
1461 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001462 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001463 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001464 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001465 // Do not use, this is used for coulomb delta count.
1466 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001467 // These are on the lower bits used for the command; if they change
1468 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001469 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001470 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001471 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001472 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1473 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
Mike Mac2f518a2017-09-19 16:06:03 -07001474 public static final int STATE_SCREEN_DOZE_FLAG = 1 << 18;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001475 // empty slot
1476 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001477
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001478 public static final int MOST_INTERESTING_STATES =
Mike Mac2f518a2017-09-19 16:06:03 -07001479 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG | STATE_SCREEN_DOZE_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001480
1481 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001482
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001483 public int states;
1484
Dianne Hackborn3251b902014-06-20 14:40:53 -07001485 // Constants from WIFI_SUPPL_STATE_*
1486 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1487 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1488 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1489 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1490 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1491 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1492
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001493 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001494 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1495 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1496 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001497 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001498 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1499 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1500 public static final int STATE2_CHARGING_FLAG = 1<<24;
1501 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1502 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1503 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001504 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001505
1506 public static final int MOST_INTERESTING_STATES2 =
Mike Mac2f518a2017-09-19 16:06:03 -07001507 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
1508 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001509
1510 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001511
Dianne Hackborn40c87252014-03-19 16:55:40 -07001512 public int states2;
1513
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001514 // The wake lock that was acquired at this point.
1515 public HistoryTag wakelockTag;
1516
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001517 // Kernel wakeup reason at this point.
1518 public HistoryTag wakeReasonTag;
1519
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001520 // Non-null when there is more detailed information at this step.
1521 public HistoryStepDetails stepDetails;
1522
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001523 public static final int EVENT_FLAG_START = 0x8000;
1524 public static final int EVENT_FLAG_FINISH = 0x4000;
1525
1526 // No event in this item.
1527 public static final int EVENT_NONE = 0x0000;
1528 // Event is about a process that is running.
1529 public static final int EVENT_PROC = 0x0001;
1530 // Event is about an application package that is in the foreground.
1531 public static final int EVENT_FOREGROUND = 0x0002;
1532 // Event is about an application package that is at the top of the screen.
1533 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001534 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001535 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001536 // Events for all additional wake locks aquired/release within a wake block.
1537 // These are not generated by default.
1538 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001539 // Event is about an application executing a scheduled job.
1540 public static final int EVENT_JOB = 0x0006;
1541 // Events for users running.
1542 public static final int EVENT_USER_RUNNING = 0x0007;
1543 // Events for foreground user.
1544 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001545 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001546 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001547 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001548 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001549 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001550 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001551 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001552 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001553 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001554 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001555 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001556 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001557 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001558 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001559 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001560 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001561 // Event for a package being on the temporary whitelist.
1562 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001563 // Event for the screen waking up.
1564 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001565 // Event for the UID that woke up the application processor.
1566 // Used for wakeups coming from WiFi, modem, etc.
1567 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001568 // Event for reporting that a specific partial wake lock has been held for a long duration.
1569 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001570
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001571 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001572 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001573 // Mask to extract out only the type part of the event.
1574 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001575
1576 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1577 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1578 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1579 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1580 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1581 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001582 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1583 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001584 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1585 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001586 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1587 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1588 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1589 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1590 public static final int EVENT_USER_FOREGROUND_START =
1591 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1592 public static final int EVENT_USER_FOREGROUND_FINISH =
1593 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001594 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1595 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001596 public static final int EVENT_TEMP_WHITELIST_START =
1597 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1598 public static final int EVENT_TEMP_WHITELIST_FINISH =
1599 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001600 public static final int EVENT_LONG_WAKE_LOCK_START =
1601 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1602 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1603 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001604
1605 // For CMD_EVENT.
1606 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001607 public HistoryTag eventTag;
1608
Dianne Hackborn9a755432014-05-15 17:05:22 -07001609 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001610 public long currentTime;
1611
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001612 // Meta-data when reading.
1613 public int numReadInts;
1614
1615 // Pre-allocated objects.
1616 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001617 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001618 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001619
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001620 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001621 }
Bookatzc8c44962017-05-11 12:12:54 -07001622
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001623 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001624 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001625 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001626 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001627 }
Bookatzc8c44962017-05-11 12:12:54 -07001628
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001629 public int describeContents() {
1630 return 0;
1631 }
1632
1633 public void writeToParcel(Parcel dest, int flags) {
1634 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001635 int bat = (((int)cmd)&0xff)
1636 | ((((int)batteryLevel)<<8)&0xff00)
1637 | ((((int)batteryStatus)<<16)&0xf0000)
1638 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001639 | ((((int)batteryPlugType)<<24)&0xf000000)
1640 | (wakelockTag != null ? 0x10000000 : 0)
1641 | (wakeReasonTag != null ? 0x20000000 : 0)
1642 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001643 dest.writeInt(bat);
1644 bat = (((int)batteryTemperature)&0xffff)
1645 | ((((int)batteryVoltage)<<16)&0xffff0000);
1646 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001647 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001648 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001649 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001650 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001651 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001652 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001653 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001654 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001655 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001656 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001657 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001658 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001659 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001660 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001661 dest.writeLong(currentTime);
1662 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001663 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001664
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001665 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001666 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001667 int bat = src.readInt();
1668 cmd = (byte)(bat&0xff);
1669 batteryLevel = (byte)((bat>>8)&0xff);
1670 batteryStatus = (byte)((bat>>16)&0xf);
1671 batteryHealth = (byte)((bat>>20)&0xf);
1672 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001673 int bat2 = src.readInt();
1674 batteryTemperature = (short)(bat2&0xffff);
1675 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001676 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001677 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001678 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001679 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001680 wakelockTag = localWakelockTag;
1681 wakelockTag.readFromParcel(src);
1682 } else {
1683 wakelockTag = null;
1684 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001685 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001686 wakeReasonTag = localWakeReasonTag;
1687 wakeReasonTag.readFromParcel(src);
1688 } else {
1689 wakeReasonTag = null;
1690 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001691 if ((bat&0x40000000) != 0) {
1692 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001693 eventTag = localEventTag;
1694 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001695 } else {
1696 eventCode = EVENT_NONE;
1697 eventTag = null;
1698 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001699 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001700 currentTime = src.readLong();
1701 } else {
1702 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001703 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001704 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001705 }
1706
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001707 public void clear() {
1708 time = 0;
1709 cmd = CMD_NULL;
1710 batteryLevel = 0;
1711 batteryStatus = 0;
1712 batteryHealth = 0;
1713 batteryPlugType = 0;
1714 batteryTemperature = 0;
1715 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001716 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001717 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001718 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001719 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001720 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001721 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001722 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001723 }
Bookatzc8c44962017-05-11 12:12:54 -07001724
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001725 public void setTo(HistoryItem o) {
1726 time = o.time;
1727 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001728 setToCommon(o);
1729 }
1730
1731 public void setTo(long time, byte cmd, HistoryItem o) {
1732 this.time = time;
1733 this.cmd = cmd;
1734 setToCommon(o);
1735 }
1736
1737 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001738 batteryLevel = o.batteryLevel;
1739 batteryStatus = o.batteryStatus;
1740 batteryHealth = o.batteryHealth;
1741 batteryPlugType = o.batteryPlugType;
1742 batteryTemperature = o.batteryTemperature;
1743 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001744 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001745 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001746 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001747 if (o.wakelockTag != null) {
1748 wakelockTag = localWakelockTag;
1749 wakelockTag.setTo(o.wakelockTag);
1750 } else {
1751 wakelockTag = null;
1752 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001753 if (o.wakeReasonTag != null) {
1754 wakeReasonTag = localWakeReasonTag;
1755 wakeReasonTag.setTo(o.wakeReasonTag);
1756 } else {
1757 wakeReasonTag = null;
1758 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001759 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001760 if (o.eventTag != null) {
1761 eventTag = localEventTag;
1762 eventTag.setTo(o.eventTag);
1763 } else {
1764 eventTag = null;
1765 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001766 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001767 }
1768
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001769 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001770 return batteryLevel == o.batteryLevel
1771 && batteryStatus == o.batteryStatus
1772 && batteryHealth == o.batteryHealth
1773 && batteryPlugType == o.batteryPlugType
1774 && batteryTemperature == o.batteryTemperature
1775 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001776 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001777 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001778 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001779 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001780 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001781
1782 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001783 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001784 return false;
1785 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001786 if (wakelockTag != o.wakelockTag) {
1787 if (wakelockTag == null || o.wakelockTag == null) {
1788 return false;
1789 }
1790 if (!wakelockTag.equals(o.wakelockTag)) {
1791 return false;
1792 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001793 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001794 if (wakeReasonTag != o.wakeReasonTag) {
1795 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1796 return false;
1797 }
1798 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1799 return false;
1800 }
1801 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001802 if (eventTag != o.eventTag) {
1803 if (eventTag == null || o.eventTag == null) {
1804 return false;
1805 }
1806 if (!eventTag.equals(o.eventTag)) {
1807 return false;
1808 }
1809 }
1810 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001811 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001812 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001813
1814 public final static class HistoryEventTracker {
1815 private final HashMap<String, SparseIntArray>[] mActiveEvents
1816 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1817
1818 public boolean updateState(int code, String name, int uid, int poolIdx) {
1819 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1820 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1821 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1822 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001823 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001824 mActiveEvents[idx] = active;
1825 }
1826 SparseIntArray uids = active.get(name);
1827 if (uids == null) {
1828 uids = new SparseIntArray();
1829 active.put(name, uids);
1830 }
1831 if (uids.indexOfKey(uid) >= 0) {
1832 // Already set, nothing to do!
1833 return false;
1834 }
1835 uids.put(uid, poolIdx);
1836 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1837 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1838 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1839 if (active == null) {
1840 // not currently active, nothing to do.
1841 return false;
1842 }
1843 SparseIntArray uids = active.get(name);
1844 if (uids == null) {
1845 // not currently active, nothing to do.
1846 return false;
1847 }
1848 idx = uids.indexOfKey(uid);
1849 if (idx < 0) {
1850 // not currently active, nothing to do.
1851 return false;
1852 }
1853 uids.removeAt(idx);
1854 if (uids.size() <= 0) {
1855 active.remove(name);
1856 }
1857 }
1858 return true;
1859 }
1860
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001861 public void removeEvents(int code) {
1862 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1863 mActiveEvents[idx] = null;
1864 }
1865
Dianne Hackborn37de0982014-05-09 09:32:18 -07001866 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1867 return mActiveEvents[code];
1868 }
1869 }
1870
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001871 public static final class BitDescription {
1872 public final int mask;
1873 public final int shift;
1874 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001875 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001876 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001877 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07001878
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001879 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001880 this.mask = mask;
1881 this.shift = -1;
1882 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001883 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001884 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001885 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001886 }
Bookatzc8c44962017-05-11 12:12:54 -07001887
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001888 public BitDescription(int mask, int shift, String name, String shortName,
1889 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001890 this.mask = mask;
1891 this.shift = shift;
1892 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001893 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001894 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001895 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001896 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001897 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001898
Dianne Hackbornfc064132014-06-02 12:42:12 -07001899 /**
1900 * Don't allow any more batching in to the current history event. This
1901 * is called when printing partial histories, so to ensure that the next
1902 * history event will go in to a new batch after what was printed in the
1903 * last partial history.
1904 */
1905 public abstract void commitCurrentHistoryBatchLocked();
1906
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001907 public abstract int getHistoryTotalSize();
1908
1909 public abstract int getHistoryUsedSize();
1910
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001911 public abstract boolean startIteratingHistoryLocked();
1912
Dianne Hackborn099bc622014-01-22 13:39:16 -08001913 public abstract int getHistoryStringPoolSize();
1914
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001915 public abstract int getHistoryStringPoolBytes();
1916
1917 public abstract String getHistoryTagPoolString(int index);
1918
1919 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001920
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001921 public abstract boolean getNextHistoryLocked(HistoryItem out);
1922
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001923 public abstract void finishIteratingHistoryLocked();
1924
1925 public abstract boolean startIteratingOldHistoryLocked();
1926
1927 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1928
1929 public abstract void finishIteratingOldHistoryLocked();
1930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001932 * Return the base time offset for the battery history.
1933 */
1934 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07001935
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001936 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 * Returns the number of times the device has been started.
1938 */
1939 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07001940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001942 * 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 -08001943 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07001944 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 * {@hide}
1946 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001947 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07001948
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001949 /**
1950 * Returns the number of times the screen was turned on.
1951 *
1952 * {@hide}
1953 */
1954 public abstract int getScreenOnCount(int which);
1955
Mike Mac2f518a2017-09-19 16:06:03 -07001956 /**
1957 * Returns the time in microseconds that the screen has been dozing while the device was
1958 * running on battery.
1959 *
1960 * {@hide}
1961 */
1962 public abstract long getScreenDozeTime(long elapsedRealtimeUs, int which);
1963
1964 /**
1965 * Returns the number of times the screen was turned dozing.
1966 *
1967 * {@hide}
1968 */
1969 public abstract int getScreenDozeCount(int which);
1970
Jeff Browne95c3cd2014-05-02 16:59:26 -07001971 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1972
Dianne Hackborn617f8772009-03-31 15:04:46 -07001973 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1974 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1975 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1976 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1977 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07001978
Dianne Hackborn617f8772009-03-31 15:04:46 -07001979 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1980 "dark", "dim", "medium", "light", "bright"
1981 };
Bookatzc8c44962017-05-11 12:12:54 -07001982
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001983 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1984 "0", "1", "2", "3", "4"
1985 };
1986
Dianne Hackborn617f8772009-03-31 15:04:46 -07001987 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001988
Dianne Hackborn617f8772009-03-31 15:04:46 -07001989 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001990 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001991 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07001992 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07001993 * {@hide}
1994 */
1995 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001996 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07001999 * Returns the {@link Timer} object that tracks the given screen brightness.
2000 *
2001 * {@hide}
2002 */
2003 public abstract Timer getScreenBrightnessTimer(int brightnessBin);
2004
2005 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002006 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002007 * running on battery.
2008 *
2009 * {@hide}
2010 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002011 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002012
2013 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002014 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002015 *
2016 * {@hide}
2017 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002018 public abstract int getPowerSaveModeEnabledCount(int which);
2019
2020 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002021 * Constant for device idle mode: not active.
2022 */
2023 public static final int DEVICE_IDLE_MODE_OFF = 0;
2024
2025 /**
2026 * Constant for device idle mode: active in lightweight mode.
2027 */
2028 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
2029
2030 /**
2031 * Constant for device idle mode: active in full mode.
2032 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002033 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002034
2035 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002036 * Returns the time in microseconds that device has been in idle mode while
2037 * running on battery.
2038 *
2039 * {@hide}
2040 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002041 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002042
2043 /**
2044 * Returns the number of times that the devie has gone in to idle mode.
2045 *
2046 * {@hide}
2047 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002048 public abstract int getDeviceIdleModeCount(int mode, int which);
2049
2050 /**
2051 * Return the longest duration we spent in a particular device idle mode (fully in the
2052 * mode, not in idle maintenance etc).
2053 */
2054 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002055
2056 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002057 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002058 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002059 * counts all of the time that we consider the device to be idle, whether or not
2060 * it is currently in the actual device idle mode.
2061 *
2062 * {@hide}
2063 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002064 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002065
2066 /**
Bookatz8c6571b2017-10-24 15:04:41 -07002067 * Returns the number of times that the device has started idling.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002068 *
2069 * {@hide}
2070 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002071 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002072
2073 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002074 * Returns the number of times that connectivity state changed.
2075 *
2076 * {@hide}
2077 */
2078 public abstract int getNumConnectivityChange(int which);
2079
2080 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002081 * 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 -08002082 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002083 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 * {@hide}
2085 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002086 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002087
Dianne Hackborn627bba72009-03-24 22:32:56 -07002088 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002089 * Returns the number of times a phone call was activated.
2090 *
2091 * {@hide}
2092 */
2093 public abstract int getPhoneOnCount(int which);
2094
2095 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002096 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002097 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002098 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002099 * {@hide}
2100 */
2101 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002102 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002103
Dianne Hackborn617f8772009-03-31 15:04:46 -07002104 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07002105 * Returns the time in microseconds that the phone has been trying to
2106 * acquire a signal.
2107 *
2108 * {@hide}
2109 */
2110 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002111 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002112
2113 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002114 * Returns the {@link Timer} object that tracks how much the phone has been trying to
2115 * acquire a signal.
2116 *
2117 * {@hide}
2118 */
2119 public abstract Timer getPhoneSignalScanningTimer();
2120
2121 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002122 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002123 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002124 * {@hide}
2125 */
2126 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
2127
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002128 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002129 * Return the {@link Timer} object used to track the given signal strength's duration and
2130 * counts.
2131 */
2132 protected abstract Timer getPhoneSignalStrengthTimer(int strengthBin);
2133
2134 /**
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002135 * Returns the time in microseconds that the mobile network has been active
2136 * (in a high power state).
2137 *
2138 * {@hide}
2139 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002140 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002141
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002142 /**
2143 * Returns the number of times that the mobile network has transitioned to the
2144 * active state.
2145 *
2146 * {@hide}
2147 */
2148 public abstract int getMobileRadioActiveCount(int which);
2149
2150 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002151 * Returns the time in microseconds that is the difference between the mobile radio
2152 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
2153 * from the radio.
2154 *
2155 * {@hide}
2156 */
2157 public abstract long getMobileRadioActiveAdjustedTime(int which);
2158
2159 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002160 * Returns the time in microseconds that the mobile network has been active
2161 * (in a high power state) but not being able to blame on an app.
2162 *
2163 * {@hide}
2164 */
2165 public abstract long getMobileRadioActiveUnknownTime(int which);
2166
2167 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002168 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002169 *
2170 * {@hide}
2171 */
2172 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002173
Dianne Hackborn627bba72009-03-24 22:32:56 -07002174 public static final int DATA_CONNECTION_NONE = 0;
2175 public static final int DATA_CONNECTION_GPRS = 1;
2176 public static final int DATA_CONNECTION_EDGE = 2;
2177 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002178 public static final int DATA_CONNECTION_CDMA = 4;
2179 public static final int DATA_CONNECTION_EVDO_0 = 5;
2180 public static final int DATA_CONNECTION_EVDO_A = 6;
2181 public static final int DATA_CONNECTION_1xRTT = 7;
2182 public static final int DATA_CONNECTION_HSDPA = 8;
2183 public static final int DATA_CONNECTION_HSUPA = 9;
2184 public static final int DATA_CONNECTION_HSPA = 10;
2185 public static final int DATA_CONNECTION_IDEN = 11;
2186 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002187 public static final int DATA_CONNECTION_LTE = 13;
2188 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08002189 public static final int DATA_CONNECTION_HSPAP = 15;
2190 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002191
Dianne Hackborn627bba72009-03-24 22:32:56 -07002192 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002193 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002194 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08002195 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002196 };
Bookatzc8c44962017-05-11 12:12:54 -07002197
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002198 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002199
Dianne Hackborn627bba72009-03-24 22:32:56 -07002200 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002201 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002202 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002203 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002204 * {@hide}
2205 */
2206 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002207 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002210 * Returns the number of times the phone has entered the given data
2211 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002212 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002213 * {@hide}
2214 */
2215 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002216
Kweku Adams87b19ec2017-10-09 12:40:03 -07002217 /**
2218 * Returns the {@link Timer} object that tracks the phone's data connection type stats.
2219 */
2220 public abstract Timer getPhoneDataConnectionTimer(int dataType);
2221
Dianne Hackborn3251b902014-06-20 14:40:53 -07002222 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2223 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2224 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2225 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2226 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2227 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2228 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2229 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2230 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2231 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2232 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2233 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2234 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2235
2236 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2237
2238 static final String[] WIFI_SUPPL_STATE_NAMES = {
2239 "invalid", "disconn", "disabled", "inactive", "scanning",
2240 "authenticating", "associating", "associated", "4-way-handshake",
2241 "group-handshake", "completed", "dormant", "uninit"
2242 };
2243
2244 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2245 "inv", "dsc", "dis", "inact", "scan",
2246 "auth", "ascing", "asced", "4-way",
2247 "group", "compl", "dorm", "uninit"
2248 };
2249
Mike Mac2f518a2017-09-19 16:06:03 -07002250 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002251 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002252 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2253 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002254 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002255 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2256 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2257 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002258 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002259 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002260 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002261 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002262 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2263 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Mike Mac2f518a2017-09-19 16:06:03 -07002264 new BitDescription(HistoryItem.STATE_SCREEN_DOZE_FLAG, "screen_doze", "Sd"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002265 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2266 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2267 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2268 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2269 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2270 new String[] {"in", "out", "emergency", "off"},
2271 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002272 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2273 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2274 SignalStrength.SIGNAL_STRENGTH_NAMES,
2275 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002276 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2277 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2278 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002279 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002280
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002281 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2282 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002283 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002284 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002285 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002286 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002287 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002288 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2289 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2290 new String[] { "off", "light", "full", "???" },
2291 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002292 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2293 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2294 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002295 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2296 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2297 new String[] { "0", "1", "2", "3", "4" },
2298 new String[] { "0", "1", "2", "3", "4" }),
2299 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2300 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2301 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002302 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002303 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002304 };
2305
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002306 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002307 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002308 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2309 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002310 };
2311
2312 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002313 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002314 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002315 "Esw", "Ewa", "Elw", "Eec"
2316 };
2317
2318 @FunctionalInterface
2319 public interface IntToString {
2320 String applyAsString(int val);
2321 }
2322
2323 private static final IntToString sUidToString = UserHandle::formatUid;
2324 private static final IntToString sIntToString = Integer::toString;
2325
2326 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2327 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2328 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2329 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2330 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002331 };
2332
Dianne Hackborn617f8772009-03-31 15:04:46 -07002333 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002334 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002335 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002336 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002337 * {@hide}
2338 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002339 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002340
2341 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002342 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002343 * been in the running state while the device was running on battery.
2344 *
2345 * {@hide}
2346 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002347 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002348
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002349 public static final int WIFI_STATE_OFF = 0;
2350 public static final int WIFI_STATE_OFF_SCANNING = 1;
2351 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2352 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2353 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2354 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2355 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2356 public static final int WIFI_STATE_SOFT_AP = 7;
2357
2358 static final String[] WIFI_STATE_NAMES = {
2359 "off", "scanning", "no_net", "disconn",
2360 "sta", "p2p", "sta_p2p", "soft_ap"
2361 };
2362
2363 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2364
2365 /**
2366 * Returns the time in microseconds that WiFi has been running in the given state.
2367 *
2368 * {@hide}
2369 */
2370 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002371 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002372
2373 /**
2374 * Returns the number of times that WiFi has entered the given state.
2375 *
2376 * {@hide}
2377 */
2378 public abstract int getWifiStateCount(int wifiState, int which);
2379
The Android Open Source Project10592532009-03-18 17:39:46 -07002380 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002381 * Returns the {@link Timer} object that tracks the given WiFi state.
2382 *
2383 * {@hide}
2384 */
2385 public abstract Timer getWifiStateTimer(int wifiState);
2386
2387 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002388 * Returns the time in microseconds that the wifi supplicant has been
2389 * in a given state.
2390 *
2391 * {@hide}
2392 */
2393 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2394
2395 /**
2396 * Returns the number of times that the wifi supplicant has transitioned
2397 * to a given state.
2398 *
2399 * {@hide}
2400 */
2401 public abstract int getWifiSupplStateCount(int state, int which);
2402
Kweku Adams87b19ec2017-10-09 12:40:03 -07002403 /**
2404 * Returns the {@link Timer} object that tracks the given wifi supplicant state.
2405 *
2406 * {@hide}
2407 */
2408 public abstract Timer getWifiSupplStateTimer(int state);
2409
Dianne Hackborn3251b902014-06-20 14:40:53 -07002410 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2411
2412 /**
2413 * Returns the time in microseconds that WIFI has been running with
2414 * the given signal strength.
2415 *
2416 * {@hide}
2417 */
2418 public abstract long getWifiSignalStrengthTime(int strengthBin,
2419 long elapsedRealtimeUs, int which);
2420
2421 /**
2422 * Returns the number of times WIFI has entered the given signal strength.
2423 *
2424 * {@hide}
2425 */
2426 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2427
2428 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002429 * Returns the {@link Timer} object that tracks the given WIFI signal strength.
2430 *
2431 * {@hide}
2432 */
2433 public abstract Timer getWifiSignalStrengthTimer(int strengthBin);
2434
2435 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002436 * Returns the time in microseconds that the flashlight has been on while the device was
2437 * running on battery.
2438 *
2439 * {@hide}
2440 */
2441 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2442
2443 /**
2444 * Returns the number of times that the flashlight has been turned on while the device was
2445 * running on battery.
2446 *
2447 * {@hide}
2448 */
2449 public abstract long getFlashlightOnCount(int which);
2450
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002451 /**
2452 * Returns the time in microseconds that the camera has been on while the device was
2453 * running on battery.
2454 *
2455 * {@hide}
2456 */
2457 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2458
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002459 /**
2460 * Returns the time in microseconds that bluetooth scans were running while the device was
2461 * on battery.
2462 *
2463 * {@hide}
2464 */
2465 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002466
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002467 public static final int NETWORK_MOBILE_RX_DATA = 0;
2468 public static final int NETWORK_MOBILE_TX_DATA = 1;
2469 public static final int NETWORK_WIFI_RX_DATA = 2;
2470 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002471 public static final int NETWORK_BT_RX_DATA = 4;
2472 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002473 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2474 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2475 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2476 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2477 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002478
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002479 public abstract long getNetworkActivityBytes(int type, int which);
2480 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002481
Adam Lesinskie08af192015-03-25 16:42:59 -07002482 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002483 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002484 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002485 * actual power data.
2486 */
2487 public abstract boolean hasWifiActivityReporting();
2488
2489 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002490 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2491 * in various radio controller states, such as transmit, receive, and idle.
2492 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002493 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002494 public abstract ControllerActivityCounter getWifiControllerActivity();
2495
2496 /**
2497 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2498 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2499 * actual power data.
2500 */
2501 public abstract boolean hasBluetoothActivityReporting();
2502
2503 /**
2504 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2505 * in various radio controller states, such as transmit, receive, and idle.
2506 * @return non-null {@link ControllerActivityCounter}
2507 */
2508 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2509
2510 /**
2511 * Returns true if the BatteryStats object has detailed modem power reports.
2512 * When true, calling {@link #getModemControllerActivity()} will yield the
2513 * actual power data.
2514 */
2515 public abstract boolean hasModemActivityReporting();
2516
2517 /**
2518 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2519 * in various radio controller states, such as transmit, receive, and idle.
2520 * @return non-null {@link ControllerActivityCounter}
2521 */
2522 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002523
The Android Open Source Project10592532009-03-18 17:39:46 -07002524 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002525 * Return the wall clock time when battery stats data collection started.
2526 */
2527 public abstract long getStartClockTime();
2528
2529 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002530 * Return platform version tag that we were running in when the battery stats started.
2531 */
2532 public abstract String getStartPlatformVersion();
2533
2534 /**
2535 * Return platform version tag that we were running in when the battery stats ended.
2536 */
2537 public abstract String getEndPlatformVersion();
2538
2539 /**
2540 * Return the internal version code of the parcelled format.
2541 */
2542 public abstract int getParcelVersion();
2543
2544 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 * Return whether we are currently running on battery.
2546 */
2547 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 /**
2550 * Returns a SparseArray containing the statistics for each uid.
2551 */
2552 public abstract SparseArray<? extends Uid> getUidStats();
2553
2554 /**
2555 * Returns the current battery uptime in microseconds.
2556 *
2557 * @param curTime the amount of elapsed realtime in microseconds.
2558 */
2559 public abstract long getBatteryUptime(long curTime);
2560
2561 /**
2562 * Returns the current battery realtime in microseconds.
2563 *
2564 * @param curTime the amount of elapsed realtime in microseconds.
2565 */
2566 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002567
The Android Open Source Project10592532009-03-18 17:39:46 -07002568 /**
Evan Millar633a1742009-04-02 16:36:33 -07002569 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002570 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002571 */
Evan Millar633a1742009-04-02 16:36:33 -07002572 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002573
The Android Open Source Project10592532009-03-18 17:39:46 -07002574 /**
Evan Millar633a1742009-04-02 16:36:33 -07002575 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2576 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002577 */
Evan Millar633a1742009-04-02 16:36:33 -07002578 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579
2580 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002581 * Get the amount the battery has discharged since the stats were
2582 * last reset after charging, as a lower-end approximation.
2583 */
2584 public abstract int getLowDischargeAmountSinceCharge();
2585
2586 /**
2587 * Get the amount the battery has discharged since the stats were
2588 * last reset after charging, as an upper-end approximation.
2589 */
2590 public abstract int getHighDischargeAmountSinceCharge();
2591
2592 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002593 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2594 */
2595 public abstract int getDischargeAmount(int which);
2596
2597 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002598 * Get the amount the battery has discharged while the screen was on,
2599 * since the last time power was unplugged.
2600 */
2601 public abstract int getDischargeAmountScreenOn();
2602
2603 /**
2604 * Get the amount the battery has discharged while the screen was on,
2605 * since the last time the device was charged.
2606 */
2607 public abstract int getDischargeAmountScreenOnSinceCharge();
2608
2609 /**
2610 * Get the amount the battery has discharged while the screen was off,
2611 * since the last time power was unplugged.
2612 */
2613 public abstract int getDischargeAmountScreenOff();
2614
2615 /**
2616 * Get the amount the battery has discharged while the screen was off,
2617 * since the last time the device was charged.
2618 */
2619 public abstract int getDischargeAmountScreenOffSinceCharge();
2620
2621 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002622 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002623 * since the last time power was unplugged.
2624 */
2625 public abstract int getDischargeAmountScreenDoze();
2626
2627 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002628 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002629 * since the last time the device was charged.
2630 */
2631 public abstract int getDischargeAmountScreenDozeSinceCharge();
2632
2633 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 * Returns the total, last, or current battery uptime in microseconds.
2635 *
2636 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002637 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 */
2639 public abstract long computeBatteryUptime(long curTime, int which);
2640
2641 /**
2642 * Returns the total, last, or current battery realtime in microseconds.
2643 *
2644 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002645 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 */
2647 public abstract long computeBatteryRealtime(long curTime, int which);
2648
2649 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002650 * Returns the total, last, or current battery screen off/doze uptime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002651 *
2652 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002653 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002654 */
2655 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2656
2657 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002658 * Returns the total, last, or current battery screen off/doze realtime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002659 *
2660 * @param curTime the current 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.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002662 */
2663 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2664
2665 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 * Returns the total, last, or current uptime 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 computeUptime(long curTime, int which);
2672
2673 /**
2674 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002675 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 * @param curTime the current 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.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 */
2679 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002680
2681 /**
2682 * Compute an approximation for how much run time (in microseconds) is remaining on
2683 * the battery. Returns -1 if no time can be computed: either there is not
2684 * enough current data to make a decision, or the battery is currently
2685 * charging.
2686 *
2687 * @param curTime The current elepsed realtime in microseconds.
2688 */
2689 public abstract long computeBatteryTimeRemaining(long curTime);
2690
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002691 // The part of a step duration that is the actual time.
2692 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2693
2694 // Bits in a step duration that are the new battery level we are at.
2695 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002696 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002697
2698 // Bits in a step duration that are the initial mode we were in at that step.
2699 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002700 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002701
2702 // Bits in a step duration that indicate which modes changed during that step.
2703 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002704 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002705
2706 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2707 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2708
Santos Cordone94f0502017-02-24 12:31:20 -08002709 // The largest value for screen state that is tracked in battery states. Any values above
2710 // this should be mapped back to one of the tracked values before being tracked here.
2711 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2712
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002713 // Step duration mode: power save is on.
2714 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2715
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002716 // Step duration mode: device is currently in idle mode.
2717 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2718
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002719 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2720 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002721 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2722 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002723 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2724 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2725 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2726 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2727 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002728 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2729 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002730 };
2731 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2732 (Display.STATE_OFF-1),
2733 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002734 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002735 (Display.STATE_ON-1),
2736 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2737 (Display.STATE_DOZE-1),
2738 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2739 (Display.STATE_DOZE_SUSPEND-1),
2740 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002741 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002742 };
2743 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2744 "screen off",
2745 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002746 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002747 "screen on",
2748 "screen on power save",
2749 "screen doze",
2750 "screen doze power save",
2751 "screen doze-suspend",
2752 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002753 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002754 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002755
2756 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002757 * Return the amount of battery discharge while the screen was off, measured in
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002758 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2759 * a coulomb counter.
2760 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002761 public abstract long getUahDischargeScreenOff(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002762
2763 /**
2764 * Return the amount of battery discharge while the screen was in doze mode, measured in
2765 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2766 * a coulomb counter.
2767 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002768 public abstract long getUahDischargeScreenDoze(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002769
2770 /**
2771 * Return the amount of battery discharge measured in micro-Ampere-hours. This will be
2772 * non-zero only if the device's battery has a coulomb counter.
2773 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002774 public abstract long getUahDischarge(int which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002775
2776 /**
Mike Ma15313c92017-11-15 17:58:21 -08002777 * @return the amount of battery discharge while the device is in light idle mode, measured in
2778 * micro-Ampere-hours.
2779 */
2780 public abstract long getUahDischargeLightDoze(int which);
2781
2782 /**
2783 * @return the amount of battery discharge while the device is in deep idle mode, measured in
2784 * micro-Ampere-hours.
2785 */
2786 public abstract long getUahDischargeDeepDoze(int which);
2787
2788 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002789 * Returns the estimated real battery capacity, which may be less than the capacity
2790 * declared by the PowerProfile.
2791 * @return The estimated battery capacity in mAh.
2792 */
2793 public abstract int getEstimatedBatteryCapacity();
2794
2795 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002796 * @return The minimum learned battery capacity in uAh.
2797 */
2798 public abstract int getMinLearnedBatteryCapacity();
2799
2800 /**
2801 * @return The maximum learned battery capacity in uAh.
2802 */
2803 public abstract int getMaxLearnedBatteryCapacity() ;
2804
2805 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002806 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002807 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002808 public abstract LevelStepTracker getDischargeLevelStepTracker();
2809
2810 /**
2811 * Return the array of daily discharge step durations.
2812 */
2813 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002814
2815 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002816 * Compute an approximation for how much time (in microseconds) remains until the battery
2817 * is fully charged. Returns -1 if no time can be computed: either there is not
2818 * enough current data to make a decision, or the battery is currently
2819 * discharging.
2820 *
2821 * @param curTime The current elepsed realtime in microseconds.
2822 */
2823 public abstract long computeChargeTimeRemaining(long curTime);
2824
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002825 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002826 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002827 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002828 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002829
2830 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002831 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002832 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002833 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002834
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002835 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2836
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002837 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002838
Evan Millarc64edde2009-04-18 12:26:32 -07002839 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840
Bookatz50df7112017-08-04 14:53:26 -07002841 /**
2842 * Returns Timers tracking the total time of each Resource Power Manager state and voter.
2843 */
2844 public abstract Map<String, ? extends Timer> getRpmStats();
2845 /**
2846 * Returns Timers tracking the screen-off time of each Resource Power Manager state and voter.
2847 */
2848 public abstract Map<String, ? extends Timer> getScreenOffRpmStats();
2849
2850
James Carr2dd7e5e2016-07-20 18:48:39 -07002851 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2852
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002853 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2854
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002855 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 long days = seconds / (60 * 60 * 24);
2857 if (days != 0) {
2858 out.append(days);
2859 out.append("d ");
2860 }
2861 long used = days * 60 * 60 * 24;
2862
2863 long hours = (seconds - used) / (60 * 60);
2864 if (hours != 0 || used != 0) {
2865 out.append(hours);
2866 out.append("h ");
2867 }
2868 used += hours * 60 * 60;
2869
2870 long mins = (seconds-used) / 60;
2871 if (mins != 0 || used != 0) {
2872 out.append(mins);
2873 out.append("m ");
2874 }
2875 used += mins * 60;
2876
2877 if (seconds != 0 || used != 0) {
2878 out.append(seconds-used);
2879 out.append("s ");
2880 }
2881 }
2882
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002883 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002885 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 sb.append(time - (sec * 1000));
2887 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 }
2889
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002890 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002891 long sec = time / 1000;
2892 formatTimeRaw(sb, sec);
2893 sb.append(time - (sec * 1000));
2894 sb.append("ms");
2895 }
2896
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002897 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002899 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 }
2901 float perc = ((float)num) / ((float)den) * 100;
2902 mFormatBuilder.setLength(0);
2903 mFormatter.format("%.1f%%", perc);
2904 return mFormatBuilder.toString();
2905 }
2906
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002907 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002908 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07002909
Evan Millar22ac0432009-03-31 11:33:18 -07002910 if (bytes < BYTES_PER_KB) {
2911 return bytes + "B";
2912 } else if (bytes < BYTES_PER_MB) {
2913 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2914 return mFormatBuilder.toString();
2915 } else if (bytes < BYTES_PER_GB){
2916 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2917 return mFormatBuilder.toString();
2918 } else {
2919 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2920 return mFormatBuilder.toString();
2921 }
2922 }
2923
Kweku Adams103351f2017-10-16 14:39:34 -07002924 private static long roundUsToMs(long timeUs) {
2925 return (timeUs + 500) / 1000;
2926 }
2927
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002928 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002929 if (timer != null) {
2930 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002931 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002932 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2933 return totalTimeMillis;
2934 }
2935 return 0;
2936 }
2937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 /**
2939 *
2940 * @param sb a StringBuilder object.
2941 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002942 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002944 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 * @param linePrefix a String to be prepended to each line of output.
2946 * @return the line prefix
2947 */
2948 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002949 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002952 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002953
Evan Millarc64edde2009-04-18 12:26:32 -07002954 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 if (totalTimeMillis != 0) {
2956 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002957 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002958 if (name != null) {
2959 sb.append(name);
2960 sb.append(' ');
2961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 sb.append('(');
2963 sb.append(count);
2964 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002965 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2966 if (maxDurationMs >= 0) {
2967 sb.append(" max=");
2968 sb.append(maxDurationMs);
2969 }
Bookatz506a8182017-05-01 14:18:42 -07002970 // Put actual time if it is available and different from totalTimeMillis.
2971 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
2972 if (totalDurMs > totalTimeMillis) {
2973 sb.append(" actual=");
2974 sb.append(totalDurMs);
2975 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07002976 if (timer.isRunningLocked()) {
2977 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2978 if (currentMs >= 0) {
2979 sb.append(" (running for ");
2980 sb.append(currentMs);
2981 sb.append("ms)");
2982 } else {
2983 sb.append(" (running)");
2984 }
2985 }
2986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002987 return ", ";
2988 }
2989 }
2990 return linePrefix;
2991 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002992
2993 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07002994 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002995 *
2996 * @param pw a PrintWriter object to print to.
2997 * @param sb a StringBuilder object.
2998 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08002999 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003000 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
3001 * @param prefix a String to be prepended to each line of output.
3002 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07003003 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003004 */
3005 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07003006 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003007 if (timer != null) {
3008 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07003009 final long totalTimeMs = (timer.getTotalTimeLocked(
3010 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003011 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003012 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003013 sb.setLength(0);
3014 sb.append(prefix);
3015 sb.append(" ");
3016 sb.append(type);
3017 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003018 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003019 sb.append("realtime (");
3020 sb.append(count);
3021 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003022 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
3023 if (maxDurationMs >= 0) {
3024 sb.append(" max=");
3025 sb.append(maxDurationMs);
3026 }
3027 if (timer.isRunningLocked()) {
3028 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
3029 if (currentMs >= 0) {
3030 sb.append(" (running for ");
3031 sb.append(currentMs);
3032 sb.append("ms)");
3033 } else {
3034 sb.append(" (running)");
3035 }
3036 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003037 pw.println(sb.toString());
3038 return true;
3039 }
3040 }
3041 return false;
3042 }
Bookatzc8c44962017-05-11 12:12:54 -07003043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 /**
3045 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07003046 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047 * @param sb a StringBuilder object.
3048 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003049 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003050 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003051 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052 * @param linePrefix a String to be prepended to each line of output.
3053 * @return the line prefix
3054 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003055 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
3056 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 long totalTimeMicros = 0;
3058 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07003059 long max = 0;
3060 long current = 0;
3061 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003063 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07003064 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003065 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3066 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07003067 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003068 }
3069 sb.append(linePrefix);
3070 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
3071 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07003072 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003074 sb.append(',');
3075 sb.append(current);
3076 sb.append(',');
3077 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07003078 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
3079 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
3080 // totalDuration independent of totalTimeMicros (since they are not pooled).
3081 if (name != null) {
3082 sb.append(',');
3083 sb.append(totalDuration);
3084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 return ",";
3086 }
Bookatz506a8182017-05-01 14:18:42 -07003087
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003088 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
3089 String type) {
3090 pw.print(BATTERY_STATS_CHECKIN_VERSION);
3091 pw.print(',');
3092 pw.print(uid);
3093 pw.print(',');
3094 pw.print(category);
3095 pw.print(',');
3096 pw.print(type);
3097 }
3098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 /**
3100 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07003101 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 * @param pw the PageWriter to dump log to
3103 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3104 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3105 * @param args type-dependent data arguments
3106 */
Bookatzc8c44962017-05-11 12:12:54 -07003107 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003109 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003110 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003111 pw.print(',');
3112 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003114 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07003116
3117 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003118 * Dump a given timer stat for terse checkin mode.
3119 *
3120 * @param pw the PageWriter to dump log to
3121 * @param uid the UID to log
3122 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3123 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3124 * @param timer a {@link Timer} to dump stats for
3125 * @param rawRealtime the current elapsed realtime of the system in microseconds
3126 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3127 */
3128 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
3129 Timer timer, long rawRealtime, int which) {
3130 if (timer != null) {
3131 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003132 final long totalTime = roundUsToMs(timer.getTotalTimeLocked(rawRealtime, which));
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003133 final int count = timer.getCountLocked(which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003134 if (totalTime != 0 || count != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003135 dumpLine(pw, uid, category, type, totalTime, count);
3136 }
3137 }
3138 }
3139
3140 /**
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003141 * Dump a given timer stat to the proto stream.
3142 *
3143 * @param proto the ProtoOutputStream to log to
3144 * @param fieldId type of data, the field to save to (e.g. AggregatedBatteryStats.WAKELOCK)
3145 * @param timer a {@link Timer} to dump stats for
3146 * @param rawRealtimeUs the current elapsed realtime of the system in microseconds
3147 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3148 */
3149 private static void dumpTimer(ProtoOutputStream proto, long fieldId,
Kweku Adams87b19ec2017-10-09 12:40:03 -07003150 Timer timer, long rawRealtimeUs, int which) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003151 if (timer == null) {
3152 return;
3153 }
3154 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003155 final long timeMs = roundUsToMs(timer.getTotalTimeLocked(rawRealtimeUs, which));
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003156 final int count = timer.getCountLocked(which);
Kweku Adams103351f2017-10-16 14:39:34 -07003157 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs / 1000);
3158 final long curDurationMs = timer.getCurrentDurationMsLocked(rawRealtimeUs / 1000);
3159 final long totalDurationMs = timer.getTotalDurationMsLocked(rawRealtimeUs / 1000);
3160 if (timeMs != 0 || count != 0 || maxDurationMs != -1 || curDurationMs != -1
3161 || totalDurationMs != -1) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003162 final long token = proto.start(fieldId);
Kweku Adams103351f2017-10-16 14:39:34 -07003163 proto.write(TimerProto.DURATION_MS, timeMs);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003164 proto.write(TimerProto.COUNT, count);
Kweku Adams103351f2017-10-16 14:39:34 -07003165 // These values will be -1 for timers that don't implement the functionality.
3166 if (maxDurationMs != -1) {
3167 proto.write(TimerProto.MAX_DURATION_MS, maxDurationMs);
3168 }
3169 if (curDurationMs != -1) {
3170 proto.write(TimerProto.CURRENT_DURATION_MS, curDurationMs);
3171 }
3172 if (totalDurationMs != -1) {
3173 proto.write(TimerProto.TOTAL_DURATION_MS, totalDurationMs);
3174 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003175 proto.end(token);
3176 }
3177 }
3178
3179 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003180 * Checks if the ControllerActivityCounter has any data worth dumping.
3181 */
3182 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
3183 if (counter == null) {
3184 return false;
3185 }
3186
3187 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
3188 || counter.getRxTimeCounter().getCountLocked(which) != 0
3189 || counter.getPowerCounter().getCountLocked(which) != 0) {
3190 return true;
3191 }
3192
3193 for (LongCounter c : counter.getTxTimeCounters()) {
3194 if (c.getCountLocked(which) != 0) {
3195 return true;
3196 }
3197 }
3198 return false;
3199 }
3200
3201 /**
3202 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3203 * The order of the arguments in the final check in line is:
3204 *
3205 * idle, rx, power, tx...
3206 *
3207 * where tx... is one or more transmit level times.
3208 */
3209 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
3210 String type,
3211 ControllerActivityCounter counter,
3212 int which) {
3213 if (!controllerActivityHasData(counter, which)) {
3214 return;
3215 }
3216
3217 dumpLineHeader(pw, uid, category, type);
3218 pw.print(",");
3219 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
3220 pw.print(",");
3221 pw.print(counter.getRxTimeCounter().getCountLocked(which));
3222 pw.print(",");
3223 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3224 for (LongCounter c : counter.getTxTimeCounters()) {
3225 pw.print(",");
3226 pw.print(c.getCountLocked(which));
3227 }
3228 pw.println();
3229 }
3230
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003231 /**
3232 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3233 */
3234 private static void dumpControllerActivityProto(ProtoOutputStream proto, long fieldId,
3235 ControllerActivityCounter counter,
3236 int which) {
3237 if (!controllerActivityHasData(counter, which)) {
3238 return;
3239 }
3240
3241 final long cToken = proto.start(fieldId);
3242
3243 proto.write(ControllerActivityProto.IDLE_DURATION_MS,
3244 counter.getIdleTimeCounter().getCountLocked(which));
3245 proto.write(ControllerActivityProto.RX_DURATION_MS,
3246 counter.getRxTimeCounter().getCountLocked(which));
3247 proto.write(ControllerActivityProto.POWER_MAH,
3248 counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3249
3250 long tToken;
3251 LongCounter[] txCounters = counter.getTxTimeCounters();
3252 for (int i = 0; i < txCounters.length; ++i) {
3253 LongCounter c = txCounters[i];
3254 tToken = proto.start(ControllerActivityProto.TX);
3255 proto.write(ControllerActivityProto.TxLevel.LEVEL, i);
3256 proto.write(ControllerActivityProto.TxLevel.DURATION_MS, c.getCountLocked(which));
3257 proto.end(tToken);
3258 }
3259
3260 proto.end(cToken);
3261 }
3262
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003263 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
3264 String prefix, String controllerName,
3265 ControllerActivityCounter counter,
3266 int which) {
3267 if (controllerActivityHasData(counter, which)) {
3268 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
3269 }
3270 }
3271
3272 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
3273 String controllerName,
3274 ControllerActivityCounter counter, int which) {
3275 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
3276 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
3277 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003278 // Battery real time
3279 final long totalControllerActivityTimeMs
3280 = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003281 long totalTxTimeMs = 0;
3282 for (LongCounter txState : counter.getTxTimeCounters()) {
3283 totalTxTimeMs += txState.getCountLocked(which);
3284 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003285 final long sleepTimeMs
3286 = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + totalTxTimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003287
3288 sb.setLength(0);
3289 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003290 sb.append(" ");
3291 sb.append(controllerName);
3292 sb.append(" Sleep time: ");
3293 formatTimeMs(sb, sleepTimeMs);
3294 sb.append("(");
3295 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3296 sb.append(")");
3297 pw.println(sb.toString());
3298
3299 sb.setLength(0);
3300 sb.append(prefix);
3301 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003302 sb.append(controllerName);
3303 sb.append(" Idle time: ");
3304 formatTimeMs(sb, idleTimeMs);
3305 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003306 sb.append(formatRatioLocked(idleTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003307 sb.append(")");
3308 pw.println(sb.toString());
3309
3310 sb.setLength(0);
3311 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003312 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003313 sb.append(controllerName);
3314 sb.append(" Rx time: ");
3315 formatTimeMs(sb, rxTimeMs);
3316 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003317 sb.append(formatRatioLocked(rxTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003318 sb.append(")");
3319 pw.println(sb.toString());
3320
3321 sb.setLength(0);
3322 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003323 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003324 sb.append(controllerName);
3325 sb.append(" Tx time: ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003326
Siddharth Ray3c648c42017-10-02 17:30:58 -07003327 String [] powerLevel;
3328 switch(controllerName) {
3329 case "Cellular":
3330 powerLevel = new String[] {
3331 " less than 0dBm: ",
3332 " 0dBm to 8dBm: ",
3333 " 8dBm to 15dBm: ",
3334 " 15dBm to 20dBm: ",
3335 " above 20dBm: "};
3336 break;
3337 default:
3338 powerLevel = new String[] {"[0]", "[1]", "[2]", "[3]", "[4]"};
3339 break;
3340 }
3341 final int numTxLvls = Math.min(counter.getTxTimeCounters().length, powerLevel.length);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003342 if (numTxLvls > 1) {
Siddharth Ray3c648c42017-10-02 17:30:58 -07003343 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003344 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3345 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3346 sb.setLength(0);
3347 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003348 sb.append(" ");
3349 sb.append(powerLevel[lvl]);
3350 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003351 formatTimeMs(sb, txLvlTimeMs);
3352 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003353 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003354 sb.append(")");
3355 pw.println(sb.toString());
3356 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003357 } else {
3358 final long txLvlTimeMs = counter.getTxTimeCounters()[0].getCountLocked(which);
3359 formatTimeMs(sb, txLvlTimeMs);
3360 sb.append("(");
3361 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
3362 sb.append(")");
3363 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003364 }
3365
Siddharth Ray3c648c42017-10-02 17:30:58 -07003366 if (powerDrainMaMs > 0) {
3367 sb.setLength(0);
3368 sb.append(prefix);
3369 sb.append(" ");
3370 sb.append(controllerName);
3371 sb.append(" Battery drain: ").append(
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003372 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
Siddharth Ray3c648c42017-10-02 17:30:58 -07003373 sb.append("mAh");
3374 pw.println(sb.toString());
3375 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003376 }
3377
3378 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003379 * Temporary for settings.
3380 */
3381 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3382 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3383 }
3384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385 /**
3386 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003387 *
Kweku Adams87b19ec2017-10-09 12:40:03 -07003388 * NOTE: all times are expressed in microseconds, unless specified otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003389 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003390 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3391 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 final long rawUptime = SystemClock.uptimeMillis() * 1000;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003393 final long rawRealtimeMs = SystemClock.elapsedRealtime();
3394 final long rawRealtime = rawRealtimeMs * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003395 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3397 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003398 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3399 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3400 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 final long totalRealtime = computeRealtime(rawRealtime, which);
3402 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003403 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Mike Mac2f518a2017-09-19 16:06:03 -07003404 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003405 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003406 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003407 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3408 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003409 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003410 rawRealtime, which);
3411 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3412 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003413 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003414 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003415 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003416 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003417 final long dischargeCount = getUahDischarge(which);
3418 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
3419 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Ma15313c92017-11-15 17:58:21 -08003420 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
3421 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003422
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003423 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003424
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003425 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003426 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003427
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003428 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003431 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003432 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003433 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003434 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003435 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003436 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003437 getEstimatedBatteryCapacity(),
Mike Mac2f518a2017-09-19 16:06:03 -07003438 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity(),
3439 screenDozeTime / 1000);
Adam Lesinski67c134f2016-06-10 15:15:08 -07003440
Bookatzc8c44962017-05-11 12:12:54 -07003441
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003442 // Calculate both wakelock and wifi multicast wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003443 long fullWakeLockTimeTotal = 0;
3444 long partialWakeLockTimeTotal = 0;
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003445 long multicastWakeLockTimeTotalMicros = 0;
3446 int multicastWakeLockCountTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003447
Evan Millar22ac0432009-03-31 11:33:18 -07003448 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003449 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003450
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003451 // First calculating the wakelock stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003452 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3453 = u.getWakelockStats();
3454 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3455 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003456
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003457 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3458 if (fullWakeTimer != null) {
3459 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3460 which);
3461 }
3462
3463 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3464 if (partialWakeTimer != null) {
3465 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3466 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003467 }
3468 }
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003469
3470 // Now calculating the wifi multicast wakelock stats
3471 final Timer mcTimer = u.getMulticastWakelockStats();
3472 if (mcTimer != null) {
3473 multicastWakeLockTimeTotalMicros += mcTimer.getTotalTimeLocked(rawRealtime, which);
3474 multicastWakeLockCountTotal += mcTimer.getCountLocked(which);
3475 }
Evan Millar22ac0432009-03-31 11:33:18 -07003476 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003477
3478 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003479 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3480 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3481 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3482 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3483 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3484 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3485 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3486 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003487 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3488 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003489 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3490 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003491 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3492 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003493
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003494 // Dump Modem controller stats
3495 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3496 getModemControllerActivity(), which);
3497
Adam Lesinskie283d332015-04-16 12:29:25 -07003498 // Dump Wifi controller stats
3499 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3500 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003501 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003502 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003503
3504 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3505 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003506
3507 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003508 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3509 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003511 // Dump misc stats
3512 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003513 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003514 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003515 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003516 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003517 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003518 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3519 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003520 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003521 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3522 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3523 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3524 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003525 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003526
Dianne Hackborn617f8772009-03-31 15:04:46 -07003527 // Dump screen brightness stats
3528 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3529 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003530 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003531 }
3532 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003533
Dianne Hackborn627bba72009-03-24 22:32:56 -07003534 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003535 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3536 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003537 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003538 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003539 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003540 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003541 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003542 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003543 args[i] = getPhoneSignalStrengthCount(i, which);
3544 }
3545 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003546
Dianne Hackborn627bba72009-03-24 22:32:56 -07003547 // Dump network type stats
3548 args = new Object[NUM_DATA_CONNECTION_TYPES];
3549 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003550 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003551 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003552 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3553 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3554 args[i] = getPhoneDataConnectionCount(i, which);
3555 }
3556 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003557
3558 // Dump wifi state stats
3559 args = new Object[NUM_WIFI_STATES];
3560 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003561 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003562 }
3563 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3564 for (int i=0; i<NUM_WIFI_STATES; i++) {
3565 args[i] = getWifiStateCount(i, which);
3566 }
3567 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3568
Dianne Hackborn3251b902014-06-20 14:40:53 -07003569 // Dump wifi suppl state stats
3570 args = new Object[NUM_WIFI_SUPPL_STATES];
3571 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3572 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3573 }
3574 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3575 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3576 args[i] = getWifiSupplStateCount(i, which);
3577 }
3578 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3579
3580 // Dump wifi signal strength stats
3581 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3582 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3583 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3584 }
3585 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3586 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3587 args[i] = getWifiSignalStrengthCount(i, which);
3588 }
3589 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3590
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003591 // Dump Multicast total stats
3592 dumpLine(pw, 0 /* uid */, category, WIFI_MULTICAST_TOTAL_DATA,
3593 multicastWakeLockTimeTotalMicros / 1000,
3594 multicastWakeLockCountTotal);
3595
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003596 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003597 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003598 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003599 }
Bookatzc8c44962017-05-11 12:12:54 -07003600
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003601 if (which == STATS_SINCE_UNPLUGGED) {
3602 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3603 getDischargeStartLevel()-getDischargeCurrentLevel(),
3604 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003605 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
Mike Mac2f518a2017-09-19 16:06:03 -07003606 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003607 getDischargeAmountScreenDoze(), dischargeScreenDozeCount / 1000,
3608 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003609 } else {
3610 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3611 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003612 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003613 getDischargeAmountScreenOffSinceCharge(),
Mike Mac2f518a2017-09-19 16:06:03 -07003614 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003615 getDischargeAmountScreenDozeSinceCharge(), dischargeScreenDozeCount / 1000,
3616 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003617 }
Bookatzc8c44962017-05-11 12:12:54 -07003618
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003619 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003620 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003621 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003622 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003623 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003624 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003625 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3626 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003627 }
Evan Millarc64edde2009-04-18 12:26:32 -07003628 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003629 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003630 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003631 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3632 // Not doing the regular wake lock formatting to remain compatible
3633 // with the old checkin format.
3634 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3635 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003636 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003637 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003638 }
3639 }
Evan Millarc64edde2009-04-18 12:26:32 -07003640 }
Bookatzc8c44962017-05-11 12:12:54 -07003641
Bookatz50df7112017-08-04 14:53:26 -07003642 final Map<String, ? extends Timer> rpmStats = getRpmStats();
3643 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
3644 if (rpmStats.size() > 0) {
3645 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
3646 sb.setLength(0);
3647 Timer totalTimer = ent.getValue();
3648 long timeMs = (totalTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3649 int count = totalTimer.getCountLocked(which);
3650 Timer screenOffTimer = screenOffRpmStats.get(ent.getKey());
3651 long screenOffTimeMs = screenOffTimer != null
3652 ? (screenOffTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3653 int screenOffCount = screenOffTimer != null
3654 ? screenOffTimer.getCountLocked(which) : 0;
Bookatz82b341172017-09-07 19:06:08 -07003655 if (SCREEN_OFF_RPM_STATS_ENABLED) {
3656 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3657 "\"" + ent.getKey() + "\"", timeMs, count, screenOffTimeMs,
3658 screenOffCount);
3659 } else {
3660 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3661 "\"" + ent.getKey() + "\"", timeMs, count);
3662 }
Bookatz50df7112017-08-04 14:53:26 -07003663 }
3664 }
3665
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003666 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003667 helper.create(this);
3668 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003669 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003670 if (sippers != null && sippers.size() > 0) {
3671 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3672 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003673 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003674 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3675 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
Kweku Adams87b19ec2017-10-09 12:40:03 -07003676 int uid = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003677 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003678 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003679 String label;
3680 switch (bs.drainType) {
3681 case IDLE:
3682 label="idle";
3683 break;
3684 case CELL:
3685 label="cell";
3686 break;
3687 case PHONE:
3688 label="phone";
3689 break;
3690 case WIFI:
3691 label="wifi";
3692 break;
3693 case BLUETOOTH:
3694 label="blue";
3695 break;
3696 case SCREEN:
3697 label="scrn";
3698 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003699 case FLASHLIGHT:
3700 label="flashlight";
3701 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003702 case APP:
3703 uid = bs.uidObj.getUid();
3704 label = "uid";
3705 break;
3706 case USER:
3707 uid = UserHandle.getUid(bs.userId, 0);
3708 label = "user";
3709 break;
3710 case UNACCOUNTED:
3711 label = "unacc";
3712 break;
3713 case OVERCOUNTED:
3714 label = "over";
3715 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003716 case CAMERA:
3717 label = "camera";
3718 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003719 case MEMORY:
3720 label = "memory";
3721 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003722 default:
3723 label = "???";
3724 }
3725 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07003726 BatteryStatsHelper.makemAh(bs.totalPowerMah),
3727 bs.shouldHide ? 1 : 0,
3728 BatteryStatsHelper.makemAh(bs.screenPowerMah),
3729 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003730 }
3731 }
3732
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003733 final long[] cpuFreqs = getCpuFreqs();
3734 if (cpuFreqs != null) {
3735 sb.setLength(0);
3736 for (int i = 0; i < cpuFreqs.length; ++i) {
3737 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3738 }
3739 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3740 }
3741
Kweku Adams87b19ec2017-10-09 12:40:03 -07003742 // Dump stats per UID.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003743 for (int iu = 0; iu < NU; iu++) {
3744 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003745 if (reqUid >= 0 && uid != reqUid) {
3746 continue;
3747 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003748 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003750 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003751 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3752 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3753 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3754 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3755 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3756 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3757 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3758 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003759 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003760 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3761 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003762 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003763 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3764 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003765 // Background data transfers
3766 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3767 which);
3768 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3769 which);
3770 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3771 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3772 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3773 which);
3774 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3775 which);
3776 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3777 which);
3778 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3779 which);
3780
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003781 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3782 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003783 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003784 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3785 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3786 || wifiBytesBgTx > 0
3787 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3788 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003789 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3790 wifiBytesRx, wifiBytesTx,
3791 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003792 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003793 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003794 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3795 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3796 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3797 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003798 }
3799
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003800 // Dump modem controller data, per UID.
3801 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3802 u.getModemControllerActivity(), which);
3803
3804 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003805 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3806 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3807 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003808 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3809 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003810 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3811 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3812 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003813 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003814 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003815 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3816 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003817 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3818 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003819 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003820 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003822
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003823 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3824 u.getWifiControllerActivity(), which);
3825
Bookatz867c0d72017-03-07 18:23:42 -08003826 final Timer bleTimer = u.getBluetoothScanTimer();
3827 if (bleTimer != null) {
3828 // Convert from microseconds to milliseconds with rounding
3829 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3830 / 1000;
3831 if (totalTime != 0) {
3832 final int count = bleTimer.getCountLocked(which);
3833 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3834 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003835 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3836 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3837 final long actualTimeBg = bleTimerBg != null ?
3838 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003839 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003840 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3841 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003842 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3843 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3844 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3845 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3846 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
3847 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3848 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
3849 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3850 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
3851 final Timer unoptimizedScanTimerBg =
3852 u.getBluetoothUnoptimizedScanBackgroundTimer();
3853 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
3854 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3855 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
3856 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3857
Bookatz867c0d72017-03-07 18:23:42 -08003858 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07003859 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
3860 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
3861 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08003862 }
3863 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003864
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003865 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3866 u.getBluetoothControllerActivity(), which);
3867
Dianne Hackborn617f8772009-03-31 15:04:46 -07003868 if (u.hasUserActivity()) {
3869 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3870 boolean hasData = false;
3871 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3872 int val = u.getUserActivityCount(i, which);
3873 args[i] = val;
3874 if (val != 0) hasData = true;
3875 }
3876 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003877 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003878 }
3879 }
Bookatzc8c44962017-05-11 12:12:54 -07003880
3881 if (u.getAggregatedPartialWakelockTimer() != null) {
3882 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07003883 // Times are since reset (regardless of 'which')
3884 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07003885 final Timer bgTimer = timer.getSubTimer();
3886 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003887 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07003888 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
3889 }
3890
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003891 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3892 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3893 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3894 String linePrefix = "";
3895 sb.setLength(0);
3896 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3897 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003898 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3899 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003900 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003901 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
3902 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003903 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3904 rawRealtime, "w", which, linePrefix);
3905
Kweku Adams103351f2017-10-16 14:39:34 -07003906 // Only log if we had at least one wakelock...
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003907 if (sb.length() > 0) {
3908 String name = wakelocks.keyAt(iw);
3909 if (name.indexOf(',') >= 0) {
3910 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003911 }
Yi Jin02483362017-08-04 11:30:44 -07003912 if (name.indexOf('\n') >= 0) {
3913 name = name.replace('\n', '_');
3914 }
3915 if (name.indexOf('\r') >= 0) {
3916 name = name.replace('\r', '_');
3917 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003918 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003919 }
3920 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003921
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003922 // WiFi Multicast Wakelock Statistics
3923 final Timer mcTimer = u.getMulticastWakelockStats();
3924 if (mcTimer != null) {
3925 final long totalMcWakelockTimeMs =
3926 mcTimer.getTotalTimeLocked(rawRealtime, which) / 1000 ;
3927 final int countMcWakelock = mcTimer.getCountLocked(which);
3928 if(totalMcWakelockTimeMs > 0) {
3929 dumpLine(pw, uid, category, WIFI_MULTICAST_DATA,
3930 totalMcWakelockTimeMs, countMcWakelock);
3931 }
3932 }
3933
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003934 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3935 for (int isy=syncs.size()-1; isy>=0; isy--) {
3936 final Timer timer = syncs.valueAt(isy);
3937 // Convert from microseconds to milliseconds with rounding
3938 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3939 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07003940 final Timer bgTimer = timer.getSubTimer();
3941 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003942 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07003943 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003944 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003945 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07003946 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003947 }
3948 }
3949
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003950 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3951 for (int ij=jobs.size()-1; ij>=0; ij--) {
3952 final Timer timer = jobs.valueAt(ij);
3953 // Convert from microseconds to milliseconds with rounding
3954 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3955 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07003956 final Timer bgTimer = timer.getSubTimer();
3957 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003958 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07003959 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003960 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003961 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07003962 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003963 }
3964 }
3965
Dianne Hackborn94326cb2017-06-28 16:17:20 -07003966 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
3967 for (int ic=completions.size()-1; ic>=0; ic--) {
3968 SparseIntArray types = completions.valueAt(ic);
3969 if (types != null) {
3970 dumpLine(pw, uid, category, JOB_COMPLETION_DATA,
3971 "\"" + completions.keyAt(ic) + "\"",
3972 types.get(JobParameters.REASON_CANCELED, 0),
3973 types.get(JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, 0),
3974 types.get(JobParameters.REASON_PREEMPT, 0),
3975 types.get(JobParameters.REASON_TIMEOUT, 0),
3976 types.get(JobParameters.REASON_DEVICE_IDLE, 0));
3977 }
3978 }
3979
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003980 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3981 rawRealtime, which);
3982 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3983 rawRealtime, which);
3984 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3985 rawRealtime, which);
3986 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3987 rawRealtime, which);
3988
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003989 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3990 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003991 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003992 final Uid.Sensor se = sensors.valueAt(ise);
3993 final int sensorNumber = sensors.keyAt(ise);
3994 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003995 if (timer != null) {
3996 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003997 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3998 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003999 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004000 final int count = timer.getCountLocked(which);
4001 final Timer bgTimer = se.getSensorBackgroundTime();
4002 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004003 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4004 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4005 final long bgActualTime = bgTimer != null ?
4006 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4007 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
4008 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004009 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004010 }
4011 }
4012
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004013 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
4014 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004015
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07004016 dumpTimer(pw, uid, category, FOREGROUND_ACTIVITY_DATA, u.getForegroundActivityTimer(),
4017 rawRealtime, which);
4018
4019 dumpTimer(pw, uid, category, FOREGROUND_SERVICE_DATA, u.getForegroundServiceTimer(),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004020 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004021
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004022 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07004023 long totalStateTime = 0;
4024 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08004025 final long time = u.getProcessStateTime(ips, rawRealtime, which);
4026 totalStateTime += time;
4027 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004028 }
4029 if (totalStateTime > 0) {
4030 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
4031 }
4032
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004033 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4034 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004035 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004036 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004037 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004038 }
4039
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004040 // If the cpuFreqs is null, then don't bother checking for cpu freq times.
4041 if (cpuFreqs != null) {
4042 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
4043 // If total cpuFreqTimes is null, then we don't need to check for
4044 // screenOffCpuFreqTimes.
4045 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
4046 sb.setLength(0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004047 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004048 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004049 }
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004050 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
4051 if (screenOffCpuFreqTimeMs != null) {
4052 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
4053 sb.append("," + screenOffCpuFreqTimeMs[i]);
4054 }
4055 } else {
4056 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
4057 sb.append(",0");
4058 }
4059 }
4060 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
4061 cpuFreqTimeMs.length, sb.toString());
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004062 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -08004063
4064 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
4065 final long[] timesMs = u.getCpuFreqTimes(which, procState);
4066 if (timesMs != null && timesMs.length == cpuFreqs.length) {
4067 sb.setLength(0);
4068 for (int i = 0; i < timesMs.length; ++i) {
4069 sb.append((i == 0 ? "" : ",") + timesMs[i]);
4070 }
4071 final long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(
4072 which, procState);
4073 if (screenOffTimesMs != null) {
4074 for (int i = 0; i < screenOffTimesMs.length; ++i) {
4075 sb.append("," + screenOffTimesMs[i]);
4076 }
4077 } else {
4078 for (int i = 0; i < timesMs.length; ++i) {
4079 sb.append(",0");
4080 }
4081 }
4082 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA,
4083 Uid.UID_PROCESS_TYPES[procState], timesMs.length, sb.toString());
4084 }
4085 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004086 }
4087
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004088 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4089 = u.getProcessStats();
4090 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4091 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004092
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004093 final long userMillis = ps.getUserTime(which);
4094 final long systemMillis = ps.getSystemTime(which);
4095 final long foregroundMillis = ps.getForegroundTime(which);
4096 final int starts = ps.getStarts(which);
4097 final int numCrashes = ps.getNumCrashes(which);
4098 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004099
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004100 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
4101 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004102 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
4103 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004104 }
4105 }
4106
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004107 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4108 = u.getPackageStats();
4109 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4110 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4111 int wakeups = 0;
4112 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4113 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07004114 int count = alarms.valueAt(iwa).getCountLocked(which);
4115 wakeups += count;
4116 String name = alarms.keyAt(iwa).replace(',', '_');
4117 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004118 }
4119 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4120 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4121 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4122 final long startTime = ss.getStartTime(batteryUptime, which);
4123 final int starts = ss.getStarts(which);
4124 final int launches = ss.getLaunches(which);
4125 if (startTime != 0 || starts != 0 || launches != 0) {
4126 dumpLine(pw, uid, category, APK_DATA,
4127 wakeups, // wakeup alarms
4128 packageStats.keyAt(ipkg), // Apk
4129 serviceStats.keyAt(isvc), // service
4130 startTime / 1000, // time spent started, in ms
4131 starts,
4132 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004133 }
4134 }
4135 }
4136 }
4137 }
4138
Dianne Hackborn81038902012-11-26 17:04:09 -08004139 static final class TimerEntry {
4140 final String mName;
4141 final int mId;
4142 final BatteryStats.Timer mTimer;
4143 final long mTime;
4144 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
4145 mName = name;
4146 mId = id;
4147 mTimer = timer;
4148 mTime = time;
4149 }
4150 }
4151
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004152 private void printmAh(PrintWriter printer, double power) {
4153 printer.print(BatteryStatsHelper.makemAh(power));
4154 }
4155
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004156 private void printmAh(StringBuilder sb, double power) {
4157 sb.append(BatteryStatsHelper.makemAh(power));
4158 }
4159
Dianne Hackbornd953c532014-08-16 18:17:38 -07004160 /**
4161 * Temporary for settings.
4162 */
4163 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
4164 int reqUid) {
4165 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
4166 }
4167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004169 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07004170 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004171 final long rawUptime = SystemClock.uptimeMillis() * 1000;
4172 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07004173 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175
4176 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
4177 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
4178 final long totalRealtime = computeRealtime(rawRealtime, which);
4179 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004180 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
4181 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
4182 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004183 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
4184 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -07004185 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004186
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004187 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07004188
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004189 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07004190 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004191
Adam Lesinskif9b20a92016-06-17 17:30:01 -07004192 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
4193 if (estimatedBatteryCapacity > 0) {
4194 sb.setLength(0);
4195 sb.append(prefix);
4196 sb.append(" Estimated battery capacity: ");
4197 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
4198 sb.append(" mAh");
4199 pw.println(sb.toString());
4200 }
4201
Jocelyn Dangc627d102017-04-14 13:15:14 -07004202 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
4203 if (minLearnedBatteryCapacity > 0) {
4204 sb.setLength(0);
4205 sb.append(prefix);
4206 sb.append(" Min learned battery capacity: ");
4207 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
4208 sb.append(" mAh");
4209 pw.println(sb.toString());
4210 }
4211 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
4212 if (maxLearnedBatteryCapacity > 0) {
4213 sb.setLength(0);
4214 sb.append(prefix);
4215 sb.append(" Max learned battery capacity: ");
4216 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
4217 sb.append(" mAh");
4218 pw.println(sb.toString());
4219 }
4220
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004221 sb.setLength(0);
4222 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004223 sb.append(" Time on battery: ");
4224 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
4225 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
4226 sb.append(") realtime, ");
4227 formatTimeMs(sb, whichBatteryUptime / 1000);
4228 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, whichBatteryRealtime));
4229 sb.append(") uptime");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004230 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004231
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004232 sb.setLength(0);
4233 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004234 sb.append(" Time on battery screen off: ");
4235 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
4236 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, whichBatteryRealtime));
4237 sb.append(") realtime, ");
4238 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
4239 sb.append("(");
4240 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, whichBatteryRealtime));
4241 sb.append(") uptime");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004242 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004243
4244 sb.setLength(0);
4245 sb.append(prefix);
4246 sb.append(" Time on battery screen doze: ");
4247 formatTimeMs(sb, screenDozeTime / 1000); sb.append("(");
4248 sb.append(formatRatioLocked(screenDozeTime, whichBatteryRealtime));
4249 sb.append(")");
4250 pw.println(sb.toString());
4251
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004252 sb.setLength(0);
4253 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004254 sb.append(" Total run time: ");
4255 formatTimeMs(sb, totalRealtime / 1000);
4256 sb.append("realtime, ");
4257 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004258 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07004259 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004260 if (batteryTimeRemaining >= 0) {
4261 sb.setLength(0);
4262 sb.append(prefix);
4263 sb.append(" Battery time remaining: ");
4264 formatTimeMs(sb, batteryTimeRemaining / 1000);
4265 pw.println(sb.toString());
4266 }
4267 if (chargeTimeRemaining >= 0) {
4268 sb.setLength(0);
4269 sb.append(prefix);
4270 sb.append(" Charge time remaining: ");
4271 formatTimeMs(sb, chargeTimeRemaining / 1000);
4272 pw.println(sb.toString());
4273 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004274
Kweku Adams87b19ec2017-10-09 12:40:03 -07004275 final long dischargeCount = getUahDischarge(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004276 if (dischargeCount >= 0) {
4277 sb.setLength(0);
4278 sb.append(prefix);
4279 sb.append(" Discharge: ");
4280 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
4281 sb.append(" mAh");
4282 pw.println(sb.toString());
4283 }
4284
Kweku Adams87b19ec2017-10-09 12:40:03 -07004285 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004286 if (dischargeScreenOffCount >= 0) {
4287 sb.setLength(0);
4288 sb.append(prefix);
4289 sb.append(" Screen off discharge: ");
4290 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
4291 sb.append(" mAh");
4292 pw.println(sb.toString());
4293 }
4294
Kweku Adams87b19ec2017-10-09 12:40:03 -07004295 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Mac2f518a2017-09-19 16:06:03 -07004296 if (dischargeScreenDozeCount >= 0) {
4297 sb.setLength(0);
4298 sb.append(prefix);
4299 sb.append(" Screen doze discharge: ");
4300 sb.append(BatteryStatsHelper.makemAh(dischargeScreenDozeCount / 1000.0));
4301 sb.append(" mAh");
4302 pw.println(sb.toString());
4303 }
4304
4305 final long dischargeScreenOnCount =
4306 dischargeCount - dischargeScreenOffCount - dischargeScreenDozeCount;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004307 if (dischargeScreenOnCount >= 0) {
4308 sb.setLength(0);
4309 sb.append(prefix);
4310 sb.append(" Screen on discharge: ");
4311 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
4312 sb.append(" mAh");
4313 pw.println(sb.toString());
4314 }
4315
Mike Ma15313c92017-11-15 17:58:21 -08004316 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
4317 if (dischargeLightDozeCount >= 0) {
4318 sb.setLength(0);
4319 sb.append(prefix);
4320 sb.append(" Device light doze discharge: ");
4321 sb.append(BatteryStatsHelper.makemAh(dischargeLightDozeCount / 1000.0));
4322 sb.append(" mAh");
4323 pw.println(sb.toString());
4324 }
4325
4326 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
4327 if (dischargeDeepDozeCount >= 0) {
4328 sb.setLength(0);
4329 sb.append(prefix);
4330 sb.append(" Device deep doze discharge: ");
4331 sb.append(BatteryStatsHelper.makemAh(dischargeDeepDozeCount / 1000.0));
4332 sb.append(" mAh");
4333 pw.println(sb.toString());
4334 }
4335
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004336 pw.print(" Start clock time: ");
4337 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
4338
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004339 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004340 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004341 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004342 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
4343 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004344 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004345 rawRealtime, which);
4346 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
4347 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004348 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004349 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004350 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
4351 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
4352 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004353 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004354 sb.append(prefix);
4355 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
4356 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004357 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004358 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
4359 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004360 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004361 pw.println(sb.toString());
4362 sb.setLength(0);
4363 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004364 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004365 boolean didOne = false;
4366 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004367 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004368 if (time == 0) {
4369 continue;
4370 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004371 sb.append("\n ");
4372 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004373 didOne = true;
4374 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
4375 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004376 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004377 sb.append("(");
4378 sb.append(formatRatioLocked(time, screenOnTime));
4379 sb.append(")");
4380 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004381 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004382 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004383 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004384 sb.setLength(0);
4385 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004386 sb.append(" Power save mode enabled: ");
4387 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004388 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004389 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004390 sb.append(")");
4391 pw.println(sb.toString());
4392 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004393 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004394 sb.setLength(0);
4395 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004396 sb.append(" Device light idling: ");
4397 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004398 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004399 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
4400 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004401 sb.append("x");
4402 pw.println(sb.toString());
4403 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004404 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004405 sb.setLength(0);
4406 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004407 sb.append(" Idle mode light time: ");
4408 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004409 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004410 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
4411 sb.append(") ");
4412 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004413 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004414 sb.append(" -- longest ");
4415 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
4416 pw.println(sb.toString());
4417 }
4418 if (deviceIdlingTime != 0) {
4419 sb.setLength(0);
4420 sb.append(prefix);
4421 sb.append(" Device full idling: ");
4422 formatTimeMs(sb, deviceIdlingTime / 1000);
4423 sb.append("(");
4424 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004425 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004426 sb.append("x");
4427 pw.println(sb.toString());
4428 }
4429 if (deviceIdleModeFullTime != 0) {
4430 sb.setLength(0);
4431 sb.append(prefix);
4432 sb.append(" Idle mode full time: ");
4433 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
4434 sb.append("(");
4435 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
4436 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004437 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004438 sb.append("x");
4439 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004440 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004441 pw.println(sb.toString());
4442 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004443 if (phoneOnTime != 0) {
4444 sb.setLength(0);
4445 sb.append(prefix);
4446 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
4447 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004448 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004449 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004450 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004451 if (connChanges != 0) {
4452 pw.print(prefix);
4453 pw.print(" Connectivity changes: "); pw.println(connChanges);
4454 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004455
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004456 // Calculate both wakelock and wifi multicast wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07004457 long fullWakeLockTimeTotalMicros = 0;
4458 long partialWakeLockTimeTotalMicros = 0;
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004459 long multicastWakeLockTimeTotalMicros = 0;
4460 int multicastWakeLockCountTotal = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08004461
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004462 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08004463
Evan Millar22ac0432009-03-31 11:33:18 -07004464 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004465 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004466
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004467 // First calculate wakelock statistics
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004468 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4469 = u.getWakelockStats();
4470 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4471 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07004472
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004473 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
4474 if (fullWakeTimer != null) {
4475 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
4476 rawRealtime, which);
4477 }
4478
4479 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4480 if (partialWakeTimer != null) {
4481 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
4482 rawRealtime, which);
4483 if (totalTimeMicros > 0) {
4484 if (reqUid < 0) {
4485 // Only show the ordered list of all wake
4486 // locks if the caller is not asking for data
4487 // about a specific uid.
4488 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
4489 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08004490 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004491 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07004492 }
4493 }
4494 }
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004495
4496 // Next calculate wifi multicast wakelock statistics
4497 final Timer mcTimer = u.getMulticastWakelockStats();
4498 if (mcTimer != null) {
4499 multicastWakeLockTimeTotalMicros += mcTimer.getTotalTimeLocked(rawRealtime, which);
4500 multicastWakeLockCountTotal += mcTimer.getCountLocked(which);
4501 }
Evan Millar22ac0432009-03-31 11:33:18 -07004502 }
Bookatzc8c44962017-05-11 12:12:54 -07004503
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004504 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4505 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4506 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4507 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4508 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4509 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4510 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4511 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004512 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4513 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004514
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004515 if (fullWakeLockTimeTotalMicros != 0) {
4516 sb.setLength(0);
4517 sb.append(prefix);
4518 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4519 (fullWakeLockTimeTotalMicros + 500) / 1000);
4520 pw.println(sb.toString());
4521 }
4522
4523 if (partialWakeLockTimeTotalMicros != 0) {
4524 sb.setLength(0);
4525 sb.append(prefix);
4526 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4527 (partialWakeLockTimeTotalMicros + 500) / 1000);
4528 pw.println(sb.toString());
4529 }
4530
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004531 if (multicastWakeLockTimeTotalMicros != 0) {
4532 sb.setLength(0);
4533 sb.append(prefix);
4534 sb.append(" Total WiFi Multicast wakelock Count: ");
4535 sb.append(multicastWakeLockCountTotal);
4536 pw.println(sb.toString());
4537
4538 sb.setLength(0);
4539 sb.append(prefix);
4540 sb.append(" Total WiFi Multicast wakelock time: ");
4541 formatTimeMsNoSpace(sb, (multicastWakeLockTimeTotalMicros + 500) / 1000);
4542 pw.println(sb.toString());
4543 }
4544
Siddharth Ray3c648c42017-10-02 17:30:58 -07004545 pw.println("");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004546 pw.print(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004547 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004548 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004549 sb.append(" CONNECTIVITY POWER SUMMARY START");
4550 pw.println(sb.toString());
4551
4552 pw.print(prefix);
4553 sb.setLength(0);
4554 sb.append(prefix);
4555 sb.append(" Logging duration for connectivity statistics: ");
4556 formatTimeMs(sb, whichBatteryRealtime / 1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004557 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004558
4559 sb.setLength(0);
4560 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004561 sb.append(" Cellular Statistics:");
Amith Yamasanif37447b2009-10-08 18:28:01 -07004562 pw.println(sb.toString());
4563
Siddharth Ray3c648c42017-10-02 17:30:58 -07004564 pw.print(prefix);
4565 sb.setLength(0);
4566 sb.append(prefix);
4567 sb.append(" Cellular kernel active time: ");
4568 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
4569 formatTimeMs(sb, mobileActiveTime / 1000);
4570 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4571 sb.append(")");
4572 pw.println(sb.toString());
4573
4574 pw.print(" Cellular data received: "); pw.println(formatBytesLocked(mobileRxTotalBytes));
4575 pw.print(" Cellular data sent: "); pw.println(formatBytesLocked(mobileTxTotalBytes));
4576 pw.print(" Cellular packets received: "); pw.println(mobileRxTotalPackets);
4577 pw.print(" Cellular packets sent: "); pw.println(mobileTxTotalPackets);
4578
Dianne Hackborn627bba72009-03-24 22:32:56 -07004579 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004580 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004581 sb.append(" Cellular Radio Access Technology:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004582 didOne = false;
4583 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004584 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004585 if (time == 0) {
4586 continue;
4587 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004588 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004589 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004590 didOne = true;
4591 sb.append(DATA_CONNECTION_NAMES[i]);
4592 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004593 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004594 sb.append("(");
4595 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004596 sb.append(") ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004597 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004598 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004599 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004600
4601 sb.setLength(0);
4602 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004603 sb.append(" Cellular Rx signal strength (RSRP):");
4604 final String[] cellularRxSignalStrengthDescription = new String[]{
4605 "very poor (less than -128dBm): ",
4606 "poor (-128dBm to -118dBm): ",
4607 "moderate (-118dBm to -108dBm): ",
4608 "good (-108dBm to -98dBm): ",
4609 "great (greater than -98dBm): "};
4610 didOne = false;
4611 final int numCellularRxBins = Math.min(SignalStrength.NUM_SIGNAL_STRENGTH_BINS,
4612 cellularRxSignalStrengthDescription.length);
4613 for (int i=0; i<numCellularRxBins; i++) {
4614 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
4615 if (time == 0) {
4616 continue;
4617 }
4618 sb.append("\n ");
4619 sb.append(prefix);
4620 didOne = true;
4621 sb.append(cellularRxSignalStrengthDescription[i]);
4622 sb.append(" ");
4623 formatTimeMs(sb, time/1000);
4624 sb.append("(");
4625 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4626 sb.append(") ");
4627 }
4628 if (!didOne) sb.append(" (no activity)");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004629 pw.println(sb.toString());
4630
Siddharth Ray3c648c42017-10-02 17:30:58 -07004631 printControllerActivity(pw, sb, prefix, "Cellular",
4632 getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004633
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004634 pw.print(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004635 sb.setLength(0);
4636 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004637 sb.append(" Wifi Statistics:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004638 pw.println(sb.toString());
4639
Siddharth Ray3c648c42017-10-02 17:30:58 -07004640 pw.print(" Wifi data received: "); pw.println(formatBytesLocked(wifiRxTotalBytes));
4641 pw.print(" Wifi data sent: "); pw.println(formatBytesLocked(wifiTxTotalBytes));
4642 pw.print(" Wifi packets received: "); pw.println(wifiRxTotalPackets);
4643 pw.print(" Wifi packets sent: "); pw.println(wifiTxTotalPackets);
4644
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004645 sb.setLength(0);
4646 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004647 sb.append(" Wifi states:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004648 didOne = false;
4649 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004650 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004651 if (time == 0) {
4652 continue;
4653 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004654 sb.append("\n ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004655 didOne = true;
4656 sb.append(WIFI_STATE_NAMES[i]);
4657 sb.append(" ");
4658 formatTimeMs(sb, time/1000);
4659 sb.append("(");
4660 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4661 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004662 }
4663 if (!didOne) sb.append(" (no activity)");
4664 pw.println(sb.toString());
4665
4666 sb.setLength(0);
4667 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004668 sb.append(" Wifi supplicant states:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004669 didOne = false;
4670 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4671 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4672 if (time == 0) {
4673 continue;
4674 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004675 sb.append("\n ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004676 didOne = true;
4677 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4678 sb.append(" ");
4679 formatTimeMs(sb, time/1000);
4680 sb.append("(");
4681 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4682 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004683 }
4684 if (!didOne) sb.append(" (no activity)");
4685 pw.println(sb.toString());
4686
4687 sb.setLength(0);
4688 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004689 sb.append(" Wifi Rx signal strength (RSSI):");
4690 final String[] wifiRxSignalStrengthDescription = new String[]{
4691 "very poor (less than -88.75dBm): ",
4692 "poor (-88.75 to -77.5dBm): ",
4693 "moderate (-77.5dBm to -66.25dBm): ",
4694 "good (-66.25dBm to -55dBm): ",
4695 "great (greater than -55dBm): "};
Dianne Hackborn3251b902014-06-20 14:40:53 -07004696 didOne = false;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004697 final int numWifiRxBins = Math.min(NUM_WIFI_SIGNAL_STRENGTH_BINS,
4698 wifiRxSignalStrengthDescription.length);
4699 for (int i=0; i<numWifiRxBins; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004700 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4701 if (time == 0) {
4702 continue;
4703 }
4704 sb.append("\n ");
4705 sb.append(prefix);
4706 didOne = true;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004707 sb.append(" ");
4708 sb.append(wifiRxSignalStrengthDescription[i]);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004709 formatTimeMs(sb, time/1000);
4710 sb.append("(");
4711 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4712 sb.append(") ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004713 }
4714 if (!didOne) sb.append(" (no activity)");
4715 pw.println(sb.toString());
4716
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004717 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004718
Adam Lesinski50e47602015-12-04 17:04:54 -08004719 pw.print(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004720 sb.setLength(0);
4721 sb.append(prefix);
4722 sb.append(" CONNECTIVITY POWER SUMMARY END");
4723 pw.println(sb.toString());
4724 pw.println("");
4725
4726 pw.print(prefix);
Adam Lesinski50e47602015-12-04 17:04:54 -08004727 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4728 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4729
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004730 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4731 sb.setLength(0);
4732 sb.append(prefix);
4733 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4734 pw.println(sb.toString());
4735
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004736 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4737 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004738
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004739 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004740
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004741 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004742 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004743 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004744 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004745 pw.println(getDischargeStartLevel());
4746 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4747 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004748 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004749 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004750 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004751 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004752 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004753 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004754 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004755 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004756 pw.println(getDischargeAmountScreenOn());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004757 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004758 pw.println(getDischargeAmountScreenOff());
4759 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4760 pw.println(getDischargeAmountScreenDoze());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004761 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004762 } else {
4763 pw.print(prefix); pw.println(" Device battery use since last full charge");
4764 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004765 pw.println(getLowDischargeAmountSinceCharge());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004766 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004767 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004768 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004769 pw.println(getDischargeAmountScreenOnSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004770 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004771 pw.println(getDischargeAmountScreenOffSinceCharge());
4772 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4773 pw.println(getDischargeAmountScreenDozeSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004774 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004775 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004776
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004777 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004778 helper.create(this);
4779 helper.refreshStats(which, UserHandle.USER_ALL);
4780 List<BatterySipper> sippers = helper.getUsageList();
4781 if (sippers != null && sippers.size() > 0) {
4782 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4783 pw.print(prefix); pw.print(" Capacity: ");
4784 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004785 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004786 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4787 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4788 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4789 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004790 pw.println();
4791 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004792 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004793 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004794 switch (bs.drainType) {
4795 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004796 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004797 break;
4798 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004799 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004800 break;
4801 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004802 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004803 break;
4804 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004805 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004806 break;
4807 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004808 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004809 break;
4810 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004811 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004812 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004813 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004814 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004815 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004816 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004817 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004818 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004819 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004820 break;
4821 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004822 pw.print(" User "); pw.print(bs.userId);
4823 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004824 break;
4825 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004826 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004827 break;
4828 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004829 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004830 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004831 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004832 pw.print(" Camera: ");
4833 break;
4834 default:
4835 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004836 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004837 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004838 printmAh(pw, bs.totalPowerMah);
4839
Adam Lesinski57123002015-06-12 16:12:07 -07004840 if (bs.usagePowerMah != bs.totalPowerMah) {
4841 // If the usage (generic power) isn't the whole amount, we list out
4842 // what components are involved in the calculation.
4843
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004844 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004845 if (bs.usagePowerMah != 0) {
4846 pw.print(" usage=");
4847 printmAh(pw, bs.usagePowerMah);
4848 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004849 if (bs.cpuPowerMah != 0) {
4850 pw.print(" cpu=");
4851 printmAh(pw, bs.cpuPowerMah);
4852 }
4853 if (bs.wakeLockPowerMah != 0) {
4854 pw.print(" wake=");
4855 printmAh(pw, bs.wakeLockPowerMah);
4856 }
4857 if (bs.mobileRadioPowerMah != 0) {
4858 pw.print(" radio=");
4859 printmAh(pw, bs.mobileRadioPowerMah);
4860 }
4861 if (bs.wifiPowerMah != 0) {
4862 pw.print(" wifi=");
4863 printmAh(pw, bs.wifiPowerMah);
4864 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004865 if (bs.bluetoothPowerMah != 0) {
4866 pw.print(" bt=");
4867 printmAh(pw, bs.bluetoothPowerMah);
4868 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004869 if (bs.gpsPowerMah != 0) {
4870 pw.print(" gps=");
4871 printmAh(pw, bs.gpsPowerMah);
4872 }
4873 if (bs.sensorPowerMah != 0) {
4874 pw.print(" sensor=");
4875 printmAh(pw, bs.sensorPowerMah);
4876 }
4877 if (bs.cameraPowerMah != 0) {
4878 pw.print(" camera=");
4879 printmAh(pw, bs.cameraPowerMah);
4880 }
4881 if (bs.flashlightPowerMah != 0) {
4882 pw.print(" flash=");
4883 printmAh(pw, bs.flashlightPowerMah);
4884 }
4885 pw.print(" )");
4886 }
Bookatz17d7d9d2017-06-08 14:50:46 -07004887
4888 // If there is additional smearing information, include it.
4889 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
4890 pw.print(" Including smearing: ");
4891 printmAh(pw, bs.totalSmearedPowerMah);
4892 pw.print(" (");
4893 if (bs.screenPowerMah != 0) {
4894 pw.print(" screen=");
4895 printmAh(pw, bs.screenPowerMah);
4896 }
4897 if (bs.proportionalSmearMah != 0) {
4898 pw.print(" proportional=");
4899 printmAh(pw, bs.proportionalSmearMah);
4900 }
4901 pw.print(" )");
4902 }
4903 if (bs.shouldHide) {
4904 pw.print(" Excluded from smearing");
4905 }
4906
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004907 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004908 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004909 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004910 }
4911
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004912 sippers = helper.getMobilemsppList();
4913 if (sippers != null && sippers.size() > 0) {
4914 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004915 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004916 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004917 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004918 sb.setLength(0);
4919 sb.append(prefix); sb.append(" Uid ");
4920 UserHandle.formatUid(sb, bs.uidObj.getUid());
4921 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4922 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4923 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004924 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004925 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004926 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004927 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004928 sb.setLength(0);
4929 sb.append(prefix);
4930 sb.append(" TOTAL TIME: ");
4931 formatTimeMs(sb, totalTime);
4932 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4933 sb.append(")");
4934 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004935 pw.println();
4936 }
4937
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004938 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4939 @Override
4940 public int compare(TimerEntry lhs, TimerEntry rhs) {
4941 long lhsTime = lhs.mTime;
4942 long rhsTime = rhs.mTime;
4943 if (lhsTime < rhsTime) {
4944 return 1;
4945 }
4946 if (lhsTime > rhsTime) {
4947 return -1;
4948 }
4949 return 0;
4950 }
4951 };
4952
4953 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004954 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4955 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004956 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004957 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4958 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4959 : kernelWakelocks.entrySet()) {
4960 final BatteryStats.Timer timer = ent.getValue();
4961 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004962 if (totalTimeMillis > 0) {
4963 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4964 }
4965 }
4966 if (ktimers.size() > 0) {
4967 Collections.sort(ktimers, timerComparator);
4968 pw.print(prefix); pw.println(" All kernel wake locks:");
4969 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004970 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004971 String linePrefix = ": ";
4972 sb.setLength(0);
4973 sb.append(prefix);
4974 sb.append(" Kernel Wake lock ");
4975 sb.append(timer.mName);
4976 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4977 which, linePrefix);
4978 if (!linePrefix.equals(": ")) {
4979 sb.append(" realtime");
4980 // Only print out wake locks that were held
4981 pw.println(sb.toString());
4982 }
4983 }
4984 pw.println();
4985 }
4986 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004987
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004988 if (timers.size() > 0) {
4989 Collections.sort(timers, timerComparator);
4990 pw.print(prefix); pw.println(" All partial wake locks:");
4991 for (int i=0; i<timers.size(); i++) {
4992 TimerEntry timer = timers.get(i);
4993 sb.setLength(0);
4994 sb.append(" Wake lock ");
4995 UserHandle.formatUid(sb, timer.mId);
4996 sb.append(" ");
4997 sb.append(timer.mName);
4998 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4999 sb.append(" realtime");
5000 pw.println(sb.toString());
5001 }
5002 timers.clear();
5003 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08005004 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005005
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005006 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005007 if (wakeupReasons.size() > 0) {
5008 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005009 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005010 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005011 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005012 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
5013 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005014 }
5015 Collections.sort(reasons, timerComparator);
5016 for (int i=0; i<reasons.size(); i++) {
5017 TimerEntry timer = reasons.get(i);
5018 String linePrefix = ": ";
5019 sb.setLength(0);
5020 sb.append(prefix);
5021 sb.append(" Wakeup reason ");
5022 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005023 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5024 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005025 pw.println(sb.toString());
5026 }
5027 pw.println();
5028 }
Dianne Hackborn81038902012-11-26 17:04:09 -08005029 }
Evan Millar22ac0432009-03-31 11:33:18 -07005030
James Carr2dd7e5e2016-07-20 18:48:39 -07005031 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
Bookatz50df7112017-08-04 14:53:26 -07005032 if (mMemoryStats.size() > 0) {
5033 pw.println(" Memory Stats");
5034 for (int i = 0; i < mMemoryStats.size(); i++) {
5035 sb.setLength(0);
5036 sb.append(" Bandwidth ");
5037 sb.append(mMemoryStats.keyAt(i));
5038 sb.append(" Time ");
5039 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
5040 pw.println(sb.toString());
5041 }
5042 pw.println();
5043 }
5044
5045 final Map<String, ? extends Timer> rpmStats = getRpmStats();
5046 if (rpmStats.size() > 0) {
5047 pw.print(prefix); pw.println(" Resource Power Manager Stats");
5048 if (rpmStats.size() > 0) {
5049 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
5050 final String timerName = ent.getKey();
5051 final Timer timer = ent.getValue();
5052 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5053 }
5054 }
5055 pw.println();
5056 }
Bookatz82b341172017-09-07 19:06:08 -07005057 if (SCREEN_OFF_RPM_STATS_ENABLED) {
5058 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
Bookatz50df7112017-08-04 14:53:26 -07005059 if (screenOffRpmStats.size() > 0) {
Bookatz82b341172017-09-07 19:06:08 -07005060 pw.print(prefix);
5061 pw.println(" Resource Power Manager Stats for when screen was off");
5062 if (screenOffRpmStats.size() > 0) {
5063 for (Map.Entry<String, ? extends Timer> ent : screenOffRpmStats.entrySet()) {
5064 final String timerName = ent.getKey();
5065 final Timer timer = ent.getValue();
5066 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5067 }
Bookatz50df7112017-08-04 14:53:26 -07005068 }
Bookatz82b341172017-09-07 19:06:08 -07005069 pw.println();
Bookatz50df7112017-08-04 14:53:26 -07005070 }
James Carr2dd7e5e2016-07-20 18:48:39 -07005071 }
5072
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005073 final long[] cpuFreqs = getCpuFreqs();
5074 if (cpuFreqs != null) {
5075 sb.setLength(0);
Bookatz50df7112017-08-04 14:53:26 -07005076 sb.append(" CPU freqs:");
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005077 for (int i = 0; i < cpuFreqs.length; ++i) {
5078 sb.append(" " + cpuFreqs[i]);
5079 }
5080 pw.println(sb.toString());
Bookatz50df7112017-08-04 14:53:26 -07005081 pw.println();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005082 }
5083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005084 for (int iu=0; iu<NU; iu++) {
5085 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005086 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005087 continue;
5088 }
Bookatzc8c44962017-05-11 12:12:54 -07005089
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005090 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07005091
5092 pw.print(prefix);
5093 pw.print(" ");
5094 UserHandle.formatUid(pw, uid);
5095 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005096 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005097
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005098 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
5099 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
5100 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
5101 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005102 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
5103 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
5104
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005105 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
5106 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005107 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
5108 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005109
5110 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
5111 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
5112
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005113 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
5114 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
5115 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08005116 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
5117 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5118 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
5119 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005120 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005121
Adam Lesinski5f056f62016-07-14 16:56:08 -07005122 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
5123 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
5124
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005125 if (mobileRxBytes > 0 || mobileTxBytes > 0
5126 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005127 pw.print(prefix); pw.print(" Mobile network: ");
5128 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005129 pw.print(formatBytesLocked(mobileTxBytes));
5130 pw.print(" sent (packets "); pw.print(mobileRxPackets);
5131 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005132 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005133 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
5134 sb.setLength(0);
5135 sb.append(prefix); sb.append(" Mobile radio active: ");
5136 formatTimeMs(sb, uidMobileActiveTime / 1000);
5137 sb.append("(");
5138 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
5139 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
5140 long packets = mobileRxPackets + mobileTxPackets;
5141 if (packets == 0) {
5142 packets = 1;
5143 }
5144 sb.append(" @ ");
5145 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
5146 sb.append(" mspp");
5147 pw.println(sb.toString());
5148 }
5149
Adam Lesinski5f056f62016-07-14 16:56:08 -07005150 if (mobileWakeup > 0) {
5151 sb.setLength(0);
5152 sb.append(prefix);
5153 sb.append(" Mobile radio AP wakeups: ");
5154 sb.append(mobileWakeup);
5155 pw.println(sb.toString());
5156 }
5157
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005158 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
5159 u.getModemControllerActivity(), which);
5160
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005161 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005162 pw.print(prefix); pw.print(" Wi-Fi network: ");
5163 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005164 pw.print(formatBytesLocked(wifiTxBytes));
5165 pw.print(" sent (packets "); pw.print(wifiRxPackets);
5166 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005167 }
5168
Dianne Hackborn62793e42015-03-09 11:15:41 -07005169 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08005170 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005171 || uidWifiRunningTime != 0) {
5172 sb.setLength(0);
5173 sb.append(prefix); sb.append(" Wifi Running: ");
5174 formatTimeMs(sb, uidWifiRunningTime / 1000);
5175 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
5176 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07005177 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005178 formatTimeMs(sb, fullWifiLockOnTime / 1000);
5179 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
5180 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08005181 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005182 formatTimeMs(sb, wifiScanTime / 1000);
5183 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07005184 whichBatteryRealtime)); sb.append(") ");
5185 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08005186 sb.append("x\n");
5187 // actual and background times are unpooled and since reset (regardless of 'which')
5188 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
5189 formatTimeMs(sb, wifiScanActualTime / 1000);
5190 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
5191 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5192 sb.append(") ");
5193 sb.append(wifiScanCount);
5194 sb.append("x\n");
5195 sb.append(prefix); sb.append(" Background Wifi Scan: ");
5196 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
5197 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
5198 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5199 sb.append(") ");
5200 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07005201 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005202 pw.println(sb.toString());
5203 }
5204
Adam Lesinski5f056f62016-07-14 16:56:08 -07005205 if (wifiWakeup > 0) {
5206 sb.setLength(0);
5207 sb.append(prefix);
5208 sb.append(" WiFi AP wakeups: ");
5209 sb.append(wifiWakeup);
5210 pw.println(sb.toString());
5211 }
5212
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005213 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
5214 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07005215
Adam Lesinski50e47602015-12-04 17:04:54 -08005216 if (btRxBytes > 0 || btTxBytes > 0) {
5217 pw.print(prefix); pw.print(" Bluetooth network: ");
5218 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
5219 pw.print(formatBytesLocked(btTxBytes));
5220 pw.println(" sent");
5221 }
5222
Bookatz867c0d72017-03-07 18:23:42 -08005223 final Timer bleTimer = u.getBluetoothScanTimer();
5224 if (bleTimer != null) {
5225 // Convert from microseconds to milliseconds with rounding
5226 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
5227 / 1000;
5228 if (totalTimeMs != 0) {
5229 final int count = bleTimer.getCountLocked(which);
5230 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
5231 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005232 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5233 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
5234 final long actualTimeMsBg = bleTimerBg != null ?
5235 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005236 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07005237 final int resultCount = u.getBluetoothScanResultCounter() != null ?
5238 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005239 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
5240 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
5241 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
5242 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
5243 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
5244 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5245 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
5246 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
5247 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
5248 final Timer unoptimizedScanTimerBg =
5249 u.getBluetoothUnoptimizedScanBackgroundTimer();
5250 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
5251 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5252 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
5253 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005254
5255 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08005256 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07005257 sb.append(prefix);
5258 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08005259 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07005260 sb.append(" (");
5261 sb.append(count);
5262 sb.append(" times)");
5263 if (bleTimer.isRunningLocked()) {
5264 sb.append(" (currently running)");
5265 }
5266 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005267 }
Bookatzb1f04f32017-05-19 13:57:32 -07005268
5269 sb.append(prefix);
5270 sb.append(" Bluetooth Scan (total actual realtime): ");
5271 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
5272 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005273 sb.append(count);
5274 sb.append(" times)");
5275 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07005276 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08005277 }
Bookatzb1f04f32017-05-19 13:57:32 -07005278 sb.append("\n");
5279 if (actualTimeMsBg > 0 || countBg > 0) {
5280 sb.append(prefix);
5281 sb.append(" Bluetooth Scan (background realtime): ");
5282 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
5283 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005284 sb.append(countBg);
5285 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07005286 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
5287 sb.append(" (currently running in background)");
5288 }
5289 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005290 }
Bookatzb1f04f32017-05-19 13:57:32 -07005291
5292 sb.append(prefix);
5293 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07005294 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07005295 sb.append(" (");
5296 sb.append(resultCountBg);
5297 sb.append(" in background)");
5298
5299 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
5300 sb.append("\n");
5301 sb.append(prefix);
5302 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
5303 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
5304 sb.append(" (max ");
5305 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
5306 sb.append(")");
5307 if (unoptimizedScanTimer != null
5308 && unoptimizedScanTimer.isRunningLocked()) {
5309 sb.append(" (currently running unoptimized)");
5310 }
5311 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
5312 sb.append("\n");
5313 sb.append(prefix);
5314 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
5315 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
5316 sb.append(" (max ");
5317 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
5318 sb.append(")");
5319 if (unoptimizedScanTimerBg.isRunningLocked()) {
5320 sb.append(" (currently running unoptimized in background)");
5321 }
5322 }
5323 }
Bookatz867c0d72017-03-07 18:23:42 -08005324 pw.println(sb.toString());
5325 uidActivity = true;
5326 }
5327 }
5328
5329
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005330
Dianne Hackborn617f8772009-03-31 15:04:46 -07005331 if (u.hasUserActivity()) {
5332 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07005333 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005334 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005335 if (val != 0) {
5336 if (!hasData) {
5337 sb.setLength(0);
5338 sb.append(" User activity: ");
5339 hasData = true;
5340 } else {
5341 sb.append(", ");
5342 }
5343 sb.append(val);
5344 sb.append(" ");
5345 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
5346 }
5347 }
5348 if (hasData) {
5349 pw.println(sb.toString());
5350 }
5351 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005352
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005353 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
5354 = u.getWakelockStats();
5355 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005356 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005357 int countWakelock = 0;
5358 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
5359 final Uid.Wakelock wl = wakelocks.valueAt(iw);
5360 String linePrefix = ": ";
5361 sb.setLength(0);
5362 sb.append(prefix);
5363 sb.append(" Wake lock ");
5364 sb.append(wakelocks.keyAt(iw));
5365 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
5366 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005367 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
5368 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005369 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005370 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
5371 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005372 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
5373 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005374 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
5375 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07005376 sb.append(" realtime");
5377 pw.println(sb.toString());
5378 uidActivity = true;
5379 countWakelock++;
5380
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005381 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
5382 rawRealtime, which);
5383 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
5384 rawRealtime, which);
5385 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
5386 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005387 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07005388 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005389 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005390 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07005391 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
5392 // pooled and therefore just a lower bound)
5393 long actualTotalPartialWakelock = 0;
5394 long actualBgPartialWakelock = 0;
5395 if (u.getAggregatedPartialWakelockTimer() != null) {
5396 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
5397 // Convert from microseconds to milliseconds with rounding
5398 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07005399 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07005400 final Timer bgAggTimer = aggTimer.getSubTimer();
5401 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005402 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07005403 }
5404
5405 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
5406 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
5407 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005408 sb.setLength(0);
5409 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005410 sb.append(" TOTAL wake: ");
5411 boolean needComma = false;
5412 if (totalFullWakelock != 0) {
5413 needComma = true;
5414 formatTimeMs(sb, totalFullWakelock);
5415 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005416 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005417 if (totalPartialWakelock != 0) {
5418 if (needComma) {
5419 sb.append(", ");
5420 }
5421 needComma = true;
5422 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07005423 sb.append("blamed partial");
5424 }
5425 if (actualTotalPartialWakelock != 0) {
5426 if (needComma) {
5427 sb.append(", ");
5428 }
5429 needComma = true;
5430 formatTimeMs(sb, actualTotalPartialWakelock);
5431 sb.append("actual partial");
5432 }
5433 if (actualBgPartialWakelock != 0) {
5434 if (needComma) {
5435 sb.append(", ");
5436 }
5437 needComma = true;
5438 formatTimeMs(sb, actualBgPartialWakelock);
5439 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005440 }
5441 if (totalWindowWakelock != 0) {
5442 if (needComma) {
5443 sb.append(", ");
5444 }
5445 needComma = true;
5446 formatTimeMs(sb, totalWindowWakelock);
5447 sb.append("window");
5448 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005449 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07005450 if (needComma) {
5451 sb.append(",");
5452 }
5453 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005454 formatTimeMs(sb, totalDrawWakelock);
5455 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07005456 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005457 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005458 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005459 }
5460 }
5461
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07005462 // Calculate multicast wakelock stats
5463 final Timer mcTimer = u.getMulticastWakelockStats();
5464 if (mcTimer != null) {
5465 final long multicastWakeLockTimeMicros = mcTimer.getTotalTimeLocked(rawRealtime, which);
5466 final int multicastWakeLockCount = mcTimer.getCountLocked(which);
5467
5468 if (multicastWakeLockTimeMicros > 0) {
5469 sb.setLength(0);
5470 sb.append(prefix);
5471 sb.append(" WiFi Multicast Wakelock");
5472 sb.append(" count = ");
5473 sb.append(multicastWakeLockCount);
5474 sb.append(" time = ");
5475 formatTimeMsNoSpace(sb, (multicastWakeLockTimeMicros + 500) / 1000);
5476 pw.println(sb.toString());
5477 }
5478 }
5479
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005480 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
5481 for (int isy=syncs.size()-1; isy>=0; isy--) {
5482 final Timer timer = syncs.valueAt(isy);
5483 // Convert from microseconds to milliseconds with rounding
5484 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5485 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07005486 final Timer bgTimer = timer.getSubTimer();
5487 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005488 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07005489 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005490 sb.setLength(0);
5491 sb.append(prefix);
5492 sb.append(" Sync ");
5493 sb.append(syncs.keyAt(isy));
5494 sb.append(": ");
5495 if (totalTime != 0) {
5496 formatTimeMs(sb, totalTime);
5497 sb.append("realtime (");
5498 sb.append(count);
5499 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07005500 if (bgTime > 0) {
5501 sb.append(", ");
5502 formatTimeMs(sb, bgTime);
5503 sb.append("background (");
5504 sb.append(bgCount);
5505 sb.append(" times)");
5506 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005507 } else {
5508 sb.append("(not used)");
5509 }
5510 pw.println(sb.toString());
5511 uidActivity = true;
5512 }
5513
5514 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
5515 for (int ij=jobs.size()-1; ij>=0; ij--) {
5516 final Timer timer = jobs.valueAt(ij);
5517 // Convert from microseconds to milliseconds with rounding
5518 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5519 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07005520 final Timer bgTimer = timer.getSubTimer();
5521 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005522 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07005523 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005524 sb.setLength(0);
5525 sb.append(prefix);
5526 sb.append(" Job ");
5527 sb.append(jobs.keyAt(ij));
5528 sb.append(": ");
5529 if (totalTime != 0) {
5530 formatTimeMs(sb, totalTime);
5531 sb.append("realtime (");
5532 sb.append(count);
5533 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07005534 if (bgTime > 0) {
5535 sb.append(", ");
5536 formatTimeMs(sb, bgTime);
5537 sb.append("background (");
5538 sb.append(bgCount);
5539 sb.append(" times)");
5540 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005541 } else {
5542 sb.append("(not used)");
5543 }
5544 pw.println(sb.toString());
5545 uidActivity = true;
5546 }
5547
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005548 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
5549 for (int ic=completions.size()-1; ic>=0; ic--) {
5550 SparseIntArray types = completions.valueAt(ic);
5551 if (types != null) {
5552 pw.print(prefix);
5553 pw.print(" Job Completions ");
5554 pw.print(completions.keyAt(ic));
5555 pw.print(":");
5556 for (int it=0; it<types.size(); it++) {
5557 pw.print(" ");
5558 pw.print(JobParameters.getReasonName(types.keyAt(it)));
5559 pw.print("(");
5560 pw.print(types.valueAt(it));
5561 pw.print("x)");
5562 }
5563 pw.println();
5564 }
5565 }
5566
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005567 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
5568 prefix, "Flashlight");
5569 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
5570 prefix, "Camera");
5571 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
5572 prefix, "Video");
5573 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
5574 prefix, "Audio");
5575
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005576 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
5577 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005578 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005579 final Uid.Sensor se = sensors.valueAt(ise);
5580 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005581 sb.setLength(0);
5582 sb.append(prefix);
5583 sb.append(" Sensor ");
5584 int handle = se.getHandle();
5585 if (handle == Uid.Sensor.GPS) {
5586 sb.append("GPS");
5587 } else {
5588 sb.append(handle);
5589 }
5590 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005591
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005592 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005593 if (timer != null) {
5594 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005595 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5596 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005597 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005598 final Timer bgTimer = se.getSensorBackgroundTime();
5599 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005600 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5601 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5602 final long bgActualTime = bgTimer != null ?
5603 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5604
Dianne Hackborn61659e52014-07-09 16:13:01 -07005605 //timer.logState();
5606 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005607 if (actualTime != totalTime) {
5608 formatTimeMs(sb, totalTime);
5609 sb.append("blamed realtime, ");
5610 }
5611
5612 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005613 sb.append("realtime (");
5614 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005615 sb.append(" times)");
5616
5617 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005618 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005619 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5620 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005621 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005622 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005624 } else {
5625 sb.append("(not used)");
5626 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005627 } else {
5628 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005629 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005630
5631 pw.println(sb.toString());
5632 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005633 }
5634
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005635 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5636 "Vibrator");
5637 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5638 prefix, "Foreground activities");
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005639 uidActivity |= printTimer(pw, sb, u.getForegroundServiceTimer(), rawRealtime, which,
5640 prefix, "Foreground services");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005641
Dianne Hackborn61659e52014-07-09 16:13:01 -07005642 long totalStateTime = 0;
5643 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5644 long time = u.getProcessStateTime(ips, rawRealtime, which);
5645 if (time > 0) {
5646 totalStateTime += time;
5647 sb.setLength(0);
5648 sb.append(prefix);
5649 sb.append(" ");
5650 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5651 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005652 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005653 pw.println(sb.toString());
5654 uidActivity = true;
5655 }
5656 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005657 if (totalStateTime > 0) {
5658 sb.setLength(0);
5659 sb.append(prefix);
5660 sb.append(" Total running: ");
5661 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5662 pw.println(sb.toString());
5663 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005664
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005665 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5666 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005667 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005668 sb.setLength(0);
5669 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005670 sb.append(" Total cpu time: u=");
5671 formatTimeMs(sb, userCpuTimeUs / 1000);
5672 sb.append("s=");
5673 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005674 pw.println(sb.toString());
5675 }
5676
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005677 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5678 if (cpuFreqTimes != null) {
5679 sb.setLength(0);
5680 sb.append(" Total cpu time per freq:");
5681 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5682 sb.append(" " + cpuFreqTimes[i]);
5683 }
5684 pw.println(sb.toString());
5685 }
5686 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5687 if (screenOffCpuFreqTimes != null) {
5688 sb.setLength(0);
5689 sb.append(" Total screen-off cpu time per freq:");
5690 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5691 sb.append(" " + screenOffCpuFreqTimes[i]);
5692 }
5693 pw.println(sb.toString());
5694 }
5695
Sudheer Shankab2f83c12017-11-13 19:25:01 -08005696 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
5697 final long[] cpuTimes = u.getCpuFreqTimes(which, procState);
5698 if (cpuTimes != null) {
5699 sb.setLength(0);
5700 sb.append(" Cpu times per freq at state "
5701 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5702 for (int i = 0; i < cpuTimes.length; ++i) {
5703 sb.append(" " + cpuTimes[i]);
5704 }
5705 pw.println(sb.toString());
5706 }
5707
5708 final long[] screenOffCpuTimes = u.getScreenOffCpuFreqTimes(which, procState);
5709 if (screenOffCpuTimes != null) {
5710 sb.setLength(0);
5711 sb.append(" Screen-off cpu times per freq at state "
5712 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5713 for (int i = 0; i < screenOffCpuTimes.length; ++i) {
5714 sb.append(" " + screenOffCpuTimes[i]);
5715 }
5716 pw.println(sb.toString());
5717 }
5718 }
5719
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005720 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5721 = u.getProcessStats();
5722 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5723 final Uid.Proc ps = processStats.valueAt(ipr);
5724 long userTime;
5725 long systemTime;
5726 long foregroundTime;
5727 int starts;
5728 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005729
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005730 userTime = ps.getUserTime(which);
5731 systemTime = ps.getSystemTime(which);
5732 foregroundTime = ps.getForegroundTime(which);
5733 starts = ps.getStarts(which);
5734 final int numCrashes = ps.getNumCrashes(which);
5735 final int numAnrs = ps.getNumAnrs(which);
5736 numExcessive = which == STATS_SINCE_CHARGED
5737 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005738
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005739 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5740 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5741 sb.setLength(0);
5742 sb.append(prefix); sb.append(" Proc ");
5743 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5744 sb.append(prefix); sb.append(" CPU: ");
5745 formatTimeMs(sb, userTime); sb.append("usr + ");
5746 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5747 formatTimeMs(sb, foregroundTime); sb.append("fg");
5748 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5749 sb.append("\n"); sb.append(prefix); sb.append(" ");
5750 boolean hasOne = false;
5751 if (starts != 0) {
5752 hasOne = true;
5753 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005754 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005755 if (numCrashes != 0) {
5756 if (hasOne) {
5757 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005758 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005759 hasOne = true;
5760 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005761 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005762 if (numAnrs != 0) {
5763 if (hasOne) {
5764 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005765 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005766 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005767 }
5768 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005769 pw.println(sb.toString());
5770 for (int e=0; e<numExcessive; e++) {
5771 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5772 if (ew != null) {
5773 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07005774 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005775 pw.print("cpu");
5776 } else {
5777 pw.print("unknown");
5778 }
5779 pw.print(" use: ");
5780 TimeUtils.formatDuration(ew.usedTime, pw);
5781 pw.print(" over ");
5782 TimeUtils.formatDuration(ew.overTime, pw);
5783 if (ew.overTime != 0) {
5784 pw.print(" (");
5785 pw.print((ew.usedTime*100)/ew.overTime);
5786 pw.println("%)");
5787 }
5788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005789 }
5790 uidActivity = true;
5791 }
5792 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005793
5794 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5795 = u.getPackageStats();
5796 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
5797 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
5798 pw.println(":");
5799 boolean apkActivity = false;
5800 final Uid.Pkg ps = packageStats.valueAt(ipkg);
5801 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
5802 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
5803 pw.print(prefix); pw.print(" Wakeup alarm ");
5804 pw.print(alarms.keyAt(iwa)); pw.print(": ");
5805 pw.print(alarms.valueAt(iwa).getCountLocked(which));
5806 pw.println(" times");
5807 apkActivity = true;
5808 }
5809 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
5810 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
5811 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
5812 final long startTime = ss.getStartTime(batteryUptime, which);
5813 final int starts = ss.getStarts(which);
5814 final int launches = ss.getLaunches(which);
5815 if (startTime != 0 || starts != 0 || launches != 0) {
5816 sb.setLength(0);
5817 sb.append(prefix); sb.append(" Service ");
5818 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
5819 sb.append(prefix); sb.append(" Created for: ");
5820 formatTimeMs(sb, startTime / 1000);
5821 sb.append("uptime\n");
5822 sb.append(prefix); sb.append(" Starts: ");
5823 sb.append(starts);
5824 sb.append(", launches: "); sb.append(launches);
5825 pw.println(sb.toString());
5826 apkActivity = true;
5827 }
5828 }
5829 if (!apkActivity) {
5830 pw.print(prefix); pw.println(" (nothing executed)");
5831 }
5832 uidActivity = true;
5833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005834 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005835 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005836 }
5837 }
5838 }
5839
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005840 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005841 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005842 int diff = oldval ^ newval;
5843 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005844 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005845 for (int i=0; i<descriptions.length; i++) {
5846 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005847 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005848 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005849 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005850 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005851 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005852 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
5853 didWake = true;
5854 pw.print("=");
5855 if (longNames) {
5856 UserHandle.formatUid(pw, wakelockTag.uid);
5857 pw.print(":\"");
5858 pw.print(wakelockTag.string);
5859 pw.print("\"");
5860 } else {
5861 pw.print(wakelockTag.poolIdx);
5862 }
5863 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005864 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005865 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005866 pw.print("=");
5867 int val = (newval&bd.mask)>>bd.shift;
5868 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005869 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005870 } else {
5871 pw.print(val);
5872 }
5873 }
5874 }
5875 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005876 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07005877 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005878 if (longNames) {
5879 UserHandle.formatUid(pw, wakelockTag.uid);
5880 pw.print(":\"");
5881 pw.print(wakelockTag.string);
5882 pw.print("\"");
5883 } else {
5884 pw.print(wakelockTag.poolIdx);
5885 }
5886 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005887 }
Mike Mac2f518a2017-09-19 16:06:03 -07005888
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005889 public void prepareForDumpLocked() {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07005890 // We don't need to require subclasses implement this.
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005891 }
5892
5893 public static class HistoryPrinter {
5894 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005895 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005896 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005897 int oldStatus = -1;
5898 int oldHealth = -1;
5899 int oldPlug = -1;
5900 int oldTemp = -1;
5901 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005902 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005903 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005904
Dianne Hackborn3251b902014-06-20 14:40:53 -07005905 void reset() {
5906 oldState = oldState2 = 0;
5907 oldLevel = -1;
5908 oldStatus = -1;
5909 oldHealth = -1;
5910 oldPlug = -1;
5911 oldTemp = -1;
5912 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005913 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07005914 }
5915
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005916 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005917 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005918 if (!checkin) {
5919 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005920 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005921 pw.print(" (");
5922 pw.print(rec.numReadInts);
5923 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005924 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005925 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5926 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005927 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005928 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005929 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005930 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005931 }
5932 lastTime = rec.time;
5933 }
5934 if (rec.cmd == HistoryItem.CMD_START) {
5935 if (checkin) {
5936 pw.print(":");
5937 }
5938 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005939 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005940 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
5941 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005942 if (checkin) {
5943 pw.print(":");
5944 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005945 if (rec.cmd == HistoryItem.CMD_RESET) {
5946 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005947 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005948 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005949 pw.print("TIME:");
5950 if (checkin) {
5951 pw.println(rec.currentTime);
5952 } else {
5953 pw.print(" ");
5954 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5955 rec.currentTime).toString());
5956 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005957 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
5958 if (checkin) {
5959 pw.print(":");
5960 }
5961 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005962 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
5963 if (checkin) {
5964 pw.print(":");
5965 }
5966 pw.println("*OVERFLOW*");
5967 } else {
5968 if (!checkin) {
5969 if (rec.batteryLevel < 10) pw.print("00");
5970 else if (rec.batteryLevel < 100) pw.print("0");
5971 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005972 if (verbose) {
5973 pw.print(" ");
5974 if (rec.states < 0) ;
5975 else if (rec.states < 0x10) pw.print("0000000");
5976 else if (rec.states < 0x100) pw.print("000000");
5977 else if (rec.states < 0x1000) pw.print("00000");
5978 else if (rec.states < 0x10000) pw.print("0000");
5979 else if (rec.states < 0x100000) pw.print("000");
5980 else if (rec.states < 0x1000000) pw.print("00");
5981 else if (rec.states < 0x10000000) pw.print("0");
5982 pw.print(Integer.toHexString(rec.states));
5983 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005984 } else {
5985 if (oldLevel != rec.batteryLevel) {
5986 oldLevel = rec.batteryLevel;
5987 pw.print(",Bl="); pw.print(rec.batteryLevel);
5988 }
5989 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005990 if (oldStatus != rec.batteryStatus) {
5991 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005992 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005993 switch (oldStatus) {
5994 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005995 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005996 break;
5997 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005998 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005999 break;
6000 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006001 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006002 break;
6003 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006004 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006005 break;
6006 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006007 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006008 break;
6009 default:
6010 pw.print(oldStatus);
6011 break;
6012 }
6013 }
6014 if (oldHealth != rec.batteryHealth) {
6015 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006016 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006017 switch (oldHealth) {
6018 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006019 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006020 break;
6021 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006022 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006023 break;
6024 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006025 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006026 break;
6027 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006028 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006029 break;
6030 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006031 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006032 break;
6033 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006034 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006035 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006036 case BatteryManager.BATTERY_HEALTH_COLD:
6037 pw.print(checkin ? "c" : "cold");
6038 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006039 default:
6040 pw.print(oldHealth);
6041 break;
6042 }
6043 }
6044 if (oldPlug != rec.batteryPlugType) {
6045 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006046 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006047 switch (oldPlug) {
6048 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006049 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006050 break;
6051 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006052 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006053 break;
6054 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006055 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006056 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006057 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006058 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006059 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006060 default:
6061 pw.print(oldPlug);
6062 break;
6063 }
6064 }
6065 if (oldTemp != rec.batteryTemperature) {
6066 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006067 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006068 pw.print(oldTemp);
6069 }
6070 if (oldVolt != rec.batteryVoltage) {
6071 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006072 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006073 pw.print(oldVolt);
6074 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006075 final int chargeMAh = rec.batteryChargeUAh / 1000;
6076 if (oldChargeMAh != chargeMAh) {
6077 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07006078 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006079 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07006080 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006081 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006082 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006083 printBitDescriptions(pw, oldState2, rec.states2, null,
6084 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006085 if (rec.wakeReasonTag != null) {
6086 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006087 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006088 pw.print(rec.wakeReasonTag.poolIdx);
6089 } else {
6090 pw.print(" wake_reason=");
6091 pw.print(rec.wakeReasonTag.uid);
6092 pw.print(":\"");
6093 pw.print(rec.wakeReasonTag.string);
6094 pw.print("\"");
6095 }
6096 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08006097 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006098 pw.print(checkin ? "," : " ");
6099 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
6100 pw.print("+");
6101 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
6102 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006103 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006104 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
6105 : HISTORY_EVENT_NAMES;
6106 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
6107 | HistoryItem.EVENT_FLAG_FINISH);
6108 if (idx >= 0 && idx < eventNames.length) {
6109 pw.print(eventNames[idx]);
6110 } else {
6111 pw.print(checkin ? "Ev" : "event");
6112 pw.print(idx);
6113 }
6114 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006115 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006116 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08006117 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08006118 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
6119 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006120 pw.print(":\"");
6121 pw.print(rec.eventTag.string);
6122 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006123 }
6124 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006125 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006126 if (rec.stepDetails != null) {
6127 if (!checkin) {
6128 pw.print(" Details: cpu=");
6129 pw.print(rec.stepDetails.userTime);
6130 pw.print("u+");
6131 pw.print(rec.stepDetails.systemTime);
6132 pw.print("s");
6133 if (rec.stepDetails.appCpuUid1 >= 0) {
6134 pw.print(" (");
6135 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
6136 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6137 if (rec.stepDetails.appCpuUid2 >= 0) {
6138 pw.print(", ");
6139 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
6140 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6141 }
6142 if (rec.stepDetails.appCpuUid3 >= 0) {
6143 pw.print(", ");
6144 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
6145 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6146 }
6147 pw.print(')');
6148 }
6149 pw.println();
6150 pw.print(" /proc/stat=");
6151 pw.print(rec.stepDetails.statUserTime);
6152 pw.print(" usr, ");
6153 pw.print(rec.stepDetails.statSystemTime);
6154 pw.print(" sys, ");
6155 pw.print(rec.stepDetails.statIOWaitTime);
6156 pw.print(" io, ");
6157 pw.print(rec.stepDetails.statIrqTime);
6158 pw.print(" irq, ");
6159 pw.print(rec.stepDetails.statSoftIrqTime);
6160 pw.print(" sirq, ");
6161 pw.print(rec.stepDetails.statIdlTime);
6162 pw.print(" idle");
6163 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
6164 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
6165 + rec.stepDetails.statSoftIrqTime;
6166 int total = totalRun + rec.stepDetails.statIdlTime;
6167 if (total > 0) {
6168 pw.print(" (");
6169 float perc = ((float)totalRun) / ((float)total) * 100;
6170 pw.print(String.format("%.1f%%", perc));
6171 pw.print(" of ");
6172 StringBuilder sb = new StringBuilder(64);
6173 formatTimeMsNoSpace(sb, total*10);
6174 pw.print(sb);
6175 pw.print(")");
6176 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006177 pw.print(", PlatformIdleStat ");
6178 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006179 pw.println();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006180
6181 pw.print(", SubsystemPowerState ");
6182 pw.print(rec.stepDetails.statSubsystemPowerState);
6183 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006184 } else {
6185 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6186 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
6187 pw.print(rec.stepDetails.userTime);
6188 pw.print(":");
6189 pw.print(rec.stepDetails.systemTime);
6190 if (rec.stepDetails.appCpuUid1 >= 0) {
6191 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
6192 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6193 if (rec.stepDetails.appCpuUid2 >= 0) {
6194 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
6195 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6196 }
6197 if (rec.stepDetails.appCpuUid3 >= 0) {
6198 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
6199 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6200 }
6201 }
6202 pw.println();
6203 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6204 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
6205 pw.print(rec.stepDetails.statUserTime);
6206 pw.print(',');
6207 pw.print(rec.stepDetails.statSystemTime);
6208 pw.print(',');
6209 pw.print(rec.stepDetails.statIOWaitTime);
6210 pw.print(',');
6211 pw.print(rec.stepDetails.statIrqTime);
6212 pw.print(',');
6213 pw.print(rec.stepDetails.statSoftIrqTime);
6214 pw.print(',');
6215 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006216 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006217 if (rec.stepDetails.statPlatformIdleState != null) {
6218 pw.print(rec.stepDetails.statPlatformIdleState);
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006219 if (rec.stepDetails.statSubsystemPowerState != null) {
6220 pw.print(',');
6221 }
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006222 }
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006223
6224 if (rec.stepDetails.statSubsystemPowerState != null) {
6225 pw.print(rec.stepDetails.statSubsystemPowerState);
6226 }
6227 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006228 }
6229 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006230 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006231 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006232 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006233 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006234
6235 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
6236 UserHandle.formatUid(pw, uid);
6237 pw.print("=");
6238 pw.print(utime);
6239 pw.print("u+");
6240 pw.print(stime);
6241 pw.print("s");
6242 }
6243
6244 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
6245 pw.print('/');
6246 pw.print(uid);
6247 pw.print(":");
6248 pw.print(utime);
6249 pw.print(":");
6250 pw.print(stime);
6251 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006252 }
6253
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006254 private void printSizeValue(PrintWriter pw, long size) {
6255 float result = size;
6256 String suffix = "";
6257 if (result >= 10*1024) {
6258 suffix = "KB";
6259 result = result / 1024;
6260 }
6261 if (result >= 10*1024) {
6262 suffix = "MB";
6263 result = result / 1024;
6264 }
6265 if (result >= 10*1024) {
6266 suffix = "GB";
6267 result = result / 1024;
6268 }
6269 if (result >= 10*1024) {
6270 suffix = "TB";
6271 result = result / 1024;
6272 }
6273 if (result >= 10*1024) {
6274 suffix = "PB";
6275 result = result / 1024;
6276 }
6277 pw.print((int)result);
6278 pw.print(suffix);
6279 }
6280
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006281 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
6282 String label3, long estimatedTime) {
6283 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006284 return false;
6285 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006286 pw.print(label1);
6287 pw.print(label2);
6288 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006289 StringBuilder sb = new StringBuilder(64);
6290 formatTimeMs(sb, estimatedTime);
6291 pw.print(sb);
6292 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006293 return true;
6294 }
6295
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006296 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
6297 LevelStepTracker steps, boolean checkin) {
6298 if (steps == null) {
6299 return false;
6300 }
6301 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006302 if (count <= 0) {
6303 return false;
6304 }
6305 if (!checkin) {
6306 pw.println(header);
6307 }
Kweku Adams030980a2015-04-01 16:07:48 -07006308 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006309 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006310 long duration = steps.getDurationAt(i);
6311 int level = steps.getLevelAt(i);
6312 long initMode = steps.getInitModeAt(i);
6313 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006314 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006315 lineArgs[0] = Long.toString(duration);
6316 lineArgs[1] = Integer.toString(level);
6317 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6318 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6319 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
6320 case Display.STATE_ON: lineArgs[2] = "s+"; break;
6321 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
6322 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07006323 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006324 }
6325 } else {
6326 lineArgs[2] = "";
6327 }
6328 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6329 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
6330 } else {
6331 lineArgs[3] = "";
6332 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006333 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07006334 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006335 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07006336 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006337 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006338 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
6339 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006340 pw.print(prefix);
6341 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006342 TimeUtils.formatDuration(duration, pw);
6343 pw.print(" to "); pw.print(level);
6344 boolean haveModes = false;
6345 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6346 pw.print(" (");
6347 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6348 case Display.STATE_OFF: pw.print("screen-off"); break;
6349 case Display.STATE_ON: pw.print("screen-on"); break;
6350 case Display.STATE_DOZE: pw.print("screen-doze"); break;
6351 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07006352 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006353 }
6354 haveModes = true;
6355 }
6356 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6357 pw.print(haveModes ? ", " : " (");
6358 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
6359 ? "power-save-on" : "power-save-off");
6360 haveModes = true;
6361 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006362 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6363 pw.print(haveModes ? ", " : " (");
6364 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6365 ? "device-idle-on" : "device-idle-off");
6366 haveModes = true;
6367 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006368 if (haveModes) {
6369 pw.print(")");
6370 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006371 pw.println();
6372 }
6373 }
6374 return true;
6375 }
6376
Kweku Adams87b19ec2017-10-09 12:40:03 -07006377 private static void dumpDurationSteps(ProtoOutputStream proto, long fieldId,
6378 LevelStepTracker steps) {
6379 if (steps == null) {
6380 return;
6381 }
6382 int count = steps.mNumStepDurations;
Kweku Adams87b19ec2017-10-09 12:40:03 -07006383 for (int i = 0; i < count; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07006384 long token = proto.start(fieldId);
Kweku Adams87b19ec2017-10-09 12:40:03 -07006385 proto.write(SystemProto.BatteryLevelStep.DURATION_MS, steps.getDurationAt(i));
6386 proto.write(SystemProto.BatteryLevelStep.LEVEL, steps.getLevelAt(i));
6387
6388 final long initMode = steps.getInitModeAt(i);
6389 final long modMode = steps.getModModeAt(i);
6390
6391 int ds = SystemProto.BatteryLevelStep.DS_MIXED;
6392 if ((modMode & STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6393 switch ((int) (initMode & STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6394 case Display.STATE_OFF:
6395 ds = SystemProto.BatteryLevelStep.DS_OFF;
6396 break;
6397 case Display.STATE_ON:
6398 ds = SystemProto.BatteryLevelStep.DS_ON;
6399 break;
6400 case Display.STATE_DOZE:
6401 ds = SystemProto.BatteryLevelStep.DS_DOZE;
6402 break;
6403 case Display.STATE_DOZE_SUSPEND:
6404 ds = SystemProto.BatteryLevelStep.DS_DOZE_SUSPEND;
6405 break;
6406 default:
6407 ds = SystemProto.BatteryLevelStep.DS_ERROR;
6408 break;
6409 }
6410 }
6411 proto.write(SystemProto.BatteryLevelStep.DISPLAY_STATE, ds);
6412
6413 int psm = SystemProto.BatteryLevelStep.PSM_MIXED;
6414 if ((modMode & STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6415 psm = (initMode & STEP_LEVEL_MODE_POWER_SAVE) != 0
6416 ? SystemProto.BatteryLevelStep.PSM_ON : SystemProto.BatteryLevelStep.PSM_OFF;
6417 }
6418 proto.write(SystemProto.BatteryLevelStep.POWER_SAVE_MODE, psm);
6419
6420 int im = SystemProto.BatteryLevelStep.IM_MIXED;
6421 if ((modMode & STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6422 im = (initMode & STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6423 ? SystemProto.BatteryLevelStep.IM_ON : SystemProto.BatteryLevelStep.IM_OFF;
6424 }
6425 proto.write(SystemProto.BatteryLevelStep.IDLE_MODE, im);
6426
6427 proto.end(token);
6428 }
6429 }
6430
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006431 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006432 public static final int DUMP_DAILY_ONLY = 1<<2;
6433 public static final int DUMP_HISTORY_ONLY = 1<<3;
6434 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
6435 public static final int DUMP_VERBOSE = 1<<5;
6436 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006437
Dianne Hackborn37de0982014-05-09 09:32:18 -07006438 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
6439 final HistoryPrinter hprinter = new HistoryPrinter();
6440 final HistoryItem rec = new HistoryItem();
6441 long lastTime = -1;
6442 long baseTime = -1;
6443 boolean printed = false;
6444 HistoryEventTracker tracker = null;
6445 while (getNextHistoryLocked(rec)) {
6446 lastTime = rec.time;
6447 if (baseTime < 0) {
6448 baseTime = lastTime;
6449 }
6450 if (rec.time >= histStart) {
6451 if (histStart >= 0 && !printed) {
6452 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07006453 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006454 || rec.cmd == HistoryItem.CMD_START
6455 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006456 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006457 hprinter.printNextItem(pw, rec, baseTime, checkin,
6458 (flags&DUMP_VERBOSE) != 0);
6459 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006460 } else if (rec.currentTime != 0) {
6461 printed = true;
6462 byte cmd = rec.cmd;
6463 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006464 hprinter.printNextItem(pw, rec, baseTime, checkin,
6465 (flags&DUMP_VERBOSE) != 0);
6466 rec.cmd = cmd;
6467 }
6468 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006469 if (rec.cmd != HistoryItem.CMD_UPDATE) {
6470 hprinter.printNextItem(pw, rec, baseTime, checkin,
6471 (flags&DUMP_VERBOSE) != 0);
6472 rec.cmd = HistoryItem.CMD_UPDATE;
6473 }
6474 int oldEventCode = rec.eventCode;
6475 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006476 rec.eventTag = new HistoryTag();
6477 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
6478 HashMap<String, SparseIntArray> active
6479 = tracker.getStateForEvent(i);
6480 if (active == null) {
6481 continue;
6482 }
6483 for (HashMap.Entry<String, SparseIntArray> ent
6484 : active.entrySet()) {
6485 SparseIntArray uids = ent.getValue();
6486 for (int j=0; j<uids.size(); j++) {
6487 rec.eventCode = i;
6488 rec.eventTag.string = ent.getKey();
6489 rec.eventTag.uid = uids.keyAt(j);
6490 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006491 hprinter.printNextItem(pw, rec, baseTime, checkin,
6492 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006493 rec.wakeReasonTag = null;
6494 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006495 }
6496 }
6497 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006498 rec.eventCode = oldEventCode;
6499 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006500 tracker = null;
6501 }
6502 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006503 hprinter.printNextItem(pw, rec, baseTime, checkin,
6504 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07006505 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
6506 // This is an attempt to aggregate the previous state and generate
6507 // fake events to reflect that state at the point where we start
6508 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07006509 if (tracker == null) {
6510 tracker = new HistoryEventTracker();
6511 }
6512 tracker.updateState(rec.eventCode, rec.eventTag.string,
6513 rec.eventTag.uid, rec.eventTag.poolIdx);
6514 }
6515 }
6516 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07006517 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006518 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
6519 }
6520 }
6521
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006522 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
6523 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
6524 if (steps == null) {
6525 return;
6526 }
6527 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
6528 if (timeRemaining >= 0) {
6529 pw.print(prefix); pw.print(label); pw.print(" total time: ");
6530 tmpSb.setLength(0);
6531 formatTimeMs(tmpSb, timeRemaining);
6532 pw.print(tmpSb);
6533 pw.print(" (from "); pw.print(tmpOutInt[0]);
6534 pw.println(" steps)");
6535 }
6536 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6537 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6538 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
6539 if (estimatedTime > 0) {
6540 pw.print(prefix); pw.print(label); pw.print(" ");
6541 pw.print(STEP_LEVEL_MODE_LABELS[i]);
6542 pw.print(" time: ");
6543 tmpSb.setLength(0);
6544 formatTimeMs(tmpSb, estimatedTime);
6545 pw.print(tmpSb);
6546 pw.print(" (from "); pw.print(tmpOutInt[0]);
6547 pw.println(" steps)");
6548 }
6549 }
6550 }
6551
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006552 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
6553 ArrayList<PackageChange> changes) {
6554 if (changes == null) {
6555 return;
6556 }
6557 pw.print(prefix); pw.println("Package changes:");
6558 for (int i=0; i<changes.size(); i++) {
6559 PackageChange pc = changes.get(i);
6560 if (pc.mUpdate) {
6561 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
6562 pw.print(" vers="); pw.println(pc.mVersionCode);
6563 } else {
6564 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
6565 }
6566 }
6567 }
6568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006569 /**
6570 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
6571 *
6572 * @param pw a Printer to receive the dump output.
6573 */
6574 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006575 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006576 prepareForDumpLocked();
6577
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006578 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006579 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006580
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006581 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006582 final long historyTotalSize = getHistoryTotalSize();
6583 final long historyUsedSize = getHistoryUsedSize();
6584 if (startIteratingHistoryLocked()) {
6585 try {
6586 pw.print("Battery History (");
6587 pw.print((100*historyUsedSize)/historyTotalSize);
6588 pw.print("% used, ");
6589 printSizeValue(pw, historyUsedSize);
6590 pw.print(" used of ");
6591 printSizeValue(pw, historyTotalSize);
6592 pw.print(", ");
6593 pw.print(getHistoryStringPoolSize());
6594 pw.print(" strings using ");
6595 printSizeValue(pw, getHistoryStringPoolBytes());
6596 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07006597 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006598 pw.println();
6599 } finally {
6600 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006601 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006602 }
6603
6604 if (startIteratingOldHistoryLocked()) {
6605 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006606 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006607 pw.println("Old battery History:");
6608 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006609 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006610 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006611 if (baseTime < 0) {
6612 baseTime = rec.time;
6613 }
6614 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006615 }
6616 pw.println();
6617 } finally {
6618 finishIteratingOldHistoryLocked();
6619 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006620 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006621 }
6622
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006623 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006624 return;
6625 }
6626
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006627 if (!filtering) {
6628 SparseArray<? extends Uid> uidStats = getUidStats();
6629 final int NU = uidStats.size();
6630 boolean didPid = false;
6631 long nowRealtime = SystemClock.elapsedRealtime();
6632 for (int i=0; i<NU; i++) {
6633 Uid uid = uidStats.valueAt(i);
6634 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
6635 if (pids != null) {
6636 for (int j=0; j<pids.size(); j++) {
6637 Uid.Pid pid = pids.valueAt(j);
6638 if (!didPid) {
6639 pw.println("Per-PID Stats:");
6640 didPid = true;
6641 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006642 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
6643 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006644 pw.print(" PID "); pw.print(pids.keyAt(j));
6645 pw.print(" wake time: ");
6646 TimeUtils.formatDuration(time, pw);
6647 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006648 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006649 }
6650 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006651 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006652 pw.println();
6653 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006654 }
6655
6656 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006657 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
6658 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006659 long timeRemaining = computeBatteryTimeRemaining(
6660 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006661 if (timeRemaining >= 0) {
6662 pw.print(" Estimated discharge time remaining: ");
6663 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6664 pw.println();
6665 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006666 final LevelStepTracker steps = getDischargeLevelStepTracker();
6667 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6668 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
6669 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6670 STEP_LEVEL_MODE_VALUES[i], null));
6671 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006672 pw.println();
6673 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006674 if (dumpDurationSteps(pw, " ", "Charge step durations:",
6675 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006676 long timeRemaining = computeChargeTimeRemaining(
6677 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006678 if (timeRemaining >= 0) {
6679 pw.print(" Estimated charge time remaining: ");
6680 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6681 pw.println();
6682 }
6683 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006684 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006685 }
Dianne Hackbornc81983a2017-10-20 16:16:32 -07006686 if (!filtering || (flags & DUMP_DAILY_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006687 pw.println("Daily stats:");
6688 pw.print(" Current start time: ");
6689 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6690 getCurrentDailyStartTime()).toString());
6691 pw.print(" Next min deadline: ");
6692 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6693 getNextMinDailyDeadline()).toString());
6694 pw.print(" Next max deadline: ");
6695 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6696 getNextMaxDailyDeadline()).toString());
6697 StringBuilder sb = new StringBuilder(64);
6698 int[] outInt = new int[1];
6699 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
6700 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006701 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
6702 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006703 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006704 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
6705 dsteps, false)) {
6706 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6707 sb, outInt);
6708 }
6709 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6710 csteps, false)) {
6711 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6712 sb, outInt);
6713 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006714 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006715 } else {
6716 pw.println(" Current daily steps:");
6717 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6718 sb, outInt);
6719 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6720 sb, outInt);
6721 }
6722 }
6723 DailyItem dit;
6724 int curIndex = 0;
6725 while ((dit=getDailyItemLocked(curIndex)) != null) {
6726 curIndex++;
6727 if ((flags&DUMP_DAILY_ONLY) != 0) {
6728 pw.println();
6729 }
6730 pw.print(" Daily from ");
6731 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6732 pw.print(" to ");
6733 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6734 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006735 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006736 if (dumpDurationSteps(pw, " ",
6737 " Discharge step durations:", dit.mDischargeSteps, false)) {
6738 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6739 sb, outInt);
6740 }
6741 if (dumpDurationSteps(pw, " ",
6742 " Charge step durations:", dit.mChargeSteps, false)) {
6743 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6744 sb, outInt);
6745 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006746 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006747 } else {
6748 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6749 sb, outInt);
6750 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6751 sb, outInt);
6752 }
6753 }
6754 pw.println();
6755 }
6756 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006757 pw.println("Statistics since last charge:");
6758 pw.println(" System starts: " + getStartCount()
6759 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006760 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6761 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006762 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006763 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006764 }
Mike Mac2f518a2017-09-19 16:06:03 -07006765
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006766 // This is called from BatteryStatsService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006767 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006768 public void dumpCheckinLocked(Context context, PrintWriter pw,
6769 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006770 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006771
6772 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006773 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6774 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006775
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006776 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6777
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006778 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07006779 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006780 try {
6781 for (int i=0; i<getHistoryStringPoolSize(); i++) {
6782 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6783 pw.print(HISTORY_STRING_POOL); pw.print(',');
6784 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006785 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006786 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006787 pw.print(",\"");
6788 String str = getHistoryTagPoolString(i);
6789 str = str.replace("\\", "\\\\");
6790 str = str.replace("\"", "\\\"");
6791 pw.print(str);
6792 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006793 pw.println();
6794 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006795 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006796 } finally {
6797 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08006798 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006799 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006800 }
6801
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006802 if ((flags & DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006803 return;
6804 }
6805
Dianne Hackborne4a59512010-12-07 11:08:07 -08006806 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006807 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08006808 for (int i=0; i<apps.size(); i++) {
6809 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006810 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
6811 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08006812 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006813 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
6814 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006815 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006816 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006817 }
6818 SparseArray<? extends Uid> uidStats = getUidStats();
6819 final int NU = uidStats.size();
6820 String[] lineArgs = new String[2];
6821 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006822 int uid = UserHandle.getAppId(uidStats.keyAt(i));
6823 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
6824 if (pkgs != null && !pkgs.second.value) {
6825 pkgs.second.value = true;
6826 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08006827 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006828 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006829 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
6830 (Object[])lineArgs);
6831 }
6832 }
6833 }
6834 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006835 if ((flags & DUMP_DAILY_ONLY) == 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006836 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006837 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07006838 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006839 if (timeRemaining >= 0) {
6840 lineArgs[0] = Long.toString(timeRemaining);
6841 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
6842 (Object[])lineArgs);
6843 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006844 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07006845 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006846 if (timeRemaining >= 0) {
6847 lineArgs[0] = Long.toString(timeRemaining);
6848 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
6849 (Object[])lineArgs);
6850 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006851 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
6852 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006853 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006854 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006855
Kweku Adams87b19ec2017-10-09 12:40:03 -07006856 /** Dump #STATS_SINCE_CHARGED batterystats data to a proto. @hide */
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006857 public void dumpProtoLocked(Context context, FileDescriptor fd, List<ApplicationInfo> apps,
Kweku Adams6ccebf22017-12-11 12:30:35 -08006858 int flags) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006859 final ProtoOutputStream proto = new ProtoOutputStream(fd);
6860 final long bToken = proto.start(BatteryStatsServiceDumpProto.BATTERYSTATS);
6861 prepareForDumpLocked();
6862
6863 proto.write(BatteryStatsProto.REPORT_VERSION, CHECKIN_VERSION);
6864 proto.write(BatteryStatsProto.PARCEL_VERSION, getParcelVersion());
6865 proto.write(BatteryStatsProto.START_PLATFORM_VERSION, getStartPlatformVersion());
6866 proto.write(BatteryStatsProto.END_PLATFORM_VERSION, getEndPlatformVersion());
6867
Kweku Adams6ccebf22017-12-11 12:30:35 -08006868 // History intentionally not included in proto dump.
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006869
6870 if ((flags & (DUMP_HISTORY_ONLY | DUMP_DAILY_ONLY)) == 0) {
Kweku Adams103351f2017-10-16 14:39:34 -07006871 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false,
6872 (flags & DUMP_DEVICE_WIFI_ONLY) != 0);
6873 helper.create(this);
6874 helper.refreshStats(STATS_SINCE_CHARGED, UserHandle.USER_ALL);
6875
6876 dumpProtoAppsLocked(proto, helper, apps);
6877 dumpProtoSystemLocked(proto, helper);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006878 }
6879
6880 proto.end(bToken);
6881 proto.flush();
6882 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07006883
Kweku Adams103351f2017-10-16 14:39:34 -07006884 private void dumpProtoAppsLocked(ProtoOutputStream proto, BatteryStatsHelper helper,
6885 List<ApplicationInfo> apps) {
6886 final int which = STATS_SINCE_CHARGED;
6887 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
6888 final long rawRealtimeMs = SystemClock.elapsedRealtime();
6889 final long rawRealtimeUs = rawRealtimeMs * 1000;
6890 final long batteryUptimeUs = getBatteryUptime(rawUptimeUs);
6891
6892 SparseArray<ArrayList<String>> aidToPackages = new SparseArray<>();
6893 if (apps != null) {
6894 for (int i = 0; i < apps.size(); ++i) {
6895 ApplicationInfo ai = apps.get(i);
6896 int aid = UserHandle.getAppId(ai.uid);
6897 ArrayList<String> pkgs = aidToPackages.get(aid);
6898 if (pkgs == null) {
6899 pkgs = new ArrayList<String>();
6900 aidToPackages.put(aid, pkgs);
6901 }
6902 pkgs.add(ai.packageName);
6903 }
6904 }
6905
6906 SparseArray<BatterySipper> uidToSipper = new SparseArray<>();
6907 final List<BatterySipper> sippers = helper.getUsageList();
6908 if (sippers != null) {
6909 for (int i = 0; i < sippers.size(); ++i) {
6910 final BatterySipper bs = sippers.get(i);
6911 if (bs.drainType != BatterySipper.DrainType.APP) {
6912 // Others are handled by dumpProtoSystemLocked()
6913 continue;
6914 }
6915 uidToSipper.put(bs.uidObj.getUid(), bs);
6916 }
6917 }
6918
6919 SparseArray<? extends Uid> uidStats = getUidStats();
6920 final int n = uidStats.size();
6921 for (int iu = 0; iu < n; ++iu) {
6922 final long uTkn = proto.start(BatteryStatsProto.UIDS);
6923 final Uid u = uidStats.valueAt(iu);
6924
6925 final int uid = uidStats.keyAt(iu);
6926 proto.write(UidProto.UID, uid);
6927
6928 // Print packages and apk stats (UID_DATA & APK_DATA)
6929 ArrayList<String> pkgs = aidToPackages.get(UserHandle.getAppId(uid));
6930 if (pkgs == null) {
6931 pkgs = new ArrayList<String>();
6932 }
6933 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats =
6934 u.getPackageStats();
6935 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
6936 String pkg = packageStats.keyAt(ipkg);
6937 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats =
6938 packageStats.valueAt(ipkg).getServiceStats();
6939 if (serviceStats.size() == 0) {
6940 // Due to the way ActivityManagerService logs wakeup alarms, some packages (for
6941 // example, "android") may be included in the packageStats that aren't part of
6942 // the UID. If they don't have any services, then they shouldn't be listed here.
6943 // These packages won't be a part in the pkgs List.
6944 continue;
6945 }
6946
6947 final long pToken = proto.start(UidProto.PACKAGES);
6948 proto.write(UidProto.Package.NAME, pkg);
6949 // Remove from the packages list since we're logging it here.
6950 pkgs.remove(pkg);
6951
6952 for (int isvc = serviceStats.size() - 1; isvc >= 0; --isvc) {
6953 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
6954 long sToken = proto.start(UidProto.Package.SERVICES);
6955
6956 proto.write(UidProto.Package.Service.NAME, serviceStats.keyAt(isvc));
6957 proto.write(UidProto.Package.Service.START_DURATION_MS,
6958 roundUsToMs(ss.getStartTime(batteryUptimeUs, which)));
6959 proto.write(UidProto.Package.Service.START_COUNT, ss.getStarts(which));
6960 proto.write(UidProto.Package.Service.LAUNCH_COUNT, ss.getLaunches(which));
6961
6962 proto.end(sToken);
6963 }
6964 proto.end(pToken);
6965 }
6966 // Print any remaining packages that weren't in the packageStats map. pkgs is pulled
6967 // from PackageManager data. Packages are only included in packageStats if there was
6968 // specific data tracked for them (services and wakeup alarms, etc.).
6969 for (String p : pkgs) {
6970 final long pToken = proto.start(UidProto.PACKAGES);
6971 proto.write(UidProto.Package.NAME, p);
6972 proto.end(pToken);
6973 }
6974
6975 // Total wakelock data (AGGREGATED_WAKELOCK_DATA)
6976 if (u.getAggregatedPartialWakelockTimer() != null) {
6977 final Timer timer = u.getAggregatedPartialWakelockTimer();
6978 // Times are since reset (regardless of 'which')
6979 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
6980 final Timer bgTimer = timer.getSubTimer();
6981 final long bgTimeMs = bgTimer != null
6982 ? bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
6983 final long awToken = proto.start(UidProto.AGGREGATED_WAKELOCK);
6984 proto.write(UidProto.AggregatedWakelock.PARTIAL_DURATION_MS, totTimeMs);
6985 proto.write(UidProto.AggregatedWakelock.BACKGROUND_PARTIAL_DURATION_MS, bgTimeMs);
6986 proto.end(awToken);
6987 }
6988
6989 // Audio (AUDIO_DATA)
6990 dumpTimer(proto, UidProto.AUDIO, u.getAudioTurnedOnTimer(), rawRealtimeUs, which);
6991
6992 // Bluetooth Controller (BLUETOOTH_CONTROLLER_DATA)
6993 dumpControllerActivityProto(proto, UidProto.BLUETOOTH_CONTROLLER,
6994 u.getBluetoothControllerActivity(), which);
6995
6996 // BLE scans (BLUETOOTH_MISC_DATA) (uses totalDurationMsLocked and MaxDurationMsLocked)
6997 final Timer bleTimer = u.getBluetoothScanTimer();
6998 if (bleTimer != null) {
6999 final long bmToken = proto.start(UidProto.BLUETOOTH_MISC);
7000
7001 dumpTimer(proto, UidProto.BluetoothMisc.APPORTIONED_BLE_SCAN, bleTimer,
7002 rawRealtimeUs, which);
7003 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN,
7004 u.getBluetoothScanBackgroundTimer(), rawRealtimeUs, which);
7005 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
7006 dumpTimer(proto, UidProto.BluetoothMisc.UNOPTIMIZED_BLE_SCAN,
7007 u.getBluetoothUnoptimizedScanTimer(), rawRealtimeUs, which);
7008 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
7009 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_UNOPTIMIZED_BLE_SCAN,
7010 u.getBluetoothUnoptimizedScanBackgroundTimer(), rawRealtimeUs, which);
7011 // Result counters
7012 proto.write(UidProto.BluetoothMisc.BLE_SCAN_RESULT_COUNT,
7013 u.getBluetoothScanResultCounter() != null
7014 ? u.getBluetoothScanResultCounter().getCountLocked(which) : 0);
7015 proto.write(UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN_RESULT_COUNT,
7016 u.getBluetoothScanResultBgCounter() != null
7017 ? u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0);
7018
7019 proto.end(bmToken);
7020 }
7021
7022 // Camera (CAMERA_DATA)
7023 dumpTimer(proto, UidProto.CAMERA, u.getCameraTurnedOnTimer(), rawRealtimeUs, which);
7024
7025 // CPU stats (CPU_DATA & CPU_TIMES_AT_FREQ_DATA)
7026 final long cpuToken = proto.start(UidProto.CPU);
7027 proto.write(UidProto.Cpu.USER_DURATION_MS, roundUsToMs(u.getUserCpuTimeUs(which)));
7028 proto.write(UidProto.Cpu.SYSTEM_DURATION_MS, roundUsToMs(u.getSystemCpuTimeUs(which)));
7029
7030 final long[] cpuFreqs = getCpuFreqs();
7031 if (cpuFreqs != null) {
7032 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
7033 // If total cpuFreqTimes is null, then we don't need to check for
7034 // screenOffCpuFreqTimes.
7035 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
7036 long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
7037 if (screenOffCpuFreqTimeMs == null) {
7038 screenOffCpuFreqTimeMs = new long[cpuFreqTimeMs.length];
7039 }
7040 for (int ic = 0; ic < cpuFreqTimeMs.length; ++ic) {
7041 long cToken = proto.start(UidProto.Cpu.BY_FREQUENCY);
7042 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7043 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7044 cpuFreqTimeMs[ic]);
7045 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7046 screenOffCpuFreqTimeMs[ic]);
7047 proto.end(cToken);
7048 }
7049 }
7050 }
7051 proto.end(cpuToken);
7052
7053 // Flashlight (FLASHLIGHT_DATA)
7054 dumpTimer(proto, UidProto.FLASHLIGHT, u.getFlashlightTurnedOnTimer(),
7055 rawRealtimeUs, which);
7056
7057 // Foreground activity (FOREGROUND_ACTIVITY_DATA)
7058 dumpTimer(proto, UidProto.FOREGROUND_ACTIVITY, u.getForegroundActivityTimer(),
7059 rawRealtimeUs, which);
7060
7061 // Foreground service (FOREGROUND_SERVICE_DATA)
7062 dumpTimer(proto, UidProto.FOREGROUND_SERVICE, u.getForegroundServiceTimer(),
7063 rawRealtimeUs, which);
7064
7065 // Job completion (JOB_COMPLETION_DATA)
7066 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
7067 final int[] reasons = new int[]{
7068 JobParameters.REASON_CANCELED,
7069 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED,
7070 JobParameters.REASON_PREEMPT,
7071 JobParameters.REASON_TIMEOUT,
7072 JobParameters.REASON_DEVICE_IDLE,
7073 };
7074 for (int ic = 0; ic < completions.size(); ++ic) {
7075 SparseIntArray types = completions.valueAt(ic);
7076 if (types != null) {
7077 final long jcToken = proto.start(UidProto.JOB_COMPLETION);
7078
7079 proto.write(UidProto.JobCompletion.NAME, completions.keyAt(ic));
7080
7081 for (int r : reasons) {
7082 long rToken = proto.start(UidProto.JobCompletion.REASON_COUNT);
7083 proto.write(UidProto.JobCompletion.ReasonCount.NAME, r);
7084 proto.write(UidProto.JobCompletion.ReasonCount.COUNT, types.get(r, 0));
7085 proto.end(rToken);
7086 }
7087
7088 proto.end(jcToken);
7089 }
7090 }
7091
7092 // Scheduled jobs (JOB_DATA)
7093 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
7094 for (int ij = jobs.size() - 1; ij >= 0; --ij) {
7095 final Timer timer = jobs.valueAt(ij);
7096 final Timer bgTimer = timer.getSubTimer();
7097 final long jToken = proto.start(UidProto.JOBS);
7098
7099 proto.write(UidProto.Job.NAME, jobs.keyAt(ij));
7100 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7101 dumpTimer(proto, UidProto.Job.TOTAL, timer, rawRealtimeUs, which);
7102 dumpTimer(proto, UidProto.Job.BACKGROUND, bgTimer, rawRealtimeUs, which);
7103
7104 proto.end(jToken);
7105 }
7106
7107 // Modem Controller (MODEM_CONTROLLER_DATA)
7108 dumpControllerActivityProto(proto, UidProto.MODEM_CONTROLLER,
7109 u.getModemControllerActivity(), which);
7110
7111 // Network stats (NETWORK_DATA)
7112 final long nToken = proto.start(UidProto.NETWORK);
7113 proto.write(UidProto.Network.MOBILE_BYTES_RX,
7114 u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7115 proto.write(UidProto.Network.MOBILE_BYTES_TX,
7116 u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7117 proto.write(UidProto.Network.WIFI_BYTES_RX,
7118 u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7119 proto.write(UidProto.Network.WIFI_BYTES_TX,
7120 u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7121 proto.write(UidProto.Network.BT_BYTES_RX,
7122 u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7123 proto.write(UidProto.Network.BT_BYTES_TX,
7124 u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
7125 proto.write(UidProto.Network.MOBILE_PACKETS_RX,
7126 u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7127 proto.write(UidProto.Network.MOBILE_PACKETS_TX,
7128 u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7129 proto.write(UidProto.Network.WIFI_PACKETS_RX,
7130 u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7131 proto.write(UidProto.Network.WIFI_PACKETS_TX,
7132 u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7133 proto.write(UidProto.Network.MOBILE_ACTIVE_DURATION_MS,
7134 roundUsToMs(u.getMobileRadioActiveTime(which)));
7135 proto.write(UidProto.Network.MOBILE_ACTIVE_COUNT,
7136 u.getMobileRadioActiveCount(which));
7137 proto.write(UidProto.Network.MOBILE_WAKEUP_COUNT,
7138 u.getMobileRadioApWakeupCount(which));
7139 proto.write(UidProto.Network.WIFI_WAKEUP_COUNT,
7140 u.getWifiRadioApWakeupCount(which));
7141 proto.write(UidProto.Network.MOBILE_BYTES_BG_RX,
7142 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA, which));
7143 proto.write(UidProto.Network.MOBILE_BYTES_BG_TX,
7144 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA, which));
7145 proto.write(UidProto.Network.WIFI_BYTES_BG_RX,
7146 u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which));
7147 proto.write(UidProto.Network.WIFI_BYTES_BG_TX,
7148 u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which));
7149 proto.write(UidProto.Network.MOBILE_PACKETS_BG_RX,
7150 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA, which));
7151 proto.write(UidProto.Network.MOBILE_PACKETS_BG_TX,
7152 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA, which));
7153 proto.write(UidProto.Network.WIFI_PACKETS_BG_RX,
7154 u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA, which));
7155 proto.write(UidProto.Network.WIFI_PACKETS_BG_TX,
7156 u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA, which));
7157 proto.end(nToken);
7158
7159 // Power use item (POWER_USE_ITEM_DATA)
7160 BatterySipper bs = uidToSipper.get(uid);
7161 if (bs != null) {
7162 final long bsToken = proto.start(UidProto.POWER_USE_ITEM);
7163 proto.write(UidProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7164 proto.write(UidProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7165 proto.write(UidProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7166 proto.write(UidProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7167 bs.proportionalSmearMah);
7168 proto.end(bsToken);
7169 }
7170
7171 // Processes (PROCESS_DATA)
7172 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats =
7173 u.getProcessStats();
7174 for (int ipr = processStats.size() - 1; ipr >= 0; --ipr) {
7175 final Uid.Proc ps = processStats.valueAt(ipr);
7176 final long prToken = proto.start(UidProto.PROCESS);
7177
7178 proto.write(UidProto.Process.NAME, processStats.keyAt(ipr));
7179 proto.write(UidProto.Process.USER_DURATION_MS, ps.getUserTime(which));
7180 proto.write(UidProto.Process.SYSTEM_DURATION_MS, ps.getSystemTime(which));
7181 proto.write(UidProto.Process.FOREGROUND_DURATION_MS, ps.getForegroundTime(which));
7182 proto.write(UidProto.Process.START_COUNT, ps.getStarts(which));
7183 proto.write(UidProto.Process.ANR_COUNT, ps.getNumAnrs(which));
7184 proto.write(UidProto.Process.CRASH_COUNT, ps.getNumCrashes(which));
7185
7186 proto.end(prToken);
7187 }
7188
7189 // Sensors (SENSOR_DATA)
7190 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
7191 for (int ise = 0; ise < sensors.size(); ++ise) {
7192 final Uid.Sensor se = sensors.valueAt(ise);
7193 final Timer timer = se.getSensorTime();
7194 if (timer == null) {
7195 continue;
7196 }
7197 final Timer bgTimer = se.getSensorBackgroundTime();
7198 final int sensorNumber = sensors.keyAt(ise);
7199 final long seToken = proto.start(UidProto.SENSORS);
7200
7201 proto.write(UidProto.Sensor.ID, sensorNumber);
7202 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7203 dumpTimer(proto, UidProto.Sensor.APPORTIONED, timer, rawRealtimeUs, which);
7204 dumpTimer(proto, UidProto.Sensor.BACKGROUND, bgTimer, rawRealtimeUs, which);
7205
7206 proto.end(seToken);
7207 }
7208
7209 // State times (STATE_TIME_DATA)
7210 for (int ips = 0; ips < Uid.NUM_PROCESS_STATE; ++ips) {
7211 long durMs = roundUsToMs(u.getProcessStateTime(ips, rawRealtimeUs, which));
7212 if (durMs == 0) {
7213 continue;
7214 }
7215 final long stToken = proto.start(UidProto.STATES);
7216 proto.write(UidProto.StateTime.STATE, ips);
7217 proto.write(UidProto.StateTime.DURATION_MS, durMs);
7218 proto.end(stToken);
7219 }
7220
7221 // Syncs (SYNC_DATA)
7222 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
7223 for (int isy = syncs.size() - 1; isy >= 0; --isy) {
7224 final Timer timer = syncs.valueAt(isy);
7225 final Timer bgTimer = timer.getSubTimer();
7226 final long syToken = proto.start(UidProto.SYNCS);
7227
7228 proto.write(UidProto.Sync.NAME, syncs.keyAt(isy));
7229 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7230 dumpTimer(proto, UidProto.Sync.TOTAL, timer, rawRealtimeUs, which);
7231 dumpTimer(proto, UidProto.Sync.BACKGROUND, bgTimer, rawRealtimeUs, which);
7232
7233 proto.end(syToken);
7234 }
7235
7236 // User activity (USER_ACTIVITY_DATA)
7237 if (u.hasUserActivity()) {
7238 for (int i = 0; i < Uid.NUM_USER_ACTIVITY_TYPES; ++i) {
7239 int val = u.getUserActivityCount(i, which);
7240 if (val != 0) {
7241 final long uaToken = proto.start(UidProto.USER_ACTIVITY);
7242 proto.write(UidProto.UserActivity.NAME, i);
7243 proto.write(UidProto.UserActivity.COUNT, val);
7244 proto.end(uaToken);
7245 }
7246 }
7247 }
7248
7249 // Vibrator (VIBRATOR_DATA)
7250 dumpTimer(proto, UidProto.VIBRATOR, u.getVibratorOnTimer(), rawRealtimeUs, which);
7251
7252 // Video (VIDEO_DATA)
7253 dumpTimer(proto, UidProto.VIDEO, u.getVideoTurnedOnTimer(), rawRealtimeUs, which);
7254
7255 // Wakelocks (WAKELOCK_DATA)
7256 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
7257 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7258 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7259 final long wToken = proto.start(UidProto.WAKELOCKS);
7260 proto.write(UidProto.Wakelock.NAME, wakelocks.keyAt(iw));
7261 dumpTimer(proto, UidProto.Wakelock.FULL, wl.getWakeTime(WAKE_TYPE_FULL),
7262 rawRealtimeUs, which);
7263 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7264 if (pTimer != null) {
7265 dumpTimer(proto, UidProto.Wakelock.PARTIAL, pTimer, rawRealtimeUs, which);
7266 dumpTimer(proto, UidProto.Wakelock.BACKGROUND_PARTIAL, pTimer.getSubTimer(),
7267 rawRealtimeUs, which);
7268 }
7269 dumpTimer(proto, UidProto.Wakelock.WINDOW, wl.getWakeTime(WAKE_TYPE_WINDOW),
7270 rawRealtimeUs, which);
7271 proto.end(wToken);
7272 }
7273
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007274 // Wifi Multicast Wakelock (WIFI_MULTICAST_WAKELOCK_DATA)
7275 dumpTimer(proto, UidProto.WIFI_MULTICAST_WAKELOCK, u.getMulticastWakelockStats(),
7276 rawRealtimeUs, which);
7277
Kweku Adams103351f2017-10-16 14:39:34 -07007278 // Wakeup alarms (WAKEUP_ALARM_DATA)
7279 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7280 final Uid.Pkg ps = packageStats.valueAt(ipkg);
7281 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
7282 for (int iwa = alarms.size() - 1; iwa >= 0; --iwa) {
7283 final long waToken = proto.start(UidProto.WAKEUP_ALARM);
7284 proto.write(UidProto.WakeupAlarm.NAME, alarms.keyAt(iwa));
7285 proto.write(UidProto.WakeupAlarm.COUNT,
7286 alarms.valueAt(iwa).getCountLocked(which));
7287 proto.end(waToken);
7288 }
7289 }
7290
7291 // Wifi Controller (WIFI_CONTROLLER_DATA)
7292 dumpControllerActivityProto(proto, UidProto.WIFI_CONTROLLER,
7293 u.getWifiControllerActivity(), which);
7294
7295 // Wifi data (WIFI_DATA)
7296 final long wToken = proto.start(UidProto.WIFI);
7297 proto.write(UidProto.Wifi.FULL_WIFI_LOCK_DURATION_MS,
7298 roundUsToMs(u.getFullWifiLockTime(rawRealtimeUs, which)));
7299 dumpTimer(proto, UidProto.Wifi.APPORTIONED_SCAN, u.getWifiScanTimer(),
7300 rawRealtimeUs, which);
7301 proto.write(UidProto.Wifi.RUNNING_DURATION_MS,
7302 roundUsToMs(u.getWifiRunningTime(rawRealtimeUs, which)));
7303 dumpTimer(proto, UidProto.Wifi.BACKGROUND_SCAN, u.getWifiScanBackgroundTimer(),
7304 rawRealtimeUs, which);
7305 proto.end(wToken);
7306
7307 proto.end(uTkn);
7308 }
7309 }
7310
7311 private void dumpProtoSystemLocked(ProtoOutputStream proto, BatteryStatsHelper helper) {
Kweku Adams87b19ec2017-10-09 12:40:03 -07007312 final long sToken = proto.start(BatteryStatsProto.SYSTEM);
7313 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7314 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7315 final long rawRealtimeUs = rawRealtimeMs * 1000;
7316 final int which = STATS_SINCE_CHARGED;
7317
7318 // Battery data (BATTERY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007319 final long bToken = proto.start(SystemProto.BATTERY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007320 proto.write(SystemProto.Battery.START_CLOCK_TIME_MS, getStartClockTime());
7321 proto.write(SystemProto.Battery.START_COUNT, getStartCount());
7322 proto.write(SystemProto.Battery.TOTAL_REALTIME_MS,
7323 computeRealtime(rawRealtimeUs, which) / 1000);
7324 proto.write(SystemProto.Battery.TOTAL_UPTIME_MS,
7325 computeUptime(rawUptimeUs, which) / 1000);
7326 proto.write(SystemProto.Battery.BATTERY_REALTIME_MS,
7327 computeBatteryRealtime(rawRealtimeUs, which) / 1000);
7328 proto.write(SystemProto.Battery.BATTERY_UPTIME_MS,
7329 computeBatteryUptime(rawUptimeUs, which) / 1000);
7330 proto.write(SystemProto.Battery.SCREEN_OFF_REALTIME_MS,
7331 computeBatteryScreenOffRealtime(rawRealtimeUs, which) / 1000);
7332 proto.write(SystemProto.Battery.SCREEN_OFF_UPTIME_MS,
7333 computeBatteryScreenOffUptime(rawUptimeUs, which) / 1000);
7334 proto.write(SystemProto.Battery.SCREEN_DOZE_DURATION_MS,
7335 getScreenDozeTime(rawRealtimeUs, which) / 1000);
7336 proto.write(SystemProto.Battery.ESTIMATED_BATTERY_CAPACITY_MAH,
7337 getEstimatedBatteryCapacity());
7338 proto.write(SystemProto.Battery.MIN_LEARNED_BATTERY_CAPACITY_UAH,
7339 getMinLearnedBatteryCapacity());
7340 proto.write(SystemProto.Battery.MAX_LEARNED_BATTERY_CAPACITY_UAH,
7341 getMaxLearnedBatteryCapacity());
Kweku Adams103351f2017-10-16 14:39:34 -07007342 proto.end(bToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007343
7344 // Battery discharge (BATTERY_DISCHARGE_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007345 final long bdToken = proto.start(SystemProto.BATTERY_DISCHARGE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007346 proto.write(SystemProto.BatteryDischarge.LOWER_BOUND_SINCE_CHARGE,
7347 getLowDischargeAmountSinceCharge());
7348 proto.write(SystemProto.BatteryDischarge.UPPER_BOUND_SINCE_CHARGE,
7349 getHighDischargeAmountSinceCharge());
7350 proto.write(SystemProto.BatteryDischarge.SCREEN_ON_SINCE_CHARGE,
7351 getDischargeAmountScreenOnSinceCharge());
7352 proto.write(SystemProto.BatteryDischarge.SCREEN_OFF_SINCE_CHARGE,
7353 getDischargeAmountScreenOffSinceCharge());
7354 proto.write(SystemProto.BatteryDischarge.SCREEN_DOZE_SINCE_CHARGE,
7355 getDischargeAmountScreenDozeSinceCharge());
7356 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH,
7357 getUahDischarge(which) / 1000);
7358 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_OFF,
7359 getUahDischargeScreenOff(which) / 1000);
7360 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_DOZE,
7361 getUahDischargeScreenDoze(which) / 1000);
Mike Ma15313c92017-11-15 17:58:21 -08007362 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_LIGHT_DOZE,
7363 getUahDischargeLightDoze(which) / 1000);
7364 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_DEEP_DOZE,
7365 getUahDischargeDeepDoze(which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007366 proto.end(bdToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007367
7368 // Time remaining
7369 long timeRemainingUs = computeChargeTimeRemaining(rawRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -07007370 // These are part of a oneof, so we should only set one of them.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007371 if (timeRemainingUs >= 0) {
7372 // Charge time remaining (CHARGE_TIME_REMAIN_DATA)
7373 proto.write(SystemProto.CHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7374 } else {
7375 timeRemainingUs = computeBatteryTimeRemaining(rawRealtimeUs);
7376 // Discharge time remaining (DISCHARGE_TIME_REMAIN_DATA)
7377 if (timeRemainingUs >= 0) {
7378 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7379 } else {
7380 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, -1);
7381 }
7382 }
7383
7384 // Charge step (CHARGE_STEP_DATA)
7385 dumpDurationSteps(proto, SystemProto.CHARGE_STEP, getChargeLevelStepTracker());
7386
7387 // Phone data connection (DATA_CONNECTION_TIME_DATA and DATA_CONNECTION_COUNT_DATA)
7388 for (int i = 0; i < NUM_DATA_CONNECTION_TYPES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007389 final long pdcToken = proto.start(SystemProto.DATA_CONNECTION);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007390 proto.write(SystemProto.DataConnection.NAME, i);
7391 dumpTimer(proto, SystemProto.DataConnection.TOTAL, getPhoneDataConnectionTimer(i),
7392 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007393 proto.end(pdcToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007394 }
7395
7396 // Discharge step (DISCHARGE_STEP_DATA)
7397 dumpDurationSteps(proto, SystemProto.DISCHARGE_STEP, getDischargeLevelStepTracker());
7398
7399 // CPU frequencies (GLOBAL_CPU_FREQ_DATA)
7400 final long[] cpuFreqs = getCpuFreqs();
7401 if (cpuFreqs != null) {
7402 for (long i : cpuFreqs) {
7403 proto.write(SystemProto.CPU_FREQUENCY, i);
7404 }
7405 }
7406
7407 // Bluetooth controller (GLOBAL_BLUETOOTH_CONTROLLER_DATA)
7408 dumpControllerActivityProto(proto, SystemProto.GLOBAL_BLUETOOTH_CONTROLLER,
7409 getBluetoothControllerActivity(), which);
7410
7411 // Modem controller (GLOBAL_MODEM_CONTROLLER_DATA)
7412 dumpControllerActivityProto(proto, SystemProto.GLOBAL_MODEM_CONTROLLER,
7413 getModemControllerActivity(), which);
7414
7415 // Global network data (GLOBAL_NETWORK_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007416 final long gnToken = proto.start(SystemProto.GLOBAL_NETWORK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007417 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_RX,
7418 getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7419 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_TX,
7420 getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7421 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_RX,
7422 getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7423 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_TX,
7424 getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7425 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_RX,
7426 getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7427 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_TX,
7428 getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7429 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_RX,
7430 getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7431 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_TX,
7432 getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7433 proto.write(SystemProto.GlobalNetwork.BT_BYTES_RX,
7434 getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7435 proto.write(SystemProto.GlobalNetwork.BT_BYTES_TX,
7436 getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
Kweku Adams103351f2017-10-16 14:39:34 -07007437 proto.end(gnToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007438
7439 // Wifi controller (GLOBAL_WIFI_CONTROLLER_DATA)
7440 dumpControllerActivityProto(proto, SystemProto.GLOBAL_WIFI_CONTROLLER,
7441 getWifiControllerActivity(), which);
7442
7443
7444 // Global wifi (GLOBAL_WIFI_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007445 final long gwToken = proto.start(SystemProto.GLOBAL_WIFI);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007446 proto.write(SystemProto.GlobalWifi.ON_DURATION_MS,
7447 getWifiOnTime(rawRealtimeUs, which) / 1000);
7448 proto.write(SystemProto.GlobalWifi.RUNNING_DURATION_MS,
7449 getGlobalWifiRunningTime(rawRealtimeUs, which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007450 proto.end(gwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007451
7452 // Kernel wakelock (KERNEL_WAKELOCK_DATA)
7453 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
7454 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007455 final long kwToken = proto.start(SystemProto.KERNEL_WAKELOCK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007456 proto.write(SystemProto.KernelWakelock.NAME, ent.getKey());
7457 dumpTimer(proto, SystemProto.KernelWakelock.TOTAL, ent.getValue(),
7458 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007459 proto.end(kwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007460 }
7461
7462 // Misc (MISC_DATA)
7463 // Calculate wakelock times across all uids.
7464 long fullWakeLockTimeTotalUs = 0;
7465 long partialWakeLockTimeTotalUs = 0;
7466
7467 final SparseArray<? extends Uid> uidStats = getUidStats();
7468 for (int iu = 0; iu < uidStats.size(); iu++) {
7469 final Uid u = uidStats.valueAt(iu);
7470
7471 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks =
7472 u.getWakelockStats();
7473 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7474 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7475
7476 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
7477 if (fullWakeTimer != null) {
7478 fullWakeLockTimeTotalUs += fullWakeTimer.getTotalTimeLocked(rawRealtimeUs,
7479 which);
7480 }
7481
7482 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7483 if (partialWakeTimer != null) {
7484 partialWakeLockTimeTotalUs += partialWakeTimer.getTotalTimeLocked(
7485 rawRealtimeUs, which);
7486 }
7487 }
7488 }
Kweku Adams103351f2017-10-16 14:39:34 -07007489 final long mToken = proto.start(SystemProto.MISC);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007490 proto.write(SystemProto.Misc.SCREEN_ON_DURATION_MS,
7491 getScreenOnTime(rawRealtimeUs, which) / 1000);
7492 proto.write(SystemProto.Misc.PHONE_ON_DURATION_MS,
7493 getPhoneOnTime(rawRealtimeUs, which) / 1000);
7494 proto.write(SystemProto.Misc.FULL_WAKELOCK_TOTAL_DURATION_MS,
7495 fullWakeLockTimeTotalUs / 1000);
7496 proto.write(SystemProto.Misc.PARTIAL_WAKELOCK_TOTAL_DURATION_MS,
7497 partialWakeLockTimeTotalUs / 1000);
7498 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_DURATION_MS,
7499 getMobileRadioActiveTime(rawRealtimeUs, which) / 1000);
7500 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_ADJUSTED_TIME_MS,
7501 getMobileRadioActiveAdjustedTime(which) / 1000);
7502 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_COUNT,
7503 getMobileRadioActiveCount(which));
7504 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_UNKNOWN_DURATION_MS,
7505 getMobileRadioActiveUnknownTime(which) / 1000);
7506 proto.write(SystemProto.Misc.INTERACTIVE_DURATION_MS,
7507 getInteractiveTime(rawRealtimeUs, which) / 1000);
7508 proto.write(SystemProto.Misc.BATTERY_SAVER_MODE_ENABLED_DURATION_MS,
7509 getPowerSaveModeEnabledTime(rawRealtimeUs, which) / 1000);
7510 proto.write(SystemProto.Misc.NUM_CONNECTIVITY_CHANGES,
7511 getNumConnectivityChange(which));
7512 proto.write(SystemProto.Misc.DEEP_DOZE_ENABLED_DURATION_MS,
7513 getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7514 proto.write(SystemProto.Misc.DEEP_DOZE_COUNT,
7515 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
7516 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_DURATION_MS,
7517 getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7518 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_COUNT,
7519 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
7520 proto.write(SystemProto.Misc.LONGEST_DEEP_DOZE_DURATION_MS,
7521 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
7522 proto.write(SystemProto.Misc.LIGHT_DOZE_ENABLED_DURATION_MS,
7523 getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7524 proto.write(SystemProto.Misc.LIGHT_DOZE_COUNT,
7525 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
7526 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_DURATION_MS,
7527 getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7528 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_COUNT,
7529 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
7530 proto.write(SystemProto.Misc.LONGEST_LIGHT_DOZE_DURATION_MS,
7531 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
Kweku Adams103351f2017-10-16 14:39:34 -07007532 proto.end(mToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007533
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007534 // Wifi multicast wakelock total stats (WIFI_MULTICAST_WAKELOCK_TOTAL_DATA)
7535 // Calculate multicast wakelock stats across all uids.
7536 long multicastWakeLockTimeTotalUs = 0;
7537 int multicastWakeLockCountTotal = 0;
7538
7539 for (int iu = 0; iu < uidStats.size(); iu++) {
7540 final Uid u = uidStats.valueAt(iu);
7541
7542 final Timer mcTimer = u.getMulticastWakelockStats();
7543
7544 if (mcTimer != null) {
7545 multicastWakeLockTimeTotalUs +=
7546 mcTimer.getTotalTimeLocked(rawRealtimeUs, which);
7547 multicastWakeLockCountTotal += mcTimer.getCountLocked(which);
7548 }
7549 }
7550
7551 final long wmctToken = proto.start(SystemProto.WIFI_MULTICAST_WAKELOCK_TOTAL);
7552 proto.write(SystemProto.WifiMulticastWakelockTotal.DURATION_MS,
7553 multicastWakeLockTimeTotalUs / 1000);
7554 proto.write(SystemProto.WifiMulticastWakelockTotal.COUNT,
7555 multicastWakeLockCountTotal);
7556 proto.end(wmctToken);
7557
Kweku Adams87b19ec2017-10-09 12:40:03 -07007558 // Power use item (POWER_USE_ITEM_DATA)
7559 final List<BatterySipper> sippers = helper.getUsageList();
7560 if (sippers != null) {
7561 for (int i = 0; i < sippers.size(); ++i) {
7562 final BatterySipper bs = sippers.get(i);
7563 int n = SystemProto.PowerUseItem.UNKNOWN_SIPPER;
7564 int uid = 0;
7565 switch (bs.drainType) {
7566 case IDLE:
7567 n = SystemProto.PowerUseItem.IDLE;
7568 break;
7569 case CELL:
7570 n = SystemProto.PowerUseItem.CELL;
7571 break;
7572 case PHONE:
7573 n = SystemProto.PowerUseItem.PHONE;
7574 break;
7575 case WIFI:
7576 n = SystemProto.PowerUseItem.WIFI;
7577 break;
7578 case BLUETOOTH:
7579 n = SystemProto.PowerUseItem.BLUETOOTH;
7580 break;
7581 case SCREEN:
7582 n = SystemProto.PowerUseItem.SCREEN;
7583 break;
7584 case FLASHLIGHT:
7585 n = SystemProto.PowerUseItem.FLASHLIGHT;
7586 break;
7587 case APP:
Kweku Adams103351f2017-10-16 14:39:34 -07007588 // dumpProtoAppsLocked will handle this.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007589 continue;
7590 case USER:
7591 n = SystemProto.PowerUseItem.USER;
7592 uid = UserHandle.getUid(bs.userId, 0);
7593 break;
7594 case UNACCOUNTED:
7595 n = SystemProto.PowerUseItem.UNACCOUNTED;
7596 break;
7597 case OVERCOUNTED:
7598 n = SystemProto.PowerUseItem.OVERCOUNTED;
7599 break;
7600 case CAMERA:
7601 n = SystemProto.PowerUseItem.CAMERA;
7602 break;
7603 case MEMORY:
7604 n = SystemProto.PowerUseItem.MEMORY;
7605 break;
7606 }
Kweku Adams103351f2017-10-16 14:39:34 -07007607 final long puiToken = proto.start(SystemProto.POWER_USE_ITEM);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007608 proto.write(SystemProto.PowerUseItem.NAME, n);
7609 proto.write(SystemProto.PowerUseItem.UID, uid);
7610 proto.write(SystemProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7611 proto.write(SystemProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7612 proto.write(SystemProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7613 proto.write(SystemProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7614 bs.proportionalSmearMah);
Kweku Adams103351f2017-10-16 14:39:34 -07007615 proto.end(puiToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007616 }
7617 }
7618
7619 // Power use summary (POWER_USE_SUMMARY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007620 final long pusToken = proto.start(SystemProto.POWER_USE_SUMMARY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007621 proto.write(SystemProto.PowerUseSummary.BATTERY_CAPACITY_MAH,
7622 helper.getPowerProfile().getBatteryCapacity());
7623 proto.write(SystemProto.PowerUseSummary.COMPUTED_POWER_MAH, helper.getComputedPower());
7624 proto.write(SystemProto.PowerUseSummary.MIN_DRAINED_POWER_MAH, helper.getMinDrainedPower());
7625 proto.write(SystemProto.PowerUseSummary.MAX_DRAINED_POWER_MAH, helper.getMaxDrainedPower());
Kweku Adams103351f2017-10-16 14:39:34 -07007626 proto.end(pusToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007627
7628 // RPM stats (RESOURCE_POWER_MANAGER_DATA)
7629 final Map<String, ? extends Timer> rpmStats = getRpmStats();
7630 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
7631 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007632 final long rpmToken = proto.start(SystemProto.RESOURCE_POWER_MANAGER);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007633 proto.write(SystemProto.ResourcePowerManager.NAME, ent.getKey());
7634 dumpTimer(proto, SystemProto.ResourcePowerManager.TOTAL,
7635 ent.getValue(), rawRealtimeUs, which);
7636 dumpTimer(proto, SystemProto.ResourcePowerManager.SCREEN_OFF,
7637 screenOffRpmStats.get(ent.getKey()), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007638 proto.end(rpmToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007639 }
7640
7641 // Screen brightness (SCREEN_BRIGHTNESS_DATA)
7642 for (int i = 0; i < NUM_SCREEN_BRIGHTNESS_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007643 final long sbToken = proto.start(SystemProto.SCREEN_BRIGHTNESS);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007644 proto.write(SystemProto.ScreenBrightness.NAME, i);
7645 dumpTimer(proto, SystemProto.ScreenBrightness.TOTAL, getScreenBrightnessTimer(i),
7646 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007647 proto.end(sbToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007648 }
7649
7650 // Signal scanning time (SIGNAL_SCANNING_TIME_DATA)
7651 dumpTimer(proto, SystemProto.SIGNAL_SCANNING, getPhoneSignalScanningTimer(), rawRealtimeUs,
7652 which);
7653
7654 // Phone signal strength (SIGNAL_STRENGTH_TIME_DATA and SIGNAL_STRENGTH_COUNT_DATA)
7655 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007656 final long pssToken = proto.start(SystemProto.PHONE_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007657 proto.write(SystemProto.PhoneSignalStrength.NAME, i);
7658 dumpTimer(proto, SystemProto.PhoneSignalStrength.TOTAL, getPhoneSignalStrengthTimer(i),
7659 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007660 proto.end(pssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007661 }
7662
7663 // Wakeup reasons (WAKEUP_REASON_DATA)
7664 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
7665 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007666 final long wrToken = proto.start(SystemProto.WAKEUP_REASON);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007667 proto.write(SystemProto.WakeupReason.NAME, ent.getKey());
7668 dumpTimer(proto, SystemProto.WakeupReason.TOTAL, ent.getValue(), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007669 proto.end(wrToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007670 }
7671
7672 // Wifi signal strength (WIFI_SIGNAL_STRENGTH_TIME_DATA and WIFI_SIGNAL_STRENGTH_COUNT_DATA)
7673 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007674 final long wssToken = proto.start(SystemProto.WIFI_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007675 proto.write(SystemProto.WifiSignalStrength.NAME, i);
7676 dumpTimer(proto, SystemProto.WifiSignalStrength.TOTAL, getWifiSignalStrengthTimer(i),
7677 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007678 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007679 }
7680
7681 // Wifi state (WIFI_STATE_TIME_DATA and WIFI_STATE_COUNT_DATA)
7682 for (int i = 0; i < NUM_WIFI_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007683 final long wsToken = proto.start(SystemProto.WIFI_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007684 proto.write(SystemProto.WifiState.NAME, i);
7685 dumpTimer(proto, SystemProto.WifiState.TOTAL, getWifiStateTimer(i),
7686 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007687 proto.end(wsToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007688 }
7689
7690 // Wifi supplicant state (WIFI_SUPPL_STATE_TIME_DATA and WIFI_SUPPL_STATE_COUNT_DATA)
7691 for (int i = 0; i < NUM_WIFI_SUPPL_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007692 final long wssToken = proto.start(SystemProto.WIFI_SUPPLICANT_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007693 proto.write(SystemProto.WifiSupplicantState.NAME, i);
7694 dumpTimer(proto, SystemProto.WifiSupplicantState.TOTAL, getWifiSupplStateTimer(i),
7695 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007696 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007697 }
7698
7699 proto.end(sToken);
7700 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007701}