blob: dc271d8639d562786430fd1f92591261f3078dcd [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);
686
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800687 /**
688 * Returns cpu times of an uid at a particular process state.
689 */
690 public abstract long[] getCpuFreqTimes(int which, int procState);
691 /**
692 * Returns cpu times of an uid while the screen if off at a particular process state.
693 */
694 public abstract long[] getScreenOffCpuFreqTimes(int which, int procState);
695
Dianne Hackborna0200e32016-03-30 18:01:41 -0700696 // Note: the following times are disjoint. They can be added together to find the
697 // total time a uid has had any processes running at all.
698
699 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800700 * Time this uid has any processes in the top state.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700701 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800702 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700703 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800704 * Time this uid has any process with a started foreground service, but
Dianne Hackborna0200e32016-03-30 18:01:41 -0700705 * none in the "top" state.
706 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800707 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700708 /**
Dianne Hackborna0200e32016-03-30 18:01:41 -0700709 * Time this uid has any process in an active foreground state, but none in the
zhouwenjie46712bc2018-01-11 15:21:27 -0800710 * "foreground service" or better state. Persistent and other foreground states go here.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700711 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800712 public static final int PROCESS_STATE_FOREGROUND = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700713 /**
714 * Time this uid has any process in an active background state, but none in the
715 * "foreground" or better state.
716 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800717 public static final int PROCESS_STATE_BACKGROUND = 3;
718 /**
719 * Time this uid has any process that is top while the device is sleeping, but not
720 * active for any other reason. We kind-of consider it a kind of cached process
721 * for execution restrictions.
722 */
723 public static final int PROCESS_STATE_TOP_SLEEPING = 4;
724 /**
725 * Time this uid has any process that is in the background but it has an activity
726 * marked as "can't save state". This is essentially a cached process, though the
727 * system will try much harder than normal to avoid killing it.
728 */
729 public static final int PROCESS_STATE_HEAVY_WEIGHT = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700730 /**
731 * Time this uid has any processes that are sitting around cached, not in one of the
732 * other active states.
733 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800734 public static final int PROCESS_STATE_CACHED = 6;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700735 /**
736 * Total number of process states we track.
737 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800738 public static final int NUM_PROCESS_STATE = 7;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700739
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800740 // Used in dump
Dianne Hackborn61659e52014-07-09 16:13:01 -0700741 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800742 "Top", "Fg Service", "Foreground", "Background", "Top Sleeping", "Heavy Weight",
743 "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700744 };
745
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800746 // Used in checkin dump
747 @VisibleForTesting
748 public static final String[] UID_PROCESS_TYPES = {
749 "T", // TOP
750 "FS", // FOREGROUND_SERVICE
751 "F", // FOREGROUND
752 "B", // BACKGROUND
753 "TS", // TOP_SLEEPING
754 "HW", // HEAVY_WEIGHT
755 "C" // CACHED
756 };
757
758 /**
759 * When the process exits one of these states, we need to make sure cpu time in this state
760 * is not attributed to any non-critical process states.
761 */
762 public static final int[] CRITICAL_PROC_STATES = {
763 PROCESS_STATE_TOP, PROCESS_STATE_FOREGROUND_SERVICE, PROCESS_STATE_FOREGROUND
764 };
765
Dianne Hackborn61659e52014-07-09 16:13:01 -0700766 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800767 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700768
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800769 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770
Robert Greenwalta029ea12013-09-25 16:38:12 -0700771 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
772
Dianne Hackborn617f8772009-03-31 15:04:46 -0700773 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700774 * Note that these must match the constants in android.os.PowerManager.
775 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
776 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700777 */
778 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700779 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700780 };
Bookatzc8c44962017-05-11 12:12:54 -0700781
Phil Weaverda80d672016-03-15 16:25:46 -0700782 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700783
Dianne Hackborn617f8772009-03-31 15:04:46 -0700784 public abstract void noteUserActivityLocked(int type);
785 public abstract boolean hasUserActivity();
786 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700787
788 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800789 public abstract long getNetworkActivityBytes(int type, int which);
790 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800791 public abstract long getMobileRadioActiveTime(int which);
792 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700793
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700794 /**
795 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
796 */
797 public abstract long getUserCpuTimeUs(int which);
798
799 /**
800 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
801 */
802 public abstract long getSystemCpuTimeUs(int which);
803
804 /**
Sudheer Shanka71f34b32017-07-21 00:14:24 -0700805 * Returns the approximate cpu time (in microseconds) spent at a certain CPU speed for a
Adam Lesinski6832f392015-09-05 18:05:40 -0700806 * given CPU cluster.
807 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700808 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700809 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700810 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
811 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700812 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700813 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700814
Adam Lesinski5f056f62016-07-14 16:56:08 -0700815 /**
816 * Returns the number of times this UID woke up the Application Processor to
817 * process a mobile radio packet.
818 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
819 */
820 public abstract long getMobileRadioApWakeupCount(int which);
821
822 /**
823 * Returns the number of times this UID woke up the Application Processor to
824 * process a WiFi packet.
825 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
826 */
827 public abstract long getWifiRadioApWakeupCount(int which);
828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800830 /*
831 * FIXME: it's not correct to use this magic value because it
832 * could clash with a sensor handle (which are defined by
833 * the sensor HAL, and therefore out of our control
834 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 // Magic sensor number for the GPS.
836 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800841
Bookatz867c0d72017-03-07 18:23:42 -0800842 /** Returns a Timer for sensor usage when app is in the background. */
843 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 }
845
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700846 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800847 public int mWakeNesting;
848 public long mWakeSumMs;
849 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700850 }
851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 /**
853 * The statistics associated with a particular process.
854 */
855 public static abstract class Proc {
856
Dianne Hackborn287952c2010-09-22 22:34:31 -0700857 public static class ExcessivePower {
858 public static final int TYPE_WAKE = 1;
859 public static final int TYPE_CPU = 2;
860
861 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700862 public long overTime;
863 public long usedTime;
864 }
865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800867 * Returns true if this process is still active in the battery stats.
868 */
869 public abstract boolean isActive();
870
871 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700872 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700874 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 */
876 public abstract long getUserTime(int which);
877
878 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700879 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700881 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 */
883 public abstract long getSystemTime(int which);
884
885 /**
886 * Returns the number of times the process has been started.
887 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700888 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 */
890 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700891
892 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800893 * Returns the number of times the process has crashed.
894 *
895 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
896 */
897 public abstract int getNumCrashes(int which);
898
899 /**
900 * Returns the number of times the process has ANRed.
901 *
902 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
903 */
904 public abstract int getNumAnrs(int which);
905
906 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700907 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700908 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700909 * @return foreground cpu time in microseconds
910 */
911 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700912
Dianne Hackborn287952c2010-09-22 22:34:31 -0700913 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700914
Dianne Hackborn287952c2010-09-22 22:34:31 -0700915 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 }
917
918 /**
919 * The statistics associated with a particular package.
920 */
921 public static abstract class Pkg {
922
923 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700924 * Returns information about all wakeup alarms that have been triggered for this
925 * package. The mapping keys are tag names for the alarms, the counter contains
926 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700928 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929
930 /**
931 * Returns a mapping containing service statistics.
932 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700933 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934
935 /**
936 * The statistics associated with a particular service.
937 */
Joe Onoratoabded112016-02-08 16:49:39 -0800938 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939
940 /**
941 * Returns the amount of time spent started.
942 *
943 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700944 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 * @return
946 */
947 public abstract long getStartTime(long batteryUptime, int which);
948
949 /**
950 * Returns the total number of times startService() has been called.
951 *
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 */
954 public abstract int getStarts(int which);
955
956 /**
957 * Returns the total number times the service has been launched.
958 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700959 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 */
961 public abstract int getLaunches(int which);
962 }
963 }
964 }
965
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800966 public static final class LevelStepTracker {
967 public long mLastStepTime = -1;
968 public int mNumStepDurations;
969 public final long[] mStepDurations;
970
971 public LevelStepTracker(int maxLevelSteps) {
972 mStepDurations = new long[maxLevelSteps];
973 }
974
975 public LevelStepTracker(int numSteps, long[] steps) {
976 mNumStepDurations = numSteps;
977 mStepDurations = new long[numSteps];
978 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
979 }
980
981 public long getDurationAt(int index) {
982 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
983 }
984
985 public int getLevelAt(int index) {
986 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
987 >> STEP_LEVEL_LEVEL_SHIFT);
988 }
989
990 public int getInitModeAt(int index) {
991 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
992 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
993 }
994
995 public int getModModeAt(int index) {
996 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
997 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
998 }
999
1000 private void appendHex(long val, int topOffset, StringBuilder out) {
1001 boolean hasData = false;
1002 while (topOffset >= 0) {
1003 int digit = (int)( (val>>topOffset) & 0xf );
1004 topOffset -= 4;
1005 if (!hasData && digit == 0) {
1006 continue;
1007 }
1008 hasData = true;
1009 if (digit >= 0 && digit <= 9) {
1010 out.append((char)('0' + digit));
1011 } else {
1012 out.append((char)('a' + digit - 10));
1013 }
1014 }
1015 }
1016
1017 public void encodeEntryAt(int index, StringBuilder out) {
1018 long item = mStepDurations[index];
1019 long duration = item & STEP_LEVEL_TIME_MASK;
1020 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
1021 >> STEP_LEVEL_LEVEL_SHIFT);
1022 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
1023 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1024 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
1025 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1026 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1027 case Display.STATE_OFF: out.append('f'); break;
1028 case Display.STATE_ON: out.append('o'); break;
1029 case Display.STATE_DOZE: out.append('d'); break;
1030 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
1031 }
1032 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1033 out.append('p');
1034 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001035 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1036 out.append('i');
1037 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001038 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1039 case Display.STATE_OFF: out.append('F'); break;
1040 case Display.STATE_ON: out.append('O'); break;
1041 case Display.STATE_DOZE: out.append('D'); break;
1042 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
1043 }
1044 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1045 out.append('P');
1046 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001047 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1048 out.append('I');
1049 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001050 out.append('-');
1051 appendHex(level, 4, out);
1052 out.append('-');
1053 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
1054 }
1055
1056 public void decodeEntryAt(int index, String value) {
1057 final int N = value.length();
1058 int i = 0;
1059 char c;
1060 long out = 0;
1061 while (i < N && (c=value.charAt(i)) != '-') {
1062 i++;
1063 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001064 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001065 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001066 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001067 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001068 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001069 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001070 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1071 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1072 break;
1073 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1074 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1075 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001076 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1077 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1078 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001079 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1080 break;
1081 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1082 break;
1083 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1084 break;
1085 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1086 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1087 break;
1088 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1089 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001090 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001091 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1092 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1093 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001094 }
1095 }
1096 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001097 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001098 while (i < N && (c=value.charAt(i)) != '-') {
1099 i++;
1100 level <<= 4;
1101 if (c >= '0' && c <= '9') {
1102 level += c - '0';
1103 } else if (c >= 'a' && c <= 'f') {
1104 level += c - 'a' + 10;
1105 } else if (c >= 'A' && c <= 'F') {
1106 level += c - 'A' + 10;
1107 }
1108 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001109 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001110 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
1111 long duration = 0;
1112 while (i < N && (c=value.charAt(i)) != '-') {
1113 i++;
1114 duration <<= 4;
1115 if (c >= '0' && c <= '9') {
1116 duration += c - '0';
1117 } else if (c >= 'a' && c <= 'f') {
1118 duration += c - 'a' + 10;
1119 } else if (c >= 'A' && c <= 'F') {
1120 duration += c - 'A' + 10;
1121 }
1122 }
1123 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
1124 }
1125
1126 public void init() {
1127 mLastStepTime = -1;
1128 mNumStepDurations = 0;
1129 }
1130
1131 public void clearTime() {
1132 mLastStepTime = -1;
1133 }
1134
1135 public long computeTimePerLevel() {
1136 final long[] steps = mStepDurations;
1137 final int numSteps = mNumStepDurations;
1138
1139 // For now we'll do a simple average across all steps.
1140 if (numSteps <= 0) {
1141 return -1;
1142 }
1143 long total = 0;
1144 for (int i=0; i<numSteps; i++) {
1145 total += steps[i] & STEP_LEVEL_TIME_MASK;
1146 }
1147 return total / numSteps;
1148 /*
1149 long[] buckets = new long[numSteps];
1150 int numBuckets = 0;
1151 int numToAverage = 4;
1152 int i = 0;
1153 while (i < numSteps) {
1154 long totalTime = 0;
1155 int num = 0;
1156 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1157 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1158 num++;
1159 }
1160 buckets[numBuckets] = totalTime / num;
1161 numBuckets++;
1162 numToAverage *= 2;
1163 i += num;
1164 }
1165 if (numBuckets < 1) {
1166 return -1;
1167 }
1168 long averageTime = buckets[numBuckets-1];
1169 for (i=numBuckets-2; i>=0; i--) {
1170 averageTime = (averageTime + buckets[i]) / 2;
1171 }
1172 return averageTime;
1173 */
1174 }
1175
1176 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1177 int[] outNumOfInterest) {
1178 final long[] steps = mStepDurations;
1179 final int count = mNumStepDurations;
1180 if (count <= 0) {
1181 return -1;
1182 }
1183 long total = 0;
1184 int numOfInterest = 0;
1185 for (int i=0; i<count; i++) {
1186 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1187 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1188 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1189 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1190 // If the modes of interest didn't change during this step period...
1191 if ((modMode&modesOfInterest) == 0) {
1192 // And the mode values during this period match those we are measuring...
1193 if ((initMode&modesOfInterest) == modeValues) {
1194 // Then this can be used to estimate the total time!
1195 numOfInterest++;
1196 total += steps[i] & STEP_LEVEL_TIME_MASK;
1197 }
1198 }
1199 }
1200 if (numOfInterest <= 0) {
1201 return -1;
1202 }
1203
1204 if (outNumOfInterest != null) {
1205 outNumOfInterest[0] = numOfInterest;
1206 }
1207
1208 // The estimated time is the average time we spend in each level, multipled
1209 // by 100 -- the total number of battery levels
1210 return (total / numOfInterest) * 100;
1211 }
1212
1213 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1214 int stepCount = mNumStepDurations;
1215 final long lastStepTime = mLastStepTime;
1216 if (lastStepTime >= 0 && numStepLevels > 0) {
1217 final long[] steps = mStepDurations;
1218 long duration = elapsedRealtime - lastStepTime;
1219 for (int i=0; i<numStepLevels; i++) {
1220 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1221 long thisDuration = duration / (numStepLevels-i);
1222 duration -= thisDuration;
1223 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1224 thisDuration = STEP_LEVEL_TIME_MASK;
1225 }
1226 steps[0] = thisDuration | modeBits;
1227 }
1228 stepCount += numStepLevels;
1229 if (stepCount > steps.length) {
1230 stepCount = steps.length;
1231 }
1232 }
1233 mNumStepDurations = stepCount;
1234 mLastStepTime = elapsedRealtime;
1235 }
1236
1237 public void readFromParcel(Parcel in) {
1238 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001239 if (N > mStepDurations.length) {
1240 throw new ParcelFormatException("more step durations than available: " + N);
1241 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001242 mNumStepDurations = N;
1243 for (int i=0; i<N; i++) {
1244 mStepDurations[i] = in.readLong();
1245 }
1246 }
1247
1248 public void writeToParcel(Parcel out) {
1249 final int N = mNumStepDurations;
1250 out.writeInt(N);
1251 for (int i=0; i<N; i++) {
1252 out.writeLong(mStepDurations[i]);
1253 }
1254 }
1255 }
1256
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001257 public static final class PackageChange {
1258 public String mPackageName;
1259 public boolean mUpdate;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001260 public long mVersionCode;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001261 }
1262
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001263 public static final class DailyItem {
1264 public long mStartTime;
1265 public long mEndTime;
1266 public LevelStepTracker mDischargeSteps;
1267 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001268 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001269 }
1270
1271 public abstract DailyItem getDailyItemLocked(int daysAgo);
1272
1273 public abstract long getCurrentDailyStartTime();
1274
1275 public abstract long getNextMinDailyDeadline();
1276
1277 public abstract long getNextMaxDailyDeadline();
1278
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001279 public abstract long[] getCpuFreqs();
1280
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001281 public final static class HistoryTag {
1282 public String string;
1283 public int uid;
1284
1285 public int poolIdx;
1286
1287 public void setTo(HistoryTag o) {
1288 string = o.string;
1289 uid = o.uid;
1290 poolIdx = o.poolIdx;
1291 }
1292
1293 public void setTo(String _string, int _uid) {
1294 string = _string;
1295 uid = _uid;
1296 poolIdx = -1;
1297 }
1298
1299 public void writeToParcel(Parcel dest, int flags) {
1300 dest.writeString(string);
1301 dest.writeInt(uid);
1302 }
1303
1304 public void readFromParcel(Parcel src) {
1305 string = src.readString();
1306 uid = src.readInt();
1307 poolIdx = -1;
1308 }
1309
1310 @Override
1311 public boolean equals(Object o) {
1312 if (this == o) return true;
1313 if (o == null || getClass() != o.getClass()) return false;
1314
1315 HistoryTag that = (HistoryTag) o;
1316
1317 if (uid != that.uid) return false;
1318 if (!string.equals(that.string)) return false;
1319
1320 return true;
1321 }
1322
1323 @Override
1324 public int hashCode() {
1325 int result = string.hashCode();
1326 result = 31 * result + uid;
1327 return result;
1328 }
1329 }
1330
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001331 /**
1332 * Optional detailed information that can go into a history step. This is typically
1333 * generated each time the battery level changes.
1334 */
1335 public final static class HistoryStepDetails {
1336 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1337 public int userTime;
1338 public int systemTime;
1339
1340 // Top three apps using CPU in the last step, with times in 1/100 second.
1341 public int appCpuUid1;
1342 public int appCpuUTime1;
1343 public int appCpuSTime1;
1344 public int appCpuUid2;
1345 public int appCpuUTime2;
1346 public int appCpuSTime2;
1347 public int appCpuUid3;
1348 public int appCpuUTime3;
1349 public int appCpuSTime3;
1350
1351 // Information from /proc/stat
1352 public int statUserTime;
1353 public int statSystemTime;
1354 public int statIOWaitTime;
1355 public int statIrqTime;
1356 public int statSoftIrqTime;
1357 public int statIdlTime;
1358
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001359 // Platform-level low power state stats
1360 public String statPlatformIdleState;
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001361 public String statSubsystemPowerState;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001362
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001363 public HistoryStepDetails() {
1364 clear();
1365 }
1366
1367 public void clear() {
1368 userTime = systemTime = 0;
1369 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1370 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1371 = appCpuUTime3 = appCpuSTime3 = 0;
1372 }
1373
1374 public void writeToParcel(Parcel out) {
1375 out.writeInt(userTime);
1376 out.writeInt(systemTime);
1377 out.writeInt(appCpuUid1);
1378 out.writeInt(appCpuUTime1);
1379 out.writeInt(appCpuSTime1);
1380 out.writeInt(appCpuUid2);
1381 out.writeInt(appCpuUTime2);
1382 out.writeInt(appCpuSTime2);
1383 out.writeInt(appCpuUid3);
1384 out.writeInt(appCpuUTime3);
1385 out.writeInt(appCpuSTime3);
1386 out.writeInt(statUserTime);
1387 out.writeInt(statSystemTime);
1388 out.writeInt(statIOWaitTime);
1389 out.writeInt(statIrqTime);
1390 out.writeInt(statSoftIrqTime);
1391 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001392 out.writeString(statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001393 out.writeString(statSubsystemPowerState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001394 }
1395
1396 public void readFromParcel(Parcel in) {
1397 userTime = in.readInt();
1398 systemTime = in.readInt();
1399 appCpuUid1 = in.readInt();
1400 appCpuUTime1 = in.readInt();
1401 appCpuSTime1 = in.readInt();
1402 appCpuUid2 = in.readInt();
1403 appCpuUTime2 = in.readInt();
1404 appCpuSTime2 = in.readInt();
1405 appCpuUid3 = in.readInt();
1406 appCpuUTime3 = in.readInt();
1407 appCpuSTime3 = in.readInt();
1408 statUserTime = in.readInt();
1409 statSystemTime = in.readInt();
1410 statIOWaitTime = in.readInt();
1411 statIrqTime = in.readInt();
1412 statSoftIrqTime = in.readInt();
1413 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001414 statPlatformIdleState = in.readString();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001415 statSubsystemPowerState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001416 }
1417 }
1418
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001419 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001420 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001421
1422 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001423 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001424
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001425 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001426 public static final byte CMD_NULL = -1;
1427 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001428 public static final byte CMD_CURRENT_TIME = 5;
1429 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001430 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001431 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001432
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001433 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001434
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001435 /**
1436 * Return whether the command code is a delta data update.
1437 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001438 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001439 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001440 }
1441
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001442 public byte batteryLevel;
1443 public byte batteryStatus;
1444 public byte batteryHealth;
1445 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001446
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001447 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001448 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001449
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001450 // The charge of the battery in micro-Ampere-hours.
1451 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001452
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001453 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001454 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001455 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001456 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001457 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1458 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001459 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001460 public static final int STATE_PHONE_STATE_SHIFT = 6;
1461 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001462 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001463 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001464 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001465
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001466 // These states always appear directly in the first int token
1467 // of a delta change; they should be ones that change relatively
1468 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001469 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1470 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001471 public static final int STATE_GPS_ON_FLAG = 1<<29;
1472 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001473 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001474 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001475 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001476 // Do not use, this is used for coulomb delta count.
1477 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001478 // These are on the lower bits used for the command; if they change
1479 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001480 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001481 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001482 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001483 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1484 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
Mike Mac2f518a2017-09-19 16:06:03 -07001485 public static final int STATE_SCREEN_DOZE_FLAG = 1 << 18;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001486 // empty slot
1487 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001488
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001489 public static final int MOST_INTERESTING_STATES =
Mike Mac2f518a2017-09-19 16:06:03 -07001490 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG | STATE_SCREEN_DOZE_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001491
1492 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001493
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001494 public int states;
1495
Dianne Hackborn3251b902014-06-20 14:40:53 -07001496 // Constants from WIFI_SUPPL_STATE_*
1497 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1498 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1499 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1500 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1501 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1502 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1503
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001504 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001505 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1506 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1507 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001508 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001509 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1510 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1511 public static final int STATE2_CHARGING_FLAG = 1<<24;
1512 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1513 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1514 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001515 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001516
1517 public static final int MOST_INTERESTING_STATES2 =
Mike Mac2f518a2017-09-19 16:06:03 -07001518 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
1519 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001520
1521 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001522
Dianne Hackborn40c87252014-03-19 16:55:40 -07001523 public int states2;
1524
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001525 // The wake lock that was acquired at this point.
1526 public HistoryTag wakelockTag;
1527
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001528 // Kernel wakeup reason at this point.
1529 public HistoryTag wakeReasonTag;
1530
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001531 // Non-null when there is more detailed information at this step.
1532 public HistoryStepDetails stepDetails;
1533
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001534 public static final int EVENT_FLAG_START = 0x8000;
1535 public static final int EVENT_FLAG_FINISH = 0x4000;
1536
1537 // No event in this item.
1538 public static final int EVENT_NONE = 0x0000;
1539 // Event is about a process that is running.
1540 public static final int EVENT_PROC = 0x0001;
1541 // Event is about an application package that is in the foreground.
1542 public static final int EVENT_FOREGROUND = 0x0002;
1543 // Event is about an application package that is at the top of the screen.
1544 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001545 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001546 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001547 // Events for all additional wake locks aquired/release within a wake block.
1548 // These are not generated by default.
1549 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001550 // Event is about an application executing a scheduled job.
1551 public static final int EVENT_JOB = 0x0006;
1552 // Events for users running.
1553 public static final int EVENT_USER_RUNNING = 0x0007;
1554 // Events for foreground user.
1555 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001556 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001557 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001558 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001559 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001560 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001561 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001562 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001563 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001564 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001565 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001566 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001567 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001568 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001569 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001570 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001571 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001572 // Event for a package being on the temporary whitelist.
1573 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001574 // Event for the screen waking up.
1575 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001576 // Event for the UID that woke up the application processor.
1577 // Used for wakeups coming from WiFi, modem, etc.
1578 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001579 // Event for reporting that a specific partial wake lock has been held for a long duration.
1580 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001581
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001582 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001583 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001584 // Mask to extract out only the type part of the event.
1585 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001586
1587 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1588 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1589 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1590 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1591 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1592 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001593 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1594 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001595 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1596 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001597 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1598 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1599 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1600 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1601 public static final int EVENT_USER_FOREGROUND_START =
1602 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1603 public static final int EVENT_USER_FOREGROUND_FINISH =
1604 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001605 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1606 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001607 public static final int EVENT_TEMP_WHITELIST_START =
1608 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1609 public static final int EVENT_TEMP_WHITELIST_FINISH =
1610 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001611 public static final int EVENT_LONG_WAKE_LOCK_START =
1612 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1613 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1614 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001615
1616 // For CMD_EVENT.
1617 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001618 public HistoryTag eventTag;
1619
Dianne Hackborn9a755432014-05-15 17:05:22 -07001620 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001621 public long currentTime;
1622
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001623 // Meta-data when reading.
1624 public int numReadInts;
1625
1626 // Pre-allocated objects.
1627 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001628 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001629 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001630
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001631 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001632 }
Bookatzc8c44962017-05-11 12:12:54 -07001633
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001634 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001635 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001636 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001637 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001638 }
Bookatzc8c44962017-05-11 12:12:54 -07001639
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001640 public int describeContents() {
1641 return 0;
1642 }
1643
1644 public void writeToParcel(Parcel dest, int flags) {
1645 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001646 int bat = (((int)cmd)&0xff)
1647 | ((((int)batteryLevel)<<8)&0xff00)
1648 | ((((int)batteryStatus)<<16)&0xf0000)
1649 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001650 | ((((int)batteryPlugType)<<24)&0xf000000)
1651 | (wakelockTag != null ? 0x10000000 : 0)
1652 | (wakeReasonTag != null ? 0x20000000 : 0)
1653 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001654 dest.writeInt(bat);
1655 bat = (((int)batteryTemperature)&0xffff)
1656 | ((((int)batteryVoltage)<<16)&0xffff0000);
1657 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001658 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001659 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001660 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001661 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001662 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001663 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001664 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001665 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001666 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001667 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001668 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001669 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001670 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001671 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001672 dest.writeLong(currentTime);
1673 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001674 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001675
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001676 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001677 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001678 int bat = src.readInt();
1679 cmd = (byte)(bat&0xff);
1680 batteryLevel = (byte)((bat>>8)&0xff);
1681 batteryStatus = (byte)((bat>>16)&0xf);
1682 batteryHealth = (byte)((bat>>20)&0xf);
1683 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001684 int bat2 = src.readInt();
1685 batteryTemperature = (short)(bat2&0xffff);
1686 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001687 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001688 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001689 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001690 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001691 wakelockTag = localWakelockTag;
1692 wakelockTag.readFromParcel(src);
1693 } else {
1694 wakelockTag = null;
1695 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001696 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001697 wakeReasonTag = localWakeReasonTag;
1698 wakeReasonTag.readFromParcel(src);
1699 } else {
1700 wakeReasonTag = null;
1701 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001702 if ((bat&0x40000000) != 0) {
1703 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001704 eventTag = localEventTag;
1705 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001706 } else {
1707 eventCode = EVENT_NONE;
1708 eventTag = null;
1709 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001710 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001711 currentTime = src.readLong();
1712 } else {
1713 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001714 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001715 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001716 }
1717
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001718 public void clear() {
1719 time = 0;
1720 cmd = CMD_NULL;
1721 batteryLevel = 0;
1722 batteryStatus = 0;
1723 batteryHealth = 0;
1724 batteryPlugType = 0;
1725 batteryTemperature = 0;
1726 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001727 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001728 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001729 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001730 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001731 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001732 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001733 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001734 }
Bookatzc8c44962017-05-11 12:12:54 -07001735
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001736 public void setTo(HistoryItem o) {
1737 time = o.time;
1738 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001739 setToCommon(o);
1740 }
1741
1742 public void setTo(long time, byte cmd, HistoryItem o) {
1743 this.time = time;
1744 this.cmd = cmd;
1745 setToCommon(o);
1746 }
1747
1748 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001749 batteryLevel = o.batteryLevel;
1750 batteryStatus = o.batteryStatus;
1751 batteryHealth = o.batteryHealth;
1752 batteryPlugType = o.batteryPlugType;
1753 batteryTemperature = o.batteryTemperature;
1754 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001755 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001756 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001757 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001758 if (o.wakelockTag != null) {
1759 wakelockTag = localWakelockTag;
1760 wakelockTag.setTo(o.wakelockTag);
1761 } else {
1762 wakelockTag = null;
1763 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001764 if (o.wakeReasonTag != null) {
1765 wakeReasonTag = localWakeReasonTag;
1766 wakeReasonTag.setTo(o.wakeReasonTag);
1767 } else {
1768 wakeReasonTag = null;
1769 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001770 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001771 if (o.eventTag != null) {
1772 eventTag = localEventTag;
1773 eventTag.setTo(o.eventTag);
1774 } else {
1775 eventTag = null;
1776 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001777 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001778 }
1779
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001780 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001781 return batteryLevel == o.batteryLevel
1782 && batteryStatus == o.batteryStatus
1783 && batteryHealth == o.batteryHealth
1784 && batteryPlugType == o.batteryPlugType
1785 && batteryTemperature == o.batteryTemperature
1786 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001787 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001788 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001789 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001790 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001791 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001792
1793 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001794 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001795 return false;
1796 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001797 if (wakelockTag != o.wakelockTag) {
1798 if (wakelockTag == null || o.wakelockTag == null) {
1799 return false;
1800 }
1801 if (!wakelockTag.equals(o.wakelockTag)) {
1802 return false;
1803 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001804 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001805 if (wakeReasonTag != o.wakeReasonTag) {
1806 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1807 return false;
1808 }
1809 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1810 return false;
1811 }
1812 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001813 if (eventTag != o.eventTag) {
1814 if (eventTag == null || o.eventTag == null) {
1815 return false;
1816 }
1817 if (!eventTag.equals(o.eventTag)) {
1818 return false;
1819 }
1820 }
1821 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001822 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001823 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001824
1825 public final static class HistoryEventTracker {
1826 private final HashMap<String, SparseIntArray>[] mActiveEvents
1827 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1828
1829 public boolean updateState(int code, String name, int uid, int poolIdx) {
1830 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1831 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1832 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1833 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001834 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001835 mActiveEvents[idx] = active;
1836 }
1837 SparseIntArray uids = active.get(name);
1838 if (uids == null) {
1839 uids = new SparseIntArray();
1840 active.put(name, uids);
1841 }
1842 if (uids.indexOfKey(uid) >= 0) {
1843 // Already set, nothing to do!
1844 return false;
1845 }
1846 uids.put(uid, poolIdx);
1847 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1848 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1849 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1850 if (active == null) {
1851 // not currently active, nothing to do.
1852 return false;
1853 }
1854 SparseIntArray uids = active.get(name);
1855 if (uids == null) {
1856 // not currently active, nothing to do.
1857 return false;
1858 }
1859 idx = uids.indexOfKey(uid);
1860 if (idx < 0) {
1861 // not currently active, nothing to do.
1862 return false;
1863 }
1864 uids.removeAt(idx);
1865 if (uids.size() <= 0) {
1866 active.remove(name);
1867 }
1868 }
1869 return true;
1870 }
1871
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001872 public void removeEvents(int code) {
1873 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1874 mActiveEvents[idx] = null;
1875 }
1876
Dianne Hackborn37de0982014-05-09 09:32:18 -07001877 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1878 return mActiveEvents[code];
1879 }
1880 }
1881
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001882 public static final class BitDescription {
1883 public final int mask;
1884 public final int shift;
1885 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001886 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001887 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001888 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07001889
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001890 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001891 this.mask = mask;
1892 this.shift = -1;
1893 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001894 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001895 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001896 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001897 }
Bookatzc8c44962017-05-11 12:12:54 -07001898
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001899 public BitDescription(int mask, int shift, String name, String shortName,
1900 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001901 this.mask = mask;
1902 this.shift = shift;
1903 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001904 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001905 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001906 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001907 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001908 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001909
Dianne Hackbornfc064132014-06-02 12:42:12 -07001910 /**
1911 * Don't allow any more batching in to the current history event. This
1912 * is called when printing partial histories, so to ensure that the next
1913 * history event will go in to a new batch after what was printed in the
1914 * last partial history.
1915 */
1916 public abstract void commitCurrentHistoryBatchLocked();
1917
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001918 public abstract int getHistoryTotalSize();
1919
1920 public abstract int getHistoryUsedSize();
1921
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001922 public abstract boolean startIteratingHistoryLocked();
1923
Dianne Hackborn099bc622014-01-22 13:39:16 -08001924 public abstract int getHistoryStringPoolSize();
1925
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001926 public abstract int getHistoryStringPoolBytes();
1927
1928 public abstract String getHistoryTagPoolString(int index);
1929
1930 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001931
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001932 public abstract boolean getNextHistoryLocked(HistoryItem out);
1933
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001934 public abstract void finishIteratingHistoryLocked();
1935
1936 public abstract boolean startIteratingOldHistoryLocked();
1937
1938 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1939
1940 public abstract void finishIteratingOldHistoryLocked();
1941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001943 * Return the base time offset for the battery history.
1944 */
1945 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07001946
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001947 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 * Returns the number of times the device has been started.
1949 */
1950 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07001951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001953 * 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 -08001954 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07001955 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 * {@hide}
1957 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001958 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07001959
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001960 /**
1961 * Returns the number of times the screen was turned on.
1962 *
1963 * {@hide}
1964 */
1965 public abstract int getScreenOnCount(int which);
1966
Mike Mac2f518a2017-09-19 16:06:03 -07001967 /**
1968 * Returns the time in microseconds that the screen has been dozing while the device was
1969 * running on battery.
1970 *
1971 * {@hide}
1972 */
1973 public abstract long getScreenDozeTime(long elapsedRealtimeUs, int which);
1974
1975 /**
1976 * Returns the number of times the screen was turned dozing.
1977 *
1978 * {@hide}
1979 */
1980 public abstract int getScreenDozeCount(int which);
1981
Jeff Browne95c3cd2014-05-02 16:59:26 -07001982 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1983
Dianne Hackborn617f8772009-03-31 15:04:46 -07001984 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1985 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1986 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1987 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1988 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07001989
Dianne Hackborn617f8772009-03-31 15:04:46 -07001990 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1991 "dark", "dim", "medium", "light", "bright"
1992 };
Bookatzc8c44962017-05-11 12:12:54 -07001993
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001994 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1995 "0", "1", "2", "3", "4"
1996 };
1997
Dianne Hackborn617f8772009-03-31 15:04:46 -07001998 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001999
Dianne Hackborn617f8772009-03-31 15:04:46 -07002000 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002001 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07002002 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07002003 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002004 * {@hide}
2005 */
2006 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002007 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002010 * Returns the {@link Timer} object that tracks the given screen brightness.
2011 *
2012 * {@hide}
2013 */
2014 public abstract Timer getScreenBrightnessTimer(int brightnessBin);
2015
2016 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002017 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002018 * running on battery.
2019 *
2020 * {@hide}
2021 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002022 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002023
2024 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002025 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002026 *
2027 * {@hide}
2028 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002029 public abstract int getPowerSaveModeEnabledCount(int which);
2030
2031 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002032 * Constant for device idle mode: not active.
2033 */
2034 public static final int DEVICE_IDLE_MODE_OFF = 0;
2035
2036 /**
2037 * Constant for device idle mode: active in lightweight mode.
2038 */
2039 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
2040
2041 /**
2042 * Constant for device idle mode: active in full mode.
2043 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002044 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002045
2046 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002047 * Returns the time in microseconds that device has been in idle mode while
2048 * running on battery.
2049 *
2050 * {@hide}
2051 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002052 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002053
2054 /**
2055 * Returns the number of times that the devie has gone in to idle mode.
2056 *
2057 * {@hide}
2058 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002059 public abstract int getDeviceIdleModeCount(int mode, int which);
2060
2061 /**
2062 * Return the longest duration we spent in a particular device idle mode (fully in the
2063 * mode, not in idle maintenance etc).
2064 */
2065 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002066
2067 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002068 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002069 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002070 * counts all of the time that we consider the device to be idle, whether or not
2071 * it is currently in the actual device idle mode.
2072 *
2073 * {@hide}
2074 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002075 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002076
2077 /**
Bookatz8c6571b2017-10-24 15:04:41 -07002078 * Returns the number of times that the device has started idling.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002079 *
2080 * {@hide}
2081 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002082 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002083
2084 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002085 * Returns the number of times that connectivity state changed.
2086 *
2087 * {@hide}
2088 */
2089 public abstract int getNumConnectivityChange(int which);
2090
2091 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002092 * 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 -08002093 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002094 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 * {@hide}
2096 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002097 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002098
Dianne Hackborn627bba72009-03-24 22:32:56 -07002099 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002100 * Returns the number of times a phone call was activated.
2101 *
2102 * {@hide}
2103 */
2104 public abstract int getPhoneOnCount(int which);
2105
2106 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002107 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002108 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002109 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002110 * {@hide}
2111 */
2112 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002113 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002114
Dianne Hackborn617f8772009-03-31 15:04:46 -07002115 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07002116 * Returns the time in microseconds that the phone has been trying to
2117 * acquire a signal.
2118 *
2119 * {@hide}
2120 */
2121 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002122 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002123
2124 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002125 * Returns the {@link Timer} object that tracks how much the phone has been trying to
2126 * acquire a signal.
2127 *
2128 * {@hide}
2129 */
2130 public abstract Timer getPhoneSignalScanningTimer();
2131
2132 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002133 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002134 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002135 * {@hide}
2136 */
2137 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
2138
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002139 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002140 * Return the {@link Timer} object used to track the given signal strength's duration and
2141 * counts.
2142 */
2143 protected abstract Timer getPhoneSignalStrengthTimer(int strengthBin);
2144
2145 /**
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002146 * Returns the time in microseconds that the mobile network has been active
2147 * (in a high power state).
2148 *
2149 * {@hide}
2150 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002151 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002152
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002153 /**
2154 * Returns the number of times that the mobile network has transitioned to the
2155 * active state.
2156 *
2157 * {@hide}
2158 */
2159 public abstract int getMobileRadioActiveCount(int which);
2160
2161 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002162 * Returns the time in microseconds that is the difference between the mobile radio
2163 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
2164 * from the radio.
2165 *
2166 * {@hide}
2167 */
2168 public abstract long getMobileRadioActiveAdjustedTime(int which);
2169
2170 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002171 * Returns the time in microseconds that the mobile network has been active
2172 * (in a high power state) but not being able to blame on an app.
2173 *
2174 * {@hide}
2175 */
2176 public abstract long getMobileRadioActiveUnknownTime(int which);
2177
2178 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002179 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002180 *
2181 * {@hide}
2182 */
2183 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002184
Dianne Hackborn627bba72009-03-24 22:32:56 -07002185 public static final int DATA_CONNECTION_NONE = 0;
2186 public static final int DATA_CONNECTION_GPRS = 1;
2187 public static final int DATA_CONNECTION_EDGE = 2;
2188 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002189 public static final int DATA_CONNECTION_CDMA = 4;
2190 public static final int DATA_CONNECTION_EVDO_0 = 5;
2191 public static final int DATA_CONNECTION_EVDO_A = 6;
2192 public static final int DATA_CONNECTION_1xRTT = 7;
2193 public static final int DATA_CONNECTION_HSDPA = 8;
2194 public static final int DATA_CONNECTION_HSUPA = 9;
2195 public static final int DATA_CONNECTION_HSPA = 10;
2196 public static final int DATA_CONNECTION_IDEN = 11;
2197 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002198 public static final int DATA_CONNECTION_LTE = 13;
2199 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08002200 public static final int DATA_CONNECTION_HSPAP = 15;
2201 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002202
Dianne Hackborn627bba72009-03-24 22:32:56 -07002203 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002204 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002205 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08002206 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002207 };
Bookatzc8c44962017-05-11 12:12:54 -07002208
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002209 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002210
Dianne Hackborn627bba72009-03-24 22:32:56 -07002211 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002212 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002213 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002214 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002215 * {@hide}
2216 */
2217 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002218 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002221 * Returns the number of times the phone has entered the given data
2222 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002223 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002224 * {@hide}
2225 */
2226 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002227
Kweku Adams87b19ec2017-10-09 12:40:03 -07002228 /**
2229 * Returns the {@link Timer} object that tracks the phone's data connection type stats.
2230 */
2231 public abstract Timer getPhoneDataConnectionTimer(int dataType);
2232
Dianne Hackborn3251b902014-06-20 14:40:53 -07002233 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2234 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2235 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2236 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2237 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2238 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2239 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2240 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2241 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2242 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2243 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2244 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2245 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2246
2247 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2248
2249 static final String[] WIFI_SUPPL_STATE_NAMES = {
2250 "invalid", "disconn", "disabled", "inactive", "scanning",
2251 "authenticating", "associating", "associated", "4-way-handshake",
2252 "group-handshake", "completed", "dormant", "uninit"
2253 };
2254
2255 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2256 "inv", "dsc", "dis", "inact", "scan",
2257 "auth", "ascing", "asced", "4-way",
2258 "group", "compl", "dorm", "uninit"
2259 };
2260
Mike Mac2f518a2017-09-19 16:06:03 -07002261 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002262 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002263 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2264 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002265 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002266 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2267 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2268 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002269 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002270 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002271 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002272 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002273 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2274 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Mike Mac2f518a2017-09-19 16:06:03 -07002275 new BitDescription(HistoryItem.STATE_SCREEN_DOZE_FLAG, "screen_doze", "Sd"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002276 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2277 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2278 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2279 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2280 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2281 new String[] {"in", "out", "emergency", "off"},
2282 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002283 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2284 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2285 SignalStrength.SIGNAL_STRENGTH_NAMES,
2286 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002287 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2288 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2289 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002290 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002291
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002292 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2293 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002294 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002295 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002296 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002297 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002298 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002299 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2300 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2301 new String[] { "off", "light", "full", "???" },
2302 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002303 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2304 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2305 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002306 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2307 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2308 new String[] { "0", "1", "2", "3", "4" },
2309 new String[] { "0", "1", "2", "3", "4" }),
2310 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2311 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2312 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002313 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002314 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002315 };
2316
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002317 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002318 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002319 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2320 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002321 };
2322
2323 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002324 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002325 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002326 "Esw", "Ewa", "Elw", "Eec"
2327 };
2328
2329 @FunctionalInterface
2330 public interface IntToString {
2331 String applyAsString(int val);
2332 }
2333
2334 private static final IntToString sUidToString = UserHandle::formatUid;
2335 private static final IntToString sIntToString = Integer::toString;
2336
2337 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2338 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2339 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2340 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2341 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002342 };
2343
Dianne Hackborn617f8772009-03-31 15:04:46 -07002344 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08002345 * Returns total time for WiFi Multicast Wakelock timer.
2346 * Note that this may be different from the sum of per uid timer values.
2347 *
2348 * {@hide}
2349 */
2350 public abstract long getWifiMulticastWakelockTime(long elapsedRealtimeUs, int which);
2351
2352 /**
2353 * 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 int getWifiMulticastWakelockCount(int which);
2359
2360 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002361 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002362 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002363 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002364 * {@hide}
2365 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002366 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002367
2368 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002369 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002370 * been in the running state while the device was running on battery.
2371 *
2372 * {@hide}
2373 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002374 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002375
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002376 public static final int WIFI_STATE_OFF = 0;
2377 public static final int WIFI_STATE_OFF_SCANNING = 1;
2378 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2379 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2380 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2381 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2382 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2383 public static final int WIFI_STATE_SOFT_AP = 7;
2384
2385 static final String[] WIFI_STATE_NAMES = {
2386 "off", "scanning", "no_net", "disconn",
2387 "sta", "p2p", "sta_p2p", "soft_ap"
2388 };
2389
2390 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2391
2392 /**
2393 * Returns the time in microseconds that WiFi has been running in the given state.
2394 *
2395 * {@hide}
2396 */
2397 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002398 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002399
2400 /**
2401 * Returns the number of times that WiFi has entered the given state.
2402 *
2403 * {@hide}
2404 */
2405 public abstract int getWifiStateCount(int wifiState, int which);
2406
The Android Open Source Project10592532009-03-18 17:39:46 -07002407 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002408 * Returns the {@link Timer} object that tracks the given WiFi state.
2409 *
2410 * {@hide}
2411 */
2412 public abstract Timer getWifiStateTimer(int wifiState);
2413
2414 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002415 * Returns the time in microseconds that the wifi supplicant has been
2416 * in a given state.
2417 *
2418 * {@hide}
2419 */
2420 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2421
2422 /**
2423 * Returns the number of times that the wifi supplicant has transitioned
2424 * to a given state.
2425 *
2426 * {@hide}
2427 */
2428 public abstract int getWifiSupplStateCount(int state, int which);
2429
Kweku Adams87b19ec2017-10-09 12:40:03 -07002430 /**
2431 * Returns the {@link Timer} object that tracks the given wifi supplicant state.
2432 *
2433 * {@hide}
2434 */
2435 public abstract Timer getWifiSupplStateTimer(int state);
2436
Dianne Hackborn3251b902014-06-20 14:40:53 -07002437 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2438
2439 /**
2440 * Returns the time in microseconds that WIFI has been running with
2441 * the given signal strength.
2442 *
2443 * {@hide}
2444 */
2445 public abstract long getWifiSignalStrengthTime(int strengthBin,
2446 long elapsedRealtimeUs, int which);
2447
2448 /**
2449 * Returns the number of times WIFI has entered the given signal strength.
2450 *
2451 * {@hide}
2452 */
2453 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2454
2455 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002456 * Returns the {@link Timer} object that tracks the given WIFI signal strength.
2457 *
2458 * {@hide}
2459 */
2460 public abstract Timer getWifiSignalStrengthTimer(int strengthBin);
2461
2462 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002463 * Returns the time in microseconds that the flashlight has been on while the device was
2464 * running on battery.
2465 *
2466 * {@hide}
2467 */
2468 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2469
2470 /**
2471 * Returns the number of times that the flashlight has been turned on while the device was
2472 * running on battery.
2473 *
2474 * {@hide}
2475 */
2476 public abstract long getFlashlightOnCount(int which);
2477
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002478 /**
2479 * Returns the time in microseconds that the camera has been on while the device was
2480 * running on battery.
2481 *
2482 * {@hide}
2483 */
2484 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2485
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002486 /**
2487 * Returns the time in microseconds that bluetooth scans were running while the device was
2488 * on battery.
2489 *
2490 * {@hide}
2491 */
2492 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002493
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002494 public static final int NETWORK_MOBILE_RX_DATA = 0;
2495 public static final int NETWORK_MOBILE_TX_DATA = 1;
2496 public static final int NETWORK_WIFI_RX_DATA = 2;
2497 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002498 public static final int NETWORK_BT_RX_DATA = 4;
2499 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002500 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2501 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2502 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2503 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2504 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002505
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002506 public abstract long getNetworkActivityBytes(int type, int which);
2507 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002508
Adam Lesinskie08af192015-03-25 16:42:59 -07002509 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002510 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002511 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002512 * actual power data.
2513 */
2514 public abstract boolean hasWifiActivityReporting();
2515
2516 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002517 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2518 * in various radio controller states, such as transmit, receive, and idle.
2519 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002520 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002521 public abstract ControllerActivityCounter getWifiControllerActivity();
2522
2523 /**
2524 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2525 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2526 * actual power data.
2527 */
2528 public abstract boolean hasBluetoothActivityReporting();
2529
2530 /**
2531 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2532 * in various radio controller states, such as transmit, receive, and idle.
2533 * @return non-null {@link ControllerActivityCounter}
2534 */
2535 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2536
2537 /**
2538 * Returns true if the BatteryStats object has detailed modem power reports.
2539 * When true, calling {@link #getModemControllerActivity()} will yield the
2540 * actual power data.
2541 */
2542 public abstract boolean hasModemActivityReporting();
2543
2544 /**
2545 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2546 * in various radio controller states, such as transmit, receive, and idle.
2547 * @return non-null {@link ControllerActivityCounter}
2548 */
2549 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002550
The Android Open Source Project10592532009-03-18 17:39:46 -07002551 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002552 * Return the wall clock time when battery stats data collection started.
2553 */
2554 public abstract long getStartClockTime();
2555
2556 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002557 * Return platform version tag that we were running in when the battery stats started.
2558 */
2559 public abstract String getStartPlatformVersion();
2560
2561 /**
2562 * Return platform version tag that we were running in when the battery stats ended.
2563 */
2564 public abstract String getEndPlatformVersion();
2565
2566 /**
2567 * Return the internal version code of the parcelled format.
2568 */
2569 public abstract int getParcelVersion();
2570
2571 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 * Return whether we are currently running on battery.
2573 */
2574 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 /**
2577 * Returns a SparseArray containing the statistics for each uid.
2578 */
2579 public abstract SparseArray<? extends Uid> getUidStats();
2580
2581 /**
2582 * Returns the current battery uptime in microseconds.
2583 *
2584 * @param curTime the amount of elapsed realtime in microseconds.
2585 */
2586 public abstract long getBatteryUptime(long curTime);
2587
2588 /**
2589 * Returns the current battery realtime in microseconds.
2590 *
2591 * @param curTime the amount of elapsed realtime in microseconds.
2592 */
2593 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002594
The Android Open Source Project10592532009-03-18 17:39:46 -07002595 /**
Evan Millar633a1742009-04-02 16:36:33 -07002596 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002597 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002598 */
Evan Millar633a1742009-04-02 16:36:33 -07002599 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002600
The Android Open Source Project10592532009-03-18 17:39:46 -07002601 /**
Evan Millar633a1742009-04-02 16:36:33 -07002602 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2603 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002604 */
Evan Millar633a1742009-04-02 16:36:33 -07002605 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002606
2607 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002608 * Get the amount the battery has discharged since the stats were
2609 * last reset after charging, as a lower-end approximation.
2610 */
2611 public abstract int getLowDischargeAmountSinceCharge();
2612
2613 /**
2614 * Get the amount the battery has discharged since the stats were
2615 * last reset after charging, as an upper-end approximation.
2616 */
2617 public abstract int getHighDischargeAmountSinceCharge();
2618
2619 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002620 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2621 */
2622 public abstract int getDischargeAmount(int which);
2623
2624 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002625 * Get the amount the battery has discharged while the screen was on,
2626 * since the last time power was unplugged.
2627 */
2628 public abstract int getDischargeAmountScreenOn();
2629
2630 /**
2631 * Get the amount the battery has discharged while the screen was on,
2632 * since the last time the device was charged.
2633 */
2634 public abstract int getDischargeAmountScreenOnSinceCharge();
2635
2636 /**
2637 * Get the amount the battery has discharged while the screen was off,
2638 * since the last time power was unplugged.
2639 */
2640 public abstract int getDischargeAmountScreenOff();
2641
2642 /**
2643 * Get the amount the battery has discharged while the screen was off,
2644 * since the last time the device was charged.
2645 */
2646 public abstract int getDischargeAmountScreenOffSinceCharge();
2647
2648 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002649 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002650 * since the last time power was unplugged.
2651 */
2652 public abstract int getDischargeAmountScreenDoze();
2653
2654 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002655 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002656 * since the last time the device was charged.
2657 */
2658 public abstract int getDischargeAmountScreenDozeSinceCharge();
2659
2660 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 * Returns the total, last, or current battery uptime in microseconds.
2662 *
2663 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002664 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 */
2666 public abstract long computeBatteryUptime(long curTime, int which);
2667
2668 /**
2669 * Returns the total, last, or current battery realtime in microseconds.
2670 *
2671 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002672 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 */
2674 public abstract long computeBatteryRealtime(long curTime, int which);
2675
2676 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002677 * Returns the total, last, or current battery screen off/doze uptime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002678 *
2679 * @param curTime the 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.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002681 */
2682 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2683
2684 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002685 * Returns the total, last, or current battery screen off/doze realtime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002686 *
2687 * @param curTime the current 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 computeBatteryScreenOffRealtime(long curTime, int which);
2691
2692 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 * Returns the total, last, or current uptime in microseconds.
2694 *
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.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 */
2698 public abstract long computeUptime(long curTime, int which);
2699
2700 /**
2701 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002702 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 * @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 computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002707
2708 /**
2709 * Compute an approximation for how much run time (in microseconds) is remaining on
2710 * the battery. Returns -1 if no time can be computed: either there is not
2711 * enough current data to make a decision, or the battery is currently
2712 * charging.
2713 *
2714 * @param curTime The current elepsed realtime in microseconds.
2715 */
2716 public abstract long computeBatteryTimeRemaining(long curTime);
2717
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002718 // The part of a step duration that is the actual time.
2719 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2720
2721 // Bits in a step duration that are the new battery level we are at.
2722 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002723 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002724
2725 // Bits in a step duration that are the initial mode we were in at that step.
2726 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002727 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002728
2729 // Bits in a step duration that indicate which modes changed during that step.
2730 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002731 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002732
2733 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2734 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2735
Santos Cordone94f0502017-02-24 12:31:20 -08002736 // The largest value for screen state that is tracked in battery states. Any values above
2737 // this should be mapped back to one of the tracked values before being tracked here.
2738 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2739
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002740 // Step duration mode: power save is on.
2741 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2742
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002743 // Step duration mode: device is currently in idle mode.
2744 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2745
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002746 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2747 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002748 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2749 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002750 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2751 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2752 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2753 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2754 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002755 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2756 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002757 };
2758 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2759 (Display.STATE_OFF-1),
2760 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002761 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002762 (Display.STATE_ON-1),
2763 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2764 (Display.STATE_DOZE-1),
2765 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2766 (Display.STATE_DOZE_SUSPEND-1),
2767 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002768 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002769 };
2770 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2771 "screen off",
2772 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002773 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002774 "screen on",
2775 "screen on power save",
2776 "screen doze",
2777 "screen doze power save",
2778 "screen doze-suspend",
2779 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002780 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002781 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002782
2783 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002784 * Return the amount of battery discharge while the screen was off, measured in
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002785 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2786 * a coulomb counter.
2787 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002788 public abstract long getUahDischargeScreenOff(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002789
2790 /**
2791 * Return the amount of battery discharge while the screen was in doze mode, measured in
2792 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2793 * a coulomb counter.
2794 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002795 public abstract long getUahDischargeScreenDoze(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002796
2797 /**
2798 * Return the amount of battery discharge measured in micro-Ampere-hours. This will be
2799 * non-zero only if the device's battery has a coulomb counter.
2800 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002801 public abstract long getUahDischarge(int which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002802
2803 /**
Mike Ma15313c92017-11-15 17:58:21 -08002804 * @return the amount of battery discharge while the device is in light idle mode, measured in
2805 * micro-Ampere-hours.
2806 */
2807 public abstract long getUahDischargeLightDoze(int which);
2808
2809 /**
2810 * @return the amount of battery discharge while the device is in deep idle mode, measured in
2811 * micro-Ampere-hours.
2812 */
2813 public abstract long getUahDischargeDeepDoze(int which);
2814
2815 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002816 * Returns the estimated real battery capacity, which may be less than the capacity
2817 * declared by the PowerProfile.
2818 * @return The estimated battery capacity in mAh.
2819 */
2820 public abstract int getEstimatedBatteryCapacity();
2821
2822 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002823 * @return The minimum learned battery capacity in uAh.
2824 */
2825 public abstract int getMinLearnedBatteryCapacity();
2826
2827 /**
2828 * @return The maximum learned battery capacity in uAh.
2829 */
2830 public abstract int getMaxLearnedBatteryCapacity() ;
2831
2832 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002833 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002834 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002835 public abstract LevelStepTracker getDischargeLevelStepTracker();
2836
2837 /**
2838 * Return the array of daily discharge step durations.
2839 */
2840 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002841
2842 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002843 * Compute an approximation for how much time (in microseconds) remains until the battery
2844 * is fully charged. Returns -1 if no time can be computed: either there is not
2845 * enough current data to make a decision, or the battery is currently
2846 * discharging.
2847 *
2848 * @param curTime The current elepsed realtime in microseconds.
2849 */
2850 public abstract long computeChargeTimeRemaining(long curTime);
2851
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002852 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002853 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002854 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002855 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002856
2857 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002858 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002859 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002860 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002861
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002862 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2863
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002864 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002865
Evan Millarc64edde2009-04-18 12:26:32 -07002866 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867
Bookatz50df7112017-08-04 14:53:26 -07002868 /**
2869 * Returns Timers tracking the total time of each Resource Power Manager state and voter.
2870 */
2871 public abstract Map<String, ? extends Timer> getRpmStats();
2872 /**
2873 * Returns Timers tracking the screen-off time of each Resource Power Manager state and voter.
2874 */
2875 public abstract Map<String, ? extends Timer> getScreenOffRpmStats();
2876
2877
James Carr2dd7e5e2016-07-20 18:48:39 -07002878 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2879
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002880 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2881
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002882 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 long days = seconds / (60 * 60 * 24);
2884 if (days != 0) {
2885 out.append(days);
2886 out.append("d ");
2887 }
2888 long used = days * 60 * 60 * 24;
2889
2890 long hours = (seconds - used) / (60 * 60);
2891 if (hours != 0 || used != 0) {
2892 out.append(hours);
2893 out.append("h ");
2894 }
2895 used += hours * 60 * 60;
2896
2897 long mins = (seconds-used) / 60;
2898 if (mins != 0 || used != 0) {
2899 out.append(mins);
2900 out.append("m ");
2901 }
2902 used += mins * 60;
2903
2904 if (seconds != 0 || used != 0) {
2905 out.append(seconds-used);
2906 out.append("s ");
2907 }
2908 }
2909
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002910 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002912 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 sb.append(time - (sec * 1000));
2914 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 }
2916
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002917 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002918 long sec = time / 1000;
2919 formatTimeRaw(sb, sec);
2920 sb.append(time - (sec * 1000));
2921 sb.append("ms");
2922 }
2923
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002924 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002926 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 }
2928 float perc = ((float)num) / ((float)den) * 100;
2929 mFormatBuilder.setLength(0);
2930 mFormatter.format("%.1f%%", perc);
2931 return mFormatBuilder.toString();
2932 }
2933
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002934 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002935 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07002936
Evan Millar22ac0432009-03-31 11:33:18 -07002937 if (bytes < BYTES_PER_KB) {
2938 return bytes + "B";
2939 } else if (bytes < BYTES_PER_MB) {
2940 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2941 return mFormatBuilder.toString();
2942 } else if (bytes < BYTES_PER_GB){
2943 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2944 return mFormatBuilder.toString();
2945 } else {
2946 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2947 return mFormatBuilder.toString();
2948 }
2949 }
2950
Kweku Adams103351f2017-10-16 14:39:34 -07002951 private static long roundUsToMs(long timeUs) {
2952 return (timeUs + 500) / 1000;
2953 }
2954
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002955 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002956 if (timer != null) {
2957 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002958 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002959 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2960 return totalTimeMillis;
2961 }
2962 return 0;
2963 }
2964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 /**
2966 *
2967 * @param sb a StringBuilder object.
2968 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002969 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002971 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 * @param linePrefix a String to be prepended to each line of output.
2973 * @return the line prefix
2974 */
2975 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002976 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002979 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002980
Evan Millarc64edde2009-04-18 12:26:32 -07002981 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 if (totalTimeMillis != 0) {
2983 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002984 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002985 if (name != null) {
2986 sb.append(name);
2987 sb.append(' ');
2988 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989 sb.append('(');
2990 sb.append(count);
2991 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002992 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2993 if (maxDurationMs >= 0) {
2994 sb.append(" max=");
2995 sb.append(maxDurationMs);
2996 }
Bookatz506a8182017-05-01 14:18:42 -07002997 // Put actual time if it is available and different from totalTimeMillis.
2998 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
2999 if (totalDurMs > totalTimeMillis) {
3000 sb.append(" actual=");
3001 sb.append(totalDurMs);
3002 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07003003 if (timer.isRunningLocked()) {
3004 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3005 if (currentMs >= 0) {
3006 sb.append(" (running for ");
3007 sb.append(currentMs);
3008 sb.append("ms)");
3009 } else {
3010 sb.append(" (running)");
3011 }
3012 }
3013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 return ", ";
3015 }
3016 }
3017 return linePrefix;
3018 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003019
3020 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07003021 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003022 *
3023 * @param pw a PrintWriter object to print to.
3024 * @param sb a StringBuilder object.
3025 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08003026 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003027 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
3028 * @param prefix a String to be prepended to each line of output.
3029 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07003030 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003031 */
3032 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07003033 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003034 if (timer != null) {
3035 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07003036 final long totalTimeMs = (timer.getTotalTimeLocked(
3037 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003038 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003039 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003040 sb.setLength(0);
3041 sb.append(prefix);
3042 sb.append(" ");
3043 sb.append(type);
3044 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003045 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003046 sb.append("realtime (");
3047 sb.append(count);
3048 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003049 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
3050 if (maxDurationMs >= 0) {
3051 sb.append(" max=");
3052 sb.append(maxDurationMs);
3053 }
3054 if (timer.isRunningLocked()) {
3055 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
3056 if (currentMs >= 0) {
3057 sb.append(" (running for ");
3058 sb.append(currentMs);
3059 sb.append("ms)");
3060 } else {
3061 sb.append(" (running)");
3062 }
3063 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003064 pw.println(sb.toString());
3065 return true;
3066 }
3067 }
3068 return false;
3069 }
Bookatzc8c44962017-05-11 12:12:54 -07003070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 /**
3072 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07003073 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074 * @param sb a StringBuilder object.
3075 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003076 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003078 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 * @param linePrefix a String to be prepended to each line of output.
3080 * @return the line prefix
3081 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003082 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
3083 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084 long totalTimeMicros = 0;
3085 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07003086 long max = 0;
3087 long current = 0;
3088 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003090 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07003091 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003092 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3093 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07003094 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003095 }
3096 sb.append(linePrefix);
3097 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
3098 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07003099 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003101 sb.append(',');
3102 sb.append(current);
3103 sb.append(',');
3104 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07003105 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
3106 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
3107 // totalDuration independent of totalTimeMicros (since they are not pooled).
3108 if (name != null) {
3109 sb.append(',');
3110 sb.append(totalDuration);
3111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 return ",";
3113 }
Bookatz506a8182017-05-01 14:18:42 -07003114
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003115 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
3116 String type) {
3117 pw.print(BATTERY_STATS_CHECKIN_VERSION);
3118 pw.print(',');
3119 pw.print(uid);
3120 pw.print(',');
3121 pw.print(category);
3122 pw.print(',');
3123 pw.print(type);
3124 }
3125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 /**
3127 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07003128 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003129 * @param pw the PageWriter to dump log to
3130 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3131 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3132 * @param args type-dependent data arguments
3133 */
Bookatzc8c44962017-05-11 12:12:54 -07003134 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003136 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003137 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003138 pw.print(',');
3139 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003140 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003141 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07003143
3144 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003145 * Dump a given timer stat for terse checkin mode.
3146 *
3147 * @param pw the PageWriter to dump log to
3148 * @param uid the UID to log
3149 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3150 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3151 * @param timer a {@link Timer} to dump stats for
3152 * @param rawRealtime the current elapsed realtime of the system in microseconds
3153 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3154 */
3155 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
3156 Timer timer, long rawRealtime, int which) {
3157 if (timer != null) {
3158 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003159 final long totalTime = roundUsToMs(timer.getTotalTimeLocked(rawRealtime, which));
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003160 final int count = timer.getCountLocked(which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003161 if (totalTime != 0 || count != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003162 dumpLine(pw, uid, category, type, totalTime, count);
3163 }
3164 }
3165 }
3166
3167 /**
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003168 * Dump a given timer stat to the proto stream.
3169 *
3170 * @param proto the ProtoOutputStream to log to
3171 * @param fieldId type of data, the field to save to (e.g. AggregatedBatteryStats.WAKELOCK)
3172 * @param timer a {@link Timer} to dump stats for
3173 * @param rawRealtimeUs the current elapsed realtime of the system in microseconds
3174 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3175 */
3176 private static void dumpTimer(ProtoOutputStream proto, long fieldId,
Kweku Adams87b19ec2017-10-09 12:40:03 -07003177 Timer timer, long rawRealtimeUs, int which) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003178 if (timer == null) {
3179 return;
3180 }
3181 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003182 final long timeMs = roundUsToMs(timer.getTotalTimeLocked(rawRealtimeUs, which));
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003183 final int count = timer.getCountLocked(which);
Kweku Adams103351f2017-10-16 14:39:34 -07003184 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs / 1000);
3185 final long curDurationMs = timer.getCurrentDurationMsLocked(rawRealtimeUs / 1000);
3186 final long totalDurationMs = timer.getTotalDurationMsLocked(rawRealtimeUs / 1000);
3187 if (timeMs != 0 || count != 0 || maxDurationMs != -1 || curDurationMs != -1
3188 || totalDurationMs != -1) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003189 final long token = proto.start(fieldId);
Kweku Adams103351f2017-10-16 14:39:34 -07003190 proto.write(TimerProto.DURATION_MS, timeMs);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003191 proto.write(TimerProto.COUNT, count);
Kweku Adams103351f2017-10-16 14:39:34 -07003192 // These values will be -1 for timers that don't implement the functionality.
3193 if (maxDurationMs != -1) {
3194 proto.write(TimerProto.MAX_DURATION_MS, maxDurationMs);
3195 }
3196 if (curDurationMs != -1) {
3197 proto.write(TimerProto.CURRENT_DURATION_MS, curDurationMs);
3198 }
3199 if (totalDurationMs != -1) {
3200 proto.write(TimerProto.TOTAL_DURATION_MS, totalDurationMs);
3201 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003202 proto.end(token);
3203 }
3204 }
3205
3206 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003207 * Checks if the ControllerActivityCounter has any data worth dumping.
3208 */
3209 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
3210 if (counter == null) {
3211 return false;
3212 }
3213
3214 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
3215 || counter.getRxTimeCounter().getCountLocked(which) != 0
3216 || counter.getPowerCounter().getCountLocked(which) != 0) {
3217 return true;
3218 }
3219
3220 for (LongCounter c : counter.getTxTimeCounters()) {
3221 if (c.getCountLocked(which) != 0) {
3222 return true;
3223 }
3224 }
3225 return false;
3226 }
3227
3228 /**
3229 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3230 * The order of the arguments in the final check in line is:
3231 *
3232 * idle, rx, power, tx...
3233 *
3234 * where tx... is one or more transmit level times.
3235 */
3236 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
3237 String type,
3238 ControllerActivityCounter counter,
3239 int which) {
3240 if (!controllerActivityHasData(counter, which)) {
3241 return;
3242 }
3243
3244 dumpLineHeader(pw, uid, category, type);
3245 pw.print(",");
3246 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
3247 pw.print(",");
3248 pw.print(counter.getRxTimeCounter().getCountLocked(which));
3249 pw.print(",");
3250 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3251 for (LongCounter c : counter.getTxTimeCounters()) {
3252 pw.print(",");
3253 pw.print(c.getCountLocked(which));
3254 }
3255 pw.println();
3256 }
3257
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003258 /**
3259 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3260 */
3261 private static void dumpControllerActivityProto(ProtoOutputStream proto, long fieldId,
3262 ControllerActivityCounter counter,
3263 int which) {
3264 if (!controllerActivityHasData(counter, which)) {
3265 return;
3266 }
3267
3268 final long cToken = proto.start(fieldId);
3269
3270 proto.write(ControllerActivityProto.IDLE_DURATION_MS,
3271 counter.getIdleTimeCounter().getCountLocked(which));
3272 proto.write(ControllerActivityProto.RX_DURATION_MS,
3273 counter.getRxTimeCounter().getCountLocked(which));
3274 proto.write(ControllerActivityProto.POWER_MAH,
3275 counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3276
3277 long tToken;
3278 LongCounter[] txCounters = counter.getTxTimeCounters();
3279 for (int i = 0; i < txCounters.length; ++i) {
3280 LongCounter c = txCounters[i];
3281 tToken = proto.start(ControllerActivityProto.TX);
3282 proto.write(ControllerActivityProto.TxLevel.LEVEL, i);
3283 proto.write(ControllerActivityProto.TxLevel.DURATION_MS, c.getCountLocked(which));
3284 proto.end(tToken);
3285 }
3286
3287 proto.end(cToken);
3288 }
3289
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003290 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
3291 String prefix, String controllerName,
3292 ControllerActivityCounter counter,
3293 int which) {
3294 if (controllerActivityHasData(counter, which)) {
3295 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
3296 }
3297 }
3298
3299 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
3300 String controllerName,
3301 ControllerActivityCounter counter, int which) {
3302 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
3303 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
3304 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003305 // Battery real time
3306 final long totalControllerActivityTimeMs
3307 = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003308 long totalTxTimeMs = 0;
3309 for (LongCounter txState : counter.getTxTimeCounters()) {
3310 totalTxTimeMs += txState.getCountLocked(which);
3311 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003312 final long sleepTimeMs
3313 = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + totalTxTimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003314
3315 sb.setLength(0);
3316 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003317 sb.append(" ");
3318 sb.append(controllerName);
3319 sb.append(" Sleep time: ");
3320 formatTimeMs(sb, sleepTimeMs);
3321 sb.append("(");
3322 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3323 sb.append(")");
3324 pw.println(sb.toString());
3325
3326 sb.setLength(0);
3327 sb.append(prefix);
3328 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003329 sb.append(controllerName);
3330 sb.append(" Idle time: ");
3331 formatTimeMs(sb, idleTimeMs);
3332 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003333 sb.append(formatRatioLocked(idleTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003334 sb.append(")");
3335 pw.println(sb.toString());
3336
3337 sb.setLength(0);
3338 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003339 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003340 sb.append(controllerName);
3341 sb.append(" Rx time: ");
3342 formatTimeMs(sb, rxTimeMs);
3343 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003344 sb.append(formatRatioLocked(rxTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003345 sb.append(")");
3346 pw.println(sb.toString());
3347
3348 sb.setLength(0);
3349 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003350 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003351 sb.append(controllerName);
3352 sb.append(" Tx time: ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003353
Siddharth Ray3c648c42017-10-02 17:30:58 -07003354 String [] powerLevel;
3355 switch(controllerName) {
3356 case "Cellular":
3357 powerLevel = new String[] {
3358 " less than 0dBm: ",
3359 " 0dBm to 8dBm: ",
3360 " 8dBm to 15dBm: ",
3361 " 15dBm to 20dBm: ",
3362 " above 20dBm: "};
3363 break;
3364 default:
3365 powerLevel = new String[] {"[0]", "[1]", "[2]", "[3]", "[4]"};
3366 break;
3367 }
3368 final int numTxLvls = Math.min(counter.getTxTimeCounters().length, powerLevel.length);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003369 if (numTxLvls > 1) {
Siddharth Ray3c648c42017-10-02 17:30:58 -07003370 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003371 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3372 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3373 sb.setLength(0);
3374 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003375 sb.append(" ");
3376 sb.append(powerLevel[lvl]);
3377 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003378 formatTimeMs(sb, txLvlTimeMs);
3379 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003380 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003381 sb.append(")");
3382 pw.println(sb.toString());
3383 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003384 } else {
3385 final long txLvlTimeMs = counter.getTxTimeCounters()[0].getCountLocked(which);
3386 formatTimeMs(sb, txLvlTimeMs);
3387 sb.append("(");
3388 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
3389 sb.append(")");
3390 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003391 }
3392
Siddharth Ray3c648c42017-10-02 17:30:58 -07003393 if (powerDrainMaMs > 0) {
3394 sb.setLength(0);
3395 sb.append(prefix);
3396 sb.append(" ");
3397 sb.append(controllerName);
3398 sb.append(" Battery drain: ").append(
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003399 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
Siddharth Ray3c648c42017-10-02 17:30:58 -07003400 sb.append("mAh");
3401 pw.println(sb.toString());
3402 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003403 }
3404
3405 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003406 * Temporary for settings.
3407 */
3408 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3409 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3410 }
3411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003412 /**
3413 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003414 *
Kweku Adams87b19ec2017-10-09 12:40:03 -07003415 * NOTE: all times are expressed in microseconds, unless specified otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003417 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3418 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003419 final long rawUptime = SystemClock.uptimeMillis() * 1000;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003420 final long rawRealtimeMs = SystemClock.elapsedRealtime();
3421 final long rawRealtime = rawRealtimeMs * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003422 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3424 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003425 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3426 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3427 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 final long totalRealtime = computeRealtime(rawRealtime, which);
3429 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003430 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Mike Mac2f518a2017-09-19 16:06:03 -07003431 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003432 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003433 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003434 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3435 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003436 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003437 rawRealtime, which);
3438 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3439 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003440 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003441 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003442 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003443 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003444 final long dischargeCount = getUahDischarge(which);
3445 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
3446 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Ma15313c92017-11-15 17:58:21 -08003447 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
3448 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003449
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003450 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003451
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003452 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003453 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003454
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003455 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003457 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003458 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003459 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003460 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003461 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003462 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003463 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003464 getEstimatedBatteryCapacity(),
Mike Mac2f518a2017-09-19 16:06:03 -07003465 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity(),
3466 screenDozeTime / 1000);
Adam Lesinski67c134f2016-06-10 15:15:08 -07003467
Bookatzc8c44962017-05-11 12:12:54 -07003468
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003469 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003470 long fullWakeLockTimeTotal = 0;
3471 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003472
Evan Millar22ac0432009-03-31 11:33:18 -07003473 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003474 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003475
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003476 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3477 = u.getWakelockStats();
3478 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3479 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003480
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003481 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3482 if (fullWakeTimer != null) {
3483 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3484 which);
3485 }
3486
3487 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3488 if (partialWakeTimer != null) {
3489 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3490 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003491 }
3492 }
3493 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003494
3495 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003496 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3497 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3498 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3499 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3500 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3501 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3502 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3503 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003504 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3505 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003506 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3507 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003508 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3509 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003510
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003511 // Dump Modem controller stats
3512 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3513 getModemControllerActivity(), which);
3514
Adam Lesinskie283d332015-04-16 12:29:25 -07003515 // Dump Wifi controller stats
3516 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3517 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003518 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003519 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003520
3521 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3522 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003523
3524 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003525 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3526 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003528 // Dump misc stats
3529 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003530 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003531 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003532 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003533 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003534 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003535 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3536 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003537 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003538 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3539 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3540 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3541 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003542 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003543
Dianne Hackborn617f8772009-03-31 15:04:46 -07003544 // Dump screen brightness stats
3545 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3546 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003547 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003548 }
3549 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003550
Dianne Hackborn627bba72009-03-24 22:32:56 -07003551 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003552 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3553 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003554 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003555 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003556 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003557 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003558 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003559 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003560 args[i] = getPhoneSignalStrengthCount(i, which);
3561 }
3562 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003563
Dianne Hackborn627bba72009-03-24 22:32:56 -07003564 // Dump network type stats
3565 args = new Object[NUM_DATA_CONNECTION_TYPES];
3566 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003567 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003568 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003569 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3570 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3571 args[i] = getPhoneDataConnectionCount(i, which);
3572 }
3573 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003574
3575 // Dump wifi state stats
3576 args = new Object[NUM_WIFI_STATES];
3577 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003578 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003579 }
3580 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3581 for (int i=0; i<NUM_WIFI_STATES; i++) {
3582 args[i] = getWifiStateCount(i, which);
3583 }
3584 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3585
Dianne Hackborn3251b902014-06-20 14:40:53 -07003586 // Dump wifi suppl state stats
3587 args = new Object[NUM_WIFI_SUPPL_STATES];
3588 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3589 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3590 }
3591 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3592 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3593 args[i] = getWifiSupplStateCount(i, which);
3594 }
3595 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3596
3597 // Dump wifi signal strength stats
3598 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3599 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3600 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3601 }
3602 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3603 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3604 args[i] = getWifiSignalStrengthCount(i, which);
3605 }
3606 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3607
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003608 // Dump Multicast total stats
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003609 final long multicastWakeLockTimeTotalMicros =
3610 getWifiMulticastWakelockTime(rawRealtime, which);
3611 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003612 dumpLine(pw, 0 /* uid */, category, WIFI_MULTICAST_TOTAL_DATA,
3613 multicastWakeLockTimeTotalMicros / 1000,
3614 multicastWakeLockCountTotal);
3615
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003616 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003617 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003618 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003619 }
Bookatzc8c44962017-05-11 12:12:54 -07003620
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003621 if (which == STATS_SINCE_UNPLUGGED) {
3622 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3623 getDischargeStartLevel()-getDischargeCurrentLevel(),
3624 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003625 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
Mike Mac2f518a2017-09-19 16:06:03 -07003626 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003627 getDischargeAmountScreenDoze(), dischargeScreenDozeCount / 1000,
3628 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003629 } else {
3630 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3631 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003632 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003633 getDischargeAmountScreenOffSinceCharge(),
Mike Mac2f518a2017-09-19 16:06:03 -07003634 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003635 getDischargeAmountScreenDozeSinceCharge(), dischargeScreenDozeCount / 1000,
3636 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003637 }
Bookatzc8c44962017-05-11 12:12:54 -07003638
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003639 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003640 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003641 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003642 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003643 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003644 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003645 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3646 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003647 }
Evan Millarc64edde2009-04-18 12:26:32 -07003648 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003649 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003650 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003651 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3652 // Not doing the regular wake lock formatting to remain compatible
3653 // with the old checkin format.
3654 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3655 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003656 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003657 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003658 }
3659 }
Evan Millarc64edde2009-04-18 12:26:32 -07003660 }
Bookatzc8c44962017-05-11 12:12:54 -07003661
Bookatz50df7112017-08-04 14:53:26 -07003662 final Map<String, ? extends Timer> rpmStats = getRpmStats();
3663 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
3664 if (rpmStats.size() > 0) {
3665 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
3666 sb.setLength(0);
3667 Timer totalTimer = ent.getValue();
3668 long timeMs = (totalTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3669 int count = totalTimer.getCountLocked(which);
3670 Timer screenOffTimer = screenOffRpmStats.get(ent.getKey());
3671 long screenOffTimeMs = screenOffTimer != null
3672 ? (screenOffTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3673 int screenOffCount = screenOffTimer != null
3674 ? screenOffTimer.getCountLocked(which) : 0;
Bookatz82b341172017-09-07 19:06:08 -07003675 if (SCREEN_OFF_RPM_STATS_ENABLED) {
3676 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3677 "\"" + ent.getKey() + "\"", timeMs, count, screenOffTimeMs,
3678 screenOffCount);
3679 } else {
3680 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3681 "\"" + ent.getKey() + "\"", timeMs, count);
3682 }
Bookatz50df7112017-08-04 14:53:26 -07003683 }
3684 }
3685
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003686 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003687 helper.create(this);
3688 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003689 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003690 if (sippers != null && sippers.size() > 0) {
3691 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3692 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003693 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003694 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3695 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
Kweku Adams87b19ec2017-10-09 12:40:03 -07003696 int uid = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003697 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003698 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003699 String label;
3700 switch (bs.drainType) {
3701 case IDLE:
3702 label="idle";
3703 break;
3704 case CELL:
3705 label="cell";
3706 break;
3707 case PHONE:
3708 label="phone";
3709 break;
3710 case WIFI:
3711 label="wifi";
3712 break;
3713 case BLUETOOTH:
3714 label="blue";
3715 break;
3716 case SCREEN:
3717 label="scrn";
3718 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003719 case FLASHLIGHT:
3720 label="flashlight";
3721 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003722 case APP:
3723 uid = bs.uidObj.getUid();
3724 label = "uid";
3725 break;
3726 case USER:
3727 uid = UserHandle.getUid(bs.userId, 0);
3728 label = "user";
3729 break;
3730 case UNACCOUNTED:
3731 label = "unacc";
3732 break;
3733 case OVERCOUNTED:
3734 label = "over";
3735 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003736 case CAMERA:
3737 label = "camera";
3738 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003739 case MEMORY:
3740 label = "memory";
3741 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003742 default:
3743 label = "???";
3744 }
3745 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07003746 BatteryStatsHelper.makemAh(bs.totalPowerMah),
3747 bs.shouldHide ? 1 : 0,
3748 BatteryStatsHelper.makemAh(bs.screenPowerMah),
3749 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003750 }
3751 }
3752
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003753 final long[] cpuFreqs = getCpuFreqs();
3754 if (cpuFreqs != null) {
3755 sb.setLength(0);
3756 for (int i = 0; i < cpuFreqs.length; ++i) {
3757 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3758 }
3759 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3760 }
3761
Kweku Adams87b19ec2017-10-09 12:40:03 -07003762 // Dump stats per UID.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 for (int iu = 0; iu < NU; iu++) {
3764 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003765 if (reqUid >= 0 && uid != reqUid) {
3766 continue;
3767 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003768 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003771 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3772 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3773 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3774 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3775 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3776 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3777 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3778 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003779 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003780 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3781 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003782 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003783 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3784 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003785 // Background data transfers
3786 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3787 which);
3788 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3789 which);
3790 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3791 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3792 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3793 which);
3794 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3795 which);
3796 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3797 which);
3798 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3799 which);
3800
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003801 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3802 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003803 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003804 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3805 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3806 || wifiBytesBgTx > 0
3807 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3808 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003809 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3810 wifiBytesRx, wifiBytesTx,
3811 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003812 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003813 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003814 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3815 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3816 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3817 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003818 }
3819
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003820 // Dump modem controller data, per UID.
3821 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3822 u.getModemControllerActivity(), which);
3823
3824 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003825 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3826 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3827 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003828 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3829 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003830 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3831 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3832 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003833 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003834 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003835 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3836 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003837 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3838 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003839 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003840 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003842
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003843 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3844 u.getWifiControllerActivity(), which);
3845
Bookatz867c0d72017-03-07 18:23:42 -08003846 final Timer bleTimer = u.getBluetoothScanTimer();
3847 if (bleTimer != null) {
3848 // Convert from microseconds to milliseconds with rounding
3849 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3850 / 1000;
3851 if (totalTime != 0) {
3852 final int count = bleTimer.getCountLocked(which);
3853 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3854 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003855 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3856 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3857 final long actualTimeBg = bleTimerBg != null ?
3858 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003859 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003860 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3861 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003862 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3863 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3864 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3865 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3866 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
3867 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3868 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
3869 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3870 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
3871 final Timer unoptimizedScanTimerBg =
3872 u.getBluetoothUnoptimizedScanBackgroundTimer();
3873 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
3874 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3875 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
3876 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3877
Bookatz867c0d72017-03-07 18:23:42 -08003878 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07003879 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
3880 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
3881 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08003882 }
3883 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003884
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003885 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3886 u.getBluetoothControllerActivity(), which);
3887
Dianne Hackborn617f8772009-03-31 15:04:46 -07003888 if (u.hasUserActivity()) {
3889 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3890 boolean hasData = false;
3891 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3892 int val = u.getUserActivityCount(i, which);
3893 args[i] = val;
3894 if (val != 0) hasData = true;
3895 }
3896 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003897 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003898 }
3899 }
Bookatzc8c44962017-05-11 12:12:54 -07003900
3901 if (u.getAggregatedPartialWakelockTimer() != null) {
3902 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07003903 // Times are since reset (regardless of 'which')
3904 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07003905 final Timer bgTimer = timer.getSubTimer();
3906 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003907 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07003908 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
3909 }
3910
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003911 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3912 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3913 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3914 String linePrefix = "";
3915 sb.setLength(0);
3916 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3917 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003918 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3919 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003920 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003921 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
3922 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003923 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3924 rawRealtime, "w", which, linePrefix);
3925
Kweku Adams103351f2017-10-16 14:39:34 -07003926 // Only log if we had at least one wakelock...
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003927 if (sb.length() > 0) {
3928 String name = wakelocks.keyAt(iw);
3929 if (name.indexOf(',') >= 0) {
3930 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003931 }
Yi Jin02483362017-08-04 11:30:44 -07003932 if (name.indexOf('\n') >= 0) {
3933 name = name.replace('\n', '_');
3934 }
3935 if (name.indexOf('\r') >= 0) {
3936 name = name.replace('\r', '_');
3937 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003938 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 }
3940 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003941
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003942 // WiFi Multicast Wakelock Statistics
3943 final Timer mcTimer = u.getMulticastWakelockStats();
3944 if (mcTimer != null) {
3945 final long totalMcWakelockTimeMs =
3946 mcTimer.getTotalTimeLocked(rawRealtime, which) / 1000 ;
3947 final int countMcWakelock = mcTimer.getCountLocked(which);
3948 if(totalMcWakelockTimeMs > 0) {
3949 dumpLine(pw, uid, category, WIFI_MULTICAST_DATA,
3950 totalMcWakelockTimeMs, countMcWakelock);
3951 }
3952 }
3953
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003954 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3955 for (int isy=syncs.size()-1; isy>=0; isy--) {
3956 final Timer timer = syncs.valueAt(isy);
3957 // Convert from microseconds to milliseconds with rounding
3958 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3959 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07003960 final Timer bgTimer = timer.getSubTimer();
3961 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003962 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07003963 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003964 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003965 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07003966 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003967 }
3968 }
3969
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003970 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3971 for (int ij=jobs.size()-1; ij>=0; ij--) {
3972 final Timer timer = jobs.valueAt(ij);
3973 // Convert from microseconds to milliseconds with rounding
3974 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3975 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07003976 final Timer bgTimer = timer.getSubTimer();
3977 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003978 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07003979 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003980 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003981 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07003982 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003983 }
3984 }
3985
Dianne Hackborn94326cb2017-06-28 16:17:20 -07003986 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
3987 for (int ic=completions.size()-1; ic>=0; ic--) {
3988 SparseIntArray types = completions.valueAt(ic);
3989 if (types != null) {
3990 dumpLine(pw, uid, category, JOB_COMPLETION_DATA,
3991 "\"" + completions.keyAt(ic) + "\"",
3992 types.get(JobParameters.REASON_CANCELED, 0),
3993 types.get(JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, 0),
3994 types.get(JobParameters.REASON_PREEMPT, 0),
3995 types.get(JobParameters.REASON_TIMEOUT, 0),
3996 types.get(JobParameters.REASON_DEVICE_IDLE, 0));
3997 }
3998 }
3999
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004000 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
4001 rawRealtime, which);
4002 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
4003 rawRealtime, which);
4004 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
4005 rawRealtime, which);
4006 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
4007 rawRealtime, which);
4008
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004009 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4010 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004011 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004012 final Uid.Sensor se = sensors.valueAt(ise);
4013 final int sensorNumber = sensors.keyAt(ise);
4014 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004015 if (timer != null) {
4016 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004017 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4018 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004019 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004020 final int count = timer.getCountLocked(which);
4021 final Timer bgTimer = se.getSensorBackgroundTime();
4022 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004023 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4024 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4025 final long bgActualTime = bgTimer != null ?
4026 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4027 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
4028 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004029 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004030 }
4031 }
4032
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004033 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
4034 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004035
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07004036 dumpTimer(pw, uid, category, FOREGROUND_ACTIVITY_DATA, u.getForegroundActivityTimer(),
4037 rawRealtime, which);
4038
4039 dumpTimer(pw, uid, category, FOREGROUND_SERVICE_DATA, u.getForegroundServiceTimer(),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004040 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004041
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004042 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07004043 long totalStateTime = 0;
4044 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08004045 final long time = u.getProcessStateTime(ips, rawRealtime, which);
4046 totalStateTime += time;
4047 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004048 }
4049 if (totalStateTime > 0) {
4050 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
4051 }
4052
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004053 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4054 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004055 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004056 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004057 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004058 }
4059
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004060 // If the cpuFreqs is null, then don't bother checking for cpu freq times.
4061 if (cpuFreqs != null) {
4062 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
4063 // If total cpuFreqTimes is null, then we don't need to check for
4064 // screenOffCpuFreqTimes.
4065 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
4066 sb.setLength(0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004067 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004068 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004069 }
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004070 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
4071 if (screenOffCpuFreqTimeMs != null) {
4072 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
4073 sb.append("," + screenOffCpuFreqTimeMs[i]);
4074 }
4075 } else {
4076 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
4077 sb.append(",0");
4078 }
4079 }
4080 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
4081 cpuFreqTimeMs.length, sb.toString());
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004082 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -08004083
4084 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
4085 final long[] timesMs = u.getCpuFreqTimes(which, procState);
4086 if (timesMs != null && timesMs.length == cpuFreqs.length) {
4087 sb.setLength(0);
4088 for (int i = 0; i < timesMs.length; ++i) {
4089 sb.append((i == 0 ? "" : ",") + timesMs[i]);
4090 }
4091 final long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(
4092 which, procState);
4093 if (screenOffTimesMs != null) {
4094 for (int i = 0; i < screenOffTimesMs.length; ++i) {
4095 sb.append("," + screenOffTimesMs[i]);
4096 }
4097 } else {
4098 for (int i = 0; i < timesMs.length; ++i) {
4099 sb.append(",0");
4100 }
4101 }
4102 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA,
4103 Uid.UID_PROCESS_TYPES[procState], timesMs.length, sb.toString());
4104 }
4105 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004106 }
4107
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004108 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4109 = u.getProcessStats();
4110 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4111 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004112
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004113 final long userMillis = ps.getUserTime(which);
4114 final long systemMillis = ps.getSystemTime(which);
4115 final long foregroundMillis = ps.getForegroundTime(which);
4116 final int starts = ps.getStarts(which);
4117 final int numCrashes = ps.getNumCrashes(which);
4118 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004119
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004120 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
4121 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004122 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
4123 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004124 }
4125 }
4126
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004127 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4128 = u.getPackageStats();
4129 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4130 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4131 int wakeups = 0;
4132 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4133 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07004134 int count = alarms.valueAt(iwa).getCountLocked(which);
4135 wakeups += count;
4136 String name = alarms.keyAt(iwa).replace(',', '_');
4137 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004138 }
4139 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4140 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4141 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4142 final long startTime = ss.getStartTime(batteryUptime, which);
4143 final int starts = ss.getStarts(which);
4144 final int launches = ss.getLaunches(which);
4145 if (startTime != 0 || starts != 0 || launches != 0) {
4146 dumpLine(pw, uid, category, APK_DATA,
4147 wakeups, // wakeup alarms
4148 packageStats.keyAt(ipkg), // Apk
4149 serviceStats.keyAt(isvc), // service
4150 startTime / 1000, // time spent started, in ms
4151 starts,
4152 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004153 }
4154 }
4155 }
4156 }
4157 }
4158
Dianne Hackborn81038902012-11-26 17:04:09 -08004159 static final class TimerEntry {
4160 final String mName;
4161 final int mId;
4162 final BatteryStats.Timer mTimer;
4163 final long mTime;
4164 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
4165 mName = name;
4166 mId = id;
4167 mTimer = timer;
4168 mTime = time;
4169 }
4170 }
4171
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004172 private void printmAh(PrintWriter printer, double power) {
4173 printer.print(BatteryStatsHelper.makemAh(power));
4174 }
4175
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004176 private void printmAh(StringBuilder sb, double power) {
4177 sb.append(BatteryStatsHelper.makemAh(power));
4178 }
4179
Dianne Hackbornd953c532014-08-16 18:17:38 -07004180 /**
4181 * Temporary for settings.
4182 */
4183 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
4184 int reqUid) {
4185 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
4186 }
4187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004188 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004189 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07004190 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004191 final long rawUptime = SystemClock.uptimeMillis() * 1000;
4192 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07004193 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004194 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195
4196 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
4197 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
4198 final long totalRealtime = computeRealtime(rawRealtime, which);
4199 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004200 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
4201 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
4202 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004203 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
4204 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -07004205 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004206
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004207 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07004208
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004209 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07004210 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211
Adam Lesinskif9b20a92016-06-17 17:30:01 -07004212 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
4213 if (estimatedBatteryCapacity > 0) {
4214 sb.setLength(0);
4215 sb.append(prefix);
4216 sb.append(" Estimated battery capacity: ");
4217 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
4218 sb.append(" mAh");
4219 pw.println(sb.toString());
4220 }
4221
Jocelyn Dangc627d102017-04-14 13:15:14 -07004222 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
4223 if (minLearnedBatteryCapacity > 0) {
4224 sb.setLength(0);
4225 sb.append(prefix);
4226 sb.append(" Min learned battery capacity: ");
4227 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
4228 sb.append(" mAh");
4229 pw.println(sb.toString());
4230 }
4231 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
4232 if (maxLearnedBatteryCapacity > 0) {
4233 sb.setLength(0);
4234 sb.append(prefix);
4235 sb.append(" Max learned battery capacity: ");
4236 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
4237 sb.append(" mAh");
4238 pw.println(sb.toString());
4239 }
4240
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004241 sb.setLength(0);
4242 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004243 sb.append(" Time on battery: ");
4244 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
4245 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
4246 sb.append(") realtime, ");
4247 formatTimeMs(sb, whichBatteryUptime / 1000);
4248 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, whichBatteryRealtime));
4249 sb.append(") uptime");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004250 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004251
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004252 sb.setLength(0);
4253 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004254 sb.append(" Time on battery screen off: ");
4255 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
4256 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, whichBatteryRealtime));
4257 sb.append(") realtime, ");
4258 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
4259 sb.append("(");
4260 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, whichBatteryRealtime));
4261 sb.append(") uptime");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004262 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004263
4264 sb.setLength(0);
4265 sb.append(prefix);
4266 sb.append(" Time on battery screen doze: ");
4267 formatTimeMs(sb, screenDozeTime / 1000); sb.append("(");
4268 sb.append(formatRatioLocked(screenDozeTime, whichBatteryRealtime));
4269 sb.append(")");
4270 pw.println(sb.toString());
4271
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004272 sb.setLength(0);
4273 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004274 sb.append(" Total run time: ");
4275 formatTimeMs(sb, totalRealtime / 1000);
4276 sb.append("realtime, ");
4277 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004278 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07004279 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004280 if (batteryTimeRemaining >= 0) {
4281 sb.setLength(0);
4282 sb.append(prefix);
4283 sb.append(" Battery time remaining: ");
4284 formatTimeMs(sb, batteryTimeRemaining / 1000);
4285 pw.println(sb.toString());
4286 }
4287 if (chargeTimeRemaining >= 0) {
4288 sb.setLength(0);
4289 sb.append(prefix);
4290 sb.append(" Charge time remaining: ");
4291 formatTimeMs(sb, chargeTimeRemaining / 1000);
4292 pw.println(sb.toString());
4293 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004294
Kweku Adams87b19ec2017-10-09 12:40:03 -07004295 final long dischargeCount = getUahDischarge(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004296 if (dischargeCount >= 0) {
4297 sb.setLength(0);
4298 sb.append(prefix);
4299 sb.append(" Discharge: ");
4300 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
4301 sb.append(" mAh");
4302 pw.println(sb.toString());
4303 }
4304
Kweku Adams87b19ec2017-10-09 12:40:03 -07004305 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004306 if (dischargeScreenOffCount >= 0) {
4307 sb.setLength(0);
4308 sb.append(prefix);
4309 sb.append(" Screen off discharge: ");
4310 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
4311 sb.append(" mAh");
4312 pw.println(sb.toString());
4313 }
4314
Kweku Adams87b19ec2017-10-09 12:40:03 -07004315 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Mac2f518a2017-09-19 16:06:03 -07004316 if (dischargeScreenDozeCount >= 0) {
4317 sb.setLength(0);
4318 sb.append(prefix);
4319 sb.append(" Screen doze discharge: ");
4320 sb.append(BatteryStatsHelper.makemAh(dischargeScreenDozeCount / 1000.0));
4321 sb.append(" mAh");
4322 pw.println(sb.toString());
4323 }
4324
4325 final long dischargeScreenOnCount =
4326 dischargeCount - dischargeScreenOffCount - dischargeScreenDozeCount;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004327 if (dischargeScreenOnCount >= 0) {
4328 sb.setLength(0);
4329 sb.append(prefix);
4330 sb.append(" Screen on discharge: ");
4331 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
4332 sb.append(" mAh");
4333 pw.println(sb.toString());
4334 }
4335
Mike Ma15313c92017-11-15 17:58:21 -08004336 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
4337 if (dischargeLightDozeCount >= 0) {
4338 sb.setLength(0);
4339 sb.append(prefix);
4340 sb.append(" Device light doze discharge: ");
4341 sb.append(BatteryStatsHelper.makemAh(dischargeLightDozeCount / 1000.0));
4342 sb.append(" mAh");
4343 pw.println(sb.toString());
4344 }
4345
4346 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
4347 if (dischargeDeepDozeCount >= 0) {
4348 sb.setLength(0);
4349 sb.append(prefix);
4350 sb.append(" Device deep doze discharge: ");
4351 sb.append(BatteryStatsHelper.makemAh(dischargeDeepDozeCount / 1000.0));
4352 sb.append(" mAh");
4353 pw.println(sb.toString());
4354 }
4355
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004356 pw.print(" Start clock time: ");
4357 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
4358
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004359 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004360 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004361 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004362 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
4363 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004364 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004365 rawRealtime, which);
4366 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
4367 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004368 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004369 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004370 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
4371 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
4372 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004373 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004374 sb.append(prefix);
4375 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
4376 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004377 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004378 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
4379 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004380 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004381 pw.println(sb.toString());
4382 sb.setLength(0);
4383 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004384 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004385 boolean didOne = false;
4386 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004387 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004388 if (time == 0) {
4389 continue;
4390 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004391 sb.append("\n ");
4392 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004393 didOne = true;
4394 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
4395 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004396 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004397 sb.append("(");
4398 sb.append(formatRatioLocked(time, screenOnTime));
4399 sb.append(")");
4400 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004401 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004402 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004403 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004404 sb.setLength(0);
4405 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004406 sb.append(" Power save mode enabled: ");
4407 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004408 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004409 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004410 sb.append(")");
4411 pw.println(sb.toString());
4412 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004413 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004414 sb.setLength(0);
4415 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004416 sb.append(" Device light idling: ");
4417 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004418 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004419 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
4420 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004421 sb.append("x");
4422 pw.println(sb.toString());
4423 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004424 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004425 sb.setLength(0);
4426 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004427 sb.append(" Idle mode light time: ");
4428 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004429 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004430 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
4431 sb.append(") ");
4432 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004433 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004434 sb.append(" -- longest ");
4435 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
4436 pw.println(sb.toString());
4437 }
4438 if (deviceIdlingTime != 0) {
4439 sb.setLength(0);
4440 sb.append(prefix);
4441 sb.append(" Device full idling: ");
4442 formatTimeMs(sb, deviceIdlingTime / 1000);
4443 sb.append("(");
4444 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004445 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004446 sb.append("x");
4447 pw.println(sb.toString());
4448 }
4449 if (deviceIdleModeFullTime != 0) {
4450 sb.setLength(0);
4451 sb.append(prefix);
4452 sb.append(" Idle mode full time: ");
4453 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
4454 sb.append("(");
4455 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
4456 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004457 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004458 sb.append("x");
4459 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004460 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004461 pw.println(sb.toString());
4462 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004463 if (phoneOnTime != 0) {
4464 sb.setLength(0);
4465 sb.append(prefix);
4466 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
4467 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004468 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004469 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004470 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004471 if (connChanges != 0) {
4472 pw.print(prefix);
4473 pw.print(" Connectivity changes: "); pw.println(connChanges);
4474 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004475
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004476 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07004477 long fullWakeLockTimeTotalMicros = 0;
4478 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08004479
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004480 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08004481
Evan Millar22ac0432009-03-31 11:33:18 -07004482 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004483 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004484
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004485 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4486 = u.getWakelockStats();
4487 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4488 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07004489
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004490 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
4491 if (fullWakeTimer != null) {
4492 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
4493 rawRealtime, which);
4494 }
4495
4496 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4497 if (partialWakeTimer != null) {
4498 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
4499 rawRealtime, which);
4500 if (totalTimeMicros > 0) {
4501 if (reqUid < 0) {
4502 // Only show the ordered list of all wake
4503 // locks if the caller is not asking for data
4504 // about a specific uid.
4505 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
4506 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08004507 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004508 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07004509 }
4510 }
4511 }
4512 }
Bookatzc8c44962017-05-11 12:12:54 -07004513
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004514 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4515 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4516 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4517 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4518 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4519 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4520 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4521 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004522 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4523 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004524
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004525 if (fullWakeLockTimeTotalMicros != 0) {
4526 sb.setLength(0);
4527 sb.append(prefix);
4528 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4529 (fullWakeLockTimeTotalMicros + 500) / 1000);
4530 pw.println(sb.toString());
4531 }
4532
4533 if (partialWakeLockTimeTotalMicros != 0) {
4534 sb.setLength(0);
4535 sb.append(prefix);
4536 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4537 (partialWakeLockTimeTotalMicros + 500) / 1000);
4538 pw.println(sb.toString());
4539 }
4540
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004541 final long multicastWakeLockTimeTotalMicros =
4542 getWifiMulticastWakelockTime(rawRealtime, which);
4543 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004544 if (multicastWakeLockTimeTotalMicros != 0) {
4545 sb.setLength(0);
4546 sb.append(prefix);
4547 sb.append(" Total WiFi Multicast wakelock Count: ");
4548 sb.append(multicastWakeLockCountTotal);
4549 pw.println(sb.toString());
4550
4551 sb.setLength(0);
4552 sb.append(prefix);
4553 sb.append(" Total WiFi Multicast wakelock time: ");
4554 formatTimeMsNoSpace(sb, (multicastWakeLockTimeTotalMicros + 500) / 1000);
4555 pw.println(sb.toString());
4556 }
4557
Siddharth Ray3c648c42017-10-02 17:30:58 -07004558 pw.println("");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004559 pw.print(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004560 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004561 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004562 sb.append(" CONNECTIVITY POWER SUMMARY START");
4563 pw.println(sb.toString());
4564
4565 pw.print(prefix);
4566 sb.setLength(0);
4567 sb.append(prefix);
4568 sb.append(" Logging duration for connectivity statistics: ");
4569 formatTimeMs(sb, whichBatteryRealtime / 1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004570 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004571
4572 sb.setLength(0);
4573 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004574 sb.append(" Cellular Statistics:");
Amith Yamasanif37447b2009-10-08 18:28:01 -07004575 pw.println(sb.toString());
4576
Siddharth Ray3c648c42017-10-02 17:30:58 -07004577 pw.print(prefix);
4578 sb.setLength(0);
4579 sb.append(prefix);
4580 sb.append(" Cellular kernel active time: ");
4581 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
4582 formatTimeMs(sb, mobileActiveTime / 1000);
4583 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4584 sb.append(")");
4585 pw.println(sb.toString());
4586
4587 pw.print(" Cellular data received: "); pw.println(formatBytesLocked(mobileRxTotalBytes));
4588 pw.print(" Cellular data sent: "); pw.println(formatBytesLocked(mobileTxTotalBytes));
4589 pw.print(" Cellular packets received: "); pw.println(mobileRxTotalPackets);
4590 pw.print(" Cellular packets sent: "); pw.println(mobileTxTotalPackets);
4591
Dianne Hackborn627bba72009-03-24 22:32:56 -07004592 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004593 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004594 sb.append(" Cellular Radio Access Technology:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004595 didOne = false;
4596 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004597 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004598 if (time == 0) {
4599 continue;
4600 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004601 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004602 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004603 didOne = true;
4604 sb.append(DATA_CONNECTION_NAMES[i]);
4605 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004606 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004607 sb.append("(");
4608 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004609 sb.append(") ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004610 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004611 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004612 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004613
4614 sb.setLength(0);
4615 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004616 sb.append(" Cellular Rx signal strength (RSRP):");
4617 final String[] cellularRxSignalStrengthDescription = new String[]{
4618 "very poor (less than -128dBm): ",
4619 "poor (-128dBm to -118dBm): ",
4620 "moderate (-118dBm to -108dBm): ",
4621 "good (-108dBm to -98dBm): ",
4622 "great (greater than -98dBm): "};
4623 didOne = false;
4624 final int numCellularRxBins = Math.min(SignalStrength.NUM_SIGNAL_STRENGTH_BINS,
4625 cellularRxSignalStrengthDescription.length);
4626 for (int i=0; i<numCellularRxBins; i++) {
4627 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
4628 if (time == 0) {
4629 continue;
4630 }
4631 sb.append("\n ");
4632 sb.append(prefix);
4633 didOne = true;
4634 sb.append(cellularRxSignalStrengthDescription[i]);
4635 sb.append(" ");
4636 formatTimeMs(sb, time/1000);
4637 sb.append("(");
4638 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4639 sb.append(") ");
4640 }
4641 if (!didOne) sb.append(" (no activity)");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004642 pw.println(sb.toString());
4643
Siddharth Ray3c648c42017-10-02 17:30:58 -07004644 printControllerActivity(pw, sb, prefix, "Cellular",
4645 getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004646
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004647 pw.print(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004648 sb.setLength(0);
4649 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004650 sb.append(" Wifi Statistics:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004651 pw.println(sb.toString());
4652
Siddharth Ray3c648c42017-10-02 17:30:58 -07004653 pw.print(" Wifi data received: "); pw.println(formatBytesLocked(wifiRxTotalBytes));
4654 pw.print(" Wifi data sent: "); pw.println(formatBytesLocked(wifiTxTotalBytes));
4655 pw.print(" Wifi packets received: "); pw.println(wifiRxTotalPackets);
4656 pw.print(" Wifi packets sent: "); pw.println(wifiTxTotalPackets);
4657
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004658 sb.setLength(0);
4659 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004660 sb.append(" Wifi states:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004661 didOne = false;
4662 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004663 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004664 if (time == 0) {
4665 continue;
4666 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004667 sb.append("\n ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004668 didOne = true;
4669 sb.append(WIFI_STATE_NAMES[i]);
4670 sb.append(" ");
4671 formatTimeMs(sb, time/1000);
4672 sb.append("(");
4673 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4674 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004675 }
4676 if (!didOne) sb.append(" (no activity)");
4677 pw.println(sb.toString());
4678
4679 sb.setLength(0);
4680 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004681 sb.append(" Wifi supplicant states:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004682 didOne = false;
4683 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4684 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4685 if (time == 0) {
4686 continue;
4687 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004688 sb.append("\n ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004689 didOne = true;
4690 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4691 sb.append(" ");
4692 formatTimeMs(sb, time/1000);
4693 sb.append("(");
4694 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4695 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004696 }
4697 if (!didOne) sb.append(" (no activity)");
4698 pw.println(sb.toString());
4699
4700 sb.setLength(0);
4701 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004702 sb.append(" Wifi Rx signal strength (RSSI):");
4703 final String[] wifiRxSignalStrengthDescription = new String[]{
4704 "very poor (less than -88.75dBm): ",
4705 "poor (-88.75 to -77.5dBm): ",
4706 "moderate (-77.5dBm to -66.25dBm): ",
4707 "good (-66.25dBm to -55dBm): ",
4708 "great (greater than -55dBm): "};
Dianne Hackborn3251b902014-06-20 14:40:53 -07004709 didOne = false;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004710 final int numWifiRxBins = Math.min(NUM_WIFI_SIGNAL_STRENGTH_BINS,
4711 wifiRxSignalStrengthDescription.length);
4712 for (int i=0; i<numWifiRxBins; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004713 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4714 if (time == 0) {
4715 continue;
4716 }
4717 sb.append("\n ");
4718 sb.append(prefix);
4719 didOne = true;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004720 sb.append(" ");
4721 sb.append(wifiRxSignalStrengthDescription[i]);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004722 formatTimeMs(sb, time/1000);
4723 sb.append("(");
4724 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4725 sb.append(") ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004726 }
4727 if (!didOne) sb.append(" (no activity)");
4728 pw.println(sb.toString());
4729
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004730 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004731
Adam Lesinski50e47602015-12-04 17:04:54 -08004732 pw.print(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004733 sb.setLength(0);
4734 sb.append(prefix);
4735 sb.append(" CONNECTIVITY POWER SUMMARY END");
4736 pw.println(sb.toString());
4737 pw.println("");
4738
4739 pw.print(prefix);
Adam Lesinski50e47602015-12-04 17:04:54 -08004740 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4741 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4742
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004743 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4744 sb.setLength(0);
4745 sb.append(prefix);
4746 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4747 pw.println(sb.toString());
4748
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004749 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4750 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004751
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004752 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004753
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004754 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004755 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004756 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004757 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004758 pw.println(getDischargeStartLevel());
4759 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4760 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004761 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004762 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004763 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004764 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004765 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004766 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004767 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004768 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004769 pw.println(getDischargeAmountScreenOn());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004770 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004771 pw.println(getDischargeAmountScreenOff());
4772 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4773 pw.println(getDischargeAmountScreenDoze());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004774 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004775 } else {
4776 pw.print(prefix); pw.println(" Device battery use since last full charge");
4777 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004778 pw.println(getLowDischargeAmountSinceCharge());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004779 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004780 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004781 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004782 pw.println(getDischargeAmountScreenOnSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004783 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004784 pw.println(getDischargeAmountScreenOffSinceCharge());
4785 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4786 pw.println(getDischargeAmountScreenDozeSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004787 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004788 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004789
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004790 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004791 helper.create(this);
4792 helper.refreshStats(which, UserHandle.USER_ALL);
4793 List<BatterySipper> sippers = helper.getUsageList();
4794 if (sippers != null && sippers.size() > 0) {
4795 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4796 pw.print(prefix); pw.print(" Capacity: ");
4797 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004798 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004799 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4800 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4801 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4802 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004803 pw.println();
4804 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004805 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004806 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004807 switch (bs.drainType) {
4808 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004809 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004810 break;
4811 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004812 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004813 break;
4814 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004815 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004816 break;
4817 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004818 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004819 break;
4820 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004821 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004822 break;
4823 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004824 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004825 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004826 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004827 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004828 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004829 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004830 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004831 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004832 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004833 break;
4834 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004835 pw.print(" User "); pw.print(bs.userId);
4836 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004837 break;
4838 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004839 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004840 break;
4841 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004842 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004843 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004844 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004845 pw.print(" Camera: ");
4846 break;
4847 default:
4848 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004849 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004850 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004851 printmAh(pw, bs.totalPowerMah);
4852
Adam Lesinski57123002015-06-12 16:12:07 -07004853 if (bs.usagePowerMah != bs.totalPowerMah) {
4854 // If the usage (generic power) isn't the whole amount, we list out
4855 // what components are involved in the calculation.
4856
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004857 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004858 if (bs.usagePowerMah != 0) {
4859 pw.print(" usage=");
4860 printmAh(pw, bs.usagePowerMah);
4861 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004862 if (bs.cpuPowerMah != 0) {
4863 pw.print(" cpu=");
4864 printmAh(pw, bs.cpuPowerMah);
4865 }
4866 if (bs.wakeLockPowerMah != 0) {
4867 pw.print(" wake=");
4868 printmAh(pw, bs.wakeLockPowerMah);
4869 }
4870 if (bs.mobileRadioPowerMah != 0) {
4871 pw.print(" radio=");
4872 printmAh(pw, bs.mobileRadioPowerMah);
4873 }
4874 if (bs.wifiPowerMah != 0) {
4875 pw.print(" wifi=");
4876 printmAh(pw, bs.wifiPowerMah);
4877 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004878 if (bs.bluetoothPowerMah != 0) {
4879 pw.print(" bt=");
4880 printmAh(pw, bs.bluetoothPowerMah);
4881 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004882 if (bs.gpsPowerMah != 0) {
4883 pw.print(" gps=");
4884 printmAh(pw, bs.gpsPowerMah);
4885 }
4886 if (bs.sensorPowerMah != 0) {
4887 pw.print(" sensor=");
4888 printmAh(pw, bs.sensorPowerMah);
4889 }
4890 if (bs.cameraPowerMah != 0) {
4891 pw.print(" camera=");
4892 printmAh(pw, bs.cameraPowerMah);
4893 }
4894 if (bs.flashlightPowerMah != 0) {
4895 pw.print(" flash=");
4896 printmAh(pw, bs.flashlightPowerMah);
4897 }
4898 pw.print(" )");
4899 }
Bookatz17d7d9d2017-06-08 14:50:46 -07004900
4901 // If there is additional smearing information, include it.
4902 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
4903 pw.print(" Including smearing: ");
4904 printmAh(pw, bs.totalSmearedPowerMah);
4905 pw.print(" (");
4906 if (bs.screenPowerMah != 0) {
4907 pw.print(" screen=");
4908 printmAh(pw, bs.screenPowerMah);
4909 }
4910 if (bs.proportionalSmearMah != 0) {
4911 pw.print(" proportional=");
4912 printmAh(pw, bs.proportionalSmearMah);
4913 }
4914 pw.print(" )");
4915 }
4916 if (bs.shouldHide) {
4917 pw.print(" Excluded from smearing");
4918 }
4919
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004920 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004921 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004922 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004923 }
4924
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004925 sippers = helper.getMobilemsppList();
4926 if (sippers != null && sippers.size() > 0) {
4927 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004928 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004929 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004930 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004931 sb.setLength(0);
4932 sb.append(prefix); sb.append(" Uid ");
4933 UserHandle.formatUid(sb, bs.uidObj.getUid());
4934 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4935 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4936 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004937 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004938 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004939 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004940 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004941 sb.setLength(0);
4942 sb.append(prefix);
4943 sb.append(" TOTAL TIME: ");
4944 formatTimeMs(sb, totalTime);
4945 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4946 sb.append(")");
4947 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004948 pw.println();
4949 }
4950
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004951 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4952 @Override
4953 public int compare(TimerEntry lhs, TimerEntry rhs) {
4954 long lhsTime = lhs.mTime;
4955 long rhsTime = rhs.mTime;
4956 if (lhsTime < rhsTime) {
4957 return 1;
4958 }
4959 if (lhsTime > rhsTime) {
4960 return -1;
4961 }
4962 return 0;
4963 }
4964 };
4965
4966 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004967 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4968 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004969 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004970 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4971 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4972 : kernelWakelocks.entrySet()) {
4973 final BatteryStats.Timer timer = ent.getValue();
4974 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004975 if (totalTimeMillis > 0) {
4976 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4977 }
4978 }
4979 if (ktimers.size() > 0) {
4980 Collections.sort(ktimers, timerComparator);
4981 pw.print(prefix); pw.println(" All kernel wake locks:");
4982 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004983 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004984 String linePrefix = ": ";
4985 sb.setLength(0);
4986 sb.append(prefix);
4987 sb.append(" Kernel Wake lock ");
4988 sb.append(timer.mName);
4989 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4990 which, linePrefix);
4991 if (!linePrefix.equals(": ")) {
4992 sb.append(" realtime");
4993 // Only print out wake locks that were held
4994 pw.println(sb.toString());
4995 }
4996 }
4997 pw.println();
4998 }
4999 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005000
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005001 if (timers.size() > 0) {
5002 Collections.sort(timers, timerComparator);
5003 pw.print(prefix); pw.println(" All partial wake locks:");
5004 for (int i=0; i<timers.size(); i++) {
5005 TimerEntry timer = timers.get(i);
5006 sb.setLength(0);
5007 sb.append(" Wake lock ");
5008 UserHandle.formatUid(sb, timer.mId);
5009 sb.append(" ");
5010 sb.append(timer.mName);
5011 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5012 sb.append(" realtime");
5013 pw.println(sb.toString());
5014 }
5015 timers.clear();
5016 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08005017 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005018
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005019 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005020 if (wakeupReasons.size() > 0) {
5021 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005022 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005023 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005024 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005025 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
5026 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005027 }
5028 Collections.sort(reasons, timerComparator);
5029 for (int i=0; i<reasons.size(); i++) {
5030 TimerEntry timer = reasons.get(i);
5031 String linePrefix = ": ";
5032 sb.setLength(0);
5033 sb.append(prefix);
5034 sb.append(" Wakeup reason ");
5035 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005036 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5037 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005038 pw.println(sb.toString());
5039 }
5040 pw.println();
5041 }
Dianne Hackborn81038902012-11-26 17:04:09 -08005042 }
Evan Millar22ac0432009-03-31 11:33:18 -07005043
James Carr2dd7e5e2016-07-20 18:48:39 -07005044 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
Bookatz50df7112017-08-04 14:53:26 -07005045 if (mMemoryStats.size() > 0) {
5046 pw.println(" Memory Stats");
5047 for (int i = 0; i < mMemoryStats.size(); i++) {
5048 sb.setLength(0);
5049 sb.append(" Bandwidth ");
5050 sb.append(mMemoryStats.keyAt(i));
5051 sb.append(" Time ");
5052 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
5053 pw.println(sb.toString());
5054 }
5055 pw.println();
5056 }
5057
5058 final Map<String, ? extends Timer> rpmStats = getRpmStats();
5059 if (rpmStats.size() > 0) {
5060 pw.print(prefix); pw.println(" Resource Power Manager Stats");
5061 if (rpmStats.size() > 0) {
5062 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
5063 final String timerName = ent.getKey();
5064 final Timer timer = ent.getValue();
5065 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5066 }
5067 }
5068 pw.println();
5069 }
Bookatz82b341172017-09-07 19:06:08 -07005070 if (SCREEN_OFF_RPM_STATS_ENABLED) {
5071 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
Bookatz50df7112017-08-04 14:53:26 -07005072 if (screenOffRpmStats.size() > 0) {
Bookatz82b341172017-09-07 19:06:08 -07005073 pw.print(prefix);
5074 pw.println(" Resource Power Manager Stats for when screen was off");
5075 if (screenOffRpmStats.size() > 0) {
5076 for (Map.Entry<String, ? extends Timer> ent : screenOffRpmStats.entrySet()) {
5077 final String timerName = ent.getKey();
5078 final Timer timer = ent.getValue();
5079 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5080 }
Bookatz50df7112017-08-04 14:53:26 -07005081 }
Bookatz82b341172017-09-07 19:06:08 -07005082 pw.println();
Bookatz50df7112017-08-04 14:53:26 -07005083 }
James Carr2dd7e5e2016-07-20 18:48:39 -07005084 }
5085
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005086 final long[] cpuFreqs = getCpuFreqs();
5087 if (cpuFreqs != null) {
5088 sb.setLength(0);
Bookatz50df7112017-08-04 14:53:26 -07005089 sb.append(" CPU freqs:");
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005090 for (int i = 0; i < cpuFreqs.length; ++i) {
5091 sb.append(" " + cpuFreqs[i]);
5092 }
5093 pw.println(sb.toString());
Bookatz50df7112017-08-04 14:53:26 -07005094 pw.println();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005095 }
5096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005097 for (int iu=0; iu<NU; iu++) {
5098 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005099 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005100 continue;
5101 }
Bookatzc8c44962017-05-11 12:12:54 -07005102
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005103 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07005104
5105 pw.print(prefix);
5106 pw.print(" ");
5107 UserHandle.formatUid(pw, uid);
5108 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005109 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005110
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005111 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
5112 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
5113 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
5114 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005115 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
5116 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
5117
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005118 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
5119 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005120 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
5121 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005122
5123 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
5124 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
5125
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005126 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
5127 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
5128 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08005129 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
5130 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5131 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
5132 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005133 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005134
Adam Lesinski5f056f62016-07-14 16:56:08 -07005135 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
5136 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
5137
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005138 if (mobileRxBytes > 0 || mobileTxBytes > 0
5139 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005140 pw.print(prefix); pw.print(" Mobile network: ");
5141 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005142 pw.print(formatBytesLocked(mobileTxBytes));
5143 pw.print(" sent (packets "); pw.print(mobileRxPackets);
5144 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005145 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005146 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
5147 sb.setLength(0);
5148 sb.append(prefix); sb.append(" Mobile radio active: ");
5149 formatTimeMs(sb, uidMobileActiveTime / 1000);
5150 sb.append("(");
5151 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
5152 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
5153 long packets = mobileRxPackets + mobileTxPackets;
5154 if (packets == 0) {
5155 packets = 1;
5156 }
5157 sb.append(" @ ");
5158 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
5159 sb.append(" mspp");
5160 pw.println(sb.toString());
5161 }
5162
Adam Lesinski5f056f62016-07-14 16:56:08 -07005163 if (mobileWakeup > 0) {
5164 sb.setLength(0);
5165 sb.append(prefix);
5166 sb.append(" Mobile radio AP wakeups: ");
5167 sb.append(mobileWakeup);
5168 pw.println(sb.toString());
5169 }
5170
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005171 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
5172 u.getModemControllerActivity(), which);
5173
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005174 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005175 pw.print(prefix); pw.print(" Wi-Fi network: ");
5176 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005177 pw.print(formatBytesLocked(wifiTxBytes));
5178 pw.print(" sent (packets "); pw.print(wifiRxPackets);
5179 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005180 }
5181
Dianne Hackborn62793e42015-03-09 11:15:41 -07005182 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08005183 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005184 || uidWifiRunningTime != 0) {
5185 sb.setLength(0);
5186 sb.append(prefix); sb.append(" Wifi Running: ");
5187 formatTimeMs(sb, uidWifiRunningTime / 1000);
5188 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
5189 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07005190 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005191 formatTimeMs(sb, fullWifiLockOnTime / 1000);
5192 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
5193 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08005194 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005195 formatTimeMs(sb, wifiScanTime / 1000);
5196 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07005197 whichBatteryRealtime)); sb.append(") ");
5198 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08005199 sb.append("x\n");
5200 // actual and background times are unpooled and since reset (regardless of 'which')
5201 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
5202 formatTimeMs(sb, wifiScanActualTime / 1000);
5203 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
5204 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5205 sb.append(") ");
5206 sb.append(wifiScanCount);
5207 sb.append("x\n");
5208 sb.append(prefix); sb.append(" Background Wifi Scan: ");
5209 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
5210 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
5211 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5212 sb.append(") ");
5213 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07005214 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005215 pw.println(sb.toString());
5216 }
5217
Adam Lesinski5f056f62016-07-14 16:56:08 -07005218 if (wifiWakeup > 0) {
5219 sb.setLength(0);
5220 sb.append(prefix);
5221 sb.append(" WiFi AP wakeups: ");
5222 sb.append(wifiWakeup);
5223 pw.println(sb.toString());
5224 }
5225
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005226 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
5227 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07005228
Adam Lesinski50e47602015-12-04 17:04:54 -08005229 if (btRxBytes > 0 || btTxBytes > 0) {
5230 pw.print(prefix); pw.print(" Bluetooth network: ");
5231 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
5232 pw.print(formatBytesLocked(btTxBytes));
5233 pw.println(" sent");
5234 }
5235
Bookatz867c0d72017-03-07 18:23:42 -08005236 final Timer bleTimer = u.getBluetoothScanTimer();
5237 if (bleTimer != null) {
5238 // Convert from microseconds to milliseconds with rounding
5239 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
5240 / 1000;
5241 if (totalTimeMs != 0) {
5242 final int count = bleTimer.getCountLocked(which);
5243 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
5244 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005245 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5246 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
5247 final long actualTimeMsBg = bleTimerBg != null ?
5248 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005249 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07005250 final int resultCount = u.getBluetoothScanResultCounter() != null ?
5251 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005252 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
5253 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
5254 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
5255 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
5256 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
5257 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5258 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
5259 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
5260 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
5261 final Timer unoptimizedScanTimerBg =
5262 u.getBluetoothUnoptimizedScanBackgroundTimer();
5263 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
5264 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5265 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
5266 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005267
5268 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08005269 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07005270 sb.append(prefix);
5271 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08005272 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07005273 sb.append(" (");
5274 sb.append(count);
5275 sb.append(" times)");
5276 if (bleTimer.isRunningLocked()) {
5277 sb.append(" (currently running)");
5278 }
5279 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005280 }
Bookatzb1f04f32017-05-19 13:57:32 -07005281
5282 sb.append(prefix);
5283 sb.append(" Bluetooth Scan (total actual realtime): ");
5284 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
5285 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005286 sb.append(count);
5287 sb.append(" times)");
5288 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07005289 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08005290 }
Bookatzb1f04f32017-05-19 13:57:32 -07005291 sb.append("\n");
5292 if (actualTimeMsBg > 0 || countBg > 0) {
5293 sb.append(prefix);
5294 sb.append(" Bluetooth Scan (background realtime): ");
5295 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
5296 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005297 sb.append(countBg);
5298 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07005299 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
5300 sb.append(" (currently running in background)");
5301 }
5302 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005303 }
Bookatzb1f04f32017-05-19 13:57:32 -07005304
5305 sb.append(prefix);
5306 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07005307 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07005308 sb.append(" (");
5309 sb.append(resultCountBg);
5310 sb.append(" in background)");
5311
5312 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
5313 sb.append("\n");
5314 sb.append(prefix);
5315 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
5316 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
5317 sb.append(" (max ");
5318 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
5319 sb.append(")");
5320 if (unoptimizedScanTimer != null
5321 && unoptimizedScanTimer.isRunningLocked()) {
5322 sb.append(" (currently running unoptimized)");
5323 }
5324 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
5325 sb.append("\n");
5326 sb.append(prefix);
5327 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
5328 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
5329 sb.append(" (max ");
5330 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
5331 sb.append(")");
5332 if (unoptimizedScanTimerBg.isRunningLocked()) {
5333 sb.append(" (currently running unoptimized in background)");
5334 }
5335 }
5336 }
Bookatz867c0d72017-03-07 18:23:42 -08005337 pw.println(sb.toString());
5338 uidActivity = true;
5339 }
5340 }
5341
5342
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005343
Dianne Hackborn617f8772009-03-31 15:04:46 -07005344 if (u.hasUserActivity()) {
5345 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07005346 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005347 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005348 if (val != 0) {
5349 if (!hasData) {
5350 sb.setLength(0);
5351 sb.append(" User activity: ");
5352 hasData = true;
5353 } else {
5354 sb.append(", ");
5355 }
5356 sb.append(val);
5357 sb.append(" ");
5358 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
5359 }
5360 }
5361 if (hasData) {
5362 pw.println(sb.toString());
5363 }
5364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005365
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005366 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
5367 = u.getWakelockStats();
5368 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005369 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005370 int countWakelock = 0;
5371 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
5372 final Uid.Wakelock wl = wakelocks.valueAt(iw);
5373 String linePrefix = ": ";
5374 sb.setLength(0);
5375 sb.append(prefix);
5376 sb.append(" Wake lock ");
5377 sb.append(wakelocks.keyAt(iw));
5378 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
5379 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005380 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
5381 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005382 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005383 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
5384 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005385 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
5386 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005387 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
5388 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07005389 sb.append(" realtime");
5390 pw.println(sb.toString());
5391 uidActivity = true;
5392 countWakelock++;
5393
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005394 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
5395 rawRealtime, which);
5396 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
5397 rawRealtime, which);
5398 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
5399 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005400 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07005401 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005402 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005403 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07005404 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
5405 // pooled and therefore just a lower bound)
5406 long actualTotalPartialWakelock = 0;
5407 long actualBgPartialWakelock = 0;
5408 if (u.getAggregatedPartialWakelockTimer() != null) {
5409 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
5410 // Convert from microseconds to milliseconds with rounding
5411 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07005412 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07005413 final Timer bgAggTimer = aggTimer.getSubTimer();
5414 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005415 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07005416 }
5417
5418 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
5419 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
5420 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005421 sb.setLength(0);
5422 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005423 sb.append(" TOTAL wake: ");
5424 boolean needComma = false;
5425 if (totalFullWakelock != 0) {
5426 needComma = true;
5427 formatTimeMs(sb, totalFullWakelock);
5428 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005429 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005430 if (totalPartialWakelock != 0) {
5431 if (needComma) {
5432 sb.append(", ");
5433 }
5434 needComma = true;
5435 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07005436 sb.append("blamed partial");
5437 }
5438 if (actualTotalPartialWakelock != 0) {
5439 if (needComma) {
5440 sb.append(", ");
5441 }
5442 needComma = true;
5443 formatTimeMs(sb, actualTotalPartialWakelock);
5444 sb.append("actual partial");
5445 }
5446 if (actualBgPartialWakelock != 0) {
5447 if (needComma) {
5448 sb.append(", ");
5449 }
5450 needComma = true;
5451 formatTimeMs(sb, actualBgPartialWakelock);
5452 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005453 }
5454 if (totalWindowWakelock != 0) {
5455 if (needComma) {
5456 sb.append(", ");
5457 }
5458 needComma = true;
5459 formatTimeMs(sb, totalWindowWakelock);
5460 sb.append("window");
5461 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005462 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07005463 if (needComma) {
5464 sb.append(",");
5465 }
5466 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005467 formatTimeMs(sb, totalDrawWakelock);
5468 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07005469 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005470 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005471 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005472 }
5473 }
5474
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07005475 // Calculate multicast wakelock stats
5476 final Timer mcTimer = u.getMulticastWakelockStats();
5477 if (mcTimer != null) {
5478 final long multicastWakeLockTimeMicros = mcTimer.getTotalTimeLocked(rawRealtime, which);
5479 final int multicastWakeLockCount = mcTimer.getCountLocked(which);
5480
5481 if (multicastWakeLockTimeMicros > 0) {
5482 sb.setLength(0);
5483 sb.append(prefix);
5484 sb.append(" WiFi Multicast Wakelock");
5485 sb.append(" count = ");
5486 sb.append(multicastWakeLockCount);
5487 sb.append(" time = ");
5488 formatTimeMsNoSpace(sb, (multicastWakeLockTimeMicros + 500) / 1000);
5489 pw.println(sb.toString());
5490 }
5491 }
5492
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005493 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
5494 for (int isy=syncs.size()-1; isy>=0; isy--) {
5495 final Timer timer = syncs.valueAt(isy);
5496 // Convert from microseconds to milliseconds with rounding
5497 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5498 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07005499 final Timer bgTimer = timer.getSubTimer();
5500 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005501 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07005502 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005503 sb.setLength(0);
5504 sb.append(prefix);
5505 sb.append(" Sync ");
5506 sb.append(syncs.keyAt(isy));
5507 sb.append(": ");
5508 if (totalTime != 0) {
5509 formatTimeMs(sb, totalTime);
5510 sb.append("realtime (");
5511 sb.append(count);
5512 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07005513 if (bgTime > 0) {
5514 sb.append(", ");
5515 formatTimeMs(sb, bgTime);
5516 sb.append("background (");
5517 sb.append(bgCount);
5518 sb.append(" times)");
5519 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005520 } else {
5521 sb.append("(not used)");
5522 }
5523 pw.println(sb.toString());
5524 uidActivity = true;
5525 }
5526
5527 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
5528 for (int ij=jobs.size()-1; ij>=0; ij--) {
5529 final Timer timer = jobs.valueAt(ij);
5530 // Convert from microseconds to milliseconds with rounding
5531 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5532 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07005533 final Timer bgTimer = timer.getSubTimer();
5534 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005535 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07005536 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005537 sb.setLength(0);
5538 sb.append(prefix);
5539 sb.append(" Job ");
5540 sb.append(jobs.keyAt(ij));
5541 sb.append(": ");
5542 if (totalTime != 0) {
5543 formatTimeMs(sb, totalTime);
5544 sb.append("realtime (");
5545 sb.append(count);
5546 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07005547 if (bgTime > 0) {
5548 sb.append(", ");
5549 formatTimeMs(sb, bgTime);
5550 sb.append("background (");
5551 sb.append(bgCount);
5552 sb.append(" times)");
5553 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005554 } else {
5555 sb.append("(not used)");
5556 }
5557 pw.println(sb.toString());
5558 uidActivity = true;
5559 }
5560
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005561 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
5562 for (int ic=completions.size()-1; ic>=0; ic--) {
5563 SparseIntArray types = completions.valueAt(ic);
5564 if (types != null) {
5565 pw.print(prefix);
5566 pw.print(" Job Completions ");
5567 pw.print(completions.keyAt(ic));
5568 pw.print(":");
5569 for (int it=0; it<types.size(); it++) {
5570 pw.print(" ");
5571 pw.print(JobParameters.getReasonName(types.keyAt(it)));
5572 pw.print("(");
5573 pw.print(types.valueAt(it));
5574 pw.print("x)");
5575 }
5576 pw.println();
5577 }
5578 }
5579
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005580 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
5581 prefix, "Flashlight");
5582 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
5583 prefix, "Camera");
5584 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
5585 prefix, "Video");
5586 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
5587 prefix, "Audio");
5588
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005589 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
5590 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005591 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005592 final Uid.Sensor se = sensors.valueAt(ise);
5593 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005594 sb.setLength(0);
5595 sb.append(prefix);
5596 sb.append(" Sensor ");
5597 int handle = se.getHandle();
5598 if (handle == Uid.Sensor.GPS) {
5599 sb.append("GPS");
5600 } else {
5601 sb.append(handle);
5602 }
5603 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005604
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005605 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005606 if (timer != null) {
5607 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005608 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5609 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005610 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005611 final Timer bgTimer = se.getSensorBackgroundTime();
5612 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005613 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5614 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5615 final long bgActualTime = bgTimer != null ?
5616 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5617
Dianne Hackborn61659e52014-07-09 16:13:01 -07005618 //timer.logState();
5619 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005620 if (actualTime != totalTime) {
5621 formatTimeMs(sb, totalTime);
5622 sb.append("blamed realtime, ");
5623 }
5624
5625 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005626 sb.append("realtime (");
5627 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005628 sb.append(" times)");
5629
5630 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005631 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005632 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5633 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005634 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005635 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005637 } else {
5638 sb.append("(not used)");
5639 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005640 } else {
5641 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005642 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005643
5644 pw.println(sb.toString());
5645 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005646 }
5647
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005648 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5649 "Vibrator");
5650 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5651 prefix, "Foreground activities");
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005652 uidActivity |= printTimer(pw, sb, u.getForegroundServiceTimer(), rawRealtime, which,
5653 prefix, "Foreground services");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005654
Dianne Hackborn61659e52014-07-09 16:13:01 -07005655 long totalStateTime = 0;
5656 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5657 long time = u.getProcessStateTime(ips, rawRealtime, which);
5658 if (time > 0) {
5659 totalStateTime += time;
5660 sb.setLength(0);
5661 sb.append(prefix);
5662 sb.append(" ");
5663 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5664 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005665 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005666 pw.println(sb.toString());
5667 uidActivity = true;
5668 }
5669 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005670 if (totalStateTime > 0) {
5671 sb.setLength(0);
5672 sb.append(prefix);
5673 sb.append(" Total running: ");
5674 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5675 pw.println(sb.toString());
5676 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005677
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005678 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5679 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005680 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005681 sb.setLength(0);
5682 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005683 sb.append(" Total cpu time: u=");
5684 formatTimeMs(sb, userCpuTimeUs / 1000);
5685 sb.append("s=");
5686 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005687 pw.println(sb.toString());
5688 }
5689
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005690 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5691 if (cpuFreqTimes != null) {
5692 sb.setLength(0);
5693 sb.append(" Total cpu time per freq:");
5694 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5695 sb.append(" " + cpuFreqTimes[i]);
5696 }
5697 pw.println(sb.toString());
5698 }
5699 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5700 if (screenOffCpuFreqTimes != null) {
5701 sb.setLength(0);
5702 sb.append(" Total screen-off cpu time per freq:");
5703 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5704 sb.append(" " + screenOffCpuFreqTimes[i]);
5705 }
5706 pw.println(sb.toString());
5707 }
5708
Sudheer Shankab2f83c12017-11-13 19:25:01 -08005709 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
5710 final long[] cpuTimes = u.getCpuFreqTimes(which, procState);
5711 if (cpuTimes != null) {
5712 sb.setLength(0);
5713 sb.append(" Cpu times per freq at state "
5714 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5715 for (int i = 0; i < cpuTimes.length; ++i) {
5716 sb.append(" " + cpuTimes[i]);
5717 }
5718 pw.println(sb.toString());
5719 }
5720
5721 final long[] screenOffCpuTimes = u.getScreenOffCpuFreqTimes(which, procState);
5722 if (screenOffCpuTimes != null) {
5723 sb.setLength(0);
5724 sb.append(" Screen-off cpu times per freq at state "
5725 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5726 for (int i = 0; i < screenOffCpuTimes.length; ++i) {
5727 sb.append(" " + screenOffCpuTimes[i]);
5728 }
5729 pw.println(sb.toString());
5730 }
5731 }
5732
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005733 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5734 = u.getProcessStats();
5735 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5736 final Uid.Proc ps = processStats.valueAt(ipr);
5737 long userTime;
5738 long systemTime;
5739 long foregroundTime;
5740 int starts;
5741 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005742
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005743 userTime = ps.getUserTime(which);
5744 systemTime = ps.getSystemTime(which);
5745 foregroundTime = ps.getForegroundTime(which);
5746 starts = ps.getStarts(which);
5747 final int numCrashes = ps.getNumCrashes(which);
5748 final int numAnrs = ps.getNumAnrs(which);
5749 numExcessive = which == STATS_SINCE_CHARGED
5750 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005751
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005752 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5753 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5754 sb.setLength(0);
5755 sb.append(prefix); sb.append(" Proc ");
5756 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5757 sb.append(prefix); sb.append(" CPU: ");
5758 formatTimeMs(sb, userTime); sb.append("usr + ");
5759 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5760 formatTimeMs(sb, foregroundTime); sb.append("fg");
5761 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5762 sb.append("\n"); sb.append(prefix); sb.append(" ");
5763 boolean hasOne = false;
5764 if (starts != 0) {
5765 hasOne = true;
5766 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005767 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005768 if (numCrashes != 0) {
5769 if (hasOne) {
5770 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005771 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005772 hasOne = true;
5773 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005774 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005775 if (numAnrs != 0) {
5776 if (hasOne) {
5777 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005778 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005779 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005780 }
5781 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005782 pw.println(sb.toString());
5783 for (int e=0; e<numExcessive; e++) {
5784 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5785 if (ew != null) {
5786 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07005787 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005788 pw.print("cpu");
5789 } else {
5790 pw.print("unknown");
5791 }
5792 pw.print(" use: ");
5793 TimeUtils.formatDuration(ew.usedTime, pw);
5794 pw.print(" over ");
5795 TimeUtils.formatDuration(ew.overTime, pw);
5796 if (ew.overTime != 0) {
5797 pw.print(" (");
5798 pw.print((ew.usedTime*100)/ew.overTime);
5799 pw.println("%)");
5800 }
5801 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005802 }
5803 uidActivity = true;
5804 }
5805 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005806
5807 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5808 = u.getPackageStats();
5809 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
5810 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
5811 pw.println(":");
5812 boolean apkActivity = false;
5813 final Uid.Pkg ps = packageStats.valueAt(ipkg);
5814 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
5815 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
5816 pw.print(prefix); pw.print(" Wakeup alarm ");
5817 pw.print(alarms.keyAt(iwa)); pw.print(": ");
5818 pw.print(alarms.valueAt(iwa).getCountLocked(which));
5819 pw.println(" times");
5820 apkActivity = true;
5821 }
5822 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
5823 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
5824 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
5825 final long startTime = ss.getStartTime(batteryUptime, which);
5826 final int starts = ss.getStarts(which);
5827 final int launches = ss.getLaunches(which);
5828 if (startTime != 0 || starts != 0 || launches != 0) {
5829 sb.setLength(0);
5830 sb.append(prefix); sb.append(" Service ");
5831 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
5832 sb.append(prefix); sb.append(" Created for: ");
5833 formatTimeMs(sb, startTime / 1000);
5834 sb.append("uptime\n");
5835 sb.append(prefix); sb.append(" Starts: ");
5836 sb.append(starts);
5837 sb.append(", launches: "); sb.append(launches);
5838 pw.println(sb.toString());
5839 apkActivity = true;
5840 }
5841 }
5842 if (!apkActivity) {
5843 pw.print(prefix); pw.println(" (nothing executed)");
5844 }
5845 uidActivity = true;
5846 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005847 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005848 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005849 }
5850 }
5851 }
5852
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005853 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005854 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005855 int diff = oldval ^ newval;
5856 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005857 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005858 for (int i=0; i<descriptions.length; i++) {
5859 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005860 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005861 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005862 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005863 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005864 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005865 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
5866 didWake = true;
5867 pw.print("=");
5868 if (longNames) {
5869 UserHandle.formatUid(pw, wakelockTag.uid);
5870 pw.print(":\"");
5871 pw.print(wakelockTag.string);
5872 pw.print("\"");
5873 } else {
5874 pw.print(wakelockTag.poolIdx);
5875 }
5876 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005877 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005878 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005879 pw.print("=");
5880 int val = (newval&bd.mask)>>bd.shift;
5881 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005882 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005883 } else {
5884 pw.print(val);
5885 }
5886 }
5887 }
5888 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005889 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07005890 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005891 if (longNames) {
5892 UserHandle.formatUid(pw, wakelockTag.uid);
5893 pw.print(":\"");
5894 pw.print(wakelockTag.string);
5895 pw.print("\"");
5896 } else {
5897 pw.print(wakelockTag.poolIdx);
5898 }
5899 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005900 }
Mike Mac2f518a2017-09-19 16:06:03 -07005901
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005902 public void prepareForDumpLocked() {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07005903 // We don't need to require subclasses implement this.
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005904 }
5905
5906 public static class HistoryPrinter {
5907 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005908 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005909 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005910 int oldStatus = -1;
5911 int oldHealth = -1;
5912 int oldPlug = -1;
5913 int oldTemp = -1;
5914 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005915 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005916 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005917
Dianne Hackborn3251b902014-06-20 14:40:53 -07005918 void reset() {
5919 oldState = oldState2 = 0;
5920 oldLevel = -1;
5921 oldStatus = -1;
5922 oldHealth = -1;
5923 oldPlug = -1;
5924 oldTemp = -1;
5925 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005926 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07005927 }
5928
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005929 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005930 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005931 if (!checkin) {
5932 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005933 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005934 pw.print(" (");
5935 pw.print(rec.numReadInts);
5936 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005937 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005938 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5939 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005940 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005941 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005942 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005943 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005944 }
5945 lastTime = rec.time;
5946 }
5947 if (rec.cmd == HistoryItem.CMD_START) {
5948 if (checkin) {
5949 pw.print(":");
5950 }
5951 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005952 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005953 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
5954 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005955 if (checkin) {
5956 pw.print(":");
5957 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005958 if (rec.cmd == HistoryItem.CMD_RESET) {
5959 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005960 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005961 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005962 pw.print("TIME:");
5963 if (checkin) {
5964 pw.println(rec.currentTime);
5965 } else {
5966 pw.print(" ");
5967 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5968 rec.currentTime).toString());
5969 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005970 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
5971 if (checkin) {
5972 pw.print(":");
5973 }
5974 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005975 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
5976 if (checkin) {
5977 pw.print(":");
5978 }
5979 pw.println("*OVERFLOW*");
5980 } else {
5981 if (!checkin) {
5982 if (rec.batteryLevel < 10) pw.print("00");
5983 else if (rec.batteryLevel < 100) pw.print("0");
5984 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005985 if (verbose) {
5986 pw.print(" ");
5987 if (rec.states < 0) ;
5988 else if (rec.states < 0x10) pw.print("0000000");
5989 else if (rec.states < 0x100) pw.print("000000");
5990 else if (rec.states < 0x1000) pw.print("00000");
5991 else if (rec.states < 0x10000) pw.print("0000");
5992 else if (rec.states < 0x100000) pw.print("000");
5993 else if (rec.states < 0x1000000) pw.print("00");
5994 else if (rec.states < 0x10000000) pw.print("0");
5995 pw.print(Integer.toHexString(rec.states));
5996 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005997 } else {
5998 if (oldLevel != rec.batteryLevel) {
5999 oldLevel = rec.batteryLevel;
6000 pw.print(",Bl="); pw.print(rec.batteryLevel);
6001 }
6002 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006003 if (oldStatus != rec.batteryStatus) {
6004 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006005 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006006 switch (oldStatus) {
6007 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006008 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006009 break;
6010 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006011 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006012 break;
6013 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006014 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006015 break;
6016 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006017 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006018 break;
6019 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006020 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006021 break;
6022 default:
6023 pw.print(oldStatus);
6024 break;
6025 }
6026 }
6027 if (oldHealth != rec.batteryHealth) {
6028 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006029 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006030 switch (oldHealth) {
6031 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006032 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006033 break;
6034 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006035 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006036 break;
6037 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006038 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006039 break;
6040 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006041 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006042 break;
6043 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006044 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006045 break;
6046 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006047 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006048 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006049 case BatteryManager.BATTERY_HEALTH_COLD:
6050 pw.print(checkin ? "c" : "cold");
6051 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006052 default:
6053 pw.print(oldHealth);
6054 break;
6055 }
6056 }
6057 if (oldPlug != rec.batteryPlugType) {
6058 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006059 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006060 switch (oldPlug) {
6061 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006062 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006063 break;
6064 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006065 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006066 break;
6067 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006068 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006069 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006070 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006071 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006072 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006073 default:
6074 pw.print(oldPlug);
6075 break;
6076 }
6077 }
6078 if (oldTemp != rec.batteryTemperature) {
6079 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006080 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006081 pw.print(oldTemp);
6082 }
6083 if (oldVolt != rec.batteryVoltage) {
6084 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006085 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006086 pw.print(oldVolt);
6087 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006088 final int chargeMAh = rec.batteryChargeUAh / 1000;
6089 if (oldChargeMAh != chargeMAh) {
6090 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07006091 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006092 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07006093 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006094 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006095 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006096 printBitDescriptions(pw, oldState2, rec.states2, null,
6097 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006098 if (rec.wakeReasonTag != null) {
6099 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006100 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006101 pw.print(rec.wakeReasonTag.poolIdx);
6102 } else {
6103 pw.print(" wake_reason=");
6104 pw.print(rec.wakeReasonTag.uid);
6105 pw.print(":\"");
6106 pw.print(rec.wakeReasonTag.string);
6107 pw.print("\"");
6108 }
6109 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08006110 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006111 pw.print(checkin ? "," : " ");
6112 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
6113 pw.print("+");
6114 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
6115 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006116 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006117 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
6118 : HISTORY_EVENT_NAMES;
6119 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
6120 | HistoryItem.EVENT_FLAG_FINISH);
6121 if (idx >= 0 && idx < eventNames.length) {
6122 pw.print(eventNames[idx]);
6123 } else {
6124 pw.print(checkin ? "Ev" : "event");
6125 pw.print(idx);
6126 }
6127 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006128 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006129 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08006130 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08006131 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
6132 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006133 pw.print(":\"");
6134 pw.print(rec.eventTag.string);
6135 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006136 }
6137 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006138 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006139 if (rec.stepDetails != null) {
6140 if (!checkin) {
6141 pw.print(" Details: cpu=");
6142 pw.print(rec.stepDetails.userTime);
6143 pw.print("u+");
6144 pw.print(rec.stepDetails.systemTime);
6145 pw.print("s");
6146 if (rec.stepDetails.appCpuUid1 >= 0) {
6147 pw.print(" (");
6148 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
6149 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6150 if (rec.stepDetails.appCpuUid2 >= 0) {
6151 pw.print(", ");
6152 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
6153 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6154 }
6155 if (rec.stepDetails.appCpuUid3 >= 0) {
6156 pw.print(", ");
6157 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
6158 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6159 }
6160 pw.print(')');
6161 }
6162 pw.println();
6163 pw.print(" /proc/stat=");
6164 pw.print(rec.stepDetails.statUserTime);
6165 pw.print(" usr, ");
6166 pw.print(rec.stepDetails.statSystemTime);
6167 pw.print(" sys, ");
6168 pw.print(rec.stepDetails.statIOWaitTime);
6169 pw.print(" io, ");
6170 pw.print(rec.stepDetails.statIrqTime);
6171 pw.print(" irq, ");
6172 pw.print(rec.stepDetails.statSoftIrqTime);
6173 pw.print(" sirq, ");
6174 pw.print(rec.stepDetails.statIdlTime);
6175 pw.print(" idle");
6176 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
6177 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
6178 + rec.stepDetails.statSoftIrqTime;
6179 int total = totalRun + rec.stepDetails.statIdlTime;
6180 if (total > 0) {
6181 pw.print(" (");
6182 float perc = ((float)totalRun) / ((float)total) * 100;
6183 pw.print(String.format("%.1f%%", perc));
6184 pw.print(" of ");
6185 StringBuilder sb = new StringBuilder(64);
6186 formatTimeMsNoSpace(sb, total*10);
6187 pw.print(sb);
6188 pw.print(")");
6189 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006190 pw.print(", PlatformIdleStat ");
6191 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006192 pw.println();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006193
6194 pw.print(", SubsystemPowerState ");
6195 pw.print(rec.stepDetails.statSubsystemPowerState);
6196 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006197 } else {
6198 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6199 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
6200 pw.print(rec.stepDetails.userTime);
6201 pw.print(":");
6202 pw.print(rec.stepDetails.systemTime);
6203 if (rec.stepDetails.appCpuUid1 >= 0) {
6204 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
6205 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6206 if (rec.stepDetails.appCpuUid2 >= 0) {
6207 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
6208 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6209 }
6210 if (rec.stepDetails.appCpuUid3 >= 0) {
6211 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
6212 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6213 }
6214 }
6215 pw.println();
6216 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6217 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
6218 pw.print(rec.stepDetails.statUserTime);
6219 pw.print(',');
6220 pw.print(rec.stepDetails.statSystemTime);
6221 pw.print(',');
6222 pw.print(rec.stepDetails.statIOWaitTime);
6223 pw.print(',');
6224 pw.print(rec.stepDetails.statIrqTime);
6225 pw.print(',');
6226 pw.print(rec.stepDetails.statSoftIrqTime);
6227 pw.print(',');
6228 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006229 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006230 if (rec.stepDetails.statPlatformIdleState != null) {
6231 pw.print(rec.stepDetails.statPlatformIdleState);
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006232 if (rec.stepDetails.statSubsystemPowerState != null) {
6233 pw.print(',');
6234 }
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006235 }
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006236
6237 if (rec.stepDetails.statSubsystemPowerState != null) {
6238 pw.print(rec.stepDetails.statSubsystemPowerState);
6239 }
6240 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006241 }
6242 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006243 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006244 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006245 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006246 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006247
6248 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
6249 UserHandle.formatUid(pw, uid);
6250 pw.print("=");
6251 pw.print(utime);
6252 pw.print("u+");
6253 pw.print(stime);
6254 pw.print("s");
6255 }
6256
6257 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
6258 pw.print('/');
6259 pw.print(uid);
6260 pw.print(":");
6261 pw.print(utime);
6262 pw.print(":");
6263 pw.print(stime);
6264 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006265 }
6266
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006267 private void printSizeValue(PrintWriter pw, long size) {
6268 float result = size;
6269 String suffix = "";
6270 if (result >= 10*1024) {
6271 suffix = "KB";
6272 result = result / 1024;
6273 }
6274 if (result >= 10*1024) {
6275 suffix = "MB";
6276 result = result / 1024;
6277 }
6278 if (result >= 10*1024) {
6279 suffix = "GB";
6280 result = result / 1024;
6281 }
6282 if (result >= 10*1024) {
6283 suffix = "TB";
6284 result = result / 1024;
6285 }
6286 if (result >= 10*1024) {
6287 suffix = "PB";
6288 result = result / 1024;
6289 }
6290 pw.print((int)result);
6291 pw.print(suffix);
6292 }
6293
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006294 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
6295 String label3, long estimatedTime) {
6296 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006297 return false;
6298 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006299 pw.print(label1);
6300 pw.print(label2);
6301 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006302 StringBuilder sb = new StringBuilder(64);
6303 formatTimeMs(sb, estimatedTime);
6304 pw.print(sb);
6305 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006306 return true;
6307 }
6308
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006309 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
6310 LevelStepTracker steps, boolean checkin) {
6311 if (steps == null) {
6312 return false;
6313 }
6314 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006315 if (count <= 0) {
6316 return false;
6317 }
6318 if (!checkin) {
6319 pw.println(header);
6320 }
Kweku Adams030980a2015-04-01 16:07:48 -07006321 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006322 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006323 long duration = steps.getDurationAt(i);
6324 int level = steps.getLevelAt(i);
6325 long initMode = steps.getInitModeAt(i);
6326 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006327 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006328 lineArgs[0] = Long.toString(duration);
6329 lineArgs[1] = Integer.toString(level);
6330 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6331 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6332 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
6333 case Display.STATE_ON: lineArgs[2] = "s+"; break;
6334 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
6335 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07006336 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006337 }
6338 } else {
6339 lineArgs[2] = "";
6340 }
6341 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6342 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
6343 } else {
6344 lineArgs[3] = "";
6345 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006346 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07006347 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006348 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07006349 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006350 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006351 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
6352 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006353 pw.print(prefix);
6354 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006355 TimeUtils.formatDuration(duration, pw);
6356 pw.print(" to "); pw.print(level);
6357 boolean haveModes = false;
6358 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6359 pw.print(" (");
6360 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6361 case Display.STATE_OFF: pw.print("screen-off"); break;
6362 case Display.STATE_ON: pw.print("screen-on"); break;
6363 case Display.STATE_DOZE: pw.print("screen-doze"); break;
6364 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07006365 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006366 }
6367 haveModes = true;
6368 }
6369 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6370 pw.print(haveModes ? ", " : " (");
6371 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
6372 ? "power-save-on" : "power-save-off");
6373 haveModes = true;
6374 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006375 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6376 pw.print(haveModes ? ", " : " (");
6377 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6378 ? "device-idle-on" : "device-idle-off");
6379 haveModes = true;
6380 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006381 if (haveModes) {
6382 pw.print(")");
6383 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006384 pw.println();
6385 }
6386 }
6387 return true;
6388 }
6389
Kweku Adams87b19ec2017-10-09 12:40:03 -07006390 private static void dumpDurationSteps(ProtoOutputStream proto, long fieldId,
6391 LevelStepTracker steps) {
6392 if (steps == null) {
6393 return;
6394 }
6395 int count = steps.mNumStepDurations;
Kweku Adams87b19ec2017-10-09 12:40:03 -07006396 for (int i = 0; i < count; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07006397 long token = proto.start(fieldId);
Kweku Adams87b19ec2017-10-09 12:40:03 -07006398 proto.write(SystemProto.BatteryLevelStep.DURATION_MS, steps.getDurationAt(i));
6399 proto.write(SystemProto.BatteryLevelStep.LEVEL, steps.getLevelAt(i));
6400
6401 final long initMode = steps.getInitModeAt(i);
6402 final long modMode = steps.getModModeAt(i);
6403
6404 int ds = SystemProto.BatteryLevelStep.DS_MIXED;
6405 if ((modMode & STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6406 switch ((int) (initMode & STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6407 case Display.STATE_OFF:
6408 ds = SystemProto.BatteryLevelStep.DS_OFF;
6409 break;
6410 case Display.STATE_ON:
6411 ds = SystemProto.BatteryLevelStep.DS_ON;
6412 break;
6413 case Display.STATE_DOZE:
6414 ds = SystemProto.BatteryLevelStep.DS_DOZE;
6415 break;
6416 case Display.STATE_DOZE_SUSPEND:
6417 ds = SystemProto.BatteryLevelStep.DS_DOZE_SUSPEND;
6418 break;
6419 default:
6420 ds = SystemProto.BatteryLevelStep.DS_ERROR;
6421 break;
6422 }
6423 }
6424 proto.write(SystemProto.BatteryLevelStep.DISPLAY_STATE, ds);
6425
6426 int psm = SystemProto.BatteryLevelStep.PSM_MIXED;
6427 if ((modMode & STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6428 psm = (initMode & STEP_LEVEL_MODE_POWER_SAVE) != 0
6429 ? SystemProto.BatteryLevelStep.PSM_ON : SystemProto.BatteryLevelStep.PSM_OFF;
6430 }
6431 proto.write(SystemProto.BatteryLevelStep.POWER_SAVE_MODE, psm);
6432
6433 int im = SystemProto.BatteryLevelStep.IM_MIXED;
6434 if ((modMode & STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6435 im = (initMode & STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6436 ? SystemProto.BatteryLevelStep.IM_ON : SystemProto.BatteryLevelStep.IM_OFF;
6437 }
6438 proto.write(SystemProto.BatteryLevelStep.IDLE_MODE, im);
6439
6440 proto.end(token);
6441 }
6442 }
6443
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006444 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006445 public static final int DUMP_DAILY_ONLY = 1<<2;
6446 public static final int DUMP_HISTORY_ONLY = 1<<3;
6447 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
6448 public static final int DUMP_VERBOSE = 1<<5;
6449 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006450
Dianne Hackborn37de0982014-05-09 09:32:18 -07006451 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
6452 final HistoryPrinter hprinter = new HistoryPrinter();
6453 final HistoryItem rec = new HistoryItem();
6454 long lastTime = -1;
6455 long baseTime = -1;
6456 boolean printed = false;
6457 HistoryEventTracker tracker = null;
6458 while (getNextHistoryLocked(rec)) {
6459 lastTime = rec.time;
6460 if (baseTime < 0) {
6461 baseTime = lastTime;
6462 }
6463 if (rec.time >= histStart) {
6464 if (histStart >= 0 && !printed) {
6465 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07006466 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006467 || rec.cmd == HistoryItem.CMD_START
6468 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006469 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006470 hprinter.printNextItem(pw, rec, baseTime, checkin,
6471 (flags&DUMP_VERBOSE) != 0);
6472 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006473 } else if (rec.currentTime != 0) {
6474 printed = true;
6475 byte cmd = rec.cmd;
6476 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006477 hprinter.printNextItem(pw, rec, baseTime, checkin,
6478 (flags&DUMP_VERBOSE) != 0);
6479 rec.cmd = cmd;
6480 }
6481 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006482 if (rec.cmd != HistoryItem.CMD_UPDATE) {
6483 hprinter.printNextItem(pw, rec, baseTime, checkin,
6484 (flags&DUMP_VERBOSE) != 0);
6485 rec.cmd = HistoryItem.CMD_UPDATE;
6486 }
6487 int oldEventCode = rec.eventCode;
6488 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006489 rec.eventTag = new HistoryTag();
6490 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
6491 HashMap<String, SparseIntArray> active
6492 = tracker.getStateForEvent(i);
6493 if (active == null) {
6494 continue;
6495 }
6496 for (HashMap.Entry<String, SparseIntArray> ent
6497 : active.entrySet()) {
6498 SparseIntArray uids = ent.getValue();
6499 for (int j=0; j<uids.size(); j++) {
6500 rec.eventCode = i;
6501 rec.eventTag.string = ent.getKey();
6502 rec.eventTag.uid = uids.keyAt(j);
6503 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006504 hprinter.printNextItem(pw, rec, baseTime, checkin,
6505 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006506 rec.wakeReasonTag = null;
6507 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006508 }
6509 }
6510 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006511 rec.eventCode = oldEventCode;
6512 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006513 tracker = null;
6514 }
6515 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006516 hprinter.printNextItem(pw, rec, baseTime, checkin,
6517 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07006518 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
6519 // This is an attempt to aggregate the previous state and generate
6520 // fake events to reflect that state at the point where we start
6521 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07006522 if (tracker == null) {
6523 tracker = new HistoryEventTracker();
6524 }
6525 tracker.updateState(rec.eventCode, rec.eventTag.string,
6526 rec.eventTag.uid, rec.eventTag.poolIdx);
6527 }
6528 }
6529 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07006530 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006531 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
6532 }
6533 }
6534
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006535 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
6536 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
6537 if (steps == null) {
6538 return;
6539 }
6540 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
6541 if (timeRemaining >= 0) {
6542 pw.print(prefix); pw.print(label); pw.print(" total time: ");
6543 tmpSb.setLength(0);
6544 formatTimeMs(tmpSb, timeRemaining);
6545 pw.print(tmpSb);
6546 pw.print(" (from "); pw.print(tmpOutInt[0]);
6547 pw.println(" steps)");
6548 }
6549 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6550 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6551 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
6552 if (estimatedTime > 0) {
6553 pw.print(prefix); pw.print(label); pw.print(" ");
6554 pw.print(STEP_LEVEL_MODE_LABELS[i]);
6555 pw.print(" time: ");
6556 tmpSb.setLength(0);
6557 formatTimeMs(tmpSb, estimatedTime);
6558 pw.print(tmpSb);
6559 pw.print(" (from "); pw.print(tmpOutInt[0]);
6560 pw.println(" steps)");
6561 }
6562 }
6563 }
6564
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006565 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
6566 ArrayList<PackageChange> changes) {
6567 if (changes == null) {
6568 return;
6569 }
6570 pw.print(prefix); pw.println("Package changes:");
6571 for (int i=0; i<changes.size(); i++) {
6572 PackageChange pc = changes.get(i);
6573 if (pc.mUpdate) {
6574 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
6575 pw.print(" vers="); pw.println(pc.mVersionCode);
6576 } else {
6577 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
6578 }
6579 }
6580 }
6581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006582 /**
6583 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
6584 *
6585 * @param pw a Printer to receive the dump output.
6586 */
6587 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006588 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006589 prepareForDumpLocked();
6590
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006591 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006592 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006593
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006594 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006595 final long historyTotalSize = getHistoryTotalSize();
6596 final long historyUsedSize = getHistoryUsedSize();
6597 if (startIteratingHistoryLocked()) {
6598 try {
6599 pw.print("Battery History (");
6600 pw.print((100*historyUsedSize)/historyTotalSize);
6601 pw.print("% used, ");
6602 printSizeValue(pw, historyUsedSize);
6603 pw.print(" used of ");
6604 printSizeValue(pw, historyTotalSize);
6605 pw.print(", ");
6606 pw.print(getHistoryStringPoolSize());
6607 pw.print(" strings using ");
6608 printSizeValue(pw, getHistoryStringPoolBytes());
6609 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07006610 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006611 pw.println();
6612 } finally {
6613 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006614 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006615 }
6616
6617 if (startIteratingOldHistoryLocked()) {
6618 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006619 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006620 pw.println("Old battery History:");
6621 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006622 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006623 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006624 if (baseTime < 0) {
6625 baseTime = rec.time;
6626 }
6627 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006628 }
6629 pw.println();
6630 } finally {
6631 finishIteratingOldHistoryLocked();
6632 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006633 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006634 }
6635
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006636 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006637 return;
6638 }
6639
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006640 if (!filtering) {
6641 SparseArray<? extends Uid> uidStats = getUidStats();
6642 final int NU = uidStats.size();
6643 boolean didPid = false;
6644 long nowRealtime = SystemClock.elapsedRealtime();
6645 for (int i=0; i<NU; i++) {
6646 Uid uid = uidStats.valueAt(i);
6647 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
6648 if (pids != null) {
6649 for (int j=0; j<pids.size(); j++) {
6650 Uid.Pid pid = pids.valueAt(j);
6651 if (!didPid) {
6652 pw.println("Per-PID Stats:");
6653 didPid = true;
6654 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006655 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
6656 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006657 pw.print(" PID "); pw.print(pids.keyAt(j));
6658 pw.print(" wake time: ");
6659 TimeUtils.formatDuration(time, pw);
6660 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006661 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006662 }
6663 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006664 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006665 pw.println();
6666 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006667 }
6668
6669 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006670 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
6671 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006672 long timeRemaining = computeBatteryTimeRemaining(
6673 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006674 if (timeRemaining >= 0) {
6675 pw.print(" Estimated discharge time remaining: ");
6676 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6677 pw.println();
6678 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006679 final LevelStepTracker steps = getDischargeLevelStepTracker();
6680 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6681 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
6682 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6683 STEP_LEVEL_MODE_VALUES[i], null));
6684 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006685 pw.println();
6686 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006687 if (dumpDurationSteps(pw, " ", "Charge step durations:",
6688 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006689 long timeRemaining = computeChargeTimeRemaining(
6690 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006691 if (timeRemaining >= 0) {
6692 pw.print(" Estimated charge time remaining: ");
6693 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6694 pw.println();
6695 }
6696 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006697 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006698 }
Dianne Hackbornc81983a2017-10-20 16:16:32 -07006699 if (!filtering || (flags & DUMP_DAILY_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006700 pw.println("Daily stats:");
6701 pw.print(" Current start time: ");
6702 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6703 getCurrentDailyStartTime()).toString());
6704 pw.print(" Next min deadline: ");
6705 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6706 getNextMinDailyDeadline()).toString());
6707 pw.print(" Next max deadline: ");
6708 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6709 getNextMaxDailyDeadline()).toString());
6710 StringBuilder sb = new StringBuilder(64);
6711 int[] outInt = new int[1];
6712 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
6713 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006714 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
6715 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006716 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006717 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
6718 dsteps, false)) {
6719 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6720 sb, outInt);
6721 }
6722 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6723 csteps, false)) {
6724 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6725 sb, outInt);
6726 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006727 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006728 } else {
6729 pw.println(" Current daily steps:");
6730 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6731 sb, outInt);
6732 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6733 sb, outInt);
6734 }
6735 }
6736 DailyItem dit;
6737 int curIndex = 0;
6738 while ((dit=getDailyItemLocked(curIndex)) != null) {
6739 curIndex++;
6740 if ((flags&DUMP_DAILY_ONLY) != 0) {
6741 pw.println();
6742 }
6743 pw.print(" Daily from ");
6744 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6745 pw.print(" to ");
6746 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6747 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006748 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006749 if (dumpDurationSteps(pw, " ",
6750 " Discharge step durations:", dit.mDischargeSteps, false)) {
6751 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6752 sb, outInt);
6753 }
6754 if (dumpDurationSteps(pw, " ",
6755 " Charge step durations:", dit.mChargeSteps, false)) {
6756 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6757 sb, outInt);
6758 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006759 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006760 } else {
6761 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6762 sb, outInt);
6763 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6764 sb, outInt);
6765 }
6766 }
6767 pw.println();
6768 }
6769 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006770 pw.println("Statistics since last charge:");
6771 pw.println(" System starts: " + getStartCount()
6772 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006773 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6774 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006775 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006777 }
Mike Mac2f518a2017-09-19 16:06:03 -07006778
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006779 // This is called from BatteryStatsService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006780 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006781 public void dumpCheckinLocked(Context context, PrintWriter pw,
6782 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006783 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006784
6785 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006786 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6787 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006788
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006789 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6790
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006791 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07006792 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006793 try {
6794 for (int i=0; i<getHistoryStringPoolSize(); i++) {
6795 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6796 pw.print(HISTORY_STRING_POOL); pw.print(',');
6797 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006798 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006799 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006800 pw.print(",\"");
6801 String str = getHistoryTagPoolString(i);
6802 str = str.replace("\\", "\\\\");
6803 str = str.replace("\"", "\\\"");
6804 pw.print(str);
6805 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006806 pw.println();
6807 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006808 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006809 } finally {
6810 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08006811 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006812 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006813 }
6814
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006815 if ((flags & DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006816 return;
6817 }
6818
Dianne Hackborne4a59512010-12-07 11:08:07 -08006819 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006820 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08006821 for (int i=0; i<apps.size(); i++) {
6822 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006823 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
6824 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08006825 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006826 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
6827 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006828 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006829 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006830 }
6831 SparseArray<? extends Uid> uidStats = getUidStats();
6832 final int NU = uidStats.size();
6833 String[] lineArgs = new String[2];
6834 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006835 int uid = UserHandle.getAppId(uidStats.keyAt(i));
6836 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
6837 if (pkgs != null && !pkgs.second.value) {
6838 pkgs.second.value = true;
6839 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08006840 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006841 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006842 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
6843 (Object[])lineArgs);
6844 }
6845 }
6846 }
6847 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006848 if ((flags & DUMP_DAILY_ONLY) == 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006849 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006850 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07006851 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006852 if (timeRemaining >= 0) {
6853 lineArgs[0] = Long.toString(timeRemaining);
6854 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
6855 (Object[])lineArgs);
6856 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006857 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07006858 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006859 if (timeRemaining >= 0) {
6860 lineArgs[0] = Long.toString(timeRemaining);
6861 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
6862 (Object[])lineArgs);
6863 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006864 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
6865 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006867 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006868
Kweku Adams87b19ec2017-10-09 12:40:03 -07006869 /** Dump #STATS_SINCE_CHARGED batterystats data to a proto. @hide */
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006870 public void dumpProtoLocked(Context context, FileDescriptor fd, List<ApplicationInfo> apps,
Kweku Adams6ccebf22017-12-11 12:30:35 -08006871 int flags) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006872 final ProtoOutputStream proto = new ProtoOutputStream(fd);
6873 final long bToken = proto.start(BatteryStatsServiceDumpProto.BATTERYSTATS);
6874 prepareForDumpLocked();
6875
6876 proto.write(BatteryStatsProto.REPORT_VERSION, CHECKIN_VERSION);
6877 proto.write(BatteryStatsProto.PARCEL_VERSION, getParcelVersion());
6878 proto.write(BatteryStatsProto.START_PLATFORM_VERSION, getStartPlatformVersion());
6879 proto.write(BatteryStatsProto.END_PLATFORM_VERSION, getEndPlatformVersion());
6880
Kweku Adams6ccebf22017-12-11 12:30:35 -08006881 // History intentionally not included in proto dump.
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006882
6883 if ((flags & (DUMP_HISTORY_ONLY | DUMP_DAILY_ONLY)) == 0) {
Kweku Adams103351f2017-10-16 14:39:34 -07006884 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false,
6885 (flags & DUMP_DEVICE_WIFI_ONLY) != 0);
6886 helper.create(this);
6887 helper.refreshStats(STATS_SINCE_CHARGED, UserHandle.USER_ALL);
6888
6889 dumpProtoAppsLocked(proto, helper, apps);
6890 dumpProtoSystemLocked(proto, helper);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006891 }
6892
6893 proto.end(bToken);
6894 proto.flush();
6895 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07006896
Kweku Adams103351f2017-10-16 14:39:34 -07006897 private void dumpProtoAppsLocked(ProtoOutputStream proto, BatteryStatsHelper helper,
6898 List<ApplicationInfo> apps) {
6899 final int which = STATS_SINCE_CHARGED;
6900 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
6901 final long rawRealtimeMs = SystemClock.elapsedRealtime();
6902 final long rawRealtimeUs = rawRealtimeMs * 1000;
6903 final long batteryUptimeUs = getBatteryUptime(rawUptimeUs);
6904
6905 SparseArray<ArrayList<String>> aidToPackages = new SparseArray<>();
6906 if (apps != null) {
6907 for (int i = 0; i < apps.size(); ++i) {
6908 ApplicationInfo ai = apps.get(i);
6909 int aid = UserHandle.getAppId(ai.uid);
6910 ArrayList<String> pkgs = aidToPackages.get(aid);
6911 if (pkgs == null) {
6912 pkgs = new ArrayList<String>();
6913 aidToPackages.put(aid, pkgs);
6914 }
6915 pkgs.add(ai.packageName);
6916 }
6917 }
6918
6919 SparseArray<BatterySipper> uidToSipper = new SparseArray<>();
6920 final List<BatterySipper> sippers = helper.getUsageList();
6921 if (sippers != null) {
6922 for (int i = 0; i < sippers.size(); ++i) {
6923 final BatterySipper bs = sippers.get(i);
6924 if (bs.drainType != BatterySipper.DrainType.APP) {
6925 // Others are handled by dumpProtoSystemLocked()
6926 continue;
6927 }
6928 uidToSipper.put(bs.uidObj.getUid(), bs);
6929 }
6930 }
6931
6932 SparseArray<? extends Uid> uidStats = getUidStats();
6933 final int n = uidStats.size();
6934 for (int iu = 0; iu < n; ++iu) {
6935 final long uTkn = proto.start(BatteryStatsProto.UIDS);
6936 final Uid u = uidStats.valueAt(iu);
6937
6938 final int uid = uidStats.keyAt(iu);
6939 proto.write(UidProto.UID, uid);
6940
6941 // Print packages and apk stats (UID_DATA & APK_DATA)
6942 ArrayList<String> pkgs = aidToPackages.get(UserHandle.getAppId(uid));
6943 if (pkgs == null) {
6944 pkgs = new ArrayList<String>();
6945 }
6946 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats =
6947 u.getPackageStats();
6948 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
6949 String pkg = packageStats.keyAt(ipkg);
6950 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats =
6951 packageStats.valueAt(ipkg).getServiceStats();
6952 if (serviceStats.size() == 0) {
6953 // Due to the way ActivityManagerService logs wakeup alarms, some packages (for
6954 // example, "android") may be included in the packageStats that aren't part of
6955 // the UID. If they don't have any services, then they shouldn't be listed here.
6956 // These packages won't be a part in the pkgs List.
6957 continue;
6958 }
6959
6960 final long pToken = proto.start(UidProto.PACKAGES);
6961 proto.write(UidProto.Package.NAME, pkg);
6962 // Remove from the packages list since we're logging it here.
6963 pkgs.remove(pkg);
6964
6965 for (int isvc = serviceStats.size() - 1; isvc >= 0; --isvc) {
6966 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
6967 long sToken = proto.start(UidProto.Package.SERVICES);
6968
6969 proto.write(UidProto.Package.Service.NAME, serviceStats.keyAt(isvc));
6970 proto.write(UidProto.Package.Service.START_DURATION_MS,
6971 roundUsToMs(ss.getStartTime(batteryUptimeUs, which)));
6972 proto.write(UidProto.Package.Service.START_COUNT, ss.getStarts(which));
6973 proto.write(UidProto.Package.Service.LAUNCH_COUNT, ss.getLaunches(which));
6974
6975 proto.end(sToken);
6976 }
6977 proto.end(pToken);
6978 }
6979 // Print any remaining packages that weren't in the packageStats map. pkgs is pulled
6980 // from PackageManager data. Packages are only included in packageStats if there was
6981 // specific data tracked for them (services and wakeup alarms, etc.).
6982 for (String p : pkgs) {
6983 final long pToken = proto.start(UidProto.PACKAGES);
6984 proto.write(UidProto.Package.NAME, p);
6985 proto.end(pToken);
6986 }
6987
6988 // Total wakelock data (AGGREGATED_WAKELOCK_DATA)
6989 if (u.getAggregatedPartialWakelockTimer() != null) {
6990 final Timer timer = u.getAggregatedPartialWakelockTimer();
6991 // Times are since reset (regardless of 'which')
6992 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
6993 final Timer bgTimer = timer.getSubTimer();
6994 final long bgTimeMs = bgTimer != null
6995 ? bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
6996 final long awToken = proto.start(UidProto.AGGREGATED_WAKELOCK);
6997 proto.write(UidProto.AggregatedWakelock.PARTIAL_DURATION_MS, totTimeMs);
6998 proto.write(UidProto.AggregatedWakelock.BACKGROUND_PARTIAL_DURATION_MS, bgTimeMs);
6999 proto.end(awToken);
7000 }
7001
7002 // Audio (AUDIO_DATA)
7003 dumpTimer(proto, UidProto.AUDIO, u.getAudioTurnedOnTimer(), rawRealtimeUs, which);
7004
7005 // Bluetooth Controller (BLUETOOTH_CONTROLLER_DATA)
7006 dumpControllerActivityProto(proto, UidProto.BLUETOOTH_CONTROLLER,
7007 u.getBluetoothControllerActivity(), which);
7008
7009 // BLE scans (BLUETOOTH_MISC_DATA) (uses totalDurationMsLocked and MaxDurationMsLocked)
7010 final Timer bleTimer = u.getBluetoothScanTimer();
7011 if (bleTimer != null) {
7012 final long bmToken = proto.start(UidProto.BLUETOOTH_MISC);
7013
7014 dumpTimer(proto, UidProto.BluetoothMisc.APPORTIONED_BLE_SCAN, bleTimer,
7015 rawRealtimeUs, which);
7016 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN,
7017 u.getBluetoothScanBackgroundTimer(), rawRealtimeUs, which);
7018 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
7019 dumpTimer(proto, UidProto.BluetoothMisc.UNOPTIMIZED_BLE_SCAN,
7020 u.getBluetoothUnoptimizedScanTimer(), rawRealtimeUs, which);
7021 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
7022 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_UNOPTIMIZED_BLE_SCAN,
7023 u.getBluetoothUnoptimizedScanBackgroundTimer(), rawRealtimeUs, which);
7024 // Result counters
7025 proto.write(UidProto.BluetoothMisc.BLE_SCAN_RESULT_COUNT,
7026 u.getBluetoothScanResultCounter() != null
7027 ? u.getBluetoothScanResultCounter().getCountLocked(which) : 0);
7028 proto.write(UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN_RESULT_COUNT,
7029 u.getBluetoothScanResultBgCounter() != null
7030 ? u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0);
7031
7032 proto.end(bmToken);
7033 }
7034
7035 // Camera (CAMERA_DATA)
7036 dumpTimer(proto, UidProto.CAMERA, u.getCameraTurnedOnTimer(), rawRealtimeUs, which);
7037
7038 // CPU stats (CPU_DATA & CPU_TIMES_AT_FREQ_DATA)
7039 final long cpuToken = proto.start(UidProto.CPU);
7040 proto.write(UidProto.Cpu.USER_DURATION_MS, roundUsToMs(u.getUserCpuTimeUs(which)));
7041 proto.write(UidProto.Cpu.SYSTEM_DURATION_MS, roundUsToMs(u.getSystemCpuTimeUs(which)));
7042
7043 final long[] cpuFreqs = getCpuFreqs();
7044 if (cpuFreqs != null) {
7045 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
7046 // If total cpuFreqTimes is null, then we don't need to check for
7047 // screenOffCpuFreqTimes.
7048 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
7049 long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
7050 if (screenOffCpuFreqTimeMs == null) {
7051 screenOffCpuFreqTimeMs = new long[cpuFreqTimeMs.length];
7052 }
7053 for (int ic = 0; ic < cpuFreqTimeMs.length; ++ic) {
7054 long cToken = proto.start(UidProto.Cpu.BY_FREQUENCY);
7055 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7056 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7057 cpuFreqTimeMs[ic]);
7058 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7059 screenOffCpuFreqTimeMs[ic]);
7060 proto.end(cToken);
7061 }
7062 }
7063 }
Sudheer Shanka6d658d72018-01-01 01:36:49 -08007064
7065 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
7066 final long[] timesMs = u.getCpuFreqTimes(which, procState);
7067 if (timesMs != null && timesMs.length == cpuFreqs.length) {
7068 long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(which, procState);
7069 if (screenOffTimesMs == null) {
7070 screenOffTimesMs = new long[timesMs.length];
7071 }
7072 final long procToken = proto.start(UidProto.Cpu.BY_PROCESS_STATE);
7073 proto.write(UidProto.Cpu.ByProcessState.PROCESS_STATE, procState);
7074 for (int ic = 0; ic < timesMs.length; ++ic) {
7075 long cToken = proto.start(UidProto.Cpu.ByProcessState.BY_FREQUENCY);
7076 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7077 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7078 timesMs[ic]);
7079 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7080 screenOffTimesMs[ic]);
7081 proto.end(cToken);
7082 }
7083 proto.end(procToken);
7084 }
7085 }
Kweku Adams103351f2017-10-16 14:39:34 -07007086 proto.end(cpuToken);
7087
7088 // Flashlight (FLASHLIGHT_DATA)
7089 dumpTimer(proto, UidProto.FLASHLIGHT, u.getFlashlightTurnedOnTimer(),
7090 rawRealtimeUs, which);
7091
7092 // Foreground activity (FOREGROUND_ACTIVITY_DATA)
7093 dumpTimer(proto, UidProto.FOREGROUND_ACTIVITY, u.getForegroundActivityTimer(),
7094 rawRealtimeUs, which);
7095
7096 // Foreground service (FOREGROUND_SERVICE_DATA)
7097 dumpTimer(proto, UidProto.FOREGROUND_SERVICE, u.getForegroundServiceTimer(),
7098 rawRealtimeUs, which);
7099
7100 // Job completion (JOB_COMPLETION_DATA)
7101 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
7102 final int[] reasons = new int[]{
7103 JobParameters.REASON_CANCELED,
7104 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED,
7105 JobParameters.REASON_PREEMPT,
7106 JobParameters.REASON_TIMEOUT,
7107 JobParameters.REASON_DEVICE_IDLE,
7108 };
7109 for (int ic = 0; ic < completions.size(); ++ic) {
7110 SparseIntArray types = completions.valueAt(ic);
7111 if (types != null) {
7112 final long jcToken = proto.start(UidProto.JOB_COMPLETION);
7113
7114 proto.write(UidProto.JobCompletion.NAME, completions.keyAt(ic));
7115
7116 for (int r : reasons) {
7117 long rToken = proto.start(UidProto.JobCompletion.REASON_COUNT);
7118 proto.write(UidProto.JobCompletion.ReasonCount.NAME, r);
7119 proto.write(UidProto.JobCompletion.ReasonCount.COUNT, types.get(r, 0));
7120 proto.end(rToken);
7121 }
7122
7123 proto.end(jcToken);
7124 }
7125 }
7126
7127 // Scheduled jobs (JOB_DATA)
7128 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
7129 for (int ij = jobs.size() - 1; ij >= 0; --ij) {
7130 final Timer timer = jobs.valueAt(ij);
7131 final Timer bgTimer = timer.getSubTimer();
7132 final long jToken = proto.start(UidProto.JOBS);
7133
7134 proto.write(UidProto.Job.NAME, jobs.keyAt(ij));
7135 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7136 dumpTimer(proto, UidProto.Job.TOTAL, timer, rawRealtimeUs, which);
7137 dumpTimer(proto, UidProto.Job.BACKGROUND, bgTimer, rawRealtimeUs, which);
7138
7139 proto.end(jToken);
7140 }
7141
7142 // Modem Controller (MODEM_CONTROLLER_DATA)
7143 dumpControllerActivityProto(proto, UidProto.MODEM_CONTROLLER,
7144 u.getModemControllerActivity(), which);
7145
7146 // Network stats (NETWORK_DATA)
7147 final long nToken = proto.start(UidProto.NETWORK);
7148 proto.write(UidProto.Network.MOBILE_BYTES_RX,
7149 u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7150 proto.write(UidProto.Network.MOBILE_BYTES_TX,
7151 u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7152 proto.write(UidProto.Network.WIFI_BYTES_RX,
7153 u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7154 proto.write(UidProto.Network.WIFI_BYTES_TX,
7155 u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7156 proto.write(UidProto.Network.BT_BYTES_RX,
7157 u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7158 proto.write(UidProto.Network.BT_BYTES_TX,
7159 u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
7160 proto.write(UidProto.Network.MOBILE_PACKETS_RX,
7161 u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7162 proto.write(UidProto.Network.MOBILE_PACKETS_TX,
7163 u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7164 proto.write(UidProto.Network.WIFI_PACKETS_RX,
7165 u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7166 proto.write(UidProto.Network.WIFI_PACKETS_TX,
7167 u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7168 proto.write(UidProto.Network.MOBILE_ACTIVE_DURATION_MS,
7169 roundUsToMs(u.getMobileRadioActiveTime(which)));
7170 proto.write(UidProto.Network.MOBILE_ACTIVE_COUNT,
7171 u.getMobileRadioActiveCount(which));
7172 proto.write(UidProto.Network.MOBILE_WAKEUP_COUNT,
7173 u.getMobileRadioApWakeupCount(which));
7174 proto.write(UidProto.Network.WIFI_WAKEUP_COUNT,
7175 u.getWifiRadioApWakeupCount(which));
7176 proto.write(UidProto.Network.MOBILE_BYTES_BG_RX,
7177 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA, which));
7178 proto.write(UidProto.Network.MOBILE_BYTES_BG_TX,
7179 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA, which));
7180 proto.write(UidProto.Network.WIFI_BYTES_BG_RX,
7181 u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which));
7182 proto.write(UidProto.Network.WIFI_BYTES_BG_TX,
7183 u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which));
7184 proto.write(UidProto.Network.MOBILE_PACKETS_BG_RX,
7185 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA, which));
7186 proto.write(UidProto.Network.MOBILE_PACKETS_BG_TX,
7187 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA, which));
7188 proto.write(UidProto.Network.WIFI_PACKETS_BG_RX,
7189 u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA, which));
7190 proto.write(UidProto.Network.WIFI_PACKETS_BG_TX,
7191 u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA, which));
7192 proto.end(nToken);
7193
7194 // Power use item (POWER_USE_ITEM_DATA)
7195 BatterySipper bs = uidToSipper.get(uid);
7196 if (bs != null) {
7197 final long bsToken = proto.start(UidProto.POWER_USE_ITEM);
7198 proto.write(UidProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7199 proto.write(UidProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7200 proto.write(UidProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7201 proto.write(UidProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7202 bs.proportionalSmearMah);
7203 proto.end(bsToken);
7204 }
7205
7206 // Processes (PROCESS_DATA)
7207 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats =
7208 u.getProcessStats();
7209 for (int ipr = processStats.size() - 1; ipr >= 0; --ipr) {
7210 final Uid.Proc ps = processStats.valueAt(ipr);
7211 final long prToken = proto.start(UidProto.PROCESS);
7212
7213 proto.write(UidProto.Process.NAME, processStats.keyAt(ipr));
7214 proto.write(UidProto.Process.USER_DURATION_MS, ps.getUserTime(which));
7215 proto.write(UidProto.Process.SYSTEM_DURATION_MS, ps.getSystemTime(which));
7216 proto.write(UidProto.Process.FOREGROUND_DURATION_MS, ps.getForegroundTime(which));
7217 proto.write(UidProto.Process.START_COUNT, ps.getStarts(which));
7218 proto.write(UidProto.Process.ANR_COUNT, ps.getNumAnrs(which));
7219 proto.write(UidProto.Process.CRASH_COUNT, ps.getNumCrashes(which));
7220
7221 proto.end(prToken);
7222 }
7223
7224 // Sensors (SENSOR_DATA)
7225 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
7226 for (int ise = 0; ise < sensors.size(); ++ise) {
7227 final Uid.Sensor se = sensors.valueAt(ise);
7228 final Timer timer = se.getSensorTime();
7229 if (timer == null) {
7230 continue;
7231 }
7232 final Timer bgTimer = se.getSensorBackgroundTime();
7233 final int sensorNumber = sensors.keyAt(ise);
7234 final long seToken = proto.start(UidProto.SENSORS);
7235
7236 proto.write(UidProto.Sensor.ID, sensorNumber);
7237 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7238 dumpTimer(proto, UidProto.Sensor.APPORTIONED, timer, rawRealtimeUs, which);
7239 dumpTimer(proto, UidProto.Sensor.BACKGROUND, bgTimer, rawRealtimeUs, which);
7240
7241 proto.end(seToken);
7242 }
7243
7244 // State times (STATE_TIME_DATA)
7245 for (int ips = 0; ips < Uid.NUM_PROCESS_STATE; ++ips) {
7246 long durMs = roundUsToMs(u.getProcessStateTime(ips, rawRealtimeUs, which));
7247 if (durMs == 0) {
7248 continue;
7249 }
7250 final long stToken = proto.start(UidProto.STATES);
7251 proto.write(UidProto.StateTime.STATE, ips);
7252 proto.write(UidProto.StateTime.DURATION_MS, durMs);
7253 proto.end(stToken);
7254 }
7255
7256 // Syncs (SYNC_DATA)
7257 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
7258 for (int isy = syncs.size() - 1; isy >= 0; --isy) {
7259 final Timer timer = syncs.valueAt(isy);
7260 final Timer bgTimer = timer.getSubTimer();
7261 final long syToken = proto.start(UidProto.SYNCS);
7262
7263 proto.write(UidProto.Sync.NAME, syncs.keyAt(isy));
7264 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7265 dumpTimer(proto, UidProto.Sync.TOTAL, timer, rawRealtimeUs, which);
7266 dumpTimer(proto, UidProto.Sync.BACKGROUND, bgTimer, rawRealtimeUs, which);
7267
7268 proto.end(syToken);
7269 }
7270
7271 // User activity (USER_ACTIVITY_DATA)
7272 if (u.hasUserActivity()) {
7273 for (int i = 0; i < Uid.NUM_USER_ACTIVITY_TYPES; ++i) {
7274 int val = u.getUserActivityCount(i, which);
7275 if (val != 0) {
7276 final long uaToken = proto.start(UidProto.USER_ACTIVITY);
7277 proto.write(UidProto.UserActivity.NAME, i);
7278 proto.write(UidProto.UserActivity.COUNT, val);
7279 proto.end(uaToken);
7280 }
7281 }
7282 }
7283
7284 // Vibrator (VIBRATOR_DATA)
7285 dumpTimer(proto, UidProto.VIBRATOR, u.getVibratorOnTimer(), rawRealtimeUs, which);
7286
7287 // Video (VIDEO_DATA)
7288 dumpTimer(proto, UidProto.VIDEO, u.getVideoTurnedOnTimer(), rawRealtimeUs, which);
7289
7290 // Wakelocks (WAKELOCK_DATA)
7291 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
7292 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7293 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7294 final long wToken = proto.start(UidProto.WAKELOCKS);
7295 proto.write(UidProto.Wakelock.NAME, wakelocks.keyAt(iw));
7296 dumpTimer(proto, UidProto.Wakelock.FULL, wl.getWakeTime(WAKE_TYPE_FULL),
7297 rawRealtimeUs, which);
7298 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7299 if (pTimer != null) {
7300 dumpTimer(proto, UidProto.Wakelock.PARTIAL, pTimer, rawRealtimeUs, which);
7301 dumpTimer(proto, UidProto.Wakelock.BACKGROUND_PARTIAL, pTimer.getSubTimer(),
7302 rawRealtimeUs, which);
7303 }
7304 dumpTimer(proto, UidProto.Wakelock.WINDOW, wl.getWakeTime(WAKE_TYPE_WINDOW),
7305 rawRealtimeUs, which);
7306 proto.end(wToken);
7307 }
7308
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007309 // Wifi Multicast Wakelock (WIFI_MULTICAST_WAKELOCK_DATA)
7310 dumpTimer(proto, UidProto.WIFI_MULTICAST_WAKELOCK, u.getMulticastWakelockStats(),
7311 rawRealtimeUs, which);
7312
Kweku Adams103351f2017-10-16 14:39:34 -07007313 // Wakeup alarms (WAKEUP_ALARM_DATA)
7314 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7315 final Uid.Pkg ps = packageStats.valueAt(ipkg);
7316 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
7317 for (int iwa = alarms.size() - 1; iwa >= 0; --iwa) {
7318 final long waToken = proto.start(UidProto.WAKEUP_ALARM);
7319 proto.write(UidProto.WakeupAlarm.NAME, alarms.keyAt(iwa));
7320 proto.write(UidProto.WakeupAlarm.COUNT,
7321 alarms.valueAt(iwa).getCountLocked(which));
7322 proto.end(waToken);
7323 }
7324 }
7325
7326 // Wifi Controller (WIFI_CONTROLLER_DATA)
7327 dumpControllerActivityProto(proto, UidProto.WIFI_CONTROLLER,
7328 u.getWifiControllerActivity(), which);
7329
7330 // Wifi data (WIFI_DATA)
7331 final long wToken = proto.start(UidProto.WIFI);
7332 proto.write(UidProto.Wifi.FULL_WIFI_LOCK_DURATION_MS,
7333 roundUsToMs(u.getFullWifiLockTime(rawRealtimeUs, which)));
7334 dumpTimer(proto, UidProto.Wifi.APPORTIONED_SCAN, u.getWifiScanTimer(),
7335 rawRealtimeUs, which);
7336 proto.write(UidProto.Wifi.RUNNING_DURATION_MS,
7337 roundUsToMs(u.getWifiRunningTime(rawRealtimeUs, which)));
7338 dumpTimer(proto, UidProto.Wifi.BACKGROUND_SCAN, u.getWifiScanBackgroundTimer(),
7339 rawRealtimeUs, which);
7340 proto.end(wToken);
7341
7342 proto.end(uTkn);
7343 }
7344 }
7345
7346 private void dumpProtoSystemLocked(ProtoOutputStream proto, BatteryStatsHelper helper) {
Kweku Adams87b19ec2017-10-09 12:40:03 -07007347 final long sToken = proto.start(BatteryStatsProto.SYSTEM);
7348 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7349 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7350 final long rawRealtimeUs = rawRealtimeMs * 1000;
7351 final int which = STATS_SINCE_CHARGED;
7352
7353 // Battery data (BATTERY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007354 final long bToken = proto.start(SystemProto.BATTERY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007355 proto.write(SystemProto.Battery.START_CLOCK_TIME_MS, getStartClockTime());
7356 proto.write(SystemProto.Battery.START_COUNT, getStartCount());
7357 proto.write(SystemProto.Battery.TOTAL_REALTIME_MS,
7358 computeRealtime(rawRealtimeUs, which) / 1000);
7359 proto.write(SystemProto.Battery.TOTAL_UPTIME_MS,
7360 computeUptime(rawUptimeUs, which) / 1000);
7361 proto.write(SystemProto.Battery.BATTERY_REALTIME_MS,
7362 computeBatteryRealtime(rawRealtimeUs, which) / 1000);
7363 proto.write(SystemProto.Battery.BATTERY_UPTIME_MS,
7364 computeBatteryUptime(rawUptimeUs, which) / 1000);
7365 proto.write(SystemProto.Battery.SCREEN_OFF_REALTIME_MS,
7366 computeBatteryScreenOffRealtime(rawRealtimeUs, which) / 1000);
7367 proto.write(SystemProto.Battery.SCREEN_OFF_UPTIME_MS,
7368 computeBatteryScreenOffUptime(rawUptimeUs, which) / 1000);
7369 proto.write(SystemProto.Battery.SCREEN_DOZE_DURATION_MS,
7370 getScreenDozeTime(rawRealtimeUs, which) / 1000);
7371 proto.write(SystemProto.Battery.ESTIMATED_BATTERY_CAPACITY_MAH,
7372 getEstimatedBatteryCapacity());
7373 proto.write(SystemProto.Battery.MIN_LEARNED_BATTERY_CAPACITY_UAH,
7374 getMinLearnedBatteryCapacity());
7375 proto.write(SystemProto.Battery.MAX_LEARNED_BATTERY_CAPACITY_UAH,
7376 getMaxLearnedBatteryCapacity());
Kweku Adams103351f2017-10-16 14:39:34 -07007377 proto.end(bToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007378
7379 // Battery discharge (BATTERY_DISCHARGE_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007380 final long bdToken = proto.start(SystemProto.BATTERY_DISCHARGE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007381 proto.write(SystemProto.BatteryDischarge.LOWER_BOUND_SINCE_CHARGE,
7382 getLowDischargeAmountSinceCharge());
7383 proto.write(SystemProto.BatteryDischarge.UPPER_BOUND_SINCE_CHARGE,
7384 getHighDischargeAmountSinceCharge());
7385 proto.write(SystemProto.BatteryDischarge.SCREEN_ON_SINCE_CHARGE,
7386 getDischargeAmountScreenOnSinceCharge());
7387 proto.write(SystemProto.BatteryDischarge.SCREEN_OFF_SINCE_CHARGE,
7388 getDischargeAmountScreenOffSinceCharge());
7389 proto.write(SystemProto.BatteryDischarge.SCREEN_DOZE_SINCE_CHARGE,
7390 getDischargeAmountScreenDozeSinceCharge());
7391 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH,
7392 getUahDischarge(which) / 1000);
7393 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_OFF,
7394 getUahDischargeScreenOff(which) / 1000);
7395 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_DOZE,
7396 getUahDischargeScreenDoze(which) / 1000);
Mike Ma15313c92017-11-15 17:58:21 -08007397 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_LIGHT_DOZE,
7398 getUahDischargeLightDoze(which) / 1000);
7399 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_DEEP_DOZE,
7400 getUahDischargeDeepDoze(which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007401 proto.end(bdToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007402
7403 // Time remaining
7404 long timeRemainingUs = computeChargeTimeRemaining(rawRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -07007405 // These are part of a oneof, so we should only set one of them.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007406 if (timeRemainingUs >= 0) {
7407 // Charge time remaining (CHARGE_TIME_REMAIN_DATA)
7408 proto.write(SystemProto.CHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7409 } else {
7410 timeRemainingUs = computeBatteryTimeRemaining(rawRealtimeUs);
7411 // Discharge time remaining (DISCHARGE_TIME_REMAIN_DATA)
7412 if (timeRemainingUs >= 0) {
7413 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7414 } else {
7415 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, -1);
7416 }
7417 }
7418
7419 // Charge step (CHARGE_STEP_DATA)
7420 dumpDurationSteps(proto, SystemProto.CHARGE_STEP, getChargeLevelStepTracker());
7421
7422 // Phone data connection (DATA_CONNECTION_TIME_DATA and DATA_CONNECTION_COUNT_DATA)
7423 for (int i = 0; i < NUM_DATA_CONNECTION_TYPES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007424 final long pdcToken = proto.start(SystemProto.DATA_CONNECTION);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007425 proto.write(SystemProto.DataConnection.NAME, i);
7426 dumpTimer(proto, SystemProto.DataConnection.TOTAL, getPhoneDataConnectionTimer(i),
7427 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007428 proto.end(pdcToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007429 }
7430
7431 // Discharge step (DISCHARGE_STEP_DATA)
7432 dumpDurationSteps(proto, SystemProto.DISCHARGE_STEP, getDischargeLevelStepTracker());
7433
7434 // CPU frequencies (GLOBAL_CPU_FREQ_DATA)
7435 final long[] cpuFreqs = getCpuFreqs();
7436 if (cpuFreqs != null) {
7437 for (long i : cpuFreqs) {
7438 proto.write(SystemProto.CPU_FREQUENCY, i);
7439 }
7440 }
7441
7442 // Bluetooth controller (GLOBAL_BLUETOOTH_CONTROLLER_DATA)
7443 dumpControllerActivityProto(proto, SystemProto.GLOBAL_BLUETOOTH_CONTROLLER,
7444 getBluetoothControllerActivity(), which);
7445
7446 // Modem controller (GLOBAL_MODEM_CONTROLLER_DATA)
7447 dumpControllerActivityProto(proto, SystemProto.GLOBAL_MODEM_CONTROLLER,
7448 getModemControllerActivity(), which);
7449
7450 // Global network data (GLOBAL_NETWORK_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007451 final long gnToken = proto.start(SystemProto.GLOBAL_NETWORK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007452 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_RX,
7453 getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7454 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_TX,
7455 getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7456 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_RX,
7457 getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7458 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_TX,
7459 getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7460 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_RX,
7461 getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7462 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_TX,
7463 getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7464 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_RX,
7465 getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7466 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_TX,
7467 getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7468 proto.write(SystemProto.GlobalNetwork.BT_BYTES_RX,
7469 getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7470 proto.write(SystemProto.GlobalNetwork.BT_BYTES_TX,
7471 getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
Kweku Adams103351f2017-10-16 14:39:34 -07007472 proto.end(gnToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007473
7474 // Wifi controller (GLOBAL_WIFI_CONTROLLER_DATA)
7475 dumpControllerActivityProto(proto, SystemProto.GLOBAL_WIFI_CONTROLLER,
7476 getWifiControllerActivity(), which);
7477
7478
7479 // Global wifi (GLOBAL_WIFI_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007480 final long gwToken = proto.start(SystemProto.GLOBAL_WIFI);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007481 proto.write(SystemProto.GlobalWifi.ON_DURATION_MS,
7482 getWifiOnTime(rawRealtimeUs, which) / 1000);
7483 proto.write(SystemProto.GlobalWifi.RUNNING_DURATION_MS,
7484 getGlobalWifiRunningTime(rawRealtimeUs, which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007485 proto.end(gwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007486
7487 // Kernel wakelock (KERNEL_WAKELOCK_DATA)
7488 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
7489 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007490 final long kwToken = proto.start(SystemProto.KERNEL_WAKELOCK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007491 proto.write(SystemProto.KernelWakelock.NAME, ent.getKey());
7492 dumpTimer(proto, SystemProto.KernelWakelock.TOTAL, ent.getValue(),
7493 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007494 proto.end(kwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007495 }
7496
7497 // Misc (MISC_DATA)
7498 // Calculate wakelock times across all uids.
7499 long fullWakeLockTimeTotalUs = 0;
7500 long partialWakeLockTimeTotalUs = 0;
7501
7502 final SparseArray<? extends Uid> uidStats = getUidStats();
7503 for (int iu = 0; iu < uidStats.size(); iu++) {
7504 final Uid u = uidStats.valueAt(iu);
7505
7506 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks =
7507 u.getWakelockStats();
7508 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7509 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7510
7511 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
7512 if (fullWakeTimer != null) {
7513 fullWakeLockTimeTotalUs += fullWakeTimer.getTotalTimeLocked(rawRealtimeUs,
7514 which);
7515 }
7516
7517 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7518 if (partialWakeTimer != null) {
7519 partialWakeLockTimeTotalUs += partialWakeTimer.getTotalTimeLocked(
7520 rawRealtimeUs, which);
7521 }
7522 }
7523 }
Kweku Adams103351f2017-10-16 14:39:34 -07007524 final long mToken = proto.start(SystemProto.MISC);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007525 proto.write(SystemProto.Misc.SCREEN_ON_DURATION_MS,
7526 getScreenOnTime(rawRealtimeUs, which) / 1000);
7527 proto.write(SystemProto.Misc.PHONE_ON_DURATION_MS,
7528 getPhoneOnTime(rawRealtimeUs, which) / 1000);
7529 proto.write(SystemProto.Misc.FULL_WAKELOCK_TOTAL_DURATION_MS,
7530 fullWakeLockTimeTotalUs / 1000);
7531 proto.write(SystemProto.Misc.PARTIAL_WAKELOCK_TOTAL_DURATION_MS,
7532 partialWakeLockTimeTotalUs / 1000);
7533 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_DURATION_MS,
7534 getMobileRadioActiveTime(rawRealtimeUs, which) / 1000);
7535 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_ADJUSTED_TIME_MS,
7536 getMobileRadioActiveAdjustedTime(which) / 1000);
7537 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_COUNT,
7538 getMobileRadioActiveCount(which));
7539 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_UNKNOWN_DURATION_MS,
7540 getMobileRadioActiveUnknownTime(which) / 1000);
7541 proto.write(SystemProto.Misc.INTERACTIVE_DURATION_MS,
7542 getInteractiveTime(rawRealtimeUs, which) / 1000);
7543 proto.write(SystemProto.Misc.BATTERY_SAVER_MODE_ENABLED_DURATION_MS,
7544 getPowerSaveModeEnabledTime(rawRealtimeUs, which) / 1000);
7545 proto.write(SystemProto.Misc.NUM_CONNECTIVITY_CHANGES,
7546 getNumConnectivityChange(which));
7547 proto.write(SystemProto.Misc.DEEP_DOZE_ENABLED_DURATION_MS,
7548 getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7549 proto.write(SystemProto.Misc.DEEP_DOZE_COUNT,
7550 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
7551 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_DURATION_MS,
7552 getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7553 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_COUNT,
7554 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
7555 proto.write(SystemProto.Misc.LONGEST_DEEP_DOZE_DURATION_MS,
7556 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
7557 proto.write(SystemProto.Misc.LIGHT_DOZE_ENABLED_DURATION_MS,
7558 getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7559 proto.write(SystemProto.Misc.LIGHT_DOZE_COUNT,
7560 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
7561 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_DURATION_MS,
7562 getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7563 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_COUNT,
7564 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
7565 proto.write(SystemProto.Misc.LONGEST_LIGHT_DOZE_DURATION_MS,
7566 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
Kweku Adams103351f2017-10-16 14:39:34 -07007567 proto.end(mToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007568
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007569 // Wifi multicast wakelock total stats (WIFI_MULTICAST_WAKELOCK_TOTAL_DATA)
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08007570 final long multicastWakeLockTimeTotalUs =
7571 getWifiMulticastWakelockTime(rawRealtimeUs, which);
7572 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007573 final long wmctToken = proto.start(SystemProto.WIFI_MULTICAST_WAKELOCK_TOTAL);
7574 proto.write(SystemProto.WifiMulticastWakelockTotal.DURATION_MS,
7575 multicastWakeLockTimeTotalUs / 1000);
7576 proto.write(SystemProto.WifiMulticastWakelockTotal.COUNT,
7577 multicastWakeLockCountTotal);
7578 proto.end(wmctToken);
7579
Kweku Adams87b19ec2017-10-09 12:40:03 -07007580 // Power use item (POWER_USE_ITEM_DATA)
7581 final List<BatterySipper> sippers = helper.getUsageList();
7582 if (sippers != null) {
7583 for (int i = 0; i < sippers.size(); ++i) {
7584 final BatterySipper bs = sippers.get(i);
7585 int n = SystemProto.PowerUseItem.UNKNOWN_SIPPER;
7586 int uid = 0;
7587 switch (bs.drainType) {
7588 case IDLE:
7589 n = SystemProto.PowerUseItem.IDLE;
7590 break;
7591 case CELL:
7592 n = SystemProto.PowerUseItem.CELL;
7593 break;
7594 case PHONE:
7595 n = SystemProto.PowerUseItem.PHONE;
7596 break;
7597 case WIFI:
7598 n = SystemProto.PowerUseItem.WIFI;
7599 break;
7600 case BLUETOOTH:
7601 n = SystemProto.PowerUseItem.BLUETOOTH;
7602 break;
7603 case SCREEN:
7604 n = SystemProto.PowerUseItem.SCREEN;
7605 break;
7606 case FLASHLIGHT:
7607 n = SystemProto.PowerUseItem.FLASHLIGHT;
7608 break;
7609 case APP:
Kweku Adams103351f2017-10-16 14:39:34 -07007610 // dumpProtoAppsLocked will handle this.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007611 continue;
7612 case USER:
7613 n = SystemProto.PowerUseItem.USER;
7614 uid = UserHandle.getUid(bs.userId, 0);
7615 break;
7616 case UNACCOUNTED:
7617 n = SystemProto.PowerUseItem.UNACCOUNTED;
7618 break;
7619 case OVERCOUNTED:
7620 n = SystemProto.PowerUseItem.OVERCOUNTED;
7621 break;
7622 case CAMERA:
7623 n = SystemProto.PowerUseItem.CAMERA;
7624 break;
7625 case MEMORY:
7626 n = SystemProto.PowerUseItem.MEMORY;
7627 break;
7628 }
Kweku Adams103351f2017-10-16 14:39:34 -07007629 final long puiToken = proto.start(SystemProto.POWER_USE_ITEM);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007630 proto.write(SystemProto.PowerUseItem.NAME, n);
7631 proto.write(SystemProto.PowerUseItem.UID, uid);
7632 proto.write(SystemProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7633 proto.write(SystemProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7634 proto.write(SystemProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7635 proto.write(SystemProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7636 bs.proportionalSmearMah);
Kweku Adams103351f2017-10-16 14:39:34 -07007637 proto.end(puiToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007638 }
7639 }
7640
7641 // Power use summary (POWER_USE_SUMMARY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007642 final long pusToken = proto.start(SystemProto.POWER_USE_SUMMARY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007643 proto.write(SystemProto.PowerUseSummary.BATTERY_CAPACITY_MAH,
7644 helper.getPowerProfile().getBatteryCapacity());
7645 proto.write(SystemProto.PowerUseSummary.COMPUTED_POWER_MAH, helper.getComputedPower());
7646 proto.write(SystemProto.PowerUseSummary.MIN_DRAINED_POWER_MAH, helper.getMinDrainedPower());
7647 proto.write(SystemProto.PowerUseSummary.MAX_DRAINED_POWER_MAH, helper.getMaxDrainedPower());
Kweku Adams103351f2017-10-16 14:39:34 -07007648 proto.end(pusToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007649
7650 // RPM stats (RESOURCE_POWER_MANAGER_DATA)
7651 final Map<String, ? extends Timer> rpmStats = getRpmStats();
7652 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
7653 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007654 final long rpmToken = proto.start(SystemProto.RESOURCE_POWER_MANAGER);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007655 proto.write(SystemProto.ResourcePowerManager.NAME, ent.getKey());
7656 dumpTimer(proto, SystemProto.ResourcePowerManager.TOTAL,
7657 ent.getValue(), rawRealtimeUs, which);
7658 dumpTimer(proto, SystemProto.ResourcePowerManager.SCREEN_OFF,
7659 screenOffRpmStats.get(ent.getKey()), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007660 proto.end(rpmToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007661 }
7662
7663 // Screen brightness (SCREEN_BRIGHTNESS_DATA)
7664 for (int i = 0; i < NUM_SCREEN_BRIGHTNESS_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007665 final long sbToken = proto.start(SystemProto.SCREEN_BRIGHTNESS);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007666 proto.write(SystemProto.ScreenBrightness.NAME, i);
7667 dumpTimer(proto, SystemProto.ScreenBrightness.TOTAL, getScreenBrightnessTimer(i),
7668 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007669 proto.end(sbToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007670 }
7671
7672 // Signal scanning time (SIGNAL_SCANNING_TIME_DATA)
7673 dumpTimer(proto, SystemProto.SIGNAL_SCANNING, getPhoneSignalScanningTimer(), rawRealtimeUs,
7674 which);
7675
7676 // Phone signal strength (SIGNAL_STRENGTH_TIME_DATA and SIGNAL_STRENGTH_COUNT_DATA)
7677 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007678 final long pssToken = proto.start(SystemProto.PHONE_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007679 proto.write(SystemProto.PhoneSignalStrength.NAME, i);
7680 dumpTimer(proto, SystemProto.PhoneSignalStrength.TOTAL, getPhoneSignalStrengthTimer(i),
7681 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007682 proto.end(pssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007683 }
7684
7685 // Wakeup reasons (WAKEUP_REASON_DATA)
7686 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
7687 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007688 final long wrToken = proto.start(SystemProto.WAKEUP_REASON);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007689 proto.write(SystemProto.WakeupReason.NAME, ent.getKey());
7690 dumpTimer(proto, SystemProto.WakeupReason.TOTAL, ent.getValue(), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007691 proto.end(wrToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007692 }
7693
7694 // Wifi signal strength (WIFI_SIGNAL_STRENGTH_TIME_DATA and WIFI_SIGNAL_STRENGTH_COUNT_DATA)
7695 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007696 final long wssToken = proto.start(SystemProto.WIFI_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007697 proto.write(SystemProto.WifiSignalStrength.NAME, i);
7698 dumpTimer(proto, SystemProto.WifiSignalStrength.TOTAL, getWifiSignalStrengthTimer(i),
7699 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007700 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007701 }
7702
7703 // Wifi state (WIFI_STATE_TIME_DATA and WIFI_STATE_COUNT_DATA)
7704 for (int i = 0; i < NUM_WIFI_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007705 final long wsToken = proto.start(SystemProto.WIFI_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007706 proto.write(SystemProto.WifiState.NAME, i);
7707 dumpTimer(proto, SystemProto.WifiState.TOTAL, getWifiStateTimer(i),
7708 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007709 proto.end(wsToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007710 }
7711
7712 // Wifi supplicant state (WIFI_SUPPL_STATE_TIME_DATA and WIFI_SUPPL_STATE_COUNT_DATA)
7713 for (int i = 0; i < NUM_WIFI_SUPPL_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007714 final long wssToken = proto.start(SystemProto.WIFI_SUPPLICANT_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007715 proto.write(SystemProto.WifiSupplicantState.NAME, i);
7716 dumpTimer(proto, SystemProto.WifiSupplicantState.TOTAL, getWifiSupplStateTimer(i),
7717 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007718 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007719 }
7720
7721 proto.end(sToken);
7722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007723}