blob: aaad9cf15e45c5adac51f8c007dfddf347216bee [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;
Siddharth Ray78ccaf52017-12-23 16:16:21 -080038import com.android.internal.location.gnssmetrics.GnssMetrics;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080039import com.android.internal.os.BatterySipper;
40import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
Kweku Adams2f73ecd2017-09-27 16:59:19 -070042import java.io.FileDescriptor;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070043import java.io.PrintWriter;
44import java.util.ArrayList;
45import java.util.Collections;
46import java.util.Comparator;
47import java.util.Formatter;
48import java.util.HashMap;
49import java.util.List;
50import java.util.Map;
51
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052/**
53 * A class providing access to battery usage statistics, including information on
54 * wakelocks, processes, packages, and services. All times are represented in microseconds
55 * except where indicated otherwise.
56 * @hide
57 */
58public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070059 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
61 private static final boolean LOCAL_LOGV = false;
Bookatz82b341172017-09-07 19:06:08 -070062 /** Fetching RPM stats is too slow to do each time screen changes, so disable it. */
63 protected static final boolean SCREEN_OFF_RPM_STATS_ENABLED = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070064
65 /** @hide */
66 public static final String SERVICE_NAME = "batterystats";
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 /**
69 * A constant indicating a partial wake lock timer.
70 */
71 public static final int WAKE_TYPE_PARTIAL = 0;
72
73 /**
74 * A constant indicating a full wake lock timer.
75 */
76 public static final int WAKE_TYPE_FULL = 1;
77
78 /**
79 * A constant indicating a window wake lock timer.
80 */
81 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 /**
84 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 */
86 public static final int SENSOR = 3;
Mike Mac2f518a2017-09-19 16:06:03 -070087
The Android Open Source Project10592532009-03-18 17:39:46 -070088 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070089 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070090 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070091 public static final int WIFI_RUNNING = 4;
Mike Mac2f518a2017-09-19 16:06:03 -070092
Dianne Hackborn617f8772009-03-31 15:04:46 -070093 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070094 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070095 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070096 public static final int FULL_WIFI_LOCK = 5;
Mike Mac2f518a2017-09-19 16:06:03 -070097
The Android Open Source Project10592532009-03-18 17:39:46 -070098 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070099 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -0700100 */
Nick Pelly6ccaa542012-06-15 15:22:47 -0700101 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
Dianne Hackborn62793e42015-03-09 11:15:41 -0700103 /**
104 * A constant indicating a wifi multicast timer
105 */
106 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700109 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700110 */
111 public static final int VIDEO_TURNED_ON = 8;
112
113 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800114 * A constant indicating a vibrator on timer
115 */
116 public static final int VIBRATOR_ON = 9;
117
118 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700119 * A constant indicating a foreground activity timer
120 */
121 public static final int FOREGROUND_ACTIVITY = 10;
122
123 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700124 * A constant indicating a wifi batched scan is active
125 */
126 public static final int WIFI_BATCHED_SCAN = 11;
127
128 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700129 * A constant indicating a process state timer
130 */
131 public static final int PROCESS_STATE = 12;
132
133 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700134 * A constant indicating a sync timer
135 */
136 public static final int SYNC = 13;
137
138 /**
139 * A constant indicating a job timer
140 */
141 public static final int JOB = 14;
142
143 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800144 * A constant indicating an audio turn on timer
145 */
146 public static final int AUDIO_TURNED_ON = 15;
147
148 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700149 * A constant indicating a flashlight turn on timer
150 */
151 public static final int FLASHLIGHT_TURNED_ON = 16;
152
153 /**
154 * A constant indicating a camera turn on timer
155 */
156 public static final int CAMERA_TURNED_ON = 17;
157
158 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700159 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700160 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700161 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700162
163 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800164 * A constant indicating a bluetooth scan timer.
165 */
166 public static final int BLUETOOTH_SCAN_ON = 19;
167
168 /**
Bookatzc8c44962017-05-11 12:12:54 -0700169 * A constant indicating an aggregated partial wake lock timer.
170 */
171 public static final int AGGREGATED_WAKE_TYPE_PARTIAL = 20;
172
173 /**
Bookatzb1f04f32017-05-19 13:57:32 -0700174 * A constant indicating a bluetooth scan timer for unoptimized scans.
175 */
176 public static final int BLUETOOTH_UNOPTIMIZED_SCAN_ON = 21;
177
178 /**
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700179 * A constant indicating a foreground service timer
180 */
181 public static final int FOREGROUND_SERVICE = 22;
182
183 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -0800184 * A constant indicating an aggregate wifi multicast timer
185 */
186 public static final int WIFI_AGGREGATE_MULTICAST_ENABLED = 23;
187
188 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 * Include all of the data in the stats, including previously saved data.
190 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700191 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192
193 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 * Include only the current run in the stats.
195 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700196 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197
198 /**
199 * Include only the run since the last time the device was unplugged in the stats.
200 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700201 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700202
203 // NOTE: Update this list if you add/change any stats above.
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700204 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700205 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700206 private static final String[] STAT_NAMES = { "l", "c", "u" };
207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700209 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700210 *
211 * New in version 19:
212 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800213 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700214 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Bookatz506a8182017-05-01 14:18:42 -0700215 * New in version 21:
216 * - Actual (not just apportioned) Wakelock time is also recorded.
Bookatzc8c44962017-05-11 12:12:54 -0700217 * - Aggregated partial wakelock time (per uid, instead of per wakelock) is recorded.
Bookatzb1f04f32017-05-19 13:57:32 -0700218 * - BLE scan result count
219 * - CPU frequency time per uid
220 * New in version 22:
221 * - BLE scan result background count, BLE unoptimized scan time
Bookatz98d4d5c2017-08-01 19:07:54 -0700222 * - Background partial wakelock time & count
223 * New in version 23:
224 * - Logging smeared power model values
225 * New in version 24:
226 * - Fixed bugs in background timers and BLE scan time
227 * New in version 25:
228 * - Package wakeup alarms are now on screen-off timebase
Bookatz50df7112017-08-04 14:53:26 -0700229 * New in version 26:
Bookatz82b341172017-09-07 19:06:08 -0700230 * - Resource power manager (rpm) states [but screenOffRpm is disabled from working properly]
Mike Mac2f518a2017-09-19 16:06:03 -0700231 * New in version 27:
232 * - Always On Display (screen doze mode) time and power
Mike Ma15313c92017-11-15 17:58:21 -0800233 * New in version 28:
234 * - Light/Deep Doze power
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700235 * - WiFi Multicast Wakelock statistics (count & duration)
Kweku Adamsa8943cb2017-12-22 13:21:06 -0800236 * New in version 29:
237 * - Process states re-ordered. TOP_SLEEPING now below BACKGROUND. HEAVY_WEIGHT introduced.
238 * - CPU times per UID process state
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700239 */
Kweku Adamsa8943cb2017-12-22 13:21:06 -0800240 static final int CHECKIN_VERSION = 29;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700241
242 /**
243 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700245 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700246
Evan Millar22ac0432009-03-31 11:33:18 -0700247 private static final long BYTES_PER_KB = 1024;
248 private static final long BYTES_PER_MB = 1048576; // 1024^2
249 private static final long BYTES_PER_GB = 1073741824; //1024^3
Bookatz506a8182017-05-01 14:18:42 -0700250
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700251 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800252 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700253 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700255 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700256 private static final String CPU_DATA = "cpu";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700257 private static final String GLOBAL_CPU_FREQ_DATA = "gcf";
258 private static final String CPU_TIMES_AT_FREQ_DATA = "ctf";
Bookatz50df7112017-08-04 14:53:26 -0700259 // rpm line is:
260 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "rpm", state/voter name, total time, total count,
261 // screen-off time, screen-off count
262 private static final String RESOURCE_POWER_MANAGER_DATA = "rpm";
Evan Millare84de8d2009-04-02 22:16:12 -0700263 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800264 private static final String VIBRATOR_DATA = "vib";
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700265 private static final String FOREGROUND_ACTIVITY_DATA = "fg";
266 // fgs line is:
267 // BATTERY_STATS_CHECKIN_VERSION, uid, category, "fgs",
268 // foreground service time, count
269 private static final String FOREGROUND_SERVICE_DATA = "fgs";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700270 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700271 // wl line is:
272 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
Bookatz5b5ec322017-05-26 09:40:38 -0700273 // full totalTime, 'f', count, current duration, max duration, total duration,
274 // partial totalTime, 'p', count, current duration, max duration, total duration,
275 // bg partial totalTime, 'bp', count, current duration, max duration, total duration,
276 // window totalTime, 'w', count, current duration, max duration, total duration
Bookatz506a8182017-05-01 14:18:42 -0700277 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700278 private static final String WAKELOCK_DATA = "wl";
Bookatzc8c44962017-05-11 12:12:54 -0700279 // awl line is:
280 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "awl",
281 // cumulative partial wakelock duration, cumulative background partial wakelock duration
282 private static final String AGGREGATED_WAKELOCK_DATA = "awl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700283 private static final String SYNC_DATA = "sy";
284 private static final String JOB_DATA = "jb";
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700285 private static final String JOB_COMPLETION_DATA = "jbc";
Evan Millarc64edde2009-04-18 12:26:32 -0700286 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700287 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700288 private static final String NETWORK_DATA = "nt";
289 private static final String USER_ACTIVITY_DATA = "ua";
290 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800291 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700292 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700293 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700294 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800295 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
296 private static final String WIFI_CONTROLLER_DATA = "wfcd";
297 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
298 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700299 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700300 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800301 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800302 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
303 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800304 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700305 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700306 private static final String SCREEN_BRIGHTNESS_DATA = "br";
307 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700308 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700309 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
310 private static final String DATA_CONNECTION_TIME_DATA = "dct";
311 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800312 private static final String WIFI_STATE_TIME_DATA = "wst";
313 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700314 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
315 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
316 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
317 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800318 private static final String POWER_USE_SUMMARY_DATA = "pws";
319 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700320 private static final String DISCHARGE_STEP_DATA = "dsd";
321 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700322 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
323 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700324 private static final String FLASHLIGHT_DATA = "fla";
325 private static final String CAMERA_DATA = "cam";
326 private static final String VIDEO_DATA = "vid";
327 private static final String AUDIO_DATA = "aud";
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700328 private static final String WIFI_MULTICAST_TOTAL_DATA = "wmct";
329 private static final String WIFI_MULTICAST_DATA = "wmc";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330
Adam Lesinski010bf372016-04-11 12:18:18 -0700331 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
332
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700333 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 private final Formatter mFormatter = new Formatter(mFormatBuilder);
335
336 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700337 * Indicates times spent by the uid at each cpu frequency in all process states.
338 *
339 * Other types might include times spent in foreground, background etc.
340 */
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800341 @VisibleForTesting
342 public static final String UID_TIMES_TYPE_ALL = "A";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700343
344 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700345 * State for keeping track of counting information.
346 */
347 public static abstract class Counter {
348
349 /**
350 * Returns the count associated with this Counter for the
351 * selected type of statistics.
352 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700353 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700354 */
Evan Millarc64edde2009-04-18 12:26:32 -0700355 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700356
357 /**
358 * Temporary for debugging.
359 */
360 public abstract void logState(Printer pw, String prefix);
361 }
362
363 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700364 * State for keeping track of long counting information.
365 */
366 public static abstract class LongCounter {
367
368 /**
369 * Returns the count associated with this Counter for the
370 * selected type of statistics.
371 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700372 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700373 */
374 public abstract long getCountLocked(int which);
375
376 /**
377 * Temporary for debugging.
378 */
379 public abstract void logState(Printer pw, String prefix);
380 }
381
382 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700383 * State for keeping track of array of long counting information.
384 */
385 public static abstract class LongCounterArray {
386 /**
387 * Returns the counts associated with this Counter for the
388 * selected type of statistics.
389 *
390 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
391 */
392 public abstract long[] getCountsLocked(int which);
393
394 /**
395 * Temporary for debugging.
396 */
397 public abstract void logState(Printer pw, String prefix);
398 }
399
400 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800401 * Container class that aggregates counters for transmit, receive, and idle state of a
402 * radio controller.
403 */
404 public static abstract class ControllerActivityCounter {
405 /**
406 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
407 * idle state.
408 */
409 public abstract LongCounter getIdleTimeCounter();
410
411 /**
412 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
413 * receive state.
414 */
415 public abstract LongCounter getRxTimeCounter();
416
417 /**
418 * An array of {@link LongCounter}, representing various transmit levels, where each level
419 * may draw a different amount of power. The levels themselves are controller-specific.
420 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
421 * various transmit level states.
422 */
423 public abstract LongCounter[] getTxTimeCounters();
424
425 /**
426 * @return a non-null {@link LongCounter} representing the power consumed by the controller
427 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
428 * yield a value of 0 if the device doesn't support power calculations.
429 */
430 public abstract LongCounter getPowerCounter();
431 }
432
433 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 * State for keeping track of timing information.
435 */
436 public static abstract class Timer {
437
438 /**
439 * Returns the count associated with this Timer for the
440 * selected type of statistics.
441 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700442 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 */
Evan Millarc64edde2009-04-18 12:26:32 -0700444 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445
446 /**
447 * Returns the total time in microseconds associated with this Timer for the
448 * selected type of statistics.
449 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800450 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700451 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 * @return a time in microseconds
453 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800454 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700457 * Returns the total time in microseconds associated with this Timer since the
458 * 'mark' was last set.
459 *
460 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
461 * @return a time in microseconds
462 */
463 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
464
465 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700466 * Returns the max duration if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700467 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700468 */
469 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
470 return -1;
471 }
472
473 /**
474 * Returns the current time the timer has been active, if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700475 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700476 */
477 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
478 return -1;
479 }
480
481 /**
Kweku Adams103351f2017-10-16 14:39:34 -0700482 * Returns the total time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800483 *
484 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
485 * been on since reset.
486 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
487 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
488 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
489 * the actual total time.
Kweku Adams103351f2017-10-16 14:39:34 -0700490 * Not all Timer subclasses track the max, total, and current durations.
Bookatz867c0d72017-03-07 18:23:42 -0800491 */
492 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
493 return -1;
494 }
495
496 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700497 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
498 * used, for example, for tracking background usage. Secondary timers are never pooled.
499 *
500 * Not all Timer subclasses have a secondary timer; those that don't return null.
501 */
502 public Timer getSubTimer() {
503 return null;
504 }
505
506 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700507 * Returns whether the timer is currently running. Some types of timers
508 * (e.g. BatchTimers) don't know whether the event is currently active,
509 * and report false.
510 */
511 public boolean isRunningLocked() {
512 return false;
513 }
514
515 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 * Temporary for debugging.
517 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700518 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 }
520
521 /**
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800522 * Maps the ActivityManager procstate into corresponding BatteryStats procstate.
523 */
524 public static int mapToInternalProcessState(int procState) {
525 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
526 return ActivityManager.PROCESS_STATE_NONEXISTENT;
527 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
528 return Uid.PROCESS_STATE_TOP;
529 } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
530 // Persistent and other foreground states go here.
531 return Uid.PROCESS_STATE_FOREGROUND_SERVICE;
532 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
533 // Persistent and other foreground states go here.
534 return Uid.PROCESS_STATE_FOREGROUND;
535 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
536 return Uid.PROCESS_STATE_BACKGROUND;
537 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
538 return Uid.PROCESS_STATE_TOP_SLEEPING;
539 } else if (procState <= ActivityManager.PROCESS_STATE_HEAVY_WEIGHT) {
540 return Uid.PROCESS_STATE_HEAVY_WEIGHT;
541 } else {
542 return Uid.PROCESS_STATE_CACHED;
543 }
544 }
545
546 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 * The statistics associated with a particular uid.
548 */
549 public static abstract class Uid {
550
551 /**
552 * Returns a mapping containing wakelock statistics.
553 *
554 * @return a Map from Strings to Uid.Wakelock objects.
555 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700556 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557
558 /**
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700559 * Returns the WiFi Multicast Wakelock statistics.
560 *
561 * @return a Timer Object for the per uid Multicast statistics.
562 */
563 public abstract Timer getMulticastWakelockStats();
564
565 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700566 * Returns a mapping containing sync statistics.
567 *
568 * @return a Map from Strings to Timer objects.
569 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700570 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700571
572 /**
573 * Returns a mapping containing scheduled job statistics.
574 *
575 * @return a Map from Strings to Timer objects.
576 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700577 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700578
579 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700580 * Returns statistics about how jobs have completed.
581 *
582 * @return A Map of String job names to completion type -> count mapping.
583 */
584 public abstract ArrayMap<String, SparseIntArray> getJobCompletionStats();
585
586 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 * The statistics associated with a particular wake lock.
588 */
589 public static abstract class Wakelock {
590 public abstract Timer getWakeTime(int type);
591 }
592
593 /**
Bookatzc8c44962017-05-11 12:12:54 -0700594 * The cumulative time the uid spent holding any partial wakelocks. This will generally
595 * differ from summing over the Wakelocks in getWakelockStats since the latter may have
596 * wakelocks that overlap in time (and therefore over-counts).
597 */
598 public abstract Timer getAggregatedPartialWakelockTimer();
599
600 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 * Returns a mapping containing sensor statistics.
602 *
603 * @return a Map from Integer sensor ids to Uid.Sensor objects.
604 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700605 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606
607 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700608 * Returns a mapping containing active process data.
609 */
610 public abstract SparseArray<? extends Pid> getPidStats();
Bookatzc8c44962017-05-11 12:12:54 -0700611
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700612 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 * Returns a mapping containing process statistics.
614 *
615 * @return a Map from Strings to Uid.Proc objects.
616 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700617 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618
619 /**
620 * Returns a mapping containing package statistics.
621 *
622 * @return a Map from Strings to Uid.Pkg objects.
623 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700624 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700625
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800626 public abstract ControllerActivityCounter getWifiControllerActivity();
627 public abstract ControllerActivityCounter getBluetoothControllerActivity();
628 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800629
630 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 * {@hide}
632 */
633 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700634
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800635 public abstract void noteWifiRunningLocked(long elapsedRealtime);
636 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
637 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
638 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
639 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
640 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
641 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
642 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
643 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
644 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800645 public abstract void noteActivityResumedLocked(long elapsedRealtime);
646 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800647 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
648 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
649 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700650 public abstract int getWifiScanCount(int which);
Kweku Adams103351f2017-10-16 14:39:34 -0700651 /**
652 * Returns the timer keeping track of wifi scans.
653 */
654 public abstract Timer getWifiScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800655 public abstract int getWifiScanBackgroundCount(int which);
656 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
657 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -0700658 /**
659 * Returns the timer keeping track of background wifi scans.
660 */
661 public abstract Timer getWifiScanBackgroundTimer();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800662 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700663 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800664 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700665 public abstract Timer getAudioTurnedOnTimer();
666 public abstract Timer getVideoTurnedOnTimer();
667 public abstract Timer getFlashlightTurnedOnTimer();
668 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700669 public abstract Timer getForegroundActivityTimer();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700670
671 /**
672 * Returns the timer keeping track of Foreground Service time
673 */
674 public abstract Timer getForegroundServiceTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800675 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800676 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatzb1f04f32017-05-19 13:57:32 -0700677 public abstract Timer getBluetoothUnoptimizedScanTimer();
678 public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700679 public abstract Counter getBluetoothScanResultCounter();
Bookatzb1f04f32017-05-19 13:57:32 -0700680 public abstract Counter getBluetoothScanResultBgCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700681
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700682 public abstract long[] getCpuFreqTimes(int which);
683 public abstract long[] getScreenOffCpuFreqTimes(int which);
684
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800685 /**
686 * Returns cpu times of an uid at a particular process state.
687 */
688 public abstract long[] getCpuFreqTimes(int which, int procState);
689 /**
690 * Returns cpu times of an uid while the screen if off at a particular process state.
691 */
692 public abstract long[] getScreenOffCpuFreqTimes(int which, int procState);
693
Dianne Hackborna0200e32016-03-30 18:01:41 -0700694 // Note: the following times are disjoint. They can be added together to find the
695 // total time a uid has had any processes running at all.
696
697 /**
698 * Time this uid has any processes in the top state (or above such as persistent).
699 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800700 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700701 /**
702 * Time this uid has any process with a started out bound foreground service, but
703 * none in the "top" state.
704 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800705 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700706 /**
Dianne Hackborna0200e32016-03-30 18:01:41 -0700707 * Time this uid has any process in an active foreground state, but none in the
708 * "top sleeping" or better state.
709 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800710 public static final int PROCESS_STATE_FOREGROUND = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700711 /**
712 * Time this uid has any process in an active background state, but none in the
713 * "foreground" or better state.
714 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800715 public static final int PROCESS_STATE_BACKGROUND = 3;
716 /**
717 * Time this uid has any process that is top while the device is sleeping, but not
718 * active for any other reason. We kind-of consider it a kind of cached process
719 * for execution restrictions.
720 */
721 public static final int PROCESS_STATE_TOP_SLEEPING = 4;
722 /**
723 * Time this uid has any process that is in the background but it has an activity
724 * marked as "can't save state". This is essentially a cached process, though the
725 * system will try much harder than normal to avoid killing it.
726 */
727 public static final int PROCESS_STATE_HEAVY_WEIGHT = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700728 /**
729 * Time this uid has any processes that are sitting around cached, not in one of the
730 * other active states.
731 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800732 public static final int PROCESS_STATE_CACHED = 6;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700733 /**
734 * Total number of process states we track.
735 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800736 public static final int NUM_PROCESS_STATE = 7;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700737
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800738 // Used in dump
Dianne Hackborn61659e52014-07-09 16:13:01 -0700739 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800740 "Top", "Fg Service", "Foreground", "Background", "Top Sleeping", "Heavy Weight",
741 "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700742 };
743
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800744 // Used in checkin dump
745 @VisibleForTesting
746 public static final String[] UID_PROCESS_TYPES = {
747 "T", // TOP
748 "FS", // FOREGROUND_SERVICE
749 "F", // FOREGROUND
750 "B", // BACKGROUND
751 "TS", // TOP_SLEEPING
752 "HW", // HEAVY_WEIGHT
753 "C" // CACHED
754 };
755
756 /**
757 * When the process exits one of these states, we need to make sure cpu time in this state
758 * is not attributed to any non-critical process states.
759 */
760 public static final int[] CRITICAL_PROC_STATES = {
761 PROCESS_STATE_TOP, PROCESS_STATE_FOREGROUND_SERVICE, PROCESS_STATE_FOREGROUND
762 };
763
Dianne Hackborn61659e52014-07-09 16:13:01 -0700764 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800765 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700766
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800767 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768
Robert Greenwalta029ea12013-09-25 16:38:12 -0700769 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
770
Dianne Hackborn617f8772009-03-31 15:04:46 -0700771 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700772 * Note that these must match the constants in android.os.PowerManager.
773 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
774 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700775 */
776 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700777 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700778 };
Bookatzc8c44962017-05-11 12:12:54 -0700779
Phil Weaverda80d672016-03-15 16:25:46 -0700780 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700781
Dianne Hackborn617f8772009-03-31 15:04:46 -0700782 public abstract void noteUserActivityLocked(int type);
783 public abstract boolean hasUserActivity();
784 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700785
786 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800787 public abstract long getNetworkActivityBytes(int type, int which);
788 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800789 public abstract long getMobileRadioActiveTime(int which);
790 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700791
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700792 /**
793 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
794 */
795 public abstract long getUserCpuTimeUs(int which);
796
797 /**
798 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
799 */
800 public abstract long getSystemCpuTimeUs(int which);
801
802 /**
Sudheer Shanka71f34b32017-07-21 00:14:24 -0700803 * Returns the approximate cpu time (in microseconds) spent at a certain CPU speed for a
Adam Lesinski6832f392015-09-05 18:05:40 -0700804 * given CPU cluster.
805 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700806 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700807 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700808 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
809 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700810 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700811 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700812
Adam Lesinski5f056f62016-07-14 16:56:08 -0700813 /**
814 * Returns the number of times this UID woke up the Application Processor to
815 * process a mobile radio packet.
816 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
817 */
818 public abstract long getMobileRadioApWakeupCount(int which);
819
820 /**
821 * Returns the number of times this UID woke up the Application Processor to
822 * process a WiFi packet.
823 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
824 */
825 public abstract long getWifiRadioApWakeupCount(int which);
826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800828 /*
829 * FIXME: it's not correct to use this magic value because it
830 * could clash with a sensor handle (which are defined by
831 * the sensor HAL, and therefore out of our control
832 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 // Magic sensor number for the GPS.
834 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800839
Bookatz867c0d72017-03-07 18:23:42 -0800840 /** Returns a Timer for sensor usage when app is in the background. */
841 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 }
843
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700844 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800845 public int mWakeNesting;
846 public long mWakeSumMs;
847 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700848 }
849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 /**
851 * The statistics associated with a particular process.
852 */
853 public static abstract class Proc {
854
Dianne Hackborn287952c2010-09-22 22:34:31 -0700855 public static class ExcessivePower {
856 public static final int TYPE_WAKE = 1;
857 public static final int TYPE_CPU = 2;
858
859 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700860 public long overTime;
861 public long usedTime;
862 }
863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800865 * Returns true if this process is still active in the battery stats.
866 */
867 public abstract boolean isActive();
868
869 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700870 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700872 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 */
874 public abstract long getUserTime(int which);
875
876 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700877 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700879 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 */
881 public abstract long getSystemTime(int which);
882
883 /**
884 * Returns the number of times the process has been started.
885 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700886 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 */
888 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700889
890 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800891 * Returns the number of times the process has crashed.
892 *
893 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
894 */
895 public abstract int getNumCrashes(int which);
896
897 /**
898 * Returns the number of times the process has ANRed.
899 *
900 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
901 */
902 public abstract int getNumAnrs(int which);
903
904 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700905 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700906 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700907 * @return foreground cpu time in microseconds
908 */
909 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700910
Dianne Hackborn287952c2010-09-22 22:34:31 -0700911 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700912
Dianne Hackborn287952c2010-09-22 22:34:31 -0700913 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 }
915
916 /**
917 * The statistics associated with a particular package.
918 */
919 public static abstract class Pkg {
920
921 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700922 * Returns information about all wakeup alarms that have been triggered for this
923 * package. The mapping keys are tag names for the alarms, the counter contains
924 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700926 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927
928 /**
929 * Returns a mapping containing service statistics.
930 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700931 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932
933 /**
934 * The statistics associated with a particular service.
935 */
Joe Onoratoabded112016-02-08 16:49:39 -0800936 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937
938 /**
939 * Returns the amount of time spent started.
940 *
941 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700942 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 * @return
944 */
945 public abstract long getStartTime(long batteryUptime, int which);
946
947 /**
948 * Returns the total number of times startService() has been called.
949 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700950 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 */
952 public abstract int getStarts(int which);
953
954 /**
955 * Returns the total number times the service has been launched.
956 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700957 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 */
959 public abstract int getLaunches(int which);
960 }
961 }
962 }
963
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800964 public static final class LevelStepTracker {
965 public long mLastStepTime = -1;
966 public int mNumStepDurations;
967 public final long[] mStepDurations;
968
969 public LevelStepTracker(int maxLevelSteps) {
970 mStepDurations = new long[maxLevelSteps];
971 }
972
973 public LevelStepTracker(int numSteps, long[] steps) {
974 mNumStepDurations = numSteps;
975 mStepDurations = new long[numSteps];
976 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
977 }
978
979 public long getDurationAt(int index) {
980 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
981 }
982
983 public int getLevelAt(int index) {
984 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
985 >> STEP_LEVEL_LEVEL_SHIFT);
986 }
987
988 public int getInitModeAt(int index) {
989 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
990 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
991 }
992
993 public int getModModeAt(int index) {
994 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
995 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
996 }
997
998 private void appendHex(long val, int topOffset, StringBuilder out) {
999 boolean hasData = false;
1000 while (topOffset >= 0) {
1001 int digit = (int)( (val>>topOffset) & 0xf );
1002 topOffset -= 4;
1003 if (!hasData && digit == 0) {
1004 continue;
1005 }
1006 hasData = true;
1007 if (digit >= 0 && digit <= 9) {
1008 out.append((char)('0' + digit));
1009 } else {
1010 out.append((char)('a' + digit - 10));
1011 }
1012 }
1013 }
1014
1015 public void encodeEntryAt(int index, StringBuilder out) {
1016 long item = mStepDurations[index];
1017 long duration = item & STEP_LEVEL_TIME_MASK;
1018 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
1019 >> STEP_LEVEL_LEVEL_SHIFT);
1020 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
1021 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1022 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
1023 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1024 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1025 case Display.STATE_OFF: out.append('f'); break;
1026 case Display.STATE_ON: out.append('o'); break;
1027 case Display.STATE_DOZE: out.append('d'); break;
1028 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
1029 }
1030 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1031 out.append('p');
1032 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001033 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1034 out.append('i');
1035 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001036 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1037 case Display.STATE_OFF: out.append('F'); break;
1038 case Display.STATE_ON: out.append('O'); break;
1039 case Display.STATE_DOZE: out.append('D'); break;
1040 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
1041 }
1042 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1043 out.append('P');
1044 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001045 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1046 out.append('I');
1047 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001048 out.append('-');
1049 appendHex(level, 4, out);
1050 out.append('-');
1051 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
1052 }
1053
1054 public void decodeEntryAt(int index, String value) {
1055 final int N = value.length();
1056 int i = 0;
1057 char c;
1058 long out = 0;
1059 while (i < N && (c=value.charAt(i)) != '-') {
1060 i++;
1061 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001062 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001063 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001064 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001065 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001066 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001067 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001068 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1069 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1070 break;
1071 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1072 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1073 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001074 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1075 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1076 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001077 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1078 break;
1079 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1080 break;
1081 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1082 break;
1083 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1084 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1085 break;
1086 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1087 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001088 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001089 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1090 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1091 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001092 }
1093 }
1094 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001095 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001096 while (i < N && (c=value.charAt(i)) != '-') {
1097 i++;
1098 level <<= 4;
1099 if (c >= '0' && c <= '9') {
1100 level += c - '0';
1101 } else if (c >= 'a' && c <= 'f') {
1102 level += c - 'a' + 10;
1103 } else if (c >= 'A' && c <= 'F') {
1104 level += c - 'A' + 10;
1105 }
1106 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001107 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001108 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
1109 long duration = 0;
1110 while (i < N && (c=value.charAt(i)) != '-') {
1111 i++;
1112 duration <<= 4;
1113 if (c >= '0' && c <= '9') {
1114 duration += c - '0';
1115 } else if (c >= 'a' && c <= 'f') {
1116 duration += c - 'a' + 10;
1117 } else if (c >= 'A' && c <= 'F') {
1118 duration += c - 'A' + 10;
1119 }
1120 }
1121 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
1122 }
1123
1124 public void init() {
1125 mLastStepTime = -1;
1126 mNumStepDurations = 0;
1127 }
1128
1129 public void clearTime() {
1130 mLastStepTime = -1;
1131 }
1132
1133 public long computeTimePerLevel() {
1134 final long[] steps = mStepDurations;
1135 final int numSteps = mNumStepDurations;
1136
1137 // For now we'll do a simple average across all steps.
1138 if (numSteps <= 0) {
1139 return -1;
1140 }
1141 long total = 0;
1142 for (int i=0; i<numSteps; i++) {
1143 total += steps[i] & STEP_LEVEL_TIME_MASK;
1144 }
1145 return total / numSteps;
1146 /*
1147 long[] buckets = new long[numSteps];
1148 int numBuckets = 0;
1149 int numToAverage = 4;
1150 int i = 0;
1151 while (i < numSteps) {
1152 long totalTime = 0;
1153 int num = 0;
1154 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1155 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1156 num++;
1157 }
1158 buckets[numBuckets] = totalTime / num;
1159 numBuckets++;
1160 numToAverage *= 2;
1161 i += num;
1162 }
1163 if (numBuckets < 1) {
1164 return -1;
1165 }
1166 long averageTime = buckets[numBuckets-1];
1167 for (i=numBuckets-2; i>=0; i--) {
1168 averageTime = (averageTime + buckets[i]) / 2;
1169 }
1170 return averageTime;
1171 */
1172 }
1173
1174 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1175 int[] outNumOfInterest) {
1176 final long[] steps = mStepDurations;
1177 final int count = mNumStepDurations;
1178 if (count <= 0) {
1179 return -1;
1180 }
1181 long total = 0;
1182 int numOfInterest = 0;
1183 for (int i=0; i<count; i++) {
1184 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1185 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1186 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1187 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1188 // If the modes of interest didn't change during this step period...
1189 if ((modMode&modesOfInterest) == 0) {
1190 // And the mode values during this period match those we are measuring...
1191 if ((initMode&modesOfInterest) == modeValues) {
1192 // Then this can be used to estimate the total time!
1193 numOfInterest++;
1194 total += steps[i] & STEP_LEVEL_TIME_MASK;
1195 }
1196 }
1197 }
1198 if (numOfInterest <= 0) {
1199 return -1;
1200 }
1201
1202 if (outNumOfInterest != null) {
1203 outNumOfInterest[0] = numOfInterest;
1204 }
1205
1206 // The estimated time is the average time we spend in each level, multipled
1207 // by 100 -- the total number of battery levels
1208 return (total / numOfInterest) * 100;
1209 }
1210
1211 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1212 int stepCount = mNumStepDurations;
1213 final long lastStepTime = mLastStepTime;
1214 if (lastStepTime >= 0 && numStepLevels > 0) {
1215 final long[] steps = mStepDurations;
1216 long duration = elapsedRealtime - lastStepTime;
1217 for (int i=0; i<numStepLevels; i++) {
1218 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1219 long thisDuration = duration / (numStepLevels-i);
1220 duration -= thisDuration;
1221 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1222 thisDuration = STEP_LEVEL_TIME_MASK;
1223 }
1224 steps[0] = thisDuration | modeBits;
1225 }
1226 stepCount += numStepLevels;
1227 if (stepCount > steps.length) {
1228 stepCount = steps.length;
1229 }
1230 }
1231 mNumStepDurations = stepCount;
1232 mLastStepTime = elapsedRealtime;
1233 }
1234
1235 public void readFromParcel(Parcel in) {
1236 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001237 if (N > mStepDurations.length) {
1238 throw new ParcelFormatException("more step durations than available: " + N);
1239 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001240 mNumStepDurations = N;
1241 for (int i=0; i<N; i++) {
1242 mStepDurations[i] = in.readLong();
1243 }
1244 }
1245
1246 public void writeToParcel(Parcel out) {
1247 final int N = mNumStepDurations;
1248 out.writeInt(N);
1249 for (int i=0; i<N; i++) {
1250 out.writeLong(mStepDurations[i]);
1251 }
1252 }
1253 }
1254
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001255 public static final class PackageChange {
1256 public String mPackageName;
1257 public boolean mUpdate;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001258 public long mVersionCode;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001259 }
1260
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001261 public static final class DailyItem {
1262 public long mStartTime;
1263 public long mEndTime;
1264 public LevelStepTracker mDischargeSteps;
1265 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001266 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001267 }
1268
1269 public abstract DailyItem getDailyItemLocked(int daysAgo);
1270
1271 public abstract long getCurrentDailyStartTime();
1272
1273 public abstract long getNextMinDailyDeadline();
1274
1275 public abstract long getNextMaxDailyDeadline();
1276
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001277 public abstract long[] getCpuFreqs();
1278
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001279 public final static class HistoryTag {
1280 public String string;
1281 public int uid;
1282
1283 public int poolIdx;
1284
1285 public void setTo(HistoryTag o) {
1286 string = o.string;
1287 uid = o.uid;
1288 poolIdx = o.poolIdx;
1289 }
1290
1291 public void setTo(String _string, int _uid) {
1292 string = _string;
1293 uid = _uid;
1294 poolIdx = -1;
1295 }
1296
1297 public void writeToParcel(Parcel dest, int flags) {
1298 dest.writeString(string);
1299 dest.writeInt(uid);
1300 }
1301
1302 public void readFromParcel(Parcel src) {
1303 string = src.readString();
1304 uid = src.readInt();
1305 poolIdx = -1;
1306 }
1307
1308 @Override
1309 public boolean equals(Object o) {
1310 if (this == o) return true;
1311 if (o == null || getClass() != o.getClass()) return false;
1312
1313 HistoryTag that = (HistoryTag) o;
1314
1315 if (uid != that.uid) return false;
1316 if (!string.equals(that.string)) return false;
1317
1318 return true;
1319 }
1320
1321 @Override
1322 public int hashCode() {
1323 int result = string.hashCode();
1324 result = 31 * result + uid;
1325 return result;
1326 }
1327 }
1328
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001329 /**
1330 * Optional detailed information that can go into a history step. This is typically
1331 * generated each time the battery level changes.
1332 */
1333 public final static class HistoryStepDetails {
1334 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1335 public int userTime;
1336 public int systemTime;
1337
1338 // Top three apps using CPU in the last step, with times in 1/100 second.
1339 public int appCpuUid1;
1340 public int appCpuUTime1;
1341 public int appCpuSTime1;
1342 public int appCpuUid2;
1343 public int appCpuUTime2;
1344 public int appCpuSTime2;
1345 public int appCpuUid3;
1346 public int appCpuUTime3;
1347 public int appCpuSTime3;
1348
1349 // Information from /proc/stat
1350 public int statUserTime;
1351 public int statSystemTime;
1352 public int statIOWaitTime;
1353 public int statIrqTime;
1354 public int statSoftIrqTime;
1355 public int statIdlTime;
1356
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001357 // Platform-level low power state stats
1358 public String statPlatformIdleState;
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001359 public String statSubsystemPowerState;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001360
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001361 public HistoryStepDetails() {
1362 clear();
1363 }
1364
1365 public void clear() {
1366 userTime = systemTime = 0;
1367 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1368 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1369 = appCpuUTime3 = appCpuSTime3 = 0;
1370 }
1371
1372 public void writeToParcel(Parcel out) {
1373 out.writeInt(userTime);
1374 out.writeInt(systemTime);
1375 out.writeInt(appCpuUid1);
1376 out.writeInt(appCpuUTime1);
1377 out.writeInt(appCpuSTime1);
1378 out.writeInt(appCpuUid2);
1379 out.writeInt(appCpuUTime2);
1380 out.writeInt(appCpuSTime2);
1381 out.writeInt(appCpuUid3);
1382 out.writeInt(appCpuUTime3);
1383 out.writeInt(appCpuSTime3);
1384 out.writeInt(statUserTime);
1385 out.writeInt(statSystemTime);
1386 out.writeInt(statIOWaitTime);
1387 out.writeInt(statIrqTime);
1388 out.writeInt(statSoftIrqTime);
1389 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001390 out.writeString(statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001391 out.writeString(statSubsystemPowerState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001392 }
1393
1394 public void readFromParcel(Parcel in) {
1395 userTime = in.readInt();
1396 systemTime = in.readInt();
1397 appCpuUid1 = in.readInt();
1398 appCpuUTime1 = in.readInt();
1399 appCpuSTime1 = in.readInt();
1400 appCpuUid2 = in.readInt();
1401 appCpuUTime2 = in.readInt();
1402 appCpuSTime2 = in.readInt();
1403 appCpuUid3 = in.readInt();
1404 appCpuUTime3 = in.readInt();
1405 appCpuSTime3 = in.readInt();
1406 statUserTime = in.readInt();
1407 statSystemTime = in.readInt();
1408 statIOWaitTime = in.readInt();
1409 statIrqTime = in.readInt();
1410 statSoftIrqTime = in.readInt();
1411 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001412 statPlatformIdleState = in.readString();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001413 statSubsystemPowerState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001414 }
1415 }
1416
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001417 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001418 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001419
1420 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001421 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001422
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001423 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001424 public static final byte CMD_NULL = -1;
1425 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001426 public static final byte CMD_CURRENT_TIME = 5;
1427 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001428 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001429 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001430
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001431 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001432
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001433 /**
1434 * Return whether the command code is a delta data update.
1435 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001436 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001437 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001438 }
1439
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001440 public byte batteryLevel;
1441 public byte batteryStatus;
1442 public byte batteryHealth;
1443 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001444
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001445 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001446 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001447
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001448 // The charge of the battery in micro-Ampere-hours.
1449 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001450
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001451 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001452 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001453 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001454 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001455 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1456 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001457 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001458 public static final int STATE_PHONE_STATE_SHIFT = 6;
1459 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001460 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001461 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001462 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001463
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001464 // These states always appear directly in the first int token
1465 // of a delta change; they should be ones that change relatively
1466 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001467 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1468 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001469 public static final int STATE_GPS_ON_FLAG = 1<<29;
1470 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001471 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001472 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001473 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001474 // Do not use, this is used for coulomb delta count.
1475 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001476 // These are on the lower bits used for the command; if they change
1477 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001478 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001479 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001480 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001481 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1482 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
Mike Mac2f518a2017-09-19 16:06:03 -07001483 public static final int STATE_SCREEN_DOZE_FLAG = 1 << 18;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001484 // empty slot
1485 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001486
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001487 public static final int MOST_INTERESTING_STATES =
Mike Mac2f518a2017-09-19 16:06:03 -07001488 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG | STATE_SCREEN_DOZE_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001489
1490 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001491
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001492 public int states;
1493
Dianne Hackborn3251b902014-06-20 14:40:53 -07001494 // Constants from WIFI_SUPPL_STATE_*
1495 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1496 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1497 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1498 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1499 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1500 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
Siddharth Ray78ccaf52017-12-23 16:16:21 -08001501 // Values for NUM_GPS_SIGNAL_QUALITY_LEVELS
1502 public static final int STATE2_GPS_SIGNAL_QUALITY_SHIFT = 7;
1503 public static final int STATE2_GPS_SIGNAL_QUALITY_MASK =
1504 0x1 << STATE2_GPS_SIGNAL_QUALITY_SHIFT;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001505
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001506 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001507 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1508 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1509 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001510 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001511 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1512 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1513 public static final int STATE2_CHARGING_FLAG = 1<<24;
1514 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1515 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1516 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001517 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001518
1519 public static final int MOST_INTERESTING_STATES2 =
Mike Mac2f518a2017-09-19 16:06:03 -07001520 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
1521 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001522
1523 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001524
Dianne Hackborn40c87252014-03-19 16:55:40 -07001525 public int states2;
1526
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001527 // The wake lock that was acquired at this point.
1528 public HistoryTag wakelockTag;
1529
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001530 // Kernel wakeup reason at this point.
1531 public HistoryTag wakeReasonTag;
1532
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001533 // Non-null when there is more detailed information at this step.
1534 public HistoryStepDetails stepDetails;
1535
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001536 public static final int EVENT_FLAG_START = 0x8000;
1537 public static final int EVENT_FLAG_FINISH = 0x4000;
1538
1539 // No event in this item.
1540 public static final int EVENT_NONE = 0x0000;
1541 // Event is about a process that is running.
1542 public static final int EVENT_PROC = 0x0001;
1543 // Event is about an application package that is in the foreground.
1544 public static final int EVENT_FOREGROUND = 0x0002;
1545 // Event is about an application package that is at the top of the screen.
1546 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001547 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001548 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001549 // Events for all additional wake locks aquired/release within a wake block.
1550 // These are not generated by default.
1551 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001552 // Event is about an application executing a scheduled job.
1553 public static final int EVENT_JOB = 0x0006;
1554 // Events for users running.
1555 public static final int EVENT_USER_RUNNING = 0x0007;
1556 // Events for foreground user.
1557 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001558 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001559 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001560 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001561 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001562 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001563 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001564 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001565 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001566 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001567 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001568 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001569 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001570 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001571 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001572 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001573 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001574 // Event for a package being on the temporary whitelist.
1575 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001576 // Event for the screen waking up.
1577 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001578 // Event for the UID that woke up the application processor.
1579 // Used for wakeups coming from WiFi, modem, etc.
1580 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001581 // Event for reporting that a specific partial wake lock has been held for a long duration.
1582 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001583
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001584 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001585 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001586 // Mask to extract out only the type part of the event.
1587 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001588
1589 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1590 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1591 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1592 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1593 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1594 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001595 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1596 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001597 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1598 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001599 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1600 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1601 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1602 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1603 public static final int EVENT_USER_FOREGROUND_START =
1604 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1605 public static final int EVENT_USER_FOREGROUND_FINISH =
1606 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001607 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1608 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001609 public static final int EVENT_TEMP_WHITELIST_START =
1610 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1611 public static final int EVENT_TEMP_WHITELIST_FINISH =
1612 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001613 public static final int EVENT_LONG_WAKE_LOCK_START =
1614 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1615 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1616 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001617
1618 // For CMD_EVENT.
1619 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001620 public HistoryTag eventTag;
1621
Dianne Hackborn9a755432014-05-15 17:05:22 -07001622 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001623 public long currentTime;
1624
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001625 // Meta-data when reading.
1626 public int numReadInts;
1627
1628 // Pre-allocated objects.
1629 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001630 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001631 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001632
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001633 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001634 }
Bookatzc8c44962017-05-11 12:12:54 -07001635
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001636 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001637 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001638 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001639 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001640 }
Bookatzc8c44962017-05-11 12:12:54 -07001641
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001642 public int describeContents() {
1643 return 0;
1644 }
1645
1646 public void writeToParcel(Parcel dest, int flags) {
1647 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001648 int bat = (((int)cmd)&0xff)
1649 | ((((int)batteryLevel)<<8)&0xff00)
1650 | ((((int)batteryStatus)<<16)&0xf0000)
1651 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001652 | ((((int)batteryPlugType)<<24)&0xf000000)
1653 | (wakelockTag != null ? 0x10000000 : 0)
1654 | (wakeReasonTag != null ? 0x20000000 : 0)
1655 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001656 dest.writeInt(bat);
1657 bat = (((int)batteryTemperature)&0xffff)
1658 | ((((int)batteryVoltage)<<16)&0xffff0000);
1659 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001660 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001661 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001662 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001663 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001664 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001665 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001666 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001667 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001668 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001669 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001670 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001671 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001672 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001673 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001674 dest.writeLong(currentTime);
1675 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001676 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001677
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001678 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001679 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001680 int bat = src.readInt();
1681 cmd = (byte)(bat&0xff);
1682 batteryLevel = (byte)((bat>>8)&0xff);
1683 batteryStatus = (byte)((bat>>16)&0xf);
1684 batteryHealth = (byte)((bat>>20)&0xf);
1685 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001686 int bat2 = src.readInt();
1687 batteryTemperature = (short)(bat2&0xffff);
1688 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001689 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001690 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001691 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001692 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001693 wakelockTag = localWakelockTag;
1694 wakelockTag.readFromParcel(src);
1695 } else {
1696 wakelockTag = null;
1697 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001698 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001699 wakeReasonTag = localWakeReasonTag;
1700 wakeReasonTag.readFromParcel(src);
1701 } else {
1702 wakeReasonTag = null;
1703 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001704 if ((bat&0x40000000) != 0) {
1705 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001706 eventTag = localEventTag;
1707 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001708 } else {
1709 eventCode = EVENT_NONE;
1710 eventTag = null;
1711 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001712 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001713 currentTime = src.readLong();
1714 } else {
1715 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001716 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001717 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001718 }
1719
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001720 public void clear() {
1721 time = 0;
1722 cmd = CMD_NULL;
1723 batteryLevel = 0;
1724 batteryStatus = 0;
1725 batteryHealth = 0;
1726 batteryPlugType = 0;
1727 batteryTemperature = 0;
1728 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001729 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001730 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001731 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001732 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001733 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001734 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001735 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001736 }
Bookatzc8c44962017-05-11 12:12:54 -07001737
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001738 public void setTo(HistoryItem o) {
1739 time = o.time;
1740 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001741 setToCommon(o);
1742 }
1743
1744 public void setTo(long time, byte cmd, HistoryItem o) {
1745 this.time = time;
1746 this.cmd = cmd;
1747 setToCommon(o);
1748 }
1749
1750 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001751 batteryLevel = o.batteryLevel;
1752 batteryStatus = o.batteryStatus;
1753 batteryHealth = o.batteryHealth;
1754 batteryPlugType = o.batteryPlugType;
1755 batteryTemperature = o.batteryTemperature;
1756 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001757 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001758 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001759 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001760 if (o.wakelockTag != null) {
1761 wakelockTag = localWakelockTag;
1762 wakelockTag.setTo(o.wakelockTag);
1763 } else {
1764 wakelockTag = null;
1765 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001766 if (o.wakeReasonTag != null) {
1767 wakeReasonTag = localWakeReasonTag;
1768 wakeReasonTag.setTo(o.wakeReasonTag);
1769 } else {
1770 wakeReasonTag = null;
1771 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001772 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001773 if (o.eventTag != null) {
1774 eventTag = localEventTag;
1775 eventTag.setTo(o.eventTag);
1776 } else {
1777 eventTag = null;
1778 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001779 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001780 }
1781
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001782 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001783 return batteryLevel == o.batteryLevel
1784 && batteryStatus == o.batteryStatus
1785 && batteryHealth == o.batteryHealth
1786 && batteryPlugType == o.batteryPlugType
1787 && batteryTemperature == o.batteryTemperature
1788 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001789 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001790 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001791 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001792 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001793 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001794
1795 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001796 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001797 return false;
1798 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001799 if (wakelockTag != o.wakelockTag) {
1800 if (wakelockTag == null || o.wakelockTag == null) {
1801 return false;
1802 }
1803 if (!wakelockTag.equals(o.wakelockTag)) {
1804 return false;
1805 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001806 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001807 if (wakeReasonTag != o.wakeReasonTag) {
1808 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1809 return false;
1810 }
1811 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1812 return false;
1813 }
1814 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001815 if (eventTag != o.eventTag) {
1816 if (eventTag == null || o.eventTag == null) {
1817 return false;
1818 }
1819 if (!eventTag.equals(o.eventTag)) {
1820 return false;
1821 }
1822 }
1823 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001824 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001825 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001826
1827 public final static class HistoryEventTracker {
1828 private final HashMap<String, SparseIntArray>[] mActiveEvents
1829 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1830
1831 public boolean updateState(int code, String name, int uid, int poolIdx) {
1832 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1833 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1834 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1835 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001836 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001837 mActiveEvents[idx] = active;
1838 }
1839 SparseIntArray uids = active.get(name);
1840 if (uids == null) {
1841 uids = new SparseIntArray();
1842 active.put(name, uids);
1843 }
1844 if (uids.indexOfKey(uid) >= 0) {
1845 // Already set, nothing to do!
1846 return false;
1847 }
1848 uids.put(uid, poolIdx);
1849 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1850 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1851 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1852 if (active == null) {
1853 // not currently active, nothing to do.
1854 return false;
1855 }
1856 SparseIntArray uids = active.get(name);
1857 if (uids == null) {
1858 // not currently active, nothing to do.
1859 return false;
1860 }
1861 idx = uids.indexOfKey(uid);
1862 if (idx < 0) {
1863 // not currently active, nothing to do.
1864 return false;
1865 }
1866 uids.removeAt(idx);
1867 if (uids.size() <= 0) {
1868 active.remove(name);
1869 }
1870 }
1871 return true;
1872 }
1873
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001874 public void removeEvents(int code) {
1875 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1876 mActiveEvents[idx] = null;
1877 }
1878
Dianne Hackborn37de0982014-05-09 09:32:18 -07001879 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1880 return mActiveEvents[code];
1881 }
1882 }
1883
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001884 public static final class BitDescription {
1885 public final int mask;
1886 public final int shift;
1887 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001888 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001889 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001890 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07001891
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001892 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001893 this.mask = mask;
1894 this.shift = -1;
1895 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001896 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001897 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001898 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001899 }
Bookatzc8c44962017-05-11 12:12:54 -07001900
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001901 public BitDescription(int mask, int shift, String name, String shortName,
1902 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001903 this.mask = mask;
1904 this.shift = shift;
1905 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001906 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001907 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001908 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001909 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001910 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001911
Dianne Hackbornfc064132014-06-02 12:42:12 -07001912 /**
1913 * Don't allow any more batching in to the current history event. This
1914 * is called when printing partial histories, so to ensure that the next
1915 * history event will go in to a new batch after what was printed in the
1916 * last partial history.
1917 */
1918 public abstract void commitCurrentHistoryBatchLocked();
1919
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001920 public abstract int getHistoryTotalSize();
1921
1922 public abstract int getHistoryUsedSize();
1923
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001924 public abstract boolean startIteratingHistoryLocked();
1925
Dianne Hackborn099bc622014-01-22 13:39:16 -08001926 public abstract int getHistoryStringPoolSize();
1927
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001928 public abstract int getHistoryStringPoolBytes();
1929
1930 public abstract String getHistoryTagPoolString(int index);
1931
1932 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001933
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001934 public abstract boolean getNextHistoryLocked(HistoryItem out);
1935
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001936 public abstract void finishIteratingHistoryLocked();
1937
1938 public abstract boolean startIteratingOldHistoryLocked();
1939
1940 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1941
1942 public abstract void finishIteratingOldHistoryLocked();
1943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001945 * Return the base time offset for the battery history.
1946 */
1947 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07001948
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001949 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 * Returns the number of times the device has been started.
1951 */
1952 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07001953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001955 * 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 -08001956 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07001957 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 * {@hide}
1959 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001960 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07001961
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001962 /**
1963 * Returns the number of times the screen was turned on.
1964 *
1965 * {@hide}
1966 */
1967 public abstract int getScreenOnCount(int which);
1968
Mike Mac2f518a2017-09-19 16:06:03 -07001969 /**
1970 * Returns the time in microseconds that the screen has been dozing while the device was
1971 * running on battery.
1972 *
1973 * {@hide}
1974 */
1975 public abstract long getScreenDozeTime(long elapsedRealtimeUs, int which);
1976
1977 /**
1978 * Returns the number of times the screen was turned dozing.
1979 *
1980 * {@hide}
1981 */
1982 public abstract int getScreenDozeCount(int which);
1983
Jeff Browne95c3cd2014-05-02 16:59:26 -07001984 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1985
Dianne Hackborn617f8772009-03-31 15:04:46 -07001986 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1987 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1988 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1989 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1990 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07001991
Dianne Hackborn617f8772009-03-31 15:04:46 -07001992 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1993 "dark", "dim", "medium", "light", "bright"
1994 };
Bookatzc8c44962017-05-11 12:12:54 -07001995
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001996 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1997 "0", "1", "2", "3", "4"
1998 };
1999
Dianne Hackborn617f8772009-03-31 15:04:46 -07002000 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07002001
Dianne Hackborn617f8772009-03-31 15:04:46 -07002002 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002003 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07002004 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07002005 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002006 * {@hide}
2007 */
2008 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002009 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002012 * Returns the {@link Timer} object that tracks the given screen brightness.
2013 *
2014 * {@hide}
2015 */
2016 public abstract Timer getScreenBrightnessTimer(int brightnessBin);
2017
2018 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002019 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002020 * running on battery.
2021 *
2022 * {@hide}
2023 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002024 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002025
2026 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002027 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002028 *
2029 * {@hide}
2030 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002031 public abstract int getPowerSaveModeEnabledCount(int which);
2032
2033 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002034 * Constant for device idle mode: not active.
2035 */
2036 public static final int DEVICE_IDLE_MODE_OFF = 0;
2037
2038 /**
2039 * Constant for device idle mode: active in lightweight mode.
2040 */
2041 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
2042
2043 /**
2044 * Constant for device idle mode: active in full mode.
2045 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002046 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002047
2048 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002049 * Returns the time in microseconds that device has been in idle mode while
2050 * running on battery.
2051 *
2052 * {@hide}
2053 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002054 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002055
2056 /**
2057 * Returns the number of times that the devie has gone in to idle mode.
2058 *
2059 * {@hide}
2060 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002061 public abstract int getDeviceIdleModeCount(int mode, int which);
2062
2063 /**
2064 * Return the longest duration we spent in a particular device idle mode (fully in the
2065 * mode, not in idle maintenance etc).
2066 */
2067 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002068
2069 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002070 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002071 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002072 * counts all of the time that we consider the device to be idle, whether or not
2073 * it is currently in the actual device idle mode.
2074 *
2075 * {@hide}
2076 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002077 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002078
2079 /**
Bookatz8c6571b2017-10-24 15:04:41 -07002080 * Returns the number of times that the device has started idling.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002081 *
2082 * {@hide}
2083 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002084 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002085
2086 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002087 * Returns the number of times that connectivity state changed.
2088 *
2089 * {@hide}
2090 */
2091 public abstract int getNumConnectivityChange(int which);
2092
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002093
2094 /**
2095 * Returns the time in microseconds that the phone has been running with
2096 * the given GPS signal quality level
2097 *
2098 * {@hide}
2099 */
2100 public abstract long getGpsSignalQualityTime(int strengthBin,
2101 long elapsedRealtimeUs, int which);
2102
2103 /**
2104 * Returns the GPS battery drain in mA-ms
2105 *
2106 * {@hide}
2107 */
2108 public abstract long getGpsBatteryDrainMaMs();
2109
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002110 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002111 * 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 -08002112 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002113 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 * {@hide}
2115 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002116 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002117
Dianne Hackborn627bba72009-03-24 22:32:56 -07002118 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002119 * Returns the number of times a phone call was activated.
2120 *
2121 * {@hide}
2122 */
2123 public abstract int getPhoneOnCount(int which);
2124
2125 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002126 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002127 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002128 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002129 * {@hide}
2130 */
2131 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002132 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002133
Dianne Hackborn617f8772009-03-31 15:04:46 -07002134 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07002135 * Returns the time in microseconds that the phone has been trying to
2136 * acquire a signal.
2137 *
2138 * {@hide}
2139 */
2140 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002141 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002142
2143 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002144 * Returns the {@link Timer} object that tracks how much the phone has been trying to
2145 * acquire a signal.
2146 *
2147 * {@hide}
2148 */
2149 public abstract Timer getPhoneSignalScanningTimer();
2150
2151 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002152 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002153 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002154 * {@hide}
2155 */
2156 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
2157
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002158 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002159 * Return the {@link Timer} object used to track the given signal strength's duration and
2160 * counts.
2161 */
2162 protected abstract Timer getPhoneSignalStrengthTimer(int strengthBin);
2163
2164 /**
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002165 * Returns the time in microseconds that the mobile network has been active
2166 * (in a high power state).
2167 *
2168 * {@hide}
2169 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002170 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002171
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002172 /**
2173 * Returns the number of times that the mobile network has transitioned to the
2174 * active state.
2175 *
2176 * {@hide}
2177 */
2178 public abstract int getMobileRadioActiveCount(int which);
2179
2180 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002181 * Returns the time in microseconds that is the difference between the mobile radio
2182 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
2183 * from the radio.
2184 *
2185 * {@hide}
2186 */
2187 public abstract long getMobileRadioActiveAdjustedTime(int which);
2188
2189 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002190 * Returns the time in microseconds that the mobile network has been active
2191 * (in a high power state) but not being able to blame on an app.
2192 *
2193 * {@hide}
2194 */
2195 public abstract long getMobileRadioActiveUnknownTime(int which);
2196
2197 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002198 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002199 *
2200 * {@hide}
2201 */
2202 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002203
Dianne Hackborn627bba72009-03-24 22:32:56 -07002204 public static final int DATA_CONNECTION_NONE = 0;
2205 public static final int DATA_CONNECTION_GPRS = 1;
2206 public static final int DATA_CONNECTION_EDGE = 2;
2207 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002208 public static final int DATA_CONNECTION_CDMA = 4;
2209 public static final int DATA_CONNECTION_EVDO_0 = 5;
2210 public static final int DATA_CONNECTION_EVDO_A = 6;
2211 public static final int DATA_CONNECTION_1xRTT = 7;
2212 public static final int DATA_CONNECTION_HSDPA = 8;
2213 public static final int DATA_CONNECTION_HSUPA = 9;
2214 public static final int DATA_CONNECTION_HSPA = 10;
2215 public static final int DATA_CONNECTION_IDEN = 11;
2216 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002217 public static final int DATA_CONNECTION_LTE = 13;
2218 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08002219 public static final int DATA_CONNECTION_HSPAP = 15;
2220 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002221
Dianne Hackborn627bba72009-03-24 22:32:56 -07002222 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002223 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002224 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08002225 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002226 };
Bookatzc8c44962017-05-11 12:12:54 -07002227
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002228 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002229
Dianne Hackborn627bba72009-03-24 22:32:56 -07002230 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002231 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002232 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002233 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002234 * {@hide}
2235 */
2236 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002237 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002240 * Returns the number of times the phone has entered the given data
2241 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002242 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002243 * {@hide}
2244 */
2245 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002246
Kweku Adams87b19ec2017-10-09 12:40:03 -07002247 /**
2248 * Returns the {@link Timer} object that tracks the phone's data connection type stats.
2249 */
2250 public abstract Timer getPhoneDataConnectionTimer(int dataType);
2251
Dianne Hackborn3251b902014-06-20 14:40:53 -07002252 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2253 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2254 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2255 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2256 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2257 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2258 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2259 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2260 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2261 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2262 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2263 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2264 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2265
2266 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2267
2268 static final String[] WIFI_SUPPL_STATE_NAMES = {
2269 "invalid", "disconn", "disabled", "inactive", "scanning",
2270 "authenticating", "associating", "associated", "4-way-handshake",
2271 "group-handshake", "completed", "dormant", "uninit"
2272 };
2273
2274 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2275 "inv", "dsc", "dis", "inact", "scan",
2276 "auth", "ascing", "asced", "4-way",
2277 "group", "compl", "dorm", "uninit"
2278 };
2279
Mike Mac2f518a2017-09-19 16:06:03 -07002280 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002281 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002282 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2283 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002284 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002285 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2286 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2287 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002288 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002289 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002290 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002291 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002292 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2293 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Mike Mac2f518a2017-09-19 16:06:03 -07002294 new BitDescription(HistoryItem.STATE_SCREEN_DOZE_FLAG, "screen_doze", "Sd"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002295 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2296 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2297 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2298 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2299 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2300 new String[] {"in", "out", "emergency", "off"},
2301 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002302 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2303 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2304 SignalStrength.SIGNAL_STRENGTH_NAMES,
2305 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002306 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2307 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2308 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002309 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002310
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002311 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2312 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002313 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002314 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002315 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002316 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002317 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002318 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2319 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2320 new String[] { "off", "light", "full", "???" },
2321 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002322 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2323 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2324 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002325 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2326 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2327 new String[] { "0", "1", "2", "3", "4" },
2328 new String[] { "0", "1", "2", "3", "4" }),
2329 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2330 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2331 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002332 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002333 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002334 new BitDescription(HistoryItem.STATE2_GPS_SIGNAL_QUALITY_MASK,
2335 HistoryItem.STATE2_GPS_SIGNAL_QUALITY_SHIFT, "gps_signal_quality", "Gss",
2336 new String[] { "poor", "good"}, new String[] { "poor", "good"}),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002337 };
2338
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002339 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002340 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002341 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2342 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002343 };
2344
2345 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002346 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002347 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002348 "Esw", "Ewa", "Elw", "Eec"
2349 };
2350
2351 @FunctionalInterface
2352 public interface IntToString {
2353 String applyAsString(int val);
2354 }
2355
2356 private static final IntToString sUidToString = UserHandle::formatUid;
2357 private static final IntToString sIntToString = Integer::toString;
2358
2359 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2360 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2361 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2362 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2363 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002364 };
2365
Dianne Hackborn617f8772009-03-31 15:04:46 -07002366 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08002367 * Returns total time for WiFi Multicast Wakelock timer.
2368 * Note that this may be different from the sum of per uid timer values.
2369 *
2370 * {@hide}
2371 */
2372 public abstract long getWifiMulticastWakelockTime(long elapsedRealtimeUs, int which);
2373
2374 /**
2375 * Returns total time for WiFi Multicast Wakelock timer
2376 * Note that this may be different from the sum of per uid timer values.
2377 *
2378 * {@hide}
2379 */
2380 public abstract int getWifiMulticastWakelockCount(int which);
2381
2382 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002383 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002384 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002385 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002386 * {@hide}
2387 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002388 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002389
2390 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002391 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002392 * been in the running state while the device was running on battery.
2393 *
2394 * {@hide}
2395 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002396 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002397
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002398 public static final int WIFI_STATE_OFF = 0;
2399 public static final int WIFI_STATE_OFF_SCANNING = 1;
2400 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2401 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2402 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2403 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2404 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2405 public static final int WIFI_STATE_SOFT_AP = 7;
2406
2407 static final String[] WIFI_STATE_NAMES = {
2408 "off", "scanning", "no_net", "disconn",
2409 "sta", "p2p", "sta_p2p", "soft_ap"
2410 };
2411
2412 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2413
2414 /**
2415 * Returns the time in microseconds that WiFi has been running in the given state.
2416 *
2417 * {@hide}
2418 */
2419 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002420 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002421
2422 /**
2423 * Returns the number of times that WiFi has entered the given state.
2424 *
2425 * {@hide}
2426 */
2427 public abstract int getWifiStateCount(int wifiState, int which);
2428
The Android Open Source Project10592532009-03-18 17:39:46 -07002429 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002430 * Returns the {@link Timer} object that tracks the given WiFi state.
2431 *
2432 * {@hide}
2433 */
2434 public abstract Timer getWifiStateTimer(int wifiState);
2435
2436 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002437 * Returns the time in microseconds that the wifi supplicant has been
2438 * in a given state.
2439 *
2440 * {@hide}
2441 */
2442 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2443
2444 /**
2445 * Returns the number of times that the wifi supplicant has transitioned
2446 * to a given state.
2447 *
2448 * {@hide}
2449 */
2450 public abstract int getWifiSupplStateCount(int state, int which);
2451
Kweku Adams87b19ec2017-10-09 12:40:03 -07002452 /**
2453 * Returns the {@link Timer} object that tracks the given wifi supplicant state.
2454 *
2455 * {@hide}
2456 */
2457 public abstract Timer getWifiSupplStateTimer(int state);
2458
Dianne Hackborn3251b902014-06-20 14:40:53 -07002459 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2460
2461 /**
2462 * Returns the time in microseconds that WIFI has been running with
2463 * the given signal strength.
2464 *
2465 * {@hide}
2466 */
2467 public abstract long getWifiSignalStrengthTime(int strengthBin,
2468 long elapsedRealtimeUs, int which);
2469
2470 /**
2471 * Returns the number of times WIFI has entered the given signal strength.
2472 *
2473 * {@hide}
2474 */
2475 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2476
2477 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002478 * Returns the {@link Timer} object that tracks the given WIFI signal strength.
2479 *
2480 * {@hide}
2481 */
2482 public abstract Timer getWifiSignalStrengthTimer(int strengthBin);
2483
2484 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002485 * Returns the time in microseconds that the flashlight has been on while the device was
2486 * running on battery.
2487 *
2488 * {@hide}
2489 */
2490 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2491
2492 /**
2493 * Returns the number of times that the flashlight has been turned on while the device was
2494 * running on battery.
2495 *
2496 * {@hide}
2497 */
2498 public abstract long getFlashlightOnCount(int which);
2499
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002500 /**
2501 * Returns the time in microseconds that the camera has been on while the device was
2502 * running on battery.
2503 *
2504 * {@hide}
2505 */
2506 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2507
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002508 /**
2509 * Returns the time in microseconds that bluetooth scans were running while the device was
2510 * on battery.
2511 *
2512 * {@hide}
2513 */
2514 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002515
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002516 public static final int NETWORK_MOBILE_RX_DATA = 0;
2517 public static final int NETWORK_MOBILE_TX_DATA = 1;
2518 public static final int NETWORK_WIFI_RX_DATA = 2;
2519 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002520 public static final int NETWORK_BT_RX_DATA = 4;
2521 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002522 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2523 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2524 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2525 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2526 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002527
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002528 public abstract long getNetworkActivityBytes(int type, int which);
2529 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002530
Adam Lesinskie08af192015-03-25 16:42:59 -07002531 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002532 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002533 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002534 * actual power data.
2535 */
2536 public abstract boolean hasWifiActivityReporting();
2537
2538 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002539 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2540 * in various radio controller states, such as transmit, receive, and idle.
2541 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002542 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002543 public abstract ControllerActivityCounter getWifiControllerActivity();
2544
2545 /**
2546 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2547 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2548 * actual power data.
2549 */
2550 public abstract boolean hasBluetoothActivityReporting();
2551
2552 /**
2553 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2554 * in various radio controller states, such as transmit, receive, and idle.
2555 * @return non-null {@link ControllerActivityCounter}
2556 */
2557 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2558
2559 /**
2560 * Returns true if the BatteryStats object has detailed modem power reports.
2561 * When true, calling {@link #getModemControllerActivity()} will yield the
2562 * actual power data.
2563 */
2564 public abstract boolean hasModemActivityReporting();
2565
2566 /**
2567 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2568 * in various radio controller states, such as transmit, receive, and idle.
2569 * @return non-null {@link ControllerActivityCounter}
2570 */
2571 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002572
The Android Open Source Project10592532009-03-18 17:39:46 -07002573 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002574 * Return the wall clock time when battery stats data collection started.
2575 */
2576 public abstract long getStartClockTime();
2577
2578 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002579 * Return platform version tag that we were running in when the battery stats started.
2580 */
2581 public abstract String getStartPlatformVersion();
2582
2583 /**
2584 * Return platform version tag that we were running in when the battery stats ended.
2585 */
2586 public abstract String getEndPlatformVersion();
2587
2588 /**
2589 * Return the internal version code of the parcelled format.
2590 */
2591 public abstract int getParcelVersion();
2592
2593 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 * Return whether we are currently running on battery.
2595 */
2596 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 /**
2599 * Returns a SparseArray containing the statistics for each uid.
2600 */
2601 public abstract SparseArray<? extends Uid> getUidStats();
2602
2603 /**
2604 * Returns the current battery uptime in microseconds.
2605 *
2606 * @param curTime the amount of elapsed realtime in microseconds.
2607 */
2608 public abstract long getBatteryUptime(long curTime);
2609
2610 /**
2611 * Returns the current battery realtime in microseconds.
2612 *
2613 * @param curTime the amount of elapsed realtime in microseconds.
2614 */
2615 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002616
The Android Open Source Project10592532009-03-18 17:39:46 -07002617 /**
Evan Millar633a1742009-04-02 16:36:33 -07002618 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002619 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002620 */
Evan Millar633a1742009-04-02 16:36:33 -07002621 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002622
The Android Open Source Project10592532009-03-18 17:39:46 -07002623 /**
Evan Millar633a1742009-04-02 16:36:33 -07002624 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2625 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002626 */
Evan Millar633a1742009-04-02 16:36:33 -07002627 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628
2629 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002630 * Get the amount the battery has discharged since the stats were
2631 * last reset after charging, as a lower-end approximation.
2632 */
2633 public abstract int getLowDischargeAmountSinceCharge();
2634
2635 /**
2636 * Get the amount the battery has discharged since the stats were
2637 * last reset after charging, as an upper-end approximation.
2638 */
2639 public abstract int getHighDischargeAmountSinceCharge();
2640
2641 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002642 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2643 */
2644 public abstract int getDischargeAmount(int which);
2645
2646 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002647 * Get the amount the battery has discharged while the screen was on,
2648 * since the last time power was unplugged.
2649 */
2650 public abstract int getDischargeAmountScreenOn();
2651
2652 /**
2653 * Get the amount the battery has discharged while the screen was on,
2654 * since the last time the device was charged.
2655 */
2656 public abstract int getDischargeAmountScreenOnSinceCharge();
2657
2658 /**
2659 * Get the amount the battery has discharged while the screen was off,
2660 * since the last time power was unplugged.
2661 */
2662 public abstract int getDischargeAmountScreenOff();
2663
2664 /**
2665 * Get the amount the battery has discharged while the screen was off,
2666 * since the last time the device was charged.
2667 */
2668 public abstract int getDischargeAmountScreenOffSinceCharge();
2669
2670 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002671 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002672 * since the last time power was unplugged.
2673 */
2674 public abstract int getDischargeAmountScreenDoze();
2675
2676 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002677 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002678 * since the last time the device was charged.
2679 */
2680 public abstract int getDischargeAmountScreenDozeSinceCharge();
2681
2682 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 * Returns the total, last, or current battery uptime in microseconds.
2684 *
2685 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002686 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 */
2688 public abstract long computeBatteryUptime(long curTime, int which);
2689
2690 /**
2691 * Returns the total, last, or current battery realtime in microseconds.
2692 *
2693 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002694 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 */
2696 public abstract long computeBatteryRealtime(long curTime, int which);
2697
2698 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002699 * Returns the total, last, or current battery screen off/doze uptime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002700 *
2701 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002702 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002703 */
2704 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2705
2706 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002707 * Returns the total, last, or current battery screen off/doze realtime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002708 *
2709 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002710 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002711 */
2712 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2713
2714 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 * Returns the total, last, or current uptime in microseconds.
2716 *
2717 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002718 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 */
2720 public abstract long computeUptime(long curTime, int which);
2721
2722 /**
2723 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002724 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002726 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 */
2728 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002729
2730 /**
2731 * Compute an approximation for how much run time (in microseconds) is remaining on
2732 * the battery. Returns -1 if no time can be computed: either there is not
2733 * enough current data to make a decision, or the battery is currently
2734 * charging.
2735 *
2736 * @param curTime The current elepsed realtime in microseconds.
2737 */
2738 public abstract long computeBatteryTimeRemaining(long curTime);
2739
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002740 // The part of a step duration that is the actual time.
2741 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2742
2743 // Bits in a step duration that are the new battery level we are at.
2744 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002745 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002746
2747 // Bits in a step duration that are the initial mode we were in at that step.
2748 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002749 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002750
2751 // Bits in a step duration that indicate which modes changed during that step.
2752 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002753 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002754
2755 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2756 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2757
Santos Cordone94f0502017-02-24 12:31:20 -08002758 // The largest value for screen state that is tracked in battery states. Any values above
2759 // this should be mapped back to one of the tracked values before being tracked here.
2760 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2761
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002762 // Step duration mode: power save is on.
2763 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2764
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002765 // Step duration mode: device is currently in idle mode.
2766 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2767
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002768 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2769 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002770 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2771 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002772 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2773 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2774 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2775 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2776 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002777 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2778 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002779 };
2780 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2781 (Display.STATE_OFF-1),
2782 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002783 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002784 (Display.STATE_ON-1),
2785 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2786 (Display.STATE_DOZE-1),
2787 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2788 (Display.STATE_DOZE_SUSPEND-1),
2789 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002790 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002791 };
2792 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2793 "screen off",
2794 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002795 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002796 "screen on",
2797 "screen on power save",
2798 "screen doze",
2799 "screen doze power save",
2800 "screen doze-suspend",
2801 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002802 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002803 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002804
2805 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002806 * Return the amount of battery discharge while the screen was off, measured in
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002807 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2808 * a coulomb counter.
2809 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002810 public abstract long getUahDischargeScreenOff(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002811
2812 /**
2813 * Return the amount of battery discharge while the screen was in doze mode, measured in
2814 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2815 * a coulomb counter.
2816 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002817 public abstract long getUahDischargeScreenDoze(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002818
2819 /**
2820 * Return the amount of battery discharge measured in micro-Ampere-hours. This will be
2821 * non-zero only if the device's battery has a coulomb counter.
2822 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002823 public abstract long getUahDischarge(int which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002824
2825 /**
Mike Ma15313c92017-11-15 17:58:21 -08002826 * @return the amount of battery discharge while the device is in light idle mode, measured in
2827 * micro-Ampere-hours.
2828 */
2829 public abstract long getUahDischargeLightDoze(int which);
2830
2831 /**
2832 * @return the amount of battery discharge while the device is in deep idle mode, measured in
2833 * micro-Ampere-hours.
2834 */
2835 public abstract long getUahDischargeDeepDoze(int which);
2836
2837 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002838 * Returns the estimated real battery capacity, which may be less than the capacity
2839 * declared by the PowerProfile.
2840 * @return The estimated battery capacity in mAh.
2841 */
2842 public abstract int getEstimatedBatteryCapacity();
2843
2844 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002845 * @return The minimum learned battery capacity in uAh.
2846 */
2847 public abstract int getMinLearnedBatteryCapacity();
2848
2849 /**
2850 * @return The maximum learned battery capacity in uAh.
2851 */
2852 public abstract int getMaxLearnedBatteryCapacity() ;
2853
2854 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002855 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002856 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002857 public abstract LevelStepTracker getDischargeLevelStepTracker();
2858
2859 /**
2860 * Return the array of daily discharge step durations.
2861 */
2862 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002863
2864 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002865 * Compute an approximation for how much time (in microseconds) remains until the battery
2866 * is fully charged. Returns -1 if no time can be computed: either there is not
2867 * enough current data to make a decision, or the battery is currently
2868 * discharging.
2869 *
2870 * @param curTime The current elepsed realtime in microseconds.
2871 */
2872 public abstract long computeChargeTimeRemaining(long curTime);
2873
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002874 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002875 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002876 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002877 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002878
2879 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002880 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002881 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002882 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002883
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002884 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2885
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002886 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002887
Evan Millarc64edde2009-04-18 12:26:32 -07002888 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889
Bookatz50df7112017-08-04 14:53:26 -07002890 /**
2891 * Returns Timers tracking the total time of each Resource Power Manager state and voter.
2892 */
2893 public abstract Map<String, ? extends Timer> getRpmStats();
2894 /**
2895 * Returns Timers tracking the screen-off time of each Resource Power Manager state and voter.
2896 */
2897 public abstract Map<String, ? extends Timer> getScreenOffRpmStats();
2898
2899
James Carr2dd7e5e2016-07-20 18:48:39 -07002900 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2901
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002902 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2903
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002904 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 long days = seconds / (60 * 60 * 24);
2906 if (days != 0) {
2907 out.append(days);
2908 out.append("d ");
2909 }
2910 long used = days * 60 * 60 * 24;
2911
2912 long hours = (seconds - used) / (60 * 60);
2913 if (hours != 0 || used != 0) {
2914 out.append(hours);
2915 out.append("h ");
2916 }
2917 used += hours * 60 * 60;
2918
2919 long mins = (seconds-used) / 60;
2920 if (mins != 0 || used != 0) {
2921 out.append(mins);
2922 out.append("m ");
2923 }
2924 used += mins * 60;
2925
2926 if (seconds != 0 || used != 0) {
2927 out.append(seconds-used);
2928 out.append("s ");
2929 }
2930 }
2931
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002932 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002934 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 sb.append(time - (sec * 1000));
2936 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 }
2938
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002939 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002940 long sec = time / 1000;
2941 formatTimeRaw(sb, sec);
2942 sb.append(time - (sec * 1000));
2943 sb.append("ms");
2944 }
2945
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002946 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002948 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 }
2950 float perc = ((float)num) / ((float)den) * 100;
2951 mFormatBuilder.setLength(0);
2952 mFormatter.format("%.1f%%", perc);
2953 return mFormatBuilder.toString();
2954 }
2955
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002956 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002957 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07002958
Evan Millar22ac0432009-03-31 11:33:18 -07002959 if (bytes < BYTES_PER_KB) {
2960 return bytes + "B";
2961 } else if (bytes < BYTES_PER_MB) {
2962 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2963 return mFormatBuilder.toString();
2964 } else if (bytes < BYTES_PER_GB){
2965 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2966 return mFormatBuilder.toString();
2967 } else {
2968 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2969 return mFormatBuilder.toString();
2970 }
2971 }
2972
Kweku Adams103351f2017-10-16 14:39:34 -07002973 private static long roundUsToMs(long timeUs) {
2974 return (timeUs + 500) / 1000;
2975 }
2976
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002977 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002978 if (timer != null) {
2979 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002980 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002981 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2982 return totalTimeMillis;
2983 }
2984 return 0;
2985 }
2986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002987 /**
2988 *
2989 * @param sb a StringBuilder object.
2990 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002991 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002993 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002994 * @param linePrefix a String to be prepended to each line of output.
2995 * @return the line prefix
2996 */
2997 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002998 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003000 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003001 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003002
Evan Millarc64edde2009-04-18 12:26:32 -07003003 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 if (totalTimeMillis != 0) {
3005 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003006 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08003007 if (name != null) {
3008 sb.append(name);
3009 sb.append(' ');
3010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 sb.append('(');
3012 sb.append(count);
3013 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003014 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
3015 if (maxDurationMs >= 0) {
3016 sb.append(" max=");
3017 sb.append(maxDurationMs);
3018 }
Bookatz506a8182017-05-01 14:18:42 -07003019 // Put actual time if it is available and different from totalTimeMillis.
3020 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
3021 if (totalDurMs > totalTimeMillis) {
3022 sb.append(" actual=");
3023 sb.append(totalDurMs);
3024 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07003025 if (timer.isRunningLocked()) {
3026 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3027 if (currentMs >= 0) {
3028 sb.append(" (running for ");
3029 sb.append(currentMs);
3030 sb.append("ms)");
3031 } else {
3032 sb.append(" (running)");
3033 }
3034 }
3035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 return ", ";
3037 }
3038 }
3039 return linePrefix;
3040 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003041
3042 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07003043 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003044 *
3045 * @param pw a PrintWriter object to print to.
3046 * @param sb a StringBuilder object.
3047 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08003048 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003049 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
3050 * @param prefix a String to be prepended to each line of output.
3051 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07003052 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003053 */
3054 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07003055 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003056 if (timer != null) {
3057 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07003058 final long totalTimeMs = (timer.getTotalTimeLocked(
3059 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003060 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003061 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003062 sb.setLength(0);
3063 sb.append(prefix);
3064 sb.append(" ");
3065 sb.append(type);
3066 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003067 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003068 sb.append("realtime (");
3069 sb.append(count);
3070 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003071 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
3072 if (maxDurationMs >= 0) {
3073 sb.append(" max=");
3074 sb.append(maxDurationMs);
3075 }
3076 if (timer.isRunningLocked()) {
3077 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
3078 if (currentMs >= 0) {
3079 sb.append(" (running for ");
3080 sb.append(currentMs);
3081 sb.append("ms)");
3082 } else {
3083 sb.append(" (running)");
3084 }
3085 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003086 pw.println(sb.toString());
3087 return true;
3088 }
3089 }
3090 return false;
3091 }
Bookatzc8c44962017-05-11 12:12:54 -07003092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003093 /**
3094 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07003095 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096 * @param sb a StringBuilder object.
3097 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003098 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003100 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 * @param linePrefix a String to be prepended to each line of output.
3102 * @return the line prefix
3103 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003104 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
3105 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106 long totalTimeMicros = 0;
3107 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07003108 long max = 0;
3109 long current = 0;
3110 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003112 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07003113 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003114 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3115 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07003116 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117 }
3118 sb.append(linePrefix);
3119 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
3120 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07003121 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003123 sb.append(',');
3124 sb.append(current);
3125 sb.append(',');
3126 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07003127 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
3128 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
3129 // totalDuration independent of totalTimeMicros (since they are not pooled).
3130 if (name != null) {
3131 sb.append(',');
3132 sb.append(totalDuration);
3133 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003134 return ",";
3135 }
Bookatz506a8182017-05-01 14:18:42 -07003136
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003137 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
3138 String type) {
3139 pw.print(BATTERY_STATS_CHECKIN_VERSION);
3140 pw.print(',');
3141 pw.print(uid);
3142 pw.print(',');
3143 pw.print(category);
3144 pw.print(',');
3145 pw.print(type);
3146 }
3147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003148 /**
3149 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07003150 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 * @param pw the PageWriter to dump log to
3152 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3153 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3154 * @param args type-dependent data arguments
3155 */
Bookatzc8c44962017-05-11 12:12:54 -07003156 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003158 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003159 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003160 pw.print(',');
3161 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003163 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003164 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07003165
3166 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003167 * Dump a given timer stat for terse checkin mode.
3168 *
3169 * @param pw the PageWriter to dump log to
3170 * @param uid the UID to log
3171 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3172 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3173 * @param timer a {@link Timer} to dump stats for
3174 * @param rawRealtime the current elapsed realtime of the system in microseconds
3175 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3176 */
3177 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
3178 Timer timer, long rawRealtime, int which) {
3179 if (timer != null) {
3180 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003181 final long totalTime = roundUsToMs(timer.getTotalTimeLocked(rawRealtime, which));
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003182 final int count = timer.getCountLocked(which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003183 if (totalTime != 0 || count != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003184 dumpLine(pw, uid, category, type, totalTime, count);
3185 }
3186 }
3187 }
3188
3189 /**
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003190 * Dump a given timer stat to the proto stream.
3191 *
3192 * @param proto the ProtoOutputStream to log to
3193 * @param fieldId type of data, the field to save to (e.g. AggregatedBatteryStats.WAKELOCK)
3194 * @param timer a {@link Timer} to dump stats for
3195 * @param rawRealtimeUs the current elapsed realtime of the system in microseconds
3196 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3197 */
3198 private static void dumpTimer(ProtoOutputStream proto, long fieldId,
Kweku Adams87b19ec2017-10-09 12:40:03 -07003199 Timer timer, long rawRealtimeUs, int which) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003200 if (timer == null) {
3201 return;
3202 }
3203 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003204 final long timeMs = roundUsToMs(timer.getTotalTimeLocked(rawRealtimeUs, which));
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003205 final int count = timer.getCountLocked(which);
Kweku Adams103351f2017-10-16 14:39:34 -07003206 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs / 1000);
3207 final long curDurationMs = timer.getCurrentDurationMsLocked(rawRealtimeUs / 1000);
3208 final long totalDurationMs = timer.getTotalDurationMsLocked(rawRealtimeUs / 1000);
3209 if (timeMs != 0 || count != 0 || maxDurationMs != -1 || curDurationMs != -1
3210 || totalDurationMs != -1) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003211 final long token = proto.start(fieldId);
Kweku Adams103351f2017-10-16 14:39:34 -07003212 proto.write(TimerProto.DURATION_MS, timeMs);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003213 proto.write(TimerProto.COUNT, count);
Kweku Adams103351f2017-10-16 14:39:34 -07003214 // These values will be -1 for timers that don't implement the functionality.
3215 if (maxDurationMs != -1) {
3216 proto.write(TimerProto.MAX_DURATION_MS, maxDurationMs);
3217 }
3218 if (curDurationMs != -1) {
3219 proto.write(TimerProto.CURRENT_DURATION_MS, curDurationMs);
3220 }
3221 if (totalDurationMs != -1) {
3222 proto.write(TimerProto.TOTAL_DURATION_MS, totalDurationMs);
3223 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003224 proto.end(token);
3225 }
3226 }
3227
3228 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003229 * Checks if the ControllerActivityCounter has any data worth dumping.
3230 */
3231 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
3232 if (counter == null) {
3233 return false;
3234 }
3235
3236 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
3237 || counter.getRxTimeCounter().getCountLocked(which) != 0
3238 || counter.getPowerCounter().getCountLocked(which) != 0) {
3239 return true;
3240 }
3241
3242 for (LongCounter c : counter.getTxTimeCounters()) {
3243 if (c.getCountLocked(which) != 0) {
3244 return true;
3245 }
3246 }
3247 return false;
3248 }
3249
3250 /**
3251 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3252 * The order of the arguments in the final check in line is:
3253 *
3254 * idle, rx, power, tx...
3255 *
3256 * where tx... is one or more transmit level times.
3257 */
3258 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
3259 String type,
3260 ControllerActivityCounter counter,
3261 int which) {
3262 if (!controllerActivityHasData(counter, which)) {
3263 return;
3264 }
3265
3266 dumpLineHeader(pw, uid, category, type);
3267 pw.print(",");
3268 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
3269 pw.print(",");
3270 pw.print(counter.getRxTimeCounter().getCountLocked(which));
3271 pw.print(",");
3272 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3273 for (LongCounter c : counter.getTxTimeCounters()) {
3274 pw.print(",");
3275 pw.print(c.getCountLocked(which));
3276 }
3277 pw.println();
3278 }
3279
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003280 /**
3281 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3282 */
3283 private static void dumpControllerActivityProto(ProtoOutputStream proto, long fieldId,
3284 ControllerActivityCounter counter,
3285 int which) {
3286 if (!controllerActivityHasData(counter, which)) {
3287 return;
3288 }
3289
3290 final long cToken = proto.start(fieldId);
3291
3292 proto.write(ControllerActivityProto.IDLE_DURATION_MS,
3293 counter.getIdleTimeCounter().getCountLocked(which));
3294 proto.write(ControllerActivityProto.RX_DURATION_MS,
3295 counter.getRxTimeCounter().getCountLocked(which));
3296 proto.write(ControllerActivityProto.POWER_MAH,
3297 counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3298
3299 long tToken;
3300 LongCounter[] txCounters = counter.getTxTimeCounters();
3301 for (int i = 0; i < txCounters.length; ++i) {
3302 LongCounter c = txCounters[i];
3303 tToken = proto.start(ControllerActivityProto.TX);
3304 proto.write(ControllerActivityProto.TxLevel.LEVEL, i);
3305 proto.write(ControllerActivityProto.TxLevel.DURATION_MS, c.getCountLocked(which));
3306 proto.end(tToken);
3307 }
3308
3309 proto.end(cToken);
3310 }
3311
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003312 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
3313 String prefix, String controllerName,
3314 ControllerActivityCounter counter,
3315 int which) {
3316 if (controllerActivityHasData(counter, which)) {
3317 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
3318 }
3319 }
3320
3321 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
3322 String controllerName,
3323 ControllerActivityCounter counter, int which) {
3324 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
3325 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
3326 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003327 // Battery real time
3328 final long totalControllerActivityTimeMs
3329 = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003330 long totalTxTimeMs = 0;
3331 for (LongCounter txState : counter.getTxTimeCounters()) {
3332 totalTxTimeMs += txState.getCountLocked(which);
3333 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003334 final long sleepTimeMs
3335 = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + totalTxTimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003336
3337 sb.setLength(0);
3338 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003339 sb.append(" ");
3340 sb.append(controllerName);
3341 sb.append(" Sleep time: ");
3342 formatTimeMs(sb, sleepTimeMs);
3343 sb.append("(");
3344 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3345 sb.append(")");
3346 pw.println(sb.toString());
3347
3348 sb.setLength(0);
3349 sb.append(prefix);
3350 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003351 sb.append(controllerName);
3352 sb.append(" Idle time: ");
3353 formatTimeMs(sb, idleTimeMs);
3354 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003355 sb.append(formatRatioLocked(idleTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003356 sb.append(")");
3357 pw.println(sb.toString());
3358
3359 sb.setLength(0);
3360 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003361 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003362 sb.append(controllerName);
3363 sb.append(" Rx time: ");
3364 formatTimeMs(sb, rxTimeMs);
3365 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003366 sb.append(formatRatioLocked(rxTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003367 sb.append(")");
3368 pw.println(sb.toString());
3369
3370 sb.setLength(0);
3371 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003372 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003373 sb.append(controllerName);
3374 sb.append(" Tx time: ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003375
Siddharth Ray3c648c42017-10-02 17:30:58 -07003376 String [] powerLevel;
3377 switch(controllerName) {
3378 case "Cellular":
3379 powerLevel = new String[] {
3380 " less than 0dBm: ",
3381 " 0dBm to 8dBm: ",
3382 " 8dBm to 15dBm: ",
3383 " 15dBm to 20dBm: ",
3384 " above 20dBm: "};
3385 break;
3386 default:
3387 powerLevel = new String[] {"[0]", "[1]", "[2]", "[3]", "[4]"};
3388 break;
3389 }
3390 final int numTxLvls = Math.min(counter.getTxTimeCounters().length, powerLevel.length);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003391 if (numTxLvls > 1) {
Siddharth Ray3c648c42017-10-02 17:30:58 -07003392 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003393 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3394 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3395 sb.setLength(0);
3396 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003397 sb.append(" ");
3398 sb.append(powerLevel[lvl]);
3399 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003400 formatTimeMs(sb, txLvlTimeMs);
3401 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003402 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003403 sb.append(")");
3404 pw.println(sb.toString());
3405 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003406 } else {
3407 final long txLvlTimeMs = counter.getTxTimeCounters()[0].getCountLocked(which);
3408 formatTimeMs(sb, txLvlTimeMs);
3409 sb.append("(");
3410 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
3411 sb.append(")");
3412 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003413 }
3414
Siddharth Ray3c648c42017-10-02 17:30:58 -07003415 if (powerDrainMaMs > 0) {
3416 sb.setLength(0);
3417 sb.append(prefix);
3418 sb.append(" ");
3419 sb.append(controllerName);
3420 sb.append(" Battery drain: ").append(
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003421 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
Siddharth Ray3c648c42017-10-02 17:30:58 -07003422 sb.append("mAh");
3423 pw.println(sb.toString());
3424 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003425 }
3426
3427 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003428 * Temporary for settings.
3429 */
3430 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3431 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3432 }
3433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 /**
3435 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003436 *
Kweku Adams87b19ec2017-10-09 12:40:03 -07003437 * NOTE: all times are expressed in microseconds, unless specified otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003439 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3440 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003441 final long rawUptime = SystemClock.uptimeMillis() * 1000;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003442 final long rawRealtimeMs = SystemClock.elapsedRealtime();
3443 final long rawRealtime = rawRealtimeMs * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003444 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003445 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3446 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003447 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3448 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3449 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003450 final long totalRealtime = computeRealtime(rawRealtime, which);
3451 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003452 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Mike Mac2f518a2017-09-19 16:06:03 -07003453 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003454 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003455 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003456 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3457 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003458 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003459 rawRealtime, which);
3460 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3461 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003462 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003463 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003464 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003465 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003466 final long dischargeCount = getUahDischarge(which);
3467 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
3468 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Ma15313c92017-11-15 17:58:21 -08003469 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
3470 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003471
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003472 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003473
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003474 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003475 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003476
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003477 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003480 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003481 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003482 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003483 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003484 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003485 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003486 getEstimatedBatteryCapacity(),
Mike Mac2f518a2017-09-19 16:06:03 -07003487 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity(),
3488 screenDozeTime / 1000);
Adam Lesinski67c134f2016-06-10 15:15:08 -07003489
Bookatzc8c44962017-05-11 12:12:54 -07003490
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003491 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003492 long fullWakeLockTimeTotal = 0;
3493 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003494
Evan Millar22ac0432009-03-31 11:33:18 -07003495 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003496 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003497
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003498 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3499 = u.getWakelockStats();
3500 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3501 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003502
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003503 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3504 if (fullWakeTimer != null) {
3505 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3506 which);
3507 }
3508
3509 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3510 if (partialWakeTimer != null) {
3511 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3512 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003513 }
3514 }
3515 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003516
3517 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003518 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3519 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3520 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3521 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3522 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3523 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3524 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3525 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003526 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3527 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003528 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3529 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003530 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3531 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003532
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003533 // Dump Modem controller stats
3534 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3535 getModemControllerActivity(), which);
3536
Adam Lesinskie283d332015-04-16 12:29:25 -07003537 // Dump Wifi controller stats
3538 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3539 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003540 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003541 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003542
3543 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3544 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003545
3546 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003547 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3548 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 // Dump misc stats
3551 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003552 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003553 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003554 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003555 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003556 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003557 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3558 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003559 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003560 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3561 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3562 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3563 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003564 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003565
Dianne Hackborn617f8772009-03-31 15:04:46 -07003566 // Dump screen brightness stats
3567 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3568 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003569 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003570 }
3571 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003572
Dianne Hackborn627bba72009-03-24 22:32:56 -07003573 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003574 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3575 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003576 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003577 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003578 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003579 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003580 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003581 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003582 args[i] = getPhoneSignalStrengthCount(i, which);
3583 }
3584 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003585
Dianne Hackborn627bba72009-03-24 22:32:56 -07003586 // Dump network type stats
3587 args = new Object[NUM_DATA_CONNECTION_TYPES];
3588 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003589 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003590 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003591 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3592 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3593 args[i] = getPhoneDataConnectionCount(i, which);
3594 }
3595 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003596
3597 // Dump wifi state stats
3598 args = new Object[NUM_WIFI_STATES];
3599 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003600 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003601 }
3602 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3603 for (int i=0; i<NUM_WIFI_STATES; i++) {
3604 args[i] = getWifiStateCount(i, which);
3605 }
3606 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3607
Dianne Hackborn3251b902014-06-20 14:40:53 -07003608 // Dump wifi suppl state stats
3609 args = new Object[NUM_WIFI_SUPPL_STATES];
3610 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3611 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3612 }
3613 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3614 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3615 args[i] = getWifiSupplStateCount(i, which);
3616 }
3617 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3618
3619 // Dump wifi signal strength stats
3620 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3621 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3622 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3623 }
3624 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3625 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3626 args[i] = getWifiSignalStrengthCount(i, which);
3627 }
3628 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3629
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003630 // Dump Multicast total stats
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003631 final long multicastWakeLockTimeTotalMicros =
3632 getWifiMulticastWakelockTime(rawRealtime, which);
3633 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003634 dumpLine(pw, 0 /* uid */, category, WIFI_MULTICAST_TOTAL_DATA,
3635 multicastWakeLockTimeTotalMicros / 1000,
3636 multicastWakeLockCountTotal);
3637
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003638 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003639 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003640 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003641 }
Bookatzc8c44962017-05-11 12:12:54 -07003642
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003643 if (which == STATS_SINCE_UNPLUGGED) {
3644 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3645 getDischargeStartLevel()-getDischargeCurrentLevel(),
3646 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003647 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
Mike Mac2f518a2017-09-19 16:06:03 -07003648 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003649 getDischargeAmountScreenDoze(), dischargeScreenDozeCount / 1000,
3650 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003651 } else {
3652 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3653 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003654 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003655 getDischargeAmountScreenOffSinceCharge(),
Mike Mac2f518a2017-09-19 16:06:03 -07003656 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003657 getDischargeAmountScreenDozeSinceCharge(), dischargeScreenDozeCount / 1000,
3658 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003659 }
Bookatzc8c44962017-05-11 12:12:54 -07003660
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003661 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003662 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003663 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003664 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003665 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003666 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003667 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3668 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003669 }
Evan Millarc64edde2009-04-18 12:26:32 -07003670 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003671 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003672 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003673 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3674 // Not doing the regular wake lock formatting to remain compatible
3675 // with the old checkin format.
3676 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3677 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003678 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003679 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003680 }
3681 }
Evan Millarc64edde2009-04-18 12:26:32 -07003682 }
Bookatzc8c44962017-05-11 12:12:54 -07003683
Bookatz50df7112017-08-04 14:53:26 -07003684 final Map<String, ? extends Timer> rpmStats = getRpmStats();
3685 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
3686 if (rpmStats.size() > 0) {
3687 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
3688 sb.setLength(0);
3689 Timer totalTimer = ent.getValue();
3690 long timeMs = (totalTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3691 int count = totalTimer.getCountLocked(which);
3692 Timer screenOffTimer = screenOffRpmStats.get(ent.getKey());
3693 long screenOffTimeMs = screenOffTimer != null
3694 ? (screenOffTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3695 int screenOffCount = screenOffTimer != null
3696 ? screenOffTimer.getCountLocked(which) : 0;
Bookatz82b341172017-09-07 19:06:08 -07003697 if (SCREEN_OFF_RPM_STATS_ENABLED) {
3698 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3699 "\"" + ent.getKey() + "\"", timeMs, count, screenOffTimeMs,
3700 screenOffCount);
3701 } else {
3702 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3703 "\"" + ent.getKey() + "\"", timeMs, count);
3704 }
Bookatz50df7112017-08-04 14:53:26 -07003705 }
3706 }
3707
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003708 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003709 helper.create(this);
3710 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003711 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003712 if (sippers != null && sippers.size() > 0) {
3713 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3714 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003715 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003716 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3717 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
Kweku Adams87b19ec2017-10-09 12:40:03 -07003718 int uid = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003719 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003720 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003721 String label;
3722 switch (bs.drainType) {
3723 case IDLE:
3724 label="idle";
3725 break;
3726 case CELL:
3727 label="cell";
3728 break;
3729 case PHONE:
3730 label="phone";
3731 break;
3732 case WIFI:
3733 label="wifi";
3734 break;
3735 case BLUETOOTH:
3736 label="blue";
3737 break;
3738 case SCREEN:
3739 label="scrn";
3740 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003741 case FLASHLIGHT:
3742 label="flashlight";
3743 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003744 case APP:
3745 uid = bs.uidObj.getUid();
3746 label = "uid";
3747 break;
3748 case USER:
3749 uid = UserHandle.getUid(bs.userId, 0);
3750 label = "user";
3751 break;
3752 case UNACCOUNTED:
3753 label = "unacc";
3754 break;
3755 case OVERCOUNTED:
3756 label = "over";
3757 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003758 case CAMERA:
3759 label = "camera";
3760 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003761 case MEMORY:
3762 label = "memory";
3763 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003764 default:
3765 label = "???";
3766 }
3767 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07003768 BatteryStatsHelper.makemAh(bs.totalPowerMah),
3769 bs.shouldHide ? 1 : 0,
3770 BatteryStatsHelper.makemAh(bs.screenPowerMah),
3771 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003772 }
3773 }
3774
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003775 final long[] cpuFreqs = getCpuFreqs();
3776 if (cpuFreqs != null) {
3777 sb.setLength(0);
3778 for (int i = 0; i < cpuFreqs.length; ++i) {
3779 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3780 }
3781 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3782 }
3783
Kweku Adams87b19ec2017-10-09 12:40:03 -07003784 // Dump stats per UID.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003785 for (int iu = 0; iu < NU; iu++) {
3786 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003787 if (reqUid >= 0 && uid != reqUid) {
3788 continue;
3789 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003790 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003792 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003793 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3794 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3795 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3796 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3797 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3798 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3799 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3800 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003801 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003802 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3803 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003804 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003805 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3806 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003807 // Background data transfers
3808 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3809 which);
3810 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3811 which);
3812 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3813 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3814 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3815 which);
3816 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3817 which);
3818 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3819 which);
3820 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3821 which);
3822
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003823 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3824 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003825 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003826 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3827 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3828 || wifiBytesBgTx > 0
3829 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3830 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003831 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3832 wifiBytesRx, wifiBytesTx,
3833 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003834 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003835 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003836 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3837 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3838 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3839 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003840 }
3841
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003842 // Dump modem controller data, per UID.
3843 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3844 u.getModemControllerActivity(), which);
3845
3846 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003847 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3848 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3849 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003850 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3851 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003852 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3853 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3854 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003855 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003856 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003857 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3858 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003859 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3860 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003861 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003862 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003864
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003865 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3866 u.getWifiControllerActivity(), which);
3867
Bookatz867c0d72017-03-07 18:23:42 -08003868 final Timer bleTimer = u.getBluetoothScanTimer();
3869 if (bleTimer != null) {
3870 // Convert from microseconds to milliseconds with rounding
3871 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3872 / 1000;
3873 if (totalTime != 0) {
3874 final int count = bleTimer.getCountLocked(which);
3875 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3876 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003877 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3878 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3879 final long actualTimeBg = bleTimerBg != null ?
3880 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003881 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003882 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3883 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003884 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3885 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3886 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3887 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3888 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
3889 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3890 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
3891 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3892 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
3893 final Timer unoptimizedScanTimerBg =
3894 u.getBluetoothUnoptimizedScanBackgroundTimer();
3895 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
3896 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3897 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
3898 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3899
Bookatz867c0d72017-03-07 18:23:42 -08003900 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07003901 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
3902 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
3903 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08003904 }
3905 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003906
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003907 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3908 u.getBluetoothControllerActivity(), which);
3909
Dianne Hackborn617f8772009-03-31 15:04:46 -07003910 if (u.hasUserActivity()) {
3911 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3912 boolean hasData = false;
3913 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3914 int val = u.getUserActivityCount(i, which);
3915 args[i] = val;
3916 if (val != 0) hasData = true;
3917 }
3918 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003919 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003920 }
3921 }
Bookatzc8c44962017-05-11 12:12:54 -07003922
3923 if (u.getAggregatedPartialWakelockTimer() != null) {
3924 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07003925 // Times are since reset (regardless of 'which')
3926 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07003927 final Timer bgTimer = timer.getSubTimer();
3928 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003929 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07003930 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
3931 }
3932
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003933 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3934 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3935 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3936 String linePrefix = "";
3937 sb.setLength(0);
3938 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3939 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003940 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3941 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003942 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003943 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
3944 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003945 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3946 rawRealtime, "w", which, linePrefix);
3947
Kweku Adams103351f2017-10-16 14:39:34 -07003948 // Only log if we had at least one wakelock...
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003949 if (sb.length() > 0) {
3950 String name = wakelocks.keyAt(iw);
3951 if (name.indexOf(',') >= 0) {
3952 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003953 }
Yi Jin02483362017-08-04 11:30:44 -07003954 if (name.indexOf('\n') >= 0) {
3955 name = name.replace('\n', '_');
3956 }
3957 if (name.indexOf('\r') >= 0) {
3958 name = name.replace('\r', '_');
3959 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003960 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003961 }
3962 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003963
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003964 // WiFi Multicast Wakelock Statistics
3965 final Timer mcTimer = u.getMulticastWakelockStats();
3966 if (mcTimer != null) {
3967 final long totalMcWakelockTimeMs =
3968 mcTimer.getTotalTimeLocked(rawRealtime, which) / 1000 ;
3969 final int countMcWakelock = mcTimer.getCountLocked(which);
3970 if(totalMcWakelockTimeMs > 0) {
3971 dumpLine(pw, uid, category, WIFI_MULTICAST_DATA,
3972 totalMcWakelockTimeMs, countMcWakelock);
3973 }
3974 }
3975
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003976 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3977 for (int isy=syncs.size()-1; isy>=0; isy--) {
3978 final Timer timer = syncs.valueAt(isy);
3979 // Convert from microseconds to milliseconds with rounding
3980 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3981 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07003982 final Timer bgTimer = timer.getSubTimer();
3983 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003984 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07003985 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003986 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003987 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07003988 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003989 }
3990 }
3991
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003992 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3993 for (int ij=jobs.size()-1; ij>=0; ij--) {
3994 final Timer timer = jobs.valueAt(ij);
3995 // Convert from microseconds to milliseconds with rounding
3996 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3997 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07003998 final Timer bgTimer = timer.getSubTimer();
3999 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004000 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07004001 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004002 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004003 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07004004 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004005 }
4006 }
4007
Dianne Hackborn94326cb2017-06-28 16:17:20 -07004008 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
4009 for (int ic=completions.size()-1; ic>=0; ic--) {
4010 SparseIntArray types = completions.valueAt(ic);
4011 if (types != null) {
4012 dumpLine(pw, uid, category, JOB_COMPLETION_DATA,
4013 "\"" + completions.keyAt(ic) + "\"",
4014 types.get(JobParameters.REASON_CANCELED, 0),
4015 types.get(JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, 0),
4016 types.get(JobParameters.REASON_PREEMPT, 0),
4017 types.get(JobParameters.REASON_TIMEOUT, 0),
4018 types.get(JobParameters.REASON_DEVICE_IDLE, 0));
4019 }
4020 }
4021
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004022 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
4023 rawRealtime, which);
4024 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
4025 rawRealtime, which);
4026 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
4027 rawRealtime, which);
4028 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
4029 rawRealtime, which);
4030
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004031 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4032 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004033 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004034 final Uid.Sensor se = sensors.valueAt(ise);
4035 final int sensorNumber = sensors.keyAt(ise);
4036 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004037 if (timer != null) {
4038 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004039 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4040 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004041 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004042 final int count = timer.getCountLocked(which);
4043 final Timer bgTimer = se.getSensorBackgroundTime();
4044 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004045 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4046 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4047 final long bgActualTime = bgTimer != null ?
4048 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4049 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
4050 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004051 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004052 }
4053 }
4054
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004055 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
4056 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004057
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07004058 dumpTimer(pw, uid, category, FOREGROUND_ACTIVITY_DATA, u.getForegroundActivityTimer(),
4059 rawRealtime, which);
4060
4061 dumpTimer(pw, uid, category, FOREGROUND_SERVICE_DATA, u.getForegroundServiceTimer(),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004062 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004063
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004064 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07004065 long totalStateTime = 0;
4066 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08004067 final long time = u.getProcessStateTime(ips, rawRealtime, which);
4068 totalStateTime += time;
4069 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004070 }
4071 if (totalStateTime > 0) {
4072 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
4073 }
4074
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004075 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4076 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004077 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004078 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004079 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004080 }
4081
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004082 // If the cpuFreqs is null, then don't bother checking for cpu freq times.
4083 if (cpuFreqs != null) {
4084 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
4085 // If total cpuFreqTimes is null, then we don't need to check for
4086 // screenOffCpuFreqTimes.
4087 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
4088 sb.setLength(0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004089 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004090 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004091 }
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004092 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
4093 if (screenOffCpuFreqTimeMs != null) {
4094 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
4095 sb.append("," + screenOffCpuFreqTimeMs[i]);
4096 }
4097 } else {
4098 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
4099 sb.append(",0");
4100 }
4101 }
4102 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
4103 cpuFreqTimeMs.length, sb.toString());
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004104 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -08004105
4106 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
4107 final long[] timesMs = u.getCpuFreqTimes(which, procState);
4108 if (timesMs != null && timesMs.length == cpuFreqs.length) {
4109 sb.setLength(0);
4110 for (int i = 0; i < timesMs.length; ++i) {
4111 sb.append((i == 0 ? "" : ",") + timesMs[i]);
4112 }
4113 final long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(
4114 which, procState);
4115 if (screenOffTimesMs != null) {
4116 for (int i = 0; i < screenOffTimesMs.length; ++i) {
4117 sb.append("," + screenOffTimesMs[i]);
4118 }
4119 } else {
4120 for (int i = 0; i < timesMs.length; ++i) {
4121 sb.append(",0");
4122 }
4123 }
4124 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA,
4125 Uid.UID_PROCESS_TYPES[procState], timesMs.length, sb.toString());
4126 }
4127 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004128 }
4129
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004130 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4131 = u.getProcessStats();
4132 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4133 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004134
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004135 final long userMillis = ps.getUserTime(which);
4136 final long systemMillis = ps.getSystemTime(which);
4137 final long foregroundMillis = ps.getForegroundTime(which);
4138 final int starts = ps.getStarts(which);
4139 final int numCrashes = ps.getNumCrashes(which);
4140 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004141
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004142 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
4143 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004144 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
4145 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 }
4147 }
4148
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004149 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4150 = u.getPackageStats();
4151 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4152 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4153 int wakeups = 0;
4154 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4155 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07004156 int count = alarms.valueAt(iwa).getCountLocked(which);
4157 wakeups += count;
4158 String name = alarms.keyAt(iwa).replace(',', '_');
4159 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004160 }
4161 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4162 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4163 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4164 final long startTime = ss.getStartTime(batteryUptime, which);
4165 final int starts = ss.getStarts(which);
4166 final int launches = ss.getLaunches(which);
4167 if (startTime != 0 || starts != 0 || launches != 0) {
4168 dumpLine(pw, uid, category, APK_DATA,
4169 wakeups, // wakeup alarms
4170 packageStats.keyAt(ipkg), // Apk
4171 serviceStats.keyAt(isvc), // service
4172 startTime / 1000, // time spent started, in ms
4173 starts,
4174 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 }
4176 }
4177 }
4178 }
4179 }
4180
Dianne Hackborn81038902012-11-26 17:04:09 -08004181 static final class TimerEntry {
4182 final String mName;
4183 final int mId;
4184 final BatteryStats.Timer mTimer;
4185 final long mTime;
4186 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
4187 mName = name;
4188 mId = id;
4189 mTimer = timer;
4190 mTime = time;
4191 }
4192 }
4193
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004194 private void printmAh(PrintWriter printer, double power) {
4195 printer.print(BatteryStatsHelper.makemAh(power));
4196 }
4197
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004198 private void printmAh(StringBuilder sb, double power) {
4199 sb.append(BatteryStatsHelper.makemAh(power));
4200 }
4201
Dianne Hackbornd953c532014-08-16 18:17:38 -07004202 /**
4203 * Temporary for settings.
4204 */
4205 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
4206 int reqUid) {
4207 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
4208 }
4209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004210 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004211 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07004212 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004213 final long rawUptime = SystemClock.uptimeMillis() * 1000;
4214 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07004215 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004216 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004217
4218 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
4219 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
4220 final long totalRealtime = computeRealtime(rawRealtime, which);
4221 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004222 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
4223 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
4224 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004225 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
4226 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -07004227 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004228
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004229 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07004230
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004231 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07004232 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004233
Adam Lesinskif9b20a92016-06-17 17:30:01 -07004234 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
4235 if (estimatedBatteryCapacity > 0) {
4236 sb.setLength(0);
4237 sb.append(prefix);
4238 sb.append(" Estimated battery capacity: ");
4239 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
4240 sb.append(" mAh");
4241 pw.println(sb.toString());
4242 }
4243
Jocelyn Dangc627d102017-04-14 13:15:14 -07004244 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
4245 if (minLearnedBatteryCapacity > 0) {
4246 sb.setLength(0);
4247 sb.append(prefix);
4248 sb.append(" Min learned battery capacity: ");
4249 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
4250 sb.append(" mAh");
4251 pw.println(sb.toString());
4252 }
4253 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
4254 if (maxLearnedBatteryCapacity > 0) {
4255 sb.setLength(0);
4256 sb.append(prefix);
4257 sb.append(" Max learned battery capacity: ");
4258 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
4259 sb.append(" mAh");
4260 pw.println(sb.toString());
4261 }
4262
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004263 sb.setLength(0);
4264 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004265 sb.append(" Time on battery: ");
4266 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
4267 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
4268 sb.append(") realtime, ");
4269 formatTimeMs(sb, whichBatteryUptime / 1000);
4270 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, whichBatteryRealtime));
4271 sb.append(") uptime");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004272 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004273
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004274 sb.setLength(0);
4275 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004276 sb.append(" Time on battery screen off: ");
4277 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
4278 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, whichBatteryRealtime));
4279 sb.append(") realtime, ");
4280 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
4281 sb.append("(");
4282 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, whichBatteryRealtime));
4283 sb.append(") uptime");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004284 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004285
4286 sb.setLength(0);
4287 sb.append(prefix);
4288 sb.append(" Time on battery screen doze: ");
4289 formatTimeMs(sb, screenDozeTime / 1000); sb.append("(");
4290 sb.append(formatRatioLocked(screenDozeTime, whichBatteryRealtime));
4291 sb.append(")");
4292 pw.println(sb.toString());
4293
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004294 sb.setLength(0);
4295 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004296 sb.append(" Total run time: ");
4297 formatTimeMs(sb, totalRealtime / 1000);
4298 sb.append("realtime, ");
4299 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004300 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07004301 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004302 if (batteryTimeRemaining >= 0) {
4303 sb.setLength(0);
4304 sb.append(prefix);
4305 sb.append(" Battery time remaining: ");
4306 formatTimeMs(sb, batteryTimeRemaining / 1000);
4307 pw.println(sb.toString());
4308 }
4309 if (chargeTimeRemaining >= 0) {
4310 sb.setLength(0);
4311 sb.append(prefix);
4312 sb.append(" Charge time remaining: ");
4313 formatTimeMs(sb, chargeTimeRemaining / 1000);
4314 pw.println(sb.toString());
4315 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004316
Kweku Adams87b19ec2017-10-09 12:40:03 -07004317 final long dischargeCount = getUahDischarge(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004318 if (dischargeCount >= 0) {
4319 sb.setLength(0);
4320 sb.append(prefix);
4321 sb.append(" Discharge: ");
4322 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
4323 sb.append(" mAh");
4324 pw.println(sb.toString());
4325 }
4326
Kweku Adams87b19ec2017-10-09 12:40:03 -07004327 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004328 if (dischargeScreenOffCount >= 0) {
4329 sb.setLength(0);
4330 sb.append(prefix);
4331 sb.append(" Screen off discharge: ");
4332 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
4333 sb.append(" mAh");
4334 pw.println(sb.toString());
4335 }
4336
Kweku Adams87b19ec2017-10-09 12:40:03 -07004337 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Mac2f518a2017-09-19 16:06:03 -07004338 if (dischargeScreenDozeCount >= 0) {
4339 sb.setLength(0);
4340 sb.append(prefix);
4341 sb.append(" Screen doze discharge: ");
4342 sb.append(BatteryStatsHelper.makemAh(dischargeScreenDozeCount / 1000.0));
4343 sb.append(" mAh");
4344 pw.println(sb.toString());
4345 }
4346
4347 final long dischargeScreenOnCount =
4348 dischargeCount - dischargeScreenOffCount - dischargeScreenDozeCount;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004349 if (dischargeScreenOnCount >= 0) {
4350 sb.setLength(0);
4351 sb.append(prefix);
4352 sb.append(" Screen on discharge: ");
4353 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
4354 sb.append(" mAh");
4355 pw.println(sb.toString());
4356 }
4357
Mike Ma15313c92017-11-15 17:58:21 -08004358 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
4359 if (dischargeLightDozeCount >= 0) {
4360 sb.setLength(0);
4361 sb.append(prefix);
4362 sb.append(" Device light doze discharge: ");
4363 sb.append(BatteryStatsHelper.makemAh(dischargeLightDozeCount / 1000.0));
4364 sb.append(" mAh");
4365 pw.println(sb.toString());
4366 }
4367
4368 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
4369 if (dischargeDeepDozeCount >= 0) {
4370 sb.setLength(0);
4371 sb.append(prefix);
4372 sb.append(" Device deep doze discharge: ");
4373 sb.append(BatteryStatsHelper.makemAh(dischargeDeepDozeCount / 1000.0));
4374 sb.append(" mAh");
4375 pw.println(sb.toString());
4376 }
4377
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004378 pw.print(" Start clock time: ");
4379 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
4380
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004381 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004382 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004383 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004384 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
4385 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004386 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004387 rawRealtime, which);
4388 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
4389 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004390 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004391 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004392 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
4393 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
4394 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004395 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004396 sb.append(prefix);
4397 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
4398 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004399 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004400 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
4401 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004402 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004403 pw.println(sb.toString());
4404 sb.setLength(0);
4405 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004406 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004407 boolean didOne = false;
4408 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004409 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004410 if (time == 0) {
4411 continue;
4412 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004413 sb.append("\n ");
4414 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004415 didOne = true;
4416 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
4417 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004418 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004419 sb.append("(");
4420 sb.append(formatRatioLocked(time, screenOnTime));
4421 sb.append(")");
4422 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004423 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004424 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004425 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004426 sb.setLength(0);
4427 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004428 sb.append(" Power save mode enabled: ");
4429 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004430 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004431 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004432 sb.append(")");
4433 pw.println(sb.toString());
4434 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004435 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004436 sb.setLength(0);
4437 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004438 sb.append(" Device light idling: ");
4439 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004440 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004441 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
4442 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004443 sb.append("x");
4444 pw.println(sb.toString());
4445 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004446 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004447 sb.setLength(0);
4448 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004449 sb.append(" Idle mode light time: ");
4450 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004451 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004452 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
4453 sb.append(") ");
4454 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004455 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004456 sb.append(" -- longest ");
4457 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
4458 pw.println(sb.toString());
4459 }
4460 if (deviceIdlingTime != 0) {
4461 sb.setLength(0);
4462 sb.append(prefix);
4463 sb.append(" Device full idling: ");
4464 formatTimeMs(sb, deviceIdlingTime / 1000);
4465 sb.append("(");
4466 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004467 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004468 sb.append("x");
4469 pw.println(sb.toString());
4470 }
4471 if (deviceIdleModeFullTime != 0) {
4472 sb.setLength(0);
4473 sb.append(prefix);
4474 sb.append(" Idle mode full time: ");
4475 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
4476 sb.append("(");
4477 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
4478 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004479 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004480 sb.append("x");
4481 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004482 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004483 pw.println(sb.toString());
4484 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004485 if (phoneOnTime != 0) {
4486 sb.setLength(0);
4487 sb.append(prefix);
4488 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
4489 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004490 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004491 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004492 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004493 if (connChanges != 0) {
4494 pw.print(prefix);
4495 pw.print(" Connectivity changes: "); pw.println(connChanges);
4496 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004497
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004498 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07004499 long fullWakeLockTimeTotalMicros = 0;
4500 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08004501
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004502 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08004503
Evan Millar22ac0432009-03-31 11:33:18 -07004504 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004505 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004506
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004507 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4508 = u.getWakelockStats();
4509 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4510 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07004511
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004512 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
4513 if (fullWakeTimer != null) {
4514 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
4515 rawRealtime, which);
4516 }
4517
4518 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4519 if (partialWakeTimer != null) {
4520 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
4521 rawRealtime, which);
4522 if (totalTimeMicros > 0) {
4523 if (reqUid < 0) {
4524 // Only show the ordered list of all wake
4525 // locks if the caller is not asking for data
4526 // about a specific uid.
4527 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
4528 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08004529 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004530 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07004531 }
4532 }
4533 }
4534 }
Bookatzc8c44962017-05-11 12:12:54 -07004535
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004536 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4537 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4538 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4539 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4540 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4541 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4542 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4543 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004544 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4545 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004546
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004547 if (fullWakeLockTimeTotalMicros != 0) {
4548 sb.setLength(0);
4549 sb.append(prefix);
4550 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4551 (fullWakeLockTimeTotalMicros + 500) / 1000);
4552 pw.println(sb.toString());
4553 }
4554
4555 if (partialWakeLockTimeTotalMicros != 0) {
4556 sb.setLength(0);
4557 sb.append(prefix);
4558 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4559 (partialWakeLockTimeTotalMicros + 500) / 1000);
4560 pw.println(sb.toString());
4561 }
4562
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004563 final long multicastWakeLockTimeTotalMicros =
4564 getWifiMulticastWakelockTime(rawRealtime, which);
4565 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004566 if (multicastWakeLockTimeTotalMicros != 0) {
4567 sb.setLength(0);
4568 sb.append(prefix);
4569 sb.append(" Total WiFi Multicast wakelock Count: ");
4570 sb.append(multicastWakeLockCountTotal);
4571 pw.println(sb.toString());
4572
4573 sb.setLength(0);
4574 sb.append(prefix);
4575 sb.append(" Total WiFi Multicast wakelock time: ");
4576 formatTimeMsNoSpace(sb, (multicastWakeLockTimeTotalMicros + 500) / 1000);
4577 pw.println(sb.toString());
4578 }
4579
Siddharth Ray3c648c42017-10-02 17:30:58 -07004580 pw.println("");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004581 pw.print(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004582 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004583 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004584 sb.append(" CONNECTIVITY POWER SUMMARY START");
4585 pw.println(sb.toString());
4586
4587 pw.print(prefix);
4588 sb.setLength(0);
4589 sb.append(prefix);
4590 sb.append(" Logging duration for connectivity statistics: ");
4591 formatTimeMs(sb, whichBatteryRealtime / 1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004592 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004593
4594 sb.setLength(0);
4595 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004596 sb.append(" Cellular Statistics:");
Amith Yamasanif37447b2009-10-08 18:28:01 -07004597 pw.println(sb.toString());
4598
Siddharth Ray3c648c42017-10-02 17:30:58 -07004599 pw.print(prefix);
4600 sb.setLength(0);
4601 sb.append(prefix);
4602 sb.append(" Cellular kernel active time: ");
4603 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
4604 formatTimeMs(sb, mobileActiveTime / 1000);
4605 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4606 sb.append(")");
4607 pw.println(sb.toString());
4608
4609 pw.print(" Cellular data received: "); pw.println(formatBytesLocked(mobileRxTotalBytes));
4610 pw.print(" Cellular data sent: "); pw.println(formatBytesLocked(mobileTxTotalBytes));
4611 pw.print(" Cellular packets received: "); pw.println(mobileRxTotalPackets);
4612 pw.print(" Cellular packets sent: "); pw.println(mobileTxTotalPackets);
4613
Dianne Hackborn627bba72009-03-24 22:32:56 -07004614 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004615 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004616 sb.append(" Cellular Radio Access Technology:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004617 didOne = false;
4618 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004619 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004620 if (time == 0) {
4621 continue;
4622 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004623 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004624 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004625 didOne = true;
4626 sb.append(DATA_CONNECTION_NAMES[i]);
4627 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004628 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004629 sb.append("(");
4630 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004631 sb.append(") ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004632 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004633 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004634 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004635
4636 sb.setLength(0);
4637 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004638 sb.append(" Cellular Rx signal strength (RSRP):");
4639 final String[] cellularRxSignalStrengthDescription = new String[]{
4640 "very poor (less than -128dBm): ",
4641 "poor (-128dBm to -118dBm): ",
4642 "moderate (-118dBm to -108dBm): ",
4643 "good (-108dBm to -98dBm): ",
4644 "great (greater than -98dBm): "};
4645 didOne = false;
4646 final int numCellularRxBins = Math.min(SignalStrength.NUM_SIGNAL_STRENGTH_BINS,
4647 cellularRxSignalStrengthDescription.length);
4648 for (int i=0; i<numCellularRxBins; i++) {
4649 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
4650 if (time == 0) {
4651 continue;
4652 }
4653 sb.append("\n ");
4654 sb.append(prefix);
4655 didOne = true;
4656 sb.append(cellularRxSignalStrengthDescription[i]);
4657 sb.append(" ");
4658 formatTimeMs(sb, time/1000);
4659 sb.append("(");
4660 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4661 sb.append(") ");
4662 }
4663 if (!didOne) sb.append(" (no activity)");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004664 pw.println(sb.toString());
4665
Siddharth Ray3c648c42017-10-02 17:30:58 -07004666 printControllerActivity(pw, sb, prefix, "Cellular",
4667 getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004668
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004669 pw.print(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004670 sb.setLength(0);
4671 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004672 sb.append(" Wifi Statistics:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004673 pw.println(sb.toString());
4674
Siddharth Ray3c648c42017-10-02 17:30:58 -07004675 pw.print(" Wifi data received: "); pw.println(formatBytesLocked(wifiRxTotalBytes));
4676 pw.print(" Wifi data sent: "); pw.println(formatBytesLocked(wifiTxTotalBytes));
4677 pw.print(" Wifi packets received: "); pw.println(wifiRxTotalPackets);
4678 pw.print(" Wifi packets sent: "); pw.println(wifiTxTotalPackets);
4679
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004680 sb.setLength(0);
4681 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004682 sb.append(" Wifi states:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004683 didOne = false;
4684 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004685 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004686 if (time == 0) {
4687 continue;
4688 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004689 sb.append("\n ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004690 didOne = true;
4691 sb.append(WIFI_STATE_NAMES[i]);
4692 sb.append(" ");
4693 formatTimeMs(sb, time/1000);
4694 sb.append("(");
4695 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4696 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004697 }
4698 if (!didOne) sb.append(" (no activity)");
4699 pw.println(sb.toString());
4700
4701 sb.setLength(0);
4702 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004703 sb.append(" Wifi supplicant states:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004704 didOne = false;
4705 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4706 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4707 if (time == 0) {
4708 continue;
4709 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004710 sb.append("\n ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004711 didOne = true;
4712 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4713 sb.append(" ");
4714 formatTimeMs(sb, time/1000);
4715 sb.append("(");
4716 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4717 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004718 }
4719 if (!didOne) sb.append(" (no activity)");
4720 pw.println(sb.toString());
4721
4722 sb.setLength(0);
4723 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004724 sb.append(" Wifi Rx signal strength (RSSI):");
4725 final String[] wifiRxSignalStrengthDescription = new String[]{
4726 "very poor (less than -88.75dBm): ",
4727 "poor (-88.75 to -77.5dBm): ",
4728 "moderate (-77.5dBm to -66.25dBm): ",
4729 "good (-66.25dBm to -55dBm): ",
4730 "great (greater than -55dBm): "};
Dianne Hackborn3251b902014-06-20 14:40:53 -07004731 didOne = false;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004732 final int numWifiRxBins = Math.min(NUM_WIFI_SIGNAL_STRENGTH_BINS,
4733 wifiRxSignalStrengthDescription.length);
4734 for (int i=0; i<numWifiRxBins; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004735 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4736 if (time == 0) {
4737 continue;
4738 }
4739 sb.append("\n ");
4740 sb.append(prefix);
4741 didOne = true;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004742 sb.append(" ");
4743 sb.append(wifiRxSignalStrengthDescription[i]);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004744 formatTimeMs(sb, time/1000);
4745 sb.append("(");
4746 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4747 sb.append(") ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004748 }
4749 if (!didOne) sb.append(" (no activity)");
4750 pw.println(sb.toString());
4751
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004752 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004753
Adam Lesinski50e47602015-12-04 17:04:54 -08004754 pw.print(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004755 sb.setLength(0);
4756 sb.append(prefix);
Siddharth Ray78ccaf52017-12-23 16:16:21 -08004757 sb.append(" GPS Statistics:");
4758 pw.println(sb.toString());
4759
4760 sb.setLength(0);
4761 sb.append(prefix);
4762 sb.append(" GPS signal quality (Top 4 Average CN0):");
4763 final String[] gpsSignalQualityDescription = new String[]{
4764 "poor (less than 20 dBHz): ",
4765 "good (greater than 20 dBHz): "};
4766 final int numGpsSignalQualityBins = Math.min(GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS,
4767 gpsSignalQualityDescription.length);
4768 for (int i=0; i<numGpsSignalQualityBins; i++) {
4769 final long time = getGpsSignalQualityTime(i, rawRealtime, which);
4770 sb.append("\n ");
4771 sb.append(prefix);
4772 sb.append(" ");
4773 sb.append(gpsSignalQualityDescription[i]);
4774 formatTimeMs(sb, time/1000);
4775 sb.append("(");
4776 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4777 sb.append(") ");
4778 }
4779 pw.println(sb.toString());
4780
4781 final long gpsBatteryDrainMaMs = getGpsBatteryDrainMaMs();
4782 if (gpsBatteryDrainMaMs > 0) {
4783 pw.print(prefix);
4784 sb.setLength(0);
4785 sb.append(prefix);
4786 sb.append(" Battery Drain (mAh): ");
4787 sb.append(Double.toString(((double) gpsBatteryDrainMaMs)/(3600 * 1000)));
4788 pw.println(sb.toString());
4789 }
4790
4791 pw.print(prefix);
4792 sb.setLength(0);
4793 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004794 sb.append(" CONNECTIVITY POWER SUMMARY END");
4795 pw.println(sb.toString());
4796 pw.println("");
4797
4798 pw.print(prefix);
Adam Lesinski50e47602015-12-04 17:04:54 -08004799 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4800 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4801
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004802 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4803 sb.setLength(0);
4804 sb.append(prefix);
4805 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4806 pw.println(sb.toString());
4807
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004808 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4809 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004810
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004811 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004812
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004813 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004814 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004815 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004816 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004817 pw.println(getDischargeStartLevel());
4818 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4819 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004820 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004821 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004822 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004823 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004824 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004825 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004826 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004827 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004828 pw.println(getDischargeAmountScreenOn());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004829 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004830 pw.println(getDischargeAmountScreenOff());
4831 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4832 pw.println(getDischargeAmountScreenDoze());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004833 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004834 } else {
4835 pw.print(prefix); pw.println(" Device battery use since last full charge");
4836 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004837 pw.println(getLowDischargeAmountSinceCharge());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004838 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004839 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004840 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004841 pw.println(getDischargeAmountScreenOnSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004842 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004843 pw.println(getDischargeAmountScreenOffSinceCharge());
4844 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4845 pw.println(getDischargeAmountScreenDozeSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004846 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004847 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004848
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004849 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004850 helper.create(this);
4851 helper.refreshStats(which, UserHandle.USER_ALL);
4852 List<BatterySipper> sippers = helper.getUsageList();
4853 if (sippers != null && sippers.size() > 0) {
4854 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4855 pw.print(prefix); pw.print(" Capacity: ");
4856 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004857 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004858 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4859 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4860 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4861 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004862 pw.println();
4863 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004864 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004865 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004866 switch (bs.drainType) {
4867 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004868 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004869 break;
4870 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004871 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004872 break;
4873 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004874 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004875 break;
4876 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004877 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004878 break;
4879 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004880 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004881 break;
4882 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004883 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004884 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004885 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004886 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004887 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004888 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004889 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004890 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004891 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004892 break;
4893 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004894 pw.print(" User "); pw.print(bs.userId);
4895 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004896 break;
4897 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004898 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004899 break;
4900 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004901 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004902 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004903 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004904 pw.print(" Camera: ");
4905 break;
4906 default:
4907 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004908 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004909 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004910 printmAh(pw, bs.totalPowerMah);
4911
Adam Lesinski57123002015-06-12 16:12:07 -07004912 if (bs.usagePowerMah != bs.totalPowerMah) {
4913 // If the usage (generic power) isn't the whole amount, we list out
4914 // what components are involved in the calculation.
4915
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004916 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004917 if (bs.usagePowerMah != 0) {
4918 pw.print(" usage=");
4919 printmAh(pw, bs.usagePowerMah);
4920 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004921 if (bs.cpuPowerMah != 0) {
4922 pw.print(" cpu=");
4923 printmAh(pw, bs.cpuPowerMah);
4924 }
4925 if (bs.wakeLockPowerMah != 0) {
4926 pw.print(" wake=");
4927 printmAh(pw, bs.wakeLockPowerMah);
4928 }
4929 if (bs.mobileRadioPowerMah != 0) {
4930 pw.print(" radio=");
4931 printmAh(pw, bs.mobileRadioPowerMah);
4932 }
4933 if (bs.wifiPowerMah != 0) {
4934 pw.print(" wifi=");
4935 printmAh(pw, bs.wifiPowerMah);
4936 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004937 if (bs.bluetoothPowerMah != 0) {
4938 pw.print(" bt=");
4939 printmAh(pw, bs.bluetoothPowerMah);
4940 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004941 if (bs.gpsPowerMah != 0) {
4942 pw.print(" gps=");
4943 printmAh(pw, bs.gpsPowerMah);
4944 }
4945 if (bs.sensorPowerMah != 0) {
4946 pw.print(" sensor=");
4947 printmAh(pw, bs.sensorPowerMah);
4948 }
4949 if (bs.cameraPowerMah != 0) {
4950 pw.print(" camera=");
4951 printmAh(pw, bs.cameraPowerMah);
4952 }
4953 if (bs.flashlightPowerMah != 0) {
4954 pw.print(" flash=");
4955 printmAh(pw, bs.flashlightPowerMah);
4956 }
4957 pw.print(" )");
4958 }
Bookatz17d7d9d2017-06-08 14:50:46 -07004959
4960 // If there is additional smearing information, include it.
4961 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
4962 pw.print(" Including smearing: ");
4963 printmAh(pw, bs.totalSmearedPowerMah);
4964 pw.print(" (");
4965 if (bs.screenPowerMah != 0) {
4966 pw.print(" screen=");
4967 printmAh(pw, bs.screenPowerMah);
4968 }
4969 if (bs.proportionalSmearMah != 0) {
4970 pw.print(" proportional=");
4971 printmAh(pw, bs.proportionalSmearMah);
4972 }
4973 pw.print(" )");
4974 }
4975 if (bs.shouldHide) {
4976 pw.print(" Excluded from smearing");
4977 }
4978
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004979 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004980 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004981 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004982 }
4983
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004984 sippers = helper.getMobilemsppList();
4985 if (sippers != null && sippers.size() > 0) {
4986 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004987 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004988 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004989 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004990 sb.setLength(0);
4991 sb.append(prefix); sb.append(" Uid ");
4992 UserHandle.formatUid(sb, bs.uidObj.getUid());
4993 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4994 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4995 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004996 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004997 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004998 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004999 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005000 sb.setLength(0);
5001 sb.append(prefix);
5002 sb.append(" TOTAL TIME: ");
5003 formatTimeMs(sb, totalTime);
5004 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
5005 sb.append(")");
5006 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005007 pw.println();
5008 }
5009
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005010 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
5011 @Override
5012 public int compare(TimerEntry lhs, TimerEntry rhs) {
5013 long lhsTime = lhs.mTime;
5014 long rhsTime = rhs.mTime;
5015 if (lhsTime < rhsTime) {
5016 return 1;
5017 }
5018 if (lhsTime > rhsTime) {
5019 return -1;
5020 }
5021 return 0;
5022 }
5023 };
5024
5025 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005026 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
5027 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005028 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005029 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
5030 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
5031 : kernelWakelocks.entrySet()) {
5032 final BatteryStats.Timer timer = ent.getValue();
5033 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005034 if (totalTimeMillis > 0) {
5035 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
5036 }
5037 }
5038 if (ktimers.size() > 0) {
5039 Collections.sort(ktimers, timerComparator);
5040 pw.print(prefix); pw.println(" All kernel wake locks:");
5041 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005042 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005043 String linePrefix = ": ";
5044 sb.setLength(0);
5045 sb.append(prefix);
5046 sb.append(" Kernel Wake lock ");
5047 sb.append(timer.mName);
5048 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
5049 which, linePrefix);
5050 if (!linePrefix.equals(": ")) {
5051 sb.append(" realtime");
5052 // Only print out wake locks that were held
5053 pw.println(sb.toString());
5054 }
5055 }
5056 pw.println();
5057 }
5058 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005059
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005060 if (timers.size() > 0) {
5061 Collections.sort(timers, timerComparator);
5062 pw.print(prefix); pw.println(" All partial wake locks:");
5063 for (int i=0; i<timers.size(); i++) {
5064 TimerEntry timer = timers.get(i);
5065 sb.setLength(0);
5066 sb.append(" Wake lock ");
5067 UserHandle.formatUid(sb, timer.mId);
5068 sb.append(" ");
5069 sb.append(timer.mName);
5070 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5071 sb.append(" realtime");
5072 pw.println(sb.toString());
5073 }
5074 timers.clear();
5075 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08005076 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005077
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005078 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005079 if (wakeupReasons.size() > 0) {
5080 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005081 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005082 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005083 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005084 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
5085 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005086 }
5087 Collections.sort(reasons, timerComparator);
5088 for (int i=0; i<reasons.size(); i++) {
5089 TimerEntry timer = reasons.get(i);
5090 String linePrefix = ": ";
5091 sb.setLength(0);
5092 sb.append(prefix);
5093 sb.append(" Wakeup reason ");
5094 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005095 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5096 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005097 pw.println(sb.toString());
5098 }
5099 pw.println();
5100 }
Dianne Hackborn81038902012-11-26 17:04:09 -08005101 }
Evan Millar22ac0432009-03-31 11:33:18 -07005102
James Carr2dd7e5e2016-07-20 18:48:39 -07005103 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
Bookatz50df7112017-08-04 14:53:26 -07005104 if (mMemoryStats.size() > 0) {
5105 pw.println(" Memory Stats");
5106 for (int i = 0; i < mMemoryStats.size(); i++) {
5107 sb.setLength(0);
5108 sb.append(" Bandwidth ");
5109 sb.append(mMemoryStats.keyAt(i));
5110 sb.append(" Time ");
5111 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
5112 pw.println(sb.toString());
5113 }
5114 pw.println();
5115 }
5116
5117 final Map<String, ? extends Timer> rpmStats = getRpmStats();
5118 if (rpmStats.size() > 0) {
5119 pw.print(prefix); pw.println(" Resource Power Manager Stats");
5120 if (rpmStats.size() > 0) {
5121 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
5122 final String timerName = ent.getKey();
5123 final Timer timer = ent.getValue();
5124 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5125 }
5126 }
5127 pw.println();
5128 }
Bookatz82b341172017-09-07 19:06:08 -07005129 if (SCREEN_OFF_RPM_STATS_ENABLED) {
5130 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
Bookatz50df7112017-08-04 14:53:26 -07005131 if (screenOffRpmStats.size() > 0) {
Bookatz82b341172017-09-07 19:06:08 -07005132 pw.print(prefix);
5133 pw.println(" Resource Power Manager Stats for when screen was off");
5134 if (screenOffRpmStats.size() > 0) {
5135 for (Map.Entry<String, ? extends Timer> ent : screenOffRpmStats.entrySet()) {
5136 final String timerName = ent.getKey();
5137 final Timer timer = ent.getValue();
5138 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5139 }
Bookatz50df7112017-08-04 14:53:26 -07005140 }
Bookatz82b341172017-09-07 19:06:08 -07005141 pw.println();
Bookatz50df7112017-08-04 14:53:26 -07005142 }
James Carr2dd7e5e2016-07-20 18:48:39 -07005143 }
5144
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005145 final long[] cpuFreqs = getCpuFreqs();
5146 if (cpuFreqs != null) {
5147 sb.setLength(0);
Bookatz50df7112017-08-04 14:53:26 -07005148 sb.append(" CPU freqs:");
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005149 for (int i = 0; i < cpuFreqs.length; ++i) {
5150 sb.append(" " + cpuFreqs[i]);
5151 }
5152 pw.println(sb.toString());
Bookatz50df7112017-08-04 14:53:26 -07005153 pw.println();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005154 }
5155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005156 for (int iu=0; iu<NU; iu++) {
5157 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005158 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005159 continue;
5160 }
Bookatzc8c44962017-05-11 12:12:54 -07005161
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005162 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07005163
5164 pw.print(prefix);
5165 pw.print(" ");
5166 UserHandle.formatUid(pw, uid);
5167 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005168 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005169
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005170 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
5171 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
5172 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
5173 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005174 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
5175 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
5176
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005177 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
5178 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005179 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
5180 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005181
5182 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
5183 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
5184
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005185 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
5186 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
5187 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08005188 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
5189 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5190 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
5191 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005192 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005193
Adam Lesinski5f056f62016-07-14 16:56:08 -07005194 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
5195 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
5196
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005197 if (mobileRxBytes > 0 || mobileTxBytes > 0
5198 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005199 pw.print(prefix); pw.print(" Mobile network: ");
5200 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005201 pw.print(formatBytesLocked(mobileTxBytes));
5202 pw.print(" sent (packets "); pw.print(mobileRxPackets);
5203 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005204 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005205 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
5206 sb.setLength(0);
5207 sb.append(prefix); sb.append(" Mobile radio active: ");
5208 formatTimeMs(sb, uidMobileActiveTime / 1000);
5209 sb.append("(");
5210 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
5211 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
5212 long packets = mobileRxPackets + mobileTxPackets;
5213 if (packets == 0) {
5214 packets = 1;
5215 }
5216 sb.append(" @ ");
5217 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
5218 sb.append(" mspp");
5219 pw.println(sb.toString());
5220 }
5221
Adam Lesinski5f056f62016-07-14 16:56:08 -07005222 if (mobileWakeup > 0) {
5223 sb.setLength(0);
5224 sb.append(prefix);
5225 sb.append(" Mobile radio AP wakeups: ");
5226 sb.append(mobileWakeup);
5227 pw.println(sb.toString());
5228 }
5229
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005230 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
5231 u.getModemControllerActivity(), which);
5232
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005233 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005234 pw.print(prefix); pw.print(" Wi-Fi network: ");
5235 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005236 pw.print(formatBytesLocked(wifiTxBytes));
5237 pw.print(" sent (packets "); pw.print(wifiRxPackets);
5238 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005239 }
5240
Dianne Hackborn62793e42015-03-09 11:15:41 -07005241 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08005242 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005243 || uidWifiRunningTime != 0) {
5244 sb.setLength(0);
5245 sb.append(prefix); sb.append(" Wifi Running: ");
5246 formatTimeMs(sb, uidWifiRunningTime / 1000);
5247 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
5248 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07005249 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005250 formatTimeMs(sb, fullWifiLockOnTime / 1000);
5251 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
5252 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08005253 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005254 formatTimeMs(sb, wifiScanTime / 1000);
5255 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07005256 whichBatteryRealtime)); sb.append(") ");
5257 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08005258 sb.append("x\n");
5259 // actual and background times are unpooled and since reset (regardless of 'which')
5260 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
5261 formatTimeMs(sb, wifiScanActualTime / 1000);
5262 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
5263 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5264 sb.append(") ");
5265 sb.append(wifiScanCount);
5266 sb.append("x\n");
5267 sb.append(prefix); sb.append(" Background Wifi Scan: ");
5268 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
5269 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
5270 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5271 sb.append(") ");
5272 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07005273 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005274 pw.println(sb.toString());
5275 }
5276
Adam Lesinski5f056f62016-07-14 16:56:08 -07005277 if (wifiWakeup > 0) {
5278 sb.setLength(0);
5279 sb.append(prefix);
5280 sb.append(" WiFi AP wakeups: ");
5281 sb.append(wifiWakeup);
5282 pw.println(sb.toString());
5283 }
5284
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005285 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
5286 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07005287
Adam Lesinski50e47602015-12-04 17:04:54 -08005288 if (btRxBytes > 0 || btTxBytes > 0) {
5289 pw.print(prefix); pw.print(" Bluetooth network: ");
5290 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
5291 pw.print(formatBytesLocked(btTxBytes));
5292 pw.println(" sent");
5293 }
5294
Bookatz867c0d72017-03-07 18:23:42 -08005295 final Timer bleTimer = u.getBluetoothScanTimer();
5296 if (bleTimer != null) {
5297 // Convert from microseconds to milliseconds with rounding
5298 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
5299 / 1000;
5300 if (totalTimeMs != 0) {
5301 final int count = bleTimer.getCountLocked(which);
5302 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
5303 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005304 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5305 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
5306 final long actualTimeMsBg = bleTimerBg != null ?
5307 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005308 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07005309 final int resultCount = u.getBluetoothScanResultCounter() != null ?
5310 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005311 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
5312 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
5313 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
5314 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
5315 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
5316 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5317 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
5318 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
5319 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
5320 final Timer unoptimizedScanTimerBg =
5321 u.getBluetoothUnoptimizedScanBackgroundTimer();
5322 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
5323 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5324 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
5325 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005326
5327 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08005328 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07005329 sb.append(prefix);
5330 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08005331 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07005332 sb.append(" (");
5333 sb.append(count);
5334 sb.append(" times)");
5335 if (bleTimer.isRunningLocked()) {
5336 sb.append(" (currently running)");
5337 }
5338 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005339 }
Bookatzb1f04f32017-05-19 13:57:32 -07005340
5341 sb.append(prefix);
5342 sb.append(" Bluetooth Scan (total actual realtime): ");
5343 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
5344 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005345 sb.append(count);
5346 sb.append(" times)");
5347 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07005348 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08005349 }
Bookatzb1f04f32017-05-19 13:57:32 -07005350 sb.append("\n");
5351 if (actualTimeMsBg > 0 || countBg > 0) {
5352 sb.append(prefix);
5353 sb.append(" Bluetooth Scan (background realtime): ");
5354 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
5355 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005356 sb.append(countBg);
5357 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07005358 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
5359 sb.append(" (currently running in background)");
5360 }
5361 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005362 }
Bookatzb1f04f32017-05-19 13:57:32 -07005363
5364 sb.append(prefix);
5365 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07005366 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07005367 sb.append(" (");
5368 sb.append(resultCountBg);
5369 sb.append(" in background)");
5370
5371 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
5372 sb.append("\n");
5373 sb.append(prefix);
5374 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
5375 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
5376 sb.append(" (max ");
5377 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
5378 sb.append(")");
5379 if (unoptimizedScanTimer != null
5380 && unoptimizedScanTimer.isRunningLocked()) {
5381 sb.append(" (currently running unoptimized)");
5382 }
5383 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
5384 sb.append("\n");
5385 sb.append(prefix);
5386 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
5387 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
5388 sb.append(" (max ");
5389 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
5390 sb.append(")");
5391 if (unoptimizedScanTimerBg.isRunningLocked()) {
5392 sb.append(" (currently running unoptimized in background)");
5393 }
5394 }
5395 }
Bookatz867c0d72017-03-07 18:23:42 -08005396 pw.println(sb.toString());
5397 uidActivity = true;
5398 }
5399 }
5400
5401
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005402
Dianne Hackborn617f8772009-03-31 15:04:46 -07005403 if (u.hasUserActivity()) {
5404 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07005405 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005406 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005407 if (val != 0) {
5408 if (!hasData) {
5409 sb.setLength(0);
5410 sb.append(" User activity: ");
5411 hasData = true;
5412 } else {
5413 sb.append(", ");
5414 }
5415 sb.append(val);
5416 sb.append(" ");
5417 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
5418 }
5419 }
5420 if (hasData) {
5421 pw.println(sb.toString());
5422 }
5423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005424
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005425 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
5426 = u.getWakelockStats();
5427 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005428 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005429 int countWakelock = 0;
5430 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
5431 final Uid.Wakelock wl = wakelocks.valueAt(iw);
5432 String linePrefix = ": ";
5433 sb.setLength(0);
5434 sb.append(prefix);
5435 sb.append(" Wake lock ");
5436 sb.append(wakelocks.keyAt(iw));
5437 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
5438 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005439 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
5440 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005441 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005442 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
5443 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005444 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
5445 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005446 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
5447 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07005448 sb.append(" realtime");
5449 pw.println(sb.toString());
5450 uidActivity = true;
5451 countWakelock++;
5452
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005453 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
5454 rawRealtime, which);
5455 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
5456 rawRealtime, which);
5457 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
5458 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005459 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07005460 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005461 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005462 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07005463 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
5464 // pooled and therefore just a lower bound)
5465 long actualTotalPartialWakelock = 0;
5466 long actualBgPartialWakelock = 0;
5467 if (u.getAggregatedPartialWakelockTimer() != null) {
5468 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
5469 // Convert from microseconds to milliseconds with rounding
5470 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07005471 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07005472 final Timer bgAggTimer = aggTimer.getSubTimer();
5473 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005474 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07005475 }
5476
5477 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
5478 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
5479 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005480 sb.setLength(0);
5481 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005482 sb.append(" TOTAL wake: ");
5483 boolean needComma = false;
5484 if (totalFullWakelock != 0) {
5485 needComma = true;
5486 formatTimeMs(sb, totalFullWakelock);
5487 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005488 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005489 if (totalPartialWakelock != 0) {
5490 if (needComma) {
5491 sb.append(", ");
5492 }
5493 needComma = true;
5494 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07005495 sb.append("blamed partial");
5496 }
5497 if (actualTotalPartialWakelock != 0) {
5498 if (needComma) {
5499 sb.append(", ");
5500 }
5501 needComma = true;
5502 formatTimeMs(sb, actualTotalPartialWakelock);
5503 sb.append("actual partial");
5504 }
5505 if (actualBgPartialWakelock != 0) {
5506 if (needComma) {
5507 sb.append(", ");
5508 }
5509 needComma = true;
5510 formatTimeMs(sb, actualBgPartialWakelock);
5511 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005512 }
5513 if (totalWindowWakelock != 0) {
5514 if (needComma) {
5515 sb.append(", ");
5516 }
5517 needComma = true;
5518 formatTimeMs(sb, totalWindowWakelock);
5519 sb.append("window");
5520 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005521 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07005522 if (needComma) {
5523 sb.append(",");
5524 }
5525 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005526 formatTimeMs(sb, totalDrawWakelock);
5527 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07005528 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005529 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005530 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005531 }
5532 }
5533
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07005534 // Calculate multicast wakelock stats
5535 final Timer mcTimer = u.getMulticastWakelockStats();
5536 if (mcTimer != null) {
5537 final long multicastWakeLockTimeMicros = mcTimer.getTotalTimeLocked(rawRealtime, which);
5538 final int multicastWakeLockCount = mcTimer.getCountLocked(which);
5539
5540 if (multicastWakeLockTimeMicros > 0) {
5541 sb.setLength(0);
5542 sb.append(prefix);
5543 sb.append(" WiFi Multicast Wakelock");
5544 sb.append(" count = ");
5545 sb.append(multicastWakeLockCount);
5546 sb.append(" time = ");
5547 formatTimeMsNoSpace(sb, (multicastWakeLockTimeMicros + 500) / 1000);
5548 pw.println(sb.toString());
5549 }
5550 }
5551
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005552 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
5553 for (int isy=syncs.size()-1; isy>=0; isy--) {
5554 final Timer timer = syncs.valueAt(isy);
5555 // Convert from microseconds to milliseconds with rounding
5556 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5557 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07005558 final Timer bgTimer = timer.getSubTimer();
5559 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005560 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07005561 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005562 sb.setLength(0);
5563 sb.append(prefix);
5564 sb.append(" Sync ");
5565 sb.append(syncs.keyAt(isy));
5566 sb.append(": ");
5567 if (totalTime != 0) {
5568 formatTimeMs(sb, totalTime);
5569 sb.append("realtime (");
5570 sb.append(count);
5571 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07005572 if (bgTime > 0) {
5573 sb.append(", ");
5574 formatTimeMs(sb, bgTime);
5575 sb.append("background (");
5576 sb.append(bgCount);
5577 sb.append(" times)");
5578 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005579 } else {
5580 sb.append("(not used)");
5581 }
5582 pw.println(sb.toString());
5583 uidActivity = true;
5584 }
5585
5586 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
5587 for (int ij=jobs.size()-1; ij>=0; ij--) {
5588 final Timer timer = jobs.valueAt(ij);
5589 // Convert from microseconds to milliseconds with rounding
5590 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5591 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07005592 final Timer bgTimer = timer.getSubTimer();
5593 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005594 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07005595 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005596 sb.setLength(0);
5597 sb.append(prefix);
5598 sb.append(" Job ");
5599 sb.append(jobs.keyAt(ij));
5600 sb.append(": ");
5601 if (totalTime != 0) {
5602 formatTimeMs(sb, totalTime);
5603 sb.append("realtime (");
5604 sb.append(count);
5605 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07005606 if (bgTime > 0) {
5607 sb.append(", ");
5608 formatTimeMs(sb, bgTime);
5609 sb.append("background (");
5610 sb.append(bgCount);
5611 sb.append(" times)");
5612 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005613 } else {
5614 sb.append("(not used)");
5615 }
5616 pw.println(sb.toString());
5617 uidActivity = true;
5618 }
5619
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005620 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
5621 for (int ic=completions.size()-1; ic>=0; ic--) {
5622 SparseIntArray types = completions.valueAt(ic);
5623 if (types != null) {
5624 pw.print(prefix);
5625 pw.print(" Job Completions ");
5626 pw.print(completions.keyAt(ic));
5627 pw.print(":");
5628 for (int it=0; it<types.size(); it++) {
5629 pw.print(" ");
5630 pw.print(JobParameters.getReasonName(types.keyAt(it)));
5631 pw.print("(");
5632 pw.print(types.valueAt(it));
5633 pw.print("x)");
5634 }
5635 pw.println();
5636 }
5637 }
5638
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005639 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
5640 prefix, "Flashlight");
5641 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
5642 prefix, "Camera");
5643 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
5644 prefix, "Video");
5645 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
5646 prefix, "Audio");
5647
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005648 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
5649 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005650 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005651 final Uid.Sensor se = sensors.valueAt(ise);
5652 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005653 sb.setLength(0);
5654 sb.append(prefix);
5655 sb.append(" Sensor ");
5656 int handle = se.getHandle();
5657 if (handle == Uid.Sensor.GPS) {
5658 sb.append("GPS");
5659 } else {
5660 sb.append(handle);
5661 }
5662 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005663
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005664 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005665 if (timer != null) {
5666 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005667 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5668 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005669 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005670 final Timer bgTimer = se.getSensorBackgroundTime();
5671 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005672 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5673 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5674 final long bgActualTime = bgTimer != null ?
5675 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5676
Dianne Hackborn61659e52014-07-09 16:13:01 -07005677 //timer.logState();
5678 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005679 if (actualTime != totalTime) {
5680 formatTimeMs(sb, totalTime);
5681 sb.append("blamed realtime, ");
5682 }
5683
5684 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005685 sb.append("realtime (");
5686 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005687 sb.append(" times)");
5688
5689 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005690 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005691 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5692 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005693 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005694 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005696 } else {
5697 sb.append("(not used)");
5698 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005699 } else {
5700 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005701 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005702
5703 pw.println(sb.toString());
5704 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005705 }
5706
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005707 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5708 "Vibrator");
5709 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5710 prefix, "Foreground activities");
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005711 uidActivity |= printTimer(pw, sb, u.getForegroundServiceTimer(), rawRealtime, which,
5712 prefix, "Foreground services");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005713
Dianne Hackborn61659e52014-07-09 16:13:01 -07005714 long totalStateTime = 0;
5715 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5716 long time = u.getProcessStateTime(ips, rawRealtime, which);
5717 if (time > 0) {
5718 totalStateTime += time;
5719 sb.setLength(0);
5720 sb.append(prefix);
5721 sb.append(" ");
5722 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5723 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005724 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005725 pw.println(sb.toString());
5726 uidActivity = true;
5727 }
5728 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005729 if (totalStateTime > 0) {
5730 sb.setLength(0);
5731 sb.append(prefix);
5732 sb.append(" Total running: ");
5733 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5734 pw.println(sb.toString());
5735 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005736
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005737 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5738 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005739 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005740 sb.setLength(0);
5741 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005742 sb.append(" Total cpu time: u=");
5743 formatTimeMs(sb, userCpuTimeUs / 1000);
5744 sb.append("s=");
5745 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005746 pw.println(sb.toString());
5747 }
5748
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005749 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5750 if (cpuFreqTimes != null) {
5751 sb.setLength(0);
5752 sb.append(" Total cpu time per freq:");
5753 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5754 sb.append(" " + cpuFreqTimes[i]);
5755 }
5756 pw.println(sb.toString());
5757 }
5758 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5759 if (screenOffCpuFreqTimes != null) {
5760 sb.setLength(0);
5761 sb.append(" Total screen-off cpu time per freq:");
5762 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5763 sb.append(" " + screenOffCpuFreqTimes[i]);
5764 }
5765 pw.println(sb.toString());
5766 }
5767
Sudheer Shankab2f83c12017-11-13 19:25:01 -08005768 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
5769 final long[] cpuTimes = u.getCpuFreqTimes(which, procState);
5770 if (cpuTimes != null) {
5771 sb.setLength(0);
5772 sb.append(" Cpu times per freq at state "
5773 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5774 for (int i = 0; i < cpuTimes.length; ++i) {
5775 sb.append(" " + cpuTimes[i]);
5776 }
5777 pw.println(sb.toString());
5778 }
5779
5780 final long[] screenOffCpuTimes = u.getScreenOffCpuFreqTimes(which, procState);
5781 if (screenOffCpuTimes != null) {
5782 sb.setLength(0);
5783 sb.append(" Screen-off cpu times per freq at state "
5784 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5785 for (int i = 0; i < screenOffCpuTimes.length; ++i) {
5786 sb.append(" " + screenOffCpuTimes[i]);
5787 }
5788 pw.println(sb.toString());
5789 }
5790 }
5791
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005792 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5793 = u.getProcessStats();
5794 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5795 final Uid.Proc ps = processStats.valueAt(ipr);
5796 long userTime;
5797 long systemTime;
5798 long foregroundTime;
5799 int starts;
5800 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005801
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005802 userTime = ps.getUserTime(which);
5803 systemTime = ps.getSystemTime(which);
5804 foregroundTime = ps.getForegroundTime(which);
5805 starts = ps.getStarts(which);
5806 final int numCrashes = ps.getNumCrashes(which);
5807 final int numAnrs = ps.getNumAnrs(which);
5808 numExcessive = which == STATS_SINCE_CHARGED
5809 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005810
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005811 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5812 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5813 sb.setLength(0);
5814 sb.append(prefix); sb.append(" Proc ");
5815 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5816 sb.append(prefix); sb.append(" CPU: ");
5817 formatTimeMs(sb, userTime); sb.append("usr + ");
5818 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5819 formatTimeMs(sb, foregroundTime); sb.append("fg");
5820 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5821 sb.append("\n"); sb.append(prefix); sb.append(" ");
5822 boolean hasOne = false;
5823 if (starts != 0) {
5824 hasOne = true;
5825 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005826 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005827 if (numCrashes != 0) {
5828 if (hasOne) {
5829 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005830 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005831 hasOne = true;
5832 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005833 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005834 if (numAnrs != 0) {
5835 if (hasOne) {
5836 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005837 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005838 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005839 }
5840 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005841 pw.println(sb.toString());
5842 for (int e=0; e<numExcessive; e++) {
5843 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5844 if (ew != null) {
5845 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07005846 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005847 pw.print("cpu");
5848 } else {
5849 pw.print("unknown");
5850 }
5851 pw.print(" use: ");
5852 TimeUtils.formatDuration(ew.usedTime, pw);
5853 pw.print(" over ");
5854 TimeUtils.formatDuration(ew.overTime, pw);
5855 if (ew.overTime != 0) {
5856 pw.print(" (");
5857 pw.print((ew.usedTime*100)/ew.overTime);
5858 pw.println("%)");
5859 }
5860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005861 }
5862 uidActivity = true;
5863 }
5864 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005865
5866 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5867 = u.getPackageStats();
5868 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
5869 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
5870 pw.println(":");
5871 boolean apkActivity = false;
5872 final Uid.Pkg ps = packageStats.valueAt(ipkg);
5873 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
5874 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
5875 pw.print(prefix); pw.print(" Wakeup alarm ");
5876 pw.print(alarms.keyAt(iwa)); pw.print(": ");
5877 pw.print(alarms.valueAt(iwa).getCountLocked(which));
5878 pw.println(" times");
5879 apkActivity = true;
5880 }
5881 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
5882 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
5883 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
5884 final long startTime = ss.getStartTime(batteryUptime, which);
5885 final int starts = ss.getStarts(which);
5886 final int launches = ss.getLaunches(which);
5887 if (startTime != 0 || starts != 0 || launches != 0) {
5888 sb.setLength(0);
5889 sb.append(prefix); sb.append(" Service ");
5890 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
5891 sb.append(prefix); sb.append(" Created for: ");
5892 formatTimeMs(sb, startTime / 1000);
5893 sb.append("uptime\n");
5894 sb.append(prefix); sb.append(" Starts: ");
5895 sb.append(starts);
5896 sb.append(", launches: "); sb.append(launches);
5897 pw.println(sb.toString());
5898 apkActivity = true;
5899 }
5900 }
5901 if (!apkActivity) {
5902 pw.print(prefix); pw.println(" (nothing executed)");
5903 }
5904 uidActivity = true;
5905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005906 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005907 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005908 }
5909 }
5910 }
5911
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005912 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005913 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005914 int diff = oldval ^ newval;
5915 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005916 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005917 for (int i=0; i<descriptions.length; i++) {
5918 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005919 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005920 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005921 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005922 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005923 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005924 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
5925 didWake = true;
5926 pw.print("=");
5927 if (longNames) {
5928 UserHandle.formatUid(pw, wakelockTag.uid);
5929 pw.print(":\"");
5930 pw.print(wakelockTag.string);
5931 pw.print("\"");
5932 } else {
5933 pw.print(wakelockTag.poolIdx);
5934 }
5935 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005936 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005937 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005938 pw.print("=");
5939 int val = (newval&bd.mask)>>bd.shift;
5940 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005941 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005942 } else {
5943 pw.print(val);
5944 }
5945 }
5946 }
5947 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005948 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07005949 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005950 if (longNames) {
5951 UserHandle.formatUid(pw, wakelockTag.uid);
5952 pw.print(":\"");
5953 pw.print(wakelockTag.string);
5954 pw.print("\"");
5955 } else {
5956 pw.print(wakelockTag.poolIdx);
5957 }
5958 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005959 }
Mike Mac2f518a2017-09-19 16:06:03 -07005960
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005961 public void prepareForDumpLocked() {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07005962 // We don't need to require subclasses implement this.
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005963 }
5964
5965 public static class HistoryPrinter {
5966 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005967 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005968 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005969 int oldStatus = -1;
5970 int oldHealth = -1;
5971 int oldPlug = -1;
5972 int oldTemp = -1;
5973 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005974 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005975 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005976
Dianne Hackborn3251b902014-06-20 14:40:53 -07005977 void reset() {
5978 oldState = oldState2 = 0;
5979 oldLevel = -1;
5980 oldStatus = -1;
5981 oldHealth = -1;
5982 oldPlug = -1;
5983 oldTemp = -1;
5984 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005985 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07005986 }
5987
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005988 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005989 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005990 if (!checkin) {
5991 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005992 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005993 pw.print(" (");
5994 pw.print(rec.numReadInts);
5995 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005996 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005997 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5998 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005999 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006000 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006001 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006002 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006003 }
6004 lastTime = rec.time;
6005 }
6006 if (rec.cmd == HistoryItem.CMD_START) {
6007 if (checkin) {
6008 pw.print(":");
6009 }
6010 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006011 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006012 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
6013 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006014 if (checkin) {
6015 pw.print(":");
6016 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006017 if (rec.cmd == HistoryItem.CMD_RESET) {
6018 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006019 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006020 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006021 pw.print("TIME:");
6022 if (checkin) {
6023 pw.println(rec.currentTime);
6024 } else {
6025 pw.print(" ");
6026 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6027 rec.currentTime).toString());
6028 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006029 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
6030 if (checkin) {
6031 pw.print(":");
6032 }
6033 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006034 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
6035 if (checkin) {
6036 pw.print(":");
6037 }
6038 pw.println("*OVERFLOW*");
6039 } else {
6040 if (!checkin) {
6041 if (rec.batteryLevel < 10) pw.print("00");
6042 else if (rec.batteryLevel < 100) pw.print("0");
6043 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006044 if (verbose) {
6045 pw.print(" ");
6046 if (rec.states < 0) ;
6047 else if (rec.states < 0x10) pw.print("0000000");
6048 else if (rec.states < 0x100) pw.print("000000");
6049 else if (rec.states < 0x1000) pw.print("00000");
6050 else if (rec.states < 0x10000) pw.print("0000");
6051 else if (rec.states < 0x100000) pw.print("000");
6052 else if (rec.states < 0x1000000) pw.print("00");
6053 else if (rec.states < 0x10000000) pw.print("0");
6054 pw.print(Integer.toHexString(rec.states));
6055 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006056 } else {
6057 if (oldLevel != rec.batteryLevel) {
6058 oldLevel = rec.batteryLevel;
6059 pw.print(",Bl="); pw.print(rec.batteryLevel);
6060 }
6061 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006062 if (oldStatus != rec.batteryStatus) {
6063 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006064 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006065 switch (oldStatus) {
6066 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006067 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006068 break;
6069 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006070 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006071 break;
6072 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006073 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006074 break;
6075 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006076 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006077 break;
6078 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006079 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006080 break;
6081 default:
6082 pw.print(oldStatus);
6083 break;
6084 }
6085 }
6086 if (oldHealth != rec.batteryHealth) {
6087 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006088 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006089 switch (oldHealth) {
6090 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006091 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006092 break;
6093 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006094 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006095 break;
6096 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006097 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006098 break;
6099 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006100 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006101 break;
6102 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006103 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006104 break;
6105 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006106 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006107 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006108 case BatteryManager.BATTERY_HEALTH_COLD:
6109 pw.print(checkin ? "c" : "cold");
6110 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006111 default:
6112 pw.print(oldHealth);
6113 break;
6114 }
6115 }
6116 if (oldPlug != rec.batteryPlugType) {
6117 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006118 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006119 switch (oldPlug) {
6120 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006121 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006122 break;
6123 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006124 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006125 break;
6126 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006127 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006128 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006129 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006130 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006131 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006132 default:
6133 pw.print(oldPlug);
6134 break;
6135 }
6136 }
6137 if (oldTemp != rec.batteryTemperature) {
6138 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006139 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006140 pw.print(oldTemp);
6141 }
6142 if (oldVolt != rec.batteryVoltage) {
6143 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006144 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006145 pw.print(oldVolt);
6146 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006147 final int chargeMAh = rec.batteryChargeUAh / 1000;
6148 if (oldChargeMAh != chargeMAh) {
6149 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07006150 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006151 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07006152 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006153 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006154 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006155 printBitDescriptions(pw, oldState2, rec.states2, null,
6156 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006157 if (rec.wakeReasonTag != null) {
6158 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006159 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006160 pw.print(rec.wakeReasonTag.poolIdx);
6161 } else {
6162 pw.print(" wake_reason=");
6163 pw.print(rec.wakeReasonTag.uid);
6164 pw.print(":\"");
6165 pw.print(rec.wakeReasonTag.string);
6166 pw.print("\"");
6167 }
6168 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08006169 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006170 pw.print(checkin ? "," : " ");
6171 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
6172 pw.print("+");
6173 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
6174 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006175 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006176 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
6177 : HISTORY_EVENT_NAMES;
6178 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
6179 | HistoryItem.EVENT_FLAG_FINISH);
6180 if (idx >= 0 && idx < eventNames.length) {
6181 pw.print(eventNames[idx]);
6182 } else {
6183 pw.print(checkin ? "Ev" : "event");
6184 pw.print(idx);
6185 }
6186 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006187 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006188 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08006189 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08006190 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
6191 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006192 pw.print(":\"");
6193 pw.print(rec.eventTag.string);
6194 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006195 }
6196 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006197 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006198 if (rec.stepDetails != null) {
6199 if (!checkin) {
6200 pw.print(" Details: cpu=");
6201 pw.print(rec.stepDetails.userTime);
6202 pw.print("u+");
6203 pw.print(rec.stepDetails.systemTime);
6204 pw.print("s");
6205 if (rec.stepDetails.appCpuUid1 >= 0) {
6206 pw.print(" (");
6207 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
6208 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6209 if (rec.stepDetails.appCpuUid2 >= 0) {
6210 pw.print(", ");
6211 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
6212 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6213 }
6214 if (rec.stepDetails.appCpuUid3 >= 0) {
6215 pw.print(", ");
6216 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
6217 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6218 }
6219 pw.print(')');
6220 }
6221 pw.println();
6222 pw.print(" /proc/stat=");
6223 pw.print(rec.stepDetails.statUserTime);
6224 pw.print(" usr, ");
6225 pw.print(rec.stepDetails.statSystemTime);
6226 pw.print(" sys, ");
6227 pw.print(rec.stepDetails.statIOWaitTime);
6228 pw.print(" io, ");
6229 pw.print(rec.stepDetails.statIrqTime);
6230 pw.print(" irq, ");
6231 pw.print(rec.stepDetails.statSoftIrqTime);
6232 pw.print(" sirq, ");
6233 pw.print(rec.stepDetails.statIdlTime);
6234 pw.print(" idle");
6235 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
6236 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
6237 + rec.stepDetails.statSoftIrqTime;
6238 int total = totalRun + rec.stepDetails.statIdlTime;
6239 if (total > 0) {
6240 pw.print(" (");
6241 float perc = ((float)totalRun) / ((float)total) * 100;
6242 pw.print(String.format("%.1f%%", perc));
6243 pw.print(" of ");
6244 StringBuilder sb = new StringBuilder(64);
6245 formatTimeMsNoSpace(sb, total*10);
6246 pw.print(sb);
6247 pw.print(")");
6248 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006249 pw.print(", PlatformIdleStat ");
6250 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006251 pw.println();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006252
6253 pw.print(", SubsystemPowerState ");
6254 pw.print(rec.stepDetails.statSubsystemPowerState);
6255 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006256 } else {
6257 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6258 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
6259 pw.print(rec.stepDetails.userTime);
6260 pw.print(":");
6261 pw.print(rec.stepDetails.systemTime);
6262 if (rec.stepDetails.appCpuUid1 >= 0) {
6263 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
6264 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6265 if (rec.stepDetails.appCpuUid2 >= 0) {
6266 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
6267 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6268 }
6269 if (rec.stepDetails.appCpuUid3 >= 0) {
6270 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
6271 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6272 }
6273 }
6274 pw.println();
6275 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6276 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
6277 pw.print(rec.stepDetails.statUserTime);
6278 pw.print(',');
6279 pw.print(rec.stepDetails.statSystemTime);
6280 pw.print(',');
6281 pw.print(rec.stepDetails.statIOWaitTime);
6282 pw.print(',');
6283 pw.print(rec.stepDetails.statIrqTime);
6284 pw.print(',');
6285 pw.print(rec.stepDetails.statSoftIrqTime);
6286 pw.print(',');
6287 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006288 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006289 if (rec.stepDetails.statPlatformIdleState != null) {
6290 pw.print(rec.stepDetails.statPlatformIdleState);
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006291 if (rec.stepDetails.statSubsystemPowerState != null) {
6292 pw.print(',');
6293 }
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006294 }
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006295
6296 if (rec.stepDetails.statSubsystemPowerState != null) {
6297 pw.print(rec.stepDetails.statSubsystemPowerState);
6298 }
6299 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006300 }
6301 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006302 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006303 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006304 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006305 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006306
6307 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
6308 UserHandle.formatUid(pw, uid);
6309 pw.print("=");
6310 pw.print(utime);
6311 pw.print("u+");
6312 pw.print(stime);
6313 pw.print("s");
6314 }
6315
6316 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
6317 pw.print('/');
6318 pw.print(uid);
6319 pw.print(":");
6320 pw.print(utime);
6321 pw.print(":");
6322 pw.print(stime);
6323 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006324 }
6325
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006326 private void printSizeValue(PrintWriter pw, long size) {
6327 float result = size;
6328 String suffix = "";
6329 if (result >= 10*1024) {
6330 suffix = "KB";
6331 result = result / 1024;
6332 }
6333 if (result >= 10*1024) {
6334 suffix = "MB";
6335 result = result / 1024;
6336 }
6337 if (result >= 10*1024) {
6338 suffix = "GB";
6339 result = result / 1024;
6340 }
6341 if (result >= 10*1024) {
6342 suffix = "TB";
6343 result = result / 1024;
6344 }
6345 if (result >= 10*1024) {
6346 suffix = "PB";
6347 result = result / 1024;
6348 }
6349 pw.print((int)result);
6350 pw.print(suffix);
6351 }
6352
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006353 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
6354 String label3, long estimatedTime) {
6355 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006356 return false;
6357 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006358 pw.print(label1);
6359 pw.print(label2);
6360 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006361 StringBuilder sb = new StringBuilder(64);
6362 formatTimeMs(sb, estimatedTime);
6363 pw.print(sb);
6364 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006365 return true;
6366 }
6367
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006368 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
6369 LevelStepTracker steps, boolean checkin) {
6370 if (steps == null) {
6371 return false;
6372 }
6373 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006374 if (count <= 0) {
6375 return false;
6376 }
6377 if (!checkin) {
6378 pw.println(header);
6379 }
Kweku Adams030980a2015-04-01 16:07:48 -07006380 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006381 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006382 long duration = steps.getDurationAt(i);
6383 int level = steps.getLevelAt(i);
6384 long initMode = steps.getInitModeAt(i);
6385 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006386 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006387 lineArgs[0] = Long.toString(duration);
6388 lineArgs[1] = Integer.toString(level);
6389 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6390 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6391 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
6392 case Display.STATE_ON: lineArgs[2] = "s+"; break;
6393 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
6394 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07006395 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006396 }
6397 } else {
6398 lineArgs[2] = "";
6399 }
6400 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6401 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
6402 } else {
6403 lineArgs[3] = "";
6404 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006405 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07006406 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006407 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07006408 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006409 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006410 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
6411 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006412 pw.print(prefix);
6413 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006414 TimeUtils.formatDuration(duration, pw);
6415 pw.print(" to "); pw.print(level);
6416 boolean haveModes = false;
6417 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6418 pw.print(" (");
6419 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6420 case Display.STATE_OFF: pw.print("screen-off"); break;
6421 case Display.STATE_ON: pw.print("screen-on"); break;
6422 case Display.STATE_DOZE: pw.print("screen-doze"); break;
6423 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07006424 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006425 }
6426 haveModes = true;
6427 }
6428 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6429 pw.print(haveModes ? ", " : " (");
6430 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
6431 ? "power-save-on" : "power-save-off");
6432 haveModes = true;
6433 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006434 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6435 pw.print(haveModes ? ", " : " (");
6436 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6437 ? "device-idle-on" : "device-idle-off");
6438 haveModes = true;
6439 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006440 if (haveModes) {
6441 pw.print(")");
6442 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006443 pw.println();
6444 }
6445 }
6446 return true;
6447 }
6448
Kweku Adams87b19ec2017-10-09 12:40:03 -07006449 private static void dumpDurationSteps(ProtoOutputStream proto, long fieldId,
6450 LevelStepTracker steps) {
6451 if (steps == null) {
6452 return;
6453 }
6454 int count = steps.mNumStepDurations;
Kweku Adams87b19ec2017-10-09 12:40:03 -07006455 for (int i = 0; i < count; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07006456 long token = proto.start(fieldId);
Kweku Adams87b19ec2017-10-09 12:40:03 -07006457 proto.write(SystemProto.BatteryLevelStep.DURATION_MS, steps.getDurationAt(i));
6458 proto.write(SystemProto.BatteryLevelStep.LEVEL, steps.getLevelAt(i));
6459
6460 final long initMode = steps.getInitModeAt(i);
6461 final long modMode = steps.getModModeAt(i);
6462
6463 int ds = SystemProto.BatteryLevelStep.DS_MIXED;
6464 if ((modMode & STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6465 switch ((int) (initMode & STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6466 case Display.STATE_OFF:
6467 ds = SystemProto.BatteryLevelStep.DS_OFF;
6468 break;
6469 case Display.STATE_ON:
6470 ds = SystemProto.BatteryLevelStep.DS_ON;
6471 break;
6472 case Display.STATE_DOZE:
6473 ds = SystemProto.BatteryLevelStep.DS_DOZE;
6474 break;
6475 case Display.STATE_DOZE_SUSPEND:
6476 ds = SystemProto.BatteryLevelStep.DS_DOZE_SUSPEND;
6477 break;
6478 default:
6479 ds = SystemProto.BatteryLevelStep.DS_ERROR;
6480 break;
6481 }
6482 }
6483 proto.write(SystemProto.BatteryLevelStep.DISPLAY_STATE, ds);
6484
6485 int psm = SystemProto.BatteryLevelStep.PSM_MIXED;
6486 if ((modMode & STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6487 psm = (initMode & STEP_LEVEL_MODE_POWER_SAVE) != 0
6488 ? SystemProto.BatteryLevelStep.PSM_ON : SystemProto.BatteryLevelStep.PSM_OFF;
6489 }
6490 proto.write(SystemProto.BatteryLevelStep.POWER_SAVE_MODE, psm);
6491
6492 int im = SystemProto.BatteryLevelStep.IM_MIXED;
6493 if ((modMode & STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6494 im = (initMode & STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6495 ? SystemProto.BatteryLevelStep.IM_ON : SystemProto.BatteryLevelStep.IM_OFF;
6496 }
6497 proto.write(SystemProto.BatteryLevelStep.IDLE_MODE, im);
6498
6499 proto.end(token);
6500 }
6501 }
6502
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006503 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006504 public static final int DUMP_DAILY_ONLY = 1<<2;
6505 public static final int DUMP_HISTORY_ONLY = 1<<3;
6506 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
6507 public static final int DUMP_VERBOSE = 1<<5;
6508 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006509
Dianne Hackborn37de0982014-05-09 09:32:18 -07006510 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
6511 final HistoryPrinter hprinter = new HistoryPrinter();
6512 final HistoryItem rec = new HistoryItem();
6513 long lastTime = -1;
6514 long baseTime = -1;
6515 boolean printed = false;
6516 HistoryEventTracker tracker = null;
6517 while (getNextHistoryLocked(rec)) {
6518 lastTime = rec.time;
6519 if (baseTime < 0) {
6520 baseTime = lastTime;
6521 }
6522 if (rec.time >= histStart) {
6523 if (histStart >= 0 && !printed) {
6524 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07006525 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006526 || rec.cmd == HistoryItem.CMD_START
6527 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006528 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006529 hprinter.printNextItem(pw, rec, baseTime, checkin,
6530 (flags&DUMP_VERBOSE) != 0);
6531 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006532 } else if (rec.currentTime != 0) {
6533 printed = true;
6534 byte cmd = rec.cmd;
6535 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006536 hprinter.printNextItem(pw, rec, baseTime, checkin,
6537 (flags&DUMP_VERBOSE) != 0);
6538 rec.cmd = cmd;
6539 }
6540 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006541 if (rec.cmd != HistoryItem.CMD_UPDATE) {
6542 hprinter.printNextItem(pw, rec, baseTime, checkin,
6543 (flags&DUMP_VERBOSE) != 0);
6544 rec.cmd = HistoryItem.CMD_UPDATE;
6545 }
6546 int oldEventCode = rec.eventCode;
6547 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006548 rec.eventTag = new HistoryTag();
6549 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
6550 HashMap<String, SparseIntArray> active
6551 = tracker.getStateForEvent(i);
6552 if (active == null) {
6553 continue;
6554 }
6555 for (HashMap.Entry<String, SparseIntArray> ent
6556 : active.entrySet()) {
6557 SparseIntArray uids = ent.getValue();
6558 for (int j=0; j<uids.size(); j++) {
6559 rec.eventCode = i;
6560 rec.eventTag.string = ent.getKey();
6561 rec.eventTag.uid = uids.keyAt(j);
6562 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006563 hprinter.printNextItem(pw, rec, baseTime, checkin,
6564 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006565 rec.wakeReasonTag = null;
6566 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006567 }
6568 }
6569 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006570 rec.eventCode = oldEventCode;
6571 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006572 tracker = null;
6573 }
6574 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006575 hprinter.printNextItem(pw, rec, baseTime, checkin,
6576 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07006577 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
6578 // This is an attempt to aggregate the previous state and generate
6579 // fake events to reflect that state at the point where we start
6580 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07006581 if (tracker == null) {
6582 tracker = new HistoryEventTracker();
6583 }
6584 tracker.updateState(rec.eventCode, rec.eventTag.string,
6585 rec.eventTag.uid, rec.eventTag.poolIdx);
6586 }
6587 }
6588 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07006589 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006590 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
6591 }
6592 }
6593
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006594 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
6595 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
6596 if (steps == null) {
6597 return;
6598 }
6599 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
6600 if (timeRemaining >= 0) {
6601 pw.print(prefix); pw.print(label); pw.print(" total time: ");
6602 tmpSb.setLength(0);
6603 formatTimeMs(tmpSb, timeRemaining);
6604 pw.print(tmpSb);
6605 pw.print(" (from "); pw.print(tmpOutInt[0]);
6606 pw.println(" steps)");
6607 }
6608 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6609 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6610 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
6611 if (estimatedTime > 0) {
6612 pw.print(prefix); pw.print(label); pw.print(" ");
6613 pw.print(STEP_LEVEL_MODE_LABELS[i]);
6614 pw.print(" time: ");
6615 tmpSb.setLength(0);
6616 formatTimeMs(tmpSb, estimatedTime);
6617 pw.print(tmpSb);
6618 pw.print(" (from "); pw.print(tmpOutInt[0]);
6619 pw.println(" steps)");
6620 }
6621 }
6622 }
6623
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006624 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
6625 ArrayList<PackageChange> changes) {
6626 if (changes == null) {
6627 return;
6628 }
6629 pw.print(prefix); pw.println("Package changes:");
6630 for (int i=0; i<changes.size(); i++) {
6631 PackageChange pc = changes.get(i);
6632 if (pc.mUpdate) {
6633 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
6634 pw.print(" vers="); pw.println(pc.mVersionCode);
6635 } else {
6636 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
6637 }
6638 }
6639 }
6640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006641 /**
6642 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
6643 *
6644 * @param pw a Printer to receive the dump output.
6645 */
6646 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006647 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006648 prepareForDumpLocked();
6649
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006650 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006651 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006652
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006653 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006654 final long historyTotalSize = getHistoryTotalSize();
6655 final long historyUsedSize = getHistoryUsedSize();
6656 if (startIteratingHistoryLocked()) {
6657 try {
6658 pw.print("Battery History (");
6659 pw.print((100*historyUsedSize)/historyTotalSize);
6660 pw.print("% used, ");
6661 printSizeValue(pw, historyUsedSize);
6662 pw.print(" used of ");
6663 printSizeValue(pw, historyTotalSize);
6664 pw.print(", ");
6665 pw.print(getHistoryStringPoolSize());
6666 pw.print(" strings using ");
6667 printSizeValue(pw, getHistoryStringPoolBytes());
6668 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07006669 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006670 pw.println();
6671 } finally {
6672 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006673 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006674 }
6675
6676 if (startIteratingOldHistoryLocked()) {
6677 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006678 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006679 pw.println("Old battery History:");
6680 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006681 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006682 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006683 if (baseTime < 0) {
6684 baseTime = rec.time;
6685 }
6686 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006687 }
6688 pw.println();
6689 } finally {
6690 finishIteratingOldHistoryLocked();
6691 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006692 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006693 }
6694
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006695 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006696 return;
6697 }
6698
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006699 if (!filtering) {
6700 SparseArray<? extends Uid> uidStats = getUidStats();
6701 final int NU = uidStats.size();
6702 boolean didPid = false;
6703 long nowRealtime = SystemClock.elapsedRealtime();
6704 for (int i=0; i<NU; i++) {
6705 Uid uid = uidStats.valueAt(i);
6706 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
6707 if (pids != null) {
6708 for (int j=0; j<pids.size(); j++) {
6709 Uid.Pid pid = pids.valueAt(j);
6710 if (!didPid) {
6711 pw.println("Per-PID Stats:");
6712 didPid = true;
6713 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006714 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
6715 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006716 pw.print(" PID "); pw.print(pids.keyAt(j));
6717 pw.print(" wake time: ");
6718 TimeUtils.formatDuration(time, pw);
6719 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006720 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006721 }
6722 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006723 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006724 pw.println();
6725 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006726 }
6727
6728 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006729 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
6730 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006731 long timeRemaining = computeBatteryTimeRemaining(
6732 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006733 if (timeRemaining >= 0) {
6734 pw.print(" Estimated discharge time remaining: ");
6735 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6736 pw.println();
6737 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006738 final LevelStepTracker steps = getDischargeLevelStepTracker();
6739 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6740 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
6741 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6742 STEP_LEVEL_MODE_VALUES[i], null));
6743 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006744 pw.println();
6745 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006746 if (dumpDurationSteps(pw, " ", "Charge step durations:",
6747 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006748 long timeRemaining = computeChargeTimeRemaining(
6749 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006750 if (timeRemaining >= 0) {
6751 pw.print(" Estimated charge time remaining: ");
6752 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6753 pw.println();
6754 }
6755 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006756 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006757 }
Dianne Hackbornc81983a2017-10-20 16:16:32 -07006758 if (!filtering || (flags & DUMP_DAILY_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006759 pw.println("Daily stats:");
6760 pw.print(" Current start time: ");
6761 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6762 getCurrentDailyStartTime()).toString());
6763 pw.print(" Next min deadline: ");
6764 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6765 getNextMinDailyDeadline()).toString());
6766 pw.print(" Next max deadline: ");
6767 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6768 getNextMaxDailyDeadline()).toString());
6769 StringBuilder sb = new StringBuilder(64);
6770 int[] outInt = new int[1];
6771 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
6772 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006773 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
6774 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006775 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006776 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
6777 dsteps, false)) {
6778 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6779 sb, outInt);
6780 }
6781 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6782 csteps, false)) {
6783 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6784 sb, outInt);
6785 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006786 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006787 } else {
6788 pw.println(" Current daily steps:");
6789 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6790 sb, outInt);
6791 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6792 sb, outInt);
6793 }
6794 }
6795 DailyItem dit;
6796 int curIndex = 0;
6797 while ((dit=getDailyItemLocked(curIndex)) != null) {
6798 curIndex++;
6799 if ((flags&DUMP_DAILY_ONLY) != 0) {
6800 pw.println();
6801 }
6802 pw.print(" Daily from ");
6803 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6804 pw.print(" to ");
6805 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6806 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006807 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006808 if (dumpDurationSteps(pw, " ",
6809 " Discharge step durations:", dit.mDischargeSteps, false)) {
6810 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6811 sb, outInt);
6812 }
6813 if (dumpDurationSteps(pw, " ",
6814 " Charge step durations:", dit.mChargeSteps, false)) {
6815 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6816 sb, outInt);
6817 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006818 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006819 } else {
6820 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6821 sb, outInt);
6822 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6823 sb, outInt);
6824 }
6825 }
6826 pw.println();
6827 }
6828 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006829 pw.println("Statistics since last charge:");
6830 pw.println(" System starts: " + getStartCount()
6831 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006832 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6833 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006834 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006836 }
Mike Mac2f518a2017-09-19 16:06:03 -07006837
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006838 // This is called from BatteryStatsService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006839 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006840 public void dumpCheckinLocked(Context context, PrintWriter pw,
6841 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006842 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006843
6844 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006845 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6846 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006847
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006848 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6849
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006850 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07006851 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006852 try {
6853 for (int i=0; i<getHistoryStringPoolSize(); i++) {
6854 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6855 pw.print(HISTORY_STRING_POOL); pw.print(',');
6856 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006857 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006858 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006859 pw.print(",\"");
6860 String str = getHistoryTagPoolString(i);
6861 str = str.replace("\\", "\\\\");
6862 str = str.replace("\"", "\\\"");
6863 pw.print(str);
6864 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006865 pw.println();
6866 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006867 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006868 } finally {
6869 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08006870 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006871 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006872 }
6873
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006874 if ((flags & DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006875 return;
6876 }
6877
Dianne Hackborne4a59512010-12-07 11:08:07 -08006878 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006879 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08006880 for (int i=0; i<apps.size(); i++) {
6881 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006882 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
6883 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08006884 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006885 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
6886 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006887 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006888 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006889 }
6890 SparseArray<? extends Uid> uidStats = getUidStats();
6891 final int NU = uidStats.size();
6892 String[] lineArgs = new String[2];
6893 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006894 int uid = UserHandle.getAppId(uidStats.keyAt(i));
6895 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
6896 if (pkgs != null && !pkgs.second.value) {
6897 pkgs.second.value = true;
6898 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08006899 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006900 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006901 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
6902 (Object[])lineArgs);
6903 }
6904 }
6905 }
6906 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006907 if ((flags & DUMP_DAILY_ONLY) == 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006908 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006909 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07006910 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006911 if (timeRemaining >= 0) {
6912 lineArgs[0] = Long.toString(timeRemaining);
6913 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
6914 (Object[])lineArgs);
6915 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006916 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07006917 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006918 if (timeRemaining >= 0) {
6919 lineArgs[0] = Long.toString(timeRemaining);
6920 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
6921 (Object[])lineArgs);
6922 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006923 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
6924 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006926 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006927
Kweku Adams87b19ec2017-10-09 12:40:03 -07006928 /** Dump #STATS_SINCE_CHARGED batterystats data to a proto. @hide */
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006929 public void dumpProtoLocked(Context context, FileDescriptor fd, List<ApplicationInfo> apps,
Kweku Adams6ccebf22017-12-11 12:30:35 -08006930 int flags) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006931 final ProtoOutputStream proto = new ProtoOutputStream(fd);
6932 final long bToken = proto.start(BatteryStatsServiceDumpProto.BATTERYSTATS);
6933 prepareForDumpLocked();
6934
6935 proto.write(BatteryStatsProto.REPORT_VERSION, CHECKIN_VERSION);
6936 proto.write(BatteryStatsProto.PARCEL_VERSION, getParcelVersion());
6937 proto.write(BatteryStatsProto.START_PLATFORM_VERSION, getStartPlatformVersion());
6938 proto.write(BatteryStatsProto.END_PLATFORM_VERSION, getEndPlatformVersion());
6939
Kweku Adams6ccebf22017-12-11 12:30:35 -08006940 // History intentionally not included in proto dump.
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006941
6942 if ((flags & (DUMP_HISTORY_ONLY | DUMP_DAILY_ONLY)) == 0) {
Kweku Adams103351f2017-10-16 14:39:34 -07006943 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false,
6944 (flags & DUMP_DEVICE_WIFI_ONLY) != 0);
6945 helper.create(this);
6946 helper.refreshStats(STATS_SINCE_CHARGED, UserHandle.USER_ALL);
6947
6948 dumpProtoAppsLocked(proto, helper, apps);
6949 dumpProtoSystemLocked(proto, helper);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006950 }
6951
6952 proto.end(bToken);
6953 proto.flush();
6954 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07006955
Kweku Adams103351f2017-10-16 14:39:34 -07006956 private void dumpProtoAppsLocked(ProtoOutputStream proto, BatteryStatsHelper helper,
6957 List<ApplicationInfo> apps) {
6958 final int which = STATS_SINCE_CHARGED;
6959 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
6960 final long rawRealtimeMs = SystemClock.elapsedRealtime();
6961 final long rawRealtimeUs = rawRealtimeMs * 1000;
6962 final long batteryUptimeUs = getBatteryUptime(rawUptimeUs);
6963
6964 SparseArray<ArrayList<String>> aidToPackages = new SparseArray<>();
6965 if (apps != null) {
6966 for (int i = 0; i < apps.size(); ++i) {
6967 ApplicationInfo ai = apps.get(i);
6968 int aid = UserHandle.getAppId(ai.uid);
6969 ArrayList<String> pkgs = aidToPackages.get(aid);
6970 if (pkgs == null) {
6971 pkgs = new ArrayList<String>();
6972 aidToPackages.put(aid, pkgs);
6973 }
6974 pkgs.add(ai.packageName);
6975 }
6976 }
6977
6978 SparseArray<BatterySipper> uidToSipper = new SparseArray<>();
6979 final List<BatterySipper> sippers = helper.getUsageList();
6980 if (sippers != null) {
6981 for (int i = 0; i < sippers.size(); ++i) {
6982 final BatterySipper bs = sippers.get(i);
6983 if (bs.drainType != BatterySipper.DrainType.APP) {
6984 // Others are handled by dumpProtoSystemLocked()
6985 continue;
6986 }
6987 uidToSipper.put(bs.uidObj.getUid(), bs);
6988 }
6989 }
6990
6991 SparseArray<? extends Uid> uidStats = getUidStats();
6992 final int n = uidStats.size();
6993 for (int iu = 0; iu < n; ++iu) {
6994 final long uTkn = proto.start(BatteryStatsProto.UIDS);
6995 final Uid u = uidStats.valueAt(iu);
6996
6997 final int uid = uidStats.keyAt(iu);
6998 proto.write(UidProto.UID, uid);
6999
7000 // Print packages and apk stats (UID_DATA & APK_DATA)
7001 ArrayList<String> pkgs = aidToPackages.get(UserHandle.getAppId(uid));
7002 if (pkgs == null) {
7003 pkgs = new ArrayList<String>();
7004 }
7005 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats =
7006 u.getPackageStats();
7007 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7008 String pkg = packageStats.keyAt(ipkg);
7009 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats =
7010 packageStats.valueAt(ipkg).getServiceStats();
7011 if (serviceStats.size() == 0) {
7012 // Due to the way ActivityManagerService logs wakeup alarms, some packages (for
7013 // example, "android") may be included in the packageStats that aren't part of
7014 // the UID. If they don't have any services, then they shouldn't be listed here.
7015 // These packages won't be a part in the pkgs List.
7016 continue;
7017 }
7018
7019 final long pToken = proto.start(UidProto.PACKAGES);
7020 proto.write(UidProto.Package.NAME, pkg);
7021 // Remove from the packages list since we're logging it here.
7022 pkgs.remove(pkg);
7023
7024 for (int isvc = serviceStats.size() - 1; isvc >= 0; --isvc) {
7025 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
7026 long sToken = proto.start(UidProto.Package.SERVICES);
7027
7028 proto.write(UidProto.Package.Service.NAME, serviceStats.keyAt(isvc));
7029 proto.write(UidProto.Package.Service.START_DURATION_MS,
7030 roundUsToMs(ss.getStartTime(batteryUptimeUs, which)));
7031 proto.write(UidProto.Package.Service.START_COUNT, ss.getStarts(which));
7032 proto.write(UidProto.Package.Service.LAUNCH_COUNT, ss.getLaunches(which));
7033
7034 proto.end(sToken);
7035 }
7036 proto.end(pToken);
7037 }
7038 // Print any remaining packages that weren't in the packageStats map. pkgs is pulled
7039 // from PackageManager data. Packages are only included in packageStats if there was
7040 // specific data tracked for them (services and wakeup alarms, etc.).
7041 for (String p : pkgs) {
7042 final long pToken = proto.start(UidProto.PACKAGES);
7043 proto.write(UidProto.Package.NAME, p);
7044 proto.end(pToken);
7045 }
7046
7047 // Total wakelock data (AGGREGATED_WAKELOCK_DATA)
7048 if (u.getAggregatedPartialWakelockTimer() != null) {
7049 final Timer timer = u.getAggregatedPartialWakelockTimer();
7050 // Times are since reset (regardless of 'which')
7051 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
7052 final Timer bgTimer = timer.getSubTimer();
7053 final long bgTimeMs = bgTimer != null
7054 ? bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
7055 final long awToken = proto.start(UidProto.AGGREGATED_WAKELOCK);
7056 proto.write(UidProto.AggregatedWakelock.PARTIAL_DURATION_MS, totTimeMs);
7057 proto.write(UidProto.AggregatedWakelock.BACKGROUND_PARTIAL_DURATION_MS, bgTimeMs);
7058 proto.end(awToken);
7059 }
7060
7061 // Audio (AUDIO_DATA)
7062 dumpTimer(proto, UidProto.AUDIO, u.getAudioTurnedOnTimer(), rawRealtimeUs, which);
7063
7064 // Bluetooth Controller (BLUETOOTH_CONTROLLER_DATA)
7065 dumpControllerActivityProto(proto, UidProto.BLUETOOTH_CONTROLLER,
7066 u.getBluetoothControllerActivity(), which);
7067
7068 // BLE scans (BLUETOOTH_MISC_DATA) (uses totalDurationMsLocked and MaxDurationMsLocked)
7069 final Timer bleTimer = u.getBluetoothScanTimer();
7070 if (bleTimer != null) {
7071 final long bmToken = proto.start(UidProto.BLUETOOTH_MISC);
7072
7073 dumpTimer(proto, UidProto.BluetoothMisc.APPORTIONED_BLE_SCAN, bleTimer,
7074 rawRealtimeUs, which);
7075 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN,
7076 u.getBluetoothScanBackgroundTimer(), rawRealtimeUs, which);
7077 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
7078 dumpTimer(proto, UidProto.BluetoothMisc.UNOPTIMIZED_BLE_SCAN,
7079 u.getBluetoothUnoptimizedScanTimer(), rawRealtimeUs, which);
7080 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
7081 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_UNOPTIMIZED_BLE_SCAN,
7082 u.getBluetoothUnoptimizedScanBackgroundTimer(), rawRealtimeUs, which);
7083 // Result counters
7084 proto.write(UidProto.BluetoothMisc.BLE_SCAN_RESULT_COUNT,
7085 u.getBluetoothScanResultCounter() != null
7086 ? u.getBluetoothScanResultCounter().getCountLocked(which) : 0);
7087 proto.write(UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN_RESULT_COUNT,
7088 u.getBluetoothScanResultBgCounter() != null
7089 ? u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0);
7090
7091 proto.end(bmToken);
7092 }
7093
7094 // Camera (CAMERA_DATA)
7095 dumpTimer(proto, UidProto.CAMERA, u.getCameraTurnedOnTimer(), rawRealtimeUs, which);
7096
7097 // CPU stats (CPU_DATA & CPU_TIMES_AT_FREQ_DATA)
7098 final long cpuToken = proto.start(UidProto.CPU);
7099 proto.write(UidProto.Cpu.USER_DURATION_MS, roundUsToMs(u.getUserCpuTimeUs(which)));
7100 proto.write(UidProto.Cpu.SYSTEM_DURATION_MS, roundUsToMs(u.getSystemCpuTimeUs(which)));
7101
7102 final long[] cpuFreqs = getCpuFreqs();
7103 if (cpuFreqs != null) {
7104 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
7105 // If total cpuFreqTimes is null, then we don't need to check for
7106 // screenOffCpuFreqTimes.
7107 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
7108 long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
7109 if (screenOffCpuFreqTimeMs == null) {
7110 screenOffCpuFreqTimeMs = new long[cpuFreqTimeMs.length];
7111 }
7112 for (int ic = 0; ic < cpuFreqTimeMs.length; ++ic) {
7113 long cToken = proto.start(UidProto.Cpu.BY_FREQUENCY);
7114 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7115 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7116 cpuFreqTimeMs[ic]);
7117 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7118 screenOffCpuFreqTimeMs[ic]);
7119 proto.end(cToken);
7120 }
7121 }
7122 }
Sudheer Shanka6d658d72018-01-01 01:36:49 -08007123
7124 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
7125 final long[] timesMs = u.getCpuFreqTimes(which, procState);
7126 if (timesMs != null && timesMs.length == cpuFreqs.length) {
7127 long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(which, procState);
7128 if (screenOffTimesMs == null) {
7129 screenOffTimesMs = new long[timesMs.length];
7130 }
7131 final long procToken = proto.start(UidProto.Cpu.BY_PROCESS_STATE);
7132 proto.write(UidProto.Cpu.ByProcessState.PROCESS_STATE, procState);
7133 for (int ic = 0; ic < timesMs.length; ++ic) {
7134 long cToken = proto.start(UidProto.Cpu.ByProcessState.BY_FREQUENCY);
7135 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7136 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7137 timesMs[ic]);
7138 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7139 screenOffTimesMs[ic]);
7140 proto.end(cToken);
7141 }
7142 proto.end(procToken);
7143 }
7144 }
Kweku Adams103351f2017-10-16 14:39:34 -07007145 proto.end(cpuToken);
7146
7147 // Flashlight (FLASHLIGHT_DATA)
7148 dumpTimer(proto, UidProto.FLASHLIGHT, u.getFlashlightTurnedOnTimer(),
7149 rawRealtimeUs, which);
7150
7151 // Foreground activity (FOREGROUND_ACTIVITY_DATA)
7152 dumpTimer(proto, UidProto.FOREGROUND_ACTIVITY, u.getForegroundActivityTimer(),
7153 rawRealtimeUs, which);
7154
7155 // Foreground service (FOREGROUND_SERVICE_DATA)
7156 dumpTimer(proto, UidProto.FOREGROUND_SERVICE, u.getForegroundServiceTimer(),
7157 rawRealtimeUs, which);
7158
7159 // Job completion (JOB_COMPLETION_DATA)
7160 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
7161 final int[] reasons = new int[]{
7162 JobParameters.REASON_CANCELED,
7163 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED,
7164 JobParameters.REASON_PREEMPT,
7165 JobParameters.REASON_TIMEOUT,
7166 JobParameters.REASON_DEVICE_IDLE,
7167 };
7168 for (int ic = 0; ic < completions.size(); ++ic) {
7169 SparseIntArray types = completions.valueAt(ic);
7170 if (types != null) {
7171 final long jcToken = proto.start(UidProto.JOB_COMPLETION);
7172
7173 proto.write(UidProto.JobCompletion.NAME, completions.keyAt(ic));
7174
7175 for (int r : reasons) {
7176 long rToken = proto.start(UidProto.JobCompletion.REASON_COUNT);
7177 proto.write(UidProto.JobCompletion.ReasonCount.NAME, r);
7178 proto.write(UidProto.JobCompletion.ReasonCount.COUNT, types.get(r, 0));
7179 proto.end(rToken);
7180 }
7181
7182 proto.end(jcToken);
7183 }
7184 }
7185
7186 // Scheduled jobs (JOB_DATA)
7187 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
7188 for (int ij = jobs.size() - 1; ij >= 0; --ij) {
7189 final Timer timer = jobs.valueAt(ij);
7190 final Timer bgTimer = timer.getSubTimer();
7191 final long jToken = proto.start(UidProto.JOBS);
7192
7193 proto.write(UidProto.Job.NAME, jobs.keyAt(ij));
7194 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7195 dumpTimer(proto, UidProto.Job.TOTAL, timer, rawRealtimeUs, which);
7196 dumpTimer(proto, UidProto.Job.BACKGROUND, bgTimer, rawRealtimeUs, which);
7197
7198 proto.end(jToken);
7199 }
7200
7201 // Modem Controller (MODEM_CONTROLLER_DATA)
7202 dumpControllerActivityProto(proto, UidProto.MODEM_CONTROLLER,
7203 u.getModemControllerActivity(), which);
7204
7205 // Network stats (NETWORK_DATA)
7206 final long nToken = proto.start(UidProto.NETWORK);
7207 proto.write(UidProto.Network.MOBILE_BYTES_RX,
7208 u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7209 proto.write(UidProto.Network.MOBILE_BYTES_TX,
7210 u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7211 proto.write(UidProto.Network.WIFI_BYTES_RX,
7212 u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7213 proto.write(UidProto.Network.WIFI_BYTES_TX,
7214 u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7215 proto.write(UidProto.Network.BT_BYTES_RX,
7216 u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7217 proto.write(UidProto.Network.BT_BYTES_TX,
7218 u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
7219 proto.write(UidProto.Network.MOBILE_PACKETS_RX,
7220 u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7221 proto.write(UidProto.Network.MOBILE_PACKETS_TX,
7222 u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7223 proto.write(UidProto.Network.WIFI_PACKETS_RX,
7224 u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7225 proto.write(UidProto.Network.WIFI_PACKETS_TX,
7226 u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7227 proto.write(UidProto.Network.MOBILE_ACTIVE_DURATION_MS,
7228 roundUsToMs(u.getMobileRadioActiveTime(which)));
7229 proto.write(UidProto.Network.MOBILE_ACTIVE_COUNT,
7230 u.getMobileRadioActiveCount(which));
7231 proto.write(UidProto.Network.MOBILE_WAKEUP_COUNT,
7232 u.getMobileRadioApWakeupCount(which));
7233 proto.write(UidProto.Network.WIFI_WAKEUP_COUNT,
7234 u.getWifiRadioApWakeupCount(which));
7235 proto.write(UidProto.Network.MOBILE_BYTES_BG_RX,
7236 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA, which));
7237 proto.write(UidProto.Network.MOBILE_BYTES_BG_TX,
7238 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA, which));
7239 proto.write(UidProto.Network.WIFI_BYTES_BG_RX,
7240 u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which));
7241 proto.write(UidProto.Network.WIFI_BYTES_BG_TX,
7242 u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which));
7243 proto.write(UidProto.Network.MOBILE_PACKETS_BG_RX,
7244 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA, which));
7245 proto.write(UidProto.Network.MOBILE_PACKETS_BG_TX,
7246 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA, which));
7247 proto.write(UidProto.Network.WIFI_PACKETS_BG_RX,
7248 u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA, which));
7249 proto.write(UidProto.Network.WIFI_PACKETS_BG_TX,
7250 u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA, which));
7251 proto.end(nToken);
7252
7253 // Power use item (POWER_USE_ITEM_DATA)
7254 BatterySipper bs = uidToSipper.get(uid);
7255 if (bs != null) {
7256 final long bsToken = proto.start(UidProto.POWER_USE_ITEM);
7257 proto.write(UidProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7258 proto.write(UidProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7259 proto.write(UidProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7260 proto.write(UidProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7261 bs.proportionalSmearMah);
7262 proto.end(bsToken);
7263 }
7264
7265 // Processes (PROCESS_DATA)
7266 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats =
7267 u.getProcessStats();
7268 for (int ipr = processStats.size() - 1; ipr >= 0; --ipr) {
7269 final Uid.Proc ps = processStats.valueAt(ipr);
7270 final long prToken = proto.start(UidProto.PROCESS);
7271
7272 proto.write(UidProto.Process.NAME, processStats.keyAt(ipr));
7273 proto.write(UidProto.Process.USER_DURATION_MS, ps.getUserTime(which));
7274 proto.write(UidProto.Process.SYSTEM_DURATION_MS, ps.getSystemTime(which));
7275 proto.write(UidProto.Process.FOREGROUND_DURATION_MS, ps.getForegroundTime(which));
7276 proto.write(UidProto.Process.START_COUNT, ps.getStarts(which));
7277 proto.write(UidProto.Process.ANR_COUNT, ps.getNumAnrs(which));
7278 proto.write(UidProto.Process.CRASH_COUNT, ps.getNumCrashes(which));
7279
7280 proto.end(prToken);
7281 }
7282
7283 // Sensors (SENSOR_DATA)
7284 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
7285 for (int ise = 0; ise < sensors.size(); ++ise) {
7286 final Uid.Sensor se = sensors.valueAt(ise);
7287 final Timer timer = se.getSensorTime();
7288 if (timer == null) {
7289 continue;
7290 }
7291 final Timer bgTimer = se.getSensorBackgroundTime();
7292 final int sensorNumber = sensors.keyAt(ise);
7293 final long seToken = proto.start(UidProto.SENSORS);
7294
7295 proto.write(UidProto.Sensor.ID, sensorNumber);
7296 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7297 dumpTimer(proto, UidProto.Sensor.APPORTIONED, timer, rawRealtimeUs, which);
7298 dumpTimer(proto, UidProto.Sensor.BACKGROUND, bgTimer, rawRealtimeUs, which);
7299
7300 proto.end(seToken);
7301 }
7302
7303 // State times (STATE_TIME_DATA)
7304 for (int ips = 0; ips < Uid.NUM_PROCESS_STATE; ++ips) {
7305 long durMs = roundUsToMs(u.getProcessStateTime(ips, rawRealtimeUs, which));
7306 if (durMs == 0) {
7307 continue;
7308 }
7309 final long stToken = proto.start(UidProto.STATES);
7310 proto.write(UidProto.StateTime.STATE, ips);
7311 proto.write(UidProto.StateTime.DURATION_MS, durMs);
7312 proto.end(stToken);
7313 }
7314
7315 // Syncs (SYNC_DATA)
7316 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
7317 for (int isy = syncs.size() - 1; isy >= 0; --isy) {
7318 final Timer timer = syncs.valueAt(isy);
7319 final Timer bgTimer = timer.getSubTimer();
7320 final long syToken = proto.start(UidProto.SYNCS);
7321
7322 proto.write(UidProto.Sync.NAME, syncs.keyAt(isy));
7323 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7324 dumpTimer(proto, UidProto.Sync.TOTAL, timer, rawRealtimeUs, which);
7325 dumpTimer(proto, UidProto.Sync.BACKGROUND, bgTimer, rawRealtimeUs, which);
7326
7327 proto.end(syToken);
7328 }
7329
7330 // User activity (USER_ACTIVITY_DATA)
7331 if (u.hasUserActivity()) {
7332 for (int i = 0; i < Uid.NUM_USER_ACTIVITY_TYPES; ++i) {
7333 int val = u.getUserActivityCount(i, which);
7334 if (val != 0) {
7335 final long uaToken = proto.start(UidProto.USER_ACTIVITY);
7336 proto.write(UidProto.UserActivity.NAME, i);
7337 proto.write(UidProto.UserActivity.COUNT, val);
7338 proto.end(uaToken);
7339 }
7340 }
7341 }
7342
7343 // Vibrator (VIBRATOR_DATA)
7344 dumpTimer(proto, UidProto.VIBRATOR, u.getVibratorOnTimer(), rawRealtimeUs, which);
7345
7346 // Video (VIDEO_DATA)
7347 dumpTimer(proto, UidProto.VIDEO, u.getVideoTurnedOnTimer(), rawRealtimeUs, which);
7348
7349 // Wakelocks (WAKELOCK_DATA)
7350 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
7351 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7352 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7353 final long wToken = proto.start(UidProto.WAKELOCKS);
7354 proto.write(UidProto.Wakelock.NAME, wakelocks.keyAt(iw));
7355 dumpTimer(proto, UidProto.Wakelock.FULL, wl.getWakeTime(WAKE_TYPE_FULL),
7356 rawRealtimeUs, which);
7357 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7358 if (pTimer != null) {
7359 dumpTimer(proto, UidProto.Wakelock.PARTIAL, pTimer, rawRealtimeUs, which);
7360 dumpTimer(proto, UidProto.Wakelock.BACKGROUND_PARTIAL, pTimer.getSubTimer(),
7361 rawRealtimeUs, which);
7362 }
7363 dumpTimer(proto, UidProto.Wakelock.WINDOW, wl.getWakeTime(WAKE_TYPE_WINDOW),
7364 rawRealtimeUs, which);
7365 proto.end(wToken);
7366 }
7367
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007368 // Wifi Multicast Wakelock (WIFI_MULTICAST_WAKELOCK_DATA)
7369 dumpTimer(proto, UidProto.WIFI_MULTICAST_WAKELOCK, u.getMulticastWakelockStats(),
7370 rawRealtimeUs, which);
7371
Kweku Adams103351f2017-10-16 14:39:34 -07007372 // Wakeup alarms (WAKEUP_ALARM_DATA)
7373 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7374 final Uid.Pkg ps = packageStats.valueAt(ipkg);
7375 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
7376 for (int iwa = alarms.size() - 1; iwa >= 0; --iwa) {
7377 final long waToken = proto.start(UidProto.WAKEUP_ALARM);
7378 proto.write(UidProto.WakeupAlarm.NAME, alarms.keyAt(iwa));
7379 proto.write(UidProto.WakeupAlarm.COUNT,
7380 alarms.valueAt(iwa).getCountLocked(which));
7381 proto.end(waToken);
7382 }
7383 }
7384
7385 // Wifi Controller (WIFI_CONTROLLER_DATA)
7386 dumpControllerActivityProto(proto, UidProto.WIFI_CONTROLLER,
7387 u.getWifiControllerActivity(), which);
7388
7389 // Wifi data (WIFI_DATA)
7390 final long wToken = proto.start(UidProto.WIFI);
7391 proto.write(UidProto.Wifi.FULL_WIFI_LOCK_DURATION_MS,
7392 roundUsToMs(u.getFullWifiLockTime(rawRealtimeUs, which)));
7393 dumpTimer(proto, UidProto.Wifi.APPORTIONED_SCAN, u.getWifiScanTimer(),
7394 rawRealtimeUs, which);
7395 proto.write(UidProto.Wifi.RUNNING_DURATION_MS,
7396 roundUsToMs(u.getWifiRunningTime(rawRealtimeUs, which)));
7397 dumpTimer(proto, UidProto.Wifi.BACKGROUND_SCAN, u.getWifiScanBackgroundTimer(),
7398 rawRealtimeUs, which);
7399 proto.end(wToken);
7400
7401 proto.end(uTkn);
7402 }
7403 }
7404
7405 private void dumpProtoSystemLocked(ProtoOutputStream proto, BatteryStatsHelper helper) {
Kweku Adams87b19ec2017-10-09 12:40:03 -07007406 final long sToken = proto.start(BatteryStatsProto.SYSTEM);
7407 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7408 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7409 final long rawRealtimeUs = rawRealtimeMs * 1000;
7410 final int which = STATS_SINCE_CHARGED;
7411
7412 // Battery data (BATTERY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007413 final long bToken = proto.start(SystemProto.BATTERY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007414 proto.write(SystemProto.Battery.START_CLOCK_TIME_MS, getStartClockTime());
7415 proto.write(SystemProto.Battery.START_COUNT, getStartCount());
7416 proto.write(SystemProto.Battery.TOTAL_REALTIME_MS,
7417 computeRealtime(rawRealtimeUs, which) / 1000);
7418 proto.write(SystemProto.Battery.TOTAL_UPTIME_MS,
7419 computeUptime(rawUptimeUs, which) / 1000);
7420 proto.write(SystemProto.Battery.BATTERY_REALTIME_MS,
7421 computeBatteryRealtime(rawRealtimeUs, which) / 1000);
7422 proto.write(SystemProto.Battery.BATTERY_UPTIME_MS,
7423 computeBatteryUptime(rawUptimeUs, which) / 1000);
7424 proto.write(SystemProto.Battery.SCREEN_OFF_REALTIME_MS,
7425 computeBatteryScreenOffRealtime(rawRealtimeUs, which) / 1000);
7426 proto.write(SystemProto.Battery.SCREEN_OFF_UPTIME_MS,
7427 computeBatteryScreenOffUptime(rawUptimeUs, which) / 1000);
7428 proto.write(SystemProto.Battery.SCREEN_DOZE_DURATION_MS,
7429 getScreenDozeTime(rawRealtimeUs, which) / 1000);
7430 proto.write(SystemProto.Battery.ESTIMATED_BATTERY_CAPACITY_MAH,
7431 getEstimatedBatteryCapacity());
7432 proto.write(SystemProto.Battery.MIN_LEARNED_BATTERY_CAPACITY_UAH,
7433 getMinLearnedBatteryCapacity());
7434 proto.write(SystemProto.Battery.MAX_LEARNED_BATTERY_CAPACITY_UAH,
7435 getMaxLearnedBatteryCapacity());
Kweku Adams103351f2017-10-16 14:39:34 -07007436 proto.end(bToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007437
7438 // Battery discharge (BATTERY_DISCHARGE_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007439 final long bdToken = proto.start(SystemProto.BATTERY_DISCHARGE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007440 proto.write(SystemProto.BatteryDischarge.LOWER_BOUND_SINCE_CHARGE,
7441 getLowDischargeAmountSinceCharge());
7442 proto.write(SystemProto.BatteryDischarge.UPPER_BOUND_SINCE_CHARGE,
7443 getHighDischargeAmountSinceCharge());
7444 proto.write(SystemProto.BatteryDischarge.SCREEN_ON_SINCE_CHARGE,
7445 getDischargeAmountScreenOnSinceCharge());
7446 proto.write(SystemProto.BatteryDischarge.SCREEN_OFF_SINCE_CHARGE,
7447 getDischargeAmountScreenOffSinceCharge());
7448 proto.write(SystemProto.BatteryDischarge.SCREEN_DOZE_SINCE_CHARGE,
7449 getDischargeAmountScreenDozeSinceCharge());
7450 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH,
7451 getUahDischarge(which) / 1000);
7452 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_OFF,
7453 getUahDischargeScreenOff(which) / 1000);
7454 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_DOZE,
7455 getUahDischargeScreenDoze(which) / 1000);
Mike Ma15313c92017-11-15 17:58:21 -08007456 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_LIGHT_DOZE,
7457 getUahDischargeLightDoze(which) / 1000);
7458 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_DEEP_DOZE,
7459 getUahDischargeDeepDoze(which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007460 proto.end(bdToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007461
7462 // Time remaining
7463 long timeRemainingUs = computeChargeTimeRemaining(rawRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -07007464 // These are part of a oneof, so we should only set one of them.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007465 if (timeRemainingUs >= 0) {
7466 // Charge time remaining (CHARGE_TIME_REMAIN_DATA)
7467 proto.write(SystemProto.CHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7468 } else {
7469 timeRemainingUs = computeBatteryTimeRemaining(rawRealtimeUs);
7470 // Discharge time remaining (DISCHARGE_TIME_REMAIN_DATA)
7471 if (timeRemainingUs >= 0) {
7472 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7473 } else {
7474 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, -1);
7475 }
7476 }
7477
7478 // Charge step (CHARGE_STEP_DATA)
7479 dumpDurationSteps(proto, SystemProto.CHARGE_STEP, getChargeLevelStepTracker());
7480
7481 // Phone data connection (DATA_CONNECTION_TIME_DATA and DATA_CONNECTION_COUNT_DATA)
7482 for (int i = 0; i < NUM_DATA_CONNECTION_TYPES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007483 final long pdcToken = proto.start(SystemProto.DATA_CONNECTION);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007484 proto.write(SystemProto.DataConnection.NAME, i);
7485 dumpTimer(proto, SystemProto.DataConnection.TOTAL, getPhoneDataConnectionTimer(i),
7486 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007487 proto.end(pdcToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007488 }
7489
7490 // Discharge step (DISCHARGE_STEP_DATA)
7491 dumpDurationSteps(proto, SystemProto.DISCHARGE_STEP, getDischargeLevelStepTracker());
7492
7493 // CPU frequencies (GLOBAL_CPU_FREQ_DATA)
7494 final long[] cpuFreqs = getCpuFreqs();
7495 if (cpuFreqs != null) {
7496 for (long i : cpuFreqs) {
7497 proto.write(SystemProto.CPU_FREQUENCY, i);
7498 }
7499 }
7500
7501 // Bluetooth controller (GLOBAL_BLUETOOTH_CONTROLLER_DATA)
7502 dumpControllerActivityProto(proto, SystemProto.GLOBAL_BLUETOOTH_CONTROLLER,
7503 getBluetoothControllerActivity(), which);
7504
7505 // Modem controller (GLOBAL_MODEM_CONTROLLER_DATA)
7506 dumpControllerActivityProto(proto, SystemProto.GLOBAL_MODEM_CONTROLLER,
7507 getModemControllerActivity(), which);
7508
7509 // Global network data (GLOBAL_NETWORK_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007510 final long gnToken = proto.start(SystemProto.GLOBAL_NETWORK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007511 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_RX,
7512 getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7513 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_TX,
7514 getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7515 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_RX,
7516 getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7517 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_TX,
7518 getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7519 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_RX,
7520 getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7521 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_TX,
7522 getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7523 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_RX,
7524 getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7525 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_TX,
7526 getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7527 proto.write(SystemProto.GlobalNetwork.BT_BYTES_RX,
7528 getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7529 proto.write(SystemProto.GlobalNetwork.BT_BYTES_TX,
7530 getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
Kweku Adams103351f2017-10-16 14:39:34 -07007531 proto.end(gnToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007532
7533 // Wifi controller (GLOBAL_WIFI_CONTROLLER_DATA)
7534 dumpControllerActivityProto(proto, SystemProto.GLOBAL_WIFI_CONTROLLER,
7535 getWifiControllerActivity(), which);
7536
7537
7538 // Global wifi (GLOBAL_WIFI_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007539 final long gwToken = proto.start(SystemProto.GLOBAL_WIFI);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007540 proto.write(SystemProto.GlobalWifi.ON_DURATION_MS,
7541 getWifiOnTime(rawRealtimeUs, which) / 1000);
7542 proto.write(SystemProto.GlobalWifi.RUNNING_DURATION_MS,
7543 getGlobalWifiRunningTime(rawRealtimeUs, which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007544 proto.end(gwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007545
7546 // Kernel wakelock (KERNEL_WAKELOCK_DATA)
7547 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
7548 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007549 final long kwToken = proto.start(SystemProto.KERNEL_WAKELOCK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007550 proto.write(SystemProto.KernelWakelock.NAME, ent.getKey());
7551 dumpTimer(proto, SystemProto.KernelWakelock.TOTAL, ent.getValue(),
7552 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007553 proto.end(kwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007554 }
7555
7556 // Misc (MISC_DATA)
7557 // Calculate wakelock times across all uids.
7558 long fullWakeLockTimeTotalUs = 0;
7559 long partialWakeLockTimeTotalUs = 0;
7560
7561 final SparseArray<? extends Uid> uidStats = getUidStats();
7562 for (int iu = 0; iu < uidStats.size(); iu++) {
7563 final Uid u = uidStats.valueAt(iu);
7564
7565 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks =
7566 u.getWakelockStats();
7567 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7568 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7569
7570 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
7571 if (fullWakeTimer != null) {
7572 fullWakeLockTimeTotalUs += fullWakeTimer.getTotalTimeLocked(rawRealtimeUs,
7573 which);
7574 }
7575
7576 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7577 if (partialWakeTimer != null) {
7578 partialWakeLockTimeTotalUs += partialWakeTimer.getTotalTimeLocked(
7579 rawRealtimeUs, which);
7580 }
7581 }
7582 }
Kweku Adams103351f2017-10-16 14:39:34 -07007583 final long mToken = proto.start(SystemProto.MISC);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007584 proto.write(SystemProto.Misc.SCREEN_ON_DURATION_MS,
7585 getScreenOnTime(rawRealtimeUs, which) / 1000);
7586 proto.write(SystemProto.Misc.PHONE_ON_DURATION_MS,
7587 getPhoneOnTime(rawRealtimeUs, which) / 1000);
7588 proto.write(SystemProto.Misc.FULL_WAKELOCK_TOTAL_DURATION_MS,
7589 fullWakeLockTimeTotalUs / 1000);
7590 proto.write(SystemProto.Misc.PARTIAL_WAKELOCK_TOTAL_DURATION_MS,
7591 partialWakeLockTimeTotalUs / 1000);
7592 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_DURATION_MS,
7593 getMobileRadioActiveTime(rawRealtimeUs, which) / 1000);
7594 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_ADJUSTED_TIME_MS,
7595 getMobileRadioActiveAdjustedTime(which) / 1000);
7596 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_COUNT,
7597 getMobileRadioActiveCount(which));
7598 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_UNKNOWN_DURATION_MS,
7599 getMobileRadioActiveUnknownTime(which) / 1000);
7600 proto.write(SystemProto.Misc.INTERACTIVE_DURATION_MS,
7601 getInteractiveTime(rawRealtimeUs, which) / 1000);
7602 proto.write(SystemProto.Misc.BATTERY_SAVER_MODE_ENABLED_DURATION_MS,
7603 getPowerSaveModeEnabledTime(rawRealtimeUs, which) / 1000);
7604 proto.write(SystemProto.Misc.NUM_CONNECTIVITY_CHANGES,
7605 getNumConnectivityChange(which));
7606 proto.write(SystemProto.Misc.DEEP_DOZE_ENABLED_DURATION_MS,
7607 getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7608 proto.write(SystemProto.Misc.DEEP_DOZE_COUNT,
7609 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
7610 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_DURATION_MS,
7611 getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7612 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_COUNT,
7613 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
7614 proto.write(SystemProto.Misc.LONGEST_DEEP_DOZE_DURATION_MS,
7615 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
7616 proto.write(SystemProto.Misc.LIGHT_DOZE_ENABLED_DURATION_MS,
7617 getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7618 proto.write(SystemProto.Misc.LIGHT_DOZE_COUNT,
7619 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
7620 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_DURATION_MS,
7621 getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7622 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_COUNT,
7623 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
7624 proto.write(SystemProto.Misc.LONGEST_LIGHT_DOZE_DURATION_MS,
7625 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
Kweku Adams103351f2017-10-16 14:39:34 -07007626 proto.end(mToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007627
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007628 // Wifi multicast wakelock total stats (WIFI_MULTICAST_WAKELOCK_TOTAL_DATA)
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08007629 final long multicastWakeLockTimeTotalUs =
7630 getWifiMulticastWakelockTime(rawRealtimeUs, which);
7631 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007632 final long wmctToken = proto.start(SystemProto.WIFI_MULTICAST_WAKELOCK_TOTAL);
7633 proto.write(SystemProto.WifiMulticastWakelockTotal.DURATION_MS,
7634 multicastWakeLockTimeTotalUs / 1000);
7635 proto.write(SystemProto.WifiMulticastWakelockTotal.COUNT,
7636 multicastWakeLockCountTotal);
7637 proto.end(wmctToken);
7638
Kweku Adams87b19ec2017-10-09 12:40:03 -07007639 // Power use item (POWER_USE_ITEM_DATA)
7640 final List<BatterySipper> sippers = helper.getUsageList();
7641 if (sippers != null) {
7642 for (int i = 0; i < sippers.size(); ++i) {
7643 final BatterySipper bs = sippers.get(i);
7644 int n = SystemProto.PowerUseItem.UNKNOWN_SIPPER;
7645 int uid = 0;
7646 switch (bs.drainType) {
7647 case IDLE:
7648 n = SystemProto.PowerUseItem.IDLE;
7649 break;
7650 case CELL:
7651 n = SystemProto.PowerUseItem.CELL;
7652 break;
7653 case PHONE:
7654 n = SystemProto.PowerUseItem.PHONE;
7655 break;
7656 case WIFI:
7657 n = SystemProto.PowerUseItem.WIFI;
7658 break;
7659 case BLUETOOTH:
7660 n = SystemProto.PowerUseItem.BLUETOOTH;
7661 break;
7662 case SCREEN:
7663 n = SystemProto.PowerUseItem.SCREEN;
7664 break;
7665 case FLASHLIGHT:
7666 n = SystemProto.PowerUseItem.FLASHLIGHT;
7667 break;
7668 case APP:
Kweku Adams103351f2017-10-16 14:39:34 -07007669 // dumpProtoAppsLocked will handle this.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007670 continue;
7671 case USER:
7672 n = SystemProto.PowerUseItem.USER;
7673 uid = UserHandle.getUid(bs.userId, 0);
7674 break;
7675 case UNACCOUNTED:
7676 n = SystemProto.PowerUseItem.UNACCOUNTED;
7677 break;
7678 case OVERCOUNTED:
7679 n = SystemProto.PowerUseItem.OVERCOUNTED;
7680 break;
7681 case CAMERA:
7682 n = SystemProto.PowerUseItem.CAMERA;
7683 break;
7684 case MEMORY:
7685 n = SystemProto.PowerUseItem.MEMORY;
7686 break;
7687 }
Kweku Adams103351f2017-10-16 14:39:34 -07007688 final long puiToken = proto.start(SystemProto.POWER_USE_ITEM);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007689 proto.write(SystemProto.PowerUseItem.NAME, n);
7690 proto.write(SystemProto.PowerUseItem.UID, uid);
7691 proto.write(SystemProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7692 proto.write(SystemProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7693 proto.write(SystemProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7694 proto.write(SystemProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7695 bs.proportionalSmearMah);
Kweku Adams103351f2017-10-16 14:39:34 -07007696 proto.end(puiToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007697 }
7698 }
7699
7700 // Power use summary (POWER_USE_SUMMARY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007701 final long pusToken = proto.start(SystemProto.POWER_USE_SUMMARY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007702 proto.write(SystemProto.PowerUseSummary.BATTERY_CAPACITY_MAH,
7703 helper.getPowerProfile().getBatteryCapacity());
7704 proto.write(SystemProto.PowerUseSummary.COMPUTED_POWER_MAH, helper.getComputedPower());
7705 proto.write(SystemProto.PowerUseSummary.MIN_DRAINED_POWER_MAH, helper.getMinDrainedPower());
7706 proto.write(SystemProto.PowerUseSummary.MAX_DRAINED_POWER_MAH, helper.getMaxDrainedPower());
Kweku Adams103351f2017-10-16 14:39:34 -07007707 proto.end(pusToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007708
7709 // RPM stats (RESOURCE_POWER_MANAGER_DATA)
7710 final Map<String, ? extends Timer> rpmStats = getRpmStats();
7711 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
7712 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007713 final long rpmToken = proto.start(SystemProto.RESOURCE_POWER_MANAGER);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007714 proto.write(SystemProto.ResourcePowerManager.NAME, ent.getKey());
7715 dumpTimer(proto, SystemProto.ResourcePowerManager.TOTAL,
7716 ent.getValue(), rawRealtimeUs, which);
7717 dumpTimer(proto, SystemProto.ResourcePowerManager.SCREEN_OFF,
7718 screenOffRpmStats.get(ent.getKey()), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007719 proto.end(rpmToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007720 }
7721
7722 // Screen brightness (SCREEN_BRIGHTNESS_DATA)
7723 for (int i = 0; i < NUM_SCREEN_BRIGHTNESS_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007724 final long sbToken = proto.start(SystemProto.SCREEN_BRIGHTNESS);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007725 proto.write(SystemProto.ScreenBrightness.NAME, i);
7726 dumpTimer(proto, SystemProto.ScreenBrightness.TOTAL, getScreenBrightnessTimer(i),
7727 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007728 proto.end(sbToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007729 }
7730
7731 // Signal scanning time (SIGNAL_SCANNING_TIME_DATA)
7732 dumpTimer(proto, SystemProto.SIGNAL_SCANNING, getPhoneSignalScanningTimer(), rawRealtimeUs,
7733 which);
7734
7735 // Phone signal strength (SIGNAL_STRENGTH_TIME_DATA and SIGNAL_STRENGTH_COUNT_DATA)
7736 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007737 final long pssToken = proto.start(SystemProto.PHONE_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007738 proto.write(SystemProto.PhoneSignalStrength.NAME, i);
7739 dumpTimer(proto, SystemProto.PhoneSignalStrength.TOTAL, getPhoneSignalStrengthTimer(i),
7740 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007741 proto.end(pssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007742 }
7743
7744 // Wakeup reasons (WAKEUP_REASON_DATA)
7745 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
7746 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007747 final long wrToken = proto.start(SystemProto.WAKEUP_REASON);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007748 proto.write(SystemProto.WakeupReason.NAME, ent.getKey());
7749 dumpTimer(proto, SystemProto.WakeupReason.TOTAL, ent.getValue(), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007750 proto.end(wrToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007751 }
7752
7753 // Wifi signal strength (WIFI_SIGNAL_STRENGTH_TIME_DATA and WIFI_SIGNAL_STRENGTH_COUNT_DATA)
7754 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007755 final long wssToken = proto.start(SystemProto.WIFI_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007756 proto.write(SystemProto.WifiSignalStrength.NAME, i);
7757 dumpTimer(proto, SystemProto.WifiSignalStrength.TOTAL, getWifiSignalStrengthTimer(i),
7758 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007759 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007760 }
7761
7762 // Wifi state (WIFI_STATE_TIME_DATA and WIFI_STATE_COUNT_DATA)
7763 for (int i = 0; i < NUM_WIFI_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007764 final long wsToken = proto.start(SystemProto.WIFI_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007765 proto.write(SystemProto.WifiState.NAME, i);
7766 dumpTimer(proto, SystemProto.WifiState.TOTAL, getWifiStateTimer(i),
7767 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007768 proto.end(wsToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007769 }
7770
7771 // Wifi supplicant state (WIFI_SUPPL_STATE_TIME_DATA and WIFI_SUPPL_STATE_COUNT_DATA)
7772 for (int i = 0; i < NUM_WIFI_SUPPL_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007773 final long wssToken = proto.start(SystemProto.WIFI_SUPPLICANT_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007774 proto.write(SystemProto.WifiSupplicantState.NAME, i);
7775 dumpTimer(proto, SystemProto.WifiSupplicantState.TOTAL, getWifiSupplStateTimer(i),
7776 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007777 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007778 }
7779
7780 proto.end(sToken);
7781 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007782}