blob: 4c35a5c75c47627ce99628b141ad3346784c121d [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.os;
18
Sudheer Shankab2f83c12017-11-13 19:25:01 -080019import android.app.ActivityManager;
Dianne Hackborn94326cb2017-06-28 16:17:20 -070020import android.app.job.JobParameters;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080021import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080022import android.content.pm.ApplicationInfo;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070023import android.service.batterystats.BatteryStatsServiceDumpProto;
Wink Saville52840902011-02-18 12:40:47 -080024import android.telephony.SignalStrength;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080025import android.text.format.DateFormat;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070026import android.util.ArrayMap;
James Carr2dd7e5e2016-07-20 18:48:39 -070027import android.util.LongSparseArray;
Dianne Hackborn9cfba352016-03-24 17:31:28 -070028import android.util.MutableBoolean;
29import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.util.Printer;
31import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070032import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070033import android.util.TimeUtils;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070034import android.util.proto.ProtoOutputStream;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070035import android.view.Display;
Amith Yamasaniab9ad192016-12-06 12:46:59 -080036
Sudheer Shankab2f83c12017-11-13 19:25:01 -080037import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080038import com.android.internal.os.BatterySipper;
39import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Kweku Adams2f73ecd2017-09-27 16:59:19 -070041import java.io.FileDescriptor;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070042import java.io.PrintWriter;
43import java.util.ArrayList;
44import java.util.Collections;
45import java.util.Comparator;
46import java.util.Formatter;
47import java.util.HashMap;
48import java.util.List;
49import java.util.Map;
50
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051/**
52 * A class providing access to battery usage statistics, including information on
53 * wakelocks, processes, packages, and services. All times are represented in microseconds
54 * except where indicated otherwise.
55 * @hide
56 */
57public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070058 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
60 private static final boolean LOCAL_LOGV = false;
Bookatz82b341172017-09-07 19:06:08 -070061 /** Fetching RPM stats is too slow to do each time screen changes, so disable it. */
62 protected static final boolean SCREEN_OFF_RPM_STATS_ENABLED = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070063
64 /** @hide */
65 public static final String SERVICE_NAME = "batterystats";
66
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 /**
68 * A constant indicating a partial wake lock timer.
69 */
70 public static final int WAKE_TYPE_PARTIAL = 0;
71
72 /**
73 * A constant indicating a full wake lock timer.
74 */
75 public static final int WAKE_TYPE_FULL = 1;
76
77 /**
78 * A constant indicating a window wake lock timer.
79 */
80 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 /**
83 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 */
85 public static final int SENSOR = 3;
Mike Mac2f518a2017-09-19 16:06:03 -070086
The Android Open Source Project10592532009-03-18 17:39:46 -070087 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070088 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070089 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070090 public static final int WIFI_RUNNING = 4;
Mike Mac2f518a2017-09-19 16:06:03 -070091
Dianne Hackborn617f8772009-03-31 15:04:46 -070092 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070093 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070094 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070095 public static final int FULL_WIFI_LOCK = 5;
Mike Mac2f518a2017-09-19 16:06:03 -070096
The Android Open Source Project10592532009-03-18 17:39:46 -070097 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070098 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070099 */
Nick Pelly6ccaa542012-06-15 15:22:47 -0700100 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
Dianne Hackborn62793e42015-03-09 11:15:41 -0700102 /**
103 * A constant indicating a wifi multicast timer
104 */
105 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700108 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700109 */
110 public static final int VIDEO_TURNED_ON = 8;
111
112 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800113 * A constant indicating a vibrator on timer
114 */
115 public static final int VIBRATOR_ON = 9;
116
117 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700118 * A constant indicating a foreground activity timer
119 */
120 public static final int FOREGROUND_ACTIVITY = 10;
121
122 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700123 * A constant indicating a wifi batched scan is active
124 */
125 public static final int WIFI_BATCHED_SCAN = 11;
126
127 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700128 * A constant indicating a process state timer
129 */
130 public static final int PROCESS_STATE = 12;
131
132 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700133 * A constant indicating a sync timer
134 */
135 public static final int SYNC = 13;
136
137 /**
138 * A constant indicating a job timer
139 */
140 public static final int JOB = 14;
141
142 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800143 * A constant indicating an audio turn on timer
144 */
145 public static final int AUDIO_TURNED_ON = 15;
146
147 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700148 * A constant indicating a flashlight turn on timer
149 */
150 public static final int FLASHLIGHT_TURNED_ON = 16;
151
152 /**
153 * A constant indicating a camera turn on timer
154 */
155 public static final int CAMERA_TURNED_ON = 17;
156
157 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700158 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700159 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700160 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700161
162 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800163 * A constant indicating a bluetooth scan timer.
164 */
165 public static final int BLUETOOTH_SCAN_ON = 19;
166
167 /**
Bookatzc8c44962017-05-11 12:12:54 -0700168 * A constant indicating an aggregated partial wake lock timer.
169 */
170 public static final int AGGREGATED_WAKE_TYPE_PARTIAL = 20;
171
172 /**
Bookatzb1f04f32017-05-19 13:57:32 -0700173 * A constant indicating a bluetooth scan timer for unoptimized scans.
174 */
175 public static final int BLUETOOTH_UNOPTIMIZED_SCAN_ON = 21;
176
177 /**
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700178 * A constant indicating a foreground service timer
179 */
180 public static final int FOREGROUND_SERVICE = 22;
181
182 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -0800183 * A constant indicating an aggregate wifi multicast timer
184 */
185 public static final int WIFI_AGGREGATE_MULTICAST_ENABLED = 23;
186
187 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 * Include all of the data in the stats, including previously saved data.
189 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700190 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191
192 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * Include only the current run in the stats.
194 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700195 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196
197 /**
198 * Include only the run since the last time the device was unplugged in the stats.
199 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700200 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700201
202 // NOTE: Update this list if you add/change any stats above.
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700203 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700204 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700205 private static final String[] STAT_NAMES = { "l", "c", "u" };
206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700208 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700209 *
210 * New in version 19:
211 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800212 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700213 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Bookatz506a8182017-05-01 14:18:42 -0700214 * New in version 21:
215 * - Actual (not just apportioned) Wakelock time is also recorded.
Bookatzc8c44962017-05-11 12:12:54 -0700216 * - Aggregated partial wakelock time (per uid, instead of per wakelock) is recorded.
Bookatzb1f04f32017-05-19 13:57:32 -0700217 * - BLE scan result count
218 * - CPU frequency time per uid
219 * New in version 22:
220 * - BLE scan result background count, BLE unoptimized scan time
Bookatz98d4d5c2017-08-01 19:07:54 -0700221 * - Background partial wakelock time & count
222 * New in version 23:
223 * - Logging smeared power model values
224 * New in version 24:
225 * - Fixed bugs in background timers and BLE scan time
226 * New in version 25:
227 * - Package wakeup alarms are now on screen-off timebase
Bookatz50df7112017-08-04 14:53:26 -0700228 * New in version 26:
Bookatz82b341172017-09-07 19:06:08 -0700229 * - Resource power manager (rpm) states [but screenOffRpm is disabled from working properly]
Mike Mac2f518a2017-09-19 16:06:03 -0700230 * New in version 27:
231 * - Always On Display (screen doze mode) time and power
Mike Ma15313c92017-11-15 17:58:21 -0800232 * New in version 28:
233 * - Light/Deep Doze power
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700234 * - WiFi Multicast Wakelock statistics (count & duration)
Kweku Adamsa8943cb2017-12-22 13:21:06 -0800235 * New in version 29:
236 * - Process states re-ordered. TOP_SLEEPING now below BACKGROUND. HEAVY_WEIGHT introduced.
237 * - CPU times per UID process state
zhouwenjie46712bc2018-01-11 15:21:27 -0800238 * New in version 30:
239 * - Uid.PROCESS_STATE_FOREGROUND_SERVICE only tracks
240 * ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE.
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700241 */
zhouwenjie46712bc2018-01-11 15:21:27 -0800242 static final int CHECKIN_VERSION = 30;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700243
244 /**
245 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700247 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700248
Evan Millar22ac0432009-03-31 11:33:18 -0700249 private static final long BYTES_PER_KB = 1024;
250 private static final long BYTES_PER_MB = 1048576; // 1024^2
251 private static final long BYTES_PER_GB = 1073741824; //1024^3
Bookatz506a8182017-05-01 14:18:42 -0700252
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700253 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800254 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700255 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700257 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700258 private static final String CPU_DATA = "cpu";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700259 private static final String GLOBAL_CPU_FREQ_DATA = "gcf";
260 private static final String CPU_TIMES_AT_FREQ_DATA = "ctf";
Bookatz50df7112017-08-04 14:53:26 -0700261 // rpm line is:
262 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "rpm", state/voter name, total time, total count,
263 // screen-off time, screen-off count
264 private static final String RESOURCE_POWER_MANAGER_DATA = "rpm";
Evan Millare84de8d2009-04-02 22:16:12 -0700265 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800266 private static final String VIBRATOR_DATA = "vib";
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700267 private static final String FOREGROUND_ACTIVITY_DATA = "fg";
268 // fgs line is:
269 // BATTERY_STATS_CHECKIN_VERSION, uid, category, "fgs",
270 // foreground service time, count
271 private static final String FOREGROUND_SERVICE_DATA = "fgs";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700272 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700273 // wl line is:
274 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
Bookatz5b5ec322017-05-26 09:40:38 -0700275 // full totalTime, 'f', count, current duration, max duration, total duration,
276 // partial totalTime, 'p', count, current duration, max duration, total duration,
277 // bg partial totalTime, 'bp', count, current duration, max duration, total duration,
278 // window totalTime, 'w', count, current duration, max duration, total duration
Bookatz506a8182017-05-01 14:18:42 -0700279 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700280 private static final String WAKELOCK_DATA = "wl";
Bookatzc8c44962017-05-11 12:12:54 -0700281 // awl line is:
282 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "awl",
283 // cumulative partial wakelock duration, cumulative background partial wakelock duration
284 private static final String AGGREGATED_WAKELOCK_DATA = "awl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700285 private static final String SYNC_DATA = "sy";
286 private static final String JOB_DATA = "jb";
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700287 private static final String JOB_COMPLETION_DATA = "jbc";
Evan Millarc64edde2009-04-18 12:26:32 -0700288 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700289 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700290 private static final String NETWORK_DATA = "nt";
291 private static final String USER_ACTIVITY_DATA = "ua";
292 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800293 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700294 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700295 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700296 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800297 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
298 private static final String WIFI_CONTROLLER_DATA = "wfcd";
299 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
300 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700301 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700302 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800303 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800304 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
305 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800306 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700307 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700308 private static final String SCREEN_BRIGHTNESS_DATA = "br";
309 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700310 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700311 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
312 private static final String DATA_CONNECTION_TIME_DATA = "dct";
313 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800314 private static final String WIFI_STATE_TIME_DATA = "wst";
315 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700316 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
317 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
318 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
319 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800320 private static final String POWER_USE_SUMMARY_DATA = "pws";
321 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700322 private static final String DISCHARGE_STEP_DATA = "dsd";
323 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700324 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
325 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700326 private static final String FLASHLIGHT_DATA = "fla";
327 private static final String CAMERA_DATA = "cam";
328 private static final String VIDEO_DATA = "vid";
329 private static final String AUDIO_DATA = "aud";
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700330 private static final String WIFI_MULTICAST_TOTAL_DATA = "wmct";
331 private static final String WIFI_MULTICAST_DATA = "wmc";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332
Adam Lesinski010bf372016-04-11 12:18:18 -0700333 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
334
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700335 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 private final Formatter mFormatter = new Formatter(mFormatBuilder);
337
338 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700339 * Indicates times spent by the uid at each cpu frequency in all process states.
340 *
341 * Other types might include times spent in foreground, background etc.
342 */
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800343 @VisibleForTesting
344 public static final String UID_TIMES_TYPE_ALL = "A";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700345
346 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700347 * State for keeping track of counting information.
348 */
349 public static abstract class Counter {
350
351 /**
352 * Returns the count associated with this Counter for the
353 * selected type of statistics.
354 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700355 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700356 */
Evan Millarc64edde2009-04-18 12:26:32 -0700357 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700358
359 /**
360 * Temporary for debugging.
361 */
362 public abstract void logState(Printer pw, String prefix);
363 }
364
365 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700366 * State for keeping track of long counting information.
367 */
368 public static abstract class LongCounter {
369
370 /**
371 * Returns the count associated with this Counter for the
372 * selected type of statistics.
373 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700374 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700375 */
376 public abstract long getCountLocked(int which);
377
378 /**
379 * Temporary for debugging.
380 */
381 public abstract void logState(Printer pw, String prefix);
382 }
383
384 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700385 * State for keeping track of array of long counting information.
386 */
387 public static abstract class LongCounterArray {
388 /**
389 * Returns the counts associated with this Counter for the
390 * selected type of statistics.
391 *
392 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
393 */
394 public abstract long[] getCountsLocked(int which);
395
396 /**
397 * Temporary for debugging.
398 */
399 public abstract void logState(Printer pw, String prefix);
400 }
401
402 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800403 * Container class that aggregates counters for transmit, receive, and idle state of a
404 * radio controller.
405 */
406 public static abstract class ControllerActivityCounter {
407 /**
408 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
409 * idle state.
410 */
411 public abstract LongCounter getIdleTimeCounter();
412
413 /**
414 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
415 * receive state.
416 */
417 public abstract LongCounter getRxTimeCounter();
418
419 /**
420 * An array of {@link LongCounter}, representing various transmit levels, where each level
421 * may draw a different amount of power. The levels themselves are controller-specific.
422 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
423 * various transmit level states.
424 */
425 public abstract LongCounter[] getTxTimeCounters();
426
427 /**
428 * @return a non-null {@link LongCounter} representing the power consumed by the controller
429 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
430 * yield a value of 0 if the device doesn't support power calculations.
431 */
432 public abstract LongCounter getPowerCounter();
433 }
434
435 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 * State for keeping track of timing information.
437 */
438 public static abstract class Timer {
439
440 /**
441 * Returns the count associated with this Timer for the
442 * selected type of statistics.
443 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700444 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 */
Evan Millarc64edde2009-04-18 12:26:32 -0700446 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447
448 /**
449 * Returns the total time in microseconds associated with this Timer for the
450 * selected type of statistics.
451 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800452 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700453 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 * @return a time in microseconds
455 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800456 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700459 * Returns the total time in microseconds associated with this Timer since the
460 * 'mark' was last set.
461 *
462 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
463 * @return a time in microseconds
464 */
465 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
466
467 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700468 * Returns the max duration if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700469 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700470 */
471 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
472 return -1;
473 }
474
475 /**
476 * Returns the current time the timer has been active, if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700477 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700478 */
479 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
480 return -1;
481 }
482
483 /**
Kweku Adams103351f2017-10-16 14:39:34 -0700484 * Returns the total time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800485 *
486 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
487 * been on since reset.
488 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
489 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
490 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
491 * the actual total time.
Kweku Adams103351f2017-10-16 14:39:34 -0700492 * Not all Timer subclasses track the max, total, and current durations.
Bookatz867c0d72017-03-07 18:23:42 -0800493 */
494 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
495 return -1;
496 }
497
498 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700499 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
500 * used, for example, for tracking background usage. Secondary timers are never pooled.
501 *
502 * Not all Timer subclasses have a secondary timer; those that don't return null.
503 */
504 public Timer getSubTimer() {
505 return null;
506 }
507
508 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700509 * Returns whether the timer is currently running. Some types of timers
510 * (e.g. BatchTimers) don't know whether the event is currently active,
511 * and report false.
512 */
513 public boolean isRunningLocked() {
514 return false;
515 }
516
517 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 * Temporary for debugging.
519 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700520 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 }
522
523 /**
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800524 * Maps the ActivityManager procstate into corresponding BatteryStats procstate.
525 */
526 public static int mapToInternalProcessState(int procState) {
527 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
528 return ActivityManager.PROCESS_STATE_NONEXISTENT;
529 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
530 return Uid.PROCESS_STATE_TOP;
Dianne Hackborn10fc4fd2017-12-19 17:23:13 -0800531 } else if (procState == ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
532 // State when app has put itself in the foreground.
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800533 return Uid.PROCESS_STATE_FOREGROUND_SERVICE;
534 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
535 // Persistent and other foreground states go here.
536 return Uid.PROCESS_STATE_FOREGROUND;
537 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
538 return Uid.PROCESS_STATE_BACKGROUND;
539 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
540 return Uid.PROCESS_STATE_TOP_SLEEPING;
541 } else if (procState <= ActivityManager.PROCESS_STATE_HEAVY_WEIGHT) {
542 return Uid.PROCESS_STATE_HEAVY_WEIGHT;
543 } else {
544 return Uid.PROCESS_STATE_CACHED;
545 }
546 }
547
548 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 * The statistics associated with a particular uid.
550 */
551 public static abstract class Uid {
552
553 /**
554 * Returns a mapping containing wakelock statistics.
555 *
556 * @return a Map from Strings to Uid.Wakelock objects.
557 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700558 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559
560 /**
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700561 * Returns the WiFi Multicast Wakelock statistics.
562 *
563 * @return a Timer Object for the per uid Multicast statistics.
564 */
565 public abstract Timer getMulticastWakelockStats();
566
567 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700568 * Returns a mapping containing sync statistics.
569 *
570 * @return a Map from Strings to Timer objects.
571 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700572 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700573
574 /**
575 * Returns a mapping containing scheduled job statistics.
576 *
577 * @return a Map from Strings to Timer objects.
578 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700579 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700580
581 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700582 * Returns statistics about how jobs have completed.
583 *
584 * @return A Map of String job names to completion type -> count mapping.
585 */
586 public abstract ArrayMap<String, SparseIntArray> getJobCompletionStats();
587
588 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 * The statistics associated with a particular wake lock.
590 */
591 public static abstract class Wakelock {
592 public abstract Timer getWakeTime(int type);
593 }
594
595 /**
Bookatzc8c44962017-05-11 12:12:54 -0700596 * The cumulative time the uid spent holding any partial wakelocks. This will generally
597 * differ from summing over the Wakelocks in getWakelockStats since the latter may have
598 * wakelocks that overlap in time (and therefore over-counts).
599 */
600 public abstract Timer getAggregatedPartialWakelockTimer();
601
602 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 * Returns a mapping containing sensor statistics.
604 *
605 * @return a Map from Integer sensor ids to Uid.Sensor objects.
606 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700607 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608
609 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700610 * Returns a mapping containing active process data.
611 */
612 public abstract SparseArray<? extends Pid> getPidStats();
Bookatzc8c44962017-05-11 12:12:54 -0700613
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700614 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 * Returns a mapping containing process statistics.
616 *
617 * @return a Map from Strings to Uid.Proc objects.
618 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700619 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620
621 /**
622 * Returns a mapping containing package statistics.
623 *
624 * @return a Map from Strings to Uid.Pkg objects.
625 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700626 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700627
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800628 public abstract ControllerActivityCounter getWifiControllerActivity();
629 public abstract ControllerActivityCounter getBluetoothControllerActivity();
630 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800631
632 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 * {@hide}
634 */
635 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700636
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800637 public abstract void noteWifiRunningLocked(long elapsedRealtime);
638 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
639 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
640 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
641 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
642 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
643 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
644 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
645 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
646 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800647 public abstract void noteActivityResumedLocked(long elapsedRealtime);
648 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800649 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
650 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
651 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700652 public abstract int getWifiScanCount(int which);
Kweku Adams103351f2017-10-16 14:39:34 -0700653 /**
654 * Returns the timer keeping track of wifi scans.
655 */
656 public abstract Timer getWifiScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800657 public abstract int getWifiScanBackgroundCount(int which);
658 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
659 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -0700660 /**
661 * Returns the timer keeping track of background wifi scans.
662 */
663 public abstract Timer getWifiScanBackgroundTimer();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800664 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700665 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800666 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700667 public abstract Timer getAudioTurnedOnTimer();
668 public abstract Timer getVideoTurnedOnTimer();
669 public abstract Timer getFlashlightTurnedOnTimer();
670 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700671 public abstract Timer getForegroundActivityTimer();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700672
673 /**
674 * Returns the timer keeping track of Foreground Service time
675 */
676 public abstract Timer getForegroundServiceTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800677 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800678 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatzb1f04f32017-05-19 13:57:32 -0700679 public abstract Timer getBluetoothUnoptimizedScanTimer();
680 public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700681 public abstract Counter getBluetoothScanResultCounter();
Bookatzb1f04f32017-05-19 13:57:32 -0700682 public abstract Counter getBluetoothScanResultBgCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700683
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700684 public abstract long[] getCpuFreqTimes(int which);
685 public abstract long[] getScreenOffCpuFreqTimes(int which);
Mike Ma3d422c32017-10-25 11:08:57 -0700686 /**
687 * Returns cpu active time of an uid.
688 */
689 public abstract long getCpuActiveTime();
690 /**
691 * Returns cpu times of an uid on each cluster
692 */
693 public abstract long[] getCpuClusterTimes();
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700694
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800695 /**
696 * Returns cpu times of an uid at a particular process state.
697 */
698 public abstract long[] getCpuFreqTimes(int which, int procState);
699 /**
700 * Returns cpu times of an uid while the screen if off at a particular process state.
701 */
702 public abstract long[] getScreenOffCpuFreqTimes(int which, int procState);
703
Dianne Hackborna0200e32016-03-30 18:01:41 -0700704 // Note: the following times are disjoint. They can be added together to find the
705 // total time a uid has had any processes running at all.
706
707 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800708 * Time this uid has any processes in the top state.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700709 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800710 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700711 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800712 * Time this uid has any process with a started foreground service, but
Dianne Hackborna0200e32016-03-30 18:01:41 -0700713 * none in the "top" state.
714 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800715 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700716 /**
Dianne Hackborna0200e32016-03-30 18:01:41 -0700717 * Time this uid has any process in an active foreground state, but none in the
zhouwenjie46712bc2018-01-11 15:21:27 -0800718 * "foreground service" or better state. Persistent and other foreground states go here.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700719 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800720 public static final int PROCESS_STATE_FOREGROUND = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700721 /**
722 * Time this uid has any process in an active background state, but none in the
723 * "foreground" or better state.
724 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800725 public static final int PROCESS_STATE_BACKGROUND = 3;
726 /**
727 * Time this uid has any process that is top while the device is sleeping, but not
728 * active for any other reason. We kind-of consider it a kind of cached process
729 * for execution restrictions.
730 */
731 public static final int PROCESS_STATE_TOP_SLEEPING = 4;
732 /**
733 * Time this uid has any process that is in the background but it has an activity
734 * marked as "can't save state". This is essentially a cached process, though the
735 * system will try much harder than normal to avoid killing it.
736 */
737 public static final int PROCESS_STATE_HEAVY_WEIGHT = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700738 /**
739 * Time this uid has any processes that are sitting around cached, not in one of the
740 * other active states.
741 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800742 public static final int PROCESS_STATE_CACHED = 6;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700743 /**
744 * Total number of process states we track.
745 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800746 public static final int NUM_PROCESS_STATE = 7;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700747
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800748 // Used in dump
Dianne Hackborn61659e52014-07-09 16:13:01 -0700749 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800750 "Top", "Fg Service", "Foreground", "Background", "Top Sleeping", "Heavy Weight",
751 "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700752 };
753
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800754 // Used in checkin dump
755 @VisibleForTesting
756 public static final String[] UID_PROCESS_TYPES = {
757 "T", // TOP
758 "FS", // FOREGROUND_SERVICE
759 "F", // FOREGROUND
760 "B", // BACKGROUND
761 "TS", // TOP_SLEEPING
762 "HW", // HEAVY_WEIGHT
763 "C" // CACHED
764 };
765
766 /**
767 * When the process exits one of these states, we need to make sure cpu time in this state
768 * is not attributed to any non-critical process states.
769 */
770 public static final int[] CRITICAL_PROC_STATES = {
771 PROCESS_STATE_TOP, PROCESS_STATE_FOREGROUND_SERVICE, PROCESS_STATE_FOREGROUND
772 };
773
Dianne Hackborn61659e52014-07-09 16:13:01 -0700774 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800775 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700776
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800777 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778
Robert Greenwalta029ea12013-09-25 16:38:12 -0700779 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
780
Dianne Hackborn617f8772009-03-31 15:04:46 -0700781 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700782 * Note that these must match the constants in android.os.PowerManager.
783 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
784 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700785 */
786 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700787 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700788 };
Bookatzc8c44962017-05-11 12:12:54 -0700789
Phil Weaverda80d672016-03-15 16:25:46 -0700790 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700791
Dianne Hackborn617f8772009-03-31 15:04:46 -0700792 public abstract void noteUserActivityLocked(int type);
793 public abstract boolean hasUserActivity();
794 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700795
796 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800797 public abstract long getNetworkActivityBytes(int type, int which);
798 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800799 public abstract long getMobileRadioActiveTime(int which);
800 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700801
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700802 /**
803 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
804 */
805 public abstract long getUserCpuTimeUs(int which);
806
807 /**
808 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
809 */
810 public abstract long getSystemCpuTimeUs(int which);
811
812 /**
Sudheer Shanka71f34b32017-07-21 00:14:24 -0700813 * Returns the approximate cpu time (in microseconds) spent at a certain CPU speed for a
Adam Lesinski6832f392015-09-05 18:05:40 -0700814 * given CPU cluster.
815 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700816 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700817 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700818 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
819 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700820 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700821 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700822
Adam Lesinski5f056f62016-07-14 16:56:08 -0700823 /**
824 * Returns the number of times this UID woke up the Application Processor to
825 * process a mobile radio packet.
826 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
827 */
828 public abstract long getMobileRadioApWakeupCount(int which);
829
830 /**
831 * Returns the number of times this UID woke up the Application Processor to
832 * process a WiFi packet.
833 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
834 */
835 public abstract long getWifiRadioApWakeupCount(int which);
836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800838 /*
839 * FIXME: it's not correct to use this magic value because it
840 * could clash with a sensor handle (which are defined by
841 * the sensor HAL, and therefore out of our control
842 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 // Magic sensor number for the GPS.
844 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800849
Bookatz867c0d72017-03-07 18:23:42 -0800850 /** Returns a Timer for sensor usage when app is in the background. */
851 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 }
853
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700854 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800855 public int mWakeNesting;
856 public long mWakeSumMs;
857 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700858 }
859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 /**
861 * The statistics associated with a particular process.
862 */
863 public static abstract class Proc {
864
Dianne Hackborn287952c2010-09-22 22:34:31 -0700865 public static class ExcessivePower {
866 public static final int TYPE_WAKE = 1;
867 public static final int TYPE_CPU = 2;
868
869 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700870 public long overTime;
871 public long usedTime;
872 }
873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800875 * Returns true if this process is still active in the battery stats.
876 */
877 public abstract boolean isActive();
878
879 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700880 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700882 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 */
884 public abstract long getUserTime(int which);
885
886 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700887 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700889 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 */
891 public abstract long getSystemTime(int which);
892
893 /**
894 * Returns the number of times the process has been started.
895 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700896 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 */
898 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700899
900 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800901 * Returns the number of times the process has crashed.
902 *
903 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
904 */
905 public abstract int getNumCrashes(int which);
906
907 /**
908 * Returns the number of times the process has ANRed.
909 *
910 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
911 */
912 public abstract int getNumAnrs(int which);
913
914 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700915 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700916 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700917 * @return foreground cpu time in microseconds
918 */
919 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700920
Dianne Hackborn287952c2010-09-22 22:34:31 -0700921 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700922
Dianne Hackborn287952c2010-09-22 22:34:31 -0700923 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 }
925
926 /**
927 * The statistics associated with a particular package.
928 */
929 public static abstract class Pkg {
930
931 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700932 * Returns information about all wakeup alarms that have been triggered for this
933 * package. The mapping keys are tag names for the alarms, the counter contains
934 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700936 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937
938 /**
939 * Returns a mapping containing service statistics.
940 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700941 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942
943 /**
944 * The statistics associated with a particular service.
945 */
Joe Onoratoabded112016-02-08 16:49:39 -0800946 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947
948 /**
949 * Returns the amount of time spent started.
950 *
951 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700952 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 * @return
954 */
955 public abstract long getStartTime(long batteryUptime, int which);
956
957 /**
958 * Returns the total number of times startService() has been called.
959 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700960 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 */
962 public abstract int getStarts(int which);
963
964 /**
965 * Returns the total number times the service has been launched.
966 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700967 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 */
969 public abstract int getLaunches(int which);
970 }
971 }
972 }
973
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800974 public static final class LevelStepTracker {
975 public long mLastStepTime = -1;
976 public int mNumStepDurations;
977 public final long[] mStepDurations;
978
979 public LevelStepTracker(int maxLevelSteps) {
980 mStepDurations = new long[maxLevelSteps];
981 }
982
983 public LevelStepTracker(int numSteps, long[] steps) {
984 mNumStepDurations = numSteps;
985 mStepDurations = new long[numSteps];
986 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
987 }
988
989 public long getDurationAt(int index) {
990 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
991 }
992
993 public int getLevelAt(int index) {
994 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
995 >> STEP_LEVEL_LEVEL_SHIFT);
996 }
997
998 public int getInitModeAt(int index) {
999 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
1000 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1001 }
1002
1003 public int getModModeAt(int index) {
1004 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
1005 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1006 }
1007
1008 private void appendHex(long val, int topOffset, StringBuilder out) {
1009 boolean hasData = false;
1010 while (topOffset >= 0) {
1011 int digit = (int)( (val>>topOffset) & 0xf );
1012 topOffset -= 4;
1013 if (!hasData && digit == 0) {
1014 continue;
1015 }
1016 hasData = true;
1017 if (digit >= 0 && digit <= 9) {
1018 out.append((char)('0' + digit));
1019 } else {
1020 out.append((char)('a' + digit - 10));
1021 }
1022 }
1023 }
1024
1025 public void encodeEntryAt(int index, StringBuilder out) {
1026 long item = mStepDurations[index];
1027 long duration = item & STEP_LEVEL_TIME_MASK;
1028 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
1029 >> STEP_LEVEL_LEVEL_SHIFT);
1030 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
1031 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1032 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
1033 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1034 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1035 case Display.STATE_OFF: out.append('f'); break;
1036 case Display.STATE_ON: out.append('o'); break;
1037 case Display.STATE_DOZE: out.append('d'); break;
1038 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
1039 }
1040 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1041 out.append('p');
1042 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001043 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1044 out.append('i');
1045 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001046 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1047 case Display.STATE_OFF: out.append('F'); break;
1048 case Display.STATE_ON: out.append('O'); break;
1049 case Display.STATE_DOZE: out.append('D'); break;
1050 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
1051 }
1052 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1053 out.append('P');
1054 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001055 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1056 out.append('I');
1057 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001058 out.append('-');
1059 appendHex(level, 4, out);
1060 out.append('-');
1061 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
1062 }
1063
1064 public void decodeEntryAt(int index, String value) {
1065 final int N = value.length();
1066 int i = 0;
1067 char c;
1068 long out = 0;
1069 while (i < N && (c=value.charAt(i)) != '-') {
1070 i++;
1071 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001072 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001073 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001074 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001075 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001076 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001077 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001078 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1079 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1080 break;
1081 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1082 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1083 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001084 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1085 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1086 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001087 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1088 break;
1089 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1090 break;
1091 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1092 break;
1093 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1094 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1095 break;
1096 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1097 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001098 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001099 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1100 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1101 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001102 }
1103 }
1104 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001105 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001106 while (i < N && (c=value.charAt(i)) != '-') {
1107 i++;
1108 level <<= 4;
1109 if (c >= '0' && c <= '9') {
1110 level += c - '0';
1111 } else if (c >= 'a' && c <= 'f') {
1112 level += c - 'a' + 10;
1113 } else if (c >= 'A' && c <= 'F') {
1114 level += c - 'A' + 10;
1115 }
1116 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001117 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001118 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
1119 long duration = 0;
1120 while (i < N && (c=value.charAt(i)) != '-') {
1121 i++;
1122 duration <<= 4;
1123 if (c >= '0' && c <= '9') {
1124 duration += c - '0';
1125 } else if (c >= 'a' && c <= 'f') {
1126 duration += c - 'a' + 10;
1127 } else if (c >= 'A' && c <= 'F') {
1128 duration += c - 'A' + 10;
1129 }
1130 }
1131 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
1132 }
1133
1134 public void init() {
1135 mLastStepTime = -1;
1136 mNumStepDurations = 0;
1137 }
1138
1139 public void clearTime() {
1140 mLastStepTime = -1;
1141 }
1142
1143 public long computeTimePerLevel() {
1144 final long[] steps = mStepDurations;
1145 final int numSteps = mNumStepDurations;
1146
1147 // For now we'll do a simple average across all steps.
1148 if (numSteps <= 0) {
1149 return -1;
1150 }
1151 long total = 0;
1152 for (int i=0; i<numSteps; i++) {
1153 total += steps[i] & STEP_LEVEL_TIME_MASK;
1154 }
1155 return total / numSteps;
1156 /*
1157 long[] buckets = new long[numSteps];
1158 int numBuckets = 0;
1159 int numToAverage = 4;
1160 int i = 0;
1161 while (i < numSteps) {
1162 long totalTime = 0;
1163 int num = 0;
1164 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1165 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1166 num++;
1167 }
1168 buckets[numBuckets] = totalTime / num;
1169 numBuckets++;
1170 numToAverage *= 2;
1171 i += num;
1172 }
1173 if (numBuckets < 1) {
1174 return -1;
1175 }
1176 long averageTime = buckets[numBuckets-1];
1177 for (i=numBuckets-2; i>=0; i--) {
1178 averageTime = (averageTime + buckets[i]) / 2;
1179 }
1180 return averageTime;
1181 */
1182 }
1183
1184 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1185 int[] outNumOfInterest) {
1186 final long[] steps = mStepDurations;
1187 final int count = mNumStepDurations;
1188 if (count <= 0) {
1189 return -1;
1190 }
1191 long total = 0;
1192 int numOfInterest = 0;
1193 for (int i=0; i<count; i++) {
1194 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1195 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1196 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1197 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1198 // If the modes of interest didn't change during this step period...
1199 if ((modMode&modesOfInterest) == 0) {
1200 // And the mode values during this period match those we are measuring...
1201 if ((initMode&modesOfInterest) == modeValues) {
1202 // Then this can be used to estimate the total time!
1203 numOfInterest++;
1204 total += steps[i] & STEP_LEVEL_TIME_MASK;
1205 }
1206 }
1207 }
1208 if (numOfInterest <= 0) {
1209 return -1;
1210 }
1211
1212 if (outNumOfInterest != null) {
1213 outNumOfInterest[0] = numOfInterest;
1214 }
1215
1216 // The estimated time is the average time we spend in each level, multipled
1217 // by 100 -- the total number of battery levels
1218 return (total / numOfInterest) * 100;
1219 }
1220
1221 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1222 int stepCount = mNumStepDurations;
1223 final long lastStepTime = mLastStepTime;
1224 if (lastStepTime >= 0 && numStepLevels > 0) {
1225 final long[] steps = mStepDurations;
1226 long duration = elapsedRealtime - lastStepTime;
1227 for (int i=0; i<numStepLevels; i++) {
1228 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1229 long thisDuration = duration / (numStepLevels-i);
1230 duration -= thisDuration;
1231 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1232 thisDuration = STEP_LEVEL_TIME_MASK;
1233 }
1234 steps[0] = thisDuration | modeBits;
1235 }
1236 stepCount += numStepLevels;
1237 if (stepCount > steps.length) {
1238 stepCount = steps.length;
1239 }
1240 }
1241 mNumStepDurations = stepCount;
1242 mLastStepTime = elapsedRealtime;
1243 }
1244
1245 public void readFromParcel(Parcel in) {
1246 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001247 if (N > mStepDurations.length) {
1248 throw new ParcelFormatException("more step durations than available: " + N);
1249 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001250 mNumStepDurations = N;
1251 for (int i=0; i<N; i++) {
1252 mStepDurations[i] = in.readLong();
1253 }
1254 }
1255
1256 public void writeToParcel(Parcel out) {
1257 final int N = mNumStepDurations;
1258 out.writeInt(N);
1259 for (int i=0; i<N; i++) {
1260 out.writeLong(mStepDurations[i]);
1261 }
1262 }
1263 }
1264
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001265 public static final class PackageChange {
1266 public String mPackageName;
1267 public boolean mUpdate;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001268 public long mVersionCode;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001269 }
1270
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001271 public static final class DailyItem {
1272 public long mStartTime;
1273 public long mEndTime;
1274 public LevelStepTracker mDischargeSteps;
1275 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001276 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001277 }
1278
1279 public abstract DailyItem getDailyItemLocked(int daysAgo);
1280
1281 public abstract long getCurrentDailyStartTime();
1282
1283 public abstract long getNextMinDailyDeadline();
1284
1285 public abstract long getNextMaxDailyDeadline();
1286
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001287 public abstract long[] getCpuFreqs();
1288
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001289 public final static class HistoryTag {
1290 public String string;
1291 public int uid;
1292
1293 public int poolIdx;
1294
1295 public void setTo(HistoryTag o) {
1296 string = o.string;
1297 uid = o.uid;
1298 poolIdx = o.poolIdx;
1299 }
1300
1301 public void setTo(String _string, int _uid) {
1302 string = _string;
1303 uid = _uid;
1304 poolIdx = -1;
1305 }
1306
1307 public void writeToParcel(Parcel dest, int flags) {
1308 dest.writeString(string);
1309 dest.writeInt(uid);
1310 }
1311
1312 public void readFromParcel(Parcel src) {
1313 string = src.readString();
1314 uid = src.readInt();
1315 poolIdx = -1;
1316 }
1317
1318 @Override
1319 public boolean equals(Object o) {
1320 if (this == o) return true;
1321 if (o == null || getClass() != o.getClass()) return false;
1322
1323 HistoryTag that = (HistoryTag) o;
1324
1325 if (uid != that.uid) return false;
1326 if (!string.equals(that.string)) return false;
1327
1328 return true;
1329 }
1330
1331 @Override
1332 public int hashCode() {
1333 int result = string.hashCode();
1334 result = 31 * result + uid;
1335 return result;
1336 }
1337 }
1338
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001339 /**
1340 * Optional detailed information that can go into a history step. This is typically
1341 * generated each time the battery level changes.
1342 */
1343 public final static class HistoryStepDetails {
1344 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1345 public int userTime;
1346 public int systemTime;
1347
1348 // Top three apps using CPU in the last step, with times in 1/100 second.
1349 public int appCpuUid1;
1350 public int appCpuUTime1;
1351 public int appCpuSTime1;
1352 public int appCpuUid2;
1353 public int appCpuUTime2;
1354 public int appCpuSTime2;
1355 public int appCpuUid3;
1356 public int appCpuUTime3;
1357 public int appCpuSTime3;
1358
1359 // Information from /proc/stat
1360 public int statUserTime;
1361 public int statSystemTime;
1362 public int statIOWaitTime;
1363 public int statIrqTime;
1364 public int statSoftIrqTime;
1365 public int statIdlTime;
1366
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001367 // Platform-level low power state stats
1368 public String statPlatformIdleState;
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001369 public String statSubsystemPowerState;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001370
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001371 public HistoryStepDetails() {
1372 clear();
1373 }
1374
1375 public void clear() {
1376 userTime = systemTime = 0;
1377 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1378 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1379 = appCpuUTime3 = appCpuSTime3 = 0;
1380 }
1381
1382 public void writeToParcel(Parcel out) {
1383 out.writeInt(userTime);
1384 out.writeInt(systemTime);
1385 out.writeInt(appCpuUid1);
1386 out.writeInt(appCpuUTime1);
1387 out.writeInt(appCpuSTime1);
1388 out.writeInt(appCpuUid2);
1389 out.writeInt(appCpuUTime2);
1390 out.writeInt(appCpuSTime2);
1391 out.writeInt(appCpuUid3);
1392 out.writeInt(appCpuUTime3);
1393 out.writeInt(appCpuSTime3);
1394 out.writeInt(statUserTime);
1395 out.writeInt(statSystemTime);
1396 out.writeInt(statIOWaitTime);
1397 out.writeInt(statIrqTime);
1398 out.writeInt(statSoftIrqTime);
1399 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001400 out.writeString(statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001401 out.writeString(statSubsystemPowerState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001402 }
1403
1404 public void readFromParcel(Parcel in) {
1405 userTime = in.readInt();
1406 systemTime = in.readInt();
1407 appCpuUid1 = in.readInt();
1408 appCpuUTime1 = in.readInt();
1409 appCpuSTime1 = in.readInt();
1410 appCpuUid2 = in.readInt();
1411 appCpuUTime2 = in.readInt();
1412 appCpuSTime2 = in.readInt();
1413 appCpuUid3 = in.readInt();
1414 appCpuUTime3 = in.readInt();
1415 appCpuSTime3 = in.readInt();
1416 statUserTime = in.readInt();
1417 statSystemTime = in.readInt();
1418 statIOWaitTime = in.readInt();
1419 statIrqTime = in.readInt();
1420 statSoftIrqTime = in.readInt();
1421 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001422 statPlatformIdleState = in.readString();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001423 statSubsystemPowerState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001424 }
1425 }
1426
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001427 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001428 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001429
1430 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001431 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001432
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001433 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001434 public static final byte CMD_NULL = -1;
1435 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001436 public static final byte CMD_CURRENT_TIME = 5;
1437 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001438 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001439 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001440
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001441 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001442
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001443 /**
1444 * Return whether the command code is a delta data update.
1445 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001446 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001447 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001448 }
1449
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001450 public byte batteryLevel;
1451 public byte batteryStatus;
1452 public byte batteryHealth;
1453 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001454
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001455 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001456 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001457
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001458 // The charge of the battery in micro-Ampere-hours.
1459 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001460
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001461 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001462 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001463 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001464 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001465 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1466 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001467 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001468 public static final int STATE_PHONE_STATE_SHIFT = 6;
1469 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001470 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001471 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001472 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001473
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001474 // These states always appear directly in the first int token
1475 // of a delta change; they should be ones that change relatively
1476 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001477 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1478 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001479 public static final int STATE_GPS_ON_FLAG = 1<<29;
1480 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001481 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001482 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001483 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001484 // Do not use, this is used for coulomb delta count.
1485 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001486 // These are on the lower bits used for the command; if they change
1487 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001488 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001489 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001490 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001491 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1492 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
Mike Mac2f518a2017-09-19 16:06:03 -07001493 public static final int STATE_SCREEN_DOZE_FLAG = 1 << 18;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001494 // empty slot
1495 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001496
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001497 public static final int MOST_INTERESTING_STATES =
Mike Mac2f518a2017-09-19 16:06:03 -07001498 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG | STATE_SCREEN_DOZE_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001499
1500 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001501
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001502 public int states;
1503
Dianne Hackborn3251b902014-06-20 14:40:53 -07001504 // Constants from WIFI_SUPPL_STATE_*
1505 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1506 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1507 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1508 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1509 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1510 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1511
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001512 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001513 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1514 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1515 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001516 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001517 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1518 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1519 public static final int STATE2_CHARGING_FLAG = 1<<24;
1520 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1521 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1522 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001523 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001524
1525 public static final int MOST_INTERESTING_STATES2 =
Mike Mac2f518a2017-09-19 16:06:03 -07001526 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
1527 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001528
1529 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001530
Dianne Hackborn40c87252014-03-19 16:55:40 -07001531 public int states2;
1532
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001533 // The wake lock that was acquired at this point.
1534 public HistoryTag wakelockTag;
1535
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001536 // Kernel wakeup reason at this point.
1537 public HistoryTag wakeReasonTag;
1538
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001539 // Non-null when there is more detailed information at this step.
1540 public HistoryStepDetails stepDetails;
1541
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001542 public static final int EVENT_FLAG_START = 0x8000;
1543 public static final int EVENT_FLAG_FINISH = 0x4000;
1544
1545 // No event in this item.
1546 public static final int EVENT_NONE = 0x0000;
1547 // Event is about a process that is running.
1548 public static final int EVENT_PROC = 0x0001;
1549 // Event is about an application package that is in the foreground.
1550 public static final int EVENT_FOREGROUND = 0x0002;
1551 // Event is about an application package that is at the top of the screen.
1552 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001553 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001554 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001555 // Events for all additional wake locks aquired/release within a wake block.
1556 // These are not generated by default.
1557 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001558 // Event is about an application executing a scheduled job.
1559 public static final int EVENT_JOB = 0x0006;
1560 // Events for users running.
1561 public static final int EVENT_USER_RUNNING = 0x0007;
1562 // Events for foreground user.
1563 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001564 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001565 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001566 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001567 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001568 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001569 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001570 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001571 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001572 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001573 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001574 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001575 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001576 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001577 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001578 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001579 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001580 // Event for a package being on the temporary whitelist.
1581 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001582 // Event for the screen waking up.
1583 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001584 // Event for the UID that woke up the application processor.
1585 // Used for wakeups coming from WiFi, modem, etc.
1586 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001587 // Event for reporting that a specific partial wake lock has been held for a long duration.
1588 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001589
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001590 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001591 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001592 // Mask to extract out only the type part of the event.
1593 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001594
1595 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1596 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1597 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1598 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1599 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1600 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001601 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1602 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001603 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1604 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001605 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1606 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1607 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1608 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1609 public static final int EVENT_USER_FOREGROUND_START =
1610 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1611 public static final int EVENT_USER_FOREGROUND_FINISH =
1612 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001613 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1614 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001615 public static final int EVENT_TEMP_WHITELIST_START =
1616 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1617 public static final int EVENT_TEMP_WHITELIST_FINISH =
1618 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001619 public static final int EVENT_LONG_WAKE_LOCK_START =
1620 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1621 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1622 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001623
1624 // For CMD_EVENT.
1625 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001626 public HistoryTag eventTag;
1627
Dianne Hackborn9a755432014-05-15 17:05:22 -07001628 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001629 public long currentTime;
1630
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001631 // Meta-data when reading.
1632 public int numReadInts;
1633
1634 // Pre-allocated objects.
1635 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001636 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001637 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001638
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001639 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001640 }
Bookatzc8c44962017-05-11 12:12:54 -07001641
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001642 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001643 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001644 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001645 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001646 }
Bookatzc8c44962017-05-11 12:12:54 -07001647
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001648 public int describeContents() {
1649 return 0;
1650 }
1651
1652 public void writeToParcel(Parcel dest, int flags) {
1653 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001654 int bat = (((int)cmd)&0xff)
1655 | ((((int)batteryLevel)<<8)&0xff00)
1656 | ((((int)batteryStatus)<<16)&0xf0000)
1657 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001658 | ((((int)batteryPlugType)<<24)&0xf000000)
1659 | (wakelockTag != null ? 0x10000000 : 0)
1660 | (wakeReasonTag != null ? 0x20000000 : 0)
1661 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001662 dest.writeInt(bat);
1663 bat = (((int)batteryTemperature)&0xffff)
1664 | ((((int)batteryVoltage)<<16)&0xffff0000);
1665 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001666 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001667 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001668 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001669 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001670 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001671 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001672 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001673 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001674 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001675 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001676 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001677 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001678 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001679 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001680 dest.writeLong(currentTime);
1681 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001682 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001683
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001684 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001685 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001686 int bat = src.readInt();
1687 cmd = (byte)(bat&0xff);
1688 batteryLevel = (byte)((bat>>8)&0xff);
1689 batteryStatus = (byte)((bat>>16)&0xf);
1690 batteryHealth = (byte)((bat>>20)&0xf);
1691 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001692 int bat2 = src.readInt();
1693 batteryTemperature = (short)(bat2&0xffff);
1694 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001695 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001696 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001697 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001698 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001699 wakelockTag = localWakelockTag;
1700 wakelockTag.readFromParcel(src);
1701 } else {
1702 wakelockTag = null;
1703 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001704 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001705 wakeReasonTag = localWakeReasonTag;
1706 wakeReasonTag.readFromParcel(src);
1707 } else {
1708 wakeReasonTag = null;
1709 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001710 if ((bat&0x40000000) != 0) {
1711 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001712 eventTag = localEventTag;
1713 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001714 } else {
1715 eventCode = EVENT_NONE;
1716 eventTag = null;
1717 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001718 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001719 currentTime = src.readLong();
1720 } else {
1721 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001722 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001723 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001724 }
1725
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001726 public void clear() {
1727 time = 0;
1728 cmd = CMD_NULL;
1729 batteryLevel = 0;
1730 batteryStatus = 0;
1731 batteryHealth = 0;
1732 batteryPlugType = 0;
1733 batteryTemperature = 0;
1734 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001735 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001736 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001737 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001738 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001739 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001740 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001741 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001742 }
Bookatzc8c44962017-05-11 12:12:54 -07001743
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001744 public void setTo(HistoryItem o) {
1745 time = o.time;
1746 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001747 setToCommon(o);
1748 }
1749
1750 public void setTo(long time, byte cmd, HistoryItem o) {
1751 this.time = time;
1752 this.cmd = cmd;
1753 setToCommon(o);
1754 }
1755
1756 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001757 batteryLevel = o.batteryLevel;
1758 batteryStatus = o.batteryStatus;
1759 batteryHealth = o.batteryHealth;
1760 batteryPlugType = o.batteryPlugType;
1761 batteryTemperature = o.batteryTemperature;
1762 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001763 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001764 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001765 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001766 if (o.wakelockTag != null) {
1767 wakelockTag = localWakelockTag;
1768 wakelockTag.setTo(o.wakelockTag);
1769 } else {
1770 wakelockTag = null;
1771 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001772 if (o.wakeReasonTag != null) {
1773 wakeReasonTag = localWakeReasonTag;
1774 wakeReasonTag.setTo(o.wakeReasonTag);
1775 } else {
1776 wakeReasonTag = null;
1777 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001778 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001779 if (o.eventTag != null) {
1780 eventTag = localEventTag;
1781 eventTag.setTo(o.eventTag);
1782 } else {
1783 eventTag = null;
1784 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001785 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001786 }
1787
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001788 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001789 return batteryLevel == o.batteryLevel
1790 && batteryStatus == o.batteryStatus
1791 && batteryHealth == o.batteryHealth
1792 && batteryPlugType == o.batteryPlugType
1793 && batteryTemperature == o.batteryTemperature
1794 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001795 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001796 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001797 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001798 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001799 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001800
1801 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001802 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001803 return false;
1804 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001805 if (wakelockTag != o.wakelockTag) {
1806 if (wakelockTag == null || o.wakelockTag == null) {
1807 return false;
1808 }
1809 if (!wakelockTag.equals(o.wakelockTag)) {
1810 return false;
1811 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001812 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001813 if (wakeReasonTag != o.wakeReasonTag) {
1814 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1815 return false;
1816 }
1817 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1818 return false;
1819 }
1820 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001821 if (eventTag != o.eventTag) {
1822 if (eventTag == null || o.eventTag == null) {
1823 return false;
1824 }
1825 if (!eventTag.equals(o.eventTag)) {
1826 return false;
1827 }
1828 }
1829 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001830 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001831 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001832
1833 public final static class HistoryEventTracker {
1834 private final HashMap<String, SparseIntArray>[] mActiveEvents
1835 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1836
1837 public boolean updateState(int code, String name, int uid, int poolIdx) {
1838 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1839 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1840 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1841 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001842 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001843 mActiveEvents[idx] = active;
1844 }
1845 SparseIntArray uids = active.get(name);
1846 if (uids == null) {
1847 uids = new SparseIntArray();
1848 active.put(name, uids);
1849 }
1850 if (uids.indexOfKey(uid) >= 0) {
1851 // Already set, nothing to do!
1852 return false;
1853 }
1854 uids.put(uid, poolIdx);
1855 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1856 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1857 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1858 if (active == null) {
1859 // not currently active, nothing to do.
1860 return false;
1861 }
1862 SparseIntArray uids = active.get(name);
1863 if (uids == null) {
1864 // not currently active, nothing to do.
1865 return false;
1866 }
1867 idx = uids.indexOfKey(uid);
1868 if (idx < 0) {
1869 // not currently active, nothing to do.
1870 return false;
1871 }
1872 uids.removeAt(idx);
1873 if (uids.size() <= 0) {
1874 active.remove(name);
1875 }
1876 }
1877 return true;
1878 }
1879
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001880 public void removeEvents(int code) {
1881 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1882 mActiveEvents[idx] = null;
1883 }
1884
Dianne Hackborn37de0982014-05-09 09:32:18 -07001885 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1886 return mActiveEvents[code];
1887 }
1888 }
1889
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001890 public static final class BitDescription {
1891 public final int mask;
1892 public final int shift;
1893 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001894 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001895 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001896 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07001897
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001898 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001899 this.mask = mask;
1900 this.shift = -1;
1901 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001902 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001903 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001904 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001905 }
Bookatzc8c44962017-05-11 12:12:54 -07001906
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001907 public BitDescription(int mask, int shift, String name, String shortName,
1908 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001909 this.mask = mask;
1910 this.shift = shift;
1911 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001912 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001913 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001914 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001915 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001916 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001917
Dianne Hackbornfc064132014-06-02 12:42:12 -07001918 /**
1919 * Don't allow any more batching in to the current history event. This
1920 * is called when printing partial histories, so to ensure that the next
1921 * history event will go in to a new batch after what was printed in the
1922 * last partial history.
1923 */
1924 public abstract void commitCurrentHistoryBatchLocked();
1925
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001926 public abstract int getHistoryTotalSize();
1927
1928 public abstract int getHistoryUsedSize();
1929
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001930 public abstract boolean startIteratingHistoryLocked();
1931
Dianne Hackborn099bc622014-01-22 13:39:16 -08001932 public abstract int getHistoryStringPoolSize();
1933
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001934 public abstract int getHistoryStringPoolBytes();
1935
1936 public abstract String getHistoryTagPoolString(int index);
1937
1938 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001939
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001940 public abstract boolean getNextHistoryLocked(HistoryItem out);
1941
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001942 public abstract void finishIteratingHistoryLocked();
1943
1944 public abstract boolean startIteratingOldHistoryLocked();
1945
1946 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1947
1948 public abstract void finishIteratingOldHistoryLocked();
1949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001951 * Return the base time offset for the battery history.
1952 */
1953 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07001954
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001955 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 * Returns the number of times the device has been started.
1957 */
1958 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07001959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001961 * 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 -08001962 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07001963 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 * {@hide}
1965 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001966 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07001967
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001968 /**
1969 * Returns the number of times the screen was turned on.
1970 *
1971 * {@hide}
1972 */
1973 public abstract int getScreenOnCount(int which);
1974
Mike Mac2f518a2017-09-19 16:06:03 -07001975 /**
1976 * Returns the time in microseconds that the screen has been dozing while the device was
1977 * running on battery.
1978 *
1979 * {@hide}
1980 */
1981 public abstract long getScreenDozeTime(long elapsedRealtimeUs, int which);
1982
1983 /**
1984 * Returns the number of times the screen was turned dozing.
1985 *
1986 * {@hide}
1987 */
1988 public abstract int getScreenDozeCount(int which);
1989
Jeff Browne95c3cd2014-05-02 16:59:26 -07001990 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1991
Dianne Hackborn617f8772009-03-31 15:04:46 -07001992 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1993 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1994 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1995 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1996 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07001997
Dianne Hackborn617f8772009-03-31 15:04:46 -07001998 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1999 "dark", "dim", "medium", "light", "bright"
2000 };
Bookatzc8c44962017-05-11 12:12:54 -07002001
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002002 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
2003 "0", "1", "2", "3", "4"
2004 };
2005
Dianne Hackborn617f8772009-03-31 15:04:46 -07002006 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07002007
Dianne Hackborn617f8772009-03-31 15:04:46 -07002008 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002009 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07002010 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07002011 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002012 * {@hide}
2013 */
2014 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002015 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002018 * Returns the {@link Timer} object that tracks the given screen brightness.
2019 *
2020 * {@hide}
2021 */
2022 public abstract Timer getScreenBrightnessTimer(int brightnessBin);
2023
2024 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002025 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002026 * running on battery.
2027 *
2028 * {@hide}
2029 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002030 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002031
2032 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002033 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002034 *
2035 * {@hide}
2036 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002037 public abstract int getPowerSaveModeEnabledCount(int which);
2038
2039 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002040 * Constant for device idle mode: not active.
2041 */
2042 public static final int DEVICE_IDLE_MODE_OFF = 0;
2043
2044 /**
2045 * Constant for device idle mode: active in lightweight mode.
2046 */
2047 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
2048
2049 /**
2050 * Constant for device idle mode: active in full mode.
2051 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002052 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002053
2054 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002055 * Returns the time in microseconds that device has been in idle mode while
2056 * running on battery.
2057 *
2058 * {@hide}
2059 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002060 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002061
2062 /**
2063 * Returns the number of times that the devie has gone in to idle mode.
2064 *
2065 * {@hide}
2066 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002067 public abstract int getDeviceIdleModeCount(int mode, int which);
2068
2069 /**
2070 * Return the longest duration we spent in a particular device idle mode (fully in the
2071 * mode, not in idle maintenance etc).
2072 */
2073 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002074
2075 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002076 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002077 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002078 * counts all of the time that we consider the device to be idle, whether or not
2079 * it is currently in the actual device idle mode.
2080 *
2081 * {@hide}
2082 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002083 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002084
2085 /**
Bookatz8c6571b2017-10-24 15:04:41 -07002086 * Returns the number of times that the device has started idling.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002087 *
2088 * {@hide}
2089 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002090 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002091
2092 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002093 * Returns the number of times that connectivity state changed.
2094 *
2095 * {@hide}
2096 */
2097 public abstract int getNumConnectivityChange(int which);
2098
2099 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002100 * 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 -08002101 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002102 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 * {@hide}
2104 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002105 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002106
Dianne Hackborn627bba72009-03-24 22:32:56 -07002107 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002108 * Returns the number of times a phone call was activated.
2109 *
2110 * {@hide}
2111 */
2112 public abstract int getPhoneOnCount(int which);
2113
2114 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002115 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002116 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002117 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002118 * {@hide}
2119 */
2120 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002121 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002122
Dianne Hackborn617f8772009-03-31 15:04:46 -07002123 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07002124 * Returns the time in microseconds that the phone has been trying to
2125 * acquire a signal.
2126 *
2127 * {@hide}
2128 */
2129 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002130 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002131
2132 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002133 * Returns the {@link Timer} object that tracks how much the phone has been trying to
2134 * acquire a signal.
2135 *
2136 * {@hide}
2137 */
2138 public abstract Timer getPhoneSignalScanningTimer();
2139
2140 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002141 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002142 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002143 * {@hide}
2144 */
2145 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
2146
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002147 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002148 * Return the {@link Timer} object used to track the given signal strength's duration and
2149 * counts.
2150 */
2151 protected abstract Timer getPhoneSignalStrengthTimer(int strengthBin);
2152
2153 /**
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002154 * Returns the time in microseconds that the mobile network has been active
2155 * (in a high power state).
2156 *
2157 * {@hide}
2158 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002159 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002160
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002161 /**
2162 * Returns the number of times that the mobile network has transitioned to the
2163 * active state.
2164 *
2165 * {@hide}
2166 */
2167 public abstract int getMobileRadioActiveCount(int which);
2168
2169 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002170 * Returns the time in microseconds that is the difference between the mobile radio
2171 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
2172 * from the radio.
2173 *
2174 * {@hide}
2175 */
2176 public abstract long getMobileRadioActiveAdjustedTime(int which);
2177
2178 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002179 * Returns the time in microseconds that the mobile network has been active
2180 * (in a high power state) but not being able to blame on an app.
2181 *
2182 * {@hide}
2183 */
2184 public abstract long getMobileRadioActiveUnknownTime(int which);
2185
2186 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002187 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002188 *
2189 * {@hide}
2190 */
2191 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002192
Dianne Hackborn627bba72009-03-24 22:32:56 -07002193 public static final int DATA_CONNECTION_NONE = 0;
2194 public static final int DATA_CONNECTION_GPRS = 1;
2195 public static final int DATA_CONNECTION_EDGE = 2;
2196 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002197 public static final int DATA_CONNECTION_CDMA = 4;
2198 public static final int DATA_CONNECTION_EVDO_0 = 5;
2199 public static final int DATA_CONNECTION_EVDO_A = 6;
2200 public static final int DATA_CONNECTION_1xRTT = 7;
2201 public static final int DATA_CONNECTION_HSDPA = 8;
2202 public static final int DATA_CONNECTION_HSUPA = 9;
2203 public static final int DATA_CONNECTION_HSPA = 10;
2204 public static final int DATA_CONNECTION_IDEN = 11;
2205 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002206 public static final int DATA_CONNECTION_LTE = 13;
2207 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08002208 public static final int DATA_CONNECTION_HSPAP = 15;
2209 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002210
Dianne Hackborn627bba72009-03-24 22:32:56 -07002211 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002212 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002213 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08002214 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002215 };
Bookatzc8c44962017-05-11 12:12:54 -07002216
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002217 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002218
Dianne Hackborn627bba72009-03-24 22:32:56 -07002219 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002220 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002221 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002222 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002223 * {@hide}
2224 */
2225 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002226 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002229 * Returns the number of times the phone has entered the given data
2230 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002231 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002232 * {@hide}
2233 */
2234 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002235
Kweku Adams87b19ec2017-10-09 12:40:03 -07002236 /**
2237 * Returns the {@link Timer} object that tracks the phone's data connection type stats.
2238 */
2239 public abstract Timer getPhoneDataConnectionTimer(int dataType);
2240
Dianne Hackborn3251b902014-06-20 14:40:53 -07002241 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2242 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2243 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2244 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2245 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2246 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2247 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2248 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2249 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2250 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2251 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2252 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2253 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2254
2255 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2256
2257 static final String[] WIFI_SUPPL_STATE_NAMES = {
2258 "invalid", "disconn", "disabled", "inactive", "scanning",
2259 "authenticating", "associating", "associated", "4-way-handshake",
2260 "group-handshake", "completed", "dormant", "uninit"
2261 };
2262
2263 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2264 "inv", "dsc", "dis", "inact", "scan",
2265 "auth", "ascing", "asced", "4-way",
2266 "group", "compl", "dorm", "uninit"
2267 };
2268
Mike Mac2f518a2017-09-19 16:06:03 -07002269 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002270 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002271 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2272 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002273 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002274 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2275 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2276 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002277 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002278 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002279 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002280 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002281 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2282 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Mike Mac2f518a2017-09-19 16:06:03 -07002283 new BitDescription(HistoryItem.STATE_SCREEN_DOZE_FLAG, "screen_doze", "Sd"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002284 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2285 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2286 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2287 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2288 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2289 new String[] {"in", "out", "emergency", "off"},
2290 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002291 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2292 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2293 SignalStrength.SIGNAL_STRENGTH_NAMES,
2294 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002295 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2296 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2297 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002298 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002299
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002300 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2301 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002302 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002303 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002304 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002305 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002306 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002307 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2308 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2309 new String[] { "off", "light", "full", "???" },
2310 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002311 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2312 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2313 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002314 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2315 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2316 new String[] { "0", "1", "2", "3", "4" },
2317 new String[] { "0", "1", "2", "3", "4" }),
2318 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2319 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2320 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002321 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002322 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002323 };
2324
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002325 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002326 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002327 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2328 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002329 };
2330
2331 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002332 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002333 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002334 "Esw", "Ewa", "Elw", "Eec"
2335 };
2336
2337 @FunctionalInterface
2338 public interface IntToString {
2339 String applyAsString(int val);
2340 }
2341
2342 private static final IntToString sUidToString = UserHandle::formatUid;
2343 private static final IntToString sIntToString = Integer::toString;
2344
2345 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2346 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2347 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2348 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2349 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002350 };
2351
Dianne Hackborn617f8772009-03-31 15:04:46 -07002352 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08002353 * Returns total time for WiFi Multicast Wakelock timer.
2354 * Note that this may be different from the sum of per uid timer values.
2355 *
2356 * {@hide}
2357 */
2358 public abstract long getWifiMulticastWakelockTime(long elapsedRealtimeUs, int which);
2359
2360 /**
2361 * Returns total time for WiFi Multicast Wakelock timer
2362 * Note that this may be different from the sum of per uid timer values.
2363 *
2364 * {@hide}
2365 */
2366 public abstract int getWifiMulticastWakelockCount(int which);
2367
2368 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002369 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002370 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002371 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002372 * {@hide}
2373 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002374 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002375
2376 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002377 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002378 * been in the running state while the device was running on battery.
2379 *
2380 * {@hide}
2381 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002382 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002383
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002384 public static final int WIFI_STATE_OFF = 0;
2385 public static final int WIFI_STATE_OFF_SCANNING = 1;
2386 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2387 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2388 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2389 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2390 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2391 public static final int WIFI_STATE_SOFT_AP = 7;
2392
2393 static final String[] WIFI_STATE_NAMES = {
2394 "off", "scanning", "no_net", "disconn",
2395 "sta", "p2p", "sta_p2p", "soft_ap"
2396 };
2397
2398 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2399
2400 /**
2401 * Returns the time in microseconds that WiFi has been running in the given state.
2402 *
2403 * {@hide}
2404 */
2405 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002406 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002407
2408 /**
2409 * Returns the number of times that WiFi has entered the given state.
2410 *
2411 * {@hide}
2412 */
2413 public abstract int getWifiStateCount(int wifiState, int which);
2414
The Android Open Source Project10592532009-03-18 17:39:46 -07002415 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002416 * Returns the {@link Timer} object that tracks the given WiFi state.
2417 *
2418 * {@hide}
2419 */
2420 public abstract Timer getWifiStateTimer(int wifiState);
2421
2422 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002423 * Returns the time in microseconds that the wifi supplicant has been
2424 * in a given state.
2425 *
2426 * {@hide}
2427 */
2428 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2429
2430 /**
2431 * Returns the number of times that the wifi supplicant has transitioned
2432 * to a given state.
2433 *
2434 * {@hide}
2435 */
2436 public abstract int getWifiSupplStateCount(int state, int which);
2437
Kweku Adams87b19ec2017-10-09 12:40:03 -07002438 /**
2439 * Returns the {@link Timer} object that tracks the given wifi supplicant state.
2440 *
2441 * {@hide}
2442 */
2443 public abstract Timer getWifiSupplStateTimer(int state);
2444
Dianne Hackborn3251b902014-06-20 14:40:53 -07002445 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2446
2447 /**
2448 * Returns the time in microseconds that WIFI has been running with
2449 * the given signal strength.
2450 *
2451 * {@hide}
2452 */
2453 public abstract long getWifiSignalStrengthTime(int strengthBin,
2454 long elapsedRealtimeUs, int which);
2455
2456 /**
2457 * Returns the number of times WIFI has entered the given signal strength.
2458 *
2459 * {@hide}
2460 */
2461 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2462
2463 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002464 * Returns the {@link Timer} object that tracks the given WIFI signal strength.
2465 *
2466 * {@hide}
2467 */
2468 public abstract Timer getWifiSignalStrengthTimer(int strengthBin);
2469
2470 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002471 * Returns the time in microseconds that the flashlight has been on while the device was
2472 * running on battery.
2473 *
2474 * {@hide}
2475 */
2476 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2477
2478 /**
2479 * Returns the number of times that the flashlight has been turned on while the device was
2480 * running on battery.
2481 *
2482 * {@hide}
2483 */
2484 public abstract long getFlashlightOnCount(int which);
2485
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002486 /**
2487 * Returns the time in microseconds that the camera has been on while the device was
2488 * running on battery.
2489 *
2490 * {@hide}
2491 */
2492 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2493
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002494 /**
2495 * Returns the time in microseconds that bluetooth scans were running while the device was
2496 * on battery.
2497 *
2498 * {@hide}
2499 */
2500 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002501
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002502 public static final int NETWORK_MOBILE_RX_DATA = 0;
2503 public static final int NETWORK_MOBILE_TX_DATA = 1;
2504 public static final int NETWORK_WIFI_RX_DATA = 2;
2505 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002506 public static final int NETWORK_BT_RX_DATA = 4;
2507 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002508 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2509 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2510 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2511 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2512 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002513
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002514 public abstract long getNetworkActivityBytes(int type, int which);
2515 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002516
Adam Lesinskie08af192015-03-25 16:42:59 -07002517 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002518 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002519 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002520 * actual power data.
2521 */
2522 public abstract boolean hasWifiActivityReporting();
2523
2524 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002525 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2526 * in various radio controller states, such as transmit, receive, and idle.
2527 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002528 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002529 public abstract ControllerActivityCounter getWifiControllerActivity();
2530
2531 /**
2532 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2533 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2534 * actual power data.
2535 */
2536 public abstract boolean hasBluetoothActivityReporting();
2537
2538 /**
2539 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2540 * in various radio controller states, such as transmit, receive, and idle.
2541 * @return non-null {@link ControllerActivityCounter}
2542 */
2543 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2544
2545 /**
2546 * Returns true if the BatteryStats object has detailed modem power reports.
2547 * When true, calling {@link #getModemControllerActivity()} will yield the
2548 * actual power data.
2549 */
2550 public abstract boolean hasModemActivityReporting();
2551
2552 /**
2553 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2554 * in various radio controller states, such as transmit, receive, and idle.
2555 * @return non-null {@link ControllerActivityCounter}
2556 */
2557 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002558
The Android Open Source Project10592532009-03-18 17:39:46 -07002559 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002560 * Return the wall clock time when battery stats data collection started.
2561 */
2562 public abstract long getStartClockTime();
2563
2564 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002565 * Return platform version tag that we were running in when the battery stats started.
2566 */
2567 public abstract String getStartPlatformVersion();
2568
2569 /**
2570 * Return platform version tag that we were running in when the battery stats ended.
2571 */
2572 public abstract String getEndPlatformVersion();
2573
2574 /**
2575 * Return the internal version code of the parcelled format.
2576 */
2577 public abstract int getParcelVersion();
2578
2579 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 * Return whether we are currently running on battery.
2581 */
2582 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 /**
2585 * Returns a SparseArray containing the statistics for each uid.
2586 */
2587 public abstract SparseArray<? extends Uid> getUidStats();
2588
2589 /**
2590 * Returns the current battery uptime in microseconds.
2591 *
2592 * @param curTime the amount of elapsed realtime in microseconds.
2593 */
2594 public abstract long getBatteryUptime(long curTime);
2595
2596 /**
2597 * Returns the current battery realtime in microseconds.
2598 *
2599 * @param curTime the amount of elapsed realtime in microseconds.
2600 */
2601 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002602
The Android Open Source Project10592532009-03-18 17:39:46 -07002603 /**
Evan Millar633a1742009-04-02 16:36:33 -07002604 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002605 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002606 */
Evan Millar633a1742009-04-02 16:36:33 -07002607 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002608
The Android Open Source Project10592532009-03-18 17:39:46 -07002609 /**
Evan Millar633a1742009-04-02 16:36:33 -07002610 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2611 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002612 */
Evan Millar633a1742009-04-02 16:36:33 -07002613 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614
2615 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002616 * Get the amount the battery has discharged since the stats were
2617 * last reset after charging, as a lower-end approximation.
2618 */
2619 public abstract int getLowDischargeAmountSinceCharge();
2620
2621 /**
2622 * Get the amount the battery has discharged since the stats were
2623 * last reset after charging, as an upper-end approximation.
2624 */
2625 public abstract int getHighDischargeAmountSinceCharge();
2626
2627 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002628 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2629 */
2630 public abstract int getDischargeAmount(int which);
2631
2632 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002633 * Get the amount the battery has discharged while the screen was on,
2634 * since the last time power was unplugged.
2635 */
2636 public abstract int getDischargeAmountScreenOn();
2637
2638 /**
2639 * Get the amount the battery has discharged while the screen was on,
2640 * since the last time the device was charged.
2641 */
2642 public abstract int getDischargeAmountScreenOnSinceCharge();
2643
2644 /**
2645 * Get the amount the battery has discharged while the screen was off,
2646 * since the last time power was unplugged.
2647 */
2648 public abstract int getDischargeAmountScreenOff();
2649
2650 /**
2651 * Get the amount the battery has discharged while the screen was off,
2652 * since the last time the device was charged.
2653 */
2654 public abstract int getDischargeAmountScreenOffSinceCharge();
2655
2656 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002657 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002658 * since the last time power was unplugged.
2659 */
2660 public abstract int getDischargeAmountScreenDoze();
2661
2662 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002663 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002664 * since the last time the device was charged.
2665 */
2666 public abstract int getDischargeAmountScreenDozeSinceCharge();
2667
2668 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 * Returns the total, last, or current battery uptime in microseconds.
2670 *
2671 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002672 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 */
2674 public abstract long computeBatteryUptime(long curTime, int which);
2675
2676 /**
2677 * Returns the total, last, or current battery realtime in microseconds.
2678 *
2679 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002680 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 */
2682 public abstract long computeBatteryRealtime(long curTime, int which);
2683
2684 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002685 * Returns the total, last, or current battery screen off/doze uptime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002686 *
2687 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002688 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002689 */
2690 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2691
2692 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002693 * Returns the total, last, or current battery screen off/doze realtime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002694 *
2695 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002696 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002697 */
2698 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2699
2700 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 * Returns the total, last, or current uptime in microseconds.
2702 *
2703 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002704 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 */
2706 public abstract long computeUptime(long curTime, int which);
2707
2708 /**
2709 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002710 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002712 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 */
2714 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002715
2716 /**
2717 * Compute an approximation for how much run time (in microseconds) is remaining on
2718 * the battery. Returns -1 if no time can be computed: either there is not
2719 * enough current data to make a decision, or the battery is currently
2720 * charging.
2721 *
2722 * @param curTime The current elepsed realtime in microseconds.
2723 */
2724 public abstract long computeBatteryTimeRemaining(long curTime);
2725
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002726 // The part of a step duration that is the actual time.
2727 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2728
2729 // Bits in a step duration that are the new battery level we are at.
2730 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002731 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002732
2733 // Bits in a step duration that are the initial mode we were in at that step.
2734 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002735 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002736
2737 // Bits in a step duration that indicate which modes changed during that step.
2738 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002739 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002740
2741 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2742 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2743
Santos Cordone94f0502017-02-24 12:31:20 -08002744 // The largest value for screen state that is tracked in battery states. Any values above
2745 // this should be mapped back to one of the tracked values before being tracked here.
2746 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2747
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002748 // Step duration mode: power save is on.
2749 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2750
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002751 // Step duration mode: device is currently in idle mode.
2752 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2753
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002754 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2755 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002756 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2757 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002758 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2759 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2760 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2761 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2762 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002763 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2764 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002765 };
2766 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2767 (Display.STATE_OFF-1),
2768 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002769 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002770 (Display.STATE_ON-1),
2771 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2772 (Display.STATE_DOZE-1),
2773 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2774 (Display.STATE_DOZE_SUSPEND-1),
2775 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002776 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002777 };
2778 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2779 "screen off",
2780 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002781 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002782 "screen on",
2783 "screen on power save",
2784 "screen doze",
2785 "screen doze power save",
2786 "screen doze-suspend",
2787 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002788 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002789 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002790
2791 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002792 * Return the amount of battery discharge while the screen was off, measured in
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002793 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2794 * a coulomb counter.
2795 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002796 public abstract long getUahDischargeScreenOff(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002797
2798 /**
2799 * Return the amount of battery discharge while the screen was in doze mode, measured in
2800 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2801 * a coulomb counter.
2802 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002803 public abstract long getUahDischargeScreenDoze(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002804
2805 /**
2806 * Return the amount of battery discharge measured in micro-Ampere-hours. This will be
2807 * non-zero only if the device's battery has a coulomb counter.
2808 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002809 public abstract long getUahDischarge(int which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002810
2811 /**
Mike Ma15313c92017-11-15 17:58:21 -08002812 * @return the amount of battery discharge while the device is in light idle mode, measured in
2813 * micro-Ampere-hours.
2814 */
2815 public abstract long getUahDischargeLightDoze(int which);
2816
2817 /**
2818 * @return the amount of battery discharge while the device is in deep idle mode, measured in
2819 * micro-Ampere-hours.
2820 */
2821 public abstract long getUahDischargeDeepDoze(int which);
2822
2823 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002824 * Returns the estimated real battery capacity, which may be less than the capacity
2825 * declared by the PowerProfile.
2826 * @return The estimated battery capacity in mAh.
2827 */
2828 public abstract int getEstimatedBatteryCapacity();
2829
2830 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002831 * @return The minimum learned battery capacity in uAh.
2832 */
2833 public abstract int getMinLearnedBatteryCapacity();
2834
2835 /**
2836 * @return The maximum learned battery capacity in uAh.
2837 */
2838 public abstract int getMaxLearnedBatteryCapacity() ;
2839
2840 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002841 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002842 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002843 public abstract LevelStepTracker getDischargeLevelStepTracker();
2844
2845 /**
2846 * Return the array of daily discharge step durations.
2847 */
2848 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002849
2850 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002851 * Compute an approximation for how much time (in microseconds) remains until the battery
2852 * is fully charged. Returns -1 if no time can be computed: either there is not
2853 * enough current data to make a decision, or the battery is currently
2854 * discharging.
2855 *
2856 * @param curTime The current elepsed realtime in microseconds.
2857 */
2858 public abstract long computeChargeTimeRemaining(long curTime);
2859
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002860 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002861 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002862 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002863 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002864
2865 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002866 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002867 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002868 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002869
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002870 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2871
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002872 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002873
Evan Millarc64edde2009-04-18 12:26:32 -07002874 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875
Bookatz50df7112017-08-04 14:53:26 -07002876 /**
2877 * Returns Timers tracking the total time of each Resource Power Manager state and voter.
2878 */
2879 public abstract Map<String, ? extends Timer> getRpmStats();
2880 /**
2881 * Returns Timers tracking the screen-off time of each Resource Power Manager state and voter.
2882 */
2883 public abstract Map<String, ? extends Timer> getScreenOffRpmStats();
2884
2885
James Carr2dd7e5e2016-07-20 18:48:39 -07002886 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2887
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002888 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2889
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002890 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 long days = seconds / (60 * 60 * 24);
2892 if (days != 0) {
2893 out.append(days);
2894 out.append("d ");
2895 }
2896 long used = days * 60 * 60 * 24;
2897
2898 long hours = (seconds - used) / (60 * 60);
2899 if (hours != 0 || used != 0) {
2900 out.append(hours);
2901 out.append("h ");
2902 }
2903 used += hours * 60 * 60;
2904
2905 long mins = (seconds-used) / 60;
2906 if (mins != 0 || used != 0) {
2907 out.append(mins);
2908 out.append("m ");
2909 }
2910 used += mins * 60;
2911
2912 if (seconds != 0 || used != 0) {
2913 out.append(seconds-used);
2914 out.append("s ");
2915 }
2916 }
2917
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002918 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002920 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 sb.append(time - (sec * 1000));
2922 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 }
2924
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002925 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002926 long sec = time / 1000;
2927 formatTimeRaw(sb, sec);
2928 sb.append(time - (sec * 1000));
2929 sb.append("ms");
2930 }
2931
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002932 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002934 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 }
2936 float perc = ((float)num) / ((float)den) * 100;
2937 mFormatBuilder.setLength(0);
2938 mFormatter.format("%.1f%%", perc);
2939 return mFormatBuilder.toString();
2940 }
2941
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002942 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002943 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07002944
Evan Millar22ac0432009-03-31 11:33:18 -07002945 if (bytes < BYTES_PER_KB) {
2946 return bytes + "B";
2947 } else if (bytes < BYTES_PER_MB) {
2948 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2949 return mFormatBuilder.toString();
2950 } else if (bytes < BYTES_PER_GB){
2951 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2952 return mFormatBuilder.toString();
2953 } else {
2954 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2955 return mFormatBuilder.toString();
2956 }
2957 }
2958
Kweku Adams103351f2017-10-16 14:39:34 -07002959 private static long roundUsToMs(long timeUs) {
2960 return (timeUs + 500) / 1000;
2961 }
2962
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002963 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002964 if (timer != null) {
2965 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002966 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002967 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2968 return totalTimeMillis;
2969 }
2970 return 0;
2971 }
2972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 /**
2974 *
2975 * @param sb a StringBuilder object.
2976 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002977 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002979 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 * @param linePrefix a String to be prepended to each line of output.
2981 * @return the line prefix
2982 */
2983 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002984 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002987 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002988
Evan Millarc64edde2009-04-18 12:26:32 -07002989 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990 if (totalTimeMillis != 0) {
2991 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002992 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002993 if (name != null) {
2994 sb.append(name);
2995 sb.append(' ');
2996 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 sb.append('(');
2998 sb.append(count);
2999 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003000 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
3001 if (maxDurationMs >= 0) {
3002 sb.append(" max=");
3003 sb.append(maxDurationMs);
3004 }
Bookatz506a8182017-05-01 14:18:42 -07003005 // Put actual time if it is available and different from totalTimeMillis.
3006 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
3007 if (totalDurMs > totalTimeMillis) {
3008 sb.append(" actual=");
3009 sb.append(totalDurMs);
3010 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07003011 if (timer.isRunningLocked()) {
3012 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3013 if (currentMs >= 0) {
3014 sb.append(" (running for ");
3015 sb.append(currentMs);
3016 sb.append("ms)");
3017 } else {
3018 sb.append(" (running)");
3019 }
3020 }
3021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003022 return ", ";
3023 }
3024 }
3025 return linePrefix;
3026 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003027
3028 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07003029 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003030 *
3031 * @param pw a PrintWriter object to print to.
3032 * @param sb a StringBuilder object.
3033 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08003034 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003035 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
3036 * @param prefix a String to be prepended to each line of output.
3037 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07003038 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003039 */
3040 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07003041 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003042 if (timer != null) {
3043 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07003044 final long totalTimeMs = (timer.getTotalTimeLocked(
3045 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003046 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003047 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003048 sb.setLength(0);
3049 sb.append(prefix);
3050 sb.append(" ");
3051 sb.append(type);
3052 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003053 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003054 sb.append("realtime (");
3055 sb.append(count);
3056 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003057 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
3058 if (maxDurationMs >= 0) {
3059 sb.append(" max=");
3060 sb.append(maxDurationMs);
3061 }
3062 if (timer.isRunningLocked()) {
3063 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
3064 if (currentMs >= 0) {
3065 sb.append(" (running for ");
3066 sb.append(currentMs);
3067 sb.append("ms)");
3068 } else {
3069 sb.append(" (running)");
3070 }
3071 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003072 pw.println(sb.toString());
3073 return true;
3074 }
3075 }
3076 return false;
3077 }
Bookatzc8c44962017-05-11 12:12:54 -07003078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 /**
3080 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07003081 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 * @param sb a StringBuilder object.
3083 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003084 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003086 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 * @param linePrefix a String to be prepended to each line of output.
3088 * @return the line prefix
3089 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003090 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
3091 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 long totalTimeMicros = 0;
3093 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07003094 long max = 0;
3095 long current = 0;
3096 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003098 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07003099 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003100 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3101 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07003102 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 }
3104 sb.append(linePrefix);
3105 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
3106 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07003107 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003109 sb.append(',');
3110 sb.append(current);
3111 sb.append(',');
3112 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07003113 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
3114 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
3115 // totalDuration independent of totalTimeMicros (since they are not pooled).
3116 if (name != null) {
3117 sb.append(',');
3118 sb.append(totalDuration);
3119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 return ",";
3121 }
Bookatz506a8182017-05-01 14:18:42 -07003122
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003123 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
3124 String type) {
3125 pw.print(BATTERY_STATS_CHECKIN_VERSION);
3126 pw.print(',');
3127 pw.print(uid);
3128 pw.print(',');
3129 pw.print(category);
3130 pw.print(',');
3131 pw.print(type);
3132 }
3133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003134 /**
3135 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07003136 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 * @param pw the PageWriter to dump log to
3138 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3139 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3140 * @param args type-dependent data arguments
3141 */
Bookatzc8c44962017-05-11 12:12:54 -07003142 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003144 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003145 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003146 pw.print(',');
3147 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003148 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003149 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07003151
3152 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003153 * Dump a given timer stat for terse checkin mode.
3154 *
3155 * @param pw the PageWriter to dump log to
3156 * @param uid the UID to log
3157 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3158 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3159 * @param timer a {@link Timer} to dump stats for
3160 * @param rawRealtime the current elapsed realtime of the system in microseconds
3161 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3162 */
3163 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
3164 Timer timer, long rawRealtime, int which) {
3165 if (timer != null) {
3166 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003167 final long totalTime = roundUsToMs(timer.getTotalTimeLocked(rawRealtime, which));
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003168 final int count = timer.getCountLocked(which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003169 if (totalTime != 0 || count != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003170 dumpLine(pw, uid, category, type, totalTime, count);
3171 }
3172 }
3173 }
3174
3175 /**
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003176 * Dump a given timer stat to the proto stream.
3177 *
3178 * @param proto the ProtoOutputStream to log to
3179 * @param fieldId type of data, the field to save to (e.g. AggregatedBatteryStats.WAKELOCK)
3180 * @param timer a {@link Timer} to dump stats for
3181 * @param rawRealtimeUs the current elapsed realtime of the system in microseconds
3182 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3183 */
3184 private static void dumpTimer(ProtoOutputStream proto, long fieldId,
Kweku Adams87b19ec2017-10-09 12:40:03 -07003185 Timer timer, long rawRealtimeUs, int which) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003186 if (timer == null) {
3187 return;
3188 }
3189 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003190 final long timeMs = roundUsToMs(timer.getTotalTimeLocked(rawRealtimeUs, which));
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003191 final int count = timer.getCountLocked(which);
Kweku Adams103351f2017-10-16 14:39:34 -07003192 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs / 1000);
3193 final long curDurationMs = timer.getCurrentDurationMsLocked(rawRealtimeUs / 1000);
3194 final long totalDurationMs = timer.getTotalDurationMsLocked(rawRealtimeUs / 1000);
3195 if (timeMs != 0 || count != 0 || maxDurationMs != -1 || curDurationMs != -1
3196 || totalDurationMs != -1) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003197 final long token = proto.start(fieldId);
Kweku Adams103351f2017-10-16 14:39:34 -07003198 proto.write(TimerProto.DURATION_MS, timeMs);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003199 proto.write(TimerProto.COUNT, count);
Kweku Adams103351f2017-10-16 14:39:34 -07003200 // These values will be -1 for timers that don't implement the functionality.
3201 if (maxDurationMs != -1) {
3202 proto.write(TimerProto.MAX_DURATION_MS, maxDurationMs);
3203 }
3204 if (curDurationMs != -1) {
3205 proto.write(TimerProto.CURRENT_DURATION_MS, curDurationMs);
3206 }
3207 if (totalDurationMs != -1) {
3208 proto.write(TimerProto.TOTAL_DURATION_MS, totalDurationMs);
3209 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003210 proto.end(token);
3211 }
3212 }
3213
3214 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003215 * Checks if the ControllerActivityCounter has any data worth dumping.
3216 */
3217 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
3218 if (counter == null) {
3219 return false;
3220 }
3221
3222 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
3223 || counter.getRxTimeCounter().getCountLocked(which) != 0
3224 || counter.getPowerCounter().getCountLocked(which) != 0) {
3225 return true;
3226 }
3227
3228 for (LongCounter c : counter.getTxTimeCounters()) {
3229 if (c.getCountLocked(which) != 0) {
3230 return true;
3231 }
3232 }
3233 return false;
3234 }
3235
3236 /**
3237 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3238 * The order of the arguments in the final check in line is:
3239 *
3240 * idle, rx, power, tx...
3241 *
3242 * where tx... is one or more transmit level times.
3243 */
3244 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
3245 String type,
3246 ControllerActivityCounter counter,
3247 int which) {
3248 if (!controllerActivityHasData(counter, which)) {
3249 return;
3250 }
3251
3252 dumpLineHeader(pw, uid, category, type);
3253 pw.print(",");
3254 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
3255 pw.print(",");
3256 pw.print(counter.getRxTimeCounter().getCountLocked(which));
3257 pw.print(",");
3258 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3259 for (LongCounter c : counter.getTxTimeCounters()) {
3260 pw.print(",");
3261 pw.print(c.getCountLocked(which));
3262 }
3263 pw.println();
3264 }
3265
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003266 /**
3267 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3268 */
3269 private static void dumpControllerActivityProto(ProtoOutputStream proto, long fieldId,
3270 ControllerActivityCounter counter,
3271 int which) {
3272 if (!controllerActivityHasData(counter, which)) {
3273 return;
3274 }
3275
3276 final long cToken = proto.start(fieldId);
3277
3278 proto.write(ControllerActivityProto.IDLE_DURATION_MS,
3279 counter.getIdleTimeCounter().getCountLocked(which));
3280 proto.write(ControllerActivityProto.RX_DURATION_MS,
3281 counter.getRxTimeCounter().getCountLocked(which));
3282 proto.write(ControllerActivityProto.POWER_MAH,
3283 counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3284
3285 long tToken;
3286 LongCounter[] txCounters = counter.getTxTimeCounters();
3287 for (int i = 0; i < txCounters.length; ++i) {
3288 LongCounter c = txCounters[i];
3289 tToken = proto.start(ControllerActivityProto.TX);
3290 proto.write(ControllerActivityProto.TxLevel.LEVEL, i);
3291 proto.write(ControllerActivityProto.TxLevel.DURATION_MS, c.getCountLocked(which));
3292 proto.end(tToken);
3293 }
3294
3295 proto.end(cToken);
3296 }
3297
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003298 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
3299 String prefix, String controllerName,
3300 ControllerActivityCounter counter,
3301 int which) {
3302 if (controllerActivityHasData(counter, which)) {
3303 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
3304 }
3305 }
3306
3307 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
3308 String controllerName,
3309 ControllerActivityCounter counter, int which) {
3310 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
3311 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
3312 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003313 // Battery real time
3314 final long totalControllerActivityTimeMs
3315 = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003316 long totalTxTimeMs = 0;
3317 for (LongCounter txState : counter.getTxTimeCounters()) {
3318 totalTxTimeMs += txState.getCountLocked(which);
3319 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003320 final long sleepTimeMs
3321 = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + totalTxTimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003322
3323 sb.setLength(0);
3324 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003325 sb.append(" ");
3326 sb.append(controllerName);
3327 sb.append(" Sleep time: ");
3328 formatTimeMs(sb, sleepTimeMs);
3329 sb.append("(");
3330 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3331 sb.append(")");
3332 pw.println(sb.toString());
3333
3334 sb.setLength(0);
3335 sb.append(prefix);
3336 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003337 sb.append(controllerName);
3338 sb.append(" Idle time: ");
3339 formatTimeMs(sb, idleTimeMs);
3340 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003341 sb.append(formatRatioLocked(idleTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003342 sb.append(")");
3343 pw.println(sb.toString());
3344
3345 sb.setLength(0);
3346 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003347 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003348 sb.append(controllerName);
3349 sb.append(" Rx time: ");
3350 formatTimeMs(sb, rxTimeMs);
3351 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003352 sb.append(formatRatioLocked(rxTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003353 sb.append(")");
3354 pw.println(sb.toString());
3355
3356 sb.setLength(0);
3357 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003358 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003359 sb.append(controllerName);
3360 sb.append(" Tx time: ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003361
Siddharth Ray3c648c42017-10-02 17:30:58 -07003362 String [] powerLevel;
3363 switch(controllerName) {
3364 case "Cellular":
3365 powerLevel = new String[] {
3366 " less than 0dBm: ",
3367 " 0dBm to 8dBm: ",
3368 " 8dBm to 15dBm: ",
3369 " 15dBm to 20dBm: ",
3370 " above 20dBm: "};
3371 break;
3372 default:
3373 powerLevel = new String[] {"[0]", "[1]", "[2]", "[3]", "[4]"};
3374 break;
3375 }
3376 final int numTxLvls = Math.min(counter.getTxTimeCounters().length, powerLevel.length);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003377 if (numTxLvls > 1) {
Siddharth Ray3c648c42017-10-02 17:30:58 -07003378 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003379 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3380 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3381 sb.setLength(0);
3382 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003383 sb.append(" ");
3384 sb.append(powerLevel[lvl]);
3385 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003386 formatTimeMs(sb, txLvlTimeMs);
3387 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003388 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003389 sb.append(")");
3390 pw.println(sb.toString());
3391 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003392 } else {
3393 final long txLvlTimeMs = counter.getTxTimeCounters()[0].getCountLocked(which);
3394 formatTimeMs(sb, txLvlTimeMs);
3395 sb.append("(");
3396 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
3397 sb.append(")");
3398 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003399 }
3400
Siddharth Ray3c648c42017-10-02 17:30:58 -07003401 if (powerDrainMaMs > 0) {
3402 sb.setLength(0);
3403 sb.append(prefix);
3404 sb.append(" ");
3405 sb.append(controllerName);
3406 sb.append(" Battery drain: ").append(
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003407 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
Siddharth Ray3c648c42017-10-02 17:30:58 -07003408 sb.append("mAh");
3409 pw.println(sb.toString());
3410 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003411 }
3412
3413 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003414 * Temporary for settings.
3415 */
3416 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3417 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3418 }
3419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003420 /**
3421 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003422 *
Kweku Adams87b19ec2017-10-09 12:40:03 -07003423 * NOTE: all times are expressed in microseconds, unless specified otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003425 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3426 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 final long rawUptime = SystemClock.uptimeMillis() * 1000;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003428 final long rawRealtimeMs = SystemClock.elapsedRealtime();
3429 final long rawRealtime = rawRealtimeMs * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003431 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3432 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003433 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3434 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3435 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436 final long totalRealtime = computeRealtime(rawRealtime, which);
3437 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003438 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Mike Mac2f518a2017-09-19 16:06:03 -07003439 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003440 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003441 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003442 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3443 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003444 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003445 rawRealtime, which);
3446 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3447 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003448 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003449 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003450 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003451 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003452 final long dischargeCount = getUahDischarge(which);
3453 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
3454 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Ma15313c92017-11-15 17:58:21 -08003455 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
3456 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003457
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003458 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003459
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003460 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003461 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003462
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003463 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003466 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003467 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003468 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003469 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003470 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003471 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003472 getEstimatedBatteryCapacity(),
Mike Mac2f518a2017-09-19 16:06:03 -07003473 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity(),
3474 screenDozeTime / 1000);
Adam Lesinski67c134f2016-06-10 15:15:08 -07003475
Bookatzc8c44962017-05-11 12:12:54 -07003476
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003477 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003478 long fullWakeLockTimeTotal = 0;
3479 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003480
Evan Millar22ac0432009-03-31 11:33:18 -07003481 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003482 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003483
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003484 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3485 = u.getWakelockStats();
3486 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3487 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003488
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003489 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3490 if (fullWakeTimer != null) {
3491 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3492 which);
3493 }
3494
3495 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3496 if (partialWakeTimer != null) {
3497 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3498 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003499 }
3500 }
3501 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003502
3503 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003504 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3505 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3506 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3507 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3508 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3509 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3510 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3511 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003512 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3513 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003514 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3515 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003516 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3517 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003518
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003519 // Dump Modem controller stats
3520 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3521 getModemControllerActivity(), which);
3522
Adam Lesinskie283d332015-04-16 12:29:25 -07003523 // Dump Wifi controller stats
3524 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3525 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003526 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003527 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003528
3529 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3530 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003531
3532 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003533 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3534 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 // Dump misc stats
3537 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003538 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003539 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003540 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003541 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003542 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003543 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3544 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003545 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003546 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3547 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3548 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3549 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003550 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003551
Dianne Hackborn617f8772009-03-31 15:04:46 -07003552 // Dump screen brightness stats
3553 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3554 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003555 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003556 }
3557 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003558
Dianne Hackborn627bba72009-03-24 22:32:56 -07003559 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003560 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3561 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003562 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003563 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003564 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003565 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003566 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003567 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003568 args[i] = getPhoneSignalStrengthCount(i, which);
3569 }
3570 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003571
Dianne Hackborn627bba72009-03-24 22:32:56 -07003572 // Dump network type stats
3573 args = new Object[NUM_DATA_CONNECTION_TYPES];
3574 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003575 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003576 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003577 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3578 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3579 args[i] = getPhoneDataConnectionCount(i, which);
3580 }
3581 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003582
3583 // Dump wifi state stats
3584 args = new Object[NUM_WIFI_STATES];
3585 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003586 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003587 }
3588 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3589 for (int i=0; i<NUM_WIFI_STATES; i++) {
3590 args[i] = getWifiStateCount(i, which);
3591 }
3592 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3593
Dianne Hackborn3251b902014-06-20 14:40:53 -07003594 // Dump wifi suppl state stats
3595 args = new Object[NUM_WIFI_SUPPL_STATES];
3596 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3597 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3598 }
3599 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3600 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3601 args[i] = getWifiSupplStateCount(i, which);
3602 }
3603 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3604
3605 // Dump wifi signal strength stats
3606 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3607 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3608 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3609 }
3610 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3611 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3612 args[i] = getWifiSignalStrengthCount(i, which);
3613 }
3614 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3615
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003616 // Dump Multicast total stats
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003617 final long multicastWakeLockTimeTotalMicros =
3618 getWifiMulticastWakelockTime(rawRealtime, which);
3619 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003620 dumpLine(pw, 0 /* uid */, category, WIFI_MULTICAST_TOTAL_DATA,
3621 multicastWakeLockTimeTotalMicros / 1000,
3622 multicastWakeLockCountTotal);
3623
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003624 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003625 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003626 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003627 }
Bookatzc8c44962017-05-11 12:12:54 -07003628
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003629 if (which == STATS_SINCE_UNPLUGGED) {
3630 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3631 getDischargeStartLevel()-getDischargeCurrentLevel(),
3632 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003633 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
Mike Mac2f518a2017-09-19 16:06:03 -07003634 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003635 getDischargeAmountScreenDoze(), dischargeScreenDozeCount / 1000,
3636 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003637 } else {
3638 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3639 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003640 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003641 getDischargeAmountScreenOffSinceCharge(),
Mike Mac2f518a2017-09-19 16:06:03 -07003642 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003643 getDischargeAmountScreenDozeSinceCharge(), dischargeScreenDozeCount / 1000,
3644 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003645 }
Bookatzc8c44962017-05-11 12:12:54 -07003646
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003647 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003648 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003649 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003650 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003651 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003652 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003653 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3654 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003655 }
Evan Millarc64edde2009-04-18 12:26:32 -07003656 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003657 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003658 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003659 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3660 // Not doing the regular wake lock formatting to remain compatible
3661 // with the old checkin format.
3662 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3663 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003664 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003665 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003666 }
3667 }
Evan Millarc64edde2009-04-18 12:26:32 -07003668 }
Bookatzc8c44962017-05-11 12:12:54 -07003669
Bookatz50df7112017-08-04 14:53:26 -07003670 final Map<String, ? extends Timer> rpmStats = getRpmStats();
3671 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
3672 if (rpmStats.size() > 0) {
3673 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
3674 sb.setLength(0);
3675 Timer totalTimer = ent.getValue();
3676 long timeMs = (totalTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3677 int count = totalTimer.getCountLocked(which);
3678 Timer screenOffTimer = screenOffRpmStats.get(ent.getKey());
3679 long screenOffTimeMs = screenOffTimer != null
3680 ? (screenOffTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3681 int screenOffCount = screenOffTimer != null
3682 ? screenOffTimer.getCountLocked(which) : 0;
Bookatz82b341172017-09-07 19:06:08 -07003683 if (SCREEN_OFF_RPM_STATS_ENABLED) {
3684 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3685 "\"" + ent.getKey() + "\"", timeMs, count, screenOffTimeMs,
3686 screenOffCount);
3687 } else {
3688 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3689 "\"" + ent.getKey() + "\"", timeMs, count);
3690 }
Bookatz50df7112017-08-04 14:53:26 -07003691 }
3692 }
3693
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003694 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003695 helper.create(this);
3696 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003697 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003698 if (sippers != null && sippers.size() > 0) {
3699 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3700 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003701 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003702 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3703 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
Kweku Adams87b19ec2017-10-09 12:40:03 -07003704 int uid = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003705 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003706 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003707 String label;
3708 switch (bs.drainType) {
3709 case IDLE:
3710 label="idle";
3711 break;
3712 case CELL:
3713 label="cell";
3714 break;
3715 case PHONE:
3716 label="phone";
3717 break;
3718 case WIFI:
3719 label="wifi";
3720 break;
3721 case BLUETOOTH:
3722 label="blue";
3723 break;
3724 case SCREEN:
3725 label="scrn";
3726 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003727 case FLASHLIGHT:
3728 label="flashlight";
3729 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003730 case APP:
3731 uid = bs.uidObj.getUid();
3732 label = "uid";
3733 break;
3734 case USER:
3735 uid = UserHandle.getUid(bs.userId, 0);
3736 label = "user";
3737 break;
3738 case UNACCOUNTED:
3739 label = "unacc";
3740 break;
3741 case OVERCOUNTED:
3742 label = "over";
3743 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003744 case CAMERA:
3745 label = "camera";
3746 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003747 case MEMORY:
3748 label = "memory";
3749 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003750 default:
3751 label = "???";
3752 }
3753 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07003754 BatteryStatsHelper.makemAh(bs.totalPowerMah),
3755 bs.shouldHide ? 1 : 0,
3756 BatteryStatsHelper.makemAh(bs.screenPowerMah),
3757 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003758 }
3759 }
3760
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003761 final long[] cpuFreqs = getCpuFreqs();
3762 if (cpuFreqs != null) {
3763 sb.setLength(0);
3764 for (int i = 0; i < cpuFreqs.length; ++i) {
3765 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3766 }
3767 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3768 }
3769
Kweku Adams87b19ec2017-10-09 12:40:03 -07003770 // Dump stats per UID.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003771 for (int iu = 0; iu < NU; iu++) {
3772 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003773 if (reqUid >= 0 && uid != reqUid) {
3774 continue;
3775 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003776 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003778 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003779 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3780 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3781 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3782 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3783 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3784 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3785 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3786 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003787 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003788 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3789 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003790 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003791 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3792 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003793 // Background data transfers
3794 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3795 which);
3796 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3797 which);
3798 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3799 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3800 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3801 which);
3802 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3803 which);
3804 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3805 which);
3806 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3807 which);
3808
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003809 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3810 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003811 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003812 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3813 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3814 || wifiBytesBgTx > 0
3815 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3816 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003817 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3818 wifiBytesRx, wifiBytesTx,
3819 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003820 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003821 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003822 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3823 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3824 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3825 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003826 }
3827
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003828 // Dump modem controller data, per UID.
3829 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3830 u.getModemControllerActivity(), which);
3831
3832 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003833 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3834 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3835 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003836 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3837 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003838 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3839 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3840 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003841 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003842 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003843 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3844 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003845 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3846 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003847 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003848 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003849 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003850
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003851 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3852 u.getWifiControllerActivity(), which);
3853
Bookatz867c0d72017-03-07 18:23:42 -08003854 final Timer bleTimer = u.getBluetoothScanTimer();
3855 if (bleTimer != null) {
3856 // Convert from microseconds to milliseconds with rounding
3857 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3858 / 1000;
3859 if (totalTime != 0) {
3860 final int count = bleTimer.getCountLocked(which);
3861 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3862 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003863 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3864 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3865 final long actualTimeBg = bleTimerBg != null ?
3866 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003867 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003868 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3869 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003870 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3871 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3872 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3873 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3874 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
3875 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3876 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
3877 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3878 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
3879 final Timer unoptimizedScanTimerBg =
3880 u.getBluetoothUnoptimizedScanBackgroundTimer();
3881 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
3882 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3883 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
3884 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3885
Bookatz867c0d72017-03-07 18:23:42 -08003886 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07003887 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
3888 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
3889 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08003890 }
3891 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003892
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003893 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3894 u.getBluetoothControllerActivity(), which);
3895
Dianne Hackborn617f8772009-03-31 15:04:46 -07003896 if (u.hasUserActivity()) {
3897 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3898 boolean hasData = false;
3899 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3900 int val = u.getUserActivityCount(i, which);
3901 args[i] = val;
3902 if (val != 0) hasData = true;
3903 }
3904 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003905 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003906 }
3907 }
Bookatzc8c44962017-05-11 12:12:54 -07003908
3909 if (u.getAggregatedPartialWakelockTimer() != null) {
3910 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07003911 // Times are since reset (regardless of 'which')
3912 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07003913 final Timer bgTimer = timer.getSubTimer();
3914 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003915 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07003916 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
3917 }
3918
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003919 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3920 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3921 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3922 String linePrefix = "";
3923 sb.setLength(0);
3924 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3925 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003926 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3927 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003928 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003929 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
3930 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003931 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3932 rawRealtime, "w", which, linePrefix);
3933
Kweku Adams103351f2017-10-16 14:39:34 -07003934 // Only log if we had at least one wakelock...
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003935 if (sb.length() > 0) {
3936 String name = wakelocks.keyAt(iw);
3937 if (name.indexOf(',') >= 0) {
3938 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 }
Yi Jin02483362017-08-04 11:30:44 -07003940 if (name.indexOf('\n') >= 0) {
3941 name = name.replace('\n', '_');
3942 }
3943 if (name.indexOf('\r') >= 0) {
3944 name = name.replace('\r', '_');
3945 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003946 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947 }
3948 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003949
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003950 // WiFi Multicast Wakelock Statistics
3951 final Timer mcTimer = u.getMulticastWakelockStats();
3952 if (mcTimer != null) {
3953 final long totalMcWakelockTimeMs =
3954 mcTimer.getTotalTimeLocked(rawRealtime, which) / 1000 ;
3955 final int countMcWakelock = mcTimer.getCountLocked(which);
3956 if(totalMcWakelockTimeMs > 0) {
3957 dumpLine(pw, uid, category, WIFI_MULTICAST_DATA,
3958 totalMcWakelockTimeMs, countMcWakelock);
3959 }
3960 }
3961
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003962 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3963 for (int isy=syncs.size()-1; isy>=0; isy--) {
3964 final Timer timer = syncs.valueAt(isy);
3965 // Convert from microseconds to milliseconds with rounding
3966 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3967 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07003968 final Timer bgTimer = timer.getSubTimer();
3969 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003970 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07003971 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003972 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003973 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07003974 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003975 }
3976 }
3977
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003978 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3979 for (int ij=jobs.size()-1; ij>=0; ij--) {
3980 final Timer timer = jobs.valueAt(ij);
3981 // Convert from microseconds to milliseconds with rounding
3982 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3983 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07003984 final Timer bgTimer = timer.getSubTimer();
3985 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003986 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07003987 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003988 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003989 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07003990 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003991 }
3992 }
3993
Dianne Hackborn94326cb2017-06-28 16:17:20 -07003994 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
3995 for (int ic=completions.size()-1; ic>=0; ic--) {
3996 SparseIntArray types = completions.valueAt(ic);
3997 if (types != null) {
3998 dumpLine(pw, uid, category, JOB_COMPLETION_DATA,
3999 "\"" + completions.keyAt(ic) + "\"",
4000 types.get(JobParameters.REASON_CANCELED, 0),
4001 types.get(JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, 0),
4002 types.get(JobParameters.REASON_PREEMPT, 0),
4003 types.get(JobParameters.REASON_TIMEOUT, 0),
4004 types.get(JobParameters.REASON_DEVICE_IDLE, 0));
4005 }
4006 }
4007
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004008 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
4009 rawRealtime, which);
4010 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
4011 rawRealtime, which);
4012 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
4013 rawRealtime, which);
4014 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
4015 rawRealtime, which);
4016
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004017 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4018 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004019 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004020 final Uid.Sensor se = sensors.valueAt(ise);
4021 final int sensorNumber = sensors.keyAt(ise);
4022 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004023 if (timer != null) {
4024 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004025 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4026 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004027 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004028 final int count = timer.getCountLocked(which);
4029 final Timer bgTimer = se.getSensorBackgroundTime();
4030 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004031 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4032 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4033 final long bgActualTime = bgTimer != null ?
4034 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4035 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
4036 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004037 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004038 }
4039 }
4040
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004041 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
4042 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004043
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07004044 dumpTimer(pw, uid, category, FOREGROUND_ACTIVITY_DATA, u.getForegroundActivityTimer(),
4045 rawRealtime, which);
4046
4047 dumpTimer(pw, uid, category, FOREGROUND_SERVICE_DATA, u.getForegroundServiceTimer(),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004048 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004049
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004050 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07004051 long totalStateTime = 0;
4052 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08004053 final long time = u.getProcessStateTime(ips, rawRealtime, which);
4054 totalStateTime += time;
4055 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004056 }
4057 if (totalStateTime > 0) {
4058 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
4059 }
4060
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004061 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4062 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004063 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004064 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004065 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004066 }
4067
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004068 // If the cpuFreqs is null, then don't bother checking for cpu freq times.
4069 if (cpuFreqs != null) {
4070 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
4071 // If total cpuFreqTimes is null, then we don't need to check for
4072 // screenOffCpuFreqTimes.
4073 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
4074 sb.setLength(0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004075 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004076 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004077 }
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004078 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
4079 if (screenOffCpuFreqTimeMs != null) {
4080 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
4081 sb.append("," + screenOffCpuFreqTimeMs[i]);
4082 }
4083 } else {
4084 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
4085 sb.append(",0");
4086 }
4087 }
4088 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
4089 cpuFreqTimeMs.length, sb.toString());
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004090 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -08004091
4092 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
4093 final long[] timesMs = u.getCpuFreqTimes(which, procState);
4094 if (timesMs != null && timesMs.length == cpuFreqs.length) {
4095 sb.setLength(0);
4096 for (int i = 0; i < timesMs.length; ++i) {
4097 sb.append((i == 0 ? "" : ",") + timesMs[i]);
4098 }
4099 final long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(
4100 which, procState);
4101 if (screenOffTimesMs != null) {
4102 for (int i = 0; i < screenOffTimesMs.length; ++i) {
4103 sb.append("," + screenOffTimesMs[i]);
4104 }
4105 } else {
4106 for (int i = 0; i < timesMs.length; ++i) {
4107 sb.append(",0");
4108 }
4109 }
4110 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA,
4111 Uid.UID_PROCESS_TYPES[procState], timesMs.length, sb.toString());
4112 }
4113 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004114 }
4115
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004116 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4117 = u.getProcessStats();
4118 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4119 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004120
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004121 final long userMillis = ps.getUserTime(which);
4122 final long systemMillis = ps.getSystemTime(which);
4123 final long foregroundMillis = ps.getForegroundTime(which);
4124 final int starts = ps.getStarts(which);
4125 final int numCrashes = ps.getNumCrashes(which);
4126 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004127
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004128 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
4129 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004130 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
4131 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004132 }
4133 }
4134
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004135 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4136 = u.getPackageStats();
4137 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4138 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4139 int wakeups = 0;
4140 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4141 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07004142 int count = alarms.valueAt(iwa).getCountLocked(which);
4143 wakeups += count;
4144 String name = alarms.keyAt(iwa).replace(',', '_');
4145 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004146 }
4147 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4148 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4149 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4150 final long startTime = ss.getStartTime(batteryUptime, which);
4151 final int starts = ss.getStarts(which);
4152 final int launches = ss.getLaunches(which);
4153 if (startTime != 0 || starts != 0 || launches != 0) {
4154 dumpLine(pw, uid, category, APK_DATA,
4155 wakeups, // wakeup alarms
4156 packageStats.keyAt(ipkg), // Apk
4157 serviceStats.keyAt(isvc), // service
4158 startTime / 1000, // time spent started, in ms
4159 starts,
4160 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004161 }
4162 }
4163 }
4164 }
4165 }
4166
Dianne Hackborn81038902012-11-26 17:04:09 -08004167 static final class TimerEntry {
4168 final String mName;
4169 final int mId;
4170 final BatteryStats.Timer mTimer;
4171 final long mTime;
4172 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
4173 mName = name;
4174 mId = id;
4175 mTimer = timer;
4176 mTime = time;
4177 }
4178 }
4179
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004180 private void printmAh(PrintWriter printer, double power) {
4181 printer.print(BatteryStatsHelper.makemAh(power));
4182 }
4183
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004184 private void printmAh(StringBuilder sb, double power) {
4185 sb.append(BatteryStatsHelper.makemAh(power));
4186 }
4187
Dianne Hackbornd953c532014-08-16 18:17:38 -07004188 /**
4189 * Temporary for settings.
4190 */
4191 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
4192 int reqUid) {
4193 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
4194 }
4195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004196 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004197 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07004198 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004199 final long rawUptime = SystemClock.uptimeMillis() * 1000;
4200 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07004201 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004202 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004203
4204 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
4205 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
4206 final long totalRealtime = computeRealtime(rawRealtime, which);
4207 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004208 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
4209 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
4210 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004211 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
4212 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -07004213 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004214
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004215 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07004216
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004217 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07004218 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004219
Adam Lesinskif9b20a92016-06-17 17:30:01 -07004220 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
4221 if (estimatedBatteryCapacity > 0) {
4222 sb.setLength(0);
4223 sb.append(prefix);
4224 sb.append(" Estimated battery capacity: ");
4225 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
4226 sb.append(" mAh");
4227 pw.println(sb.toString());
4228 }
4229
Jocelyn Dangc627d102017-04-14 13:15:14 -07004230 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
4231 if (minLearnedBatteryCapacity > 0) {
4232 sb.setLength(0);
4233 sb.append(prefix);
4234 sb.append(" Min learned battery capacity: ");
4235 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
4236 sb.append(" mAh");
4237 pw.println(sb.toString());
4238 }
4239 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
4240 if (maxLearnedBatteryCapacity > 0) {
4241 sb.setLength(0);
4242 sb.append(prefix);
4243 sb.append(" Max learned battery capacity: ");
4244 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
4245 sb.append(" mAh");
4246 pw.println(sb.toString());
4247 }
4248
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004249 sb.setLength(0);
4250 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004251 sb.append(" Time on battery: ");
4252 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
4253 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
4254 sb.append(") realtime, ");
4255 formatTimeMs(sb, whichBatteryUptime / 1000);
4256 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, whichBatteryRealtime));
4257 sb.append(") uptime");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004258 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004259
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004260 sb.setLength(0);
4261 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004262 sb.append(" Time on battery screen off: ");
4263 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
4264 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, whichBatteryRealtime));
4265 sb.append(") realtime, ");
4266 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
4267 sb.append("(");
4268 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, whichBatteryRealtime));
4269 sb.append(") uptime");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004270 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004271
4272 sb.setLength(0);
4273 sb.append(prefix);
4274 sb.append(" Time on battery screen doze: ");
4275 formatTimeMs(sb, screenDozeTime / 1000); sb.append("(");
4276 sb.append(formatRatioLocked(screenDozeTime, whichBatteryRealtime));
4277 sb.append(")");
4278 pw.println(sb.toString());
4279
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004280 sb.setLength(0);
4281 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004282 sb.append(" Total run time: ");
4283 formatTimeMs(sb, totalRealtime / 1000);
4284 sb.append("realtime, ");
4285 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004286 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07004287 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004288 if (batteryTimeRemaining >= 0) {
4289 sb.setLength(0);
4290 sb.append(prefix);
4291 sb.append(" Battery time remaining: ");
4292 formatTimeMs(sb, batteryTimeRemaining / 1000);
4293 pw.println(sb.toString());
4294 }
4295 if (chargeTimeRemaining >= 0) {
4296 sb.setLength(0);
4297 sb.append(prefix);
4298 sb.append(" Charge time remaining: ");
4299 formatTimeMs(sb, chargeTimeRemaining / 1000);
4300 pw.println(sb.toString());
4301 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004302
Kweku Adams87b19ec2017-10-09 12:40:03 -07004303 final long dischargeCount = getUahDischarge(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004304 if (dischargeCount >= 0) {
4305 sb.setLength(0);
4306 sb.append(prefix);
4307 sb.append(" Discharge: ");
4308 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
4309 sb.append(" mAh");
4310 pw.println(sb.toString());
4311 }
4312
Kweku Adams87b19ec2017-10-09 12:40:03 -07004313 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004314 if (dischargeScreenOffCount >= 0) {
4315 sb.setLength(0);
4316 sb.append(prefix);
4317 sb.append(" Screen off discharge: ");
4318 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
4319 sb.append(" mAh");
4320 pw.println(sb.toString());
4321 }
4322
Kweku Adams87b19ec2017-10-09 12:40:03 -07004323 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Mac2f518a2017-09-19 16:06:03 -07004324 if (dischargeScreenDozeCount >= 0) {
4325 sb.setLength(0);
4326 sb.append(prefix);
4327 sb.append(" Screen doze discharge: ");
4328 sb.append(BatteryStatsHelper.makemAh(dischargeScreenDozeCount / 1000.0));
4329 sb.append(" mAh");
4330 pw.println(sb.toString());
4331 }
4332
4333 final long dischargeScreenOnCount =
4334 dischargeCount - dischargeScreenOffCount - dischargeScreenDozeCount;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004335 if (dischargeScreenOnCount >= 0) {
4336 sb.setLength(0);
4337 sb.append(prefix);
4338 sb.append(" Screen on discharge: ");
4339 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
4340 sb.append(" mAh");
4341 pw.println(sb.toString());
4342 }
4343
Mike Ma15313c92017-11-15 17:58:21 -08004344 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
4345 if (dischargeLightDozeCount >= 0) {
4346 sb.setLength(0);
4347 sb.append(prefix);
4348 sb.append(" Device light doze discharge: ");
4349 sb.append(BatteryStatsHelper.makemAh(dischargeLightDozeCount / 1000.0));
4350 sb.append(" mAh");
4351 pw.println(sb.toString());
4352 }
4353
4354 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
4355 if (dischargeDeepDozeCount >= 0) {
4356 sb.setLength(0);
4357 sb.append(prefix);
4358 sb.append(" Device deep doze discharge: ");
4359 sb.append(BatteryStatsHelper.makemAh(dischargeDeepDozeCount / 1000.0));
4360 sb.append(" mAh");
4361 pw.println(sb.toString());
4362 }
4363
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004364 pw.print(" Start clock time: ");
4365 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
4366
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004367 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004368 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004369 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004370 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
4371 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004372 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004373 rawRealtime, which);
4374 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
4375 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004376 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004377 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004378 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
4379 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
4380 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004381 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004382 sb.append(prefix);
4383 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
4384 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004385 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004386 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
4387 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004388 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004389 pw.println(sb.toString());
4390 sb.setLength(0);
4391 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004392 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004393 boolean didOne = false;
4394 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004395 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004396 if (time == 0) {
4397 continue;
4398 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004399 sb.append("\n ");
4400 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004401 didOne = true;
4402 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
4403 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004404 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004405 sb.append("(");
4406 sb.append(formatRatioLocked(time, screenOnTime));
4407 sb.append(")");
4408 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004409 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004410 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004411 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004412 sb.setLength(0);
4413 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004414 sb.append(" Power save mode enabled: ");
4415 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004416 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004417 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004418 sb.append(")");
4419 pw.println(sb.toString());
4420 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004421 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004422 sb.setLength(0);
4423 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004424 sb.append(" Device light idling: ");
4425 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004426 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004427 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
4428 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004429 sb.append("x");
4430 pw.println(sb.toString());
4431 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004432 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004433 sb.setLength(0);
4434 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004435 sb.append(" Idle mode light time: ");
4436 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004437 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004438 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
4439 sb.append(") ");
4440 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004441 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004442 sb.append(" -- longest ");
4443 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
4444 pw.println(sb.toString());
4445 }
4446 if (deviceIdlingTime != 0) {
4447 sb.setLength(0);
4448 sb.append(prefix);
4449 sb.append(" Device full idling: ");
4450 formatTimeMs(sb, deviceIdlingTime / 1000);
4451 sb.append("(");
4452 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004453 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004454 sb.append("x");
4455 pw.println(sb.toString());
4456 }
4457 if (deviceIdleModeFullTime != 0) {
4458 sb.setLength(0);
4459 sb.append(prefix);
4460 sb.append(" Idle mode full time: ");
4461 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
4462 sb.append("(");
4463 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
4464 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004465 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004466 sb.append("x");
4467 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004468 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004469 pw.println(sb.toString());
4470 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004471 if (phoneOnTime != 0) {
4472 sb.setLength(0);
4473 sb.append(prefix);
4474 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
4475 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004476 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004477 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004478 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004479 if (connChanges != 0) {
4480 pw.print(prefix);
4481 pw.print(" Connectivity changes: "); pw.println(connChanges);
4482 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004483
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004484 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07004485 long fullWakeLockTimeTotalMicros = 0;
4486 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08004487
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004488 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08004489
Evan Millar22ac0432009-03-31 11:33:18 -07004490 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004491 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004492
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004493 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4494 = u.getWakelockStats();
4495 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4496 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07004497
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004498 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
4499 if (fullWakeTimer != null) {
4500 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
4501 rawRealtime, which);
4502 }
4503
4504 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4505 if (partialWakeTimer != null) {
4506 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
4507 rawRealtime, which);
4508 if (totalTimeMicros > 0) {
4509 if (reqUid < 0) {
4510 // Only show the ordered list of all wake
4511 // locks if the caller is not asking for data
4512 // about a specific uid.
4513 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
4514 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08004515 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004516 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07004517 }
4518 }
4519 }
4520 }
Bookatzc8c44962017-05-11 12:12:54 -07004521
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004522 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4523 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4524 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4525 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4526 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4527 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4528 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4529 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004530 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4531 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004532
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004533 if (fullWakeLockTimeTotalMicros != 0) {
4534 sb.setLength(0);
4535 sb.append(prefix);
4536 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4537 (fullWakeLockTimeTotalMicros + 500) / 1000);
4538 pw.println(sb.toString());
4539 }
4540
4541 if (partialWakeLockTimeTotalMicros != 0) {
4542 sb.setLength(0);
4543 sb.append(prefix);
4544 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4545 (partialWakeLockTimeTotalMicros + 500) / 1000);
4546 pw.println(sb.toString());
4547 }
4548
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004549 final long multicastWakeLockTimeTotalMicros =
4550 getWifiMulticastWakelockTime(rawRealtime, which);
4551 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004552 if (multicastWakeLockTimeTotalMicros != 0) {
4553 sb.setLength(0);
4554 sb.append(prefix);
4555 sb.append(" Total WiFi Multicast wakelock Count: ");
4556 sb.append(multicastWakeLockCountTotal);
4557 pw.println(sb.toString());
4558
4559 sb.setLength(0);
4560 sb.append(prefix);
4561 sb.append(" Total WiFi Multicast wakelock time: ");
4562 formatTimeMsNoSpace(sb, (multicastWakeLockTimeTotalMicros + 500) / 1000);
4563 pw.println(sb.toString());
4564 }
4565
Siddharth Ray3c648c42017-10-02 17:30:58 -07004566 pw.println("");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004567 pw.print(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004568 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004569 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004570 sb.append(" CONNECTIVITY POWER SUMMARY START");
4571 pw.println(sb.toString());
4572
4573 pw.print(prefix);
4574 sb.setLength(0);
4575 sb.append(prefix);
4576 sb.append(" Logging duration for connectivity statistics: ");
4577 formatTimeMs(sb, whichBatteryRealtime / 1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004578 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004579
4580 sb.setLength(0);
4581 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004582 sb.append(" Cellular Statistics:");
Amith Yamasanif37447b2009-10-08 18:28:01 -07004583 pw.println(sb.toString());
4584
Siddharth Ray3c648c42017-10-02 17:30:58 -07004585 pw.print(prefix);
4586 sb.setLength(0);
4587 sb.append(prefix);
4588 sb.append(" Cellular kernel active time: ");
4589 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
4590 formatTimeMs(sb, mobileActiveTime / 1000);
4591 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4592 sb.append(")");
4593 pw.println(sb.toString());
4594
4595 pw.print(" Cellular data received: "); pw.println(formatBytesLocked(mobileRxTotalBytes));
4596 pw.print(" Cellular data sent: "); pw.println(formatBytesLocked(mobileTxTotalBytes));
4597 pw.print(" Cellular packets received: "); pw.println(mobileRxTotalPackets);
4598 pw.print(" Cellular packets sent: "); pw.println(mobileTxTotalPackets);
4599
Dianne Hackborn627bba72009-03-24 22:32:56 -07004600 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004601 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004602 sb.append(" Cellular Radio Access Technology:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004603 didOne = false;
4604 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004605 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004606 if (time == 0) {
4607 continue;
4608 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004609 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004610 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004611 didOne = true;
4612 sb.append(DATA_CONNECTION_NAMES[i]);
4613 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004614 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004615 sb.append("(");
4616 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004617 sb.append(") ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004618 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004619 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004620 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004621
4622 sb.setLength(0);
4623 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004624 sb.append(" Cellular Rx signal strength (RSRP):");
4625 final String[] cellularRxSignalStrengthDescription = new String[]{
4626 "very poor (less than -128dBm): ",
4627 "poor (-128dBm to -118dBm): ",
4628 "moderate (-118dBm to -108dBm): ",
4629 "good (-108dBm to -98dBm): ",
4630 "great (greater than -98dBm): "};
4631 didOne = false;
4632 final int numCellularRxBins = Math.min(SignalStrength.NUM_SIGNAL_STRENGTH_BINS,
4633 cellularRxSignalStrengthDescription.length);
4634 for (int i=0; i<numCellularRxBins; i++) {
4635 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
4636 if (time == 0) {
4637 continue;
4638 }
4639 sb.append("\n ");
4640 sb.append(prefix);
4641 didOne = true;
4642 sb.append(cellularRxSignalStrengthDescription[i]);
4643 sb.append(" ");
4644 formatTimeMs(sb, time/1000);
4645 sb.append("(");
4646 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4647 sb.append(") ");
4648 }
4649 if (!didOne) sb.append(" (no activity)");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004650 pw.println(sb.toString());
4651
Siddharth Ray3c648c42017-10-02 17:30:58 -07004652 printControllerActivity(pw, sb, prefix, "Cellular",
4653 getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004654
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004655 pw.print(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004656 sb.setLength(0);
4657 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004658 sb.append(" Wifi Statistics:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004659 pw.println(sb.toString());
4660
Siddharth Ray3c648c42017-10-02 17:30:58 -07004661 pw.print(" Wifi data received: "); pw.println(formatBytesLocked(wifiRxTotalBytes));
4662 pw.print(" Wifi data sent: "); pw.println(formatBytesLocked(wifiTxTotalBytes));
4663 pw.print(" Wifi packets received: "); pw.println(wifiRxTotalPackets);
4664 pw.print(" Wifi packets sent: "); pw.println(wifiTxTotalPackets);
4665
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004666 sb.setLength(0);
4667 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004668 sb.append(" Wifi states:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004669 didOne = false;
4670 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004671 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004672 if (time == 0) {
4673 continue;
4674 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004675 sb.append("\n ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004676 didOne = true;
4677 sb.append(WIFI_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 supplicant states:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004690 didOne = false;
4691 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4692 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4693 if (time == 0) {
4694 continue;
4695 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004696 sb.append("\n ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004697 didOne = true;
4698 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4699 sb.append(" ");
4700 formatTimeMs(sb, time/1000);
4701 sb.append("(");
4702 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4703 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004704 }
4705 if (!didOne) sb.append(" (no activity)");
4706 pw.println(sb.toString());
4707
4708 sb.setLength(0);
4709 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004710 sb.append(" Wifi Rx signal strength (RSSI):");
4711 final String[] wifiRxSignalStrengthDescription = new String[]{
4712 "very poor (less than -88.75dBm): ",
4713 "poor (-88.75 to -77.5dBm): ",
4714 "moderate (-77.5dBm to -66.25dBm): ",
4715 "good (-66.25dBm to -55dBm): ",
4716 "great (greater than -55dBm): "};
Dianne Hackborn3251b902014-06-20 14:40:53 -07004717 didOne = false;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004718 final int numWifiRxBins = Math.min(NUM_WIFI_SIGNAL_STRENGTH_BINS,
4719 wifiRxSignalStrengthDescription.length);
4720 for (int i=0; i<numWifiRxBins; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004721 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4722 if (time == 0) {
4723 continue;
4724 }
4725 sb.append("\n ");
4726 sb.append(prefix);
4727 didOne = true;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004728 sb.append(" ");
4729 sb.append(wifiRxSignalStrengthDescription[i]);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004730 formatTimeMs(sb, time/1000);
4731 sb.append("(");
4732 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4733 sb.append(") ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004734 }
4735 if (!didOne) sb.append(" (no activity)");
4736 pw.println(sb.toString());
4737
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004738 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004739
Adam Lesinski50e47602015-12-04 17:04:54 -08004740 pw.print(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004741 sb.setLength(0);
4742 sb.append(prefix);
4743 sb.append(" CONNECTIVITY POWER SUMMARY END");
4744 pw.println(sb.toString());
4745 pw.println("");
4746
4747 pw.print(prefix);
Adam Lesinski50e47602015-12-04 17:04:54 -08004748 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4749 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4750
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004751 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4752 sb.setLength(0);
4753 sb.append(prefix);
4754 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4755 pw.println(sb.toString());
4756
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004757 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4758 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004759
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004760 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004761
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004762 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004763 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004764 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004765 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004766 pw.println(getDischargeStartLevel());
4767 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4768 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004769 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004770 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004771 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004772 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004773 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004774 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004775 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004776 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004777 pw.println(getDischargeAmountScreenOn());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004778 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004779 pw.println(getDischargeAmountScreenOff());
4780 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4781 pw.println(getDischargeAmountScreenDoze());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004782 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004783 } else {
4784 pw.print(prefix); pw.println(" Device battery use since last full charge");
4785 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004786 pw.println(getLowDischargeAmountSinceCharge());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004787 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004788 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004789 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004790 pw.println(getDischargeAmountScreenOnSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004791 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004792 pw.println(getDischargeAmountScreenOffSinceCharge());
4793 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4794 pw.println(getDischargeAmountScreenDozeSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004795 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004796 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004797
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004798 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004799 helper.create(this);
4800 helper.refreshStats(which, UserHandle.USER_ALL);
4801 List<BatterySipper> sippers = helper.getUsageList();
4802 if (sippers != null && sippers.size() > 0) {
4803 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4804 pw.print(prefix); pw.print(" Capacity: ");
4805 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004806 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004807 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4808 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4809 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4810 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004811 pw.println();
4812 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004813 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004814 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004815 switch (bs.drainType) {
4816 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004817 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004818 break;
4819 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004820 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004821 break;
4822 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004823 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004824 break;
4825 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004826 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004827 break;
4828 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004829 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004830 break;
4831 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004832 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004833 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004834 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004835 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004836 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004837 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004838 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004839 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004840 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004841 break;
4842 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004843 pw.print(" User "); pw.print(bs.userId);
4844 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004845 break;
4846 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004847 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004848 break;
4849 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004850 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004851 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004852 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004853 pw.print(" Camera: ");
4854 break;
4855 default:
4856 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004857 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004858 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004859 printmAh(pw, bs.totalPowerMah);
4860
Adam Lesinski57123002015-06-12 16:12:07 -07004861 if (bs.usagePowerMah != bs.totalPowerMah) {
4862 // If the usage (generic power) isn't the whole amount, we list out
4863 // what components are involved in the calculation.
4864
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004865 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004866 if (bs.usagePowerMah != 0) {
4867 pw.print(" usage=");
4868 printmAh(pw, bs.usagePowerMah);
4869 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004870 if (bs.cpuPowerMah != 0) {
4871 pw.print(" cpu=");
4872 printmAh(pw, bs.cpuPowerMah);
4873 }
4874 if (bs.wakeLockPowerMah != 0) {
4875 pw.print(" wake=");
4876 printmAh(pw, bs.wakeLockPowerMah);
4877 }
4878 if (bs.mobileRadioPowerMah != 0) {
4879 pw.print(" radio=");
4880 printmAh(pw, bs.mobileRadioPowerMah);
4881 }
4882 if (bs.wifiPowerMah != 0) {
4883 pw.print(" wifi=");
4884 printmAh(pw, bs.wifiPowerMah);
4885 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004886 if (bs.bluetoothPowerMah != 0) {
4887 pw.print(" bt=");
4888 printmAh(pw, bs.bluetoothPowerMah);
4889 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004890 if (bs.gpsPowerMah != 0) {
4891 pw.print(" gps=");
4892 printmAh(pw, bs.gpsPowerMah);
4893 }
4894 if (bs.sensorPowerMah != 0) {
4895 pw.print(" sensor=");
4896 printmAh(pw, bs.sensorPowerMah);
4897 }
4898 if (bs.cameraPowerMah != 0) {
4899 pw.print(" camera=");
4900 printmAh(pw, bs.cameraPowerMah);
4901 }
4902 if (bs.flashlightPowerMah != 0) {
4903 pw.print(" flash=");
4904 printmAh(pw, bs.flashlightPowerMah);
4905 }
4906 pw.print(" )");
4907 }
Bookatz17d7d9d2017-06-08 14:50:46 -07004908
4909 // If there is additional smearing information, include it.
4910 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
4911 pw.print(" Including smearing: ");
4912 printmAh(pw, bs.totalSmearedPowerMah);
4913 pw.print(" (");
4914 if (bs.screenPowerMah != 0) {
4915 pw.print(" screen=");
4916 printmAh(pw, bs.screenPowerMah);
4917 }
4918 if (bs.proportionalSmearMah != 0) {
4919 pw.print(" proportional=");
4920 printmAh(pw, bs.proportionalSmearMah);
4921 }
4922 pw.print(" )");
4923 }
4924 if (bs.shouldHide) {
4925 pw.print(" Excluded from smearing");
4926 }
4927
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004928 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004929 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004930 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004931 }
4932
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004933 sippers = helper.getMobilemsppList();
4934 if (sippers != null && sippers.size() > 0) {
4935 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004936 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004937 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004938 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004939 sb.setLength(0);
4940 sb.append(prefix); sb.append(" Uid ");
4941 UserHandle.formatUid(sb, bs.uidObj.getUid());
4942 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4943 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4944 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004945 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004946 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004947 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004948 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004949 sb.setLength(0);
4950 sb.append(prefix);
4951 sb.append(" TOTAL TIME: ");
4952 formatTimeMs(sb, totalTime);
4953 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4954 sb.append(")");
4955 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004956 pw.println();
4957 }
4958
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004959 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4960 @Override
4961 public int compare(TimerEntry lhs, TimerEntry rhs) {
4962 long lhsTime = lhs.mTime;
4963 long rhsTime = rhs.mTime;
4964 if (lhsTime < rhsTime) {
4965 return 1;
4966 }
4967 if (lhsTime > rhsTime) {
4968 return -1;
4969 }
4970 return 0;
4971 }
4972 };
4973
4974 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004975 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4976 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004977 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004978 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4979 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4980 : kernelWakelocks.entrySet()) {
4981 final BatteryStats.Timer timer = ent.getValue();
4982 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004983 if (totalTimeMillis > 0) {
4984 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4985 }
4986 }
4987 if (ktimers.size() > 0) {
4988 Collections.sort(ktimers, timerComparator);
4989 pw.print(prefix); pw.println(" All kernel wake locks:");
4990 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004991 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004992 String linePrefix = ": ";
4993 sb.setLength(0);
4994 sb.append(prefix);
4995 sb.append(" Kernel Wake lock ");
4996 sb.append(timer.mName);
4997 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4998 which, linePrefix);
4999 if (!linePrefix.equals(": ")) {
5000 sb.append(" realtime");
5001 // Only print out wake locks that were held
5002 pw.println(sb.toString());
5003 }
5004 }
5005 pw.println();
5006 }
5007 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005008
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005009 if (timers.size() > 0) {
5010 Collections.sort(timers, timerComparator);
5011 pw.print(prefix); pw.println(" All partial wake locks:");
5012 for (int i=0; i<timers.size(); i++) {
5013 TimerEntry timer = timers.get(i);
5014 sb.setLength(0);
5015 sb.append(" Wake lock ");
5016 UserHandle.formatUid(sb, timer.mId);
5017 sb.append(" ");
5018 sb.append(timer.mName);
5019 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5020 sb.append(" realtime");
5021 pw.println(sb.toString());
5022 }
5023 timers.clear();
5024 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08005025 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005026
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005027 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005028 if (wakeupReasons.size() > 0) {
5029 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005030 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005031 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005032 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005033 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
5034 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005035 }
5036 Collections.sort(reasons, timerComparator);
5037 for (int i=0; i<reasons.size(); i++) {
5038 TimerEntry timer = reasons.get(i);
5039 String linePrefix = ": ";
5040 sb.setLength(0);
5041 sb.append(prefix);
5042 sb.append(" Wakeup reason ");
5043 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005044 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5045 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005046 pw.println(sb.toString());
5047 }
5048 pw.println();
5049 }
Dianne Hackborn81038902012-11-26 17:04:09 -08005050 }
Evan Millar22ac0432009-03-31 11:33:18 -07005051
James Carr2dd7e5e2016-07-20 18:48:39 -07005052 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
Bookatz50df7112017-08-04 14:53:26 -07005053 if (mMemoryStats.size() > 0) {
5054 pw.println(" Memory Stats");
5055 for (int i = 0; i < mMemoryStats.size(); i++) {
5056 sb.setLength(0);
5057 sb.append(" Bandwidth ");
5058 sb.append(mMemoryStats.keyAt(i));
5059 sb.append(" Time ");
5060 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
5061 pw.println(sb.toString());
5062 }
5063 pw.println();
5064 }
5065
5066 final Map<String, ? extends Timer> rpmStats = getRpmStats();
5067 if (rpmStats.size() > 0) {
5068 pw.print(prefix); pw.println(" Resource Power Manager Stats");
5069 if (rpmStats.size() > 0) {
5070 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
5071 final String timerName = ent.getKey();
5072 final Timer timer = ent.getValue();
5073 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5074 }
5075 }
5076 pw.println();
5077 }
Bookatz82b341172017-09-07 19:06:08 -07005078 if (SCREEN_OFF_RPM_STATS_ENABLED) {
5079 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
Bookatz50df7112017-08-04 14:53:26 -07005080 if (screenOffRpmStats.size() > 0) {
Bookatz82b341172017-09-07 19:06:08 -07005081 pw.print(prefix);
5082 pw.println(" Resource Power Manager Stats for when screen was off");
5083 if (screenOffRpmStats.size() > 0) {
5084 for (Map.Entry<String, ? extends Timer> ent : screenOffRpmStats.entrySet()) {
5085 final String timerName = ent.getKey();
5086 final Timer timer = ent.getValue();
5087 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5088 }
Bookatz50df7112017-08-04 14:53:26 -07005089 }
Bookatz82b341172017-09-07 19:06:08 -07005090 pw.println();
Bookatz50df7112017-08-04 14:53:26 -07005091 }
James Carr2dd7e5e2016-07-20 18:48:39 -07005092 }
5093
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005094 final long[] cpuFreqs = getCpuFreqs();
5095 if (cpuFreqs != null) {
5096 sb.setLength(0);
Bookatz50df7112017-08-04 14:53:26 -07005097 sb.append(" CPU freqs:");
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005098 for (int i = 0; i < cpuFreqs.length; ++i) {
5099 sb.append(" " + cpuFreqs[i]);
5100 }
5101 pw.println(sb.toString());
Bookatz50df7112017-08-04 14:53:26 -07005102 pw.println();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005103 }
5104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005105 for (int iu=0; iu<NU; iu++) {
5106 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005107 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005108 continue;
5109 }
Bookatzc8c44962017-05-11 12:12:54 -07005110
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005111 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07005112
5113 pw.print(prefix);
5114 pw.print(" ");
5115 UserHandle.formatUid(pw, uid);
5116 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005117 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005118
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005119 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
5120 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
5121 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
5122 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005123 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
5124 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
5125
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005126 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
5127 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005128 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
5129 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005130
5131 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
5132 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
5133
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005134 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
5135 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
5136 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08005137 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
5138 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5139 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
5140 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005141 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005142
Adam Lesinski5f056f62016-07-14 16:56:08 -07005143 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
5144 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
5145
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005146 if (mobileRxBytes > 0 || mobileTxBytes > 0
5147 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005148 pw.print(prefix); pw.print(" Mobile network: ");
5149 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005150 pw.print(formatBytesLocked(mobileTxBytes));
5151 pw.print(" sent (packets "); pw.print(mobileRxPackets);
5152 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005153 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005154 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
5155 sb.setLength(0);
5156 sb.append(prefix); sb.append(" Mobile radio active: ");
5157 formatTimeMs(sb, uidMobileActiveTime / 1000);
5158 sb.append("(");
5159 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
5160 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
5161 long packets = mobileRxPackets + mobileTxPackets;
5162 if (packets == 0) {
5163 packets = 1;
5164 }
5165 sb.append(" @ ");
5166 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
5167 sb.append(" mspp");
5168 pw.println(sb.toString());
5169 }
5170
Adam Lesinski5f056f62016-07-14 16:56:08 -07005171 if (mobileWakeup > 0) {
5172 sb.setLength(0);
5173 sb.append(prefix);
5174 sb.append(" Mobile radio AP wakeups: ");
5175 sb.append(mobileWakeup);
5176 pw.println(sb.toString());
5177 }
5178
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005179 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
5180 u.getModemControllerActivity(), which);
5181
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005182 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005183 pw.print(prefix); pw.print(" Wi-Fi network: ");
5184 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005185 pw.print(formatBytesLocked(wifiTxBytes));
5186 pw.print(" sent (packets "); pw.print(wifiRxPackets);
5187 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005188 }
5189
Dianne Hackborn62793e42015-03-09 11:15:41 -07005190 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08005191 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005192 || uidWifiRunningTime != 0) {
5193 sb.setLength(0);
5194 sb.append(prefix); sb.append(" Wifi Running: ");
5195 formatTimeMs(sb, uidWifiRunningTime / 1000);
5196 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
5197 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07005198 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005199 formatTimeMs(sb, fullWifiLockOnTime / 1000);
5200 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
5201 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08005202 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005203 formatTimeMs(sb, wifiScanTime / 1000);
5204 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07005205 whichBatteryRealtime)); sb.append(") ");
5206 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08005207 sb.append("x\n");
5208 // actual and background times are unpooled and since reset (regardless of 'which')
5209 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
5210 formatTimeMs(sb, wifiScanActualTime / 1000);
5211 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
5212 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5213 sb.append(") ");
5214 sb.append(wifiScanCount);
5215 sb.append("x\n");
5216 sb.append(prefix); sb.append(" Background Wifi Scan: ");
5217 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
5218 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
5219 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5220 sb.append(") ");
5221 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07005222 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005223 pw.println(sb.toString());
5224 }
5225
Adam Lesinski5f056f62016-07-14 16:56:08 -07005226 if (wifiWakeup > 0) {
5227 sb.setLength(0);
5228 sb.append(prefix);
5229 sb.append(" WiFi AP wakeups: ");
5230 sb.append(wifiWakeup);
5231 pw.println(sb.toString());
5232 }
5233
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005234 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
5235 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07005236
Adam Lesinski50e47602015-12-04 17:04:54 -08005237 if (btRxBytes > 0 || btTxBytes > 0) {
5238 pw.print(prefix); pw.print(" Bluetooth network: ");
5239 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
5240 pw.print(formatBytesLocked(btTxBytes));
5241 pw.println(" sent");
5242 }
5243
Bookatz867c0d72017-03-07 18:23:42 -08005244 final Timer bleTimer = u.getBluetoothScanTimer();
5245 if (bleTimer != null) {
5246 // Convert from microseconds to milliseconds with rounding
5247 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
5248 / 1000;
5249 if (totalTimeMs != 0) {
5250 final int count = bleTimer.getCountLocked(which);
5251 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
5252 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005253 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5254 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
5255 final long actualTimeMsBg = bleTimerBg != null ?
5256 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005257 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07005258 final int resultCount = u.getBluetoothScanResultCounter() != null ?
5259 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005260 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
5261 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
5262 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
5263 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
5264 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
5265 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5266 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
5267 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
5268 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
5269 final Timer unoptimizedScanTimerBg =
5270 u.getBluetoothUnoptimizedScanBackgroundTimer();
5271 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
5272 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5273 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
5274 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005275
5276 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08005277 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07005278 sb.append(prefix);
5279 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08005280 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07005281 sb.append(" (");
5282 sb.append(count);
5283 sb.append(" times)");
5284 if (bleTimer.isRunningLocked()) {
5285 sb.append(" (currently running)");
5286 }
5287 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005288 }
Bookatzb1f04f32017-05-19 13:57:32 -07005289
5290 sb.append(prefix);
5291 sb.append(" Bluetooth Scan (total actual realtime): ");
5292 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
5293 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005294 sb.append(count);
5295 sb.append(" times)");
5296 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07005297 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08005298 }
Bookatzb1f04f32017-05-19 13:57:32 -07005299 sb.append("\n");
5300 if (actualTimeMsBg > 0 || countBg > 0) {
5301 sb.append(prefix);
5302 sb.append(" Bluetooth Scan (background realtime): ");
5303 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
5304 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005305 sb.append(countBg);
5306 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07005307 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
5308 sb.append(" (currently running in background)");
5309 }
5310 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005311 }
Bookatzb1f04f32017-05-19 13:57:32 -07005312
5313 sb.append(prefix);
5314 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07005315 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07005316 sb.append(" (");
5317 sb.append(resultCountBg);
5318 sb.append(" in background)");
5319
5320 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
5321 sb.append("\n");
5322 sb.append(prefix);
5323 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
5324 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
5325 sb.append(" (max ");
5326 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
5327 sb.append(")");
5328 if (unoptimizedScanTimer != null
5329 && unoptimizedScanTimer.isRunningLocked()) {
5330 sb.append(" (currently running unoptimized)");
5331 }
5332 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
5333 sb.append("\n");
5334 sb.append(prefix);
5335 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
5336 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
5337 sb.append(" (max ");
5338 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
5339 sb.append(")");
5340 if (unoptimizedScanTimerBg.isRunningLocked()) {
5341 sb.append(" (currently running unoptimized in background)");
5342 }
5343 }
5344 }
Bookatz867c0d72017-03-07 18:23:42 -08005345 pw.println(sb.toString());
5346 uidActivity = true;
5347 }
5348 }
5349
5350
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005351
Dianne Hackborn617f8772009-03-31 15:04:46 -07005352 if (u.hasUserActivity()) {
5353 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07005354 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005355 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005356 if (val != 0) {
5357 if (!hasData) {
5358 sb.setLength(0);
5359 sb.append(" User activity: ");
5360 hasData = true;
5361 } else {
5362 sb.append(", ");
5363 }
5364 sb.append(val);
5365 sb.append(" ");
5366 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
5367 }
5368 }
5369 if (hasData) {
5370 pw.println(sb.toString());
5371 }
5372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005373
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005374 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
5375 = u.getWakelockStats();
5376 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005377 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005378 int countWakelock = 0;
5379 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
5380 final Uid.Wakelock wl = wakelocks.valueAt(iw);
5381 String linePrefix = ": ";
5382 sb.setLength(0);
5383 sb.append(prefix);
5384 sb.append(" Wake lock ");
5385 sb.append(wakelocks.keyAt(iw));
5386 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
5387 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005388 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
5389 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005390 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005391 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
5392 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005393 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
5394 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005395 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
5396 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07005397 sb.append(" realtime");
5398 pw.println(sb.toString());
5399 uidActivity = true;
5400 countWakelock++;
5401
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005402 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
5403 rawRealtime, which);
5404 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
5405 rawRealtime, which);
5406 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
5407 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005408 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07005409 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005410 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005411 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07005412 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
5413 // pooled and therefore just a lower bound)
5414 long actualTotalPartialWakelock = 0;
5415 long actualBgPartialWakelock = 0;
5416 if (u.getAggregatedPartialWakelockTimer() != null) {
5417 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
5418 // Convert from microseconds to milliseconds with rounding
5419 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07005420 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07005421 final Timer bgAggTimer = aggTimer.getSubTimer();
5422 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005423 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07005424 }
5425
5426 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
5427 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
5428 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005429 sb.setLength(0);
5430 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005431 sb.append(" TOTAL wake: ");
5432 boolean needComma = false;
5433 if (totalFullWakelock != 0) {
5434 needComma = true;
5435 formatTimeMs(sb, totalFullWakelock);
5436 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005437 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005438 if (totalPartialWakelock != 0) {
5439 if (needComma) {
5440 sb.append(", ");
5441 }
5442 needComma = true;
5443 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07005444 sb.append("blamed partial");
5445 }
5446 if (actualTotalPartialWakelock != 0) {
5447 if (needComma) {
5448 sb.append(", ");
5449 }
5450 needComma = true;
5451 formatTimeMs(sb, actualTotalPartialWakelock);
5452 sb.append("actual partial");
5453 }
5454 if (actualBgPartialWakelock != 0) {
5455 if (needComma) {
5456 sb.append(", ");
5457 }
5458 needComma = true;
5459 formatTimeMs(sb, actualBgPartialWakelock);
5460 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005461 }
5462 if (totalWindowWakelock != 0) {
5463 if (needComma) {
5464 sb.append(", ");
5465 }
5466 needComma = true;
5467 formatTimeMs(sb, totalWindowWakelock);
5468 sb.append("window");
5469 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005470 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07005471 if (needComma) {
5472 sb.append(",");
5473 }
5474 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005475 formatTimeMs(sb, totalDrawWakelock);
5476 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07005477 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005478 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005479 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005480 }
5481 }
5482
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07005483 // Calculate multicast wakelock stats
5484 final Timer mcTimer = u.getMulticastWakelockStats();
5485 if (mcTimer != null) {
5486 final long multicastWakeLockTimeMicros = mcTimer.getTotalTimeLocked(rawRealtime, which);
5487 final int multicastWakeLockCount = mcTimer.getCountLocked(which);
5488
5489 if (multicastWakeLockTimeMicros > 0) {
5490 sb.setLength(0);
5491 sb.append(prefix);
5492 sb.append(" WiFi Multicast Wakelock");
5493 sb.append(" count = ");
5494 sb.append(multicastWakeLockCount);
5495 sb.append(" time = ");
5496 formatTimeMsNoSpace(sb, (multicastWakeLockTimeMicros + 500) / 1000);
5497 pw.println(sb.toString());
5498 }
5499 }
5500
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005501 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
5502 for (int isy=syncs.size()-1; isy>=0; isy--) {
5503 final Timer timer = syncs.valueAt(isy);
5504 // Convert from microseconds to milliseconds with rounding
5505 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5506 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07005507 final Timer bgTimer = timer.getSubTimer();
5508 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005509 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07005510 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005511 sb.setLength(0);
5512 sb.append(prefix);
5513 sb.append(" Sync ");
5514 sb.append(syncs.keyAt(isy));
5515 sb.append(": ");
5516 if (totalTime != 0) {
5517 formatTimeMs(sb, totalTime);
5518 sb.append("realtime (");
5519 sb.append(count);
5520 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07005521 if (bgTime > 0) {
5522 sb.append(", ");
5523 formatTimeMs(sb, bgTime);
5524 sb.append("background (");
5525 sb.append(bgCount);
5526 sb.append(" times)");
5527 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005528 } else {
5529 sb.append("(not used)");
5530 }
5531 pw.println(sb.toString());
5532 uidActivity = true;
5533 }
5534
5535 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
5536 for (int ij=jobs.size()-1; ij>=0; ij--) {
5537 final Timer timer = jobs.valueAt(ij);
5538 // Convert from microseconds to milliseconds with rounding
5539 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5540 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07005541 final Timer bgTimer = timer.getSubTimer();
5542 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005543 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07005544 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005545 sb.setLength(0);
5546 sb.append(prefix);
5547 sb.append(" Job ");
5548 sb.append(jobs.keyAt(ij));
5549 sb.append(": ");
5550 if (totalTime != 0) {
5551 formatTimeMs(sb, totalTime);
5552 sb.append("realtime (");
5553 sb.append(count);
5554 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07005555 if (bgTime > 0) {
5556 sb.append(", ");
5557 formatTimeMs(sb, bgTime);
5558 sb.append("background (");
5559 sb.append(bgCount);
5560 sb.append(" times)");
5561 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005562 } else {
5563 sb.append("(not used)");
5564 }
5565 pw.println(sb.toString());
5566 uidActivity = true;
5567 }
5568
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005569 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
5570 for (int ic=completions.size()-1; ic>=0; ic--) {
5571 SparseIntArray types = completions.valueAt(ic);
5572 if (types != null) {
5573 pw.print(prefix);
5574 pw.print(" Job Completions ");
5575 pw.print(completions.keyAt(ic));
5576 pw.print(":");
5577 for (int it=0; it<types.size(); it++) {
5578 pw.print(" ");
5579 pw.print(JobParameters.getReasonName(types.keyAt(it)));
5580 pw.print("(");
5581 pw.print(types.valueAt(it));
5582 pw.print("x)");
5583 }
5584 pw.println();
5585 }
5586 }
5587
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005588 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
5589 prefix, "Flashlight");
5590 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
5591 prefix, "Camera");
5592 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
5593 prefix, "Video");
5594 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
5595 prefix, "Audio");
5596
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005597 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
5598 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005599 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005600 final Uid.Sensor se = sensors.valueAt(ise);
5601 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005602 sb.setLength(0);
5603 sb.append(prefix);
5604 sb.append(" Sensor ");
5605 int handle = se.getHandle();
5606 if (handle == Uid.Sensor.GPS) {
5607 sb.append("GPS");
5608 } else {
5609 sb.append(handle);
5610 }
5611 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005612
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005613 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005614 if (timer != null) {
5615 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005616 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5617 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005618 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005619 final Timer bgTimer = se.getSensorBackgroundTime();
5620 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005621 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5622 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5623 final long bgActualTime = bgTimer != null ?
5624 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5625
Dianne Hackborn61659e52014-07-09 16:13:01 -07005626 //timer.logState();
5627 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005628 if (actualTime != totalTime) {
5629 formatTimeMs(sb, totalTime);
5630 sb.append("blamed realtime, ");
5631 }
5632
5633 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005634 sb.append("realtime (");
5635 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005636 sb.append(" times)");
5637
5638 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005639 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005640 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5641 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005642 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005643 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005644 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005645 } else {
5646 sb.append("(not used)");
5647 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005648 } else {
5649 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005650 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005651
5652 pw.println(sb.toString());
5653 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005654 }
5655
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005656 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5657 "Vibrator");
5658 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5659 prefix, "Foreground activities");
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005660 uidActivity |= printTimer(pw, sb, u.getForegroundServiceTimer(), rawRealtime, which,
5661 prefix, "Foreground services");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005662
Dianne Hackborn61659e52014-07-09 16:13:01 -07005663 long totalStateTime = 0;
5664 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5665 long time = u.getProcessStateTime(ips, rawRealtime, which);
5666 if (time > 0) {
5667 totalStateTime += time;
5668 sb.setLength(0);
5669 sb.append(prefix);
5670 sb.append(" ");
5671 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5672 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005673 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005674 pw.println(sb.toString());
5675 uidActivity = true;
5676 }
5677 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005678 if (totalStateTime > 0) {
5679 sb.setLength(0);
5680 sb.append(prefix);
5681 sb.append(" Total running: ");
5682 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5683 pw.println(sb.toString());
5684 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005685
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005686 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5687 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005688 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005689 sb.setLength(0);
5690 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005691 sb.append(" Total cpu time: u=");
5692 formatTimeMs(sb, userCpuTimeUs / 1000);
5693 sb.append("s=");
5694 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005695 pw.println(sb.toString());
5696 }
5697
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005698 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5699 if (cpuFreqTimes != null) {
5700 sb.setLength(0);
5701 sb.append(" Total cpu time per freq:");
5702 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5703 sb.append(" " + cpuFreqTimes[i]);
5704 }
5705 pw.println(sb.toString());
5706 }
5707 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5708 if (screenOffCpuFreqTimes != null) {
5709 sb.setLength(0);
5710 sb.append(" Total screen-off cpu time per freq:");
5711 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5712 sb.append(" " + screenOffCpuFreqTimes[i]);
5713 }
5714 pw.println(sb.toString());
5715 }
5716
Sudheer Shankab2f83c12017-11-13 19:25:01 -08005717 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
5718 final long[] cpuTimes = u.getCpuFreqTimes(which, procState);
5719 if (cpuTimes != null) {
5720 sb.setLength(0);
5721 sb.append(" Cpu times per freq at state "
5722 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5723 for (int i = 0; i < cpuTimes.length; ++i) {
5724 sb.append(" " + cpuTimes[i]);
5725 }
5726 pw.println(sb.toString());
5727 }
5728
5729 final long[] screenOffCpuTimes = u.getScreenOffCpuFreqTimes(which, procState);
5730 if (screenOffCpuTimes != null) {
5731 sb.setLength(0);
5732 sb.append(" Screen-off cpu times per freq at state "
5733 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5734 for (int i = 0; i < screenOffCpuTimes.length; ++i) {
5735 sb.append(" " + screenOffCpuTimes[i]);
5736 }
5737 pw.println(sb.toString());
5738 }
5739 }
5740
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005741 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5742 = u.getProcessStats();
5743 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5744 final Uid.Proc ps = processStats.valueAt(ipr);
5745 long userTime;
5746 long systemTime;
5747 long foregroundTime;
5748 int starts;
5749 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005750
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005751 userTime = ps.getUserTime(which);
5752 systemTime = ps.getSystemTime(which);
5753 foregroundTime = ps.getForegroundTime(which);
5754 starts = ps.getStarts(which);
5755 final int numCrashes = ps.getNumCrashes(which);
5756 final int numAnrs = ps.getNumAnrs(which);
5757 numExcessive = which == STATS_SINCE_CHARGED
5758 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005759
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005760 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5761 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5762 sb.setLength(0);
5763 sb.append(prefix); sb.append(" Proc ");
5764 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5765 sb.append(prefix); sb.append(" CPU: ");
5766 formatTimeMs(sb, userTime); sb.append("usr + ");
5767 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5768 formatTimeMs(sb, foregroundTime); sb.append("fg");
5769 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5770 sb.append("\n"); sb.append(prefix); sb.append(" ");
5771 boolean hasOne = false;
5772 if (starts != 0) {
5773 hasOne = true;
5774 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005775 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005776 if (numCrashes != 0) {
5777 if (hasOne) {
5778 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005779 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005780 hasOne = true;
5781 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005782 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005783 if (numAnrs != 0) {
5784 if (hasOne) {
5785 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005786 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005787 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005788 }
5789 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005790 pw.println(sb.toString());
5791 for (int e=0; e<numExcessive; e++) {
5792 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5793 if (ew != null) {
5794 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07005795 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005796 pw.print("cpu");
5797 } else {
5798 pw.print("unknown");
5799 }
5800 pw.print(" use: ");
5801 TimeUtils.formatDuration(ew.usedTime, pw);
5802 pw.print(" over ");
5803 TimeUtils.formatDuration(ew.overTime, pw);
5804 if (ew.overTime != 0) {
5805 pw.print(" (");
5806 pw.print((ew.usedTime*100)/ew.overTime);
5807 pw.println("%)");
5808 }
5809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005810 }
5811 uidActivity = true;
5812 }
5813 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005814
5815 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5816 = u.getPackageStats();
5817 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
5818 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
5819 pw.println(":");
5820 boolean apkActivity = false;
5821 final Uid.Pkg ps = packageStats.valueAt(ipkg);
5822 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
5823 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
5824 pw.print(prefix); pw.print(" Wakeup alarm ");
5825 pw.print(alarms.keyAt(iwa)); pw.print(": ");
5826 pw.print(alarms.valueAt(iwa).getCountLocked(which));
5827 pw.println(" times");
5828 apkActivity = true;
5829 }
5830 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
5831 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
5832 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
5833 final long startTime = ss.getStartTime(batteryUptime, which);
5834 final int starts = ss.getStarts(which);
5835 final int launches = ss.getLaunches(which);
5836 if (startTime != 0 || starts != 0 || launches != 0) {
5837 sb.setLength(0);
5838 sb.append(prefix); sb.append(" Service ");
5839 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
5840 sb.append(prefix); sb.append(" Created for: ");
5841 formatTimeMs(sb, startTime / 1000);
5842 sb.append("uptime\n");
5843 sb.append(prefix); sb.append(" Starts: ");
5844 sb.append(starts);
5845 sb.append(", launches: "); sb.append(launches);
5846 pw.println(sb.toString());
5847 apkActivity = true;
5848 }
5849 }
5850 if (!apkActivity) {
5851 pw.print(prefix); pw.println(" (nothing executed)");
5852 }
5853 uidActivity = true;
5854 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005855 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005856 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005857 }
5858 }
5859 }
5860
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005861 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005862 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005863 int diff = oldval ^ newval;
5864 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005865 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005866 for (int i=0; i<descriptions.length; i++) {
5867 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005868 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005869 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005870 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005871 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005872 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005873 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
5874 didWake = true;
5875 pw.print("=");
5876 if (longNames) {
5877 UserHandle.formatUid(pw, wakelockTag.uid);
5878 pw.print(":\"");
5879 pw.print(wakelockTag.string);
5880 pw.print("\"");
5881 } else {
5882 pw.print(wakelockTag.poolIdx);
5883 }
5884 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005885 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005886 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005887 pw.print("=");
5888 int val = (newval&bd.mask)>>bd.shift;
5889 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005890 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005891 } else {
5892 pw.print(val);
5893 }
5894 }
5895 }
5896 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005897 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07005898 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005899 if (longNames) {
5900 UserHandle.formatUid(pw, wakelockTag.uid);
5901 pw.print(":\"");
5902 pw.print(wakelockTag.string);
5903 pw.print("\"");
5904 } else {
5905 pw.print(wakelockTag.poolIdx);
5906 }
5907 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005908 }
Mike Mac2f518a2017-09-19 16:06:03 -07005909
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005910 public void prepareForDumpLocked() {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07005911 // We don't need to require subclasses implement this.
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005912 }
5913
5914 public static class HistoryPrinter {
5915 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005916 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005917 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005918 int oldStatus = -1;
5919 int oldHealth = -1;
5920 int oldPlug = -1;
5921 int oldTemp = -1;
5922 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005923 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005924 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005925
Dianne Hackborn3251b902014-06-20 14:40:53 -07005926 void reset() {
5927 oldState = oldState2 = 0;
5928 oldLevel = -1;
5929 oldStatus = -1;
5930 oldHealth = -1;
5931 oldPlug = -1;
5932 oldTemp = -1;
5933 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005934 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07005935 }
5936
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005937 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005938 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005939 if (!checkin) {
5940 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005941 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005942 pw.print(" (");
5943 pw.print(rec.numReadInts);
5944 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005945 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005946 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5947 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005948 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005949 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005950 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005951 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005952 }
5953 lastTime = rec.time;
5954 }
5955 if (rec.cmd == HistoryItem.CMD_START) {
5956 if (checkin) {
5957 pw.print(":");
5958 }
5959 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005960 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005961 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
5962 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005963 if (checkin) {
5964 pw.print(":");
5965 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005966 if (rec.cmd == HistoryItem.CMD_RESET) {
5967 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005968 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005969 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005970 pw.print("TIME:");
5971 if (checkin) {
5972 pw.println(rec.currentTime);
5973 } else {
5974 pw.print(" ");
5975 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5976 rec.currentTime).toString());
5977 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005978 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
5979 if (checkin) {
5980 pw.print(":");
5981 }
5982 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005983 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
5984 if (checkin) {
5985 pw.print(":");
5986 }
5987 pw.println("*OVERFLOW*");
5988 } else {
5989 if (!checkin) {
5990 if (rec.batteryLevel < 10) pw.print("00");
5991 else if (rec.batteryLevel < 100) pw.print("0");
5992 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005993 if (verbose) {
5994 pw.print(" ");
5995 if (rec.states < 0) ;
5996 else if (rec.states < 0x10) pw.print("0000000");
5997 else if (rec.states < 0x100) pw.print("000000");
5998 else if (rec.states < 0x1000) pw.print("00000");
5999 else if (rec.states < 0x10000) pw.print("0000");
6000 else if (rec.states < 0x100000) pw.print("000");
6001 else if (rec.states < 0x1000000) pw.print("00");
6002 else if (rec.states < 0x10000000) pw.print("0");
6003 pw.print(Integer.toHexString(rec.states));
6004 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006005 } else {
6006 if (oldLevel != rec.batteryLevel) {
6007 oldLevel = rec.batteryLevel;
6008 pw.print(",Bl="); pw.print(rec.batteryLevel);
6009 }
6010 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006011 if (oldStatus != rec.batteryStatus) {
6012 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006013 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006014 switch (oldStatus) {
6015 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006016 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006017 break;
6018 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006019 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006020 break;
6021 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006022 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006023 break;
6024 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006025 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006026 break;
6027 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006028 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006029 break;
6030 default:
6031 pw.print(oldStatus);
6032 break;
6033 }
6034 }
6035 if (oldHealth != rec.batteryHealth) {
6036 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006037 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006038 switch (oldHealth) {
6039 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006040 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006041 break;
6042 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006043 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006044 break;
6045 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006046 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006047 break;
6048 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006049 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006050 break;
6051 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006052 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006053 break;
6054 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006055 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006056 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006057 case BatteryManager.BATTERY_HEALTH_COLD:
6058 pw.print(checkin ? "c" : "cold");
6059 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006060 default:
6061 pw.print(oldHealth);
6062 break;
6063 }
6064 }
6065 if (oldPlug != rec.batteryPlugType) {
6066 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006067 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006068 switch (oldPlug) {
6069 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006070 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006071 break;
6072 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006073 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006074 break;
6075 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006076 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006077 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006078 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006079 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006080 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006081 default:
6082 pw.print(oldPlug);
6083 break;
6084 }
6085 }
6086 if (oldTemp != rec.batteryTemperature) {
6087 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006088 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006089 pw.print(oldTemp);
6090 }
6091 if (oldVolt != rec.batteryVoltage) {
6092 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006093 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006094 pw.print(oldVolt);
6095 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006096 final int chargeMAh = rec.batteryChargeUAh / 1000;
6097 if (oldChargeMAh != chargeMAh) {
6098 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07006099 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006100 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07006101 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006102 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006103 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006104 printBitDescriptions(pw, oldState2, rec.states2, null,
6105 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006106 if (rec.wakeReasonTag != null) {
6107 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006108 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006109 pw.print(rec.wakeReasonTag.poolIdx);
6110 } else {
6111 pw.print(" wake_reason=");
6112 pw.print(rec.wakeReasonTag.uid);
6113 pw.print(":\"");
6114 pw.print(rec.wakeReasonTag.string);
6115 pw.print("\"");
6116 }
6117 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08006118 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006119 pw.print(checkin ? "," : " ");
6120 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
6121 pw.print("+");
6122 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
6123 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006124 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006125 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
6126 : HISTORY_EVENT_NAMES;
6127 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
6128 | HistoryItem.EVENT_FLAG_FINISH);
6129 if (idx >= 0 && idx < eventNames.length) {
6130 pw.print(eventNames[idx]);
6131 } else {
6132 pw.print(checkin ? "Ev" : "event");
6133 pw.print(idx);
6134 }
6135 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006136 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006137 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08006138 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08006139 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
6140 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006141 pw.print(":\"");
6142 pw.print(rec.eventTag.string);
6143 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006144 }
6145 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006146 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006147 if (rec.stepDetails != null) {
6148 if (!checkin) {
6149 pw.print(" Details: cpu=");
6150 pw.print(rec.stepDetails.userTime);
6151 pw.print("u+");
6152 pw.print(rec.stepDetails.systemTime);
6153 pw.print("s");
6154 if (rec.stepDetails.appCpuUid1 >= 0) {
6155 pw.print(" (");
6156 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
6157 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6158 if (rec.stepDetails.appCpuUid2 >= 0) {
6159 pw.print(", ");
6160 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
6161 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6162 }
6163 if (rec.stepDetails.appCpuUid3 >= 0) {
6164 pw.print(", ");
6165 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
6166 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6167 }
6168 pw.print(')');
6169 }
6170 pw.println();
6171 pw.print(" /proc/stat=");
6172 pw.print(rec.stepDetails.statUserTime);
6173 pw.print(" usr, ");
6174 pw.print(rec.stepDetails.statSystemTime);
6175 pw.print(" sys, ");
6176 pw.print(rec.stepDetails.statIOWaitTime);
6177 pw.print(" io, ");
6178 pw.print(rec.stepDetails.statIrqTime);
6179 pw.print(" irq, ");
6180 pw.print(rec.stepDetails.statSoftIrqTime);
6181 pw.print(" sirq, ");
6182 pw.print(rec.stepDetails.statIdlTime);
6183 pw.print(" idle");
6184 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
6185 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
6186 + rec.stepDetails.statSoftIrqTime;
6187 int total = totalRun + rec.stepDetails.statIdlTime;
6188 if (total > 0) {
6189 pw.print(" (");
6190 float perc = ((float)totalRun) / ((float)total) * 100;
6191 pw.print(String.format("%.1f%%", perc));
6192 pw.print(" of ");
6193 StringBuilder sb = new StringBuilder(64);
6194 formatTimeMsNoSpace(sb, total*10);
6195 pw.print(sb);
6196 pw.print(")");
6197 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006198 pw.print(", PlatformIdleStat ");
6199 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006200 pw.println();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006201
6202 pw.print(", SubsystemPowerState ");
6203 pw.print(rec.stepDetails.statSubsystemPowerState);
6204 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006205 } else {
6206 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6207 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
6208 pw.print(rec.stepDetails.userTime);
6209 pw.print(":");
6210 pw.print(rec.stepDetails.systemTime);
6211 if (rec.stepDetails.appCpuUid1 >= 0) {
6212 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
6213 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6214 if (rec.stepDetails.appCpuUid2 >= 0) {
6215 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
6216 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6217 }
6218 if (rec.stepDetails.appCpuUid3 >= 0) {
6219 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
6220 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6221 }
6222 }
6223 pw.println();
6224 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6225 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
6226 pw.print(rec.stepDetails.statUserTime);
6227 pw.print(',');
6228 pw.print(rec.stepDetails.statSystemTime);
6229 pw.print(',');
6230 pw.print(rec.stepDetails.statIOWaitTime);
6231 pw.print(',');
6232 pw.print(rec.stepDetails.statIrqTime);
6233 pw.print(',');
6234 pw.print(rec.stepDetails.statSoftIrqTime);
6235 pw.print(',');
6236 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006237 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006238 if (rec.stepDetails.statPlatformIdleState != null) {
6239 pw.print(rec.stepDetails.statPlatformIdleState);
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006240 if (rec.stepDetails.statSubsystemPowerState != null) {
6241 pw.print(',');
6242 }
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006243 }
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006244
6245 if (rec.stepDetails.statSubsystemPowerState != null) {
6246 pw.print(rec.stepDetails.statSubsystemPowerState);
6247 }
6248 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006249 }
6250 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006251 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006252 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006253 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006254 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006255
6256 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
6257 UserHandle.formatUid(pw, uid);
6258 pw.print("=");
6259 pw.print(utime);
6260 pw.print("u+");
6261 pw.print(stime);
6262 pw.print("s");
6263 }
6264
6265 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
6266 pw.print('/');
6267 pw.print(uid);
6268 pw.print(":");
6269 pw.print(utime);
6270 pw.print(":");
6271 pw.print(stime);
6272 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006273 }
6274
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006275 private void printSizeValue(PrintWriter pw, long size) {
6276 float result = size;
6277 String suffix = "";
6278 if (result >= 10*1024) {
6279 suffix = "KB";
6280 result = result / 1024;
6281 }
6282 if (result >= 10*1024) {
6283 suffix = "MB";
6284 result = result / 1024;
6285 }
6286 if (result >= 10*1024) {
6287 suffix = "GB";
6288 result = result / 1024;
6289 }
6290 if (result >= 10*1024) {
6291 suffix = "TB";
6292 result = result / 1024;
6293 }
6294 if (result >= 10*1024) {
6295 suffix = "PB";
6296 result = result / 1024;
6297 }
6298 pw.print((int)result);
6299 pw.print(suffix);
6300 }
6301
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006302 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
6303 String label3, long estimatedTime) {
6304 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006305 return false;
6306 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006307 pw.print(label1);
6308 pw.print(label2);
6309 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006310 StringBuilder sb = new StringBuilder(64);
6311 formatTimeMs(sb, estimatedTime);
6312 pw.print(sb);
6313 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006314 return true;
6315 }
6316
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006317 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
6318 LevelStepTracker steps, boolean checkin) {
6319 if (steps == null) {
6320 return false;
6321 }
6322 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006323 if (count <= 0) {
6324 return false;
6325 }
6326 if (!checkin) {
6327 pw.println(header);
6328 }
Kweku Adams030980a2015-04-01 16:07:48 -07006329 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006330 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006331 long duration = steps.getDurationAt(i);
6332 int level = steps.getLevelAt(i);
6333 long initMode = steps.getInitModeAt(i);
6334 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006335 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006336 lineArgs[0] = Long.toString(duration);
6337 lineArgs[1] = Integer.toString(level);
6338 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6339 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6340 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
6341 case Display.STATE_ON: lineArgs[2] = "s+"; break;
6342 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
6343 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07006344 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006345 }
6346 } else {
6347 lineArgs[2] = "";
6348 }
6349 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6350 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
6351 } else {
6352 lineArgs[3] = "";
6353 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006354 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07006355 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006356 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07006357 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006358 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006359 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
6360 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006361 pw.print(prefix);
6362 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006363 TimeUtils.formatDuration(duration, pw);
6364 pw.print(" to "); pw.print(level);
6365 boolean haveModes = false;
6366 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6367 pw.print(" (");
6368 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6369 case Display.STATE_OFF: pw.print("screen-off"); break;
6370 case Display.STATE_ON: pw.print("screen-on"); break;
6371 case Display.STATE_DOZE: pw.print("screen-doze"); break;
6372 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07006373 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006374 }
6375 haveModes = true;
6376 }
6377 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6378 pw.print(haveModes ? ", " : " (");
6379 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
6380 ? "power-save-on" : "power-save-off");
6381 haveModes = true;
6382 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006383 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6384 pw.print(haveModes ? ", " : " (");
6385 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6386 ? "device-idle-on" : "device-idle-off");
6387 haveModes = true;
6388 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006389 if (haveModes) {
6390 pw.print(")");
6391 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006392 pw.println();
6393 }
6394 }
6395 return true;
6396 }
6397
Kweku Adams87b19ec2017-10-09 12:40:03 -07006398 private static void dumpDurationSteps(ProtoOutputStream proto, long fieldId,
6399 LevelStepTracker steps) {
6400 if (steps == null) {
6401 return;
6402 }
6403 int count = steps.mNumStepDurations;
Kweku Adams87b19ec2017-10-09 12:40:03 -07006404 for (int i = 0; i < count; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07006405 long token = proto.start(fieldId);
Kweku Adams87b19ec2017-10-09 12:40:03 -07006406 proto.write(SystemProto.BatteryLevelStep.DURATION_MS, steps.getDurationAt(i));
6407 proto.write(SystemProto.BatteryLevelStep.LEVEL, steps.getLevelAt(i));
6408
6409 final long initMode = steps.getInitModeAt(i);
6410 final long modMode = steps.getModModeAt(i);
6411
6412 int ds = SystemProto.BatteryLevelStep.DS_MIXED;
6413 if ((modMode & STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6414 switch ((int) (initMode & STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6415 case Display.STATE_OFF:
6416 ds = SystemProto.BatteryLevelStep.DS_OFF;
6417 break;
6418 case Display.STATE_ON:
6419 ds = SystemProto.BatteryLevelStep.DS_ON;
6420 break;
6421 case Display.STATE_DOZE:
6422 ds = SystemProto.BatteryLevelStep.DS_DOZE;
6423 break;
6424 case Display.STATE_DOZE_SUSPEND:
6425 ds = SystemProto.BatteryLevelStep.DS_DOZE_SUSPEND;
6426 break;
6427 default:
6428 ds = SystemProto.BatteryLevelStep.DS_ERROR;
6429 break;
6430 }
6431 }
6432 proto.write(SystemProto.BatteryLevelStep.DISPLAY_STATE, ds);
6433
6434 int psm = SystemProto.BatteryLevelStep.PSM_MIXED;
6435 if ((modMode & STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6436 psm = (initMode & STEP_LEVEL_MODE_POWER_SAVE) != 0
6437 ? SystemProto.BatteryLevelStep.PSM_ON : SystemProto.BatteryLevelStep.PSM_OFF;
6438 }
6439 proto.write(SystemProto.BatteryLevelStep.POWER_SAVE_MODE, psm);
6440
6441 int im = SystemProto.BatteryLevelStep.IM_MIXED;
6442 if ((modMode & STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6443 im = (initMode & STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6444 ? SystemProto.BatteryLevelStep.IM_ON : SystemProto.BatteryLevelStep.IM_OFF;
6445 }
6446 proto.write(SystemProto.BatteryLevelStep.IDLE_MODE, im);
6447
6448 proto.end(token);
6449 }
6450 }
6451
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006452 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006453 public static final int DUMP_DAILY_ONLY = 1<<2;
6454 public static final int DUMP_HISTORY_ONLY = 1<<3;
6455 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
6456 public static final int DUMP_VERBOSE = 1<<5;
6457 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006458
Dianne Hackborn37de0982014-05-09 09:32:18 -07006459 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
6460 final HistoryPrinter hprinter = new HistoryPrinter();
6461 final HistoryItem rec = new HistoryItem();
6462 long lastTime = -1;
6463 long baseTime = -1;
6464 boolean printed = false;
6465 HistoryEventTracker tracker = null;
6466 while (getNextHistoryLocked(rec)) {
6467 lastTime = rec.time;
6468 if (baseTime < 0) {
6469 baseTime = lastTime;
6470 }
6471 if (rec.time >= histStart) {
6472 if (histStart >= 0 && !printed) {
6473 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07006474 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006475 || rec.cmd == HistoryItem.CMD_START
6476 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006477 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006478 hprinter.printNextItem(pw, rec, baseTime, checkin,
6479 (flags&DUMP_VERBOSE) != 0);
6480 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006481 } else if (rec.currentTime != 0) {
6482 printed = true;
6483 byte cmd = rec.cmd;
6484 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006485 hprinter.printNextItem(pw, rec, baseTime, checkin,
6486 (flags&DUMP_VERBOSE) != 0);
6487 rec.cmd = cmd;
6488 }
6489 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006490 if (rec.cmd != HistoryItem.CMD_UPDATE) {
6491 hprinter.printNextItem(pw, rec, baseTime, checkin,
6492 (flags&DUMP_VERBOSE) != 0);
6493 rec.cmd = HistoryItem.CMD_UPDATE;
6494 }
6495 int oldEventCode = rec.eventCode;
6496 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006497 rec.eventTag = new HistoryTag();
6498 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
6499 HashMap<String, SparseIntArray> active
6500 = tracker.getStateForEvent(i);
6501 if (active == null) {
6502 continue;
6503 }
6504 for (HashMap.Entry<String, SparseIntArray> ent
6505 : active.entrySet()) {
6506 SparseIntArray uids = ent.getValue();
6507 for (int j=0; j<uids.size(); j++) {
6508 rec.eventCode = i;
6509 rec.eventTag.string = ent.getKey();
6510 rec.eventTag.uid = uids.keyAt(j);
6511 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006512 hprinter.printNextItem(pw, rec, baseTime, checkin,
6513 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006514 rec.wakeReasonTag = null;
6515 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006516 }
6517 }
6518 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006519 rec.eventCode = oldEventCode;
6520 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006521 tracker = null;
6522 }
6523 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006524 hprinter.printNextItem(pw, rec, baseTime, checkin,
6525 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07006526 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
6527 // This is an attempt to aggregate the previous state and generate
6528 // fake events to reflect that state at the point where we start
6529 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07006530 if (tracker == null) {
6531 tracker = new HistoryEventTracker();
6532 }
6533 tracker.updateState(rec.eventCode, rec.eventTag.string,
6534 rec.eventTag.uid, rec.eventTag.poolIdx);
6535 }
6536 }
6537 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07006538 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006539 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
6540 }
6541 }
6542
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006543 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
6544 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
6545 if (steps == null) {
6546 return;
6547 }
6548 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
6549 if (timeRemaining >= 0) {
6550 pw.print(prefix); pw.print(label); pw.print(" total time: ");
6551 tmpSb.setLength(0);
6552 formatTimeMs(tmpSb, timeRemaining);
6553 pw.print(tmpSb);
6554 pw.print(" (from "); pw.print(tmpOutInt[0]);
6555 pw.println(" steps)");
6556 }
6557 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6558 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6559 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
6560 if (estimatedTime > 0) {
6561 pw.print(prefix); pw.print(label); pw.print(" ");
6562 pw.print(STEP_LEVEL_MODE_LABELS[i]);
6563 pw.print(" time: ");
6564 tmpSb.setLength(0);
6565 formatTimeMs(tmpSb, estimatedTime);
6566 pw.print(tmpSb);
6567 pw.print(" (from "); pw.print(tmpOutInt[0]);
6568 pw.println(" steps)");
6569 }
6570 }
6571 }
6572
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006573 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
6574 ArrayList<PackageChange> changes) {
6575 if (changes == null) {
6576 return;
6577 }
6578 pw.print(prefix); pw.println("Package changes:");
6579 for (int i=0; i<changes.size(); i++) {
6580 PackageChange pc = changes.get(i);
6581 if (pc.mUpdate) {
6582 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
6583 pw.print(" vers="); pw.println(pc.mVersionCode);
6584 } else {
6585 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
6586 }
6587 }
6588 }
6589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006590 /**
6591 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
6592 *
6593 * @param pw a Printer to receive the dump output.
6594 */
6595 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006596 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006597 prepareForDumpLocked();
6598
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006599 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006600 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006601
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006602 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006603 final long historyTotalSize = getHistoryTotalSize();
6604 final long historyUsedSize = getHistoryUsedSize();
6605 if (startIteratingHistoryLocked()) {
6606 try {
6607 pw.print("Battery History (");
6608 pw.print((100*historyUsedSize)/historyTotalSize);
6609 pw.print("% used, ");
6610 printSizeValue(pw, historyUsedSize);
6611 pw.print(" used of ");
6612 printSizeValue(pw, historyTotalSize);
6613 pw.print(", ");
6614 pw.print(getHistoryStringPoolSize());
6615 pw.print(" strings using ");
6616 printSizeValue(pw, getHistoryStringPoolBytes());
6617 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07006618 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006619 pw.println();
6620 } finally {
6621 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006622 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006623 }
6624
6625 if (startIteratingOldHistoryLocked()) {
6626 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006627 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006628 pw.println("Old battery History:");
6629 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006630 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006631 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006632 if (baseTime < 0) {
6633 baseTime = rec.time;
6634 }
6635 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006636 }
6637 pw.println();
6638 } finally {
6639 finishIteratingOldHistoryLocked();
6640 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006641 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006642 }
6643
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006644 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006645 return;
6646 }
6647
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006648 if (!filtering) {
6649 SparseArray<? extends Uid> uidStats = getUidStats();
6650 final int NU = uidStats.size();
6651 boolean didPid = false;
6652 long nowRealtime = SystemClock.elapsedRealtime();
6653 for (int i=0; i<NU; i++) {
6654 Uid uid = uidStats.valueAt(i);
6655 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
6656 if (pids != null) {
6657 for (int j=0; j<pids.size(); j++) {
6658 Uid.Pid pid = pids.valueAt(j);
6659 if (!didPid) {
6660 pw.println("Per-PID Stats:");
6661 didPid = true;
6662 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006663 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
6664 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006665 pw.print(" PID "); pw.print(pids.keyAt(j));
6666 pw.print(" wake time: ");
6667 TimeUtils.formatDuration(time, pw);
6668 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006669 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006670 }
6671 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006672 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006673 pw.println();
6674 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006675 }
6676
6677 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006678 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
6679 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006680 long timeRemaining = computeBatteryTimeRemaining(
6681 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006682 if (timeRemaining >= 0) {
6683 pw.print(" Estimated discharge time remaining: ");
6684 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6685 pw.println();
6686 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006687 final LevelStepTracker steps = getDischargeLevelStepTracker();
6688 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6689 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
6690 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6691 STEP_LEVEL_MODE_VALUES[i], null));
6692 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006693 pw.println();
6694 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006695 if (dumpDurationSteps(pw, " ", "Charge step durations:",
6696 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006697 long timeRemaining = computeChargeTimeRemaining(
6698 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006699 if (timeRemaining >= 0) {
6700 pw.print(" Estimated charge time remaining: ");
6701 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6702 pw.println();
6703 }
6704 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006705 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006706 }
Dianne Hackbornc81983a2017-10-20 16:16:32 -07006707 if (!filtering || (flags & DUMP_DAILY_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006708 pw.println("Daily stats:");
6709 pw.print(" Current start time: ");
6710 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6711 getCurrentDailyStartTime()).toString());
6712 pw.print(" Next min deadline: ");
6713 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6714 getNextMinDailyDeadline()).toString());
6715 pw.print(" Next max deadline: ");
6716 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6717 getNextMaxDailyDeadline()).toString());
6718 StringBuilder sb = new StringBuilder(64);
6719 int[] outInt = new int[1];
6720 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
6721 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006722 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
6723 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006724 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006725 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
6726 dsteps, false)) {
6727 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6728 sb, outInt);
6729 }
6730 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6731 csteps, false)) {
6732 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6733 sb, outInt);
6734 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006735 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006736 } else {
6737 pw.println(" Current daily steps:");
6738 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6739 sb, outInt);
6740 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6741 sb, outInt);
6742 }
6743 }
6744 DailyItem dit;
6745 int curIndex = 0;
6746 while ((dit=getDailyItemLocked(curIndex)) != null) {
6747 curIndex++;
6748 if ((flags&DUMP_DAILY_ONLY) != 0) {
6749 pw.println();
6750 }
6751 pw.print(" Daily from ");
6752 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6753 pw.print(" to ");
6754 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6755 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006756 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006757 if (dumpDurationSteps(pw, " ",
6758 " Discharge step durations:", dit.mDischargeSteps, false)) {
6759 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6760 sb, outInt);
6761 }
6762 if (dumpDurationSteps(pw, " ",
6763 " Charge step durations:", dit.mChargeSteps, false)) {
6764 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6765 sb, outInt);
6766 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006767 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006768 } else {
6769 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6770 sb, outInt);
6771 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6772 sb, outInt);
6773 }
6774 }
6775 pw.println();
6776 }
6777 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006778 pw.println("Statistics since last charge:");
6779 pw.println(" System starts: " + getStartCount()
6780 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006781 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6782 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006783 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006785 }
Mike Mac2f518a2017-09-19 16:06:03 -07006786
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006787 // This is called from BatteryStatsService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006788 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006789 public void dumpCheckinLocked(Context context, PrintWriter pw,
6790 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006791 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006792
6793 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006794 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6795 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006796
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006797 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6798
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006799 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07006800 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006801 try {
6802 for (int i=0; i<getHistoryStringPoolSize(); i++) {
6803 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6804 pw.print(HISTORY_STRING_POOL); pw.print(',');
6805 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006806 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006807 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006808 pw.print(",\"");
6809 String str = getHistoryTagPoolString(i);
6810 str = str.replace("\\", "\\\\");
6811 str = str.replace("\"", "\\\"");
6812 pw.print(str);
6813 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006814 pw.println();
6815 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006816 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006817 } finally {
6818 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08006819 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006820 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006821 }
6822
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006823 if ((flags & DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006824 return;
6825 }
6826
Dianne Hackborne4a59512010-12-07 11:08:07 -08006827 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006828 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08006829 for (int i=0; i<apps.size(); i++) {
6830 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006831 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
6832 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08006833 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006834 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
6835 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006836 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006837 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006838 }
6839 SparseArray<? extends Uid> uidStats = getUidStats();
6840 final int NU = uidStats.size();
6841 String[] lineArgs = new String[2];
6842 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006843 int uid = UserHandle.getAppId(uidStats.keyAt(i));
6844 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
6845 if (pkgs != null && !pkgs.second.value) {
6846 pkgs.second.value = true;
6847 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08006848 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006849 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006850 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
6851 (Object[])lineArgs);
6852 }
6853 }
6854 }
6855 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006856 if ((flags & DUMP_DAILY_ONLY) == 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006857 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006858 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07006859 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006860 if (timeRemaining >= 0) {
6861 lineArgs[0] = Long.toString(timeRemaining);
6862 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
6863 (Object[])lineArgs);
6864 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006865 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07006866 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006867 if (timeRemaining >= 0) {
6868 lineArgs[0] = Long.toString(timeRemaining);
6869 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
6870 (Object[])lineArgs);
6871 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006872 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
6873 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006874 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006875 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006876
Kweku Adams87b19ec2017-10-09 12:40:03 -07006877 /** Dump #STATS_SINCE_CHARGED batterystats data to a proto. @hide */
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006878 public void dumpProtoLocked(Context context, FileDescriptor fd, List<ApplicationInfo> apps,
Kweku Adams6ccebf22017-12-11 12:30:35 -08006879 int flags) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006880 final ProtoOutputStream proto = new ProtoOutputStream(fd);
6881 final long bToken = proto.start(BatteryStatsServiceDumpProto.BATTERYSTATS);
6882 prepareForDumpLocked();
6883
6884 proto.write(BatteryStatsProto.REPORT_VERSION, CHECKIN_VERSION);
6885 proto.write(BatteryStatsProto.PARCEL_VERSION, getParcelVersion());
6886 proto.write(BatteryStatsProto.START_PLATFORM_VERSION, getStartPlatformVersion());
6887 proto.write(BatteryStatsProto.END_PLATFORM_VERSION, getEndPlatformVersion());
6888
Kweku Adams6ccebf22017-12-11 12:30:35 -08006889 // History intentionally not included in proto dump.
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006890
6891 if ((flags & (DUMP_HISTORY_ONLY | DUMP_DAILY_ONLY)) == 0) {
Kweku Adams103351f2017-10-16 14:39:34 -07006892 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false,
6893 (flags & DUMP_DEVICE_WIFI_ONLY) != 0);
6894 helper.create(this);
6895 helper.refreshStats(STATS_SINCE_CHARGED, UserHandle.USER_ALL);
6896
6897 dumpProtoAppsLocked(proto, helper, apps);
6898 dumpProtoSystemLocked(proto, helper);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006899 }
6900
6901 proto.end(bToken);
6902 proto.flush();
6903 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07006904
Kweku Adams103351f2017-10-16 14:39:34 -07006905 private void dumpProtoAppsLocked(ProtoOutputStream proto, BatteryStatsHelper helper,
6906 List<ApplicationInfo> apps) {
6907 final int which = STATS_SINCE_CHARGED;
6908 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
6909 final long rawRealtimeMs = SystemClock.elapsedRealtime();
6910 final long rawRealtimeUs = rawRealtimeMs * 1000;
6911 final long batteryUptimeUs = getBatteryUptime(rawUptimeUs);
6912
6913 SparseArray<ArrayList<String>> aidToPackages = new SparseArray<>();
6914 if (apps != null) {
6915 for (int i = 0; i < apps.size(); ++i) {
6916 ApplicationInfo ai = apps.get(i);
6917 int aid = UserHandle.getAppId(ai.uid);
6918 ArrayList<String> pkgs = aidToPackages.get(aid);
6919 if (pkgs == null) {
6920 pkgs = new ArrayList<String>();
6921 aidToPackages.put(aid, pkgs);
6922 }
6923 pkgs.add(ai.packageName);
6924 }
6925 }
6926
6927 SparseArray<BatterySipper> uidToSipper = new SparseArray<>();
6928 final List<BatterySipper> sippers = helper.getUsageList();
6929 if (sippers != null) {
6930 for (int i = 0; i < sippers.size(); ++i) {
6931 final BatterySipper bs = sippers.get(i);
6932 if (bs.drainType != BatterySipper.DrainType.APP) {
6933 // Others are handled by dumpProtoSystemLocked()
6934 continue;
6935 }
6936 uidToSipper.put(bs.uidObj.getUid(), bs);
6937 }
6938 }
6939
6940 SparseArray<? extends Uid> uidStats = getUidStats();
6941 final int n = uidStats.size();
6942 for (int iu = 0; iu < n; ++iu) {
6943 final long uTkn = proto.start(BatteryStatsProto.UIDS);
6944 final Uid u = uidStats.valueAt(iu);
6945
6946 final int uid = uidStats.keyAt(iu);
6947 proto.write(UidProto.UID, uid);
6948
6949 // Print packages and apk stats (UID_DATA & APK_DATA)
6950 ArrayList<String> pkgs = aidToPackages.get(UserHandle.getAppId(uid));
6951 if (pkgs == null) {
6952 pkgs = new ArrayList<String>();
6953 }
6954 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats =
6955 u.getPackageStats();
6956 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
6957 String pkg = packageStats.keyAt(ipkg);
6958 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats =
6959 packageStats.valueAt(ipkg).getServiceStats();
6960 if (serviceStats.size() == 0) {
6961 // Due to the way ActivityManagerService logs wakeup alarms, some packages (for
6962 // example, "android") may be included in the packageStats that aren't part of
6963 // the UID. If they don't have any services, then they shouldn't be listed here.
6964 // These packages won't be a part in the pkgs List.
6965 continue;
6966 }
6967
6968 final long pToken = proto.start(UidProto.PACKAGES);
6969 proto.write(UidProto.Package.NAME, pkg);
6970 // Remove from the packages list since we're logging it here.
6971 pkgs.remove(pkg);
6972
6973 for (int isvc = serviceStats.size() - 1; isvc >= 0; --isvc) {
6974 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
6975 long sToken = proto.start(UidProto.Package.SERVICES);
6976
6977 proto.write(UidProto.Package.Service.NAME, serviceStats.keyAt(isvc));
6978 proto.write(UidProto.Package.Service.START_DURATION_MS,
6979 roundUsToMs(ss.getStartTime(batteryUptimeUs, which)));
6980 proto.write(UidProto.Package.Service.START_COUNT, ss.getStarts(which));
6981 proto.write(UidProto.Package.Service.LAUNCH_COUNT, ss.getLaunches(which));
6982
6983 proto.end(sToken);
6984 }
6985 proto.end(pToken);
6986 }
6987 // Print any remaining packages that weren't in the packageStats map. pkgs is pulled
6988 // from PackageManager data. Packages are only included in packageStats if there was
6989 // specific data tracked for them (services and wakeup alarms, etc.).
6990 for (String p : pkgs) {
6991 final long pToken = proto.start(UidProto.PACKAGES);
6992 proto.write(UidProto.Package.NAME, p);
6993 proto.end(pToken);
6994 }
6995
6996 // Total wakelock data (AGGREGATED_WAKELOCK_DATA)
6997 if (u.getAggregatedPartialWakelockTimer() != null) {
6998 final Timer timer = u.getAggregatedPartialWakelockTimer();
6999 // Times are since reset (regardless of 'which')
7000 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
7001 final Timer bgTimer = timer.getSubTimer();
7002 final long bgTimeMs = bgTimer != null
7003 ? bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
7004 final long awToken = proto.start(UidProto.AGGREGATED_WAKELOCK);
7005 proto.write(UidProto.AggregatedWakelock.PARTIAL_DURATION_MS, totTimeMs);
7006 proto.write(UidProto.AggregatedWakelock.BACKGROUND_PARTIAL_DURATION_MS, bgTimeMs);
7007 proto.end(awToken);
7008 }
7009
7010 // Audio (AUDIO_DATA)
7011 dumpTimer(proto, UidProto.AUDIO, u.getAudioTurnedOnTimer(), rawRealtimeUs, which);
7012
7013 // Bluetooth Controller (BLUETOOTH_CONTROLLER_DATA)
7014 dumpControllerActivityProto(proto, UidProto.BLUETOOTH_CONTROLLER,
7015 u.getBluetoothControllerActivity(), which);
7016
7017 // BLE scans (BLUETOOTH_MISC_DATA) (uses totalDurationMsLocked and MaxDurationMsLocked)
7018 final Timer bleTimer = u.getBluetoothScanTimer();
7019 if (bleTimer != null) {
7020 final long bmToken = proto.start(UidProto.BLUETOOTH_MISC);
7021
7022 dumpTimer(proto, UidProto.BluetoothMisc.APPORTIONED_BLE_SCAN, bleTimer,
7023 rawRealtimeUs, which);
7024 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN,
7025 u.getBluetoothScanBackgroundTimer(), rawRealtimeUs, which);
7026 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
7027 dumpTimer(proto, UidProto.BluetoothMisc.UNOPTIMIZED_BLE_SCAN,
7028 u.getBluetoothUnoptimizedScanTimer(), rawRealtimeUs, which);
7029 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
7030 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_UNOPTIMIZED_BLE_SCAN,
7031 u.getBluetoothUnoptimizedScanBackgroundTimer(), rawRealtimeUs, which);
7032 // Result counters
7033 proto.write(UidProto.BluetoothMisc.BLE_SCAN_RESULT_COUNT,
7034 u.getBluetoothScanResultCounter() != null
7035 ? u.getBluetoothScanResultCounter().getCountLocked(which) : 0);
7036 proto.write(UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN_RESULT_COUNT,
7037 u.getBluetoothScanResultBgCounter() != null
7038 ? u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0);
7039
7040 proto.end(bmToken);
7041 }
7042
7043 // Camera (CAMERA_DATA)
7044 dumpTimer(proto, UidProto.CAMERA, u.getCameraTurnedOnTimer(), rawRealtimeUs, which);
7045
7046 // CPU stats (CPU_DATA & CPU_TIMES_AT_FREQ_DATA)
7047 final long cpuToken = proto.start(UidProto.CPU);
7048 proto.write(UidProto.Cpu.USER_DURATION_MS, roundUsToMs(u.getUserCpuTimeUs(which)));
7049 proto.write(UidProto.Cpu.SYSTEM_DURATION_MS, roundUsToMs(u.getSystemCpuTimeUs(which)));
7050
7051 final long[] cpuFreqs = getCpuFreqs();
7052 if (cpuFreqs != null) {
7053 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
7054 // If total cpuFreqTimes is null, then we don't need to check for
7055 // screenOffCpuFreqTimes.
7056 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
7057 long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
7058 if (screenOffCpuFreqTimeMs == null) {
7059 screenOffCpuFreqTimeMs = new long[cpuFreqTimeMs.length];
7060 }
7061 for (int ic = 0; ic < cpuFreqTimeMs.length; ++ic) {
7062 long cToken = proto.start(UidProto.Cpu.BY_FREQUENCY);
7063 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7064 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7065 cpuFreqTimeMs[ic]);
7066 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7067 screenOffCpuFreqTimeMs[ic]);
7068 proto.end(cToken);
7069 }
7070 }
7071 }
Sudheer Shanka6d658d72018-01-01 01:36:49 -08007072
7073 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
7074 final long[] timesMs = u.getCpuFreqTimes(which, procState);
7075 if (timesMs != null && timesMs.length == cpuFreqs.length) {
7076 long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(which, procState);
7077 if (screenOffTimesMs == null) {
7078 screenOffTimesMs = new long[timesMs.length];
7079 }
7080 final long procToken = proto.start(UidProto.Cpu.BY_PROCESS_STATE);
7081 proto.write(UidProto.Cpu.ByProcessState.PROCESS_STATE, procState);
7082 for (int ic = 0; ic < timesMs.length; ++ic) {
7083 long cToken = proto.start(UidProto.Cpu.ByProcessState.BY_FREQUENCY);
7084 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7085 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7086 timesMs[ic]);
7087 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7088 screenOffTimesMs[ic]);
7089 proto.end(cToken);
7090 }
7091 proto.end(procToken);
7092 }
7093 }
Kweku Adams103351f2017-10-16 14:39:34 -07007094 proto.end(cpuToken);
7095
7096 // Flashlight (FLASHLIGHT_DATA)
7097 dumpTimer(proto, UidProto.FLASHLIGHT, u.getFlashlightTurnedOnTimer(),
7098 rawRealtimeUs, which);
7099
7100 // Foreground activity (FOREGROUND_ACTIVITY_DATA)
7101 dumpTimer(proto, UidProto.FOREGROUND_ACTIVITY, u.getForegroundActivityTimer(),
7102 rawRealtimeUs, which);
7103
7104 // Foreground service (FOREGROUND_SERVICE_DATA)
7105 dumpTimer(proto, UidProto.FOREGROUND_SERVICE, u.getForegroundServiceTimer(),
7106 rawRealtimeUs, which);
7107
7108 // Job completion (JOB_COMPLETION_DATA)
7109 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
7110 final int[] reasons = new int[]{
7111 JobParameters.REASON_CANCELED,
7112 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED,
7113 JobParameters.REASON_PREEMPT,
7114 JobParameters.REASON_TIMEOUT,
7115 JobParameters.REASON_DEVICE_IDLE,
7116 };
7117 for (int ic = 0; ic < completions.size(); ++ic) {
7118 SparseIntArray types = completions.valueAt(ic);
7119 if (types != null) {
7120 final long jcToken = proto.start(UidProto.JOB_COMPLETION);
7121
7122 proto.write(UidProto.JobCompletion.NAME, completions.keyAt(ic));
7123
7124 for (int r : reasons) {
7125 long rToken = proto.start(UidProto.JobCompletion.REASON_COUNT);
7126 proto.write(UidProto.JobCompletion.ReasonCount.NAME, r);
7127 proto.write(UidProto.JobCompletion.ReasonCount.COUNT, types.get(r, 0));
7128 proto.end(rToken);
7129 }
7130
7131 proto.end(jcToken);
7132 }
7133 }
7134
7135 // Scheduled jobs (JOB_DATA)
7136 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
7137 for (int ij = jobs.size() - 1; ij >= 0; --ij) {
7138 final Timer timer = jobs.valueAt(ij);
7139 final Timer bgTimer = timer.getSubTimer();
7140 final long jToken = proto.start(UidProto.JOBS);
7141
7142 proto.write(UidProto.Job.NAME, jobs.keyAt(ij));
7143 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7144 dumpTimer(proto, UidProto.Job.TOTAL, timer, rawRealtimeUs, which);
7145 dumpTimer(proto, UidProto.Job.BACKGROUND, bgTimer, rawRealtimeUs, which);
7146
7147 proto.end(jToken);
7148 }
7149
7150 // Modem Controller (MODEM_CONTROLLER_DATA)
7151 dumpControllerActivityProto(proto, UidProto.MODEM_CONTROLLER,
7152 u.getModemControllerActivity(), which);
7153
7154 // Network stats (NETWORK_DATA)
7155 final long nToken = proto.start(UidProto.NETWORK);
7156 proto.write(UidProto.Network.MOBILE_BYTES_RX,
7157 u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7158 proto.write(UidProto.Network.MOBILE_BYTES_TX,
7159 u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7160 proto.write(UidProto.Network.WIFI_BYTES_RX,
7161 u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7162 proto.write(UidProto.Network.WIFI_BYTES_TX,
7163 u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7164 proto.write(UidProto.Network.BT_BYTES_RX,
7165 u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7166 proto.write(UidProto.Network.BT_BYTES_TX,
7167 u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
7168 proto.write(UidProto.Network.MOBILE_PACKETS_RX,
7169 u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7170 proto.write(UidProto.Network.MOBILE_PACKETS_TX,
7171 u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7172 proto.write(UidProto.Network.WIFI_PACKETS_RX,
7173 u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7174 proto.write(UidProto.Network.WIFI_PACKETS_TX,
7175 u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7176 proto.write(UidProto.Network.MOBILE_ACTIVE_DURATION_MS,
7177 roundUsToMs(u.getMobileRadioActiveTime(which)));
7178 proto.write(UidProto.Network.MOBILE_ACTIVE_COUNT,
7179 u.getMobileRadioActiveCount(which));
7180 proto.write(UidProto.Network.MOBILE_WAKEUP_COUNT,
7181 u.getMobileRadioApWakeupCount(which));
7182 proto.write(UidProto.Network.WIFI_WAKEUP_COUNT,
7183 u.getWifiRadioApWakeupCount(which));
7184 proto.write(UidProto.Network.MOBILE_BYTES_BG_RX,
7185 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA, which));
7186 proto.write(UidProto.Network.MOBILE_BYTES_BG_TX,
7187 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA, which));
7188 proto.write(UidProto.Network.WIFI_BYTES_BG_RX,
7189 u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which));
7190 proto.write(UidProto.Network.WIFI_BYTES_BG_TX,
7191 u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which));
7192 proto.write(UidProto.Network.MOBILE_PACKETS_BG_RX,
7193 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA, which));
7194 proto.write(UidProto.Network.MOBILE_PACKETS_BG_TX,
7195 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA, which));
7196 proto.write(UidProto.Network.WIFI_PACKETS_BG_RX,
7197 u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA, which));
7198 proto.write(UidProto.Network.WIFI_PACKETS_BG_TX,
7199 u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA, which));
7200 proto.end(nToken);
7201
7202 // Power use item (POWER_USE_ITEM_DATA)
7203 BatterySipper bs = uidToSipper.get(uid);
7204 if (bs != null) {
7205 final long bsToken = proto.start(UidProto.POWER_USE_ITEM);
7206 proto.write(UidProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7207 proto.write(UidProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7208 proto.write(UidProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7209 proto.write(UidProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7210 bs.proportionalSmearMah);
7211 proto.end(bsToken);
7212 }
7213
7214 // Processes (PROCESS_DATA)
7215 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats =
7216 u.getProcessStats();
7217 for (int ipr = processStats.size() - 1; ipr >= 0; --ipr) {
7218 final Uid.Proc ps = processStats.valueAt(ipr);
7219 final long prToken = proto.start(UidProto.PROCESS);
7220
7221 proto.write(UidProto.Process.NAME, processStats.keyAt(ipr));
7222 proto.write(UidProto.Process.USER_DURATION_MS, ps.getUserTime(which));
7223 proto.write(UidProto.Process.SYSTEM_DURATION_MS, ps.getSystemTime(which));
7224 proto.write(UidProto.Process.FOREGROUND_DURATION_MS, ps.getForegroundTime(which));
7225 proto.write(UidProto.Process.START_COUNT, ps.getStarts(which));
7226 proto.write(UidProto.Process.ANR_COUNT, ps.getNumAnrs(which));
7227 proto.write(UidProto.Process.CRASH_COUNT, ps.getNumCrashes(which));
7228
7229 proto.end(prToken);
7230 }
7231
7232 // Sensors (SENSOR_DATA)
7233 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
7234 for (int ise = 0; ise < sensors.size(); ++ise) {
7235 final Uid.Sensor se = sensors.valueAt(ise);
7236 final Timer timer = se.getSensorTime();
7237 if (timer == null) {
7238 continue;
7239 }
7240 final Timer bgTimer = se.getSensorBackgroundTime();
7241 final int sensorNumber = sensors.keyAt(ise);
7242 final long seToken = proto.start(UidProto.SENSORS);
7243
7244 proto.write(UidProto.Sensor.ID, sensorNumber);
7245 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7246 dumpTimer(proto, UidProto.Sensor.APPORTIONED, timer, rawRealtimeUs, which);
7247 dumpTimer(proto, UidProto.Sensor.BACKGROUND, bgTimer, rawRealtimeUs, which);
7248
7249 proto.end(seToken);
7250 }
7251
7252 // State times (STATE_TIME_DATA)
7253 for (int ips = 0; ips < Uid.NUM_PROCESS_STATE; ++ips) {
7254 long durMs = roundUsToMs(u.getProcessStateTime(ips, rawRealtimeUs, which));
7255 if (durMs == 0) {
7256 continue;
7257 }
7258 final long stToken = proto.start(UidProto.STATES);
7259 proto.write(UidProto.StateTime.STATE, ips);
7260 proto.write(UidProto.StateTime.DURATION_MS, durMs);
7261 proto.end(stToken);
7262 }
7263
7264 // Syncs (SYNC_DATA)
7265 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
7266 for (int isy = syncs.size() - 1; isy >= 0; --isy) {
7267 final Timer timer = syncs.valueAt(isy);
7268 final Timer bgTimer = timer.getSubTimer();
7269 final long syToken = proto.start(UidProto.SYNCS);
7270
7271 proto.write(UidProto.Sync.NAME, syncs.keyAt(isy));
7272 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7273 dumpTimer(proto, UidProto.Sync.TOTAL, timer, rawRealtimeUs, which);
7274 dumpTimer(proto, UidProto.Sync.BACKGROUND, bgTimer, rawRealtimeUs, which);
7275
7276 proto.end(syToken);
7277 }
7278
7279 // User activity (USER_ACTIVITY_DATA)
7280 if (u.hasUserActivity()) {
7281 for (int i = 0; i < Uid.NUM_USER_ACTIVITY_TYPES; ++i) {
7282 int val = u.getUserActivityCount(i, which);
7283 if (val != 0) {
7284 final long uaToken = proto.start(UidProto.USER_ACTIVITY);
7285 proto.write(UidProto.UserActivity.NAME, i);
7286 proto.write(UidProto.UserActivity.COUNT, val);
7287 proto.end(uaToken);
7288 }
7289 }
7290 }
7291
7292 // Vibrator (VIBRATOR_DATA)
7293 dumpTimer(proto, UidProto.VIBRATOR, u.getVibratorOnTimer(), rawRealtimeUs, which);
7294
7295 // Video (VIDEO_DATA)
7296 dumpTimer(proto, UidProto.VIDEO, u.getVideoTurnedOnTimer(), rawRealtimeUs, which);
7297
7298 // Wakelocks (WAKELOCK_DATA)
7299 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
7300 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7301 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7302 final long wToken = proto.start(UidProto.WAKELOCKS);
7303 proto.write(UidProto.Wakelock.NAME, wakelocks.keyAt(iw));
7304 dumpTimer(proto, UidProto.Wakelock.FULL, wl.getWakeTime(WAKE_TYPE_FULL),
7305 rawRealtimeUs, which);
7306 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7307 if (pTimer != null) {
7308 dumpTimer(proto, UidProto.Wakelock.PARTIAL, pTimer, rawRealtimeUs, which);
7309 dumpTimer(proto, UidProto.Wakelock.BACKGROUND_PARTIAL, pTimer.getSubTimer(),
7310 rawRealtimeUs, which);
7311 }
7312 dumpTimer(proto, UidProto.Wakelock.WINDOW, wl.getWakeTime(WAKE_TYPE_WINDOW),
7313 rawRealtimeUs, which);
7314 proto.end(wToken);
7315 }
7316
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007317 // Wifi Multicast Wakelock (WIFI_MULTICAST_WAKELOCK_DATA)
7318 dumpTimer(proto, UidProto.WIFI_MULTICAST_WAKELOCK, u.getMulticastWakelockStats(),
7319 rawRealtimeUs, which);
7320
Kweku Adams103351f2017-10-16 14:39:34 -07007321 // Wakeup alarms (WAKEUP_ALARM_DATA)
7322 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7323 final Uid.Pkg ps = packageStats.valueAt(ipkg);
7324 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
7325 for (int iwa = alarms.size() - 1; iwa >= 0; --iwa) {
7326 final long waToken = proto.start(UidProto.WAKEUP_ALARM);
7327 proto.write(UidProto.WakeupAlarm.NAME, alarms.keyAt(iwa));
7328 proto.write(UidProto.WakeupAlarm.COUNT,
7329 alarms.valueAt(iwa).getCountLocked(which));
7330 proto.end(waToken);
7331 }
7332 }
7333
7334 // Wifi Controller (WIFI_CONTROLLER_DATA)
7335 dumpControllerActivityProto(proto, UidProto.WIFI_CONTROLLER,
7336 u.getWifiControllerActivity(), which);
7337
7338 // Wifi data (WIFI_DATA)
7339 final long wToken = proto.start(UidProto.WIFI);
7340 proto.write(UidProto.Wifi.FULL_WIFI_LOCK_DURATION_MS,
7341 roundUsToMs(u.getFullWifiLockTime(rawRealtimeUs, which)));
7342 dumpTimer(proto, UidProto.Wifi.APPORTIONED_SCAN, u.getWifiScanTimer(),
7343 rawRealtimeUs, which);
7344 proto.write(UidProto.Wifi.RUNNING_DURATION_MS,
7345 roundUsToMs(u.getWifiRunningTime(rawRealtimeUs, which)));
7346 dumpTimer(proto, UidProto.Wifi.BACKGROUND_SCAN, u.getWifiScanBackgroundTimer(),
7347 rawRealtimeUs, which);
7348 proto.end(wToken);
7349
7350 proto.end(uTkn);
7351 }
7352 }
7353
7354 private void dumpProtoSystemLocked(ProtoOutputStream proto, BatteryStatsHelper helper) {
Kweku Adams87b19ec2017-10-09 12:40:03 -07007355 final long sToken = proto.start(BatteryStatsProto.SYSTEM);
7356 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7357 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7358 final long rawRealtimeUs = rawRealtimeMs * 1000;
7359 final int which = STATS_SINCE_CHARGED;
7360
7361 // Battery data (BATTERY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007362 final long bToken = proto.start(SystemProto.BATTERY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007363 proto.write(SystemProto.Battery.START_CLOCK_TIME_MS, getStartClockTime());
7364 proto.write(SystemProto.Battery.START_COUNT, getStartCount());
7365 proto.write(SystemProto.Battery.TOTAL_REALTIME_MS,
7366 computeRealtime(rawRealtimeUs, which) / 1000);
7367 proto.write(SystemProto.Battery.TOTAL_UPTIME_MS,
7368 computeUptime(rawUptimeUs, which) / 1000);
7369 proto.write(SystemProto.Battery.BATTERY_REALTIME_MS,
7370 computeBatteryRealtime(rawRealtimeUs, which) / 1000);
7371 proto.write(SystemProto.Battery.BATTERY_UPTIME_MS,
7372 computeBatteryUptime(rawUptimeUs, which) / 1000);
7373 proto.write(SystemProto.Battery.SCREEN_OFF_REALTIME_MS,
7374 computeBatteryScreenOffRealtime(rawRealtimeUs, which) / 1000);
7375 proto.write(SystemProto.Battery.SCREEN_OFF_UPTIME_MS,
7376 computeBatteryScreenOffUptime(rawUptimeUs, which) / 1000);
7377 proto.write(SystemProto.Battery.SCREEN_DOZE_DURATION_MS,
7378 getScreenDozeTime(rawRealtimeUs, which) / 1000);
7379 proto.write(SystemProto.Battery.ESTIMATED_BATTERY_CAPACITY_MAH,
7380 getEstimatedBatteryCapacity());
7381 proto.write(SystemProto.Battery.MIN_LEARNED_BATTERY_CAPACITY_UAH,
7382 getMinLearnedBatteryCapacity());
7383 proto.write(SystemProto.Battery.MAX_LEARNED_BATTERY_CAPACITY_UAH,
7384 getMaxLearnedBatteryCapacity());
Kweku Adams103351f2017-10-16 14:39:34 -07007385 proto.end(bToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007386
7387 // Battery discharge (BATTERY_DISCHARGE_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007388 final long bdToken = proto.start(SystemProto.BATTERY_DISCHARGE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007389 proto.write(SystemProto.BatteryDischarge.LOWER_BOUND_SINCE_CHARGE,
7390 getLowDischargeAmountSinceCharge());
7391 proto.write(SystemProto.BatteryDischarge.UPPER_BOUND_SINCE_CHARGE,
7392 getHighDischargeAmountSinceCharge());
7393 proto.write(SystemProto.BatteryDischarge.SCREEN_ON_SINCE_CHARGE,
7394 getDischargeAmountScreenOnSinceCharge());
7395 proto.write(SystemProto.BatteryDischarge.SCREEN_OFF_SINCE_CHARGE,
7396 getDischargeAmountScreenOffSinceCharge());
7397 proto.write(SystemProto.BatteryDischarge.SCREEN_DOZE_SINCE_CHARGE,
7398 getDischargeAmountScreenDozeSinceCharge());
7399 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH,
7400 getUahDischarge(which) / 1000);
7401 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_OFF,
7402 getUahDischargeScreenOff(which) / 1000);
7403 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_DOZE,
7404 getUahDischargeScreenDoze(which) / 1000);
Mike Ma15313c92017-11-15 17:58:21 -08007405 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_LIGHT_DOZE,
7406 getUahDischargeLightDoze(which) / 1000);
7407 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_DEEP_DOZE,
7408 getUahDischargeDeepDoze(which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007409 proto.end(bdToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007410
7411 // Time remaining
7412 long timeRemainingUs = computeChargeTimeRemaining(rawRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -07007413 // These are part of a oneof, so we should only set one of them.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007414 if (timeRemainingUs >= 0) {
7415 // Charge time remaining (CHARGE_TIME_REMAIN_DATA)
7416 proto.write(SystemProto.CHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7417 } else {
7418 timeRemainingUs = computeBatteryTimeRemaining(rawRealtimeUs);
7419 // Discharge time remaining (DISCHARGE_TIME_REMAIN_DATA)
7420 if (timeRemainingUs >= 0) {
7421 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7422 } else {
7423 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, -1);
7424 }
7425 }
7426
7427 // Charge step (CHARGE_STEP_DATA)
7428 dumpDurationSteps(proto, SystemProto.CHARGE_STEP, getChargeLevelStepTracker());
7429
7430 // Phone data connection (DATA_CONNECTION_TIME_DATA and DATA_CONNECTION_COUNT_DATA)
7431 for (int i = 0; i < NUM_DATA_CONNECTION_TYPES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007432 final long pdcToken = proto.start(SystemProto.DATA_CONNECTION);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007433 proto.write(SystemProto.DataConnection.NAME, i);
7434 dumpTimer(proto, SystemProto.DataConnection.TOTAL, getPhoneDataConnectionTimer(i),
7435 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007436 proto.end(pdcToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007437 }
7438
7439 // Discharge step (DISCHARGE_STEP_DATA)
7440 dumpDurationSteps(proto, SystemProto.DISCHARGE_STEP, getDischargeLevelStepTracker());
7441
7442 // CPU frequencies (GLOBAL_CPU_FREQ_DATA)
7443 final long[] cpuFreqs = getCpuFreqs();
7444 if (cpuFreqs != null) {
7445 for (long i : cpuFreqs) {
7446 proto.write(SystemProto.CPU_FREQUENCY, i);
7447 }
7448 }
7449
7450 // Bluetooth controller (GLOBAL_BLUETOOTH_CONTROLLER_DATA)
7451 dumpControllerActivityProto(proto, SystemProto.GLOBAL_BLUETOOTH_CONTROLLER,
7452 getBluetoothControllerActivity(), which);
7453
7454 // Modem controller (GLOBAL_MODEM_CONTROLLER_DATA)
7455 dumpControllerActivityProto(proto, SystemProto.GLOBAL_MODEM_CONTROLLER,
7456 getModemControllerActivity(), which);
7457
7458 // Global network data (GLOBAL_NETWORK_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007459 final long gnToken = proto.start(SystemProto.GLOBAL_NETWORK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007460 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_RX,
7461 getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7462 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_TX,
7463 getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7464 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_RX,
7465 getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7466 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_TX,
7467 getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7468 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_RX,
7469 getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7470 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_TX,
7471 getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7472 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_RX,
7473 getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7474 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_TX,
7475 getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7476 proto.write(SystemProto.GlobalNetwork.BT_BYTES_RX,
7477 getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7478 proto.write(SystemProto.GlobalNetwork.BT_BYTES_TX,
7479 getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
Kweku Adams103351f2017-10-16 14:39:34 -07007480 proto.end(gnToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007481
7482 // Wifi controller (GLOBAL_WIFI_CONTROLLER_DATA)
7483 dumpControllerActivityProto(proto, SystemProto.GLOBAL_WIFI_CONTROLLER,
7484 getWifiControllerActivity(), which);
7485
7486
7487 // Global wifi (GLOBAL_WIFI_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007488 final long gwToken = proto.start(SystemProto.GLOBAL_WIFI);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007489 proto.write(SystemProto.GlobalWifi.ON_DURATION_MS,
7490 getWifiOnTime(rawRealtimeUs, which) / 1000);
7491 proto.write(SystemProto.GlobalWifi.RUNNING_DURATION_MS,
7492 getGlobalWifiRunningTime(rawRealtimeUs, which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007493 proto.end(gwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007494
7495 // Kernel wakelock (KERNEL_WAKELOCK_DATA)
7496 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
7497 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007498 final long kwToken = proto.start(SystemProto.KERNEL_WAKELOCK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007499 proto.write(SystemProto.KernelWakelock.NAME, ent.getKey());
7500 dumpTimer(proto, SystemProto.KernelWakelock.TOTAL, ent.getValue(),
7501 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007502 proto.end(kwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007503 }
7504
7505 // Misc (MISC_DATA)
7506 // Calculate wakelock times across all uids.
7507 long fullWakeLockTimeTotalUs = 0;
7508 long partialWakeLockTimeTotalUs = 0;
7509
7510 final SparseArray<? extends Uid> uidStats = getUidStats();
7511 for (int iu = 0; iu < uidStats.size(); iu++) {
7512 final Uid u = uidStats.valueAt(iu);
7513
7514 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks =
7515 u.getWakelockStats();
7516 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7517 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7518
7519 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
7520 if (fullWakeTimer != null) {
7521 fullWakeLockTimeTotalUs += fullWakeTimer.getTotalTimeLocked(rawRealtimeUs,
7522 which);
7523 }
7524
7525 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7526 if (partialWakeTimer != null) {
7527 partialWakeLockTimeTotalUs += partialWakeTimer.getTotalTimeLocked(
7528 rawRealtimeUs, which);
7529 }
7530 }
7531 }
Kweku Adams103351f2017-10-16 14:39:34 -07007532 final long mToken = proto.start(SystemProto.MISC);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007533 proto.write(SystemProto.Misc.SCREEN_ON_DURATION_MS,
7534 getScreenOnTime(rawRealtimeUs, which) / 1000);
7535 proto.write(SystemProto.Misc.PHONE_ON_DURATION_MS,
7536 getPhoneOnTime(rawRealtimeUs, which) / 1000);
7537 proto.write(SystemProto.Misc.FULL_WAKELOCK_TOTAL_DURATION_MS,
7538 fullWakeLockTimeTotalUs / 1000);
7539 proto.write(SystemProto.Misc.PARTIAL_WAKELOCK_TOTAL_DURATION_MS,
7540 partialWakeLockTimeTotalUs / 1000);
7541 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_DURATION_MS,
7542 getMobileRadioActiveTime(rawRealtimeUs, which) / 1000);
7543 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_ADJUSTED_TIME_MS,
7544 getMobileRadioActiveAdjustedTime(which) / 1000);
7545 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_COUNT,
7546 getMobileRadioActiveCount(which));
7547 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_UNKNOWN_DURATION_MS,
7548 getMobileRadioActiveUnknownTime(which) / 1000);
7549 proto.write(SystemProto.Misc.INTERACTIVE_DURATION_MS,
7550 getInteractiveTime(rawRealtimeUs, which) / 1000);
7551 proto.write(SystemProto.Misc.BATTERY_SAVER_MODE_ENABLED_DURATION_MS,
7552 getPowerSaveModeEnabledTime(rawRealtimeUs, which) / 1000);
7553 proto.write(SystemProto.Misc.NUM_CONNECTIVITY_CHANGES,
7554 getNumConnectivityChange(which));
7555 proto.write(SystemProto.Misc.DEEP_DOZE_ENABLED_DURATION_MS,
7556 getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7557 proto.write(SystemProto.Misc.DEEP_DOZE_COUNT,
7558 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
7559 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_DURATION_MS,
7560 getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7561 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_COUNT,
7562 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
7563 proto.write(SystemProto.Misc.LONGEST_DEEP_DOZE_DURATION_MS,
7564 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
7565 proto.write(SystemProto.Misc.LIGHT_DOZE_ENABLED_DURATION_MS,
7566 getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7567 proto.write(SystemProto.Misc.LIGHT_DOZE_COUNT,
7568 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
7569 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_DURATION_MS,
7570 getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7571 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_COUNT,
7572 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
7573 proto.write(SystemProto.Misc.LONGEST_LIGHT_DOZE_DURATION_MS,
7574 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
Kweku Adams103351f2017-10-16 14:39:34 -07007575 proto.end(mToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007576
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007577 // Wifi multicast wakelock total stats (WIFI_MULTICAST_WAKELOCK_TOTAL_DATA)
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08007578 final long multicastWakeLockTimeTotalUs =
7579 getWifiMulticastWakelockTime(rawRealtimeUs, which);
7580 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007581 final long wmctToken = proto.start(SystemProto.WIFI_MULTICAST_WAKELOCK_TOTAL);
7582 proto.write(SystemProto.WifiMulticastWakelockTotal.DURATION_MS,
7583 multicastWakeLockTimeTotalUs / 1000);
7584 proto.write(SystemProto.WifiMulticastWakelockTotal.COUNT,
7585 multicastWakeLockCountTotal);
7586 proto.end(wmctToken);
7587
Kweku Adams87b19ec2017-10-09 12:40:03 -07007588 // Power use item (POWER_USE_ITEM_DATA)
7589 final List<BatterySipper> sippers = helper.getUsageList();
7590 if (sippers != null) {
7591 for (int i = 0; i < sippers.size(); ++i) {
7592 final BatterySipper bs = sippers.get(i);
7593 int n = SystemProto.PowerUseItem.UNKNOWN_SIPPER;
7594 int uid = 0;
7595 switch (bs.drainType) {
7596 case IDLE:
7597 n = SystemProto.PowerUseItem.IDLE;
7598 break;
7599 case CELL:
7600 n = SystemProto.PowerUseItem.CELL;
7601 break;
7602 case PHONE:
7603 n = SystemProto.PowerUseItem.PHONE;
7604 break;
7605 case WIFI:
7606 n = SystemProto.PowerUseItem.WIFI;
7607 break;
7608 case BLUETOOTH:
7609 n = SystemProto.PowerUseItem.BLUETOOTH;
7610 break;
7611 case SCREEN:
7612 n = SystemProto.PowerUseItem.SCREEN;
7613 break;
7614 case FLASHLIGHT:
7615 n = SystemProto.PowerUseItem.FLASHLIGHT;
7616 break;
7617 case APP:
Kweku Adams103351f2017-10-16 14:39:34 -07007618 // dumpProtoAppsLocked will handle this.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007619 continue;
7620 case USER:
7621 n = SystemProto.PowerUseItem.USER;
7622 uid = UserHandle.getUid(bs.userId, 0);
7623 break;
7624 case UNACCOUNTED:
7625 n = SystemProto.PowerUseItem.UNACCOUNTED;
7626 break;
7627 case OVERCOUNTED:
7628 n = SystemProto.PowerUseItem.OVERCOUNTED;
7629 break;
7630 case CAMERA:
7631 n = SystemProto.PowerUseItem.CAMERA;
7632 break;
7633 case MEMORY:
7634 n = SystemProto.PowerUseItem.MEMORY;
7635 break;
7636 }
Kweku Adams103351f2017-10-16 14:39:34 -07007637 final long puiToken = proto.start(SystemProto.POWER_USE_ITEM);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007638 proto.write(SystemProto.PowerUseItem.NAME, n);
7639 proto.write(SystemProto.PowerUseItem.UID, uid);
7640 proto.write(SystemProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7641 proto.write(SystemProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7642 proto.write(SystemProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7643 proto.write(SystemProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7644 bs.proportionalSmearMah);
Kweku Adams103351f2017-10-16 14:39:34 -07007645 proto.end(puiToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007646 }
7647 }
7648
7649 // Power use summary (POWER_USE_SUMMARY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007650 final long pusToken = proto.start(SystemProto.POWER_USE_SUMMARY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007651 proto.write(SystemProto.PowerUseSummary.BATTERY_CAPACITY_MAH,
7652 helper.getPowerProfile().getBatteryCapacity());
7653 proto.write(SystemProto.PowerUseSummary.COMPUTED_POWER_MAH, helper.getComputedPower());
7654 proto.write(SystemProto.PowerUseSummary.MIN_DRAINED_POWER_MAH, helper.getMinDrainedPower());
7655 proto.write(SystemProto.PowerUseSummary.MAX_DRAINED_POWER_MAH, helper.getMaxDrainedPower());
Kweku Adams103351f2017-10-16 14:39:34 -07007656 proto.end(pusToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007657
7658 // RPM stats (RESOURCE_POWER_MANAGER_DATA)
7659 final Map<String, ? extends Timer> rpmStats = getRpmStats();
7660 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
7661 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007662 final long rpmToken = proto.start(SystemProto.RESOURCE_POWER_MANAGER);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007663 proto.write(SystemProto.ResourcePowerManager.NAME, ent.getKey());
7664 dumpTimer(proto, SystemProto.ResourcePowerManager.TOTAL,
7665 ent.getValue(), rawRealtimeUs, which);
7666 dumpTimer(proto, SystemProto.ResourcePowerManager.SCREEN_OFF,
7667 screenOffRpmStats.get(ent.getKey()), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007668 proto.end(rpmToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007669 }
7670
7671 // Screen brightness (SCREEN_BRIGHTNESS_DATA)
7672 for (int i = 0; i < NUM_SCREEN_BRIGHTNESS_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007673 final long sbToken = proto.start(SystemProto.SCREEN_BRIGHTNESS);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007674 proto.write(SystemProto.ScreenBrightness.NAME, i);
7675 dumpTimer(proto, SystemProto.ScreenBrightness.TOTAL, getScreenBrightnessTimer(i),
7676 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007677 proto.end(sbToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007678 }
7679
7680 // Signal scanning time (SIGNAL_SCANNING_TIME_DATA)
7681 dumpTimer(proto, SystemProto.SIGNAL_SCANNING, getPhoneSignalScanningTimer(), rawRealtimeUs,
7682 which);
7683
7684 // Phone signal strength (SIGNAL_STRENGTH_TIME_DATA and SIGNAL_STRENGTH_COUNT_DATA)
7685 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007686 final long pssToken = proto.start(SystemProto.PHONE_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007687 proto.write(SystemProto.PhoneSignalStrength.NAME, i);
7688 dumpTimer(proto, SystemProto.PhoneSignalStrength.TOTAL, getPhoneSignalStrengthTimer(i),
7689 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007690 proto.end(pssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007691 }
7692
7693 // Wakeup reasons (WAKEUP_REASON_DATA)
7694 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
7695 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007696 final long wrToken = proto.start(SystemProto.WAKEUP_REASON);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007697 proto.write(SystemProto.WakeupReason.NAME, ent.getKey());
7698 dumpTimer(proto, SystemProto.WakeupReason.TOTAL, ent.getValue(), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007699 proto.end(wrToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007700 }
7701
7702 // Wifi signal strength (WIFI_SIGNAL_STRENGTH_TIME_DATA and WIFI_SIGNAL_STRENGTH_COUNT_DATA)
7703 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007704 final long wssToken = proto.start(SystemProto.WIFI_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007705 proto.write(SystemProto.WifiSignalStrength.NAME, i);
7706 dumpTimer(proto, SystemProto.WifiSignalStrength.TOTAL, getWifiSignalStrengthTimer(i),
7707 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007708 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007709 }
7710
7711 // Wifi state (WIFI_STATE_TIME_DATA and WIFI_STATE_COUNT_DATA)
7712 for (int i = 0; i < NUM_WIFI_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007713 final long wsToken = proto.start(SystemProto.WIFI_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007714 proto.write(SystemProto.WifiState.NAME, i);
7715 dumpTimer(proto, SystemProto.WifiState.TOTAL, getWifiStateTimer(i),
7716 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007717 proto.end(wsToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007718 }
7719
7720 // Wifi supplicant state (WIFI_SUPPL_STATE_TIME_DATA and WIFI_SUPPL_STATE_COUNT_DATA)
7721 for (int i = 0; i < NUM_WIFI_SUPPL_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007722 final long wssToken = proto.start(SystemProto.WIFI_SUPPLICANT_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007723 proto.write(SystemProto.WifiSupplicantState.NAME, i);
7724 dumpTimer(proto, SystemProto.WifiSupplicantState.TOTAL, getWifiSupplStateTimer(i),
7725 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007726 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007727 }
7728
7729 proto.end(sToken);
7730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007731}