blob: 5d7cf1e3899fc60557f59a74728ab1ade3827830 [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;
Bookatz8bdae8d2018-01-16 11:24:30 -080023import android.server.ServerProtoEnums;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070024import android.service.batterystats.BatteryStatsServiceDumpProto;
Wink Saville52840902011-02-18 12:40:47 -080025import android.telephony.SignalStrength;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080026import android.text.format.DateFormat;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070027import android.util.ArrayMap;
James Carr2dd7e5e2016-07-20 18:48:39 -070028import android.util.LongSparseArray;
Dianne Hackborn9cfba352016-03-24 17:31:28 -070029import android.util.MutableBoolean;
30import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.util.Printer;
32import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070033import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070034import android.util.TimeUtils;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070035import android.util.proto.ProtoOutputStream;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070036import android.view.Display;
Amith Yamasaniab9ad192016-12-06 12:46:59 -080037
Sudheer Shankab2f83c12017-11-13 19:25:01 -080038import com.android.internal.annotations.VisibleForTesting;
Siddharth Ray78ccaf52017-12-23 16:16:21 -080039import com.android.internal.location.gnssmetrics.GnssMetrics;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080040import com.android.internal.os.BatterySipper;
41import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
Kweku Adams2f73ecd2017-09-27 16:59:19 -070043import java.io.FileDescriptor;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070044import java.io.PrintWriter;
45import java.util.ArrayList;
46import java.util.Collections;
47import java.util.Comparator;
48import java.util.Formatter;
49import java.util.HashMap;
50import java.util.List;
51import java.util.Map;
52
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053/**
54 * A class providing access to battery usage statistics, including information on
55 * wakelocks, processes, packages, and services. All times are represented in microseconds
56 * except where indicated otherwise.
57 * @hide
58 */
59public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070060 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061
62 private static final boolean LOCAL_LOGV = false;
Bookatz82b341172017-09-07 19:06:08 -070063 /** Fetching RPM stats is too slow to do each time screen changes, so disable it. */
64 protected static final boolean SCREEN_OFF_RPM_STATS_ENABLED = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070065
66 /** @hide */
67 public static final String SERVICE_NAME = "batterystats";
68
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 /**
70 * A constant indicating a partial wake lock timer.
71 */
72 public static final int WAKE_TYPE_PARTIAL = 0;
73
74 /**
75 * A constant indicating a full wake lock timer.
76 */
77 public static final int WAKE_TYPE_FULL = 1;
78
79 /**
80 * A constant indicating a window wake lock timer.
81 */
82 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 /**
85 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 */
87 public static final int SENSOR = 3;
Mike Mac2f518a2017-09-19 16:06:03 -070088
The Android Open Source Project10592532009-03-18 17:39:46 -070089 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070090 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070091 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070092 public static final int WIFI_RUNNING = 4;
Mike Mac2f518a2017-09-19 16:06:03 -070093
Dianne Hackborn617f8772009-03-31 15:04:46 -070094 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070095 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070096 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070097 public static final int FULL_WIFI_LOCK = 5;
Mike Mac2f518a2017-09-19 16:06:03 -070098
The Android Open Source Project10592532009-03-18 17:39:46 -070099 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -0700100 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -0700101 */
Nick Pelly6ccaa542012-06-15 15:22:47 -0700102 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
Dianne Hackborn62793e42015-03-09 11:15:41 -0700104 /**
105 * A constant indicating a wifi multicast timer
106 */
107 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700110 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700111 */
112 public static final int VIDEO_TURNED_ON = 8;
113
114 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800115 * A constant indicating a vibrator on timer
116 */
117 public static final int VIBRATOR_ON = 9;
118
119 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700120 * A constant indicating a foreground activity timer
121 */
122 public static final int FOREGROUND_ACTIVITY = 10;
123
124 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700125 * A constant indicating a wifi batched scan is active
126 */
127 public static final int WIFI_BATCHED_SCAN = 11;
128
129 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700130 * A constant indicating a process state timer
131 */
132 public static final int PROCESS_STATE = 12;
133
134 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700135 * A constant indicating a sync timer
136 */
137 public static final int SYNC = 13;
138
139 /**
140 * A constant indicating a job timer
141 */
142 public static final int JOB = 14;
143
144 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800145 * A constant indicating an audio turn on timer
146 */
147 public static final int AUDIO_TURNED_ON = 15;
148
149 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700150 * A constant indicating a flashlight turn on timer
151 */
152 public static final int FLASHLIGHT_TURNED_ON = 16;
153
154 /**
155 * A constant indicating a camera turn on timer
156 */
157 public static final int CAMERA_TURNED_ON = 17;
158
159 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700160 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700161 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700162 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700163
164 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800165 * A constant indicating a bluetooth scan timer.
166 */
167 public static final int BLUETOOTH_SCAN_ON = 19;
168
169 /**
Bookatzc8c44962017-05-11 12:12:54 -0700170 * A constant indicating an aggregated partial wake lock timer.
171 */
172 public static final int AGGREGATED_WAKE_TYPE_PARTIAL = 20;
173
174 /**
Bookatzb1f04f32017-05-19 13:57:32 -0700175 * A constant indicating a bluetooth scan timer for unoptimized scans.
176 */
177 public static final int BLUETOOTH_UNOPTIMIZED_SCAN_ON = 21;
178
179 /**
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700180 * A constant indicating a foreground service timer
181 */
182 public static final int FOREGROUND_SERVICE = 22;
183
184 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -0800185 * A constant indicating an aggregate wifi multicast timer
186 */
187 public static final int WIFI_AGGREGATE_MULTICAST_ENABLED = 23;
188
189 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 * Include all of the data in the stats, including previously saved data.
191 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700192 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193
194 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 * Include only the current run in the stats.
196 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700197 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198
199 /**
200 * Include only the run since the last time the device was unplugged in the stats.
201 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700202 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700203
204 // NOTE: Update this list if you add/change any stats above.
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700205 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700206 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700207 private static final String[] STAT_NAMES = { "l", "c", "u" };
208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700210 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700211 *
212 * New in version 19:
213 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800214 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700215 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Bookatz506a8182017-05-01 14:18:42 -0700216 * New in version 21:
217 * - Actual (not just apportioned) Wakelock time is also recorded.
Bookatzc8c44962017-05-11 12:12:54 -0700218 * - Aggregated partial wakelock time (per uid, instead of per wakelock) is recorded.
Bookatzb1f04f32017-05-19 13:57:32 -0700219 * - BLE scan result count
220 * - CPU frequency time per uid
221 * New in version 22:
222 * - BLE scan result background count, BLE unoptimized scan time
Bookatz98d4d5c2017-08-01 19:07:54 -0700223 * - Background partial wakelock time & count
224 * New in version 23:
225 * - Logging smeared power model values
226 * New in version 24:
227 * - Fixed bugs in background timers and BLE scan time
228 * New in version 25:
229 * - Package wakeup alarms are now on screen-off timebase
Bookatz50df7112017-08-04 14:53:26 -0700230 * New in version 26:
Bookatz82b341172017-09-07 19:06:08 -0700231 * - Resource power manager (rpm) states [but screenOffRpm is disabled from working properly]
Mike Mac2f518a2017-09-19 16:06:03 -0700232 * New in version 27:
233 * - Always On Display (screen doze mode) time and power
Mike Ma15313c92017-11-15 17:58:21 -0800234 * New in version 28:
235 * - Light/Deep Doze power
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700236 * - WiFi Multicast Wakelock statistics (count & duration)
Kweku Adamsa8943cb2017-12-22 13:21:06 -0800237 * New in version 29:
238 * - Process states re-ordered. TOP_SLEEPING now below BACKGROUND. HEAVY_WEIGHT introduced.
239 * - CPU times per UID process state
zhouwenjie46712bc2018-01-11 15:21:27 -0800240 * New in version 30:
241 * - Uid.PROCESS_STATE_FOREGROUND_SERVICE only tracks
242 * ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE.
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700243 */
zhouwenjie46712bc2018-01-11 15:21:27 -0800244 static final int CHECKIN_VERSION = 30;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700245
246 /**
247 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700249 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700250
Evan Millar22ac0432009-03-31 11:33:18 -0700251 private static final long BYTES_PER_KB = 1024;
252 private static final long BYTES_PER_MB = 1048576; // 1024^2
253 private static final long BYTES_PER_GB = 1073741824; //1024^3
Bookatz506a8182017-05-01 14:18:42 -0700254
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700255 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800256 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700257 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700259 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700260 private static final String CPU_DATA = "cpu";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700261 private static final String GLOBAL_CPU_FREQ_DATA = "gcf";
262 private static final String CPU_TIMES_AT_FREQ_DATA = "ctf";
Bookatz50df7112017-08-04 14:53:26 -0700263 // rpm line is:
264 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "rpm", state/voter name, total time, total count,
265 // screen-off time, screen-off count
266 private static final String RESOURCE_POWER_MANAGER_DATA = "rpm";
Evan Millare84de8d2009-04-02 22:16:12 -0700267 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800268 private static final String VIBRATOR_DATA = "vib";
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700269 private static final String FOREGROUND_ACTIVITY_DATA = "fg";
270 // fgs line is:
271 // BATTERY_STATS_CHECKIN_VERSION, uid, category, "fgs",
272 // foreground service time, count
273 private static final String FOREGROUND_SERVICE_DATA = "fgs";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700274 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700275 // wl line is:
276 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
Bookatz5b5ec322017-05-26 09:40:38 -0700277 // full totalTime, 'f', count, current duration, max duration, total duration,
278 // partial totalTime, 'p', count, current duration, max duration, total duration,
279 // bg partial totalTime, 'bp', count, current duration, max duration, total duration,
280 // window totalTime, 'w', count, current duration, max duration, total duration
Bookatz506a8182017-05-01 14:18:42 -0700281 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700282 private static final String WAKELOCK_DATA = "wl";
Bookatzc8c44962017-05-11 12:12:54 -0700283 // awl line is:
284 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "awl",
285 // cumulative partial wakelock duration, cumulative background partial wakelock duration
286 private static final String AGGREGATED_WAKELOCK_DATA = "awl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700287 private static final String SYNC_DATA = "sy";
288 private static final String JOB_DATA = "jb";
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700289 private static final String JOB_COMPLETION_DATA = "jbc";
Evan Millarc64edde2009-04-18 12:26:32 -0700290 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700291 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700292 private static final String NETWORK_DATA = "nt";
293 private static final String USER_ACTIVITY_DATA = "ua";
294 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800295 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700296 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700297 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700298 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800299 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
300 private static final String WIFI_CONTROLLER_DATA = "wfcd";
301 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
302 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700303 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700304 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800305 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800306 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
307 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800308 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700309 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700310 private static final String SCREEN_BRIGHTNESS_DATA = "br";
311 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700312 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700313 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
314 private static final String DATA_CONNECTION_TIME_DATA = "dct";
315 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800316 private static final String WIFI_STATE_TIME_DATA = "wst";
317 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700318 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
319 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
320 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
321 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800322 private static final String POWER_USE_SUMMARY_DATA = "pws";
323 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700324 private static final String DISCHARGE_STEP_DATA = "dsd";
325 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700326 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
327 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700328 private static final String FLASHLIGHT_DATA = "fla";
329 private static final String CAMERA_DATA = "cam";
330 private static final String VIDEO_DATA = "vid";
331 private static final String AUDIO_DATA = "aud";
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700332 private static final String WIFI_MULTICAST_TOTAL_DATA = "wmct";
333 private static final String WIFI_MULTICAST_DATA = "wmc";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334
Adam Lesinski010bf372016-04-11 12:18:18 -0700335 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
336
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700337 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 private final Formatter mFormatter = new Formatter(mFormatBuilder);
339
Siddharth Rayb50a6842017-12-14 15:15:28 -0800340 private static final String CELLULAR_CONTROLLER_NAME = "Cellular";
341 private static final String WIFI_CONTROLLER_NAME = "WiFi";
342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700344 * Indicates times spent by the uid at each cpu frequency in all process states.
345 *
346 * Other types might include times spent in foreground, background etc.
347 */
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800348 @VisibleForTesting
349 public static final String UID_TIMES_TYPE_ALL = "A";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700350
351 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700352 * State for keeping track of counting information.
353 */
354 public static abstract class Counter {
355
356 /**
357 * Returns the count associated with this Counter for the
358 * selected type of statistics.
359 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700360 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700361 */
Evan Millarc64edde2009-04-18 12:26:32 -0700362 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700363
364 /**
365 * Temporary for debugging.
366 */
367 public abstract void logState(Printer pw, String prefix);
368 }
369
370 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700371 * State for keeping track of long counting information.
372 */
373 public static abstract class LongCounter {
374
375 /**
376 * Returns the count associated with this Counter for the
377 * selected type of statistics.
378 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700379 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700380 */
381 public abstract long getCountLocked(int which);
382
383 /**
384 * Temporary for debugging.
385 */
386 public abstract void logState(Printer pw, String prefix);
387 }
388
389 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700390 * State for keeping track of array of long counting information.
391 */
392 public static abstract class LongCounterArray {
393 /**
394 * Returns the counts associated with this Counter for the
395 * selected type of statistics.
396 *
397 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
398 */
399 public abstract long[] getCountsLocked(int which);
400
401 /**
402 * Temporary for debugging.
403 */
404 public abstract void logState(Printer pw, String prefix);
405 }
406
407 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800408 * Container class that aggregates counters for transmit, receive, and idle state of a
409 * radio controller.
410 */
411 public static abstract class ControllerActivityCounter {
412 /**
413 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
414 * idle state.
415 */
416 public abstract LongCounter getIdleTimeCounter();
417
418 /**
419 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
Siddharth Rayb50a6842017-12-14 15:15:28 -0800420 * scan state.
421 */
422 public abstract LongCounter getScanTimeCounter();
423
424
425 /**
426 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800427 * receive state.
428 */
429 public abstract LongCounter getRxTimeCounter();
430
431 /**
432 * An array of {@link LongCounter}, representing various transmit levels, where each level
433 * may draw a different amount of power. The levels themselves are controller-specific.
434 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
435 * various transmit level states.
436 */
437 public abstract LongCounter[] getTxTimeCounters();
438
439 /**
440 * @return a non-null {@link LongCounter} representing the power consumed by the controller
441 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
442 * yield a value of 0 if the device doesn't support power calculations.
443 */
444 public abstract LongCounter getPowerCounter();
445 }
446
447 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 * State for keeping track of timing information.
449 */
450 public static abstract class Timer {
451
452 /**
453 * Returns the count associated with this Timer for the
454 * selected type of statistics.
455 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700456 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 */
Evan Millarc64edde2009-04-18 12:26:32 -0700458 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459
460 /**
461 * Returns the total time in microseconds associated with this Timer for the
462 * selected type of statistics.
463 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800464 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700465 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 * @return a time in microseconds
467 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800468 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700471 * Returns the total time in microseconds associated with this Timer since the
472 * 'mark' was last set.
473 *
474 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
475 * @return a time in microseconds
476 */
477 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
478
479 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700480 * Returns the max duration if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700481 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700482 */
483 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
484 return -1;
485 }
486
487 /**
488 * Returns the current time the timer has been active, if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700489 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700490 */
491 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
492 return -1;
493 }
494
495 /**
Kweku Adams103351f2017-10-16 14:39:34 -0700496 * Returns the total time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800497 *
498 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
499 * been on since reset.
500 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
501 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
502 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
503 * the actual total time.
Kweku Adams103351f2017-10-16 14:39:34 -0700504 * Not all Timer subclasses track the max, total, and current durations.
Bookatz867c0d72017-03-07 18:23:42 -0800505 */
506 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
507 return -1;
508 }
509
510 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700511 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
512 * used, for example, for tracking background usage. Secondary timers are never pooled.
513 *
514 * Not all Timer subclasses have a secondary timer; those that don't return null.
515 */
516 public Timer getSubTimer() {
517 return null;
518 }
519
520 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700521 * Returns whether the timer is currently running. Some types of timers
522 * (e.g. BatchTimers) don't know whether the event is currently active,
523 * and report false.
524 */
525 public boolean isRunningLocked() {
526 return false;
527 }
528
529 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 * Temporary for debugging.
531 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700532 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 }
534
535 /**
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800536 * Maps the ActivityManager procstate into corresponding BatteryStats procstate.
537 */
538 public static int mapToInternalProcessState(int procState) {
539 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
540 return ActivityManager.PROCESS_STATE_NONEXISTENT;
541 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
542 return Uid.PROCESS_STATE_TOP;
Dianne Hackborn10fc4fd2017-12-19 17:23:13 -0800543 } else if (procState == ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
544 // State when app has put itself in the foreground.
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800545 return Uid.PROCESS_STATE_FOREGROUND_SERVICE;
546 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
547 // Persistent and other foreground states go here.
548 return Uid.PROCESS_STATE_FOREGROUND;
549 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
550 return Uid.PROCESS_STATE_BACKGROUND;
551 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
552 return Uid.PROCESS_STATE_TOP_SLEEPING;
553 } else if (procState <= ActivityManager.PROCESS_STATE_HEAVY_WEIGHT) {
554 return Uid.PROCESS_STATE_HEAVY_WEIGHT;
555 } else {
556 return Uid.PROCESS_STATE_CACHED;
557 }
558 }
559
560 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 * The statistics associated with a particular uid.
562 */
563 public static abstract class Uid {
564
565 /**
566 * Returns a mapping containing wakelock statistics.
567 *
568 * @return a Map from Strings to Uid.Wakelock objects.
569 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700570 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571
572 /**
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700573 * Returns the WiFi Multicast Wakelock statistics.
574 *
575 * @return a Timer Object for the per uid Multicast statistics.
576 */
577 public abstract Timer getMulticastWakelockStats();
578
579 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700580 * Returns a mapping containing sync statistics.
581 *
582 * @return a Map from Strings to Timer objects.
583 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700584 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700585
586 /**
587 * Returns a mapping containing scheduled job statistics.
588 *
589 * @return a Map from Strings to Timer objects.
590 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700591 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700592
593 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700594 * Returns statistics about how jobs have completed.
595 *
596 * @return A Map of String job names to completion type -> count mapping.
597 */
598 public abstract ArrayMap<String, SparseIntArray> getJobCompletionStats();
599
600 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 * The statistics associated with a particular wake lock.
602 */
603 public static abstract class Wakelock {
604 public abstract Timer getWakeTime(int type);
605 }
606
607 /**
Bookatzc8c44962017-05-11 12:12:54 -0700608 * The cumulative time the uid spent holding any partial wakelocks. This will generally
609 * differ from summing over the Wakelocks in getWakelockStats since the latter may have
610 * wakelocks that overlap in time (and therefore over-counts).
611 */
612 public abstract Timer getAggregatedPartialWakelockTimer();
613
614 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 * Returns a mapping containing sensor statistics.
616 *
617 * @return a Map from Integer sensor ids to Uid.Sensor objects.
618 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700619 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620
621 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700622 * Returns a mapping containing active process data.
623 */
624 public abstract SparseArray<? extends Pid> getPidStats();
Bookatzc8c44962017-05-11 12:12:54 -0700625
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700626 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 * Returns a mapping containing process statistics.
628 *
629 * @return a Map from Strings to Uid.Proc objects.
630 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700631 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632
633 /**
634 * Returns a mapping containing package statistics.
635 *
636 * @return a Map from Strings to Uid.Pkg objects.
637 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700638 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700639
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800640 public abstract ControllerActivityCounter getWifiControllerActivity();
641 public abstract ControllerActivityCounter getBluetoothControllerActivity();
642 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800643
644 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 * {@hide}
646 */
647 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700648
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800649 public abstract void noteWifiRunningLocked(long elapsedRealtime);
650 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
651 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
652 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
653 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
654 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
655 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
656 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
657 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
658 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800659 public abstract void noteActivityResumedLocked(long elapsedRealtime);
660 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800661 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
662 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
663 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700664 public abstract int getWifiScanCount(int which);
Kweku Adams103351f2017-10-16 14:39:34 -0700665 /**
666 * Returns the timer keeping track of wifi scans.
667 */
668 public abstract Timer getWifiScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800669 public abstract int getWifiScanBackgroundCount(int which);
670 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
671 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -0700672 /**
673 * Returns the timer keeping track of background wifi scans.
674 */
675 public abstract Timer getWifiScanBackgroundTimer();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800676 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700677 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800678 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700679 public abstract Timer getAudioTurnedOnTimer();
680 public abstract Timer getVideoTurnedOnTimer();
681 public abstract Timer getFlashlightTurnedOnTimer();
682 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700683 public abstract Timer getForegroundActivityTimer();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700684
685 /**
686 * Returns the timer keeping track of Foreground Service time
687 */
688 public abstract Timer getForegroundServiceTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800689 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800690 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatzb1f04f32017-05-19 13:57:32 -0700691 public abstract Timer getBluetoothUnoptimizedScanTimer();
692 public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700693 public abstract Counter getBluetoothScanResultCounter();
Bookatzb1f04f32017-05-19 13:57:32 -0700694 public abstract Counter getBluetoothScanResultBgCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700695
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700696 public abstract long[] getCpuFreqTimes(int which);
697 public abstract long[] getScreenOffCpuFreqTimes(int which);
Mike Ma3d422c32017-10-25 11:08:57 -0700698 /**
699 * Returns cpu active time of an uid.
700 */
701 public abstract long getCpuActiveTime();
702 /**
703 * Returns cpu times of an uid on each cluster
704 */
705 public abstract long[] getCpuClusterTimes();
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700706
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800707 /**
708 * Returns cpu times of an uid at a particular process state.
709 */
710 public abstract long[] getCpuFreqTimes(int which, int procState);
711 /**
712 * Returns cpu times of an uid while the screen if off at a particular process state.
713 */
714 public abstract long[] getScreenOffCpuFreqTimes(int which, int procState);
715
Dianne Hackborna0200e32016-03-30 18:01:41 -0700716 // Note: the following times are disjoint. They can be added together to find the
717 // total time a uid has had any processes running at all.
718
719 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800720 * Time this uid has any processes in the top state.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700721 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800722 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700723 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800724 * Time this uid has any process with a started foreground service, but
Dianne Hackborna0200e32016-03-30 18:01:41 -0700725 * none in the "top" state.
726 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800727 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700728 /**
Dianne Hackborna0200e32016-03-30 18:01:41 -0700729 * Time this uid has any process in an active foreground state, but none in the
zhouwenjie46712bc2018-01-11 15:21:27 -0800730 * "foreground service" or better state. Persistent and other foreground states go here.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700731 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800732 public static final int PROCESS_STATE_FOREGROUND = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700733 /**
734 * Time this uid has any process in an active background state, but none in the
735 * "foreground" or better state.
736 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800737 public static final int PROCESS_STATE_BACKGROUND = 3;
738 /**
739 * Time this uid has any process that is top while the device is sleeping, but not
740 * active for any other reason. We kind-of consider it a kind of cached process
741 * for execution restrictions.
742 */
743 public static final int PROCESS_STATE_TOP_SLEEPING = 4;
744 /**
745 * Time this uid has any process that is in the background but it has an activity
746 * marked as "can't save state". This is essentially a cached process, though the
747 * system will try much harder than normal to avoid killing it.
748 */
749 public static final int PROCESS_STATE_HEAVY_WEIGHT = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700750 /**
751 * Time this uid has any processes that are sitting around cached, not in one of the
752 * other active states.
753 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800754 public static final int PROCESS_STATE_CACHED = 6;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700755 /**
756 * Total number of process states we track.
757 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800758 public static final int NUM_PROCESS_STATE = 7;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700759
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800760 // Used in dump
Dianne Hackborn61659e52014-07-09 16:13:01 -0700761 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800762 "Top", "Fg Service", "Foreground", "Background", "Top Sleeping", "Heavy Weight",
763 "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700764 };
765
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800766 // Used in checkin dump
767 @VisibleForTesting
768 public static final String[] UID_PROCESS_TYPES = {
769 "T", // TOP
770 "FS", // FOREGROUND_SERVICE
771 "F", // FOREGROUND
772 "B", // BACKGROUND
773 "TS", // TOP_SLEEPING
774 "HW", // HEAVY_WEIGHT
775 "C" // CACHED
776 };
777
778 /**
779 * When the process exits one of these states, we need to make sure cpu time in this state
780 * is not attributed to any non-critical process states.
781 */
782 public static final int[] CRITICAL_PROC_STATES = {
783 PROCESS_STATE_TOP, PROCESS_STATE_FOREGROUND_SERVICE, PROCESS_STATE_FOREGROUND
784 };
785
Dianne Hackborn61659e52014-07-09 16:13:01 -0700786 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800787 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700788
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800789 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790
Robert Greenwalta029ea12013-09-25 16:38:12 -0700791 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
792
Dianne Hackborn617f8772009-03-31 15:04:46 -0700793 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700794 * Note that these must match the constants in android.os.PowerManager.
795 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
796 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700797 */
798 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700799 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700800 };
Bookatzc8c44962017-05-11 12:12:54 -0700801
Phil Weaverda80d672016-03-15 16:25:46 -0700802 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700803
Dianne Hackborn617f8772009-03-31 15:04:46 -0700804 public abstract void noteUserActivityLocked(int type);
805 public abstract boolean hasUserActivity();
806 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700807
808 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800809 public abstract long getNetworkActivityBytes(int type, int which);
810 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800811 public abstract long getMobileRadioActiveTime(int which);
812 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700813
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700814 /**
815 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
816 */
817 public abstract long getUserCpuTimeUs(int which);
818
819 /**
820 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
821 */
822 public abstract long getSystemCpuTimeUs(int which);
823
824 /**
Sudheer Shanka71f34b32017-07-21 00:14:24 -0700825 * Returns the approximate cpu time (in microseconds) spent at a certain CPU speed for a
Adam Lesinski6832f392015-09-05 18:05:40 -0700826 * given CPU cluster.
827 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700828 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700829 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700830 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
831 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700832 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700833 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700834
Adam Lesinski5f056f62016-07-14 16:56:08 -0700835 /**
836 * Returns the number of times this UID woke up the Application Processor to
837 * process a mobile radio packet.
838 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
839 */
840 public abstract long getMobileRadioApWakeupCount(int which);
841
842 /**
843 * Returns the number of times this UID woke up the Application Processor to
844 * process a WiFi packet.
845 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
846 */
847 public abstract long getWifiRadioApWakeupCount(int which);
848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800850 /*
851 * FIXME: it's not correct to use this magic value because it
852 * could clash with a sensor handle (which are defined by
853 * the sensor HAL, and therefore out of our control
854 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 // Magic sensor number for the GPS.
856 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800861
Bookatz867c0d72017-03-07 18:23:42 -0800862 /** Returns a Timer for sensor usage when app is in the background. */
863 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 }
865
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700866 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800867 public int mWakeNesting;
868 public long mWakeSumMs;
869 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700870 }
871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 /**
873 * The statistics associated with a particular process.
874 */
875 public static abstract class Proc {
876
Dianne Hackborn287952c2010-09-22 22:34:31 -0700877 public static class ExcessivePower {
878 public static final int TYPE_WAKE = 1;
879 public static final int TYPE_CPU = 2;
880
881 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700882 public long overTime;
883 public long usedTime;
884 }
885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800887 * Returns true if this process is still active in the battery stats.
888 */
889 public abstract boolean isActive();
890
891 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700892 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700894 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 */
896 public abstract long getUserTime(int which);
897
898 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700899 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700901 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 */
903 public abstract long getSystemTime(int which);
904
905 /**
906 * Returns the number of times the process has been started.
907 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700908 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 */
910 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700911
912 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800913 * Returns the number of times the process has crashed.
914 *
915 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
916 */
917 public abstract int getNumCrashes(int which);
918
919 /**
920 * Returns the number of times the process has ANRed.
921 *
922 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
923 */
924 public abstract int getNumAnrs(int which);
925
926 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700927 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700928 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700929 * @return foreground cpu time in microseconds
930 */
931 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700932
Dianne Hackborn287952c2010-09-22 22:34:31 -0700933 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700934
Dianne Hackborn287952c2010-09-22 22:34:31 -0700935 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 }
937
938 /**
939 * The statistics associated with a particular package.
940 */
941 public static abstract class Pkg {
942
943 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700944 * Returns information about all wakeup alarms that have been triggered for this
945 * package. The mapping keys are tag names for the alarms, the counter contains
946 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700948 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949
950 /**
951 * Returns a mapping containing service statistics.
952 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700953 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954
955 /**
956 * The statistics associated with a particular service.
957 */
Joe Onoratoabded112016-02-08 16:49:39 -0800958 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959
960 /**
961 * Returns the amount of time spent started.
962 *
963 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700964 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 * @return
966 */
967 public abstract long getStartTime(long batteryUptime, int which);
968
969 /**
970 * Returns the total number of times startService() has been called.
971 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700972 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 */
974 public abstract int getStarts(int which);
975
976 /**
977 * Returns the total number times the service has been launched.
978 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700979 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 */
981 public abstract int getLaunches(int which);
982 }
983 }
984 }
985
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800986 public static final class LevelStepTracker {
987 public long mLastStepTime = -1;
988 public int mNumStepDurations;
989 public final long[] mStepDurations;
990
991 public LevelStepTracker(int maxLevelSteps) {
992 mStepDurations = new long[maxLevelSteps];
993 }
994
995 public LevelStepTracker(int numSteps, long[] steps) {
996 mNumStepDurations = numSteps;
997 mStepDurations = new long[numSteps];
998 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
999 }
1000
1001 public long getDurationAt(int index) {
1002 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
1003 }
1004
1005 public int getLevelAt(int index) {
1006 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
1007 >> STEP_LEVEL_LEVEL_SHIFT);
1008 }
1009
1010 public int getInitModeAt(int index) {
1011 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
1012 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1013 }
1014
1015 public int getModModeAt(int index) {
1016 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
1017 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1018 }
1019
1020 private void appendHex(long val, int topOffset, StringBuilder out) {
1021 boolean hasData = false;
1022 while (topOffset >= 0) {
1023 int digit = (int)( (val>>topOffset) & 0xf );
1024 topOffset -= 4;
1025 if (!hasData && digit == 0) {
1026 continue;
1027 }
1028 hasData = true;
1029 if (digit >= 0 && digit <= 9) {
1030 out.append((char)('0' + digit));
1031 } else {
1032 out.append((char)('a' + digit - 10));
1033 }
1034 }
1035 }
1036
1037 public void encodeEntryAt(int index, StringBuilder out) {
1038 long item = mStepDurations[index];
1039 long duration = item & STEP_LEVEL_TIME_MASK;
1040 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
1041 >> STEP_LEVEL_LEVEL_SHIFT);
1042 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
1043 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1044 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
1045 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1046 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1047 case Display.STATE_OFF: out.append('f'); break;
1048 case Display.STATE_ON: out.append('o'); break;
1049 case Display.STATE_DOZE: out.append('d'); break;
1050 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
1051 }
1052 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1053 out.append('p');
1054 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001055 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1056 out.append('i');
1057 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001058 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1059 case Display.STATE_OFF: out.append('F'); break;
1060 case Display.STATE_ON: out.append('O'); break;
1061 case Display.STATE_DOZE: out.append('D'); break;
1062 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
1063 }
1064 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1065 out.append('P');
1066 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001067 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1068 out.append('I');
1069 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001070 out.append('-');
1071 appendHex(level, 4, out);
1072 out.append('-');
1073 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
1074 }
1075
1076 public void decodeEntryAt(int index, String value) {
1077 final int N = value.length();
1078 int i = 0;
1079 char c;
1080 long out = 0;
1081 while (i < N && (c=value.charAt(i)) != '-') {
1082 i++;
1083 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001084 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001085 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001086 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001087 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001088 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001089 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001090 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1091 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1092 break;
1093 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1094 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1095 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001096 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1097 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1098 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001099 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1100 break;
1101 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1102 break;
1103 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1104 break;
1105 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1106 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1107 break;
1108 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1109 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001110 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001111 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1112 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1113 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001114 }
1115 }
1116 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001117 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001118 while (i < N && (c=value.charAt(i)) != '-') {
1119 i++;
1120 level <<= 4;
1121 if (c >= '0' && c <= '9') {
1122 level += c - '0';
1123 } else if (c >= 'a' && c <= 'f') {
1124 level += c - 'a' + 10;
1125 } else if (c >= 'A' && c <= 'F') {
1126 level += c - 'A' + 10;
1127 }
1128 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001129 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001130 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
1131 long duration = 0;
1132 while (i < N && (c=value.charAt(i)) != '-') {
1133 i++;
1134 duration <<= 4;
1135 if (c >= '0' && c <= '9') {
1136 duration += c - '0';
1137 } else if (c >= 'a' && c <= 'f') {
1138 duration += c - 'a' + 10;
1139 } else if (c >= 'A' && c <= 'F') {
1140 duration += c - 'A' + 10;
1141 }
1142 }
1143 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
1144 }
1145
1146 public void init() {
1147 mLastStepTime = -1;
1148 mNumStepDurations = 0;
1149 }
1150
1151 public void clearTime() {
1152 mLastStepTime = -1;
1153 }
1154
1155 public long computeTimePerLevel() {
1156 final long[] steps = mStepDurations;
1157 final int numSteps = mNumStepDurations;
1158
1159 // For now we'll do a simple average across all steps.
1160 if (numSteps <= 0) {
1161 return -1;
1162 }
1163 long total = 0;
1164 for (int i=0; i<numSteps; i++) {
1165 total += steps[i] & STEP_LEVEL_TIME_MASK;
1166 }
1167 return total / numSteps;
1168 /*
1169 long[] buckets = new long[numSteps];
1170 int numBuckets = 0;
1171 int numToAverage = 4;
1172 int i = 0;
1173 while (i < numSteps) {
1174 long totalTime = 0;
1175 int num = 0;
1176 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1177 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1178 num++;
1179 }
1180 buckets[numBuckets] = totalTime / num;
1181 numBuckets++;
1182 numToAverage *= 2;
1183 i += num;
1184 }
1185 if (numBuckets < 1) {
1186 return -1;
1187 }
1188 long averageTime = buckets[numBuckets-1];
1189 for (i=numBuckets-2; i>=0; i--) {
1190 averageTime = (averageTime + buckets[i]) / 2;
1191 }
1192 return averageTime;
1193 */
1194 }
1195
1196 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1197 int[] outNumOfInterest) {
1198 final long[] steps = mStepDurations;
1199 final int count = mNumStepDurations;
1200 if (count <= 0) {
1201 return -1;
1202 }
1203 long total = 0;
1204 int numOfInterest = 0;
1205 for (int i=0; i<count; i++) {
1206 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1207 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1208 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1209 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1210 // If the modes of interest didn't change during this step period...
1211 if ((modMode&modesOfInterest) == 0) {
1212 // And the mode values during this period match those we are measuring...
1213 if ((initMode&modesOfInterest) == modeValues) {
1214 // Then this can be used to estimate the total time!
1215 numOfInterest++;
1216 total += steps[i] & STEP_LEVEL_TIME_MASK;
1217 }
1218 }
1219 }
1220 if (numOfInterest <= 0) {
1221 return -1;
1222 }
1223
1224 if (outNumOfInterest != null) {
1225 outNumOfInterest[0] = numOfInterest;
1226 }
1227
1228 // The estimated time is the average time we spend in each level, multipled
1229 // by 100 -- the total number of battery levels
1230 return (total / numOfInterest) * 100;
1231 }
1232
1233 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1234 int stepCount = mNumStepDurations;
1235 final long lastStepTime = mLastStepTime;
1236 if (lastStepTime >= 0 && numStepLevels > 0) {
1237 final long[] steps = mStepDurations;
1238 long duration = elapsedRealtime - lastStepTime;
1239 for (int i=0; i<numStepLevels; i++) {
1240 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1241 long thisDuration = duration / (numStepLevels-i);
1242 duration -= thisDuration;
1243 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1244 thisDuration = STEP_LEVEL_TIME_MASK;
1245 }
1246 steps[0] = thisDuration | modeBits;
1247 }
1248 stepCount += numStepLevels;
1249 if (stepCount > steps.length) {
1250 stepCount = steps.length;
1251 }
1252 }
1253 mNumStepDurations = stepCount;
1254 mLastStepTime = elapsedRealtime;
1255 }
1256
1257 public void readFromParcel(Parcel in) {
1258 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001259 if (N > mStepDurations.length) {
1260 throw new ParcelFormatException("more step durations than available: " + N);
1261 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001262 mNumStepDurations = N;
1263 for (int i=0; i<N; i++) {
1264 mStepDurations[i] = in.readLong();
1265 }
1266 }
1267
1268 public void writeToParcel(Parcel out) {
1269 final int N = mNumStepDurations;
1270 out.writeInt(N);
1271 for (int i=0; i<N; i++) {
1272 out.writeLong(mStepDurations[i]);
1273 }
1274 }
1275 }
1276
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001277 public static final class PackageChange {
1278 public String mPackageName;
1279 public boolean mUpdate;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001280 public long mVersionCode;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001281 }
1282
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001283 public static final class DailyItem {
1284 public long mStartTime;
1285 public long mEndTime;
1286 public LevelStepTracker mDischargeSteps;
1287 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001288 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001289 }
1290
1291 public abstract DailyItem getDailyItemLocked(int daysAgo);
1292
1293 public abstract long getCurrentDailyStartTime();
1294
1295 public abstract long getNextMinDailyDeadline();
1296
1297 public abstract long getNextMaxDailyDeadline();
1298
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001299 public abstract long[] getCpuFreqs();
1300
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001301 public final static class HistoryTag {
1302 public String string;
1303 public int uid;
1304
1305 public int poolIdx;
1306
1307 public void setTo(HistoryTag o) {
1308 string = o.string;
1309 uid = o.uid;
1310 poolIdx = o.poolIdx;
1311 }
1312
1313 public void setTo(String _string, int _uid) {
1314 string = _string;
1315 uid = _uid;
1316 poolIdx = -1;
1317 }
1318
1319 public void writeToParcel(Parcel dest, int flags) {
1320 dest.writeString(string);
1321 dest.writeInt(uid);
1322 }
1323
1324 public void readFromParcel(Parcel src) {
1325 string = src.readString();
1326 uid = src.readInt();
1327 poolIdx = -1;
1328 }
1329
1330 @Override
1331 public boolean equals(Object o) {
1332 if (this == o) return true;
1333 if (o == null || getClass() != o.getClass()) return false;
1334
1335 HistoryTag that = (HistoryTag) o;
1336
1337 if (uid != that.uid) return false;
1338 if (!string.equals(that.string)) return false;
1339
1340 return true;
1341 }
1342
1343 @Override
1344 public int hashCode() {
1345 int result = string.hashCode();
1346 result = 31 * result + uid;
1347 return result;
1348 }
1349 }
1350
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001351 /**
1352 * Optional detailed information that can go into a history step. This is typically
1353 * generated each time the battery level changes.
1354 */
1355 public final static class HistoryStepDetails {
1356 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1357 public int userTime;
1358 public int systemTime;
1359
1360 // Top three apps using CPU in the last step, with times in 1/100 second.
1361 public int appCpuUid1;
1362 public int appCpuUTime1;
1363 public int appCpuSTime1;
1364 public int appCpuUid2;
1365 public int appCpuUTime2;
1366 public int appCpuSTime2;
1367 public int appCpuUid3;
1368 public int appCpuUTime3;
1369 public int appCpuSTime3;
1370
1371 // Information from /proc/stat
1372 public int statUserTime;
1373 public int statSystemTime;
1374 public int statIOWaitTime;
1375 public int statIrqTime;
1376 public int statSoftIrqTime;
1377 public int statIdlTime;
1378
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001379 // Platform-level low power state stats
1380 public String statPlatformIdleState;
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001381 public String statSubsystemPowerState;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001382
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001383 public HistoryStepDetails() {
1384 clear();
1385 }
1386
1387 public void clear() {
1388 userTime = systemTime = 0;
1389 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1390 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1391 = appCpuUTime3 = appCpuSTime3 = 0;
1392 }
1393
1394 public void writeToParcel(Parcel out) {
1395 out.writeInt(userTime);
1396 out.writeInt(systemTime);
1397 out.writeInt(appCpuUid1);
1398 out.writeInt(appCpuUTime1);
1399 out.writeInt(appCpuSTime1);
1400 out.writeInt(appCpuUid2);
1401 out.writeInt(appCpuUTime2);
1402 out.writeInt(appCpuSTime2);
1403 out.writeInt(appCpuUid3);
1404 out.writeInt(appCpuUTime3);
1405 out.writeInt(appCpuSTime3);
1406 out.writeInt(statUserTime);
1407 out.writeInt(statSystemTime);
1408 out.writeInt(statIOWaitTime);
1409 out.writeInt(statIrqTime);
1410 out.writeInt(statSoftIrqTime);
1411 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001412 out.writeString(statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001413 out.writeString(statSubsystemPowerState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001414 }
1415
1416 public void readFromParcel(Parcel in) {
1417 userTime = in.readInt();
1418 systemTime = in.readInt();
1419 appCpuUid1 = in.readInt();
1420 appCpuUTime1 = in.readInt();
1421 appCpuSTime1 = in.readInt();
1422 appCpuUid2 = in.readInt();
1423 appCpuUTime2 = in.readInt();
1424 appCpuSTime2 = in.readInt();
1425 appCpuUid3 = in.readInt();
1426 appCpuUTime3 = in.readInt();
1427 appCpuSTime3 = in.readInt();
1428 statUserTime = in.readInt();
1429 statSystemTime = in.readInt();
1430 statIOWaitTime = in.readInt();
1431 statIrqTime = in.readInt();
1432 statSoftIrqTime = in.readInt();
1433 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001434 statPlatformIdleState = in.readString();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001435 statSubsystemPowerState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001436 }
1437 }
1438
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001439 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001440 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001441
1442 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001443 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001444
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001445 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001446 public static final byte CMD_NULL = -1;
1447 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001448 public static final byte CMD_CURRENT_TIME = 5;
1449 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001450 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001451 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001452
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001453 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001454
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001455 /**
1456 * Return whether the command code is a delta data update.
1457 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001458 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001459 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001460 }
1461
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001462 public byte batteryLevel;
1463 public byte batteryStatus;
1464 public byte batteryHealth;
1465 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001466
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001467 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001468 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001469
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001470 // The charge of the battery in micro-Ampere-hours.
1471 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001472
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001473 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001474 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001475 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001476 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001477 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1478 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001479 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001480 public static final int STATE_PHONE_STATE_SHIFT = 6;
1481 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001482 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001483 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001484 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001485
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001486 // These states always appear directly in the first int token
1487 // of a delta change; they should be ones that change relatively
1488 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001489 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1490 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001491 public static final int STATE_GPS_ON_FLAG = 1<<29;
1492 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001493 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001494 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001495 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001496 // Do not use, this is used for coulomb delta count.
1497 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001498 // These are on the lower bits used for the command; if they change
1499 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001500 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001501 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001502 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001503 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1504 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
Mike Mac2f518a2017-09-19 16:06:03 -07001505 public static final int STATE_SCREEN_DOZE_FLAG = 1 << 18;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001506 // empty slot
1507 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001508
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001509 public static final int MOST_INTERESTING_STATES =
Mike Mac2f518a2017-09-19 16:06:03 -07001510 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG | STATE_SCREEN_DOZE_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001511
1512 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001513
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001514 public int states;
1515
Dianne Hackborn3251b902014-06-20 14:40:53 -07001516 // Constants from WIFI_SUPPL_STATE_*
1517 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1518 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1519 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1520 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1521 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1522 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
Siddharth Ray78ccaf52017-12-23 16:16:21 -08001523 // Values for NUM_GPS_SIGNAL_QUALITY_LEVELS
1524 public static final int STATE2_GPS_SIGNAL_QUALITY_SHIFT = 7;
1525 public static final int STATE2_GPS_SIGNAL_QUALITY_MASK =
1526 0x1 << STATE2_GPS_SIGNAL_QUALITY_SHIFT;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001527
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001528 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001529 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1530 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1531 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001532 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001533 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1534 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1535 public static final int STATE2_CHARGING_FLAG = 1<<24;
1536 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1537 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1538 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001539 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Siddharth Rayf5e796a2018-01-22 18:18:17 -08001540 public static final int STATE2_CELLULAR_HIGH_TX_POWER_FLAG = 1 << 19;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001541
1542 public static final int MOST_INTERESTING_STATES2 =
Mike Mac2f518a2017-09-19 16:06:03 -07001543 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
1544 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001545
1546 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001547
Dianne Hackborn40c87252014-03-19 16:55:40 -07001548 public int states2;
1549
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001550 // The wake lock that was acquired at this point.
1551 public HistoryTag wakelockTag;
1552
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001553 // Kernel wakeup reason at this point.
1554 public HistoryTag wakeReasonTag;
1555
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001556 // Non-null when there is more detailed information at this step.
1557 public HistoryStepDetails stepDetails;
1558
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001559 public static final int EVENT_FLAG_START = 0x8000;
1560 public static final int EVENT_FLAG_FINISH = 0x4000;
1561
1562 // No event in this item.
1563 public static final int EVENT_NONE = 0x0000;
1564 // Event is about a process that is running.
1565 public static final int EVENT_PROC = 0x0001;
1566 // Event is about an application package that is in the foreground.
1567 public static final int EVENT_FOREGROUND = 0x0002;
1568 // Event is about an application package that is at the top of the screen.
1569 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001570 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001571 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001572 // Events for all additional wake locks aquired/release within a wake block.
1573 // These are not generated by default.
1574 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001575 // Event is about an application executing a scheduled job.
1576 public static final int EVENT_JOB = 0x0006;
1577 // Events for users running.
1578 public static final int EVENT_USER_RUNNING = 0x0007;
1579 // Events for foreground user.
1580 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001581 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001582 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001583 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001584 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001585 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001586 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001587 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001588 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001589 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001590 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001591 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001592 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001593 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001594 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001595 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001596 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001597 // Event for a package being on the temporary whitelist.
1598 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001599 // Event for the screen waking up.
1600 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001601 // Event for the UID that woke up the application processor.
1602 // Used for wakeups coming from WiFi, modem, etc.
1603 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001604 // Event for reporting that a specific partial wake lock has been held for a long duration.
1605 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001606
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001607 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001608 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001609 // Mask to extract out only the type part of the event.
1610 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001611
1612 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1613 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1614 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1615 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1616 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1617 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001618 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1619 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001620 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1621 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001622 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1623 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1624 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1625 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1626 public static final int EVENT_USER_FOREGROUND_START =
1627 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1628 public static final int EVENT_USER_FOREGROUND_FINISH =
1629 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001630 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1631 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001632 public static final int EVENT_TEMP_WHITELIST_START =
1633 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1634 public static final int EVENT_TEMP_WHITELIST_FINISH =
1635 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001636 public static final int EVENT_LONG_WAKE_LOCK_START =
1637 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1638 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1639 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001640
1641 // For CMD_EVENT.
1642 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001643 public HistoryTag eventTag;
1644
Dianne Hackborn9a755432014-05-15 17:05:22 -07001645 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001646 public long currentTime;
1647
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001648 // Meta-data when reading.
1649 public int numReadInts;
1650
1651 // Pre-allocated objects.
1652 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001653 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001654 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001655
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001656 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001657 }
Bookatzc8c44962017-05-11 12:12:54 -07001658
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001659 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001660 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001661 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001662 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001663 }
Bookatzc8c44962017-05-11 12:12:54 -07001664
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001665 public int describeContents() {
1666 return 0;
1667 }
1668
1669 public void writeToParcel(Parcel dest, int flags) {
1670 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001671 int bat = (((int)cmd)&0xff)
1672 | ((((int)batteryLevel)<<8)&0xff00)
1673 | ((((int)batteryStatus)<<16)&0xf0000)
1674 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001675 | ((((int)batteryPlugType)<<24)&0xf000000)
1676 | (wakelockTag != null ? 0x10000000 : 0)
1677 | (wakeReasonTag != null ? 0x20000000 : 0)
1678 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001679 dest.writeInt(bat);
1680 bat = (((int)batteryTemperature)&0xffff)
1681 | ((((int)batteryVoltage)<<16)&0xffff0000);
1682 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001683 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001684 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001685 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001686 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001687 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001688 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001689 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001690 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001691 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001692 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001693 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001694 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001695 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001696 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001697 dest.writeLong(currentTime);
1698 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001699 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001700
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001701 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001702 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001703 int bat = src.readInt();
1704 cmd = (byte)(bat&0xff);
1705 batteryLevel = (byte)((bat>>8)&0xff);
1706 batteryStatus = (byte)((bat>>16)&0xf);
1707 batteryHealth = (byte)((bat>>20)&0xf);
1708 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001709 int bat2 = src.readInt();
1710 batteryTemperature = (short)(bat2&0xffff);
1711 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001712 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001713 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001714 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001715 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001716 wakelockTag = localWakelockTag;
1717 wakelockTag.readFromParcel(src);
1718 } else {
1719 wakelockTag = null;
1720 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001721 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001722 wakeReasonTag = localWakeReasonTag;
1723 wakeReasonTag.readFromParcel(src);
1724 } else {
1725 wakeReasonTag = null;
1726 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001727 if ((bat&0x40000000) != 0) {
1728 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001729 eventTag = localEventTag;
1730 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001731 } else {
1732 eventCode = EVENT_NONE;
1733 eventTag = null;
1734 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001735 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001736 currentTime = src.readLong();
1737 } else {
1738 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001739 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001740 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001741 }
1742
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001743 public void clear() {
1744 time = 0;
1745 cmd = CMD_NULL;
1746 batteryLevel = 0;
1747 batteryStatus = 0;
1748 batteryHealth = 0;
1749 batteryPlugType = 0;
1750 batteryTemperature = 0;
1751 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001752 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001753 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001754 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001755 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001756 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001757 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001758 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001759 }
Bookatzc8c44962017-05-11 12:12:54 -07001760
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001761 public void setTo(HistoryItem o) {
1762 time = o.time;
1763 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001764 setToCommon(o);
1765 }
1766
1767 public void setTo(long time, byte cmd, HistoryItem o) {
1768 this.time = time;
1769 this.cmd = cmd;
1770 setToCommon(o);
1771 }
1772
1773 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001774 batteryLevel = o.batteryLevel;
1775 batteryStatus = o.batteryStatus;
1776 batteryHealth = o.batteryHealth;
1777 batteryPlugType = o.batteryPlugType;
1778 batteryTemperature = o.batteryTemperature;
1779 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001780 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001781 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001782 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001783 if (o.wakelockTag != null) {
1784 wakelockTag = localWakelockTag;
1785 wakelockTag.setTo(o.wakelockTag);
1786 } else {
1787 wakelockTag = null;
1788 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001789 if (o.wakeReasonTag != null) {
1790 wakeReasonTag = localWakeReasonTag;
1791 wakeReasonTag.setTo(o.wakeReasonTag);
1792 } else {
1793 wakeReasonTag = null;
1794 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001795 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001796 if (o.eventTag != null) {
1797 eventTag = localEventTag;
1798 eventTag.setTo(o.eventTag);
1799 } else {
1800 eventTag = null;
1801 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001802 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001803 }
1804
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001805 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001806 return batteryLevel == o.batteryLevel
1807 && batteryStatus == o.batteryStatus
1808 && batteryHealth == o.batteryHealth
1809 && batteryPlugType == o.batteryPlugType
1810 && batteryTemperature == o.batteryTemperature
1811 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001812 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001813 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001814 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001815 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001816 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001817
1818 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001819 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001820 return false;
1821 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001822 if (wakelockTag != o.wakelockTag) {
1823 if (wakelockTag == null || o.wakelockTag == null) {
1824 return false;
1825 }
1826 if (!wakelockTag.equals(o.wakelockTag)) {
1827 return false;
1828 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001829 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001830 if (wakeReasonTag != o.wakeReasonTag) {
1831 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1832 return false;
1833 }
1834 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1835 return false;
1836 }
1837 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001838 if (eventTag != o.eventTag) {
1839 if (eventTag == null || o.eventTag == null) {
1840 return false;
1841 }
1842 if (!eventTag.equals(o.eventTag)) {
1843 return false;
1844 }
1845 }
1846 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001847 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001848 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001849
1850 public final static class HistoryEventTracker {
1851 private final HashMap<String, SparseIntArray>[] mActiveEvents
1852 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1853
1854 public boolean updateState(int code, String name, int uid, int poolIdx) {
1855 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1856 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1857 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1858 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001859 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001860 mActiveEvents[idx] = active;
1861 }
1862 SparseIntArray uids = active.get(name);
1863 if (uids == null) {
1864 uids = new SparseIntArray();
1865 active.put(name, uids);
1866 }
1867 if (uids.indexOfKey(uid) >= 0) {
1868 // Already set, nothing to do!
1869 return false;
1870 }
1871 uids.put(uid, poolIdx);
1872 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1873 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1874 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1875 if (active == null) {
1876 // not currently active, nothing to do.
1877 return false;
1878 }
1879 SparseIntArray uids = active.get(name);
1880 if (uids == null) {
1881 // not currently active, nothing to do.
1882 return false;
1883 }
1884 idx = uids.indexOfKey(uid);
1885 if (idx < 0) {
1886 // not currently active, nothing to do.
1887 return false;
1888 }
1889 uids.removeAt(idx);
1890 if (uids.size() <= 0) {
1891 active.remove(name);
1892 }
1893 }
1894 return true;
1895 }
1896
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001897 public void removeEvents(int code) {
1898 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1899 mActiveEvents[idx] = null;
1900 }
1901
Dianne Hackborn37de0982014-05-09 09:32:18 -07001902 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1903 return mActiveEvents[code];
1904 }
1905 }
1906
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001907 public static final class BitDescription {
1908 public final int mask;
1909 public final int shift;
1910 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001911 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001912 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001913 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07001914
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001915 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001916 this.mask = mask;
1917 this.shift = -1;
1918 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001919 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001920 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001921 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001922 }
Bookatzc8c44962017-05-11 12:12:54 -07001923
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001924 public BitDescription(int mask, int shift, String name, String shortName,
1925 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001926 this.mask = mask;
1927 this.shift = shift;
1928 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001929 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001930 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001931 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001932 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001933 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001934
Dianne Hackbornfc064132014-06-02 12:42:12 -07001935 /**
1936 * Don't allow any more batching in to the current history event. This
1937 * is called when printing partial histories, so to ensure that the next
1938 * history event will go in to a new batch after what was printed in the
1939 * last partial history.
1940 */
1941 public abstract void commitCurrentHistoryBatchLocked();
1942
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001943 public abstract int getHistoryTotalSize();
1944
1945 public abstract int getHistoryUsedSize();
1946
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001947 public abstract boolean startIteratingHistoryLocked();
1948
Dianne Hackborn099bc622014-01-22 13:39:16 -08001949 public abstract int getHistoryStringPoolSize();
1950
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001951 public abstract int getHistoryStringPoolBytes();
1952
1953 public abstract String getHistoryTagPoolString(int index);
1954
1955 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001956
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001957 public abstract boolean getNextHistoryLocked(HistoryItem out);
1958
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001959 public abstract void finishIteratingHistoryLocked();
1960
1961 public abstract boolean startIteratingOldHistoryLocked();
1962
1963 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1964
1965 public abstract void finishIteratingOldHistoryLocked();
1966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001968 * Return the base time offset for the battery history.
1969 */
1970 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07001971
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001972 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 * Returns the number of times the device has been started.
1974 */
1975 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07001976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001978 * 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 -08001979 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07001980 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 * {@hide}
1982 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001983 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07001984
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001985 /**
1986 * Returns the number of times the screen was turned on.
1987 *
1988 * {@hide}
1989 */
1990 public abstract int getScreenOnCount(int which);
1991
Mike Mac2f518a2017-09-19 16:06:03 -07001992 /**
1993 * Returns the time in microseconds that the screen has been dozing while the device was
1994 * running on battery.
1995 *
1996 * {@hide}
1997 */
1998 public abstract long getScreenDozeTime(long elapsedRealtimeUs, int which);
1999
2000 /**
2001 * Returns the number of times the screen was turned dozing.
2002 *
2003 * {@hide}
2004 */
2005 public abstract int getScreenDozeCount(int which);
2006
Jeff Browne95c3cd2014-05-02 16:59:26 -07002007 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
2008
Dianne Hackborn617f8772009-03-31 15:04:46 -07002009 public static final int SCREEN_BRIGHTNESS_DARK = 0;
2010 public static final int SCREEN_BRIGHTNESS_DIM = 1;
2011 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
2012 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
2013 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07002014
Dianne Hackborn617f8772009-03-31 15:04:46 -07002015 static final String[] SCREEN_BRIGHTNESS_NAMES = {
2016 "dark", "dim", "medium", "light", "bright"
2017 };
Bookatzc8c44962017-05-11 12:12:54 -07002018
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002019 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
2020 "0", "1", "2", "3", "4"
2021 };
2022
Dianne Hackborn617f8772009-03-31 15:04:46 -07002023 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07002024
Dianne Hackborn617f8772009-03-31 15:04:46 -07002025 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002026 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07002027 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07002028 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002029 * {@hide}
2030 */
2031 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002032 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002035 * Returns the {@link Timer} object that tracks the given screen brightness.
2036 *
2037 * {@hide}
2038 */
2039 public abstract Timer getScreenBrightnessTimer(int brightnessBin);
2040
2041 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002042 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002043 * running on battery.
2044 *
2045 * {@hide}
2046 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002047 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002048
2049 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002050 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002051 *
2052 * {@hide}
2053 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002054 public abstract int getPowerSaveModeEnabledCount(int which);
2055
2056 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002057 * Constant for device idle mode: not active.
2058 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002059 public static final int DEVICE_IDLE_MODE_OFF = ServerProtoEnums.DEVICE_IDLE_MODE_OFF; // 0
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002060
2061 /**
2062 * Constant for device idle mode: active in lightweight mode.
2063 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002064 public static final int DEVICE_IDLE_MODE_LIGHT = ServerProtoEnums.DEVICE_IDLE_MODE_LIGHT; // 1
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002065
2066 /**
2067 * Constant for device idle mode: active in full mode.
2068 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002069 public static final int DEVICE_IDLE_MODE_DEEP = ServerProtoEnums.DEVICE_IDLE_MODE_DEEP; // 2
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002070
2071 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002072 * Returns the time in microseconds that device has been in idle mode while
2073 * running on battery.
2074 *
2075 * {@hide}
2076 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002077 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002078
2079 /**
2080 * Returns the number of times that the devie has gone in to idle mode.
2081 *
2082 * {@hide}
2083 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002084 public abstract int getDeviceIdleModeCount(int mode, int which);
2085
2086 /**
2087 * Return the longest duration we spent in a particular device idle mode (fully in the
2088 * mode, not in idle maintenance etc).
2089 */
2090 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002091
2092 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002093 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002094 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002095 * counts all of the time that we consider the device to be idle, whether or not
2096 * it is currently in the actual device idle mode.
2097 *
2098 * {@hide}
2099 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002100 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002101
2102 /**
Bookatz8c6571b2017-10-24 15:04:41 -07002103 * Returns the number of times that the device has started idling.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002104 *
2105 * {@hide}
2106 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002107 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002108
2109 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002110 * Returns the number of times that connectivity state changed.
2111 *
2112 * {@hide}
2113 */
2114 public abstract int getNumConnectivityChange(int which);
2115
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002116
2117 /**
2118 * Returns the time in microseconds that the phone has been running with
2119 * the given GPS signal quality level
2120 *
2121 * {@hide}
2122 */
2123 public abstract long getGpsSignalQualityTime(int strengthBin,
2124 long elapsedRealtimeUs, int which);
2125
2126 /**
2127 * Returns the GPS battery drain in mA-ms
2128 *
2129 * {@hide}
2130 */
2131 public abstract long getGpsBatteryDrainMaMs();
2132
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002133 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002134 * 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 -08002135 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002136 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 * {@hide}
2138 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002139 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002140
Dianne Hackborn627bba72009-03-24 22:32:56 -07002141 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002142 * Returns the number of times a phone call was activated.
2143 *
2144 * {@hide}
2145 */
2146 public abstract int getPhoneOnCount(int which);
2147
2148 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002149 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002150 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002151 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002152 * {@hide}
2153 */
2154 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002155 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002156
Dianne Hackborn617f8772009-03-31 15:04:46 -07002157 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07002158 * Returns the time in microseconds that the phone has been trying to
2159 * acquire a signal.
2160 *
2161 * {@hide}
2162 */
2163 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002164 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002165
2166 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002167 * Returns the {@link Timer} object that tracks how much the phone has been trying to
2168 * acquire a signal.
2169 *
2170 * {@hide}
2171 */
2172 public abstract Timer getPhoneSignalScanningTimer();
2173
2174 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002175 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002176 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002177 * {@hide}
2178 */
2179 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
2180
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002181 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002182 * Return the {@link Timer} object used to track the given signal strength's duration and
2183 * counts.
2184 */
2185 protected abstract Timer getPhoneSignalStrengthTimer(int strengthBin);
2186
2187 /**
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002188 * Returns the time in microseconds that the mobile network has been active
2189 * (in a high power state).
2190 *
2191 * {@hide}
2192 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002193 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002194
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002195 /**
2196 * Returns the number of times that the mobile network has transitioned to the
2197 * active state.
2198 *
2199 * {@hide}
2200 */
2201 public abstract int getMobileRadioActiveCount(int which);
2202
2203 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002204 * Returns the time in microseconds that is the difference between the mobile radio
2205 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
2206 * from the radio.
2207 *
2208 * {@hide}
2209 */
2210 public abstract long getMobileRadioActiveAdjustedTime(int which);
2211
2212 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002213 * Returns the time in microseconds that the mobile network has been active
2214 * (in a high power state) but not being able to blame on an app.
2215 *
2216 * {@hide}
2217 */
2218 public abstract long getMobileRadioActiveUnknownTime(int which);
2219
2220 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002221 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002222 *
2223 * {@hide}
2224 */
2225 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002226
Dianne Hackborn627bba72009-03-24 22:32:56 -07002227 public static final int DATA_CONNECTION_NONE = 0;
2228 public static final int DATA_CONNECTION_GPRS = 1;
2229 public static final int DATA_CONNECTION_EDGE = 2;
2230 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002231 public static final int DATA_CONNECTION_CDMA = 4;
2232 public static final int DATA_CONNECTION_EVDO_0 = 5;
2233 public static final int DATA_CONNECTION_EVDO_A = 6;
2234 public static final int DATA_CONNECTION_1xRTT = 7;
2235 public static final int DATA_CONNECTION_HSDPA = 8;
2236 public static final int DATA_CONNECTION_HSUPA = 9;
2237 public static final int DATA_CONNECTION_HSPA = 10;
2238 public static final int DATA_CONNECTION_IDEN = 11;
2239 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002240 public static final int DATA_CONNECTION_LTE = 13;
2241 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08002242 public static final int DATA_CONNECTION_HSPAP = 15;
2243 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002244
Dianne Hackborn627bba72009-03-24 22:32:56 -07002245 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002246 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002247 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08002248 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002249 };
Bookatzc8c44962017-05-11 12:12:54 -07002250
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002251 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002252
Dianne Hackborn627bba72009-03-24 22:32:56 -07002253 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002254 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002255 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002256 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002257 * {@hide}
2258 */
2259 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002260 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002263 * Returns the number of times the phone has entered the given data
2264 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002265 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002266 * {@hide}
2267 */
2268 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002269
Kweku Adams87b19ec2017-10-09 12:40:03 -07002270 /**
2271 * Returns the {@link Timer} object that tracks the phone's data connection type stats.
2272 */
2273 public abstract Timer getPhoneDataConnectionTimer(int dataType);
2274
Dianne Hackborn3251b902014-06-20 14:40:53 -07002275 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2276 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2277 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2278 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2279 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2280 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2281 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2282 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2283 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2284 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2285 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2286 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2287 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2288
2289 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2290
2291 static final String[] WIFI_SUPPL_STATE_NAMES = {
2292 "invalid", "disconn", "disabled", "inactive", "scanning",
2293 "authenticating", "associating", "associated", "4-way-handshake",
2294 "group-handshake", "completed", "dormant", "uninit"
2295 };
2296
2297 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2298 "inv", "dsc", "dis", "inact", "scan",
2299 "auth", "ascing", "asced", "4-way",
2300 "group", "compl", "dorm", "uninit"
2301 };
2302
Mike Mac2f518a2017-09-19 16:06:03 -07002303 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002304 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002305 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2306 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002307 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002308 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2309 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2310 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002311 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002312 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002313 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002314 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002315 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2316 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Mike Mac2f518a2017-09-19 16:06:03 -07002317 new BitDescription(HistoryItem.STATE_SCREEN_DOZE_FLAG, "screen_doze", "Sd"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002318 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2319 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2320 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2321 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2322 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2323 new String[] {"in", "out", "emergency", "off"},
2324 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002325 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2326 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2327 SignalStrength.SIGNAL_STRENGTH_NAMES,
2328 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002329 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2330 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2331 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002332 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002333
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002334 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2335 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002336 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002337 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002338 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002339 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002340 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002341 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2342 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2343 new String[] { "off", "light", "full", "???" },
2344 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002345 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2346 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2347 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002348 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2349 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2350 new String[] { "0", "1", "2", "3", "4" },
2351 new String[] { "0", "1", "2", "3", "4" }),
2352 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2353 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2354 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002355 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002356 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Siddharth Rayf5e796a2018-01-22 18:18:17 -08002357 new BitDescription(HistoryItem.STATE2_CELLULAR_HIGH_TX_POWER_FLAG,
2358 "cellular_high_tx_power", "Chtp"),
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002359 new BitDescription(HistoryItem.STATE2_GPS_SIGNAL_QUALITY_MASK,
2360 HistoryItem.STATE2_GPS_SIGNAL_QUALITY_SHIFT, "gps_signal_quality", "Gss",
Siddharth Rayf5e796a2018-01-22 18:18:17 -08002361 new String[] { "poor", "good"}, new String[] { "poor", "good"})
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002362 };
2363
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002364 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002365 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002366 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2367 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002368 };
2369
2370 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002371 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002372 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002373 "Esw", "Ewa", "Elw", "Eec"
2374 };
2375
2376 @FunctionalInterface
2377 public interface IntToString {
2378 String applyAsString(int val);
2379 }
2380
2381 private static final IntToString sUidToString = UserHandle::formatUid;
2382 private static final IntToString sIntToString = Integer::toString;
2383
2384 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2385 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2386 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2387 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2388 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002389 };
2390
Dianne Hackborn617f8772009-03-31 15:04:46 -07002391 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08002392 * Returns total time for WiFi Multicast Wakelock timer.
2393 * Note that this may be different from the sum of per uid timer values.
2394 *
2395 * {@hide}
2396 */
2397 public abstract long getWifiMulticastWakelockTime(long elapsedRealtimeUs, int which);
2398
2399 /**
2400 * Returns total time for WiFi Multicast Wakelock timer
2401 * Note that this may be different from the sum of per uid timer values.
2402 *
2403 * {@hide}
2404 */
2405 public abstract int getWifiMulticastWakelockCount(int which);
2406
2407 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002408 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002409 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002410 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002411 * {@hide}
2412 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002413 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002414
2415 /**
Siddharth Rayb50a6842017-12-14 15:15:28 -08002416 * Returns the time in microseconds that wifi has been active while the device was
2417 * running on battery.
2418 *
2419 * {@hide}
2420 */
2421 public abstract long getWifiActiveTime(long elapsedRealtimeUs, int which);
2422
2423 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002424 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002425 * been in the running state while the device was running on battery.
2426 *
2427 * {@hide}
2428 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002429 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002430
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002431 public static final int WIFI_STATE_OFF = 0;
2432 public static final int WIFI_STATE_OFF_SCANNING = 1;
2433 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2434 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2435 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2436 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2437 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2438 public static final int WIFI_STATE_SOFT_AP = 7;
2439
2440 static final String[] WIFI_STATE_NAMES = {
2441 "off", "scanning", "no_net", "disconn",
2442 "sta", "p2p", "sta_p2p", "soft_ap"
2443 };
2444
2445 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2446
2447 /**
2448 * Returns the time in microseconds that WiFi has been running in the given state.
2449 *
2450 * {@hide}
2451 */
2452 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002453 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002454
2455 /**
2456 * Returns the number of times that WiFi has entered the given state.
2457 *
2458 * {@hide}
2459 */
2460 public abstract int getWifiStateCount(int wifiState, int which);
2461
The Android Open Source Project10592532009-03-18 17:39:46 -07002462 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002463 * Returns the {@link Timer} object that tracks the given WiFi state.
2464 *
2465 * {@hide}
2466 */
2467 public abstract Timer getWifiStateTimer(int wifiState);
2468
2469 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002470 * Returns the time in microseconds that the wifi supplicant has been
2471 * in a given state.
2472 *
2473 * {@hide}
2474 */
2475 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2476
2477 /**
2478 * Returns the number of times that the wifi supplicant has transitioned
2479 * to a given state.
2480 *
2481 * {@hide}
2482 */
2483 public abstract int getWifiSupplStateCount(int state, int which);
2484
Kweku Adams87b19ec2017-10-09 12:40:03 -07002485 /**
2486 * Returns the {@link Timer} object that tracks the given wifi supplicant state.
2487 *
2488 * {@hide}
2489 */
2490 public abstract Timer getWifiSupplStateTimer(int state);
2491
Dianne Hackborn3251b902014-06-20 14:40:53 -07002492 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2493
2494 /**
2495 * Returns the time in microseconds that WIFI has been running with
2496 * the given signal strength.
2497 *
2498 * {@hide}
2499 */
2500 public abstract long getWifiSignalStrengthTime(int strengthBin,
2501 long elapsedRealtimeUs, int which);
2502
2503 /**
2504 * Returns the number of times WIFI has entered the given signal strength.
2505 *
2506 * {@hide}
2507 */
2508 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2509
2510 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002511 * Returns the {@link Timer} object that tracks the given WIFI signal strength.
2512 *
2513 * {@hide}
2514 */
2515 public abstract Timer getWifiSignalStrengthTimer(int strengthBin);
2516
2517 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002518 * Returns the time in microseconds that the flashlight has been on while the device was
2519 * running on battery.
2520 *
2521 * {@hide}
2522 */
2523 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2524
2525 /**
2526 * Returns the number of times that the flashlight has been turned on while the device was
2527 * running on battery.
2528 *
2529 * {@hide}
2530 */
2531 public abstract long getFlashlightOnCount(int which);
2532
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002533 /**
2534 * Returns the time in microseconds that the camera has been on while the device was
2535 * running on battery.
2536 *
2537 * {@hide}
2538 */
2539 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2540
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002541 /**
2542 * Returns the time in microseconds that bluetooth scans were running while the device was
2543 * on battery.
2544 *
2545 * {@hide}
2546 */
2547 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002548
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002549 public static final int NETWORK_MOBILE_RX_DATA = 0;
2550 public static final int NETWORK_MOBILE_TX_DATA = 1;
2551 public static final int NETWORK_WIFI_RX_DATA = 2;
2552 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002553 public static final int NETWORK_BT_RX_DATA = 4;
2554 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002555 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2556 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2557 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2558 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2559 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002560
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002561 public abstract long getNetworkActivityBytes(int type, int which);
2562 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002563
Adam Lesinskie08af192015-03-25 16:42:59 -07002564 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002565 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002566 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002567 * actual power data.
2568 */
2569 public abstract boolean hasWifiActivityReporting();
2570
2571 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002572 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2573 * in various radio controller states, such as transmit, receive, and idle.
2574 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002575 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002576 public abstract ControllerActivityCounter getWifiControllerActivity();
2577
2578 /**
2579 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2580 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2581 * actual power data.
2582 */
2583 public abstract boolean hasBluetoothActivityReporting();
2584
2585 /**
2586 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2587 * in various radio controller states, such as transmit, receive, and idle.
2588 * @return non-null {@link ControllerActivityCounter}
2589 */
2590 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2591
2592 /**
2593 * Returns true if the BatteryStats object has detailed modem power reports.
2594 * When true, calling {@link #getModemControllerActivity()} will yield the
2595 * actual power data.
2596 */
2597 public abstract boolean hasModemActivityReporting();
2598
2599 /**
2600 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2601 * in various radio controller states, such as transmit, receive, and idle.
2602 * @return non-null {@link ControllerActivityCounter}
2603 */
2604 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002605
The Android Open Source Project10592532009-03-18 17:39:46 -07002606 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002607 * Return the wall clock time when battery stats data collection started.
2608 */
2609 public abstract long getStartClockTime();
2610
2611 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002612 * Return platform version tag that we were running in when the battery stats started.
2613 */
2614 public abstract String getStartPlatformVersion();
2615
2616 /**
2617 * Return platform version tag that we were running in when the battery stats ended.
2618 */
2619 public abstract String getEndPlatformVersion();
2620
2621 /**
2622 * Return the internal version code of the parcelled format.
2623 */
2624 public abstract int getParcelVersion();
2625
2626 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 * Return whether we are currently running on battery.
2628 */
2629 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 /**
2632 * Returns a SparseArray containing the statistics for each uid.
2633 */
2634 public abstract SparseArray<? extends Uid> getUidStats();
2635
2636 /**
2637 * Returns the current battery uptime in microseconds.
2638 *
2639 * @param curTime the amount of elapsed realtime in microseconds.
2640 */
2641 public abstract long getBatteryUptime(long curTime);
2642
2643 /**
2644 * Returns the current battery realtime in microseconds.
2645 *
2646 * @param curTime the amount of elapsed realtime in microseconds.
2647 */
2648 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002649
The Android Open Source Project10592532009-03-18 17:39:46 -07002650 /**
Evan Millar633a1742009-04-02 16:36:33 -07002651 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002652 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002653 */
Evan Millar633a1742009-04-02 16:36:33 -07002654 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002655
The Android Open Source Project10592532009-03-18 17:39:46 -07002656 /**
Evan Millar633a1742009-04-02 16:36:33 -07002657 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2658 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002659 */
Evan Millar633a1742009-04-02 16:36:33 -07002660 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661
2662 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002663 * Get the amount the battery has discharged since the stats were
2664 * last reset after charging, as a lower-end approximation.
2665 */
2666 public abstract int getLowDischargeAmountSinceCharge();
2667
2668 /**
2669 * Get the amount the battery has discharged since the stats were
2670 * last reset after charging, as an upper-end approximation.
2671 */
2672 public abstract int getHighDischargeAmountSinceCharge();
2673
2674 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002675 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2676 */
2677 public abstract int getDischargeAmount(int which);
2678
2679 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002680 * Get the amount the battery has discharged while the screen was on,
2681 * since the last time power was unplugged.
2682 */
2683 public abstract int getDischargeAmountScreenOn();
2684
2685 /**
2686 * Get the amount the battery has discharged while the screen was on,
2687 * since the last time the device was charged.
2688 */
2689 public abstract int getDischargeAmountScreenOnSinceCharge();
2690
2691 /**
2692 * Get the amount the battery has discharged while the screen was off,
2693 * since the last time power was unplugged.
2694 */
2695 public abstract int getDischargeAmountScreenOff();
2696
2697 /**
2698 * Get the amount the battery has discharged while the screen was off,
2699 * since the last time the device was charged.
2700 */
2701 public abstract int getDischargeAmountScreenOffSinceCharge();
2702
2703 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002704 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002705 * since the last time power was unplugged.
2706 */
2707 public abstract int getDischargeAmountScreenDoze();
2708
2709 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002710 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002711 * since the last time the device was charged.
2712 */
2713 public abstract int getDischargeAmountScreenDozeSinceCharge();
2714
2715 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 * Returns the total, last, or current battery uptime in microseconds.
2717 *
2718 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002719 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 */
2721 public abstract long computeBatteryUptime(long curTime, int which);
2722
2723 /**
2724 * Returns the total, last, or current battery realtime in microseconds.
2725 *
2726 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002727 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 */
2729 public abstract long computeBatteryRealtime(long curTime, int which);
2730
2731 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002732 * Returns the total, last, or current battery screen off/doze uptime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002733 *
2734 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002735 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002736 */
2737 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2738
2739 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002740 * Returns the total, last, or current battery screen off/doze realtime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002741 *
2742 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002743 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002744 */
2745 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2746
2747 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 * Returns the total, last, or current uptime in microseconds.
2749 *
2750 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002751 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 */
2753 public abstract long computeUptime(long curTime, int which);
2754
2755 /**
2756 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002757 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002759 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 */
2761 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002762
2763 /**
2764 * Compute an approximation for how much run time (in microseconds) is remaining on
2765 * the battery. Returns -1 if no time can be computed: either there is not
2766 * enough current data to make a decision, or the battery is currently
2767 * charging.
2768 *
2769 * @param curTime The current elepsed realtime in microseconds.
2770 */
2771 public abstract long computeBatteryTimeRemaining(long curTime);
2772
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002773 // The part of a step duration that is the actual time.
2774 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2775
2776 // Bits in a step duration that are the new battery level we are at.
2777 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002778 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002779
2780 // Bits in a step duration that are the initial mode we were in at that step.
2781 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002782 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002783
2784 // Bits in a step duration that indicate which modes changed during that step.
2785 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002786 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002787
2788 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2789 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2790
Santos Cordone94f0502017-02-24 12:31:20 -08002791 // The largest value for screen state that is tracked in battery states. Any values above
2792 // this should be mapped back to one of the tracked values before being tracked here.
2793 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2794
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002795 // Step duration mode: power save is on.
2796 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2797
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002798 // Step duration mode: device is currently in idle mode.
2799 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2800
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002801 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2802 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002803 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2804 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002805 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2806 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2807 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2808 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2809 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002810 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2811 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002812 };
2813 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2814 (Display.STATE_OFF-1),
2815 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002816 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002817 (Display.STATE_ON-1),
2818 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2819 (Display.STATE_DOZE-1),
2820 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2821 (Display.STATE_DOZE_SUSPEND-1),
2822 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002823 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002824 };
2825 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2826 "screen off",
2827 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002828 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002829 "screen on",
2830 "screen on power save",
2831 "screen doze",
2832 "screen doze power save",
2833 "screen doze-suspend",
2834 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002835 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002836 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002837
2838 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002839 * Return the amount of battery discharge while the screen was off, measured in
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002840 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2841 * a coulomb counter.
2842 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002843 public abstract long getUahDischargeScreenOff(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002844
2845 /**
2846 * Return the amount of battery discharge while the screen was in doze mode, measured in
2847 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2848 * a coulomb counter.
2849 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002850 public abstract long getUahDischargeScreenDoze(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002851
2852 /**
2853 * Return the amount of battery discharge measured in micro-Ampere-hours. This will be
2854 * non-zero only if the device's battery has a coulomb counter.
2855 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002856 public abstract long getUahDischarge(int which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002857
2858 /**
Mike Ma15313c92017-11-15 17:58:21 -08002859 * @return the amount of battery discharge while the device is in light idle mode, measured in
2860 * micro-Ampere-hours.
2861 */
2862 public abstract long getUahDischargeLightDoze(int which);
2863
2864 /**
2865 * @return the amount of battery discharge while the device is in deep idle mode, measured in
2866 * micro-Ampere-hours.
2867 */
2868 public abstract long getUahDischargeDeepDoze(int which);
2869
2870 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002871 * Returns the estimated real battery capacity, which may be less than the capacity
2872 * declared by the PowerProfile.
2873 * @return The estimated battery capacity in mAh.
2874 */
2875 public abstract int getEstimatedBatteryCapacity();
2876
2877 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002878 * @return The minimum learned battery capacity in uAh.
2879 */
2880 public abstract int getMinLearnedBatteryCapacity();
2881
2882 /**
2883 * @return The maximum learned battery capacity in uAh.
2884 */
2885 public abstract int getMaxLearnedBatteryCapacity() ;
2886
2887 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002888 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002889 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002890 public abstract LevelStepTracker getDischargeLevelStepTracker();
2891
2892 /**
2893 * Return the array of daily discharge step durations.
2894 */
2895 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002896
2897 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002898 * Compute an approximation for how much time (in microseconds) remains until the battery
2899 * is fully charged. Returns -1 if no time can be computed: either there is not
2900 * enough current data to make a decision, or the battery is currently
2901 * discharging.
2902 *
2903 * @param curTime The current elepsed realtime in microseconds.
2904 */
2905 public abstract long computeChargeTimeRemaining(long curTime);
2906
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002907 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002908 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002909 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002910 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002911
2912 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002913 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002914 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002915 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002916
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002917 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2918
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002919 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002920
Evan Millarc64edde2009-04-18 12:26:32 -07002921 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922
Bookatz50df7112017-08-04 14:53:26 -07002923 /**
2924 * Returns Timers tracking the total time of each Resource Power Manager state and voter.
2925 */
2926 public abstract Map<String, ? extends Timer> getRpmStats();
2927 /**
2928 * Returns Timers tracking the screen-off time of each Resource Power Manager state and voter.
2929 */
2930 public abstract Map<String, ? extends Timer> getScreenOffRpmStats();
2931
2932
James Carr2dd7e5e2016-07-20 18:48:39 -07002933 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2934
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002935 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2936
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002937 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 long days = seconds / (60 * 60 * 24);
2939 if (days != 0) {
2940 out.append(days);
2941 out.append("d ");
2942 }
2943 long used = days * 60 * 60 * 24;
2944
2945 long hours = (seconds - used) / (60 * 60);
2946 if (hours != 0 || used != 0) {
2947 out.append(hours);
2948 out.append("h ");
2949 }
2950 used += hours * 60 * 60;
2951
2952 long mins = (seconds-used) / 60;
2953 if (mins != 0 || used != 0) {
2954 out.append(mins);
2955 out.append("m ");
2956 }
2957 used += mins * 60;
2958
2959 if (seconds != 0 || used != 0) {
2960 out.append(seconds-used);
2961 out.append("s ");
2962 }
2963 }
2964
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002965 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002967 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 sb.append(time - (sec * 1000));
2969 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 }
2971
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002972 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002973 long sec = time / 1000;
2974 formatTimeRaw(sb, sec);
2975 sb.append(time - (sec * 1000));
2976 sb.append("ms");
2977 }
2978
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002979 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002981 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 }
2983 float perc = ((float)num) / ((float)den) * 100;
2984 mFormatBuilder.setLength(0);
2985 mFormatter.format("%.1f%%", perc);
2986 return mFormatBuilder.toString();
2987 }
2988
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002989 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002990 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07002991
Evan Millar22ac0432009-03-31 11:33:18 -07002992 if (bytes < BYTES_PER_KB) {
2993 return bytes + "B";
2994 } else if (bytes < BYTES_PER_MB) {
2995 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2996 return mFormatBuilder.toString();
2997 } else if (bytes < BYTES_PER_GB){
2998 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2999 return mFormatBuilder.toString();
3000 } else {
3001 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
3002 return mFormatBuilder.toString();
3003 }
3004 }
3005
Kweku Adams103351f2017-10-16 14:39:34 -07003006 private static long roundUsToMs(long timeUs) {
3007 return (timeUs + 500) / 1000;
3008 }
3009
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003010 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003011 if (timer != null) {
3012 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003013 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003014 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
3015 return totalTimeMillis;
3016 }
3017 return 0;
3018 }
3019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 /**
3021 *
3022 * @param sb a StringBuilder object.
3023 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003024 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003026 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 * @param linePrefix a String to be prepended to each line of output.
3028 * @return the line prefix
3029 */
3030 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003031 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003034 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003035
Evan Millarc64edde2009-04-18 12:26:32 -07003036 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037 if (totalTimeMillis != 0) {
3038 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003039 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08003040 if (name != null) {
3041 sb.append(name);
3042 sb.append(' ');
3043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 sb.append('(');
3045 sb.append(count);
3046 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003047 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
3048 if (maxDurationMs >= 0) {
3049 sb.append(" max=");
3050 sb.append(maxDurationMs);
3051 }
Bookatz506a8182017-05-01 14:18:42 -07003052 // Put actual time if it is available and different from totalTimeMillis.
3053 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
3054 if (totalDurMs > totalTimeMillis) {
3055 sb.append(" actual=");
3056 sb.append(totalDurMs);
3057 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07003058 if (timer.isRunningLocked()) {
3059 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3060 if (currentMs >= 0) {
3061 sb.append(" (running for ");
3062 sb.append(currentMs);
3063 sb.append("ms)");
3064 } else {
3065 sb.append(" (running)");
3066 }
3067 }
3068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 return ", ";
3070 }
3071 }
3072 return linePrefix;
3073 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003074
3075 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07003076 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003077 *
3078 * @param pw a PrintWriter object to print to.
3079 * @param sb a StringBuilder object.
3080 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08003081 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003082 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
3083 * @param prefix a String to be prepended to each line of output.
3084 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07003085 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003086 */
3087 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07003088 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003089 if (timer != null) {
3090 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07003091 final long totalTimeMs = (timer.getTotalTimeLocked(
3092 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003093 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003094 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003095 sb.setLength(0);
3096 sb.append(prefix);
3097 sb.append(" ");
3098 sb.append(type);
3099 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003100 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003101 sb.append("realtime (");
3102 sb.append(count);
3103 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003104 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
3105 if (maxDurationMs >= 0) {
3106 sb.append(" max=");
3107 sb.append(maxDurationMs);
3108 }
3109 if (timer.isRunningLocked()) {
3110 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
3111 if (currentMs >= 0) {
3112 sb.append(" (running for ");
3113 sb.append(currentMs);
3114 sb.append("ms)");
3115 } else {
3116 sb.append(" (running)");
3117 }
3118 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003119 pw.println(sb.toString());
3120 return true;
3121 }
3122 }
3123 return false;
3124 }
Bookatzc8c44962017-05-11 12:12:54 -07003125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 /**
3127 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07003128 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003129 * @param sb a StringBuilder object.
3130 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003131 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003133 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003134 * @param linePrefix a String to be prepended to each line of output.
3135 * @return the line prefix
3136 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003137 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
3138 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 long totalTimeMicros = 0;
3140 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07003141 long max = 0;
3142 long current = 0;
3143 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003145 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07003146 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003147 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3148 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07003149 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 }
3151 sb.append(linePrefix);
3152 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
3153 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07003154 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003155 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003156 sb.append(',');
3157 sb.append(current);
3158 sb.append(',');
3159 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07003160 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
3161 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
3162 // totalDuration independent of totalTimeMicros (since they are not pooled).
3163 if (name != null) {
3164 sb.append(',');
3165 sb.append(totalDuration);
3166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 return ",";
3168 }
Bookatz506a8182017-05-01 14:18:42 -07003169
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003170 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
3171 String type) {
3172 pw.print(BATTERY_STATS_CHECKIN_VERSION);
3173 pw.print(',');
3174 pw.print(uid);
3175 pw.print(',');
3176 pw.print(category);
3177 pw.print(',');
3178 pw.print(type);
3179 }
3180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 /**
3182 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07003183 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 * @param pw the PageWriter to dump log to
3185 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3186 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3187 * @param args type-dependent data arguments
3188 */
Bookatzc8c44962017-05-11 12:12:54 -07003189 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003190 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003191 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003192 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003193 pw.print(',');
3194 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003195 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003196 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07003198
3199 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003200 * Dump a given timer stat for terse checkin mode.
3201 *
3202 * @param pw the PageWriter to dump log to
3203 * @param uid the UID to log
3204 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3205 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3206 * @param timer a {@link Timer} to dump stats for
3207 * @param rawRealtime the current elapsed realtime of the system in microseconds
3208 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3209 */
3210 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
3211 Timer timer, long rawRealtime, int which) {
3212 if (timer != null) {
3213 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003214 final long totalTime = roundUsToMs(timer.getTotalTimeLocked(rawRealtime, which));
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003215 final int count = timer.getCountLocked(which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003216 if (totalTime != 0 || count != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003217 dumpLine(pw, uid, category, type, totalTime, count);
3218 }
3219 }
3220 }
3221
3222 /**
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003223 * Dump a given timer stat to the proto stream.
3224 *
3225 * @param proto the ProtoOutputStream to log to
3226 * @param fieldId type of data, the field to save to (e.g. AggregatedBatteryStats.WAKELOCK)
3227 * @param timer a {@link Timer} to dump stats for
3228 * @param rawRealtimeUs the current elapsed realtime of the system in microseconds
3229 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3230 */
3231 private static void dumpTimer(ProtoOutputStream proto, long fieldId,
Kweku Adams87b19ec2017-10-09 12:40:03 -07003232 Timer timer, long rawRealtimeUs, int which) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003233 if (timer == null) {
3234 return;
3235 }
3236 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003237 final long timeMs = roundUsToMs(timer.getTotalTimeLocked(rawRealtimeUs, which));
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003238 final int count = timer.getCountLocked(which);
Kweku Adams103351f2017-10-16 14:39:34 -07003239 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs / 1000);
3240 final long curDurationMs = timer.getCurrentDurationMsLocked(rawRealtimeUs / 1000);
3241 final long totalDurationMs = timer.getTotalDurationMsLocked(rawRealtimeUs / 1000);
3242 if (timeMs != 0 || count != 0 || maxDurationMs != -1 || curDurationMs != -1
3243 || totalDurationMs != -1) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003244 final long token = proto.start(fieldId);
Kweku Adams103351f2017-10-16 14:39:34 -07003245 proto.write(TimerProto.DURATION_MS, timeMs);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003246 proto.write(TimerProto.COUNT, count);
Kweku Adams103351f2017-10-16 14:39:34 -07003247 // These values will be -1 for timers that don't implement the functionality.
3248 if (maxDurationMs != -1) {
3249 proto.write(TimerProto.MAX_DURATION_MS, maxDurationMs);
3250 }
3251 if (curDurationMs != -1) {
3252 proto.write(TimerProto.CURRENT_DURATION_MS, curDurationMs);
3253 }
3254 if (totalDurationMs != -1) {
3255 proto.write(TimerProto.TOTAL_DURATION_MS, totalDurationMs);
3256 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003257 proto.end(token);
3258 }
3259 }
3260
3261 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003262 * Checks if the ControllerActivityCounter has any data worth dumping.
3263 */
3264 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
3265 if (counter == null) {
3266 return false;
3267 }
3268
3269 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
3270 || counter.getRxTimeCounter().getCountLocked(which) != 0
3271 || counter.getPowerCounter().getCountLocked(which) != 0) {
3272 return true;
3273 }
3274
3275 for (LongCounter c : counter.getTxTimeCounters()) {
3276 if (c.getCountLocked(which) != 0) {
3277 return true;
3278 }
3279 }
3280 return false;
3281 }
3282
3283 /**
3284 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3285 * The order of the arguments in the final check in line is:
3286 *
3287 * idle, rx, power, tx...
3288 *
3289 * where tx... is one or more transmit level times.
3290 */
3291 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
3292 String type,
3293 ControllerActivityCounter counter,
3294 int which) {
3295 if (!controllerActivityHasData(counter, which)) {
3296 return;
3297 }
3298
3299 dumpLineHeader(pw, uid, category, type);
3300 pw.print(",");
3301 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
3302 pw.print(",");
3303 pw.print(counter.getRxTimeCounter().getCountLocked(which));
3304 pw.print(",");
3305 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3306 for (LongCounter c : counter.getTxTimeCounters()) {
3307 pw.print(",");
3308 pw.print(c.getCountLocked(which));
3309 }
3310 pw.println();
3311 }
3312
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003313 /**
3314 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3315 */
3316 private static void dumpControllerActivityProto(ProtoOutputStream proto, long fieldId,
3317 ControllerActivityCounter counter,
3318 int which) {
3319 if (!controllerActivityHasData(counter, which)) {
3320 return;
3321 }
3322
3323 final long cToken = proto.start(fieldId);
3324
3325 proto.write(ControllerActivityProto.IDLE_DURATION_MS,
3326 counter.getIdleTimeCounter().getCountLocked(which));
3327 proto.write(ControllerActivityProto.RX_DURATION_MS,
3328 counter.getRxTimeCounter().getCountLocked(which));
3329 proto.write(ControllerActivityProto.POWER_MAH,
3330 counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3331
3332 long tToken;
3333 LongCounter[] txCounters = counter.getTxTimeCounters();
3334 for (int i = 0; i < txCounters.length; ++i) {
3335 LongCounter c = txCounters[i];
3336 tToken = proto.start(ControllerActivityProto.TX);
3337 proto.write(ControllerActivityProto.TxLevel.LEVEL, i);
3338 proto.write(ControllerActivityProto.TxLevel.DURATION_MS, c.getCountLocked(which));
3339 proto.end(tToken);
3340 }
3341
3342 proto.end(cToken);
3343 }
3344
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003345 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
3346 String prefix, String controllerName,
3347 ControllerActivityCounter counter,
3348 int which) {
3349 if (controllerActivityHasData(counter, which)) {
3350 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
3351 }
3352 }
3353
3354 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
3355 String controllerName,
3356 ControllerActivityCounter counter, int which) {
3357 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
3358 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
3359 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003360 // Battery real time
3361 final long totalControllerActivityTimeMs
3362 = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003363 long totalTxTimeMs = 0;
3364 for (LongCounter txState : counter.getTxTimeCounters()) {
3365 totalTxTimeMs += txState.getCountLocked(which);
3366 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003367 final long sleepTimeMs
3368 = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + totalTxTimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003369
Siddharth Rayb50a6842017-12-14 15:15:28 -08003370 if (controllerName.equals(WIFI_CONTROLLER_NAME)) {
3371 final long scanTimeMs = counter.getScanTimeCounter().getCountLocked(which);
3372 sb.setLength(0);
3373 sb.append(prefix);
3374 sb.append(" ");
3375 sb.append(controllerName);
3376 sb.append(" Scan time: ");
3377 formatTimeMs(sb, scanTimeMs);
3378 sb.append("(");
3379 sb.append(formatRatioLocked(scanTimeMs, totalControllerActivityTimeMs));
3380 sb.append(")");
3381 pw.println(sb.toString());
3382 }
3383
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003384 sb.setLength(0);
3385 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003386 sb.append(" ");
3387 sb.append(controllerName);
3388 sb.append(" Sleep time: ");
3389 formatTimeMs(sb, sleepTimeMs);
3390 sb.append("(");
3391 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3392 sb.append(")");
3393 pw.println(sb.toString());
3394
3395 sb.setLength(0);
3396 sb.append(prefix);
3397 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003398 sb.append(controllerName);
3399 sb.append(" Idle time: ");
3400 formatTimeMs(sb, idleTimeMs);
3401 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003402 sb.append(formatRatioLocked(idleTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003403 sb.append(")");
3404 pw.println(sb.toString());
3405
3406 sb.setLength(0);
3407 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003408 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003409 sb.append(controllerName);
3410 sb.append(" Rx time: ");
3411 formatTimeMs(sb, rxTimeMs);
3412 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003413 sb.append(formatRatioLocked(rxTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003414 sb.append(")");
3415 pw.println(sb.toString());
3416
3417 sb.setLength(0);
3418 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003419 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003420 sb.append(controllerName);
3421 sb.append(" Tx time: ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003422
Siddharth Ray3c648c42017-10-02 17:30:58 -07003423 String [] powerLevel;
3424 switch(controllerName) {
Siddharth Rayb50a6842017-12-14 15:15:28 -08003425 case CELLULAR_CONTROLLER_NAME:
Siddharth Ray3c648c42017-10-02 17:30:58 -07003426 powerLevel = new String[] {
3427 " less than 0dBm: ",
3428 " 0dBm to 8dBm: ",
3429 " 8dBm to 15dBm: ",
3430 " 15dBm to 20dBm: ",
3431 " above 20dBm: "};
3432 break;
3433 default:
3434 powerLevel = new String[] {"[0]", "[1]", "[2]", "[3]", "[4]"};
3435 break;
3436 }
3437 final int numTxLvls = Math.min(counter.getTxTimeCounters().length, powerLevel.length);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003438 if (numTxLvls > 1) {
Siddharth Ray3c648c42017-10-02 17:30:58 -07003439 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003440 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3441 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3442 sb.setLength(0);
3443 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003444 sb.append(" ");
3445 sb.append(powerLevel[lvl]);
3446 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003447 formatTimeMs(sb, txLvlTimeMs);
3448 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003449 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003450 sb.append(")");
3451 pw.println(sb.toString());
3452 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003453 } else {
3454 final long txLvlTimeMs = counter.getTxTimeCounters()[0].getCountLocked(which);
3455 formatTimeMs(sb, txLvlTimeMs);
3456 sb.append("(");
3457 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
3458 sb.append(")");
3459 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003460 }
3461
Siddharth Ray3c648c42017-10-02 17:30:58 -07003462 if (powerDrainMaMs > 0) {
3463 sb.setLength(0);
3464 sb.append(prefix);
3465 sb.append(" ");
3466 sb.append(controllerName);
3467 sb.append(" Battery drain: ").append(
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003468 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
Siddharth Ray3c648c42017-10-02 17:30:58 -07003469 sb.append("mAh");
3470 pw.println(sb.toString());
3471 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003472 }
3473
3474 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003475 * Temporary for settings.
3476 */
3477 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3478 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3479 }
3480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 /**
3482 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003483 *
Kweku Adams87b19ec2017-10-09 12:40:03 -07003484 * NOTE: all times are expressed in microseconds, unless specified otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003485 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003486 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3487 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 final long rawUptime = SystemClock.uptimeMillis() * 1000;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003489 final long rawRealtimeMs = SystemClock.elapsedRealtime();
3490 final long rawRealtime = rawRealtimeMs * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3493 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003494 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3495 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3496 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 final long totalRealtime = computeRealtime(rawRealtime, which);
3498 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003499 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Mike Mac2f518a2017-09-19 16:06:03 -07003500 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003501 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003502 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003503 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3504 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003505 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003506 rawRealtime, which);
3507 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3508 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003509 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003510 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003511 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003512 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003513 final long dischargeCount = getUahDischarge(which);
3514 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
3515 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Ma15313c92017-11-15 17:58:21 -08003516 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
3517 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003518
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003519 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003520
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003521 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003522 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003523
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003524 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003527 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003528 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003529 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003530 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003531 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003532 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003533 getEstimatedBatteryCapacity(),
Mike Mac2f518a2017-09-19 16:06:03 -07003534 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity(),
3535 screenDozeTime / 1000);
Adam Lesinski67c134f2016-06-10 15:15:08 -07003536
Bookatzc8c44962017-05-11 12:12:54 -07003537
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003538 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003539 long fullWakeLockTimeTotal = 0;
3540 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003541
Evan Millar22ac0432009-03-31 11:33:18 -07003542 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003543 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003544
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003545 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3546 = u.getWakelockStats();
3547 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3548 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003549
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003550 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3551 if (fullWakeTimer != null) {
3552 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3553 which);
3554 }
3555
3556 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3557 if (partialWakeTimer != null) {
3558 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3559 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003560 }
3561 }
3562 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003563
3564 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003565 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3566 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3567 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3568 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3569 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3570 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3571 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3572 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003573 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3574 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003575 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3576 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003577 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3578 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003579
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003580 // Dump Modem controller stats
3581 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3582 getModemControllerActivity(), which);
3583
Adam Lesinskie283d332015-04-16 12:29:25 -07003584 // Dump Wifi controller stats
3585 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3586 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003587 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003588 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003589
3590 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3591 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003592
3593 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003594 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3595 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003597 // Dump misc stats
3598 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003599 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003600 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003601 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003602 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003603 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003604 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3605 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003606 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003607 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3608 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3609 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3610 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003611 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003612
Dianne Hackborn617f8772009-03-31 15:04:46 -07003613 // Dump screen brightness stats
3614 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3615 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003616 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003617 }
3618 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003619
Dianne Hackborn627bba72009-03-24 22:32:56 -07003620 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003621 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3622 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003623 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003624 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003625 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003626 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003627 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003628 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003629 args[i] = getPhoneSignalStrengthCount(i, which);
3630 }
3631 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003632
Dianne Hackborn627bba72009-03-24 22:32:56 -07003633 // Dump network type stats
3634 args = new Object[NUM_DATA_CONNECTION_TYPES];
3635 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003636 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003637 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003638 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3639 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3640 args[i] = getPhoneDataConnectionCount(i, which);
3641 }
3642 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003643
3644 // Dump wifi state stats
3645 args = new Object[NUM_WIFI_STATES];
3646 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003647 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003648 }
3649 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3650 for (int i=0; i<NUM_WIFI_STATES; i++) {
3651 args[i] = getWifiStateCount(i, which);
3652 }
3653 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3654
Dianne Hackborn3251b902014-06-20 14:40:53 -07003655 // Dump wifi suppl state stats
3656 args = new Object[NUM_WIFI_SUPPL_STATES];
3657 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3658 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3659 }
3660 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3661 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3662 args[i] = getWifiSupplStateCount(i, which);
3663 }
3664 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3665
3666 // Dump wifi signal strength stats
3667 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3668 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3669 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3670 }
3671 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3672 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3673 args[i] = getWifiSignalStrengthCount(i, which);
3674 }
3675 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3676
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003677 // Dump Multicast total stats
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003678 final long multicastWakeLockTimeTotalMicros =
3679 getWifiMulticastWakelockTime(rawRealtime, which);
3680 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003681 dumpLine(pw, 0 /* uid */, category, WIFI_MULTICAST_TOTAL_DATA,
3682 multicastWakeLockTimeTotalMicros / 1000,
3683 multicastWakeLockCountTotal);
3684
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003685 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003686 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003687 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003688 }
Bookatzc8c44962017-05-11 12:12:54 -07003689
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003690 if (which == STATS_SINCE_UNPLUGGED) {
3691 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3692 getDischargeStartLevel()-getDischargeCurrentLevel(),
3693 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003694 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
Mike Mac2f518a2017-09-19 16:06:03 -07003695 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003696 getDischargeAmountScreenDoze(), dischargeScreenDozeCount / 1000,
3697 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003698 } else {
3699 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3700 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003701 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003702 getDischargeAmountScreenOffSinceCharge(),
Mike Mac2f518a2017-09-19 16:06:03 -07003703 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003704 getDischargeAmountScreenDozeSinceCharge(), dischargeScreenDozeCount / 1000,
3705 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003706 }
Bookatzc8c44962017-05-11 12:12:54 -07003707
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003708 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003709 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003710 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003711 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003712 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003713 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003714 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3715 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003716 }
Evan Millarc64edde2009-04-18 12:26:32 -07003717 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003718 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003719 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003720 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3721 // Not doing the regular wake lock formatting to remain compatible
3722 // with the old checkin format.
3723 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3724 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003725 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003726 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003727 }
3728 }
Evan Millarc64edde2009-04-18 12:26:32 -07003729 }
Bookatzc8c44962017-05-11 12:12:54 -07003730
Bookatz50df7112017-08-04 14:53:26 -07003731 final Map<String, ? extends Timer> rpmStats = getRpmStats();
3732 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
3733 if (rpmStats.size() > 0) {
3734 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
3735 sb.setLength(0);
3736 Timer totalTimer = ent.getValue();
3737 long timeMs = (totalTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3738 int count = totalTimer.getCountLocked(which);
3739 Timer screenOffTimer = screenOffRpmStats.get(ent.getKey());
3740 long screenOffTimeMs = screenOffTimer != null
3741 ? (screenOffTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3742 int screenOffCount = screenOffTimer != null
3743 ? screenOffTimer.getCountLocked(which) : 0;
Bookatz82b341172017-09-07 19:06:08 -07003744 if (SCREEN_OFF_RPM_STATS_ENABLED) {
3745 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3746 "\"" + ent.getKey() + "\"", timeMs, count, screenOffTimeMs,
3747 screenOffCount);
3748 } else {
3749 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3750 "\"" + ent.getKey() + "\"", timeMs, count);
3751 }
Bookatz50df7112017-08-04 14:53:26 -07003752 }
3753 }
3754
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003755 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003756 helper.create(this);
3757 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003758 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003759 if (sippers != null && sippers.size() > 0) {
3760 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3761 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003762 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003763 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3764 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
Kweku Adams87b19ec2017-10-09 12:40:03 -07003765 int uid = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003766 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003767 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003768 String label;
3769 switch (bs.drainType) {
3770 case IDLE:
3771 label="idle";
3772 break;
3773 case CELL:
3774 label="cell";
3775 break;
3776 case PHONE:
3777 label="phone";
3778 break;
3779 case WIFI:
3780 label="wifi";
3781 break;
3782 case BLUETOOTH:
3783 label="blue";
3784 break;
3785 case SCREEN:
3786 label="scrn";
3787 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003788 case FLASHLIGHT:
3789 label="flashlight";
3790 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003791 case APP:
3792 uid = bs.uidObj.getUid();
3793 label = "uid";
3794 break;
3795 case USER:
3796 uid = UserHandle.getUid(bs.userId, 0);
3797 label = "user";
3798 break;
3799 case UNACCOUNTED:
3800 label = "unacc";
3801 break;
3802 case OVERCOUNTED:
3803 label = "over";
3804 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003805 case CAMERA:
3806 label = "camera";
3807 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003808 case MEMORY:
3809 label = "memory";
3810 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003811 default:
3812 label = "???";
3813 }
3814 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07003815 BatteryStatsHelper.makemAh(bs.totalPowerMah),
3816 bs.shouldHide ? 1 : 0,
3817 BatteryStatsHelper.makemAh(bs.screenPowerMah),
3818 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003819 }
3820 }
3821
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003822 final long[] cpuFreqs = getCpuFreqs();
3823 if (cpuFreqs != null) {
3824 sb.setLength(0);
3825 for (int i = 0; i < cpuFreqs.length; ++i) {
3826 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3827 }
3828 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3829 }
3830
Kweku Adams87b19ec2017-10-09 12:40:03 -07003831 // Dump stats per UID.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003832 for (int iu = 0; iu < NU; iu++) {
3833 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003834 if (reqUid >= 0 && uid != reqUid) {
3835 continue;
3836 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003837 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003840 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3841 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3842 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3843 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3844 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3845 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3846 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3847 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003848 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003849 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3850 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003851 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003852 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3853 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003854 // Background data transfers
3855 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3856 which);
3857 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3858 which);
3859 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3860 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3861 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3862 which);
3863 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3864 which);
3865 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3866 which);
3867 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3868 which);
3869
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003870 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3871 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003872 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003873 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3874 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3875 || wifiBytesBgTx > 0
3876 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3877 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003878 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3879 wifiBytesRx, wifiBytesTx,
3880 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003881 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003882 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003883 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3884 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3885 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3886 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003887 }
3888
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003889 // Dump modem controller data, per UID.
3890 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3891 u.getModemControllerActivity(), which);
3892
3893 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003894 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3895 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3896 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003897 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3898 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003899 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3900 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3901 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003902 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003903 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003904 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3905 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003906 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3907 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003908 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003909 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003911
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003912 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3913 u.getWifiControllerActivity(), which);
3914
Bookatz867c0d72017-03-07 18:23:42 -08003915 final Timer bleTimer = u.getBluetoothScanTimer();
3916 if (bleTimer != null) {
3917 // Convert from microseconds to milliseconds with rounding
3918 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3919 / 1000;
3920 if (totalTime != 0) {
3921 final int count = bleTimer.getCountLocked(which);
3922 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3923 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003924 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3925 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3926 final long actualTimeBg = bleTimerBg != null ?
3927 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003928 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003929 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3930 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003931 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3932 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3933 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3934 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3935 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
3936 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3937 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
3938 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3939 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
3940 final Timer unoptimizedScanTimerBg =
3941 u.getBluetoothUnoptimizedScanBackgroundTimer();
3942 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
3943 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3944 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
3945 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3946
Bookatz867c0d72017-03-07 18:23:42 -08003947 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07003948 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
3949 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
3950 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08003951 }
3952 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003953
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003954 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3955 u.getBluetoothControllerActivity(), which);
3956
Dianne Hackborn617f8772009-03-31 15:04:46 -07003957 if (u.hasUserActivity()) {
3958 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3959 boolean hasData = false;
3960 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3961 int val = u.getUserActivityCount(i, which);
3962 args[i] = val;
3963 if (val != 0) hasData = true;
3964 }
3965 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003966 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003967 }
3968 }
Bookatzc8c44962017-05-11 12:12:54 -07003969
3970 if (u.getAggregatedPartialWakelockTimer() != null) {
3971 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07003972 // Times are since reset (regardless of 'which')
3973 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07003974 final Timer bgTimer = timer.getSubTimer();
3975 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003976 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07003977 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
3978 }
3979
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003980 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3981 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3982 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3983 String linePrefix = "";
3984 sb.setLength(0);
3985 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3986 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003987 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3988 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003989 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003990 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
3991 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003992 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3993 rawRealtime, "w", which, linePrefix);
3994
Kweku Adams103351f2017-10-16 14:39:34 -07003995 // Only log if we had at least one wakelock...
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003996 if (sb.length() > 0) {
3997 String name = wakelocks.keyAt(iw);
3998 if (name.indexOf(',') >= 0) {
3999 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004000 }
Yi Jin02483362017-08-04 11:30:44 -07004001 if (name.indexOf('\n') >= 0) {
4002 name = name.replace('\n', '_');
4003 }
4004 if (name.indexOf('\r') >= 0) {
4005 name = name.replace('\r', '_');
4006 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004007 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004008 }
4009 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004010
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004011 // WiFi Multicast Wakelock Statistics
4012 final Timer mcTimer = u.getMulticastWakelockStats();
4013 if (mcTimer != null) {
4014 final long totalMcWakelockTimeMs =
4015 mcTimer.getTotalTimeLocked(rawRealtime, which) / 1000 ;
4016 final int countMcWakelock = mcTimer.getCountLocked(which);
4017 if(totalMcWakelockTimeMs > 0) {
4018 dumpLine(pw, uid, category, WIFI_MULTICAST_DATA,
4019 totalMcWakelockTimeMs, countMcWakelock);
4020 }
4021 }
4022
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004023 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4024 for (int isy=syncs.size()-1; isy>=0; isy--) {
4025 final Timer timer = syncs.valueAt(isy);
4026 // Convert from microseconds to milliseconds with rounding
4027 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4028 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07004029 final Timer bgTimer = timer.getSubTimer();
4030 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004031 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07004032 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004033 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004034 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07004035 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004036 }
4037 }
4038
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004039 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4040 for (int ij=jobs.size()-1; ij>=0; ij--) {
4041 final Timer timer = jobs.valueAt(ij);
4042 // Convert from microseconds to milliseconds with rounding
4043 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4044 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07004045 final Timer bgTimer = timer.getSubTimer();
4046 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004047 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07004048 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004049 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004050 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07004051 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004052 }
4053 }
4054
Dianne Hackborn94326cb2017-06-28 16:17:20 -07004055 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
4056 for (int ic=completions.size()-1; ic>=0; ic--) {
4057 SparseIntArray types = completions.valueAt(ic);
4058 if (types != null) {
4059 dumpLine(pw, uid, category, JOB_COMPLETION_DATA,
4060 "\"" + completions.keyAt(ic) + "\"",
4061 types.get(JobParameters.REASON_CANCELED, 0),
4062 types.get(JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, 0),
4063 types.get(JobParameters.REASON_PREEMPT, 0),
4064 types.get(JobParameters.REASON_TIMEOUT, 0),
4065 types.get(JobParameters.REASON_DEVICE_IDLE, 0));
4066 }
4067 }
4068
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004069 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
4070 rawRealtime, which);
4071 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
4072 rawRealtime, which);
4073 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
4074 rawRealtime, which);
4075 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
4076 rawRealtime, which);
4077
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004078 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4079 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004080 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004081 final Uid.Sensor se = sensors.valueAt(ise);
4082 final int sensorNumber = sensors.keyAt(ise);
4083 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004084 if (timer != null) {
4085 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004086 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4087 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004088 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004089 final int count = timer.getCountLocked(which);
4090 final Timer bgTimer = se.getSensorBackgroundTime();
4091 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004092 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4093 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4094 final long bgActualTime = bgTimer != null ?
4095 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4096 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
4097 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004098 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004099 }
4100 }
4101
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004102 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
4103 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004104
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07004105 dumpTimer(pw, uid, category, FOREGROUND_ACTIVITY_DATA, u.getForegroundActivityTimer(),
4106 rawRealtime, which);
4107
4108 dumpTimer(pw, uid, category, FOREGROUND_SERVICE_DATA, u.getForegroundServiceTimer(),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004109 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004110
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004111 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07004112 long totalStateTime = 0;
4113 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08004114 final long time = u.getProcessStateTime(ips, rawRealtime, which);
4115 totalStateTime += time;
4116 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004117 }
4118 if (totalStateTime > 0) {
4119 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
4120 }
4121
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004122 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4123 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004124 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004125 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004126 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004127 }
4128
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004129 // If the cpuFreqs is null, then don't bother checking for cpu freq times.
4130 if (cpuFreqs != null) {
4131 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
4132 // If total cpuFreqTimes is null, then we don't need to check for
4133 // screenOffCpuFreqTimes.
4134 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
4135 sb.setLength(0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004136 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004137 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004138 }
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004139 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
4140 if (screenOffCpuFreqTimeMs != null) {
4141 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
4142 sb.append("," + screenOffCpuFreqTimeMs[i]);
4143 }
4144 } else {
4145 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
4146 sb.append(",0");
4147 }
4148 }
4149 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
4150 cpuFreqTimeMs.length, sb.toString());
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004151 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -08004152
4153 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
4154 final long[] timesMs = u.getCpuFreqTimes(which, procState);
4155 if (timesMs != null && timesMs.length == cpuFreqs.length) {
4156 sb.setLength(0);
4157 for (int i = 0; i < timesMs.length; ++i) {
4158 sb.append((i == 0 ? "" : ",") + timesMs[i]);
4159 }
4160 final long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(
4161 which, procState);
4162 if (screenOffTimesMs != null) {
4163 for (int i = 0; i < screenOffTimesMs.length; ++i) {
4164 sb.append("," + screenOffTimesMs[i]);
4165 }
4166 } else {
4167 for (int i = 0; i < timesMs.length; ++i) {
4168 sb.append(",0");
4169 }
4170 }
4171 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA,
4172 Uid.UID_PROCESS_TYPES[procState], timesMs.length, sb.toString());
4173 }
4174 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004175 }
4176
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004177 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4178 = u.getProcessStats();
4179 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4180 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004181
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004182 final long userMillis = ps.getUserTime(which);
4183 final long systemMillis = ps.getSystemTime(which);
4184 final long foregroundMillis = ps.getForegroundTime(which);
4185 final int starts = ps.getStarts(which);
4186 final int numCrashes = ps.getNumCrashes(which);
4187 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004188
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004189 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
4190 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004191 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
4192 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 }
4194 }
4195
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004196 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4197 = u.getPackageStats();
4198 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4199 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4200 int wakeups = 0;
4201 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4202 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07004203 int count = alarms.valueAt(iwa).getCountLocked(which);
4204 wakeups += count;
4205 String name = alarms.keyAt(iwa).replace(',', '_');
4206 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004207 }
4208 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4209 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4210 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4211 final long startTime = ss.getStartTime(batteryUptime, which);
4212 final int starts = ss.getStarts(which);
4213 final int launches = ss.getLaunches(which);
4214 if (startTime != 0 || starts != 0 || launches != 0) {
4215 dumpLine(pw, uid, category, APK_DATA,
4216 wakeups, // wakeup alarms
4217 packageStats.keyAt(ipkg), // Apk
4218 serviceStats.keyAt(isvc), // service
4219 startTime / 1000, // time spent started, in ms
4220 starts,
4221 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004222 }
4223 }
4224 }
4225 }
4226 }
4227
Dianne Hackborn81038902012-11-26 17:04:09 -08004228 static final class TimerEntry {
4229 final String mName;
4230 final int mId;
4231 final BatteryStats.Timer mTimer;
4232 final long mTime;
4233 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
4234 mName = name;
4235 mId = id;
4236 mTimer = timer;
4237 mTime = time;
4238 }
4239 }
4240
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004241 private void printmAh(PrintWriter printer, double power) {
4242 printer.print(BatteryStatsHelper.makemAh(power));
4243 }
4244
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004245 private void printmAh(StringBuilder sb, double power) {
4246 sb.append(BatteryStatsHelper.makemAh(power));
4247 }
4248
Dianne Hackbornd953c532014-08-16 18:17:38 -07004249 /**
4250 * Temporary for settings.
4251 */
4252 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
4253 int reqUid) {
4254 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
4255 }
4256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004258 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07004259 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 final long rawUptime = SystemClock.uptimeMillis() * 1000;
4261 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07004262 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004263 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004264
4265 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
4266 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
4267 final long totalRealtime = computeRealtime(rawRealtime, which);
4268 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004269 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
4270 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
4271 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004272 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
4273 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -07004274 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004275
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004276 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07004277
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004278 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07004279 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004280
Adam Lesinskif9b20a92016-06-17 17:30:01 -07004281 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
4282 if (estimatedBatteryCapacity > 0) {
4283 sb.setLength(0);
4284 sb.append(prefix);
4285 sb.append(" Estimated battery capacity: ");
4286 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
4287 sb.append(" mAh");
4288 pw.println(sb.toString());
4289 }
4290
Jocelyn Dangc627d102017-04-14 13:15:14 -07004291 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
4292 if (minLearnedBatteryCapacity > 0) {
4293 sb.setLength(0);
4294 sb.append(prefix);
4295 sb.append(" Min learned battery capacity: ");
4296 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
4297 sb.append(" mAh");
4298 pw.println(sb.toString());
4299 }
4300 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
4301 if (maxLearnedBatteryCapacity > 0) {
4302 sb.setLength(0);
4303 sb.append(prefix);
4304 sb.append(" Max learned battery capacity: ");
4305 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
4306 sb.append(" mAh");
4307 pw.println(sb.toString());
4308 }
4309
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004310 sb.setLength(0);
4311 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004312 sb.append(" Time on battery: ");
4313 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
4314 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
4315 sb.append(") realtime, ");
4316 formatTimeMs(sb, whichBatteryUptime / 1000);
4317 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, whichBatteryRealtime));
4318 sb.append(") uptime");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004319 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004320
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004321 sb.setLength(0);
4322 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004323 sb.append(" Time on battery screen off: ");
4324 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
4325 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, whichBatteryRealtime));
4326 sb.append(") realtime, ");
4327 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
4328 sb.append("(");
4329 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, whichBatteryRealtime));
4330 sb.append(") uptime");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004331 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004332
4333 sb.setLength(0);
4334 sb.append(prefix);
4335 sb.append(" Time on battery screen doze: ");
4336 formatTimeMs(sb, screenDozeTime / 1000); sb.append("(");
4337 sb.append(formatRatioLocked(screenDozeTime, whichBatteryRealtime));
4338 sb.append(")");
4339 pw.println(sb.toString());
4340
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004341 sb.setLength(0);
4342 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004343 sb.append(" Total run time: ");
4344 formatTimeMs(sb, totalRealtime / 1000);
4345 sb.append("realtime, ");
4346 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004347 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07004348 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004349 if (batteryTimeRemaining >= 0) {
4350 sb.setLength(0);
4351 sb.append(prefix);
4352 sb.append(" Battery time remaining: ");
4353 formatTimeMs(sb, batteryTimeRemaining / 1000);
4354 pw.println(sb.toString());
4355 }
4356 if (chargeTimeRemaining >= 0) {
4357 sb.setLength(0);
4358 sb.append(prefix);
4359 sb.append(" Charge time remaining: ");
4360 formatTimeMs(sb, chargeTimeRemaining / 1000);
4361 pw.println(sb.toString());
4362 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004363
Kweku Adams87b19ec2017-10-09 12:40:03 -07004364 final long dischargeCount = getUahDischarge(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004365 if (dischargeCount >= 0) {
4366 sb.setLength(0);
4367 sb.append(prefix);
4368 sb.append(" Discharge: ");
4369 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
4370 sb.append(" mAh");
4371 pw.println(sb.toString());
4372 }
4373
Kweku Adams87b19ec2017-10-09 12:40:03 -07004374 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004375 if (dischargeScreenOffCount >= 0) {
4376 sb.setLength(0);
4377 sb.append(prefix);
4378 sb.append(" Screen off discharge: ");
4379 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
4380 sb.append(" mAh");
4381 pw.println(sb.toString());
4382 }
4383
Kweku Adams87b19ec2017-10-09 12:40:03 -07004384 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Mac2f518a2017-09-19 16:06:03 -07004385 if (dischargeScreenDozeCount >= 0) {
4386 sb.setLength(0);
4387 sb.append(prefix);
4388 sb.append(" Screen doze discharge: ");
4389 sb.append(BatteryStatsHelper.makemAh(dischargeScreenDozeCount / 1000.0));
4390 sb.append(" mAh");
4391 pw.println(sb.toString());
4392 }
4393
4394 final long dischargeScreenOnCount =
4395 dischargeCount - dischargeScreenOffCount - dischargeScreenDozeCount;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004396 if (dischargeScreenOnCount >= 0) {
4397 sb.setLength(0);
4398 sb.append(prefix);
4399 sb.append(" Screen on discharge: ");
4400 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
4401 sb.append(" mAh");
4402 pw.println(sb.toString());
4403 }
4404
Mike Ma15313c92017-11-15 17:58:21 -08004405 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
4406 if (dischargeLightDozeCount >= 0) {
4407 sb.setLength(0);
4408 sb.append(prefix);
4409 sb.append(" Device light doze discharge: ");
4410 sb.append(BatteryStatsHelper.makemAh(dischargeLightDozeCount / 1000.0));
4411 sb.append(" mAh");
4412 pw.println(sb.toString());
4413 }
4414
4415 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
4416 if (dischargeDeepDozeCount >= 0) {
4417 sb.setLength(0);
4418 sb.append(prefix);
4419 sb.append(" Device deep doze discharge: ");
4420 sb.append(BatteryStatsHelper.makemAh(dischargeDeepDozeCount / 1000.0));
4421 sb.append(" mAh");
4422 pw.println(sb.toString());
4423 }
4424
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004425 pw.print(" Start clock time: ");
4426 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
4427
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004428 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004429 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004430 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004431 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
4432 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004433 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004434 rawRealtime, which);
4435 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
4436 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004437 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004438 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004439 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
4440 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
4441 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004442 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004443 sb.append(prefix);
4444 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
4445 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004446 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004447 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
4448 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004449 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004450 pw.println(sb.toString());
4451 sb.setLength(0);
4452 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004453 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004454 boolean didOne = false;
4455 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004456 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004457 if (time == 0) {
4458 continue;
4459 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004460 sb.append("\n ");
4461 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004462 didOne = true;
4463 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
4464 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004465 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004466 sb.append("(");
4467 sb.append(formatRatioLocked(time, screenOnTime));
4468 sb.append(")");
4469 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004470 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004471 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004472 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004473 sb.setLength(0);
4474 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004475 sb.append(" Power save mode enabled: ");
4476 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004477 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004478 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004479 sb.append(")");
4480 pw.println(sb.toString());
4481 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004482 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004483 sb.setLength(0);
4484 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004485 sb.append(" Device light idling: ");
4486 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004487 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004488 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
4489 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004490 sb.append("x");
4491 pw.println(sb.toString());
4492 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004493 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004494 sb.setLength(0);
4495 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004496 sb.append(" Idle mode light time: ");
4497 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004498 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004499 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
4500 sb.append(") ");
4501 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004502 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004503 sb.append(" -- longest ");
4504 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
4505 pw.println(sb.toString());
4506 }
4507 if (deviceIdlingTime != 0) {
4508 sb.setLength(0);
4509 sb.append(prefix);
4510 sb.append(" Device full idling: ");
4511 formatTimeMs(sb, deviceIdlingTime / 1000);
4512 sb.append("(");
4513 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004514 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004515 sb.append("x");
4516 pw.println(sb.toString());
4517 }
4518 if (deviceIdleModeFullTime != 0) {
4519 sb.setLength(0);
4520 sb.append(prefix);
4521 sb.append(" Idle mode full time: ");
4522 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
4523 sb.append("(");
4524 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
4525 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004526 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004527 sb.append("x");
4528 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004529 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004530 pw.println(sb.toString());
4531 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004532 if (phoneOnTime != 0) {
4533 sb.setLength(0);
4534 sb.append(prefix);
4535 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
4536 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004537 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004538 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004539 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004540 if (connChanges != 0) {
4541 pw.print(prefix);
4542 pw.print(" Connectivity changes: "); pw.println(connChanges);
4543 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004544
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004545 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07004546 long fullWakeLockTimeTotalMicros = 0;
4547 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08004548
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004549 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08004550
Evan Millar22ac0432009-03-31 11:33:18 -07004551 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004552 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004553
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004554 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4555 = u.getWakelockStats();
4556 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4557 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07004558
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004559 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
4560 if (fullWakeTimer != null) {
4561 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
4562 rawRealtime, which);
4563 }
4564
4565 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4566 if (partialWakeTimer != null) {
4567 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
4568 rawRealtime, which);
4569 if (totalTimeMicros > 0) {
4570 if (reqUid < 0) {
4571 // Only show the ordered list of all wake
4572 // locks if the caller is not asking for data
4573 // about a specific uid.
4574 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
4575 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08004576 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004577 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07004578 }
4579 }
4580 }
4581 }
Bookatzc8c44962017-05-11 12:12:54 -07004582
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004583 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4584 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4585 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4586 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4587 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4588 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4589 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4590 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004591 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4592 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004593
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004594 if (fullWakeLockTimeTotalMicros != 0) {
4595 sb.setLength(0);
4596 sb.append(prefix);
4597 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4598 (fullWakeLockTimeTotalMicros + 500) / 1000);
4599 pw.println(sb.toString());
4600 }
4601
4602 if (partialWakeLockTimeTotalMicros != 0) {
4603 sb.setLength(0);
4604 sb.append(prefix);
4605 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4606 (partialWakeLockTimeTotalMicros + 500) / 1000);
4607 pw.println(sb.toString());
4608 }
4609
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004610 final long multicastWakeLockTimeTotalMicros =
4611 getWifiMulticastWakelockTime(rawRealtime, which);
4612 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004613 if (multicastWakeLockTimeTotalMicros != 0) {
4614 sb.setLength(0);
4615 sb.append(prefix);
4616 sb.append(" Total WiFi Multicast wakelock Count: ");
4617 sb.append(multicastWakeLockCountTotal);
4618 pw.println(sb.toString());
4619
4620 sb.setLength(0);
4621 sb.append(prefix);
4622 sb.append(" Total WiFi Multicast wakelock time: ");
4623 formatTimeMsNoSpace(sb, (multicastWakeLockTimeTotalMicros + 500) / 1000);
4624 pw.println(sb.toString());
4625 }
4626
Siddharth Ray3c648c42017-10-02 17:30:58 -07004627 pw.println("");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004628 pw.print(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004629 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004630 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004631 sb.append(" CONNECTIVITY POWER SUMMARY START");
4632 pw.println(sb.toString());
4633
4634 pw.print(prefix);
4635 sb.setLength(0);
4636 sb.append(prefix);
4637 sb.append(" Logging duration for connectivity statistics: ");
4638 formatTimeMs(sb, whichBatteryRealtime / 1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004639 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004640
4641 sb.setLength(0);
4642 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004643 sb.append(" Cellular Statistics:");
Amith Yamasanif37447b2009-10-08 18:28:01 -07004644 pw.println(sb.toString());
4645
Siddharth Ray3c648c42017-10-02 17:30:58 -07004646 pw.print(prefix);
4647 sb.setLength(0);
4648 sb.append(prefix);
4649 sb.append(" Cellular kernel active time: ");
4650 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
4651 formatTimeMs(sb, mobileActiveTime / 1000);
4652 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4653 sb.append(")");
4654 pw.println(sb.toString());
4655
4656 pw.print(" Cellular data received: "); pw.println(formatBytesLocked(mobileRxTotalBytes));
4657 pw.print(" Cellular data sent: "); pw.println(formatBytesLocked(mobileTxTotalBytes));
4658 pw.print(" Cellular packets received: "); pw.println(mobileRxTotalPackets);
4659 pw.print(" Cellular packets sent: "); pw.println(mobileTxTotalPackets);
4660
Dianne Hackborn627bba72009-03-24 22:32:56 -07004661 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004662 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004663 sb.append(" Cellular Radio Access Technology:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004664 didOne = false;
4665 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004666 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004667 if (time == 0) {
4668 continue;
4669 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004670 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004671 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004672 didOne = true;
4673 sb.append(DATA_CONNECTION_NAMES[i]);
4674 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004675 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004676 sb.append("(");
4677 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004678 sb.append(") ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004679 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004680 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004681 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004682
4683 sb.setLength(0);
4684 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004685 sb.append(" Cellular Rx signal strength (RSRP):");
4686 final String[] cellularRxSignalStrengthDescription = new String[]{
4687 "very poor (less than -128dBm): ",
4688 "poor (-128dBm to -118dBm): ",
4689 "moderate (-118dBm to -108dBm): ",
4690 "good (-108dBm to -98dBm): ",
4691 "great (greater than -98dBm): "};
4692 didOne = false;
4693 final int numCellularRxBins = Math.min(SignalStrength.NUM_SIGNAL_STRENGTH_BINS,
4694 cellularRxSignalStrengthDescription.length);
4695 for (int i=0; i<numCellularRxBins; i++) {
4696 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
4697 if (time == 0) {
4698 continue;
4699 }
4700 sb.append("\n ");
4701 sb.append(prefix);
4702 didOne = true;
4703 sb.append(cellularRxSignalStrengthDescription[i]);
4704 sb.append(" ");
4705 formatTimeMs(sb, time/1000);
4706 sb.append("(");
4707 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4708 sb.append(") ");
4709 }
4710 if (!didOne) sb.append(" (no activity)");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004711 pw.println(sb.toString());
4712
Siddharth Rayb50a6842017-12-14 15:15:28 -08004713 printControllerActivity(pw, sb, prefix, CELLULAR_CONTROLLER_NAME,
Siddharth Ray3c648c42017-10-02 17:30:58 -07004714 getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004715
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004716 pw.print(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004717 sb.setLength(0);
4718 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004719 sb.append(" Wifi Statistics:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004720 pw.println(sb.toString());
4721
Siddharth Rayb50a6842017-12-14 15:15:28 -08004722 pw.print(prefix);
4723 sb.setLength(0);
4724 sb.append(prefix);
4725 sb.append(" Wifi kernel active time: ");
4726 final long wifiActiveTime = getWifiActiveTime(rawRealtime, which);
4727 formatTimeMs(sb, wifiActiveTime / 1000);
4728 sb.append("("); sb.append(formatRatioLocked(wifiActiveTime, whichBatteryRealtime));
4729 sb.append(")");
4730 pw.println(sb.toString());
4731
Siddharth Ray3c648c42017-10-02 17:30:58 -07004732 pw.print(" Wifi data received: "); pw.println(formatBytesLocked(wifiRxTotalBytes));
4733 pw.print(" Wifi data sent: "); pw.println(formatBytesLocked(wifiTxTotalBytes));
4734 pw.print(" Wifi packets received: "); pw.println(wifiRxTotalPackets);
4735 pw.print(" Wifi packets sent: "); pw.println(wifiTxTotalPackets);
4736
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004737 sb.setLength(0);
4738 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004739 sb.append(" Wifi states:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004740 didOne = false;
4741 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004742 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004743 if (time == 0) {
4744 continue;
4745 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004746 sb.append("\n ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004747 didOne = true;
4748 sb.append(WIFI_STATE_NAMES[i]);
4749 sb.append(" ");
4750 formatTimeMs(sb, time/1000);
4751 sb.append("(");
4752 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4753 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004754 }
4755 if (!didOne) sb.append(" (no activity)");
4756 pw.println(sb.toString());
4757
4758 sb.setLength(0);
4759 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004760 sb.append(" Wifi supplicant states:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004761 didOne = false;
4762 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4763 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4764 if (time == 0) {
4765 continue;
4766 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004767 sb.append("\n ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004768 didOne = true;
4769 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4770 sb.append(" ");
4771 formatTimeMs(sb, time/1000);
4772 sb.append("(");
4773 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4774 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004775 }
4776 if (!didOne) sb.append(" (no activity)");
4777 pw.println(sb.toString());
4778
4779 sb.setLength(0);
4780 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004781 sb.append(" Wifi Rx signal strength (RSSI):");
4782 final String[] wifiRxSignalStrengthDescription = new String[]{
4783 "very poor (less than -88.75dBm): ",
4784 "poor (-88.75 to -77.5dBm): ",
4785 "moderate (-77.5dBm to -66.25dBm): ",
4786 "good (-66.25dBm to -55dBm): ",
4787 "great (greater than -55dBm): "};
Dianne Hackborn3251b902014-06-20 14:40:53 -07004788 didOne = false;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004789 final int numWifiRxBins = Math.min(NUM_WIFI_SIGNAL_STRENGTH_BINS,
4790 wifiRxSignalStrengthDescription.length);
4791 for (int i=0; i<numWifiRxBins; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004792 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4793 if (time == 0) {
4794 continue;
4795 }
4796 sb.append("\n ");
4797 sb.append(prefix);
4798 didOne = true;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004799 sb.append(" ");
4800 sb.append(wifiRxSignalStrengthDescription[i]);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004801 formatTimeMs(sb, time/1000);
4802 sb.append("(");
4803 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4804 sb.append(") ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004805 }
4806 if (!didOne) sb.append(" (no activity)");
4807 pw.println(sb.toString());
4808
Siddharth Rayb50a6842017-12-14 15:15:28 -08004809 printControllerActivity(pw, sb, prefix, WIFI_CONTROLLER_NAME,
4810 getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004811
Adam Lesinski50e47602015-12-04 17:04:54 -08004812 pw.print(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004813 sb.setLength(0);
4814 sb.append(prefix);
Siddharth Ray78ccaf52017-12-23 16:16:21 -08004815 sb.append(" GPS Statistics:");
4816 pw.println(sb.toString());
4817
4818 sb.setLength(0);
4819 sb.append(prefix);
4820 sb.append(" GPS signal quality (Top 4 Average CN0):");
4821 final String[] gpsSignalQualityDescription = new String[]{
4822 "poor (less than 20 dBHz): ",
4823 "good (greater than 20 dBHz): "};
4824 final int numGpsSignalQualityBins = Math.min(GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS,
4825 gpsSignalQualityDescription.length);
4826 for (int i=0; i<numGpsSignalQualityBins; i++) {
4827 final long time = getGpsSignalQualityTime(i, rawRealtime, which);
4828 sb.append("\n ");
4829 sb.append(prefix);
4830 sb.append(" ");
4831 sb.append(gpsSignalQualityDescription[i]);
4832 formatTimeMs(sb, time/1000);
4833 sb.append("(");
4834 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4835 sb.append(") ");
4836 }
4837 pw.println(sb.toString());
4838
4839 final long gpsBatteryDrainMaMs = getGpsBatteryDrainMaMs();
4840 if (gpsBatteryDrainMaMs > 0) {
4841 pw.print(prefix);
4842 sb.setLength(0);
4843 sb.append(prefix);
4844 sb.append(" Battery Drain (mAh): ");
4845 sb.append(Double.toString(((double) gpsBatteryDrainMaMs)/(3600 * 1000)));
4846 pw.println(sb.toString());
4847 }
4848
4849 pw.print(prefix);
4850 sb.setLength(0);
4851 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004852 sb.append(" CONNECTIVITY POWER SUMMARY END");
4853 pw.println(sb.toString());
4854 pw.println("");
4855
4856 pw.print(prefix);
Adam Lesinski50e47602015-12-04 17:04:54 -08004857 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4858 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4859
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004860 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4861 sb.setLength(0);
4862 sb.append(prefix);
4863 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4864 pw.println(sb.toString());
4865
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004866 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4867 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004868
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004869 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004870
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004871 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004872 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004873 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004874 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004875 pw.println(getDischargeStartLevel());
4876 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4877 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004878 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004879 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004880 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004881 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004882 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004883 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004884 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004885 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004886 pw.println(getDischargeAmountScreenOn());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004887 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004888 pw.println(getDischargeAmountScreenOff());
4889 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4890 pw.println(getDischargeAmountScreenDoze());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004891 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004892 } else {
4893 pw.print(prefix); pw.println(" Device battery use since last full charge");
4894 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004895 pw.println(getLowDischargeAmountSinceCharge());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004896 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004897 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004898 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004899 pw.println(getDischargeAmountScreenOnSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004900 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004901 pw.println(getDischargeAmountScreenOffSinceCharge());
4902 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4903 pw.println(getDischargeAmountScreenDozeSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004904 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004905 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004906
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004907 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004908 helper.create(this);
4909 helper.refreshStats(which, UserHandle.USER_ALL);
4910 List<BatterySipper> sippers = helper.getUsageList();
4911 if (sippers != null && sippers.size() > 0) {
4912 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4913 pw.print(prefix); pw.print(" Capacity: ");
4914 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004915 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004916 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4917 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4918 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4919 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004920 pw.println();
4921 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004922 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004923 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004924 switch (bs.drainType) {
4925 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004926 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004927 break;
4928 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004929 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004930 break;
4931 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004932 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004933 break;
4934 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004935 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004936 break;
4937 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004938 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004939 break;
4940 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004941 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004942 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004943 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004944 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004945 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004946 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004947 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004948 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004949 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004950 break;
4951 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004952 pw.print(" User "); pw.print(bs.userId);
4953 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004954 break;
4955 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004956 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004957 break;
4958 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004959 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004960 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004961 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004962 pw.print(" Camera: ");
4963 break;
4964 default:
4965 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004966 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004967 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004968 printmAh(pw, bs.totalPowerMah);
4969
Adam Lesinski57123002015-06-12 16:12:07 -07004970 if (bs.usagePowerMah != bs.totalPowerMah) {
4971 // If the usage (generic power) isn't the whole amount, we list out
4972 // what components are involved in the calculation.
4973
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004974 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004975 if (bs.usagePowerMah != 0) {
4976 pw.print(" usage=");
4977 printmAh(pw, bs.usagePowerMah);
4978 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004979 if (bs.cpuPowerMah != 0) {
4980 pw.print(" cpu=");
4981 printmAh(pw, bs.cpuPowerMah);
4982 }
4983 if (bs.wakeLockPowerMah != 0) {
4984 pw.print(" wake=");
4985 printmAh(pw, bs.wakeLockPowerMah);
4986 }
4987 if (bs.mobileRadioPowerMah != 0) {
4988 pw.print(" radio=");
4989 printmAh(pw, bs.mobileRadioPowerMah);
4990 }
4991 if (bs.wifiPowerMah != 0) {
4992 pw.print(" wifi=");
4993 printmAh(pw, bs.wifiPowerMah);
4994 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004995 if (bs.bluetoothPowerMah != 0) {
4996 pw.print(" bt=");
4997 printmAh(pw, bs.bluetoothPowerMah);
4998 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004999 if (bs.gpsPowerMah != 0) {
5000 pw.print(" gps=");
5001 printmAh(pw, bs.gpsPowerMah);
5002 }
5003 if (bs.sensorPowerMah != 0) {
5004 pw.print(" sensor=");
5005 printmAh(pw, bs.sensorPowerMah);
5006 }
5007 if (bs.cameraPowerMah != 0) {
5008 pw.print(" camera=");
5009 printmAh(pw, bs.cameraPowerMah);
5010 }
5011 if (bs.flashlightPowerMah != 0) {
5012 pw.print(" flash=");
5013 printmAh(pw, bs.flashlightPowerMah);
5014 }
5015 pw.print(" )");
5016 }
Bookatz17d7d9d2017-06-08 14:50:46 -07005017
5018 // If there is additional smearing information, include it.
5019 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
5020 pw.print(" Including smearing: ");
5021 printmAh(pw, bs.totalSmearedPowerMah);
5022 pw.print(" (");
5023 if (bs.screenPowerMah != 0) {
5024 pw.print(" screen=");
5025 printmAh(pw, bs.screenPowerMah);
5026 }
5027 if (bs.proportionalSmearMah != 0) {
5028 pw.print(" proportional=");
5029 printmAh(pw, bs.proportionalSmearMah);
5030 }
5031 pw.print(" )");
5032 }
5033 if (bs.shouldHide) {
5034 pw.print(" Excluded from smearing");
5035 }
5036
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005037 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005038 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08005039 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005040 }
5041
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005042 sippers = helper.getMobilemsppList();
5043 if (sippers != null && sippers.size() > 0) {
5044 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005045 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005046 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005047 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005048 sb.setLength(0);
5049 sb.append(prefix); sb.append(" Uid ");
5050 UserHandle.formatUid(sb, bs.uidObj.getUid());
5051 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
5052 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
5053 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005054 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005055 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005056 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005057 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005058 sb.setLength(0);
5059 sb.append(prefix);
5060 sb.append(" TOTAL TIME: ");
5061 formatTimeMs(sb, totalTime);
5062 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
5063 sb.append(")");
5064 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005065 pw.println();
5066 }
5067
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005068 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
5069 @Override
5070 public int compare(TimerEntry lhs, TimerEntry rhs) {
5071 long lhsTime = lhs.mTime;
5072 long rhsTime = rhs.mTime;
5073 if (lhsTime < rhsTime) {
5074 return 1;
5075 }
5076 if (lhsTime > rhsTime) {
5077 return -1;
5078 }
5079 return 0;
5080 }
5081 };
5082
5083 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005084 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
5085 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005086 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005087 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
5088 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
5089 : kernelWakelocks.entrySet()) {
5090 final BatteryStats.Timer timer = ent.getValue();
5091 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005092 if (totalTimeMillis > 0) {
5093 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
5094 }
5095 }
5096 if (ktimers.size() > 0) {
5097 Collections.sort(ktimers, timerComparator);
5098 pw.print(prefix); pw.println(" All kernel wake locks:");
5099 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005100 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005101 String linePrefix = ": ";
5102 sb.setLength(0);
5103 sb.append(prefix);
5104 sb.append(" Kernel Wake lock ");
5105 sb.append(timer.mName);
5106 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
5107 which, linePrefix);
5108 if (!linePrefix.equals(": ")) {
5109 sb.append(" realtime");
5110 // Only print out wake locks that were held
5111 pw.println(sb.toString());
5112 }
5113 }
5114 pw.println();
5115 }
5116 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005117
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005118 if (timers.size() > 0) {
5119 Collections.sort(timers, timerComparator);
5120 pw.print(prefix); pw.println(" All partial wake locks:");
5121 for (int i=0; i<timers.size(); i++) {
5122 TimerEntry timer = timers.get(i);
5123 sb.setLength(0);
5124 sb.append(" Wake lock ");
5125 UserHandle.formatUid(sb, timer.mId);
5126 sb.append(" ");
5127 sb.append(timer.mName);
5128 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5129 sb.append(" realtime");
5130 pw.println(sb.toString());
5131 }
5132 timers.clear();
5133 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08005134 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005135
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005136 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005137 if (wakeupReasons.size() > 0) {
5138 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005139 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005140 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005141 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005142 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
5143 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005144 }
5145 Collections.sort(reasons, timerComparator);
5146 for (int i=0; i<reasons.size(); i++) {
5147 TimerEntry timer = reasons.get(i);
5148 String linePrefix = ": ";
5149 sb.setLength(0);
5150 sb.append(prefix);
5151 sb.append(" Wakeup reason ");
5152 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005153 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5154 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005155 pw.println(sb.toString());
5156 }
5157 pw.println();
5158 }
Dianne Hackborn81038902012-11-26 17:04:09 -08005159 }
Evan Millar22ac0432009-03-31 11:33:18 -07005160
James Carr2dd7e5e2016-07-20 18:48:39 -07005161 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
Bookatz50df7112017-08-04 14:53:26 -07005162 if (mMemoryStats.size() > 0) {
5163 pw.println(" Memory Stats");
5164 for (int i = 0; i < mMemoryStats.size(); i++) {
5165 sb.setLength(0);
5166 sb.append(" Bandwidth ");
5167 sb.append(mMemoryStats.keyAt(i));
5168 sb.append(" Time ");
5169 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
5170 pw.println(sb.toString());
5171 }
5172 pw.println();
5173 }
5174
5175 final Map<String, ? extends Timer> rpmStats = getRpmStats();
5176 if (rpmStats.size() > 0) {
5177 pw.print(prefix); pw.println(" Resource Power Manager Stats");
5178 if (rpmStats.size() > 0) {
5179 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
5180 final String timerName = ent.getKey();
5181 final Timer timer = ent.getValue();
5182 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5183 }
5184 }
5185 pw.println();
5186 }
Bookatz82b341172017-09-07 19:06:08 -07005187 if (SCREEN_OFF_RPM_STATS_ENABLED) {
5188 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
Bookatz50df7112017-08-04 14:53:26 -07005189 if (screenOffRpmStats.size() > 0) {
Bookatz82b341172017-09-07 19:06:08 -07005190 pw.print(prefix);
5191 pw.println(" Resource Power Manager Stats for when screen was off");
5192 if (screenOffRpmStats.size() > 0) {
5193 for (Map.Entry<String, ? extends Timer> ent : screenOffRpmStats.entrySet()) {
5194 final String timerName = ent.getKey();
5195 final Timer timer = ent.getValue();
5196 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5197 }
Bookatz50df7112017-08-04 14:53:26 -07005198 }
Bookatz82b341172017-09-07 19:06:08 -07005199 pw.println();
Bookatz50df7112017-08-04 14:53:26 -07005200 }
James Carr2dd7e5e2016-07-20 18:48:39 -07005201 }
5202
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005203 final long[] cpuFreqs = getCpuFreqs();
5204 if (cpuFreqs != null) {
5205 sb.setLength(0);
Bookatz50df7112017-08-04 14:53:26 -07005206 sb.append(" CPU freqs:");
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005207 for (int i = 0; i < cpuFreqs.length; ++i) {
5208 sb.append(" " + cpuFreqs[i]);
5209 }
5210 pw.println(sb.toString());
Bookatz50df7112017-08-04 14:53:26 -07005211 pw.println();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005212 }
5213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214 for (int iu=0; iu<NU; iu++) {
5215 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005216 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005217 continue;
5218 }
Bookatzc8c44962017-05-11 12:12:54 -07005219
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005220 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07005221
5222 pw.print(prefix);
5223 pw.print(" ");
5224 UserHandle.formatUid(pw, uid);
5225 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005226 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005227
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005228 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
5229 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
5230 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
5231 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005232 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
5233 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
5234
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005235 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
5236 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005237 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
5238 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005239
5240 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
5241 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
5242
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005243 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
5244 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
5245 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08005246 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
5247 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5248 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
5249 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005250 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005251
Adam Lesinski5f056f62016-07-14 16:56:08 -07005252 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
5253 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
5254
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005255 if (mobileRxBytes > 0 || mobileTxBytes > 0
5256 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005257 pw.print(prefix); pw.print(" Mobile network: ");
5258 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005259 pw.print(formatBytesLocked(mobileTxBytes));
5260 pw.print(" sent (packets "); pw.print(mobileRxPackets);
5261 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005262 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005263 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
5264 sb.setLength(0);
5265 sb.append(prefix); sb.append(" Mobile radio active: ");
5266 formatTimeMs(sb, uidMobileActiveTime / 1000);
5267 sb.append("(");
5268 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
5269 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
5270 long packets = mobileRxPackets + mobileTxPackets;
5271 if (packets == 0) {
5272 packets = 1;
5273 }
5274 sb.append(" @ ");
5275 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
5276 sb.append(" mspp");
5277 pw.println(sb.toString());
5278 }
5279
Adam Lesinski5f056f62016-07-14 16:56:08 -07005280 if (mobileWakeup > 0) {
5281 sb.setLength(0);
5282 sb.append(prefix);
5283 sb.append(" Mobile radio AP wakeups: ");
5284 sb.append(mobileWakeup);
5285 pw.println(sb.toString());
5286 }
5287
Siddharth Rayb50a6842017-12-14 15:15:28 -08005288 printControllerActivityIfInteresting(pw, sb, prefix + " ",
5289 CELLULAR_CONTROLLER_NAME, u.getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005290
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005291 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005292 pw.print(prefix); pw.print(" Wi-Fi network: ");
5293 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005294 pw.print(formatBytesLocked(wifiTxBytes));
5295 pw.print(" sent (packets "); pw.print(wifiRxPackets);
5296 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005297 }
5298
Dianne Hackborn62793e42015-03-09 11:15:41 -07005299 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08005300 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005301 || uidWifiRunningTime != 0) {
5302 sb.setLength(0);
5303 sb.append(prefix); sb.append(" Wifi Running: ");
5304 formatTimeMs(sb, uidWifiRunningTime / 1000);
5305 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
5306 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07005307 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005308 formatTimeMs(sb, fullWifiLockOnTime / 1000);
5309 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
5310 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08005311 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005312 formatTimeMs(sb, wifiScanTime / 1000);
5313 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07005314 whichBatteryRealtime)); sb.append(") ");
5315 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08005316 sb.append("x\n");
5317 // actual and background times are unpooled and since reset (regardless of 'which')
5318 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
5319 formatTimeMs(sb, wifiScanActualTime / 1000);
5320 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
5321 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5322 sb.append(") ");
5323 sb.append(wifiScanCount);
5324 sb.append("x\n");
5325 sb.append(prefix); sb.append(" Background Wifi Scan: ");
5326 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
5327 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
5328 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5329 sb.append(") ");
5330 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07005331 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005332 pw.println(sb.toString());
5333 }
5334
Adam Lesinski5f056f62016-07-14 16:56:08 -07005335 if (wifiWakeup > 0) {
5336 sb.setLength(0);
5337 sb.append(prefix);
5338 sb.append(" WiFi AP wakeups: ");
5339 sb.append(wifiWakeup);
5340 pw.println(sb.toString());
5341 }
5342
Siddharth Rayb50a6842017-12-14 15:15:28 -08005343 printControllerActivityIfInteresting(pw, sb, prefix + " ", WIFI_CONTROLLER_NAME,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005344 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07005345
Adam Lesinski50e47602015-12-04 17:04:54 -08005346 if (btRxBytes > 0 || btTxBytes > 0) {
5347 pw.print(prefix); pw.print(" Bluetooth network: ");
5348 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
5349 pw.print(formatBytesLocked(btTxBytes));
5350 pw.println(" sent");
5351 }
5352
Bookatz867c0d72017-03-07 18:23:42 -08005353 final Timer bleTimer = u.getBluetoothScanTimer();
5354 if (bleTimer != null) {
5355 // Convert from microseconds to milliseconds with rounding
5356 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
5357 / 1000;
5358 if (totalTimeMs != 0) {
5359 final int count = bleTimer.getCountLocked(which);
5360 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
5361 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005362 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5363 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
5364 final long actualTimeMsBg = bleTimerBg != null ?
5365 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005366 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07005367 final int resultCount = u.getBluetoothScanResultCounter() != null ?
5368 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005369 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
5370 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
5371 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
5372 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
5373 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
5374 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5375 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
5376 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
5377 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
5378 final Timer unoptimizedScanTimerBg =
5379 u.getBluetoothUnoptimizedScanBackgroundTimer();
5380 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
5381 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5382 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
5383 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005384
5385 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08005386 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07005387 sb.append(prefix);
5388 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08005389 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07005390 sb.append(" (");
5391 sb.append(count);
5392 sb.append(" times)");
5393 if (bleTimer.isRunningLocked()) {
5394 sb.append(" (currently running)");
5395 }
5396 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005397 }
Bookatzb1f04f32017-05-19 13:57:32 -07005398
5399 sb.append(prefix);
5400 sb.append(" Bluetooth Scan (total actual realtime): ");
5401 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
5402 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005403 sb.append(count);
5404 sb.append(" times)");
5405 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07005406 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08005407 }
Bookatzb1f04f32017-05-19 13:57:32 -07005408 sb.append("\n");
5409 if (actualTimeMsBg > 0 || countBg > 0) {
5410 sb.append(prefix);
5411 sb.append(" Bluetooth Scan (background realtime): ");
5412 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
5413 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005414 sb.append(countBg);
5415 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07005416 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
5417 sb.append(" (currently running in background)");
5418 }
5419 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005420 }
Bookatzb1f04f32017-05-19 13:57:32 -07005421
5422 sb.append(prefix);
5423 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07005424 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07005425 sb.append(" (");
5426 sb.append(resultCountBg);
5427 sb.append(" in background)");
5428
5429 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
5430 sb.append("\n");
5431 sb.append(prefix);
5432 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
5433 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
5434 sb.append(" (max ");
5435 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
5436 sb.append(")");
5437 if (unoptimizedScanTimer != null
5438 && unoptimizedScanTimer.isRunningLocked()) {
5439 sb.append(" (currently running unoptimized)");
5440 }
5441 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
5442 sb.append("\n");
5443 sb.append(prefix);
5444 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
5445 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
5446 sb.append(" (max ");
5447 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
5448 sb.append(")");
5449 if (unoptimizedScanTimerBg.isRunningLocked()) {
5450 sb.append(" (currently running unoptimized in background)");
5451 }
5452 }
5453 }
Bookatz867c0d72017-03-07 18:23:42 -08005454 pw.println(sb.toString());
5455 uidActivity = true;
5456 }
5457 }
5458
5459
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005460
Dianne Hackborn617f8772009-03-31 15:04:46 -07005461 if (u.hasUserActivity()) {
5462 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07005463 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005464 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005465 if (val != 0) {
5466 if (!hasData) {
5467 sb.setLength(0);
5468 sb.append(" User activity: ");
5469 hasData = true;
5470 } else {
5471 sb.append(", ");
5472 }
5473 sb.append(val);
5474 sb.append(" ");
5475 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
5476 }
5477 }
5478 if (hasData) {
5479 pw.println(sb.toString());
5480 }
5481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005482
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005483 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
5484 = u.getWakelockStats();
5485 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005486 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005487 int countWakelock = 0;
5488 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
5489 final Uid.Wakelock wl = wakelocks.valueAt(iw);
5490 String linePrefix = ": ";
5491 sb.setLength(0);
5492 sb.append(prefix);
5493 sb.append(" Wake lock ");
5494 sb.append(wakelocks.keyAt(iw));
5495 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
5496 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005497 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
5498 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005499 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005500 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
5501 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005502 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
5503 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005504 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
5505 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07005506 sb.append(" realtime");
5507 pw.println(sb.toString());
5508 uidActivity = true;
5509 countWakelock++;
5510
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005511 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
5512 rawRealtime, which);
5513 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
5514 rawRealtime, which);
5515 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
5516 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005517 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07005518 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005519 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005520 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07005521 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
5522 // pooled and therefore just a lower bound)
5523 long actualTotalPartialWakelock = 0;
5524 long actualBgPartialWakelock = 0;
5525 if (u.getAggregatedPartialWakelockTimer() != null) {
5526 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
5527 // Convert from microseconds to milliseconds with rounding
5528 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07005529 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07005530 final Timer bgAggTimer = aggTimer.getSubTimer();
5531 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005532 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07005533 }
5534
5535 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
5536 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
5537 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005538 sb.setLength(0);
5539 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005540 sb.append(" TOTAL wake: ");
5541 boolean needComma = false;
5542 if (totalFullWakelock != 0) {
5543 needComma = true;
5544 formatTimeMs(sb, totalFullWakelock);
5545 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005546 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005547 if (totalPartialWakelock != 0) {
5548 if (needComma) {
5549 sb.append(", ");
5550 }
5551 needComma = true;
5552 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07005553 sb.append("blamed partial");
5554 }
5555 if (actualTotalPartialWakelock != 0) {
5556 if (needComma) {
5557 sb.append(", ");
5558 }
5559 needComma = true;
5560 formatTimeMs(sb, actualTotalPartialWakelock);
5561 sb.append("actual partial");
5562 }
5563 if (actualBgPartialWakelock != 0) {
5564 if (needComma) {
5565 sb.append(", ");
5566 }
5567 needComma = true;
5568 formatTimeMs(sb, actualBgPartialWakelock);
5569 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005570 }
5571 if (totalWindowWakelock != 0) {
5572 if (needComma) {
5573 sb.append(", ");
5574 }
5575 needComma = true;
5576 formatTimeMs(sb, totalWindowWakelock);
5577 sb.append("window");
5578 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005579 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07005580 if (needComma) {
5581 sb.append(",");
5582 }
5583 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005584 formatTimeMs(sb, totalDrawWakelock);
5585 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07005586 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005587 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005588 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005589 }
5590 }
5591
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07005592 // Calculate multicast wakelock stats
5593 final Timer mcTimer = u.getMulticastWakelockStats();
5594 if (mcTimer != null) {
5595 final long multicastWakeLockTimeMicros = mcTimer.getTotalTimeLocked(rawRealtime, which);
5596 final int multicastWakeLockCount = mcTimer.getCountLocked(which);
5597
5598 if (multicastWakeLockTimeMicros > 0) {
5599 sb.setLength(0);
5600 sb.append(prefix);
5601 sb.append(" WiFi Multicast Wakelock");
5602 sb.append(" count = ");
5603 sb.append(multicastWakeLockCount);
5604 sb.append(" time = ");
5605 formatTimeMsNoSpace(sb, (multicastWakeLockTimeMicros + 500) / 1000);
5606 pw.println(sb.toString());
5607 }
5608 }
5609
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005610 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
5611 for (int isy=syncs.size()-1; isy>=0; isy--) {
5612 final Timer timer = syncs.valueAt(isy);
5613 // Convert from microseconds to milliseconds with rounding
5614 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5615 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07005616 final Timer bgTimer = timer.getSubTimer();
5617 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005618 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07005619 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005620 sb.setLength(0);
5621 sb.append(prefix);
5622 sb.append(" Sync ");
5623 sb.append(syncs.keyAt(isy));
5624 sb.append(": ");
5625 if (totalTime != 0) {
5626 formatTimeMs(sb, totalTime);
5627 sb.append("realtime (");
5628 sb.append(count);
5629 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07005630 if (bgTime > 0) {
5631 sb.append(", ");
5632 formatTimeMs(sb, bgTime);
5633 sb.append("background (");
5634 sb.append(bgCount);
5635 sb.append(" times)");
5636 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005637 } else {
5638 sb.append("(not used)");
5639 }
5640 pw.println(sb.toString());
5641 uidActivity = true;
5642 }
5643
5644 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
5645 for (int ij=jobs.size()-1; ij>=0; ij--) {
5646 final Timer timer = jobs.valueAt(ij);
5647 // Convert from microseconds to milliseconds with rounding
5648 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5649 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07005650 final Timer bgTimer = timer.getSubTimer();
5651 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005652 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07005653 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005654 sb.setLength(0);
5655 sb.append(prefix);
5656 sb.append(" Job ");
5657 sb.append(jobs.keyAt(ij));
5658 sb.append(": ");
5659 if (totalTime != 0) {
5660 formatTimeMs(sb, totalTime);
5661 sb.append("realtime (");
5662 sb.append(count);
5663 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07005664 if (bgTime > 0) {
5665 sb.append(", ");
5666 formatTimeMs(sb, bgTime);
5667 sb.append("background (");
5668 sb.append(bgCount);
5669 sb.append(" times)");
5670 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005671 } else {
5672 sb.append("(not used)");
5673 }
5674 pw.println(sb.toString());
5675 uidActivity = true;
5676 }
5677
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005678 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
5679 for (int ic=completions.size()-1; ic>=0; ic--) {
5680 SparseIntArray types = completions.valueAt(ic);
5681 if (types != null) {
5682 pw.print(prefix);
5683 pw.print(" Job Completions ");
5684 pw.print(completions.keyAt(ic));
5685 pw.print(":");
5686 for (int it=0; it<types.size(); it++) {
5687 pw.print(" ");
5688 pw.print(JobParameters.getReasonName(types.keyAt(it)));
5689 pw.print("(");
5690 pw.print(types.valueAt(it));
5691 pw.print("x)");
5692 }
5693 pw.println();
5694 }
5695 }
5696
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005697 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
5698 prefix, "Flashlight");
5699 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
5700 prefix, "Camera");
5701 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
5702 prefix, "Video");
5703 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
5704 prefix, "Audio");
5705
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005706 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
5707 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005708 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005709 final Uid.Sensor se = sensors.valueAt(ise);
5710 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005711 sb.setLength(0);
5712 sb.append(prefix);
5713 sb.append(" Sensor ");
5714 int handle = se.getHandle();
5715 if (handle == Uid.Sensor.GPS) {
5716 sb.append("GPS");
5717 } else {
5718 sb.append(handle);
5719 }
5720 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005721
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005722 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005723 if (timer != null) {
5724 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005725 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5726 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005727 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005728 final Timer bgTimer = se.getSensorBackgroundTime();
5729 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005730 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5731 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5732 final long bgActualTime = bgTimer != null ?
5733 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5734
Dianne Hackborn61659e52014-07-09 16:13:01 -07005735 //timer.logState();
5736 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005737 if (actualTime != totalTime) {
5738 formatTimeMs(sb, totalTime);
5739 sb.append("blamed realtime, ");
5740 }
5741
5742 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005743 sb.append("realtime (");
5744 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005745 sb.append(" times)");
5746
5747 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005748 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005749 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5750 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005751 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005752 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005754 } else {
5755 sb.append("(not used)");
5756 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005757 } else {
5758 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005759 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005760
5761 pw.println(sb.toString());
5762 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005763 }
5764
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005765 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5766 "Vibrator");
5767 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5768 prefix, "Foreground activities");
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005769 uidActivity |= printTimer(pw, sb, u.getForegroundServiceTimer(), rawRealtime, which,
5770 prefix, "Foreground services");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005771
Dianne Hackborn61659e52014-07-09 16:13:01 -07005772 long totalStateTime = 0;
5773 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5774 long time = u.getProcessStateTime(ips, rawRealtime, which);
5775 if (time > 0) {
5776 totalStateTime += time;
5777 sb.setLength(0);
5778 sb.append(prefix);
5779 sb.append(" ");
5780 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5781 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005782 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005783 pw.println(sb.toString());
5784 uidActivity = true;
5785 }
5786 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005787 if (totalStateTime > 0) {
5788 sb.setLength(0);
5789 sb.append(prefix);
5790 sb.append(" Total running: ");
5791 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5792 pw.println(sb.toString());
5793 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005794
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005795 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5796 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005797 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005798 sb.setLength(0);
5799 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005800 sb.append(" Total cpu time: u=");
5801 formatTimeMs(sb, userCpuTimeUs / 1000);
5802 sb.append("s=");
5803 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005804 pw.println(sb.toString());
5805 }
5806
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005807 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5808 if (cpuFreqTimes != null) {
5809 sb.setLength(0);
5810 sb.append(" Total cpu time per freq:");
5811 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5812 sb.append(" " + cpuFreqTimes[i]);
5813 }
5814 pw.println(sb.toString());
5815 }
5816 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5817 if (screenOffCpuFreqTimes != null) {
5818 sb.setLength(0);
5819 sb.append(" Total screen-off cpu time per freq:");
5820 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5821 sb.append(" " + screenOffCpuFreqTimes[i]);
5822 }
5823 pw.println(sb.toString());
5824 }
5825
Sudheer Shankab2f83c12017-11-13 19:25:01 -08005826 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
5827 final long[] cpuTimes = u.getCpuFreqTimes(which, procState);
5828 if (cpuTimes != null) {
5829 sb.setLength(0);
5830 sb.append(" Cpu times per freq at state "
5831 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5832 for (int i = 0; i < cpuTimes.length; ++i) {
5833 sb.append(" " + cpuTimes[i]);
5834 }
5835 pw.println(sb.toString());
5836 }
5837
5838 final long[] screenOffCpuTimes = u.getScreenOffCpuFreqTimes(which, procState);
5839 if (screenOffCpuTimes != null) {
5840 sb.setLength(0);
5841 sb.append(" Screen-off cpu times per freq at state "
5842 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5843 for (int i = 0; i < screenOffCpuTimes.length; ++i) {
5844 sb.append(" " + screenOffCpuTimes[i]);
5845 }
5846 pw.println(sb.toString());
5847 }
5848 }
5849
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005850 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5851 = u.getProcessStats();
5852 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5853 final Uid.Proc ps = processStats.valueAt(ipr);
5854 long userTime;
5855 long systemTime;
5856 long foregroundTime;
5857 int starts;
5858 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005859
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005860 userTime = ps.getUserTime(which);
5861 systemTime = ps.getSystemTime(which);
5862 foregroundTime = ps.getForegroundTime(which);
5863 starts = ps.getStarts(which);
5864 final int numCrashes = ps.getNumCrashes(which);
5865 final int numAnrs = ps.getNumAnrs(which);
5866 numExcessive = which == STATS_SINCE_CHARGED
5867 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005868
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005869 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5870 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5871 sb.setLength(0);
5872 sb.append(prefix); sb.append(" Proc ");
5873 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5874 sb.append(prefix); sb.append(" CPU: ");
5875 formatTimeMs(sb, userTime); sb.append("usr + ");
5876 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5877 formatTimeMs(sb, foregroundTime); sb.append("fg");
5878 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5879 sb.append("\n"); sb.append(prefix); sb.append(" ");
5880 boolean hasOne = false;
5881 if (starts != 0) {
5882 hasOne = true;
5883 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005884 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005885 if (numCrashes != 0) {
5886 if (hasOne) {
5887 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005888 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005889 hasOne = true;
5890 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005891 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005892 if (numAnrs != 0) {
5893 if (hasOne) {
5894 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005895 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005896 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005897 }
5898 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005899 pw.println(sb.toString());
5900 for (int e=0; e<numExcessive; e++) {
5901 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5902 if (ew != null) {
5903 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07005904 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005905 pw.print("cpu");
5906 } else {
5907 pw.print("unknown");
5908 }
5909 pw.print(" use: ");
5910 TimeUtils.formatDuration(ew.usedTime, pw);
5911 pw.print(" over ");
5912 TimeUtils.formatDuration(ew.overTime, pw);
5913 if (ew.overTime != 0) {
5914 pw.print(" (");
5915 pw.print((ew.usedTime*100)/ew.overTime);
5916 pw.println("%)");
5917 }
5918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005919 }
5920 uidActivity = true;
5921 }
5922 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005923
5924 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5925 = u.getPackageStats();
5926 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
5927 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
5928 pw.println(":");
5929 boolean apkActivity = false;
5930 final Uid.Pkg ps = packageStats.valueAt(ipkg);
5931 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
5932 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
5933 pw.print(prefix); pw.print(" Wakeup alarm ");
5934 pw.print(alarms.keyAt(iwa)); pw.print(": ");
5935 pw.print(alarms.valueAt(iwa).getCountLocked(which));
5936 pw.println(" times");
5937 apkActivity = true;
5938 }
5939 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
5940 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
5941 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
5942 final long startTime = ss.getStartTime(batteryUptime, which);
5943 final int starts = ss.getStarts(which);
5944 final int launches = ss.getLaunches(which);
5945 if (startTime != 0 || starts != 0 || launches != 0) {
5946 sb.setLength(0);
5947 sb.append(prefix); sb.append(" Service ");
5948 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
5949 sb.append(prefix); sb.append(" Created for: ");
5950 formatTimeMs(sb, startTime / 1000);
5951 sb.append("uptime\n");
5952 sb.append(prefix); sb.append(" Starts: ");
5953 sb.append(starts);
5954 sb.append(", launches: "); sb.append(launches);
5955 pw.println(sb.toString());
5956 apkActivity = true;
5957 }
5958 }
5959 if (!apkActivity) {
5960 pw.print(prefix); pw.println(" (nothing executed)");
5961 }
5962 uidActivity = true;
5963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005964 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005965 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005966 }
5967 }
5968 }
5969
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005970 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005971 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005972 int diff = oldval ^ newval;
5973 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005974 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005975 for (int i=0; i<descriptions.length; i++) {
5976 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005977 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005978 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005979 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005980 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005981 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005982 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
5983 didWake = true;
5984 pw.print("=");
5985 if (longNames) {
5986 UserHandle.formatUid(pw, wakelockTag.uid);
5987 pw.print(":\"");
5988 pw.print(wakelockTag.string);
5989 pw.print("\"");
5990 } else {
5991 pw.print(wakelockTag.poolIdx);
5992 }
5993 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005994 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005995 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005996 pw.print("=");
5997 int val = (newval&bd.mask)>>bd.shift;
5998 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005999 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006000 } else {
6001 pw.print(val);
6002 }
6003 }
6004 }
6005 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006006 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07006007 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006008 if (longNames) {
6009 UserHandle.formatUid(pw, wakelockTag.uid);
6010 pw.print(":\"");
6011 pw.print(wakelockTag.string);
6012 pw.print("\"");
6013 } else {
6014 pw.print(wakelockTag.poolIdx);
6015 }
6016 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006017 }
Mike Mac2f518a2017-09-19 16:06:03 -07006018
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006019 public void prepareForDumpLocked() {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006020 // We don't need to require subclasses implement this.
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006021 }
6022
6023 public static class HistoryPrinter {
6024 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006025 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006026 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006027 int oldStatus = -1;
6028 int oldHealth = -1;
6029 int oldPlug = -1;
6030 int oldTemp = -1;
6031 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006032 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006033 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006034
Dianne Hackborn3251b902014-06-20 14:40:53 -07006035 void reset() {
6036 oldState = oldState2 = 0;
6037 oldLevel = -1;
6038 oldStatus = -1;
6039 oldHealth = -1;
6040 oldPlug = -1;
6041 oldTemp = -1;
6042 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006043 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07006044 }
6045
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006046 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006047 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006048 if (!checkin) {
6049 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006050 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006051 pw.print(" (");
6052 pw.print(rec.numReadInts);
6053 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006054 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006055 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6056 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006057 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006058 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006059 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006060 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006061 }
6062 lastTime = rec.time;
6063 }
6064 if (rec.cmd == HistoryItem.CMD_START) {
6065 if (checkin) {
6066 pw.print(":");
6067 }
6068 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006069 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006070 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
6071 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006072 if (checkin) {
6073 pw.print(":");
6074 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006075 if (rec.cmd == HistoryItem.CMD_RESET) {
6076 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006077 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006078 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006079 pw.print("TIME:");
6080 if (checkin) {
6081 pw.println(rec.currentTime);
6082 } else {
6083 pw.print(" ");
6084 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6085 rec.currentTime).toString());
6086 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006087 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
6088 if (checkin) {
6089 pw.print(":");
6090 }
6091 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006092 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
6093 if (checkin) {
6094 pw.print(":");
6095 }
6096 pw.println("*OVERFLOW*");
6097 } else {
6098 if (!checkin) {
6099 if (rec.batteryLevel < 10) pw.print("00");
6100 else if (rec.batteryLevel < 100) pw.print("0");
6101 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006102 if (verbose) {
6103 pw.print(" ");
6104 if (rec.states < 0) ;
6105 else if (rec.states < 0x10) pw.print("0000000");
6106 else if (rec.states < 0x100) pw.print("000000");
6107 else if (rec.states < 0x1000) pw.print("00000");
6108 else if (rec.states < 0x10000) pw.print("0000");
6109 else if (rec.states < 0x100000) pw.print("000");
6110 else if (rec.states < 0x1000000) pw.print("00");
6111 else if (rec.states < 0x10000000) pw.print("0");
6112 pw.print(Integer.toHexString(rec.states));
6113 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006114 } else {
6115 if (oldLevel != rec.batteryLevel) {
6116 oldLevel = rec.batteryLevel;
6117 pw.print(",Bl="); pw.print(rec.batteryLevel);
6118 }
6119 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006120 if (oldStatus != rec.batteryStatus) {
6121 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006122 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006123 switch (oldStatus) {
6124 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006125 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006126 break;
6127 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006128 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006129 break;
6130 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006131 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006132 break;
6133 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006134 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006135 break;
6136 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006137 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006138 break;
6139 default:
6140 pw.print(oldStatus);
6141 break;
6142 }
6143 }
6144 if (oldHealth != rec.batteryHealth) {
6145 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006146 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006147 switch (oldHealth) {
6148 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006149 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006150 break;
6151 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006152 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006153 break;
6154 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006155 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006156 break;
6157 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006158 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006159 break;
6160 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006161 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006162 break;
6163 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006164 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006165 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006166 case BatteryManager.BATTERY_HEALTH_COLD:
6167 pw.print(checkin ? "c" : "cold");
6168 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006169 default:
6170 pw.print(oldHealth);
6171 break;
6172 }
6173 }
6174 if (oldPlug != rec.batteryPlugType) {
6175 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006176 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006177 switch (oldPlug) {
6178 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006179 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006180 break;
6181 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006182 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006183 break;
6184 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006185 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006186 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006187 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006188 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006189 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006190 default:
6191 pw.print(oldPlug);
6192 break;
6193 }
6194 }
6195 if (oldTemp != rec.batteryTemperature) {
6196 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006197 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006198 pw.print(oldTemp);
6199 }
6200 if (oldVolt != rec.batteryVoltage) {
6201 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006202 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006203 pw.print(oldVolt);
6204 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006205 final int chargeMAh = rec.batteryChargeUAh / 1000;
6206 if (oldChargeMAh != chargeMAh) {
6207 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07006208 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006209 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07006210 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006211 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006212 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006213 printBitDescriptions(pw, oldState2, rec.states2, null,
6214 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006215 if (rec.wakeReasonTag != null) {
6216 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006217 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006218 pw.print(rec.wakeReasonTag.poolIdx);
6219 } else {
6220 pw.print(" wake_reason=");
6221 pw.print(rec.wakeReasonTag.uid);
6222 pw.print(":\"");
6223 pw.print(rec.wakeReasonTag.string);
6224 pw.print("\"");
6225 }
6226 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08006227 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006228 pw.print(checkin ? "," : " ");
6229 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
6230 pw.print("+");
6231 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
6232 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006233 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006234 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
6235 : HISTORY_EVENT_NAMES;
6236 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
6237 | HistoryItem.EVENT_FLAG_FINISH);
6238 if (idx >= 0 && idx < eventNames.length) {
6239 pw.print(eventNames[idx]);
6240 } else {
6241 pw.print(checkin ? "Ev" : "event");
6242 pw.print(idx);
6243 }
6244 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006245 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006246 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08006247 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08006248 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
6249 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006250 pw.print(":\"");
6251 pw.print(rec.eventTag.string);
6252 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006253 }
6254 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006255 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006256 if (rec.stepDetails != null) {
6257 if (!checkin) {
6258 pw.print(" Details: cpu=");
6259 pw.print(rec.stepDetails.userTime);
6260 pw.print("u+");
6261 pw.print(rec.stepDetails.systemTime);
6262 pw.print("s");
6263 if (rec.stepDetails.appCpuUid1 >= 0) {
6264 pw.print(" (");
6265 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
6266 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6267 if (rec.stepDetails.appCpuUid2 >= 0) {
6268 pw.print(", ");
6269 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
6270 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6271 }
6272 if (rec.stepDetails.appCpuUid3 >= 0) {
6273 pw.print(", ");
6274 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
6275 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6276 }
6277 pw.print(')');
6278 }
6279 pw.println();
6280 pw.print(" /proc/stat=");
6281 pw.print(rec.stepDetails.statUserTime);
6282 pw.print(" usr, ");
6283 pw.print(rec.stepDetails.statSystemTime);
6284 pw.print(" sys, ");
6285 pw.print(rec.stepDetails.statIOWaitTime);
6286 pw.print(" io, ");
6287 pw.print(rec.stepDetails.statIrqTime);
6288 pw.print(" irq, ");
6289 pw.print(rec.stepDetails.statSoftIrqTime);
6290 pw.print(" sirq, ");
6291 pw.print(rec.stepDetails.statIdlTime);
6292 pw.print(" idle");
6293 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
6294 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
6295 + rec.stepDetails.statSoftIrqTime;
6296 int total = totalRun + rec.stepDetails.statIdlTime;
6297 if (total > 0) {
6298 pw.print(" (");
6299 float perc = ((float)totalRun) / ((float)total) * 100;
6300 pw.print(String.format("%.1f%%", perc));
6301 pw.print(" of ");
6302 StringBuilder sb = new StringBuilder(64);
6303 formatTimeMsNoSpace(sb, total*10);
6304 pw.print(sb);
6305 pw.print(")");
6306 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006307 pw.print(", PlatformIdleStat ");
6308 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006309 pw.println();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006310
6311 pw.print(", SubsystemPowerState ");
6312 pw.print(rec.stepDetails.statSubsystemPowerState);
6313 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006314 } else {
6315 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6316 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
6317 pw.print(rec.stepDetails.userTime);
6318 pw.print(":");
6319 pw.print(rec.stepDetails.systemTime);
6320 if (rec.stepDetails.appCpuUid1 >= 0) {
6321 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
6322 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6323 if (rec.stepDetails.appCpuUid2 >= 0) {
6324 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
6325 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6326 }
6327 if (rec.stepDetails.appCpuUid3 >= 0) {
6328 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
6329 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6330 }
6331 }
6332 pw.println();
6333 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6334 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
6335 pw.print(rec.stepDetails.statUserTime);
6336 pw.print(',');
6337 pw.print(rec.stepDetails.statSystemTime);
6338 pw.print(',');
6339 pw.print(rec.stepDetails.statIOWaitTime);
6340 pw.print(',');
6341 pw.print(rec.stepDetails.statIrqTime);
6342 pw.print(',');
6343 pw.print(rec.stepDetails.statSoftIrqTime);
6344 pw.print(',');
6345 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006346 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006347 if (rec.stepDetails.statPlatformIdleState != null) {
6348 pw.print(rec.stepDetails.statPlatformIdleState);
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006349 if (rec.stepDetails.statSubsystemPowerState != null) {
6350 pw.print(',');
6351 }
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006352 }
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006353
6354 if (rec.stepDetails.statSubsystemPowerState != null) {
6355 pw.print(rec.stepDetails.statSubsystemPowerState);
6356 }
6357 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006358 }
6359 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006360 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006361 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006362 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006363 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006364
6365 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
6366 UserHandle.formatUid(pw, uid);
6367 pw.print("=");
6368 pw.print(utime);
6369 pw.print("u+");
6370 pw.print(stime);
6371 pw.print("s");
6372 }
6373
6374 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
6375 pw.print('/');
6376 pw.print(uid);
6377 pw.print(":");
6378 pw.print(utime);
6379 pw.print(":");
6380 pw.print(stime);
6381 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006382 }
6383
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006384 private void printSizeValue(PrintWriter pw, long size) {
6385 float result = size;
6386 String suffix = "";
6387 if (result >= 10*1024) {
6388 suffix = "KB";
6389 result = result / 1024;
6390 }
6391 if (result >= 10*1024) {
6392 suffix = "MB";
6393 result = result / 1024;
6394 }
6395 if (result >= 10*1024) {
6396 suffix = "GB";
6397 result = result / 1024;
6398 }
6399 if (result >= 10*1024) {
6400 suffix = "TB";
6401 result = result / 1024;
6402 }
6403 if (result >= 10*1024) {
6404 suffix = "PB";
6405 result = result / 1024;
6406 }
6407 pw.print((int)result);
6408 pw.print(suffix);
6409 }
6410
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006411 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
6412 String label3, long estimatedTime) {
6413 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006414 return false;
6415 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006416 pw.print(label1);
6417 pw.print(label2);
6418 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006419 StringBuilder sb = new StringBuilder(64);
6420 formatTimeMs(sb, estimatedTime);
6421 pw.print(sb);
6422 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006423 return true;
6424 }
6425
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006426 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
6427 LevelStepTracker steps, boolean checkin) {
6428 if (steps == null) {
6429 return false;
6430 }
6431 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006432 if (count <= 0) {
6433 return false;
6434 }
6435 if (!checkin) {
6436 pw.println(header);
6437 }
Kweku Adams030980a2015-04-01 16:07:48 -07006438 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006439 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006440 long duration = steps.getDurationAt(i);
6441 int level = steps.getLevelAt(i);
6442 long initMode = steps.getInitModeAt(i);
6443 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006444 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006445 lineArgs[0] = Long.toString(duration);
6446 lineArgs[1] = Integer.toString(level);
6447 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6448 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6449 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
6450 case Display.STATE_ON: lineArgs[2] = "s+"; break;
6451 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
6452 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07006453 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006454 }
6455 } else {
6456 lineArgs[2] = "";
6457 }
6458 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6459 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
6460 } else {
6461 lineArgs[3] = "";
6462 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006463 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07006464 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006465 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07006466 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006467 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006468 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
6469 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006470 pw.print(prefix);
6471 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006472 TimeUtils.formatDuration(duration, pw);
6473 pw.print(" to "); pw.print(level);
6474 boolean haveModes = false;
6475 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6476 pw.print(" (");
6477 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6478 case Display.STATE_OFF: pw.print("screen-off"); break;
6479 case Display.STATE_ON: pw.print("screen-on"); break;
6480 case Display.STATE_DOZE: pw.print("screen-doze"); break;
6481 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07006482 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006483 }
6484 haveModes = true;
6485 }
6486 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6487 pw.print(haveModes ? ", " : " (");
6488 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
6489 ? "power-save-on" : "power-save-off");
6490 haveModes = true;
6491 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006492 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6493 pw.print(haveModes ? ", " : " (");
6494 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6495 ? "device-idle-on" : "device-idle-off");
6496 haveModes = true;
6497 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006498 if (haveModes) {
6499 pw.print(")");
6500 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006501 pw.println();
6502 }
6503 }
6504 return true;
6505 }
6506
Kweku Adams87b19ec2017-10-09 12:40:03 -07006507 private static void dumpDurationSteps(ProtoOutputStream proto, long fieldId,
6508 LevelStepTracker steps) {
6509 if (steps == null) {
6510 return;
6511 }
6512 int count = steps.mNumStepDurations;
Kweku Adams87b19ec2017-10-09 12:40:03 -07006513 for (int i = 0; i < count; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07006514 long token = proto.start(fieldId);
Kweku Adams87b19ec2017-10-09 12:40:03 -07006515 proto.write(SystemProto.BatteryLevelStep.DURATION_MS, steps.getDurationAt(i));
6516 proto.write(SystemProto.BatteryLevelStep.LEVEL, steps.getLevelAt(i));
6517
6518 final long initMode = steps.getInitModeAt(i);
6519 final long modMode = steps.getModModeAt(i);
6520
6521 int ds = SystemProto.BatteryLevelStep.DS_MIXED;
6522 if ((modMode & STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6523 switch ((int) (initMode & STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6524 case Display.STATE_OFF:
6525 ds = SystemProto.BatteryLevelStep.DS_OFF;
6526 break;
6527 case Display.STATE_ON:
6528 ds = SystemProto.BatteryLevelStep.DS_ON;
6529 break;
6530 case Display.STATE_DOZE:
6531 ds = SystemProto.BatteryLevelStep.DS_DOZE;
6532 break;
6533 case Display.STATE_DOZE_SUSPEND:
6534 ds = SystemProto.BatteryLevelStep.DS_DOZE_SUSPEND;
6535 break;
6536 default:
6537 ds = SystemProto.BatteryLevelStep.DS_ERROR;
6538 break;
6539 }
6540 }
6541 proto.write(SystemProto.BatteryLevelStep.DISPLAY_STATE, ds);
6542
6543 int psm = SystemProto.BatteryLevelStep.PSM_MIXED;
6544 if ((modMode & STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6545 psm = (initMode & STEP_LEVEL_MODE_POWER_SAVE) != 0
6546 ? SystemProto.BatteryLevelStep.PSM_ON : SystemProto.BatteryLevelStep.PSM_OFF;
6547 }
6548 proto.write(SystemProto.BatteryLevelStep.POWER_SAVE_MODE, psm);
6549
6550 int im = SystemProto.BatteryLevelStep.IM_MIXED;
6551 if ((modMode & STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6552 im = (initMode & STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6553 ? SystemProto.BatteryLevelStep.IM_ON : SystemProto.BatteryLevelStep.IM_OFF;
6554 }
6555 proto.write(SystemProto.BatteryLevelStep.IDLE_MODE, im);
6556
6557 proto.end(token);
6558 }
6559 }
6560
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006561 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006562 public static final int DUMP_DAILY_ONLY = 1<<2;
6563 public static final int DUMP_HISTORY_ONLY = 1<<3;
6564 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
6565 public static final int DUMP_VERBOSE = 1<<5;
6566 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006567
Dianne Hackborn37de0982014-05-09 09:32:18 -07006568 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
6569 final HistoryPrinter hprinter = new HistoryPrinter();
6570 final HistoryItem rec = new HistoryItem();
6571 long lastTime = -1;
6572 long baseTime = -1;
6573 boolean printed = false;
6574 HistoryEventTracker tracker = null;
6575 while (getNextHistoryLocked(rec)) {
6576 lastTime = rec.time;
6577 if (baseTime < 0) {
6578 baseTime = lastTime;
6579 }
6580 if (rec.time >= histStart) {
6581 if (histStart >= 0 && !printed) {
6582 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07006583 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006584 || rec.cmd == HistoryItem.CMD_START
6585 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006586 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006587 hprinter.printNextItem(pw, rec, baseTime, checkin,
6588 (flags&DUMP_VERBOSE) != 0);
6589 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006590 } else if (rec.currentTime != 0) {
6591 printed = true;
6592 byte cmd = rec.cmd;
6593 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006594 hprinter.printNextItem(pw, rec, baseTime, checkin,
6595 (flags&DUMP_VERBOSE) != 0);
6596 rec.cmd = cmd;
6597 }
6598 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006599 if (rec.cmd != HistoryItem.CMD_UPDATE) {
6600 hprinter.printNextItem(pw, rec, baseTime, checkin,
6601 (flags&DUMP_VERBOSE) != 0);
6602 rec.cmd = HistoryItem.CMD_UPDATE;
6603 }
6604 int oldEventCode = rec.eventCode;
6605 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006606 rec.eventTag = new HistoryTag();
6607 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
6608 HashMap<String, SparseIntArray> active
6609 = tracker.getStateForEvent(i);
6610 if (active == null) {
6611 continue;
6612 }
6613 for (HashMap.Entry<String, SparseIntArray> ent
6614 : active.entrySet()) {
6615 SparseIntArray uids = ent.getValue();
6616 for (int j=0; j<uids.size(); j++) {
6617 rec.eventCode = i;
6618 rec.eventTag.string = ent.getKey();
6619 rec.eventTag.uid = uids.keyAt(j);
6620 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006621 hprinter.printNextItem(pw, rec, baseTime, checkin,
6622 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006623 rec.wakeReasonTag = null;
6624 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006625 }
6626 }
6627 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006628 rec.eventCode = oldEventCode;
6629 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006630 tracker = null;
6631 }
6632 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006633 hprinter.printNextItem(pw, rec, baseTime, checkin,
6634 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07006635 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
6636 // This is an attempt to aggregate the previous state and generate
6637 // fake events to reflect that state at the point where we start
6638 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07006639 if (tracker == null) {
6640 tracker = new HistoryEventTracker();
6641 }
6642 tracker.updateState(rec.eventCode, rec.eventTag.string,
6643 rec.eventTag.uid, rec.eventTag.poolIdx);
6644 }
6645 }
6646 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07006647 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006648 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
6649 }
6650 }
6651
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006652 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
6653 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
6654 if (steps == null) {
6655 return;
6656 }
6657 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
6658 if (timeRemaining >= 0) {
6659 pw.print(prefix); pw.print(label); pw.print(" total time: ");
6660 tmpSb.setLength(0);
6661 formatTimeMs(tmpSb, timeRemaining);
6662 pw.print(tmpSb);
6663 pw.print(" (from "); pw.print(tmpOutInt[0]);
6664 pw.println(" steps)");
6665 }
6666 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6667 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6668 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
6669 if (estimatedTime > 0) {
6670 pw.print(prefix); pw.print(label); pw.print(" ");
6671 pw.print(STEP_LEVEL_MODE_LABELS[i]);
6672 pw.print(" time: ");
6673 tmpSb.setLength(0);
6674 formatTimeMs(tmpSb, estimatedTime);
6675 pw.print(tmpSb);
6676 pw.print(" (from "); pw.print(tmpOutInt[0]);
6677 pw.println(" steps)");
6678 }
6679 }
6680 }
6681
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006682 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
6683 ArrayList<PackageChange> changes) {
6684 if (changes == null) {
6685 return;
6686 }
6687 pw.print(prefix); pw.println("Package changes:");
6688 for (int i=0; i<changes.size(); i++) {
6689 PackageChange pc = changes.get(i);
6690 if (pc.mUpdate) {
6691 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
6692 pw.print(" vers="); pw.println(pc.mVersionCode);
6693 } else {
6694 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
6695 }
6696 }
6697 }
6698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006699 /**
6700 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
6701 *
6702 * @param pw a Printer to receive the dump output.
6703 */
6704 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006705 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006706 prepareForDumpLocked();
6707
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006708 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006709 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006710
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006711 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006712 final long historyTotalSize = getHistoryTotalSize();
6713 final long historyUsedSize = getHistoryUsedSize();
6714 if (startIteratingHistoryLocked()) {
6715 try {
6716 pw.print("Battery History (");
6717 pw.print((100*historyUsedSize)/historyTotalSize);
6718 pw.print("% used, ");
6719 printSizeValue(pw, historyUsedSize);
6720 pw.print(" used of ");
6721 printSizeValue(pw, historyTotalSize);
6722 pw.print(", ");
6723 pw.print(getHistoryStringPoolSize());
6724 pw.print(" strings using ");
6725 printSizeValue(pw, getHistoryStringPoolBytes());
6726 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07006727 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006728 pw.println();
6729 } finally {
6730 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006731 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006732 }
6733
6734 if (startIteratingOldHistoryLocked()) {
6735 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006736 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006737 pw.println("Old battery History:");
6738 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006739 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006740 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006741 if (baseTime < 0) {
6742 baseTime = rec.time;
6743 }
6744 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006745 }
6746 pw.println();
6747 } finally {
6748 finishIteratingOldHistoryLocked();
6749 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006750 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006751 }
6752
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006753 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006754 return;
6755 }
6756
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006757 if (!filtering) {
6758 SparseArray<? extends Uid> uidStats = getUidStats();
6759 final int NU = uidStats.size();
6760 boolean didPid = false;
6761 long nowRealtime = SystemClock.elapsedRealtime();
6762 for (int i=0; i<NU; i++) {
6763 Uid uid = uidStats.valueAt(i);
6764 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
6765 if (pids != null) {
6766 for (int j=0; j<pids.size(); j++) {
6767 Uid.Pid pid = pids.valueAt(j);
6768 if (!didPid) {
6769 pw.println("Per-PID Stats:");
6770 didPid = true;
6771 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006772 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
6773 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006774 pw.print(" PID "); pw.print(pids.keyAt(j));
6775 pw.print(" wake time: ");
6776 TimeUtils.formatDuration(time, pw);
6777 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006778 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006779 }
6780 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006781 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006782 pw.println();
6783 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006784 }
6785
6786 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006787 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
6788 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006789 long timeRemaining = computeBatteryTimeRemaining(
6790 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006791 if (timeRemaining >= 0) {
6792 pw.print(" Estimated discharge time remaining: ");
6793 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6794 pw.println();
6795 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006796 final LevelStepTracker steps = getDischargeLevelStepTracker();
6797 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6798 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
6799 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6800 STEP_LEVEL_MODE_VALUES[i], null));
6801 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006802 pw.println();
6803 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006804 if (dumpDurationSteps(pw, " ", "Charge step durations:",
6805 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006806 long timeRemaining = computeChargeTimeRemaining(
6807 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006808 if (timeRemaining >= 0) {
6809 pw.print(" Estimated charge time remaining: ");
6810 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6811 pw.println();
6812 }
6813 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006814 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006815 }
Dianne Hackbornc81983a2017-10-20 16:16:32 -07006816 if (!filtering || (flags & DUMP_DAILY_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006817 pw.println("Daily stats:");
6818 pw.print(" Current start time: ");
6819 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6820 getCurrentDailyStartTime()).toString());
6821 pw.print(" Next min deadline: ");
6822 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6823 getNextMinDailyDeadline()).toString());
6824 pw.print(" Next max deadline: ");
6825 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6826 getNextMaxDailyDeadline()).toString());
6827 StringBuilder sb = new StringBuilder(64);
6828 int[] outInt = new int[1];
6829 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
6830 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006831 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
6832 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006833 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006834 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
6835 dsteps, false)) {
6836 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6837 sb, outInt);
6838 }
6839 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6840 csteps, false)) {
6841 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6842 sb, outInt);
6843 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006844 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006845 } else {
6846 pw.println(" Current daily steps:");
6847 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6848 sb, outInt);
6849 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6850 sb, outInt);
6851 }
6852 }
6853 DailyItem dit;
6854 int curIndex = 0;
6855 while ((dit=getDailyItemLocked(curIndex)) != null) {
6856 curIndex++;
6857 if ((flags&DUMP_DAILY_ONLY) != 0) {
6858 pw.println();
6859 }
6860 pw.print(" Daily from ");
6861 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6862 pw.print(" to ");
6863 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6864 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006865 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006866 if (dumpDurationSteps(pw, " ",
6867 " Discharge step durations:", dit.mDischargeSteps, false)) {
6868 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6869 sb, outInt);
6870 }
6871 if (dumpDurationSteps(pw, " ",
6872 " Charge step durations:", dit.mChargeSteps, false)) {
6873 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6874 sb, outInt);
6875 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006876 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006877 } else {
6878 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6879 sb, outInt);
6880 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6881 sb, outInt);
6882 }
6883 }
6884 pw.println();
6885 }
6886 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006887 pw.println("Statistics since last charge:");
6888 pw.println(" System starts: " + getStartCount()
6889 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006890 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6891 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006892 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006893 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006894 }
Mike Mac2f518a2017-09-19 16:06:03 -07006895
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006896 // This is called from BatteryStatsService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006897 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006898 public void dumpCheckinLocked(Context context, PrintWriter pw,
6899 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006900 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006901
6902 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006903 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6904 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006905
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006906 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6907
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006908 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07006909 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006910 try {
6911 for (int i=0; i<getHistoryStringPoolSize(); i++) {
6912 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6913 pw.print(HISTORY_STRING_POOL); pw.print(',');
6914 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006915 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006916 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006917 pw.print(",\"");
6918 String str = getHistoryTagPoolString(i);
6919 str = str.replace("\\", "\\\\");
6920 str = str.replace("\"", "\\\"");
6921 pw.print(str);
6922 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006923 pw.println();
6924 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006925 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006926 } finally {
6927 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08006928 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006929 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006930 }
6931
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006932 if ((flags & DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006933 return;
6934 }
6935
Dianne Hackborne4a59512010-12-07 11:08:07 -08006936 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006937 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08006938 for (int i=0; i<apps.size(); i++) {
6939 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006940 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
6941 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08006942 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006943 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
6944 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006945 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006946 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006947 }
6948 SparseArray<? extends Uid> uidStats = getUidStats();
6949 final int NU = uidStats.size();
6950 String[] lineArgs = new String[2];
6951 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006952 int uid = UserHandle.getAppId(uidStats.keyAt(i));
6953 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
6954 if (pkgs != null && !pkgs.second.value) {
6955 pkgs.second.value = true;
6956 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08006957 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006958 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006959 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
6960 (Object[])lineArgs);
6961 }
6962 }
6963 }
6964 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006965 if ((flags & DUMP_DAILY_ONLY) == 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006966 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006967 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07006968 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006969 if (timeRemaining >= 0) {
6970 lineArgs[0] = Long.toString(timeRemaining);
6971 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
6972 (Object[])lineArgs);
6973 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006974 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07006975 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006976 if (timeRemaining >= 0) {
6977 lineArgs[0] = Long.toString(timeRemaining);
6978 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
6979 (Object[])lineArgs);
6980 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006981 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
6982 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006983 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006984 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006985
Kweku Adams87b19ec2017-10-09 12:40:03 -07006986 /** Dump #STATS_SINCE_CHARGED batterystats data to a proto. @hide */
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006987 public void dumpProtoLocked(Context context, FileDescriptor fd, List<ApplicationInfo> apps,
Kweku Adams6ccebf22017-12-11 12:30:35 -08006988 int flags) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006989 final ProtoOutputStream proto = new ProtoOutputStream(fd);
6990 final long bToken = proto.start(BatteryStatsServiceDumpProto.BATTERYSTATS);
6991 prepareForDumpLocked();
6992
6993 proto.write(BatteryStatsProto.REPORT_VERSION, CHECKIN_VERSION);
6994 proto.write(BatteryStatsProto.PARCEL_VERSION, getParcelVersion());
6995 proto.write(BatteryStatsProto.START_PLATFORM_VERSION, getStartPlatformVersion());
6996 proto.write(BatteryStatsProto.END_PLATFORM_VERSION, getEndPlatformVersion());
6997
Kweku Adams6ccebf22017-12-11 12:30:35 -08006998 // History intentionally not included in proto dump.
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006999
7000 if ((flags & (DUMP_HISTORY_ONLY | DUMP_DAILY_ONLY)) == 0) {
Kweku Adams103351f2017-10-16 14:39:34 -07007001 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false,
7002 (flags & DUMP_DEVICE_WIFI_ONLY) != 0);
7003 helper.create(this);
7004 helper.refreshStats(STATS_SINCE_CHARGED, UserHandle.USER_ALL);
7005
7006 dumpProtoAppsLocked(proto, helper, apps);
7007 dumpProtoSystemLocked(proto, helper);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007008 }
7009
7010 proto.end(bToken);
7011 proto.flush();
7012 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07007013
Kweku Adams103351f2017-10-16 14:39:34 -07007014 private void dumpProtoAppsLocked(ProtoOutputStream proto, BatteryStatsHelper helper,
7015 List<ApplicationInfo> apps) {
7016 final int which = STATS_SINCE_CHARGED;
7017 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7018 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7019 final long rawRealtimeUs = rawRealtimeMs * 1000;
7020 final long batteryUptimeUs = getBatteryUptime(rawUptimeUs);
7021
7022 SparseArray<ArrayList<String>> aidToPackages = new SparseArray<>();
7023 if (apps != null) {
7024 for (int i = 0; i < apps.size(); ++i) {
7025 ApplicationInfo ai = apps.get(i);
7026 int aid = UserHandle.getAppId(ai.uid);
7027 ArrayList<String> pkgs = aidToPackages.get(aid);
7028 if (pkgs == null) {
7029 pkgs = new ArrayList<String>();
7030 aidToPackages.put(aid, pkgs);
7031 }
7032 pkgs.add(ai.packageName);
7033 }
7034 }
7035
7036 SparseArray<BatterySipper> uidToSipper = new SparseArray<>();
7037 final List<BatterySipper> sippers = helper.getUsageList();
7038 if (sippers != null) {
7039 for (int i = 0; i < sippers.size(); ++i) {
7040 final BatterySipper bs = sippers.get(i);
7041 if (bs.drainType != BatterySipper.DrainType.APP) {
7042 // Others are handled by dumpProtoSystemLocked()
7043 continue;
7044 }
7045 uidToSipper.put(bs.uidObj.getUid(), bs);
7046 }
7047 }
7048
7049 SparseArray<? extends Uid> uidStats = getUidStats();
7050 final int n = uidStats.size();
7051 for (int iu = 0; iu < n; ++iu) {
7052 final long uTkn = proto.start(BatteryStatsProto.UIDS);
7053 final Uid u = uidStats.valueAt(iu);
7054
7055 final int uid = uidStats.keyAt(iu);
7056 proto.write(UidProto.UID, uid);
7057
7058 // Print packages and apk stats (UID_DATA & APK_DATA)
7059 ArrayList<String> pkgs = aidToPackages.get(UserHandle.getAppId(uid));
7060 if (pkgs == null) {
7061 pkgs = new ArrayList<String>();
7062 }
7063 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats =
7064 u.getPackageStats();
7065 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7066 String pkg = packageStats.keyAt(ipkg);
7067 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats =
7068 packageStats.valueAt(ipkg).getServiceStats();
7069 if (serviceStats.size() == 0) {
7070 // Due to the way ActivityManagerService logs wakeup alarms, some packages (for
7071 // example, "android") may be included in the packageStats that aren't part of
7072 // the UID. If they don't have any services, then they shouldn't be listed here.
7073 // These packages won't be a part in the pkgs List.
7074 continue;
7075 }
7076
7077 final long pToken = proto.start(UidProto.PACKAGES);
7078 proto.write(UidProto.Package.NAME, pkg);
7079 // Remove from the packages list since we're logging it here.
7080 pkgs.remove(pkg);
7081
7082 for (int isvc = serviceStats.size() - 1; isvc >= 0; --isvc) {
7083 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
7084 long sToken = proto.start(UidProto.Package.SERVICES);
7085
7086 proto.write(UidProto.Package.Service.NAME, serviceStats.keyAt(isvc));
7087 proto.write(UidProto.Package.Service.START_DURATION_MS,
7088 roundUsToMs(ss.getStartTime(batteryUptimeUs, which)));
7089 proto.write(UidProto.Package.Service.START_COUNT, ss.getStarts(which));
7090 proto.write(UidProto.Package.Service.LAUNCH_COUNT, ss.getLaunches(which));
7091
7092 proto.end(sToken);
7093 }
7094 proto.end(pToken);
7095 }
7096 // Print any remaining packages that weren't in the packageStats map. pkgs is pulled
7097 // from PackageManager data. Packages are only included in packageStats if there was
7098 // specific data tracked for them (services and wakeup alarms, etc.).
7099 for (String p : pkgs) {
7100 final long pToken = proto.start(UidProto.PACKAGES);
7101 proto.write(UidProto.Package.NAME, p);
7102 proto.end(pToken);
7103 }
7104
7105 // Total wakelock data (AGGREGATED_WAKELOCK_DATA)
7106 if (u.getAggregatedPartialWakelockTimer() != null) {
7107 final Timer timer = u.getAggregatedPartialWakelockTimer();
7108 // Times are since reset (regardless of 'which')
7109 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
7110 final Timer bgTimer = timer.getSubTimer();
7111 final long bgTimeMs = bgTimer != null
7112 ? bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
7113 final long awToken = proto.start(UidProto.AGGREGATED_WAKELOCK);
7114 proto.write(UidProto.AggregatedWakelock.PARTIAL_DURATION_MS, totTimeMs);
7115 proto.write(UidProto.AggregatedWakelock.BACKGROUND_PARTIAL_DURATION_MS, bgTimeMs);
7116 proto.end(awToken);
7117 }
7118
7119 // Audio (AUDIO_DATA)
7120 dumpTimer(proto, UidProto.AUDIO, u.getAudioTurnedOnTimer(), rawRealtimeUs, which);
7121
7122 // Bluetooth Controller (BLUETOOTH_CONTROLLER_DATA)
7123 dumpControllerActivityProto(proto, UidProto.BLUETOOTH_CONTROLLER,
7124 u.getBluetoothControllerActivity(), which);
7125
7126 // BLE scans (BLUETOOTH_MISC_DATA) (uses totalDurationMsLocked and MaxDurationMsLocked)
7127 final Timer bleTimer = u.getBluetoothScanTimer();
7128 if (bleTimer != null) {
7129 final long bmToken = proto.start(UidProto.BLUETOOTH_MISC);
7130
7131 dumpTimer(proto, UidProto.BluetoothMisc.APPORTIONED_BLE_SCAN, bleTimer,
7132 rawRealtimeUs, which);
7133 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN,
7134 u.getBluetoothScanBackgroundTimer(), rawRealtimeUs, which);
7135 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
7136 dumpTimer(proto, UidProto.BluetoothMisc.UNOPTIMIZED_BLE_SCAN,
7137 u.getBluetoothUnoptimizedScanTimer(), rawRealtimeUs, which);
7138 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
7139 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_UNOPTIMIZED_BLE_SCAN,
7140 u.getBluetoothUnoptimizedScanBackgroundTimer(), rawRealtimeUs, which);
7141 // Result counters
7142 proto.write(UidProto.BluetoothMisc.BLE_SCAN_RESULT_COUNT,
7143 u.getBluetoothScanResultCounter() != null
7144 ? u.getBluetoothScanResultCounter().getCountLocked(which) : 0);
7145 proto.write(UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN_RESULT_COUNT,
7146 u.getBluetoothScanResultBgCounter() != null
7147 ? u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0);
7148
7149 proto.end(bmToken);
7150 }
7151
7152 // Camera (CAMERA_DATA)
7153 dumpTimer(proto, UidProto.CAMERA, u.getCameraTurnedOnTimer(), rawRealtimeUs, which);
7154
7155 // CPU stats (CPU_DATA & CPU_TIMES_AT_FREQ_DATA)
7156 final long cpuToken = proto.start(UidProto.CPU);
7157 proto.write(UidProto.Cpu.USER_DURATION_MS, roundUsToMs(u.getUserCpuTimeUs(which)));
7158 proto.write(UidProto.Cpu.SYSTEM_DURATION_MS, roundUsToMs(u.getSystemCpuTimeUs(which)));
7159
7160 final long[] cpuFreqs = getCpuFreqs();
7161 if (cpuFreqs != null) {
7162 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
7163 // If total cpuFreqTimes is null, then we don't need to check for
7164 // screenOffCpuFreqTimes.
7165 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
7166 long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
7167 if (screenOffCpuFreqTimeMs == null) {
7168 screenOffCpuFreqTimeMs = new long[cpuFreqTimeMs.length];
7169 }
7170 for (int ic = 0; ic < cpuFreqTimeMs.length; ++ic) {
7171 long cToken = proto.start(UidProto.Cpu.BY_FREQUENCY);
7172 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7173 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7174 cpuFreqTimeMs[ic]);
7175 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7176 screenOffCpuFreqTimeMs[ic]);
7177 proto.end(cToken);
7178 }
7179 }
7180 }
Sudheer Shanka6d658d72018-01-01 01:36:49 -08007181
7182 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
7183 final long[] timesMs = u.getCpuFreqTimes(which, procState);
7184 if (timesMs != null && timesMs.length == cpuFreqs.length) {
7185 long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(which, procState);
7186 if (screenOffTimesMs == null) {
7187 screenOffTimesMs = new long[timesMs.length];
7188 }
7189 final long procToken = proto.start(UidProto.Cpu.BY_PROCESS_STATE);
7190 proto.write(UidProto.Cpu.ByProcessState.PROCESS_STATE, procState);
7191 for (int ic = 0; ic < timesMs.length; ++ic) {
7192 long cToken = proto.start(UidProto.Cpu.ByProcessState.BY_FREQUENCY);
7193 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7194 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7195 timesMs[ic]);
7196 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7197 screenOffTimesMs[ic]);
7198 proto.end(cToken);
7199 }
7200 proto.end(procToken);
7201 }
7202 }
Kweku Adams103351f2017-10-16 14:39:34 -07007203 proto.end(cpuToken);
7204
7205 // Flashlight (FLASHLIGHT_DATA)
7206 dumpTimer(proto, UidProto.FLASHLIGHT, u.getFlashlightTurnedOnTimer(),
7207 rawRealtimeUs, which);
7208
7209 // Foreground activity (FOREGROUND_ACTIVITY_DATA)
7210 dumpTimer(proto, UidProto.FOREGROUND_ACTIVITY, u.getForegroundActivityTimer(),
7211 rawRealtimeUs, which);
7212
7213 // Foreground service (FOREGROUND_SERVICE_DATA)
7214 dumpTimer(proto, UidProto.FOREGROUND_SERVICE, u.getForegroundServiceTimer(),
7215 rawRealtimeUs, which);
7216
7217 // Job completion (JOB_COMPLETION_DATA)
7218 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
7219 final int[] reasons = new int[]{
7220 JobParameters.REASON_CANCELED,
7221 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED,
7222 JobParameters.REASON_PREEMPT,
7223 JobParameters.REASON_TIMEOUT,
7224 JobParameters.REASON_DEVICE_IDLE,
7225 };
7226 for (int ic = 0; ic < completions.size(); ++ic) {
7227 SparseIntArray types = completions.valueAt(ic);
7228 if (types != null) {
7229 final long jcToken = proto.start(UidProto.JOB_COMPLETION);
7230
7231 proto.write(UidProto.JobCompletion.NAME, completions.keyAt(ic));
7232
7233 for (int r : reasons) {
7234 long rToken = proto.start(UidProto.JobCompletion.REASON_COUNT);
7235 proto.write(UidProto.JobCompletion.ReasonCount.NAME, r);
7236 proto.write(UidProto.JobCompletion.ReasonCount.COUNT, types.get(r, 0));
7237 proto.end(rToken);
7238 }
7239
7240 proto.end(jcToken);
7241 }
7242 }
7243
7244 // Scheduled jobs (JOB_DATA)
7245 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
7246 for (int ij = jobs.size() - 1; ij >= 0; --ij) {
7247 final Timer timer = jobs.valueAt(ij);
7248 final Timer bgTimer = timer.getSubTimer();
7249 final long jToken = proto.start(UidProto.JOBS);
7250
7251 proto.write(UidProto.Job.NAME, jobs.keyAt(ij));
7252 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7253 dumpTimer(proto, UidProto.Job.TOTAL, timer, rawRealtimeUs, which);
7254 dumpTimer(proto, UidProto.Job.BACKGROUND, bgTimer, rawRealtimeUs, which);
7255
7256 proto.end(jToken);
7257 }
7258
7259 // Modem Controller (MODEM_CONTROLLER_DATA)
7260 dumpControllerActivityProto(proto, UidProto.MODEM_CONTROLLER,
7261 u.getModemControllerActivity(), which);
7262
7263 // Network stats (NETWORK_DATA)
7264 final long nToken = proto.start(UidProto.NETWORK);
7265 proto.write(UidProto.Network.MOBILE_BYTES_RX,
7266 u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7267 proto.write(UidProto.Network.MOBILE_BYTES_TX,
7268 u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7269 proto.write(UidProto.Network.WIFI_BYTES_RX,
7270 u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7271 proto.write(UidProto.Network.WIFI_BYTES_TX,
7272 u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7273 proto.write(UidProto.Network.BT_BYTES_RX,
7274 u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7275 proto.write(UidProto.Network.BT_BYTES_TX,
7276 u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
7277 proto.write(UidProto.Network.MOBILE_PACKETS_RX,
7278 u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7279 proto.write(UidProto.Network.MOBILE_PACKETS_TX,
7280 u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7281 proto.write(UidProto.Network.WIFI_PACKETS_RX,
7282 u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7283 proto.write(UidProto.Network.WIFI_PACKETS_TX,
7284 u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7285 proto.write(UidProto.Network.MOBILE_ACTIVE_DURATION_MS,
7286 roundUsToMs(u.getMobileRadioActiveTime(which)));
7287 proto.write(UidProto.Network.MOBILE_ACTIVE_COUNT,
7288 u.getMobileRadioActiveCount(which));
7289 proto.write(UidProto.Network.MOBILE_WAKEUP_COUNT,
7290 u.getMobileRadioApWakeupCount(which));
7291 proto.write(UidProto.Network.WIFI_WAKEUP_COUNT,
7292 u.getWifiRadioApWakeupCount(which));
7293 proto.write(UidProto.Network.MOBILE_BYTES_BG_RX,
7294 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA, which));
7295 proto.write(UidProto.Network.MOBILE_BYTES_BG_TX,
7296 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA, which));
7297 proto.write(UidProto.Network.WIFI_BYTES_BG_RX,
7298 u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which));
7299 proto.write(UidProto.Network.WIFI_BYTES_BG_TX,
7300 u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which));
7301 proto.write(UidProto.Network.MOBILE_PACKETS_BG_RX,
7302 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA, which));
7303 proto.write(UidProto.Network.MOBILE_PACKETS_BG_TX,
7304 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA, which));
7305 proto.write(UidProto.Network.WIFI_PACKETS_BG_RX,
7306 u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA, which));
7307 proto.write(UidProto.Network.WIFI_PACKETS_BG_TX,
7308 u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA, which));
7309 proto.end(nToken);
7310
7311 // Power use item (POWER_USE_ITEM_DATA)
7312 BatterySipper bs = uidToSipper.get(uid);
7313 if (bs != null) {
7314 final long bsToken = proto.start(UidProto.POWER_USE_ITEM);
7315 proto.write(UidProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7316 proto.write(UidProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7317 proto.write(UidProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7318 proto.write(UidProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7319 bs.proportionalSmearMah);
7320 proto.end(bsToken);
7321 }
7322
7323 // Processes (PROCESS_DATA)
7324 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats =
7325 u.getProcessStats();
7326 for (int ipr = processStats.size() - 1; ipr >= 0; --ipr) {
7327 final Uid.Proc ps = processStats.valueAt(ipr);
7328 final long prToken = proto.start(UidProto.PROCESS);
7329
7330 proto.write(UidProto.Process.NAME, processStats.keyAt(ipr));
7331 proto.write(UidProto.Process.USER_DURATION_MS, ps.getUserTime(which));
7332 proto.write(UidProto.Process.SYSTEM_DURATION_MS, ps.getSystemTime(which));
7333 proto.write(UidProto.Process.FOREGROUND_DURATION_MS, ps.getForegroundTime(which));
7334 proto.write(UidProto.Process.START_COUNT, ps.getStarts(which));
7335 proto.write(UidProto.Process.ANR_COUNT, ps.getNumAnrs(which));
7336 proto.write(UidProto.Process.CRASH_COUNT, ps.getNumCrashes(which));
7337
7338 proto.end(prToken);
7339 }
7340
7341 // Sensors (SENSOR_DATA)
7342 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
7343 for (int ise = 0; ise < sensors.size(); ++ise) {
7344 final Uid.Sensor se = sensors.valueAt(ise);
7345 final Timer timer = se.getSensorTime();
7346 if (timer == null) {
7347 continue;
7348 }
7349 final Timer bgTimer = se.getSensorBackgroundTime();
7350 final int sensorNumber = sensors.keyAt(ise);
7351 final long seToken = proto.start(UidProto.SENSORS);
7352
7353 proto.write(UidProto.Sensor.ID, sensorNumber);
7354 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7355 dumpTimer(proto, UidProto.Sensor.APPORTIONED, timer, rawRealtimeUs, which);
7356 dumpTimer(proto, UidProto.Sensor.BACKGROUND, bgTimer, rawRealtimeUs, which);
7357
7358 proto.end(seToken);
7359 }
7360
7361 // State times (STATE_TIME_DATA)
7362 for (int ips = 0; ips < Uid.NUM_PROCESS_STATE; ++ips) {
7363 long durMs = roundUsToMs(u.getProcessStateTime(ips, rawRealtimeUs, which));
7364 if (durMs == 0) {
7365 continue;
7366 }
7367 final long stToken = proto.start(UidProto.STATES);
7368 proto.write(UidProto.StateTime.STATE, ips);
7369 proto.write(UidProto.StateTime.DURATION_MS, durMs);
7370 proto.end(stToken);
7371 }
7372
7373 // Syncs (SYNC_DATA)
7374 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
7375 for (int isy = syncs.size() - 1; isy >= 0; --isy) {
7376 final Timer timer = syncs.valueAt(isy);
7377 final Timer bgTimer = timer.getSubTimer();
7378 final long syToken = proto.start(UidProto.SYNCS);
7379
7380 proto.write(UidProto.Sync.NAME, syncs.keyAt(isy));
7381 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7382 dumpTimer(proto, UidProto.Sync.TOTAL, timer, rawRealtimeUs, which);
7383 dumpTimer(proto, UidProto.Sync.BACKGROUND, bgTimer, rawRealtimeUs, which);
7384
7385 proto.end(syToken);
7386 }
7387
7388 // User activity (USER_ACTIVITY_DATA)
7389 if (u.hasUserActivity()) {
7390 for (int i = 0; i < Uid.NUM_USER_ACTIVITY_TYPES; ++i) {
7391 int val = u.getUserActivityCount(i, which);
7392 if (val != 0) {
7393 final long uaToken = proto.start(UidProto.USER_ACTIVITY);
7394 proto.write(UidProto.UserActivity.NAME, i);
7395 proto.write(UidProto.UserActivity.COUNT, val);
7396 proto.end(uaToken);
7397 }
7398 }
7399 }
7400
7401 // Vibrator (VIBRATOR_DATA)
7402 dumpTimer(proto, UidProto.VIBRATOR, u.getVibratorOnTimer(), rawRealtimeUs, which);
7403
7404 // Video (VIDEO_DATA)
7405 dumpTimer(proto, UidProto.VIDEO, u.getVideoTurnedOnTimer(), rawRealtimeUs, which);
7406
7407 // Wakelocks (WAKELOCK_DATA)
7408 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
7409 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7410 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7411 final long wToken = proto.start(UidProto.WAKELOCKS);
7412 proto.write(UidProto.Wakelock.NAME, wakelocks.keyAt(iw));
7413 dumpTimer(proto, UidProto.Wakelock.FULL, wl.getWakeTime(WAKE_TYPE_FULL),
7414 rawRealtimeUs, which);
7415 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7416 if (pTimer != null) {
7417 dumpTimer(proto, UidProto.Wakelock.PARTIAL, pTimer, rawRealtimeUs, which);
7418 dumpTimer(proto, UidProto.Wakelock.BACKGROUND_PARTIAL, pTimer.getSubTimer(),
7419 rawRealtimeUs, which);
7420 }
7421 dumpTimer(proto, UidProto.Wakelock.WINDOW, wl.getWakeTime(WAKE_TYPE_WINDOW),
7422 rawRealtimeUs, which);
7423 proto.end(wToken);
7424 }
7425
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007426 // Wifi Multicast Wakelock (WIFI_MULTICAST_WAKELOCK_DATA)
7427 dumpTimer(proto, UidProto.WIFI_MULTICAST_WAKELOCK, u.getMulticastWakelockStats(),
7428 rawRealtimeUs, which);
7429
Kweku Adams103351f2017-10-16 14:39:34 -07007430 // Wakeup alarms (WAKEUP_ALARM_DATA)
7431 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7432 final Uid.Pkg ps = packageStats.valueAt(ipkg);
7433 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
7434 for (int iwa = alarms.size() - 1; iwa >= 0; --iwa) {
7435 final long waToken = proto.start(UidProto.WAKEUP_ALARM);
7436 proto.write(UidProto.WakeupAlarm.NAME, alarms.keyAt(iwa));
7437 proto.write(UidProto.WakeupAlarm.COUNT,
7438 alarms.valueAt(iwa).getCountLocked(which));
7439 proto.end(waToken);
7440 }
7441 }
7442
7443 // Wifi Controller (WIFI_CONTROLLER_DATA)
7444 dumpControllerActivityProto(proto, UidProto.WIFI_CONTROLLER,
7445 u.getWifiControllerActivity(), which);
7446
7447 // Wifi data (WIFI_DATA)
7448 final long wToken = proto.start(UidProto.WIFI);
7449 proto.write(UidProto.Wifi.FULL_WIFI_LOCK_DURATION_MS,
7450 roundUsToMs(u.getFullWifiLockTime(rawRealtimeUs, which)));
7451 dumpTimer(proto, UidProto.Wifi.APPORTIONED_SCAN, u.getWifiScanTimer(),
7452 rawRealtimeUs, which);
7453 proto.write(UidProto.Wifi.RUNNING_DURATION_MS,
7454 roundUsToMs(u.getWifiRunningTime(rawRealtimeUs, which)));
7455 dumpTimer(proto, UidProto.Wifi.BACKGROUND_SCAN, u.getWifiScanBackgroundTimer(),
7456 rawRealtimeUs, which);
7457 proto.end(wToken);
7458
7459 proto.end(uTkn);
7460 }
7461 }
7462
7463 private void dumpProtoSystemLocked(ProtoOutputStream proto, BatteryStatsHelper helper) {
Kweku Adams87b19ec2017-10-09 12:40:03 -07007464 final long sToken = proto.start(BatteryStatsProto.SYSTEM);
7465 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7466 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7467 final long rawRealtimeUs = rawRealtimeMs * 1000;
7468 final int which = STATS_SINCE_CHARGED;
7469
7470 // Battery data (BATTERY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007471 final long bToken = proto.start(SystemProto.BATTERY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007472 proto.write(SystemProto.Battery.START_CLOCK_TIME_MS, getStartClockTime());
7473 proto.write(SystemProto.Battery.START_COUNT, getStartCount());
7474 proto.write(SystemProto.Battery.TOTAL_REALTIME_MS,
7475 computeRealtime(rawRealtimeUs, which) / 1000);
7476 proto.write(SystemProto.Battery.TOTAL_UPTIME_MS,
7477 computeUptime(rawUptimeUs, which) / 1000);
7478 proto.write(SystemProto.Battery.BATTERY_REALTIME_MS,
7479 computeBatteryRealtime(rawRealtimeUs, which) / 1000);
7480 proto.write(SystemProto.Battery.BATTERY_UPTIME_MS,
7481 computeBatteryUptime(rawUptimeUs, which) / 1000);
7482 proto.write(SystemProto.Battery.SCREEN_OFF_REALTIME_MS,
7483 computeBatteryScreenOffRealtime(rawRealtimeUs, which) / 1000);
7484 proto.write(SystemProto.Battery.SCREEN_OFF_UPTIME_MS,
7485 computeBatteryScreenOffUptime(rawUptimeUs, which) / 1000);
7486 proto.write(SystemProto.Battery.SCREEN_DOZE_DURATION_MS,
7487 getScreenDozeTime(rawRealtimeUs, which) / 1000);
7488 proto.write(SystemProto.Battery.ESTIMATED_BATTERY_CAPACITY_MAH,
7489 getEstimatedBatteryCapacity());
7490 proto.write(SystemProto.Battery.MIN_LEARNED_BATTERY_CAPACITY_UAH,
7491 getMinLearnedBatteryCapacity());
7492 proto.write(SystemProto.Battery.MAX_LEARNED_BATTERY_CAPACITY_UAH,
7493 getMaxLearnedBatteryCapacity());
Kweku Adams103351f2017-10-16 14:39:34 -07007494 proto.end(bToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007495
7496 // Battery discharge (BATTERY_DISCHARGE_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007497 final long bdToken = proto.start(SystemProto.BATTERY_DISCHARGE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007498 proto.write(SystemProto.BatteryDischarge.LOWER_BOUND_SINCE_CHARGE,
7499 getLowDischargeAmountSinceCharge());
7500 proto.write(SystemProto.BatteryDischarge.UPPER_BOUND_SINCE_CHARGE,
7501 getHighDischargeAmountSinceCharge());
7502 proto.write(SystemProto.BatteryDischarge.SCREEN_ON_SINCE_CHARGE,
7503 getDischargeAmountScreenOnSinceCharge());
7504 proto.write(SystemProto.BatteryDischarge.SCREEN_OFF_SINCE_CHARGE,
7505 getDischargeAmountScreenOffSinceCharge());
7506 proto.write(SystemProto.BatteryDischarge.SCREEN_DOZE_SINCE_CHARGE,
7507 getDischargeAmountScreenDozeSinceCharge());
7508 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH,
7509 getUahDischarge(which) / 1000);
7510 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_OFF,
7511 getUahDischargeScreenOff(which) / 1000);
7512 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_DOZE,
7513 getUahDischargeScreenDoze(which) / 1000);
Mike Ma15313c92017-11-15 17:58:21 -08007514 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_LIGHT_DOZE,
7515 getUahDischargeLightDoze(which) / 1000);
7516 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_DEEP_DOZE,
7517 getUahDischargeDeepDoze(which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007518 proto.end(bdToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007519
7520 // Time remaining
7521 long timeRemainingUs = computeChargeTimeRemaining(rawRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -07007522 // These are part of a oneof, so we should only set one of them.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007523 if (timeRemainingUs >= 0) {
7524 // Charge time remaining (CHARGE_TIME_REMAIN_DATA)
7525 proto.write(SystemProto.CHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7526 } else {
7527 timeRemainingUs = computeBatteryTimeRemaining(rawRealtimeUs);
7528 // Discharge time remaining (DISCHARGE_TIME_REMAIN_DATA)
7529 if (timeRemainingUs >= 0) {
7530 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7531 } else {
7532 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, -1);
7533 }
7534 }
7535
7536 // Charge step (CHARGE_STEP_DATA)
7537 dumpDurationSteps(proto, SystemProto.CHARGE_STEP, getChargeLevelStepTracker());
7538
7539 // Phone data connection (DATA_CONNECTION_TIME_DATA and DATA_CONNECTION_COUNT_DATA)
7540 for (int i = 0; i < NUM_DATA_CONNECTION_TYPES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007541 final long pdcToken = proto.start(SystemProto.DATA_CONNECTION);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007542 proto.write(SystemProto.DataConnection.NAME, i);
7543 dumpTimer(proto, SystemProto.DataConnection.TOTAL, getPhoneDataConnectionTimer(i),
7544 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007545 proto.end(pdcToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007546 }
7547
7548 // Discharge step (DISCHARGE_STEP_DATA)
7549 dumpDurationSteps(proto, SystemProto.DISCHARGE_STEP, getDischargeLevelStepTracker());
7550
7551 // CPU frequencies (GLOBAL_CPU_FREQ_DATA)
7552 final long[] cpuFreqs = getCpuFreqs();
7553 if (cpuFreqs != null) {
7554 for (long i : cpuFreqs) {
7555 proto.write(SystemProto.CPU_FREQUENCY, i);
7556 }
7557 }
7558
7559 // Bluetooth controller (GLOBAL_BLUETOOTH_CONTROLLER_DATA)
7560 dumpControllerActivityProto(proto, SystemProto.GLOBAL_BLUETOOTH_CONTROLLER,
7561 getBluetoothControllerActivity(), which);
7562
7563 // Modem controller (GLOBAL_MODEM_CONTROLLER_DATA)
7564 dumpControllerActivityProto(proto, SystemProto.GLOBAL_MODEM_CONTROLLER,
7565 getModemControllerActivity(), which);
7566
7567 // Global network data (GLOBAL_NETWORK_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007568 final long gnToken = proto.start(SystemProto.GLOBAL_NETWORK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007569 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_RX,
7570 getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7571 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_TX,
7572 getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7573 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_RX,
7574 getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7575 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_TX,
7576 getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7577 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_RX,
7578 getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7579 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_TX,
7580 getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7581 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_RX,
7582 getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7583 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_TX,
7584 getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7585 proto.write(SystemProto.GlobalNetwork.BT_BYTES_RX,
7586 getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7587 proto.write(SystemProto.GlobalNetwork.BT_BYTES_TX,
7588 getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
Kweku Adams103351f2017-10-16 14:39:34 -07007589 proto.end(gnToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007590
7591 // Wifi controller (GLOBAL_WIFI_CONTROLLER_DATA)
7592 dumpControllerActivityProto(proto, SystemProto.GLOBAL_WIFI_CONTROLLER,
7593 getWifiControllerActivity(), which);
7594
7595
7596 // Global wifi (GLOBAL_WIFI_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007597 final long gwToken = proto.start(SystemProto.GLOBAL_WIFI);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007598 proto.write(SystemProto.GlobalWifi.ON_DURATION_MS,
7599 getWifiOnTime(rawRealtimeUs, which) / 1000);
7600 proto.write(SystemProto.GlobalWifi.RUNNING_DURATION_MS,
7601 getGlobalWifiRunningTime(rawRealtimeUs, which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007602 proto.end(gwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007603
7604 // Kernel wakelock (KERNEL_WAKELOCK_DATA)
7605 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
7606 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007607 final long kwToken = proto.start(SystemProto.KERNEL_WAKELOCK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007608 proto.write(SystemProto.KernelWakelock.NAME, ent.getKey());
7609 dumpTimer(proto, SystemProto.KernelWakelock.TOTAL, ent.getValue(),
7610 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007611 proto.end(kwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007612 }
7613
7614 // Misc (MISC_DATA)
7615 // Calculate wakelock times across all uids.
7616 long fullWakeLockTimeTotalUs = 0;
7617 long partialWakeLockTimeTotalUs = 0;
7618
7619 final SparseArray<? extends Uid> uidStats = getUidStats();
7620 for (int iu = 0; iu < uidStats.size(); iu++) {
7621 final Uid u = uidStats.valueAt(iu);
7622
7623 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks =
7624 u.getWakelockStats();
7625 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7626 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7627
7628 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
7629 if (fullWakeTimer != null) {
7630 fullWakeLockTimeTotalUs += fullWakeTimer.getTotalTimeLocked(rawRealtimeUs,
7631 which);
7632 }
7633
7634 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7635 if (partialWakeTimer != null) {
7636 partialWakeLockTimeTotalUs += partialWakeTimer.getTotalTimeLocked(
7637 rawRealtimeUs, which);
7638 }
7639 }
7640 }
Kweku Adams103351f2017-10-16 14:39:34 -07007641 final long mToken = proto.start(SystemProto.MISC);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007642 proto.write(SystemProto.Misc.SCREEN_ON_DURATION_MS,
7643 getScreenOnTime(rawRealtimeUs, which) / 1000);
7644 proto.write(SystemProto.Misc.PHONE_ON_DURATION_MS,
7645 getPhoneOnTime(rawRealtimeUs, which) / 1000);
7646 proto.write(SystemProto.Misc.FULL_WAKELOCK_TOTAL_DURATION_MS,
7647 fullWakeLockTimeTotalUs / 1000);
7648 proto.write(SystemProto.Misc.PARTIAL_WAKELOCK_TOTAL_DURATION_MS,
7649 partialWakeLockTimeTotalUs / 1000);
7650 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_DURATION_MS,
7651 getMobileRadioActiveTime(rawRealtimeUs, which) / 1000);
7652 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_ADJUSTED_TIME_MS,
7653 getMobileRadioActiveAdjustedTime(which) / 1000);
7654 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_COUNT,
7655 getMobileRadioActiveCount(which));
7656 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_UNKNOWN_DURATION_MS,
7657 getMobileRadioActiveUnknownTime(which) / 1000);
7658 proto.write(SystemProto.Misc.INTERACTIVE_DURATION_MS,
7659 getInteractiveTime(rawRealtimeUs, which) / 1000);
7660 proto.write(SystemProto.Misc.BATTERY_SAVER_MODE_ENABLED_DURATION_MS,
7661 getPowerSaveModeEnabledTime(rawRealtimeUs, which) / 1000);
7662 proto.write(SystemProto.Misc.NUM_CONNECTIVITY_CHANGES,
7663 getNumConnectivityChange(which));
7664 proto.write(SystemProto.Misc.DEEP_DOZE_ENABLED_DURATION_MS,
7665 getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7666 proto.write(SystemProto.Misc.DEEP_DOZE_COUNT,
7667 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
7668 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_DURATION_MS,
7669 getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7670 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_COUNT,
7671 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
7672 proto.write(SystemProto.Misc.LONGEST_DEEP_DOZE_DURATION_MS,
7673 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
7674 proto.write(SystemProto.Misc.LIGHT_DOZE_ENABLED_DURATION_MS,
7675 getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7676 proto.write(SystemProto.Misc.LIGHT_DOZE_COUNT,
7677 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
7678 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_DURATION_MS,
7679 getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7680 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_COUNT,
7681 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
7682 proto.write(SystemProto.Misc.LONGEST_LIGHT_DOZE_DURATION_MS,
7683 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
Kweku Adams103351f2017-10-16 14:39:34 -07007684 proto.end(mToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007685
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007686 // Wifi multicast wakelock total stats (WIFI_MULTICAST_WAKELOCK_TOTAL_DATA)
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08007687 final long multicastWakeLockTimeTotalUs =
7688 getWifiMulticastWakelockTime(rawRealtimeUs, which);
7689 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007690 final long wmctToken = proto.start(SystemProto.WIFI_MULTICAST_WAKELOCK_TOTAL);
7691 proto.write(SystemProto.WifiMulticastWakelockTotal.DURATION_MS,
7692 multicastWakeLockTimeTotalUs / 1000);
7693 proto.write(SystemProto.WifiMulticastWakelockTotal.COUNT,
7694 multicastWakeLockCountTotal);
7695 proto.end(wmctToken);
7696
Kweku Adams87b19ec2017-10-09 12:40:03 -07007697 // Power use item (POWER_USE_ITEM_DATA)
7698 final List<BatterySipper> sippers = helper.getUsageList();
7699 if (sippers != null) {
7700 for (int i = 0; i < sippers.size(); ++i) {
7701 final BatterySipper bs = sippers.get(i);
7702 int n = SystemProto.PowerUseItem.UNKNOWN_SIPPER;
7703 int uid = 0;
7704 switch (bs.drainType) {
7705 case IDLE:
7706 n = SystemProto.PowerUseItem.IDLE;
7707 break;
7708 case CELL:
7709 n = SystemProto.PowerUseItem.CELL;
7710 break;
7711 case PHONE:
7712 n = SystemProto.PowerUseItem.PHONE;
7713 break;
7714 case WIFI:
7715 n = SystemProto.PowerUseItem.WIFI;
7716 break;
7717 case BLUETOOTH:
7718 n = SystemProto.PowerUseItem.BLUETOOTH;
7719 break;
7720 case SCREEN:
7721 n = SystemProto.PowerUseItem.SCREEN;
7722 break;
7723 case FLASHLIGHT:
7724 n = SystemProto.PowerUseItem.FLASHLIGHT;
7725 break;
7726 case APP:
Kweku Adams103351f2017-10-16 14:39:34 -07007727 // dumpProtoAppsLocked will handle this.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007728 continue;
7729 case USER:
7730 n = SystemProto.PowerUseItem.USER;
7731 uid = UserHandle.getUid(bs.userId, 0);
7732 break;
7733 case UNACCOUNTED:
7734 n = SystemProto.PowerUseItem.UNACCOUNTED;
7735 break;
7736 case OVERCOUNTED:
7737 n = SystemProto.PowerUseItem.OVERCOUNTED;
7738 break;
7739 case CAMERA:
7740 n = SystemProto.PowerUseItem.CAMERA;
7741 break;
7742 case MEMORY:
7743 n = SystemProto.PowerUseItem.MEMORY;
7744 break;
7745 }
Kweku Adams103351f2017-10-16 14:39:34 -07007746 final long puiToken = proto.start(SystemProto.POWER_USE_ITEM);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007747 proto.write(SystemProto.PowerUseItem.NAME, n);
7748 proto.write(SystemProto.PowerUseItem.UID, uid);
7749 proto.write(SystemProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7750 proto.write(SystemProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7751 proto.write(SystemProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7752 proto.write(SystemProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7753 bs.proportionalSmearMah);
Kweku Adams103351f2017-10-16 14:39:34 -07007754 proto.end(puiToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007755 }
7756 }
7757
7758 // Power use summary (POWER_USE_SUMMARY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007759 final long pusToken = proto.start(SystemProto.POWER_USE_SUMMARY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007760 proto.write(SystemProto.PowerUseSummary.BATTERY_CAPACITY_MAH,
7761 helper.getPowerProfile().getBatteryCapacity());
7762 proto.write(SystemProto.PowerUseSummary.COMPUTED_POWER_MAH, helper.getComputedPower());
7763 proto.write(SystemProto.PowerUseSummary.MIN_DRAINED_POWER_MAH, helper.getMinDrainedPower());
7764 proto.write(SystemProto.PowerUseSummary.MAX_DRAINED_POWER_MAH, helper.getMaxDrainedPower());
Kweku Adams103351f2017-10-16 14:39:34 -07007765 proto.end(pusToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007766
7767 // RPM stats (RESOURCE_POWER_MANAGER_DATA)
7768 final Map<String, ? extends Timer> rpmStats = getRpmStats();
7769 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
7770 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007771 final long rpmToken = proto.start(SystemProto.RESOURCE_POWER_MANAGER);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007772 proto.write(SystemProto.ResourcePowerManager.NAME, ent.getKey());
7773 dumpTimer(proto, SystemProto.ResourcePowerManager.TOTAL,
7774 ent.getValue(), rawRealtimeUs, which);
7775 dumpTimer(proto, SystemProto.ResourcePowerManager.SCREEN_OFF,
7776 screenOffRpmStats.get(ent.getKey()), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007777 proto.end(rpmToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007778 }
7779
7780 // Screen brightness (SCREEN_BRIGHTNESS_DATA)
7781 for (int i = 0; i < NUM_SCREEN_BRIGHTNESS_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007782 final long sbToken = proto.start(SystemProto.SCREEN_BRIGHTNESS);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007783 proto.write(SystemProto.ScreenBrightness.NAME, i);
7784 dumpTimer(proto, SystemProto.ScreenBrightness.TOTAL, getScreenBrightnessTimer(i),
7785 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007786 proto.end(sbToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007787 }
7788
7789 // Signal scanning time (SIGNAL_SCANNING_TIME_DATA)
7790 dumpTimer(proto, SystemProto.SIGNAL_SCANNING, getPhoneSignalScanningTimer(), rawRealtimeUs,
7791 which);
7792
7793 // Phone signal strength (SIGNAL_STRENGTH_TIME_DATA and SIGNAL_STRENGTH_COUNT_DATA)
7794 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007795 final long pssToken = proto.start(SystemProto.PHONE_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007796 proto.write(SystemProto.PhoneSignalStrength.NAME, i);
7797 dumpTimer(proto, SystemProto.PhoneSignalStrength.TOTAL, getPhoneSignalStrengthTimer(i),
7798 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007799 proto.end(pssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007800 }
7801
7802 // Wakeup reasons (WAKEUP_REASON_DATA)
7803 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
7804 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007805 final long wrToken = proto.start(SystemProto.WAKEUP_REASON);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007806 proto.write(SystemProto.WakeupReason.NAME, ent.getKey());
7807 dumpTimer(proto, SystemProto.WakeupReason.TOTAL, ent.getValue(), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007808 proto.end(wrToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007809 }
7810
7811 // Wifi signal strength (WIFI_SIGNAL_STRENGTH_TIME_DATA and WIFI_SIGNAL_STRENGTH_COUNT_DATA)
7812 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007813 final long wssToken = proto.start(SystemProto.WIFI_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007814 proto.write(SystemProto.WifiSignalStrength.NAME, i);
7815 dumpTimer(proto, SystemProto.WifiSignalStrength.TOTAL, getWifiSignalStrengthTimer(i),
7816 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007817 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007818 }
7819
7820 // Wifi state (WIFI_STATE_TIME_DATA and WIFI_STATE_COUNT_DATA)
7821 for (int i = 0; i < NUM_WIFI_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007822 final long wsToken = proto.start(SystemProto.WIFI_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007823 proto.write(SystemProto.WifiState.NAME, i);
7824 dumpTimer(proto, SystemProto.WifiState.TOTAL, getWifiStateTimer(i),
7825 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007826 proto.end(wsToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007827 }
7828
7829 // Wifi supplicant state (WIFI_SUPPL_STATE_TIME_DATA and WIFI_SUPPL_STATE_COUNT_DATA)
7830 for (int i = 0; i < NUM_WIFI_SUPPL_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007831 final long wssToken = proto.start(SystemProto.WIFI_SUPPLICANT_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007832 proto.write(SystemProto.WifiSupplicantState.NAME, i);
7833 dumpTimer(proto, SystemProto.WifiSupplicantState.TOTAL, getWifiSupplStateTimer(i),
7834 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007835 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007836 }
7837
7838 proto.end(sToken);
7839 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007840}