blob: 835525c97179ea2386fc418080d37def5ade1b23 [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.
Kweku Adamsb78430e2018-02-20 18:06:09 -0800243 * New in version 31:
244 * - New cellular network types.
245 * - Deferred job metrics.
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700246 */
Kweku Adamsb78430e2018-02-20 18:06:09 -0800247 static final int CHECKIN_VERSION = 31;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700248
249 /**
250 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700252 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700253
Evan Millar22ac0432009-03-31 11:33:18 -0700254 private static final long BYTES_PER_KB = 1024;
255 private static final long BYTES_PER_MB = 1048576; // 1024^2
256 private static final long BYTES_PER_GB = 1073741824; //1024^3
Bookatz506a8182017-05-01 14:18:42 -0700257
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700258 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800259 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700260 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700262 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700263 private static final String CPU_DATA = "cpu";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700264 private static final String GLOBAL_CPU_FREQ_DATA = "gcf";
265 private static final String CPU_TIMES_AT_FREQ_DATA = "ctf";
Bookatz50df7112017-08-04 14:53:26 -0700266 // rpm line is:
267 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "rpm", state/voter name, total time, total count,
268 // screen-off time, screen-off count
269 private static final String RESOURCE_POWER_MANAGER_DATA = "rpm";
Evan Millare84de8d2009-04-02 22:16:12 -0700270 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800271 private static final String VIBRATOR_DATA = "vib";
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700272 private static final String FOREGROUND_ACTIVITY_DATA = "fg";
273 // fgs line is:
274 // BATTERY_STATS_CHECKIN_VERSION, uid, category, "fgs",
275 // foreground service time, count
276 private static final String FOREGROUND_SERVICE_DATA = "fgs";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700277 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700278 // wl line is:
279 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
Bookatz5b5ec322017-05-26 09:40:38 -0700280 // full totalTime, 'f', count, current duration, max duration, total duration,
281 // partial totalTime, 'p', count, current duration, max duration, total duration,
282 // bg partial totalTime, 'bp', count, current duration, max duration, total duration,
283 // window totalTime, 'w', count, current duration, max duration, total duration
Bookatz506a8182017-05-01 14:18:42 -0700284 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700285 private static final String WAKELOCK_DATA = "wl";
Bookatzc8c44962017-05-11 12:12:54 -0700286 // awl line is:
287 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "awl",
288 // cumulative partial wakelock duration, cumulative background partial wakelock duration
289 private static final String AGGREGATED_WAKELOCK_DATA = "awl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700290 private static final String SYNC_DATA = "sy";
291 private static final String JOB_DATA = "jb";
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700292 private static final String JOB_COMPLETION_DATA = "jbc";
Evan Millarc64edde2009-04-18 12:26:32 -0700293 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700294 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700295 private static final String NETWORK_DATA = "nt";
296 private static final String USER_ACTIVITY_DATA = "ua";
297 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800298 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700299 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700300 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700301 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800302 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
303 private static final String WIFI_CONTROLLER_DATA = "wfcd";
304 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
305 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700306 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700307 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800308 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800309 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
310 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800311 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700312 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700313 private static final String SCREEN_BRIGHTNESS_DATA = "br";
314 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700315 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700316 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
317 private static final String DATA_CONNECTION_TIME_DATA = "dct";
318 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800319 private static final String WIFI_STATE_TIME_DATA = "wst";
320 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700321 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
322 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
323 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
324 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800325 private static final String POWER_USE_SUMMARY_DATA = "pws";
326 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700327 private static final String DISCHARGE_STEP_DATA = "dsd";
328 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700329 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
330 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700331 private static final String FLASHLIGHT_DATA = "fla";
332 private static final String CAMERA_DATA = "cam";
333 private static final String VIDEO_DATA = "vid";
334 private static final String AUDIO_DATA = "aud";
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700335 private static final String WIFI_MULTICAST_TOTAL_DATA = "wmct";
336 private static final String WIFI_MULTICAST_DATA = "wmc";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337
Adam Lesinski010bf372016-04-11 12:18:18 -0700338 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
339
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700340 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 private final Formatter mFormatter = new Formatter(mFormatBuilder);
342
Siddharth Rayb50a6842017-12-14 15:15:28 -0800343 private static final String CELLULAR_CONTROLLER_NAME = "Cellular";
344 private static final String WIFI_CONTROLLER_NAME = "WiFi";
345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700347 * Indicates times spent by the uid at each cpu frequency in all process states.
348 *
349 * Other types might include times spent in foreground, background etc.
350 */
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800351 @VisibleForTesting
352 public static final String UID_TIMES_TYPE_ALL = "A";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700353
354 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700355 * State for keeping track of counting information.
356 */
357 public static abstract class Counter {
358
359 /**
360 * Returns the count associated with this Counter for the
361 * selected type of statistics.
362 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700363 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700364 */
Evan Millarc64edde2009-04-18 12:26:32 -0700365 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700366
367 /**
368 * Temporary for debugging.
369 */
370 public abstract void logState(Printer pw, String prefix);
371 }
372
373 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700374 * State for keeping track of long counting information.
375 */
376 public static abstract class LongCounter {
377
378 /**
379 * Returns the count associated with this Counter for the
380 * selected type of statistics.
381 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700382 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700383 */
384 public abstract long getCountLocked(int which);
385
386 /**
387 * Temporary for debugging.
388 */
389 public abstract void logState(Printer pw, String prefix);
390 }
391
392 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700393 * State for keeping track of array of long counting information.
394 */
395 public static abstract class LongCounterArray {
396 /**
397 * Returns the counts associated with this Counter for the
398 * selected type of statistics.
399 *
400 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
401 */
402 public abstract long[] getCountsLocked(int which);
403
404 /**
405 * Temporary for debugging.
406 */
407 public abstract void logState(Printer pw, String prefix);
408 }
409
410 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800411 * Container class that aggregates counters for transmit, receive, and idle state of a
412 * radio controller.
413 */
414 public static abstract class ControllerActivityCounter {
415 /**
416 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
417 * idle state.
418 */
419 public abstract LongCounter getIdleTimeCounter();
420
421 /**
422 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
Siddharth Rayb50a6842017-12-14 15:15:28 -0800423 * scan state.
424 */
425 public abstract LongCounter getScanTimeCounter();
426
427
428 /**
429 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800430 * receive state.
431 */
432 public abstract LongCounter getRxTimeCounter();
433
434 /**
435 * An array of {@link LongCounter}, representing various transmit levels, where each level
436 * may draw a different amount of power. The levels themselves are controller-specific.
437 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
438 * various transmit level states.
439 */
440 public abstract LongCounter[] getTxTimeCounters();
441
442 /**
443 * @return a non-null {@link LongCounter} representing the power consumed by the controller
444 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
445 * yield a value of 0 if the device doesn't support power calculations.
446 */
447 public abstract LongCounter getPowerCounter();
448 }
449
450 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 * State for keeping track of timing information.
452 */
453 public static abstract class Timer {
454
455 /**
456 * Returns the count associated with this Timer for the
457 * selected type of statistics.
458 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700459 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 */
Evan Millarc64edde2009-04-18 12:26:32 -0700461 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462
463 /**
464 * Returns the total time in microseconds associated with this Timer for the
465 * selected type of statistics.
466 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800467 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700468 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 * @return a time in microseconds
470 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800471 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700474 * Returns the total time in microseconds associated with this Timer since the
475 * 'mark' was last set.
476 *
477 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
478 * @return a time in microseconds
479 */
480 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
481
482 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700483 * Returns the max duration if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700484 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700485 */
486 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
487 return -1;
488 }
489
490 /**
491 * Returns the current time the timer has been active, if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700492 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700493 */
494 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
495 return -1;
496 }
497
498 /**
Kweku Adams103351f2017-10-16 14:39:34 -0700499 * Returns the total time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800500 *
501 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
502 * been on since reset.
503 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
504 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
505 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
506 * the actual total time.
Kweku Adams103351f2017-10-16 14:39:34 -0700507 * Not all Timer subclasses track the max, total, and current durations.
Bookatz867c0d72017-03-07 18:23:42 -0800508 */
509 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
510 return -1;
511 }
512
513 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700514 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
515 * used, for example, for tracking background usage. Secondary timers are never pooled.
516 *
517 * Not all Timer subclasses have a secondary timer; those that don't return null.
518 */
519 public Timer getSubTimer() {
520 return null;
521 }
522
523 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700524 * Returns whether the timer is currently running. Some types of timers
525 * (e.g. BatchTimers) don't know whether the event is currently active,
526 * and report false.
527 */
528 public boolean isRunningLocked() {
529 return false;
530 }
531
532 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 * Temporary for debugging.
534 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700535 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 }
537
538 /**
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800539 * Maps the ActivityManager procstate into corresponding BatteryStats procstate.
540 */
541 public static int mapToInternalProcessState(int procState) {
542 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
543 return ActivityManager.PROCESS_STATE_NONEXISTENT;
544 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
545 return Uid.PROCESS_STATE_TOP;
Dianne Hackborn10fc4fd2017-12-19 17:23:13 -0800546 } else if (procState == ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
547 // State when app has put itself in the foreground.
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800548 return Uid.PROCESS_STATE_FOREGROUND_SERVICE;
549 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
550 // Persistent and other foreground states go here.
551 return Uid.PROCESS_STATE_FOREGROUND;
552 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
553 return Uid.PROCESS_STATE_BACKGROUND;
554 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
555 return Uid.PROCESS_STATE_TOP_SLEEPING;
556 } else if (procState <= ActivityManager.PROCESS_STATE_HEAVY_WEIGHT) {
557 return Uid.PROCESS_STATE_HEAVY_WEIGHT;
558 } else {
559 return Uid.PROCESS_STATE_CACHED;
560 }
561 }
562
563 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 * The statistics associated with a particular uid.
565 */
566 public static abstract class Uid {
567
568 /**
569 * Returns a mapping containing wakelock statistics.
570 *
571 * @return a Map from Strings to Uid.Wakelock objects.
572 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700573 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574
575 /**
Ahmed ElArabawyddd09692017-10-30 17:58:29 -0700576 * Returns the WiFi Multicast Wakelock statistics.
577 *
578 * @return a Timer Object for the per uid Multicast statistics.
579 */
580 public abstract Timer getMulticastWakelockStats();
581
582 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700583 * Returns a mapping containing sync statistics.
584 *
585 * @return a Map from Strings to Timer objects.
586 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700587 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700588
589 /**
590 * Returns a mapping containing scheduled job statistics.
591 *
592 * @return a Map from Strings to Timer objects.
593 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700594 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700595
596 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700597 * Returns statistics about how jobs have completed.
598 *
599 * @return A Map of String job names to completion type -> count mapping.
600 */
601 public abstract ArrayMap<String, SparseIntArray> getJobCompletionStats();
602
603 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 * The statistics associated with a particular wake lock.
605 */
606 public static abstract class Wakelock {
607 public abstract Timer getWakeTime(int type);
608 }
609
610 /**
Bookatzc8c44962017-05-11 12:12:54 -0700611 * The cumulative time the uid spent holding any partial wakelocks. This will generally
612 * differ from summing over the Wakelocks in getWakelockStats since the latter may have
613 * wakelocks that overlap in time (and therefore over-counts).
614 */
615 public abstract Timer getAggregatedPartialWakelockTimer();
616
617 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 * Returns a mapping containing sensor statistics.
619 *
620 * @return a Map from Integer sensor ids to Uid.Sensor objects.
621 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700622 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623
624 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700625 * Returns a mapping containing active process data.
626 */
627 public abstract SparseArray<? extends Pid> getPidStats();
Bookatzc8c44962017-05-11 12:12:54 -0700628
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700629 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 * Returns a mapping containing process statistics.
631 *
632 * @return a Map from Strings to Uid.Proc objects.
633 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700634 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635
636 /**
637 * Returns a mapping containing package statistics.
638 *
639 * @return a Map from Strings to Uid.Pkg objects.
640 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700641 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700642
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800643 public abstract ControllerActivityCounter getWifiControllerActivity();
644 public abstract ControllerActivityCounter getBluetoothControllerActivity();
645 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800646
647 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 * {@hide}
649 */
650 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700651
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800652 public abstract void noteWifiRunningLocked(long elapsedRealtime);
653 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
654 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
655 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
656 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
657 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
658 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
659 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
660 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
661 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800662 public abstract void noteActivityResumedLocked(long elapsedRealtime);
663 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800664 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
665 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
666 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700667 public abstract int getWifiScanCount(int which);
Kweku Adams103351f2017-10-16 14:39:34 -0700668 /**
669 * Returns the timer keeping track of wifi scans.
670 */
671 public abstract Timer getWifiScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800672 public abstract int getWifiScanBackgroundCount(int which);
673 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
674 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -0700675 /**
676 * Returns the timer keeping track of background wifi scans.
677 */
678 public abstract Timer getWifiScanBackgroundTimer();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800679 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700680 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800681 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700682 public abstract Timer getAudioTurnedOnTimer();
683 public abstract Timer getVideoTurnedOnTimer();
684 public abstract Timer getFlashlightTurnedOnTimer();
685 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700686 public abstract Timer getForegroundActivityTimer();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700687
688 /**
689 * Returns the timer keeping track of Foreground Service time
690 */
691 public abstract Timer getForegroundServiceTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800692 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800693 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatzb1f04f32017-05-19 13:57:32 -0700694 public abstract Timer getBluetoothUnoptimizedScanTimer();
695 public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700696 public abstract Counter getBluetoothScanResultCounter();
Bookatzb1f04f32017-05-19 13:57:32 -0700697 public abstract Counter getBluetoothScanResultBgCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700698
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700699 public abstract long[] getCpuFreqTimes(int which);
700 public abstract long[] getScreenOffCpuFreqTimes(int which);
Mike Ma3d422c32017-10-25 11:08:57 -0700701 /**
702 * Returns cpu active time of an uid.
703 */
704 public abstract long getCpuActiveTime();
705 /**
706 * Returns cpu times of an uid on each cluster
707 */
708 public abstract long[] getCpuClusterTimes();
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700709
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800710 /**
711 * Returns cpu times of an uid at a particular process state.
712 */
713 public abstract long[] getCpuFreqTimes(int which, int procState);
714 /**
715 * Returns cpu times of an uid while the screen if off at a particular process state.
716 */
717 public abstract long[] getScreenOffCpuFreqTimes(int which, int procState);
718
Dianne Hackborna0200e32016-03-30 18:01:41 -0700719 // Note: the following times are disjoint. They can be added together to find the
720 // total time a uid has had any processes running at all.
721
722 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800723 * Time this uid has any processes in the top state.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700724 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800725 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700726 /**
zhouwenjie46712bc2018-01-11 15:21:27 -0800727 * Time this uid has any process with a started foreground service, but
Dianne Hackborna0200e32016-03-30 18:01:41 -0700728 * none in the "top" state.
729 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800730 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700731 /**
Dianne Hackborna0200e32016-03-30 18:01:41 -0700732 * Time this uid has any process in an active foreground state, but none in the
zhouwenjie46712bc2018-01-11 15:21:27 -0800733 * "foreground service" or better state. Persistent and other foreground states go here.
Dianne Hackborna0200e32016-03-30 18:01:41 -0700734 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800735 public static final int PROCESS_STATE_FOREGROUND = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700736 /**
737 * Time this uid has any process in an active background state, but none in the
738 * "foreground" or better state.
739 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800740 public static final int PROCESS_STATE_BACKGROUND = 3;
741 /**
742 * Time this uid has any process that is top while the device is sleeping, but not
743 * active for any other reason. We kind-of consider it a kind of cached process
744 * for execution restrictions.
745 */
746 public static final int PROCESS_STATE_TOP_SLEEPING = 4;
747 /**
748 * Time this uid has any process that is in the background but it has an activity
749 * marked as "can't save state". This is essentially a cached process, though the
750 * system will try much harder than normal to avoid killing it.
751 */
752 public static final int PROCESS_STATE_HEAVY_WEIGHT = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700753 /**
754 * Time this uid has any processes that are sitting around cached, not in one of the
755 * other active states.
756 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800757 public static final int PROCESS_STATE_CACHED = 6;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700758 /**
759 * Total number of process states we track.
760 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800761 public static final int NUM_PROCESS_STATE = 7;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700762
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800763 // Used in dump
Dianne Hackborn61659e52014-07-09 16:13:01 -0700764 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800765 "Top", "Fg Service", "Foreground", "Background", "Top Sleeping", "Heavy Weight",
766 "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700767 };
768
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800769 // Used in checkin dump
770 @VisibleForTesting
771 public static final String[] UID_PROCESS_TYPES = {
772 "T", // TOP
773 "FS", // FOREGROUND_SERVICE
774 "F", // FOREGROUND
775 "B", // BACKGROUND
776 "TS", // TOP_SLEEPING
777 "HW", // HEAVY_WEIGHT
778 "C" // CACHED
779 };
780
781 /**
782 * When the process exits one of these states, we need to make sure cpu time in this state
783 * is not attributed to any non-critical process states.
784 */
785 public static final int[] CRITICAL_PROC_STATES = {
786 PROCESS_STATE_TOP, PROCESS_STATE_FOREGROUND_SERVICE, PROCESS_STATE_FOREGROUND
787 };
788
Dianne Hackborn61659e52014-07-09 16:13:01 -0700789 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800790 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700791
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800792 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793
Robert Greenwalta029ea12013-09-25 16:38:12 -0700794 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
795
Dianne Hackborn617f8772009-03-31 15:04:46 -0700796 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700797 * Note that these must match the constants in android.os.PowerManager.
798 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
799 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700800 */
801 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700802 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700803 };
Bookatzc8c44962017-05-11 12:12:54 -0700804
Phil Weaverda80d672016-03-15 16:25:46 -0700805 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700806
Dianne Hackborn617f8772009-03-31 15:04:46 -0700807 public abstract void noteUserActivityLocked(int type);
808 public abstract boolean hasUserActivity();
809 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700810
811 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800812 public abstract long getNetworkActivityBytes(int type, int which);
813 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800814 public abstract long getMobileRadioActiveTime(int which);
815 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700816
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700817 /**
818 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
819 */
820 public abstract long getUserCpuTimeUs(int which);
821
822 /**
823 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
824 */
825 public abstract long getSystemCpuTimeUs(int which);
826
827 /**
Sudheer Shanka71f34b32017-07-21 00:14:24 -0700828 * Returns the approximate cpu time (in microseconds) spent at a certain CPU speed for a
Adam Lesinski6832f392015-09-05 18:05:40 -0700829 * given CPU cluster.
830 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700831 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700832 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700833 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
834 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700835 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700836 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700837
Adam Lesinski5f056f62016-07-14 16:56:08 -0700838 /**
839 * Returns the number of times this UID woke up the Application Processor to
840 * process a mobile radio packet.
841 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
842 */
843 public abstract long getMobileRadioApWakeupCount(int which);
844
845 /**
846 * Returns the number of times this UID woke up the Application Processor to
847 * process a WiFi packet.
848 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
849 */
850 public abstract long getWifiRadioApWakeupCount(int which);
851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800853 /*
854 * FIXME: it's not correct to use this magic value because it
855 * could clash with a sensor handle (which are defined by
856 * the sensor HAL, and therefore out of our control
857 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 // Magic sensor number for the GPS.
859 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800864
Bookatz867c0d72017-03-07 18:23:42 -0800865 /** Returns a Timer for sensor usage when app is in the background. */
866 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 }
868
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700869 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800870 public int mWakeNesting;
871 public long mWakeSumMs;
872 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700873 }
874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 /**
876 * The statistics associated with a particular process.
877 */
878 public static abstract class Proc {
879
Dianne Hackborn287952c2010-09-22 22:34:31 -0700880 public static class ExcessivePower {
881 public static final int TYPE_WAKE = 1;
882 public static final int TYPE_CPU = 2;
883
884 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700885 public long overTime;
886 public long usedTime;
887 }
888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800890 * Returns true if this process is still active in the battery stats.
891 */
892 public abstract boolean isActive();
893
894 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700895 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700897 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 */
899 public abstract long getUserTime(int which);
900
901 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700902 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700904 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 */
906 public abstract long getSystemTime(int which);
907
908 /**
909 * Returns the number of times the process has been started.
910 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700911 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 */
913 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700914
915 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800916 * Returns the number of times the process has crashed.
917 *
918 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
919 */
920 public abstract int getNumCrashes(int which);
921
922 /**
923 * Returns the number of times the process has ANRed.
924 *
925 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
926 */
927 public abstract int getNumAnrs(int which);
928
929 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700930 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700931 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700932 * @return foreground cpu time in microseconds
933 */
934 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700935
Dianne Hackborn287952c2010-09-22 22:34:31 -0700936 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700937
Dianne Hackborn287952c2010-09-22 22:34:31 -0700938 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 }
940
941 /**
942 * The statistics associated with a particular package.
943 */
944 public static abstract class Pkg {
945
946 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700947 * Returns information about all wakeup alarms that have been triggered for this
948 * package. The mapping keys are tag names for the alarms, the counter contains
949 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700951 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952
953 /**
954 * Returns a mapping containing service statistics.
955 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700956 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957
958 /**
959 * The statistics associated with a particular service.
960 */
Joe Onoratoabded112016-02-08 16:49:39 -0800961 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962
963 /**
964 * Returns the amount of time spent started.
965 *
966 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700967 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 * @return
969 */
970 public abstract long getStartTime(long batteryUptime, int which);
971
972 /**
973 * Returns the total number of times startService() has been called.
974 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700975 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 */
977 public abstract int getStarts(int which);
978
979 /**
980 * Returns the total number times the service has been launched.
981 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700982 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 */
984 public abstract int getLaunches(int which);
985 }
986 }
987 }
988
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800989 public static final class LevelStepTracker {
990 public long mLastStepTime = -1;
991 public int mNumStepDurations;
992 public final long[] mStepDurations;
993
994 public LevelStepTracker(int maxLevelSteps) {
995 mStepDurations = new long[maxLevelSteps];
996 }
997
998 public LevelStepTracker(int numSteps, long[] steps) {
999 mNumStepDurations = numSteps;
1000 mStepDurations = new long[numSteps];
1001 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
1002 }
1003
1004 public long getDurationAt(int index) {
1005 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
1006 }
1007
1008 public int getLevelAt(int index) {
1009 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
1010 >> STEP_LEVEL_LEVEL_SHIFT);
1011 }
1012
1013 public int getInitModeAt(int index) {
1014 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
1015 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1016 }
1017
1018 public int getModModeAt(int index) {
1019 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
1020 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1021 }
1022
1023 private void appendHex(long val, int topOffset, StringBuilder out) {
1024 boolean hasData = false;
1025 while (topOffset >= 0) {
1026 int digit = (int)( (val>>topOffset) & 0xf );
1027 topOffset -= 4;
1028 if (!hasData && digit == 0) {
1029 continue;
1030 }
1031 hasData = true;
1032 if (digit >= 0 && digit <= 9) {
1033 out.append((char)('0' + digit));
1034 } else {
1035 out.append((char)('a' + digit - 10));
1036 }
1037 }
1038 }
1039
1040 public void encodeEntryAt(int index, StringBuilder out) {
1041 long item = mStepDurations[index];
1042 long duration = item & STEP_LEVEL_TIME_MASK;
1043 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
1044 >> STEP_LEVEL_LEVEL_SHIFT);
1045 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
1046 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
1047 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
1048 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
1049 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1050 case Display.STATE_OFF: out.append('f'); break;
1051 case Display.STATE_ON: out.append('o'); break;
1052 case Display.STATE_DOZE: out.append('d'); break;
1053 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
1054 }
1055 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1056 out.append('p');
1057 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001058 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1059 out.append('i');
1060 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001061 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
1062 case Display.STATE_OFF: out.append('F'); break;
1063 case Display.STATE_ON: out.append('O'); break;
1064 case Display.STATE_DOZE: out.append('D'); break;
1065 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
1066 }
1067 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
1068 out.append('P');
1069 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001070 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
1071 out.append('I');
1072 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001073 out.append('-');
1074 appendHex(level, 4, out);
1075 out.append('-');
1076 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
1077 }
1078
1079 public void decodeEntryAt(int index, String value) {
1080 final int N = value.length();
1081 int i = 0;
1082 char c;
1083 long out = 0;
1084 while (i < N && (c=value.charAt(i)) != '-') {
1085 i++;
1086 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001087 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001088 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001089 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001090 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001091 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001092 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001093 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1094 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1095 break;
1096 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1097 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1098 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001099 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1100 << STEP_LEVEL_INITIAL_MODE_SHIFT);
1101 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001102 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1103 break;
1104 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1105 break;
1106 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
1107 break;
1108 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
1109 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1110 break;
1111 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1112 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001113 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001114 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1115 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1116 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001117 }
1118 }
1119 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001120 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001121 while (i < N && (c=value.charAt(i)) != '-') {
1122 i++;
1123 level <<= 4;
1124 if (c >= '0' && c <= '9') {
1125 level += c - '0';
1126 } else if (c >= 'a' && c <= 'f') {
1127 level += c - 'a' + 10;
1128 } else if (c >= 'A' && c <= 'F') {
1129 level += c - 'A' + 10;
1130 }
1131 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001132 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001133 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
1134 long duration = 0;
1135 while (i < N && (c=value.charAt(i)) != '-') {
1136 i++;
1137 duration <<= 4;
1138 if (c >= '0' && c <= '9') {
1139 duration += c - '0';
1140 } else if (c >= 'a' && c <= 'f') {
1141 duration += c - 'a' + 10;
1142 } else if (c >= 'A' && c <= 'F') {
1143 duration += c - 'A' + 10;
1144 }
1145 }
1146 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
1147 }
1148
1149 public void init() {
1150 mLastStepTime = -1;
1151 mNumStepDurations = 0;
1152 }
1153
1154 public void clearTime() {
1155 mLastStepTime = -1;
1156 }
1157
1158 public long computeTimePerLevel() {
1159 final long[] steps = mStepDurations;
1160 final int numSteps = mNumStepDurations;
1161
1162 // For now we'll do a simple average across all steps.
1163 if (numSteps <= 0) {
1164 return -1;
1165 }
1166 long total = 0;
1167 for (int i=0; i<numSteps; i++) {
1168 total += steps[i] & STEP_LEVEL_TIME_MASK;
1169 }
1170 return total / numSteps;
1171 /*
1172 long[] buckets = new long[numSteps];
1173 int numBuckets = 0;
1174 int numToAverage = 4;
1175 int i = 0;
1176 while (i < numSteps) {
1177 long totalTime = 0;
1178 int num = 0;
1179 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1180 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1181 num++;
1182 }
1183 buckets[numBuckets] = totalTime / num;
1184 numBuckets++;
1185 numToAverage *= 2;
1186 i += num;
1187 }
1188 if (numBuckets < 1) {
1189 return -1;
1190 }
1191 long averageTime = buckets[numBuckets-1];
1192 for (i=numBuckets-2; i>=0; i--) {
1193 averageTime = (averageTime + buckets[i]) / 2;
1194 }
1195 return averageTime;
1196 */
1197 }
1198
1199 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1200 int[] outNumOfInterest) {
1201 final long[] steps = mStepDurations;
1202 final int count = mNumStepDurations;
1203 if (count <= 0) {
1204 return -1;
1205 }
1206 long total = 0;
1207 int numOfInterest = 0;
1208 for (int i=0; i<count; i++) {
1209 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1210 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1211 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1212 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1213 // If the modes of interest didn't change during this step period...
1214 if ((modMode&modesOfInterest) == 0) {
1215 // And the mode values during this period match those we are measuring...
1216 if ((initMode&modesOfInterest) == modeValues) {
1217 // Then this can be used to estimate the total time!
1218 numOfInterest++;
1219 total += steps[i] & STEP_LEVEL_TIME_MASK;
1220 }
1221 }
1222 }
1223 if (numOfInterest <= 0) {
1224 return -1;
1225 }
1226
1227 if (outNumOfInterest != null) {
1228 outNumOfInterest[0] = numOfInterest;
1229 }
1230
1231 // The estimated time is the average time we spend in each level, multipled
1232 // by 100 -- the total number of battery levels
1233 return (total / numOfInterest) * 100;
1234 }
1235
1236 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1237 int stepCount = mNumStepDurations;
1238 final long lastStepTime = mLastStepTime;
1239 if (lastStepTime >= 0 && numStepLevels > 0) {
1240 final long[] steps = mStepDurations;
1241 long duration = elapsedRealtime - lastStepTime;
1242 for (int i=0; i<numStepLevels; i++) {
1243 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1244 long thisDuration = duration / (numStepLevels-i);
1245 duration -= thisDuration;
1246 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1247 thisDuration = STEP_LEVEL_TIME_MASK;
1248 }
1249 steps[0] = thisDuration | modeBits;
1250 }
1251 stepCount += numStepLevels;
1252 if (stepCount > steps.length) {
1253 stepCount = steps.length;
1254 }
1255 }
1256 mNumStepDurations = stepCount;
1257 mLastStepTime = elapsedRealtime;
1258 }
1259
1260 public void readFromParcel(Parcel in) {
1261 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001262 if (N > mStepDurations.length) {
1263 throw new ParcelFormatException("more step durations than available: " + N);
1264 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001265 mNumStepDurations = N;
1266 for (int i=0; i<N; i++) {
1267 mStepDurations[i] = in.readLong();
1268 }
1269 }
1270
1271 public void writeToParcel(Parcel out) {
1272 final int N = mNumStepDurations;
1273 out.writeInt(N);
1274 for (int i=0; i<N; i++) {
1275 out.writeLong(mStepDurations[i]);
1276 }
1277 }
1278 }
1279
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001280 public static final class PackageChange {
1281 public String mPackageName;
1282 public boolean mUpdate;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001283 public long mVersionCode;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001284 }
1285
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001286 public static final class DailyItem {
1287 public long mStartTime;
1288 public long mEndTime;
1289 public LevelStepTracker mDischargeSteps;
1290 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001291 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001292 }
1293
1294 public abstract DailyItem getDailyItemLocked(int daysAgo);
1295
1296 public abstract long getCurrentDailyStartTime();
1297
1298 public abstract long getNextMinDailyDeadline();
1299
1300 public abstract long getNextMaxDailyDeadline();
1301
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001302 public abstract long[] getCpuFreqs();
1303
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001304 public final static class HistoryTag {
1305 public String string;
1306 public int uid;
1307
1308 public int poolIdx;
1309
1310 public void setTo(HistoryTag o) {
1311 string = o.string;
1312 uid = o.uid;
1313 poolIdx = o.poolIdx;
1314 }
1315
1316 public void setTo(String _string, int _uid) {
1317 string = _string;
1318 uid = _uid;
1319 poolIdx = -1;
1320 }
1321
1322 public void writeToParcel(Parcel dest, int flags) {
1323 dest.writeString(string);
1324 dest.writeInt(uid);
1325 }
1326
1327 public void readFromParcel(Parcel src) {
1328 string = src.readString();
1329 uid = src.readInt();
1330 poolIdx = -1;
1331 }
1332
1333 @Override
1334 public boolean equals(Object o) {
1335 if (this == o) return true;
1336 if (o == null || getClass() != o.getClass()) return false;
1337
1338 HistoryTag that = (HistoryTag) o;
1339
1340 if (uid != that.uid) return false;
1341 if (!string.equals(that.string)) return false;
1342
1343 return true;
1344 }
1345
1346 @Override
1347 public int hashCode() {
1348 int result = string.hashCode();
1349 result = 31 * result + uid;
1350 return result;
1351 }
1352 }
1353
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001354 /**
1355 * Optional detailed information that can go into a history step. This is typically
1356 * generated each time the battery level changes.
1357 */
1358 public final static class HistoryStepDetails {
1359 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1360 public int userTime;
1361 public int systemTime;
1362
1363 // Top three apps using CPU in the last step, with times in 1/100 second.
1364 public int appCpuUid1;
1365 public int appCpuUTime1;
1366 public int appCpuSTime1;
1367 public int appCpuUid2;
1368 public int appCpuUTime2;
1369 public int appCpuSTime2;
1370 public int appCpuUid3;
1371 public int appCpuUTime3;
1372 public int appCpuSTime3;
1373
1374 // Information from /proc/stat
1375 public int statUserTime;
1376 public int statSystemTime;
1377 public int statIOWaitTime;
1378 public int statIrqTime;
1379 public int statSoftIrqTime;
1380 public int statIdlTime;
1381
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001382 // Platform-level low power state stats
1383 public String statPlatformIdleState;
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001384 public String statSubsystemPowerState;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001385
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001386 public HistoryStepDetails() {
1387 clear();
1388 }
1389
1390 public void clear() {
1391 userTime = systemTime = 0;
1392 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1393 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1394 = appCpuUTime3 = appCpuSTime3 = 0;
1395 }
1396
1397 public void writeToParcel(Parcel out) {
1398 out.writeInt(userTime);
1399 out.writeInt(systemTime);
1400 out.writeInt(appCpuUid1);
1401 out.writeInt(appCpuUTime1);
1402 out.writeInt(appCpuSTime1);
1403 out.writeInt(appCpuUid2);
1404 out.writeInt(appCpuUTime2);
1405 out.writeInt(appCpuSTime2);
1406 out.writeInt(appCpuUid3);
1407 out.writeInt(appCpuUTime3);
1408 out.writeInt(appCpuSTime3);
1409 out.writeInt(statUserTime);
1410 out.writeInt(statSystemTime);
1411 out.writeInt(statIOWaitTime);
1412 out.writeInt(statIrqTime);
1413 out.writeInt(statSoftIrqTime);
1414 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001415 out.writeString(statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001416 out.writeString(statSubsystemPowerState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001417 }
1418
1419 public void readFromParcel(Parcel in) {
1420 userTime = in.readInt();
1421 systemTime = in.readInt();
1422 appCpuUid1 = in.readInt();
1423 appCpuUTime1 = in.readInt();
1424 appCpuSTime1 = in.readInt();
1425 appCpuUid2 = in.readInt();
1426 appCpuUTime2 = in.readInt();
1427 appCpuSTime2 = in.readInt();
1428 appCpuUid3 = in.readInt();
1429 appCpuUTime3 = in.readInt();
1430 appCpuSTime3 = in.readInt();
1431 statUserTime = in.readInt();
1432 statSystemTime = in.readInt();
1433 statIOWaitTime = in.readInt();
1434 statIrqTime = in.readInt();
1435 statSoftIrqTime = in.readInt();
1436 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001437 statPlatformIdleState = in.readString();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001438 statSubsystemPowerState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001439 }
1440 }
1441
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001442 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001443 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001444
1445 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001446 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001447
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001448 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001449 public static final byte CMD_NULL = -1;
1450 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001451 public static final byte CMD_CURRENT_TIME = 5;
1452 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001453 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001454 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001455
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001456 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001457
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001458 /**
1459 * Return whether the command code is a delta data update.
1460 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001461 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001462 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001463 }
1464
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001465 public byte batteryLevel;
1466 public byte batteryStatus;
1467 public byte batteryHealth;
1468 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001469
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001470 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001471 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001472
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001473 // The charge of the battery in micro-Ampere-hours.
1474 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001475
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001476 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001477 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001478 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001479 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001480 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1481 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001482 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001483 public static final int STATE_PHONE_STATE_SHIFT = 6;
1484 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001485 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001486 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001487 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001488
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001489 // These states always appear directly in the first int token
1490 // of a delta change; they should be ones that change relatively
1491 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001492 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1493 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001494 public static final int STATE_GPS_ON_FLAG = 1<<29;
1495 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001496 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001497 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001498 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001499 // Do not use, this is used for coulomb delta count.
1500 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001501 // These are on the lower bits used for the command; if they change
1502 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001503 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001504 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001505 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001506 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1507 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
Mike Mac2f518a2017-09-19 16:06:03 -07001508 public static final int STATE_SCREEN_DOZE_FLAG = 1 << 18;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001509 // empty slot
1510 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001511
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001512 public static final int MOST_INTERESTING_STATES =
Mike Mac2f518a2017-09-19 16:06:03 -07001513 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG | STATE_SCREEN_DOZE_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001514
1515 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001516
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001517 public int states;
1518
Dianne Hackborn3251b902014-06-20 14:40:53 -07001519 // Constants from WIFI_SUPPL_STATE_*
1520 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1521 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1522 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1523 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1524 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1525 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
Siddharth Ray78ccaf52017-12-23 16:16:21 -08001526 // Values for NUM_GPS_SIGNAL_QUALITY_LEVELS
1527 public static final int STATE2_GPS_SIGNAL_QUALITY_SHIFT = 7;
1528 public static final int STATE2_GPS_SIGNAL_QUALITY_MASK =
1529 0x1 << STATE2_GPS_SIGNAL_QUALITY_SHIFT;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001530
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001531 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001532 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1533 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1534 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001535 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001536 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1537 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1538 public static final int STATE2_CHARGING_FLAG = 1<<24;
1539 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1540 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1541 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001542 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Siddharth Rayf5e796a2018-01-22 18:18:17 -08001543 public static final int STATE2_CELLULAR_HIGH_TX_POWER_FLAG = 1 << 19;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001544
1545 public static final int MOST_INTERESTING_STATES2 =
Mike Mac2f518a2017-09-19 16:06:03 -07001546 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
1547 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001548
1549 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001550
Dianne Hackborn40c87252014-03-19 16:55:40 -07001551 public int states2;
1552
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001553 // The wake lock that was acquired at this point.
1554 public HistoryTag wakelockTag;
1555
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001556 // Kernel wakeup reason at this point.
1557 public HistoryTag wakeReasonTag;
1558
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001559 // Non-null when there is more detailed information at this step.
1560 public HistoryStepDetails stepDetails;
1561
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001562 public static final int EVENT_FLAG_START = 0x8000;
1563 public static final int EVENT_FLAG_FINISH = 0x4000;
1564
1565 // No event in this item.
1566 public static final int EVENT_NONE = 0x0000;
1567 // Event is about a process that is running.
1568 public static final int EVENT_PROC = 0x0001;
1569 // Event is about an application package that is in the foreground.
1570 public static final int EVENT_FOREGROUND = 0x0002;
1571 // Event is about an application package that is at the top of the screen.
1572 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001573 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001574 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001575 // Events for all additional wake locks aquired/release within a wake block.
1576 // These are not generated by default.
1577 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001578 // Event is about an application executing a scheduled job.
1579 public static final int EVENT_JOB = 0x0006;
1580 // Events for users running.
1581 public static final int EVENT_USER_RUNNING = 0x0007;
1582 // Events for foreground user.
1583 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001584 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001585 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001586 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001587 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001588 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001589 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001590 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001591 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001592 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001593 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001594 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001595 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001596 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001597 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001598 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001599 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001600 // Event for a package being on the temporary whitelist.
1601 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001602 // Event for the screen waking up.
1603 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001604 // Event for the UID that woke up the application processor.
1605 // Used for wakeups coming from WiFi, modem, etc.
1606 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001607 // Event for reporting that a specific partial wake lock has been held for a long duration.
1608 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001609
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001610 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001611 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001612 // Mask to extract out only the type part of the event.
1613 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001614
1615 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1616 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1617 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1618 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1619 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1620 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001621 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1622 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001623 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1624 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001625 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1626 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1627 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1628 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1629 public static final int EVENT_USER_FOREGROUND_START =
1630 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1631 public static final int EVENT_USER_FOREGROUND_FINISH =
1632 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001633 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1634 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001635 public static final int EVENT_TEMP_WHITELIST_START =
1636 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1637 public static final int EVENT_TEMP_WHITELIST_FINISH =
1638 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001639 public static final int EVENT_LONG_WAKE_LOCK_START =
1640 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1641 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1642 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001643
1644 // For CMD_EVENT.
1645 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001646 public HistoryTag eventTag;
1647
Dianne Hackborn9a755432014-05-15 17:05:22 -07001648 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001649 public long currentTime;
1650
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001651 // Meta-data when reading.
1652 public int numReadInts;
1653
1654 // Pre-allocated objects.
1655 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001656 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001657 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001658
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001659 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001660 }
Bookatzc8c44962017-05-11 12:12:54 -07001661
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001662 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001663 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001664 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001665 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001666 }
Bookatzc8c44962017-05-11 12:12:54 -07001667
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001668 public int describeContents() {
1669 return 0;
1670 }
1671
1672 public void writeToParcel(Parcel dest, int flags) {
1673 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001674 int bat = (((int)cmd)&0xff)
1675 | ((((int)batteryLevel)<<8)&0xff00)
1676 | ((((int)batteryStatus)<<16)&0xf0000)
1677 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001678 | ((((int)batteryPlugType)<<24)&0xf000000)
1679 | (wakelockTag != null ? 0x10000000 : 0)
1680 | (wakeReasonTag != null ? 0x20000000 : 0)
1681 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001682 dest.writeInt(bat);
1683 bat = (((int)batteryTemperature)&0xffff)
1684 | ((((int)batteryVoltage)<<16)&0xffff0000);
1685 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001686 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001687 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001688 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001689 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001690 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001691 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001692 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001693 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001694 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001695 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001696 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001697 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001698 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001699 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001700 dest.writeLong(currentTime);
1701 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001702 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001703
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001704 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001705 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001706 int bat = src.readInt();
1707 cmd = (byte)(bat&0xff);
1708 batteryLevel = (byte)((bat>>8)&0xff);
1709 batteryStatus = (byte)((bat>>16)&0xf);
1710 batteryHealth = (byte)((bat>>20)&0xf);
1711 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001712 int bat2 = src.readInt();
1713 batteryTemperature = (short)(bat2&0xffff);
1714 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001715 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001716 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001717 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001718 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001719 wakelockTag = localWakelockTag;
1720 wakelockTag.readFromParcel(src);
1721 } else {
1722 wakelockTag = null;
1723 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001724 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001725 wakeReasonTag = localWakeReasonTag;
1726 wakeReasonTag.readFromParcel(src);
1727 } else {
1728 wakeReasonTag = null;
1729 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001730 if ((bat&0x40000000) != 0) {
1731 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001732 eventTag = localEventTag;
1733 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001734 } else {
1735 eventCode = EVENT_NONE;
1736 eventTag = null;
1737 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001738 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001739 currentTime = src.readLong();
1740 } else {
1741 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001742 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001743 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001744 }
1745
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001746 public void clear() {
1747 time = 0;
1748 cmd = CMD_NULL;
1749 batteryLevel = 0;
1750 batteryStatus = 0;
1751 batteryHealth = 0;
1752 batteryPlugType = 0;
1753 batteryTemperature = 0;
1754 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001755 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001756 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001757 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001758 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001759 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001760 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001761 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001762 }
Bookatzc8c44962017-05-11 12:12:54 -07001763
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001764 public void setTo(HistoryItem o) {
1765 time = o.time;
1766 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001767 setToCommon(o);
1768 }
1769
1770 public void setTo(long time, byte cmd, HistoryItem o) {
1771 this.time = time;
1772 this.cmd = cmd;
1773 setToCommon(o);
1774 }
1775
1776 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001777 batteryLevel = o.batteryLevel;
1778 batteryStatus = o.batteryStatus;
1779 batteryHealth = o.batteryHealth;
1780 batteryPlugType = o.batteryPlugType;
1781 batteryTemperature = o.batteryTemperature;
1782 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001783 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001784 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001785 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001786 if (o.wakelockTag != null) {
1787 wakelockTag = localWakelockTag;
1788 wakelockTag.setTo(o.wakelockTag);
1789 } else {
1790 wakelockTag = null;
1791 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001792 if (o.wakeReasonTag != null) {
1793 wakeReasonTag = localWakeReasonTag;
1794 wakeReasonTag.setTo(o.wakeReasonTag);
1795 } else {
1796 wakeReasonTag = null;
1797 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001798 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001799 if (o.eventTag != null) {
1800 eventTag = localEventTag;
1801 eventTag.setTo(o.eventTag);
1802 } else {
1803 eventTag = null;
1804 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001805 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001806 }
1807
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001808 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001809 return batteryLevel == o.batteryLevel
1810 && batteryStatus == o.batteryStatus
1811 && batteryHealth == o.batteryHealth
1812 && batteryPlugType == o.batteryPlugType
1813 && batteryTemperature == o.batteryTemperature
1814 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001815 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001816 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001817 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001818 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001819 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001820
1821 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001822 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001823 return false;
1824 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001825 if (wakelockTag != o.wakelockTag) {
1826 if (wakelockTag == null || o.wakelockTag == null) {
1827 return false;
1828 }
1829 if (!wakelockTag.equals(o.wakelockTag)) {
1830 return false;
1831 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001832 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001833 if (wakeReasonTag != o.wakeReasonTag) {
1834 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1835 return false;
1836 }
1837 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1838 return false;
1839 }
1840 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001841 if (eventTag != o.eventTag) {
1842 if (eventTag == null || o.eventTag == null) {
1843 return false;
1844 }
1845 if (!eventTag.equals(o.eventTag)) {
1846 return false;
1847 }
1848 }
1849 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001850 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001851 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001852
1853 public final static class HistoryEventTracker {
1854 private final HashMap<String, SparseIntArray>[] mActiveEvents
1855 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1856
1857 public boolean updateState(int code, String name, int uid, int poolIdx) {
1858 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1859 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1860 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1861 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001862 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001863 mActiveEvents[idx] = active;
1864 }
1865 SparseIntArray uids = active.get(name);
1866 if (uids == null) {
1867 uids = new SparseIntArray();
1868 active.put(name, uids);
1869 }
1870 if (uids.indexOfKey(uid) >= 0) {
1871 // Already set, nothing to do!
1872 return false;
1873 }
1874 uids.put(uid, poolIdx);
1875 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1876 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1877 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1878 if (active == null) {
1879 // not currently active, nothing to do.
1880 return false;
1881 }
1882 SparseIntArray uids = active.get(name);
1883 if (uids == null) {
1884 // not currently active, nothing to do.
1885 return false;
1886 }
1887 idx = uids.indexOfKey(uid);
1888 if (idx < 0) {
1889 // not currently active, nothing to do.
1890 return false;
1891 }
1892 uids.removeAt(idx);
1893 if (uids.size() <= 0) {
1894 active.remove(name);
1895 }
1896 }
1897 return true;
1898 }
1899
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001900 public void removeEvents(int code) {
1901 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1902 mActiveEvents[idx] = null;
1903 }
1904
Dianne Hackborn37de0982014-05-09 09:32:18 -07001905 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1906 return mActiveEvents[code];
1907 }
1908 }
1909
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001910 public static final class BitDescription {
1911 public final int mask;
1912 public final int shift;
1913 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001914 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001915 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001916 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07001917
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001918 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001919 this.mask = mask;
1920 this.shift = -1;
1921 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001922 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001923 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001924 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001925 }
Bookatzc8c44962017-05-11 12:12:54 -07001926
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001927 public BitDescription(int mask, int shift, String name, String shortName,
1928 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001929 this.mask = mask;
1930 this.shift = shift;
1931 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001932 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001933 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001934 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001935 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001936 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001937
Dianne Hackbornfc064132014-06-02 12:42:12 -07001938 /**
1939 * Don't allow any more batching in to the current history event. This
1940 * is called when printing partial histories, so to ensure that the next
1941 * history event will go in to a new batch after what was printed in the
1942 * last partial history.
1943 */
1944 public abstract void commitCurrentHistoryBatchLocked();
1945
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001946 public abstract int getHistoryTotalSize();
1947
1948 public abstract int getHistoryUsedSize();
1949
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001950 public abstract boolean startIteratingHistoryLocked();
1951
Dianne Hackborn099bc622014-01-22 13:39:16 -08001952 public abstract int getHistoryStringPoolSize();
1953
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001954 public abstract int getHistoryStringPoolBytes();
1955
1956 public abstract String getHistoryTagPoolString(int index);
1957
1958 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001959
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001960 public abstract boolean getNextHistoryLocked(HistoryItem out);
1961
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001962 public abstract void finishIteratingHistoryLocked();
1963
1964 public abstract boolean startIteratingOldHistoryLocked();
1965
1966 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1967
1968 public abstract void finishIteratingOldHistoryLocked();
1969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001971 * Return the base time offset for the battery history.
1972 */
1973 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07001974
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001975 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 * Returns the number of times the device has been started.
1977 */
1978 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07001979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001981 * 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 -08001982 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07001983 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 * {@hide}
1985 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001986 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07001987
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001988 /**
1989 * Returns the number of times the screen was turned on.
1990 *
1991 * {@hide}
1992 */
1993 public abstract int getScreenOnCount(int which);
1994
Mike Mac2f518a2017-09-19 16:06:03 -07001995 /**
1996 * Returns the time in microseconds that the screen has been dozing while the device was
1997 * running on battery.
1998 *
1999 * {@hide}
2000 */
2001 public abstract long getScreenDozeTime(long elapsedRealtimeUs, int which);
2002
2003 /**
2004 * Returns the number of times the screen was turned dozing.
2005 *
2006 * {@hide}
2007 */
2008 public abstract int getScreenDozeCount(int which);
2009
Jeff Browne95c3cd2014-05-02 16:59:26 -07002010 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
2011
Dianne Hackborn617f8772009-03-31 15:04:46 -07002012 public static final int SCREEN_BRIGHTNESS_DARK = 0;
2013 public static final int SCREEN_BRIGHTNESS_DIM = 1;
2014 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
2015 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
2016 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07002017
Dianne Hackborn617f8772009-03-31 15:04:46 -07002018 static final String[] SCREEN_BRIGHTNESS_NAMES = {
2019 "dark", "dim", "medium", "light", "bright"
2020 };
Bookatzc8c44962017-05-11 12:12:54 -07002021
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002022 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
2023 "0", "1", "2", "3", "4"
2024 };
2025
Dianne Hackborn617f8772009-03-31 15:04:46 -07002026 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07002027
Dianne Hackborn617f8772009-03-31 15:04:46 -07002028 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002029 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07002030 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07002031 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002032 * {@hide}
2033 */
2034 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002035 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002038 * Returns the {@link Timer} object that tracks the given screen brightness.
2039 *
2040 * {@hide}
2041 */
2042 public abstract Timer getScreenBrightnessTimer(int brightnessBin);
2043
2044 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002045 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002046 * running on battery.
2047 *
2048 * {@hide}
2049 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002050 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002051
2052 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002053 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002054 *
2055 * {@hide}
2056 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002057 public abstract int getPowerSaveModeEnabledCount(int which);
2058
2059 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002060 * Constant for device idle mode: not active.
2061 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002062 public static final int DEVICE_IDLE_MODE_OFF = ServerProtoEnums.DEVICE_IDLE_MODE_OFF; // 0
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002063
2064 /**
2065 * Constant for device idle mode: active in lightweight mode.
2066 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002067 public static final int DEVICE_IDLE_MODE_LIGHT = ServerProtoEnums.DEVICE_IDLE_MODE_LIGHT; // 1
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002068
2069 /**
2070 * Constant for device idle mode: active in full mode.
2071 */
Bookatz8bdae8d2018-01-16 11:24:30 -08002072 public static final int DEVICE_IDLE_MODE_DEEP = ServerProtoEnums.DEVICE_IDLE_MODE_DEEP; // 2
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002073
2074 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002075 * Returns the time in microseconds that device has been in idle mode while
2076 * running on battery.
2077 *
2078 * {@hide}
2079 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002080 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002081
2082 /**
2083 * Returns the number of times that the devie has gone in to idle mode.
2084 *
2085 * {@hide}
2086 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002087 public abstract int getDeviceIdleModeCount(int mode, int which);
2088
2089 /**
2090 * Return the longest duration we spent in a particular device idle mode (fully in the
2091 * mode, not in idle maintenance etc).
2092 */
2093 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002094
2095 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002096 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002097 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002098 * counts all of the time that we consider the device to be idle, whether or not
2099 * it is currently in the actual device idle mode.
2100 *
2101 * {@hide}
2102 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002103 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002104
2105 /**
Bookatz8c6571b2017-10-24 15:04:41 -07002106 * Returns the number of times that the device has started idling.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002107 *
2108 * {@hide}
2109 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002110 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002111
2112 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002113 * Returns the number of times that connectivity state changed.
2114 *
2115 * {@hide}
2116 */
2117 public abstract int getNumConnectivityChange(int which);
2118
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002119
2120 /**
2121 * Returns the time in microseconds that the phone has been running with
2122 * the given GPS signal quality level
2123 *
2124 * {@hide}
2125 */
2126 public abstract long getGpsSignalQualityTime(int strengthBin,
2127 long elapsedRealtimeUs, int which);
2128
2129 /**
2130 * Returns the GPS battery drain in mA-ms
2131 *
2132 * {@hide}
2133 */
2134 public abstract long getGpsBatteryDrainMaMs();
2135
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002136 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002137 * 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 -08002138 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002139 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 * {@hide}
2141 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002142 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002143
Dianne Hackborn627bba72009-03-24 22:32:56 -07002144 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002145 * Returns the number of times a phone call was activated.
2146 *
2147 * {@hide}
2148 */
2149 public abstract int getPhoneOnCount(int which);
2150
2151 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002152 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002153 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002154 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002155 * {@hide}
2156 */
2157 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002158 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002159
Dianne Hackborn617f8772009-03-31 15:04:46 -07002160 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07002161 * Returns the time in microseconds that the phone has been trying to
2162 * acquire a signal.
2163 *
2164 * {@hide}
2165 */
2166 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002167 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002168
2169 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002170 * Returns the {@link Timer} object that tracks how much the phone has been trying to
2171 * acquire a signal.
2172 *
2173 * {@hide}
2174 */
2175 public abstract Timer getPhoneSignalScanningTimer();
2176
2177 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002178 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002179 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002180 * {@hide}
2181 */
2182 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
2183
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002184 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002185 * Return the {@link Timer} object used to track the given signal strength's duration and
2186 * counts.
2187 */
2188 protected abstract Timer getPhoneSignalStrengthTimer(int strengthBin);
2189
2190 /**
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002191 * Returns the time in microseconds that the mobile network has been active
2192 * (in a high power state).
2193 *
2194 * {@hide}
2195 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002196 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002197
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002198 /**
2199 * Returns the number of times that the mobile network has transitioned to the
2200 * active state.
2201 *
2202 * {@hide}
2203 */
2204 public abstract int getMobileRadioActiveCount(int which);
2205
2206 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002207 * Returns the time in microseconds that is the difference between the mobile radio
2208 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
2209 * from the radio.
2210 *
2211 * {@hide}
2212 */
2213 public abstract long getMobileRadioActiveAdjustedTime(int which);
2214
2215 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002216 * Returns the time in microseconds that the mobile network has been active
2217 * (in a high power state) but not being able to blame on an app.
2218 *
2219 * {@hide}
2220 */
2221 public abstract long getMobileRadioActiveUnknownTime(int which);
2222
2223 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002224 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002225 *
2226 * {@hide}
2227 */
2228 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002229
Dianne Hackborn627bba72009-03-24 22:32:56 -07002230 public static final int DATA_CONNECTION_NONE = 0;
2231 public static final int DATA_CONNECTION_GPRS = 1;
2232 public static final int DATA_CONNECTION_EDGE = 2;
2233 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002234 public static final int DATA_CONNECTION_CDMA = 4;
2235 public static final int DATA_CONNECTION_EVDO_0 = 5;
2236 public static final int DATA_CONNECTION_EVDO_A = 6;
2237 public static final int DATA_CONNECTION_1xRTT = 7;
2238 public static final int DATA_CONNECTION_HSDPA = 8;
2239 public static final int DATA_CONNECTION_HSUPA = 9;
2240 public static final int DATA_CONNECTION_HSPA = 10;
2241 public static final int DATA_CONNECTION_IDEN = 11;
2242 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002243 public static final int DATA_CONNECTION_LTE = 13;
2244 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08002245 public static final int DATA_CONNECTION_HSPAP = 15;
Siddharth Rayc72081d2018-02-13 11:31:54 -08002246 public static final int DATA_CONNECTION_GSM = 16;
2247 public static final int DATA_CONNECTION_TD_SCDMA = 17;
2248 public static final int DATA_CONNECTION_IWLAN = 18;
2249 public static final int DATA_CONNECTION_LTE_CA = 19;
2250 public static final int DATA_CONNECTION_OTHER = 20;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002251
Dianne Hackborn627bba72009-03-24 22:32:56 -07002252 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002253 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002254 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Siddharth Rayc72081d2018-02-13 11:31:54 -08002255 "ehrpd", "hspap", "gsm", "td_scdma", "iwlan", "lte_ca", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002256 };
Bookatzc8c44962017-05-11 12:12:54 -07002257
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002258 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002259
Dianne Hackborn627bba72009-03-24 22:32:56 -07002260 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002261 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002262 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002263 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002264 * {@hide}
2265 */
2266 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002267 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002270 * Returns the number of times the phone has entered the given data
2271 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002272 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002273 * {@hide}
2274 */
2275 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002276
Kweku Adams87b19ec2017-10-09 12:40:03 -07002277 /**
2278 * Returns the {@link Timer} object that tracks the phone's data connection type stats.
2279 */
2280 public abstract Timer getPhoneDataConnectionTimer(int dataType);
2281
Dianne Hackborn3251b902014-06-20 14:40:53 -07002282 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2283 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2284 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2285 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2286 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2287 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2288 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2289 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2290 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2291 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2292 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2293 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2294 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2295
2296 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2297
2298 static final String[] WIFI_SUPPL_STATE_NAMES = {
2299 "invalid", "disconn", "disabled", "inactive", "scanning",
2300 "authenticating", "associating", "associated", "4-way-handshake",
2301 "group-handshake", "completed", "dormant", "uninit"
2302 };
2303
2304 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2305 "inv", "dsc", "dis", "inact", "scan",
2306 "auth", "ascing", "asced", "4-way",
2307 "group", "compl", "dorm", "uninit"
2308 };
2309
Mike Mac2f518a2017-09-19 16:06:03 -07002310 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002311 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002312 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2313 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002314 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002315 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2316 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2317 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002318 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002319 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002320 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002321 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002322 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2323 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Mike Mac2f518a2017-09-19 16:06:03 -07002324 new BitDescription(HistoryItem.STATE_SCREEN_DOZE_FLAG, "screen_doze", "Sd"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002325 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2326 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2327 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2328 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2329 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2330 new String[] {"in", "out", "emergency", "off"},
2331 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002332 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2333 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2334 SignalStrength.SIGNAL_STRENGTH_NAMES,
2335 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002336 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2337 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2338 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002339 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002340
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002341 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2342 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002343 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002344 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002345 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002346 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002347 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002348 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2349 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2350 new String[] { "off", "light", "full", "???" },
2351 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002352 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2353 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2354 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002355 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2356 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2357 new String[] { "0", "1", "2", "3", "4" },
2358 new String[] { "0", "1", "2", "3", "4" }),
2359 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2360 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2361 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002362 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002363 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Siddharth Rayf5e796a2018-01-22 18:18:17 -08002364 new BitDescription(HistoryItem.STATE2_CELLULAR_HIGH_TX_POWER_FLAG,
2365 "cellular_high_tx_power", "Chtp"),
Siddharth Ray78ccaf52017-12-23 16:16:21 -08002366 new BitDescription(HistoryItem.STATE2_GPS_SIGNAL_QUALITY_MASK,
2367 HistoryItem.STATE2_GPS_SIGNAL_QUALITY_SHIFT, "gps_signal_quality", "Gss",
Siddharth Rayf5e796a2018-01-22 18:18:17 -08002368 new String[] { "poor", "good"}, new String[] { "poor", "good"})
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002369 };
2370
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002371 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002372 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002373 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2374 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002375 };
2376
2377 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002378 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002379 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002380 "Esw", "Ewa", "Elw", "Eec"
2381 };
2382
2383 @FunctionalInterface
2384 public interface IntToString {
2385 String applyAsString(int val);
2386 }
2387
2388 private static final IntToString sUidToString = UserHandle::formatUid;
2389 private static final IntToString sIntToString = Integer::toString;
2390
2391 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2392 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2393 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2394 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2395 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002396 };
2397
Dianne Hackborn617f8772009-03-31 15:04:46 -07002398 /**
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08002399 * Returns total time for WiFi Multicast Wakelock timer.
2400 * Note that this may be different from the sum of per uid timer values.
2401 *
2402 * {@hide}
2403 */
2404 public abstract long getWifiMulticastWakelockTime(long elapsedRealtimeUs, int which);
2405
2406 /**
2407 * Returns total time for WiFi Multicast Wakelock timer
2408 * Note that this may be different from the sum of per uid timer values.
2409 *
2410 * {@hide}
2411 */
2412 public abstract int getWifiMulticastWakelockCount(int which);
2413
2414 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002415 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002416 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002417 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002418 * {@hide}
2419 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002420 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002421
2422 /**
Siddharth Rayb50a6842017-12-14 15:15:28 -08002423 * Returns the time in microseconds that wifi has been active while the device was
2424 * running on battery.
2425 *
2426 * {@hide}
2427 */
2428 public abstract long getWifiActiveTime(long elapsedRealtimeUs, int which);
2429
2430 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002431 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002432 * been in the running state while the device was running on battery.
2433 *
2434 * {@hide}
2435 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002436 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002437
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002438 public static final int WIFI_STATE_OFF = 0;
2439 public static final int WIFI_STATE_OFF_SCANNING = 1;
2440 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2441 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2442 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2443 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2444 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2445 public static final int WIFI_STATE_SOFT_AP = 7;
2446
2447 static final String[] WIFI_STATE_NAMES = {
2448 "off", "scanning", "no_net", "disconn",
2449 "sta", "p2p", "sta_p2p", "soft_ap"
2450 };
2451
2452 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2453
2454 /**
2455 * Returns the time in microseconds that WiFi has been running in the given state.
2456 *
2457 * {@hide}
2458 */
2459 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002460 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002461
2462 /**
2463 * Returns the number of times that WiFi has entered the given state.
2464 *
2465 * {@hide}
2466 */
2467 public abstract int getWifiStateCount(int wifiState, int which);
2468
The Android Open Source Project10592532009-03-18 17:39:46 -07002469 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002470 * Returns the {@link Timer} object that tracks the given WiFi state.
2471 *
2472 * {@hide}
2473 */
2474 public abstract Timer getWifiStateTimer(int wifiState);
2475
2476 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002477 * Returns the time in microseconds that the wifi supplicant has been
2478 * in a given state.
2479 *
2480 * {@hide}
2481 */
2482 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2483
2484 /**
2485 * Returns the number of times that the wifi supplicant has transitioned
2486 * to a given state.
2487 *
2488 * {@hide}
2489 */
2490 public abstract int getWifiSupplStateCount(int state, int which);
2491
Kweku Adams87b19ec2017-10-09 12:40:03 -07002492 /**
2493 * Returns the {@link Timer} object that tracks the given wifi supplicant state.
2494 *
2495 * {@hide}
2496 */
2497 public abstract Timer getWifiSupplStateTimer(int state);
2498
Dianne Hackborn3251b902014-06-20 14:40:53 -07002499 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2500
2501 /**
2502 * Returns the time in microseconds that WIFI has been running with
2503 * the given signal strength.
2504 *
2505 * {@hide}
2506 */
2507 public abstract long getWifiSignalStrengthTime(int strengthBin,
2508 long elapsedRealtimeUs, int which);
2509
2510 /**
2511 * Returns the number of times WIFI has entered the given signal strength.
2512 *
2513 * {@hide}
2514 */
2515 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2516
2517 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002518 * Returns the {@link Timer} object that tracks the given WIFI signal strength.
2519 *
2520 * {@hide}
2521 */
2522 public abstract Timer getWifiSignalStrengthTimer(int strengthBin);
2523
2524 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002525 * Returns the time in microseconds that the flashlight has been on while the device was
2526 * running on battery.
2527 *
2528 * {@hide}
2529 */
2530 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2531
2532 /**
2533 * Returns the number of times that the flashlight has been turned on while the device was
2534 * running on battery.
2535 *
2536 * {@hide}
2537 */
2538 public abstract long getFlashlightOnCount(int which);
2539
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002540 /**
2541 * Returns the time in microseconds that the camera has been on while the device was
2542 * running on battery.
2543 *
2544 * {@hide}
2545 */
2546 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2547
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002548 /**
2549 * Returns the time in microseconds that bluetooth scans were running while the device was
2550 * on battery.
2551 *
2552 * {@hide}
2553 */
2554 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002555
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002556 public static final int NETWORK_MOBILE_RX_DATA = 0;
2557 public static final int NETWORK_MOBILE_TX_DATA = 1;
2558 public static final int NETWORK_WIFI_RX_DATA = 2;
2559 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002560 public static final int NETWORK_BT_RX_DATA = 4;
2561 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002562 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2563 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2564 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2565 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2566 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002567
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002568 public abstract long getNetworkActivityBytes(int type, int which);
2569 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002570
Adam Lesinskie08af192015-03-25 16:42:59 -07002571 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002572 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002573 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002574 * actual power data.
2575 */
2576 public abstract boolean hasWifiActivityReporting();
2577
2578 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002579 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2580 * in various radio controller states, such as transmit, receive, and idle.
2581 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002582 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002583 public abstract ControllerActivityCounter getWifiControllerActivity();
2584
2585 /**
2586 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2587 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2588 * actual power data.
2589 */
2590 public abstract boolean hasBluetoothActivityReporting();
2591
2592 /**
2593 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2594 * in various radio controller states, such as transmit, receive, and idle.
2595 * @return non-null {@link ControllerActivityCounter}
2596 */
2597 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2598
2599 /**
2600 * Returns true if the BatteryStats object has detailed modem power reports.
2601 * When true, calling {@link #getModemControllerActivity()} will yield the
2602 * actual power data.
2603 */
2604 public abstract boolean hasModemActivityReporting();
2605
2606 /**
2607 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2608 * in various radio controller states, such as transmit, receive, and idle.
2609 * @return non-null {@link ControllerActivityCounter}
2610 */
2611 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002612
The Android Open Source Project10592532009-03-18 17:39:46 -07002613 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002614 * Return the wall clock time when battery stats data collection started.
2615 */
2616 public abstract long getStartClockTime();
2617
2618 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002619 * Return platform version tag that we were running in when the battery stats started.
2620 */
2621 public abstract String getStartPlatformVersion();
2622
2623 /**
2624 * Return platform version tag that we were running in when the battery stats ended.
2625 */
2626 public abstract String getEndPlatformVersion();
2627
2628 /**
2629 * Return the internal version code of the parcelled format.
2630 */
2631 public abstract int getParcelVersion();
2632
2633 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 * Return whether we are currently running on battery.
2635 */
2636 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 /**
2639 * Returns a SparseArray containing the statistics for each uid.
2640 */
2641 public abstract SparseArray<? extends Uid> getUidStats();
2642
2643 /**
2644 * Returns the current battery uptime in microseconds.
2645 *
2646 * @param curTime the amount of elapsed realtime in microseconds.
2647 */
2648 public abstract long getBatteryUptime(long curTime);
2649
2650 /**
2651 * Returns the current battery realtime in microseconds.
2652 *
2653 * @param curTime the amount of elapsed realtime in microseconds.
2654 */
2655 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002656
The Android Open Source Project10592532009-03-18 17:39:46 -07002657 /**
Evan Millar633a1742009-04-02 16:36:33 -07002658 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002659 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002660 */
Evan Millar633a1742009-04-02 16:36:33 -07002661 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002662
The Android Open Source Project10592532009-03-18 17:39:46 -07002663 /**
Evan Millar633a1742009-04-02 16:36:33 -07002664 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2665 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002666 */
Evan Millar633a1742009-04-02 16:36:33 -07002667 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002668
2669 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002670 * Get the amount the battery has discharged since the stats were
2671 * last reset after charging, as a lower-end approximation.
2672 */
2673 public abstract int getLowDischargeAmountSinceCharge();
2674
2675 /**
2676 * Get the amount the battery has discharged since the stats were
2677 * last reset after charging, as an upper-end approximation.
2678 */
2679 public abstract int getHighDischargeAmountSinceCharge();
2680
2681 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002682 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2683 */
2684 public abstract int getDischargeAmount(int which);
2685
2686 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002687 * Get the amount the battery has discharged while the screen was on,
2688 * since the last time power was unplugged.
2689 */
2690 public abstract int getDischargeAmountScreenOn();
2691
2692 /**
2693 * Get the amount the battery has discharged while the screen was on,
2694 * since the last time the device was charged.
2695 */
2696 public abstract int getDischargeAmountScreenOnSinceCharge();
2697
2698 /**
2699 * Get the amount the battery has discharged while the screen was off,
2700 * since the last time power was unplugged.
2701 */
2702 public abstract int getDischargeAmountScreenOff();
2703
2704 /**
2705 * Get the amount the battery has discharged while the screen was off,
2706 * since the last time the device was charged.
2707 */
2708 public abstract int getDischargeAmountScreenOffSinceCharge();
2709
2710 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002711 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002712 * since the last time power was unplugged.
2713 */
2714 public abstract int getDischargeAmountScreenDoze();
2715
2716 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002717 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002718 * since the last time the device was charged.
2719 */
2720 public abstract int getDischargeAmountScreenDozeSinceCharge();
2721
2722 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 * Returns the total, last, or current battery uptime in microseconds.
2724 *
2725 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002726 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 */
2728 public abstract long computeBatteryUptime(long curTime, int which);
2729
2730 /**
2731 * Returns the total, last, or current battery realtime in microseconds.
2732 *
2733 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002734 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 */
2736 public abstract long computeBatteryRealtime(long curTime, int which);
2737
2738 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002739 * Returns the total, last, or current battery screen off/doze uptime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002740 *
2741 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002742 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002743 */
2744 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2745
2746 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002747 * Returns the total, last, or current battery screen off/doze realtime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002748 *
2749 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002750 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002751 */
2752 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2753
2754 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 * Returns the total, last, or current uptime in microseconds.
2756 *
2757 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002758 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 */
2760 public abstract long computeUptime(long curTime, int which);
2761
2762 /**
2763 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002764 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002766 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 */
2768 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002769
2770 /**
2771 * Compute an approximation for how much run time (in microseconds) is remaining on
2772 * the battery. Returns -1 if no time can be computed: either there is not
2773 * enough current data to make a decision, or the battery is currently
2774 * charging.
2775 *
2776 * @param curTime The current elepsed realtime in microseconds.
2777 */
2778 public abstract long computeBatteryTimeRemaining(long curTime);
2779
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002780 // The part of a step duration that is the actual time.
2781 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2782
2783 // Bits in a step duration that are the new battery level we are at.
2784 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002785 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002786
2787 // Bits in a step duration that are the initial mode we were in at that step.
2788 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002789 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002790
2791 // Bits in a step duration that indicate which modes changed during that step.
2792 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002793 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002794
2795 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2796 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2797
Santos Cordone94f0502017-02-24 12:31:20 -08002798 // The largest value for screen state that is tracked in battery states. Any values above
2799 // this should be mapped back to one of the tracked values before being tracked here.
2800 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2801
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002802 // Step duration mode: power save is on.
2803 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2804
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002805 // Step duration mode: device is currently in idle mode.
2806 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2807
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002808 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
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 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2813 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2814 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2815 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2816 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002817 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2818 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002819 };
2820 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2821 (Display.STATE_OFF-1),
2822 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002823 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002824 (Display.STATE_ON-1),
2825 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2826 (Display.STATE_DOZE-1),
2827 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2828 (Display.STATE_DOZE_SUSPEND-1),
2829 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002830 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002831 };
2832 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2833 "screen off",
2834 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002835 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002836 "screen on",
2837 "screen on power save",
2838 "screen doze",
2839 "screen doze power save",
2840 "screen doze-suspend",
2841 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002842 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002843 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002844
2845 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002846 * Return the amount of battery discharge while the screen was off, measured in
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002847 * 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 getUahDischargeScreenOff(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002851
2852 /**
2853 * Return the amount of battery discharge while the screen was in doze mode, measured in
2854 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2855 * a coulomb counter.
2856 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002857 public abstract long getUahDischargeScreenDoze(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002858
2859 /**
2860 * Return the amount of battery discharge measured in micro-Ampere-hours. This will be
2861 * non-zero only if the device's battery has a coulomb counter.
2862 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002863 public abstract long getUahDischarge(int which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002864
2865 /**
Mike Ma15313c92017-11-15 17:58:21 -08002866 * @return the amount of battery discharge while the device is in light idle mode, measured in
2867 * micro-Ampere-hours.
2868 */
2869 public abstract long getUahDischargeLightDoze(int which);
2870
2871 /**
2872 * @return the amount of battery discharge while the device is in deep idle mode, measured in
2873 * micro-Ampere-hours.
2874 */
2875 public abstract long getUahDischargeDeepDoze(int which);
2876
2877 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002878 * Returns the estimated real battery capacity, which may be less than the capacity
2879 * declared by the PowerProfile.
2880 * @return The estimated battery capacity in mAh.
2881 */
2882 public abstract int getEstimatedBatteryCapacity();
2883
2884 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002885 * @return The minimum learned battery capacity in uAh.
2886 */
2887 public abstract int getMinLearnedBatteryCapacity();
2888
2889 /**
2890 * @return The maximum learned battery capacity in uAh.
2891 */
2892 public abstract int getMaxLearnedBatteryCapacity() ;
2893
2894 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002895 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002896 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002897 public abstract LevelStepTracker getDischargeLevelStepTracker();
2898
2899 /**
2900 * Return the array of daily discharge step durations.
2901 */
2902 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002903
2904 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002905 * Compute an approximation for how much time (in microseconds) remains until the battery
2906 * is fully charged. Returns -1 if no time can be computed: either there is not
2907 * enough current data to make a decision, or the battery is currently
2908 * discharging.
2909 *
2910 * @param curTime The current elepsed realtime in microseconds.
2911 */
2912 public abstract long computeChargeTimeRemaining(long curTime);
2913
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002914 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002915 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002916 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002917 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002918
2919 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002920 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002921 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002922 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002923
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002924 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2925
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002926 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002927
Evan Millarc64edde2009-04-18 12:26:32 -07002928 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929
Bookatz50df7112017-08-04 14:53:26 -07002930 /**
2931 * Returns Timers tracking the total time of each Resource Power Manager state and voter.
2932 */
2933 public abstract Map<String, ? extends Timer> getRpmStats();
2934 /**
2935 * Returns Timers tracking the screen-off time of each Resource Power Manager state and voter.
2936 */
2937 public abstract Map<String, ? extends Timer> getScreenOffRpmStats();
2938
2939
James Carr2dd7e5e2016-07-20 18:48:39 -07002940 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2941
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002942 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2943
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002944 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 long days = seconds / (60 * 60 * 24);
2946 if (days != 0) {
2947 out.append(days);
2948 out.append("d ");
2949 }
2950 long used = days * 60 * 60 * 24;
2951
2952 long hours = (seconds - used) / (60 * 60);
2953 if (hours != 0 || used != 0) {
2954 out.append(hours);
2955 out.append("h ");
2956 }
2957 used += hours * 60 * 60;
2958
2959 long mins = (seconds-used) / 60;
2960 if (mins != 0 || used != 0) {
2961 out.append(mins);
2962 out.append("m ");
2963 }
2964 used += mins * 60;
2965
2966 if (seconds != 0 || used != 0) {
2967 out.append(seconds-used);
2968 out.append("s ");
2969 }
2970 }
2971
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002972 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002974 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 sb.append(time - (sec * 1000));
2976 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 }
2978
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002979 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002980 long sec = time / 1000;
2981 formatTimeRaw(sb, sec);
2982 sb.append(time - (sec * 1000));
2983 sb.append("ms");
2984 }
2985
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002986 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002987 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002988 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989 }
2990 float perc = ((float)num) / ((float)den) * 100;
2991 mFormatBuilder.setLength(0);
2992 mFormatter.format("%.1f%%", perc);
2993 return mFormatBuilder.toString();
2994 }
2995
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002996 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002997 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07002998
Evan Millar22ac0432009-03-31 11:33:18 -07002999 if (bytes < BYTES_PER_KB) {
3000 return bytes + "B";
3001 } else if (bytes < BYTES_PER_MB) {
3002 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
3003 return mFormatBuilder.toString();
3004 } else if (bytes < BYTES_PER_GB){
3005 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
3006 return mFormatBuilder.toString();
3007 } else {
3008 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
3009 return mFormatBuilder.toString();
3010 }
3011 }
3012
Kweku Adams103351f2017-10-16 14:39:34 -07003013 private static long roundUsToMs(long timeUs) {
3014 return (timeUs + 500) / 1000;
3015 }
3016
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003017 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003018 if (timer != null) {
3019 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003020 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07003021 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
3022 return totalTimeMillis;
3023 }
3024 return 0;
3025 }
3026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 /**
3028 *
3029 * @param sb a StringBuilder object.
3030 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003031 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003033 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 * @param linePrefix a String to be prepended to each line of output.
3035 * @return the line prefix
3036 */
3037 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003038 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003041 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003042
Evan Millarc64edde2009-04-18 12:26:32 -07003043 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 if (totalTimeMillis != 0) {
3045 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003046 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08003047 if (name != null) {
3048 sb.append(name);
3049 sb.append(' ');
3050 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 sb.append('(');
3052 sb.append(count);
3053 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003054 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
3055 if (maxDurationMs >= 0) {
3056 sb.append(" max=");
3057 sb.append(maxDurationMs);
3058 }
Bookatz506a8182017-05-01 14:18:42 -07003059 // Put actual time if it is available and different from totalTimeMillis.
3060 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
3061 if (totalDurMs > totalTimeMillis) {
3062 sb.append(" actual=");
3063 sb.append(totalDurMs);
3064 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07003065 if (timer.isRunningLocked()) {
3066 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3067 if (currentMs >= 0) {
3068 sb.append(" (running for ");
3069 sb.append(currentMs);
3070 sb.append("ms)");
3071 } else {
3072 sb.append(" (running)");
3073 }
3074 }
3075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 return ", ";
3077 }
3078 }
3079 return linePrefix;
3080 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003081
3082 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07003083 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003084 *
3085 * @param pw a PrintWriter object to print to.
3086 * @param sb a StringBuilder object.
3087 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08003088 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003089 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
3090 * @param prefix a String to be prepended to each line of output.
3091 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07003092 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003093 */
3094 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07003095 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003096 if (timer != null) {
3097 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07003098 final long totalTimeMs = (timer.getTotalTimeLocked(
3099 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003100 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003101 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003102 sb.setLength(0);
3103 sb.append(prefix);
3104 sb.append(" ");
3105 sb.append(type);
3106 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003107 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003108 sb.append("realtime (");
3109 sb.append(count);
3110 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07003111 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
3112 if (maxDurationMs >= 0) {
3113 sb.append(" max=");
3114 sb.append(maxDurationMs);
3115 }
3116 if (timer.isRunningLocked()) {
3117 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
3118 if (currentMs >= 0) {
3119 sb.append(" (running for ");
3120 sb.append(currentMs);
3121 sb.append("ms)");
3122 } else {
3123 sb.append(" (running)");
3124 }
3125 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003126 pw.println(sb.toString());
3127 return true;
3128 }
3129 }
3130 return false;
3131 }
Bookatzc8c44962017-05-11 12:12:54 -07003132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133 /**
3134 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07003135 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003136 * @param sb a StringBuilder object.
3137 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003138 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003140 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 * @param linePrefix a String to be prepended to each line of output.
3142 * @return the line prefix
3143 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003144 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
3145 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003146 long totalTimeMicros = 0;
3147 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07003148 long max = 0;
3149 long current = 0;
3150 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003152 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07003153 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003154 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
3155 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07003156 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157 }
3158 sb.append(linePrefix);
3159 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
3160 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07003161 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07003163 sb.append(',');
3164 sb.append(current);
3165 sb.append(',');
3166 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07003167 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
3168 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
3169 // totalDuration independent of totalTimeMicros (since they are not pooled).
3170 if (name != null) {
3171 sb.append(',');
3172 sb.append(totalDuration);
3173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 return ",";
3175 }
Bookatz506a8182017-05-01 14:18:42 -07003176
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003177 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
3178 String type) {
3179 pw.print(BATTERY_STATS_CHECKIN_VERSION);
3180 pw.print(',');
3181 pw.print(uid);
3182 pw.print(',');
3183 pw.print(category);
3184 pw.print(',');
3185 pw.print(type);
3186 }
3187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 /**
3189 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07003190 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 * @param pw the PageWriter to dump log to
3192 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3193 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3194 * @param args type-dependent data arguments
3195 */
Bookatzc8c44962017-05-11 12:12:54 -07003196 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003198 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003199 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003200 pw.print(',');
3201 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003202 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003203 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07003205
3206 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003207 * Dump a given timer stat for terse checkin mode.
3208 *
3209 * @param pw the PageWriter to dump log to
3210 * @param uid the UID to log
3211 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3212 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3213 * @param timer a {@link Timer} to dump stats for
3214 * @param rawRealtime the current elapsed realtime of the system in microseconds
3215 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3216 */
3217 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
3218 Timer timer, long rawRealtime, int which) {
3219 if (timer != null) {
3220 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003221 final long totalTime = roundUsToMs(timer.getTotalTimeLocked(rawRealtime, which));
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003222 final int count = timer.getCountLocked(which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003223 if (totalTime != 0 || count != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003224 dumpLine(pw, uid, category, type, totalTime, count);
3225 }
3226 }
3227 }
3228
3229 /**
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003230 * Dump a given timer stat to the proto stream.
3231 *
3232 * @param proto the ProtoOutputStream to log to
3233 * @param fieldId type of data, the field to save to (e.g. AggregatedBatteryStats.WAKELOCK)
3234 * @param timer a {@link Timer} to dump stats for
3235 * @param rawRealtimeUs the current elapsed realtime of the system in microseconds
3236 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3237 */
3238 private static void dumpTimer(ProtoOutputStream proto, long fieldId,
Kweku Adams87b19ec2017-10-09 12:40:03 -07003239 Timer timer, long rawRealtimeUs, int which) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003240 if (timer == null) {
3241 return;
3242 }
3243 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003244 final long timeMs = roundUsToMs(timer.getTotalTimeLocked(rawRealtimeUs, which));
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003245 final int count = timer.getCountLocked(which);
Kweku Adams103351f2017-10-16 14:39:34 -07003246 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs / 1000);
3247 final long curDurationMs = timer.getCurrentDurationMsLocked(rawRealtimeUs / 1000);
3248 final long totalDurationMs = timer.getTotalDurationMsLocked(rawRealtimeUs / 1000);
3249 if (timeMs != 0 || count != 0 || maxDurationMs != -1 || curDurationMs != -1
3250 || totalDurationMs != -1) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003251 final long token = proto.start(fieldId);
Kweku Adams103351f2017-10-16 14:39:34 -07003252 proto.write(TimerProto.DURATION_MS, timeMs);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003253 proto.write(TimerProto.COUNT, count);
Kweku Adams103351f2017-10-16 14:39:34 -07003254 // These values will be -1 for timers that don't implement the functionality.
3255 if (maxDurationMs != -1) {
3256 proto.write(TimerProto.MAX_DURATION_MS, maxDurationMs);
3257 }
3258 if (curDurationMs != -1) {
3259 proto.write(TimerProto.CURRENT_DURATION_MS, curDurationMs);
3260 }
3261 if (totalDurationMs != -1) {
3262 proto.write(TimerProto.TOTAL_DURATION_MS, totalDurationMs);
3263 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003264 proto.end(token);
3265 }
3266 }
3267
3268 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003269 * Checks if the ControllerActivityCounter has any data worth dumping.
3270 */
3271 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
3272 if (counter == null) {
3273 return false;
3274 }
3275
3276 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
3277 || counter.getRxTimeCounter().getCountLocked(which) != 0
3278 || counter.getPowerCounter().getCountLocked(which) != 0) {
3279 return true;
3280 }
3281
3282 for (LongCounter c : counter.getTxTimeCounters()) {
3283 if (c.getCountLocked(which) != 0) {
3284 return true;
3285 }
3286 }
3287 return false;
3288 }
3289
3290 /**
3291 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3292 * The order of the arguments in the final check in line is:
3293 *
3294 * idle, rx, power, tx...
3295 *
3296 * where tx... is one or more transmit level times.
3297 */
3298 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
3299 String type,
3300 ControllerActivityCounter counter,
3301 int which) {
3302 if (!controllerActivityHasData(counter, which)) {
3303 return;
3304 }
3305
3306 dumpLineHeader(pw, uid, category, type);
3307 pw.print(",");
3308 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
3309 pw.print(",");
3310 pw.print(counter.getRxTimeCounter().getCountLocked(which));
3311 pw.print(",");
3312 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3313 for (LongCounter c : counter.getTxTimeCounters()) {
3314 pw.print(",");
3315 pw.print(c.getCountLocked(which));
3316 }
3317 pw.println();
3318 }
3319
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003320 /**
3321 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3322 */
3323 private static void dumpControllerActivityProto(ProtoOutputStream proto, long fieldId,
3324 ControllerActivityCounter counter,
3325 int which) {
3326 if (!controllerActivityHasData(counter, which)) {
3327 return;
3328 }
3329
3330 final long cToken = proto.start(fieldId);
3331
3332 proto.write(ControllerActivityProto.IDLE_DURATION_MS,
3333 counter.getIdleTimeCounter().getCountLocked(which));
3334 proto.write(ControllerActivityProto.RX_DURATION_MS,
3335 counter.getRxTimeCounter().getCountLocked(which));
3336 proto.write(ControllerActivityProto.POWER_MAH,
3337 counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3338
3339 long tToken;
3340 LongCounter[] txCounters = counter.getTxTimeCounters();
3341 for (int i = 0; i < txCounters.length; ++i) {
3342 LongCounter c = txCounters[i];
3343 tToken = proto.start(ControllerActivityProto.TX);
3344 proto.write(ControllerActivityProto.TxLevel.LEVEL, i);
3345 proto.write(ControllerActivityProto.TxLevel.DURATION_MS, c.getCountLocked(which));
3346 proto.end(tToken);
3347 }
3348
3349 proto.end(cToken);
3350 }
3351
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003352 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
3353 String prefix, String controllerName,
3354 ControllerActivityCounter counter,
3355 int which) {
3356 if (controllerActivityHasData(counter, which)) {
3357 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
3358 }
3359 }
3360
3361 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
3362 String controllerName,
3363 ControllerActivityCounter counter, int which) {
3364 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
3365 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
3366 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003367 // Battery real time
3368 final long totalControllerActivityTimeMs
3369 = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003370 long totalTxTimeMs = 0;
3371 for (LongCounter txState : counter.getTxTimeCounters()) {
3372 totalTxTimeMs += txState.getCountLocked(which);
3373 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003374 final long sleepTimeMs
3375 = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + totalTxTimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003376
Siddharth Rayb50a6842017-12-14 15:15:28 -08003377 if (controllerName.equals(WIFI_CONTROLLER_NAME)) {
3378 final long scanTimeMs = counter.getScanTimeCounter().getCountLocked(which);
3379 sb.setLength(0);
3380 sb.append(prefix);
3381 sb.append(" ");
3382 sb.append(controllerName);
3383 sb.append(" Scan time: ");
3384 formatTimeMs(sb, scanTimeMs);
3385 sb.append("(");
3386 sb.append(formatRatioLocked(scanTimeMs, totalControllerActivityTimeMs));
3387 sb.append(")");
3388 pw.println(sb.toString());
3389 }
3390
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003391 sb.setLength(0);
3392 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003393 sb.append(" ");
3394 sb.append(controllerName);
3395 sb.append(" Sleep time: ");
3396 formatTimeMs(sb, sleepTimeMs);
3397 sb.append("(");
3398 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3399 sb.append(")");
3400 pw.println(sb.toString());
3401
3402 sb.setLength(0);
3403 sb.append(prefix);
3404 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003405 sb.append(controllerName);
3406 sb.append(" Idle time: ");
3407 formatTimeMs(sb, idleTimeMs);
3408 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003409 sb.append(formatRatioLocked(idleTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003410 sb.append(")");
3411 pw.println(sb.toString());
3412
3413 sb.setLength(0);
3414 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003415 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003416 sb.append(controllerName);
3417 sb.append(" Rx time: ");
3418 formatTimeMs(sb, rxTimeMs);
3419 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003420 sb.append(formatRatioLocked(rxTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003421 sb.append(")");
3422 pw.println(sb.toString());
3423
3424 sb.setLength(0);
3425 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003426 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003427 sb.append(controllerName);
3428 sb.append(" Tx time: ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003429
Siddharth Ray3c648c42017-10-02 17:30:58 -07003430 String [] powerLevel;
3431 switch(controllerName) {
Siddharth Rayb50a6842017-12-14 15:15:28 -08003432 case CELLULAR_CONTROLLER_NAME:
Siddharth Ray3c648c42017-10-02 17:30:58 -07003433 powerLevel = new String[] {
3434 " less than 0dBm: ",
3435 " 0dBm to 8dBm: ",
3436 " 8dBm to 15dBm: ",
3437 " 15dBm to 20dBm: ",
3438 " above 20dBm: "};
3439 break;
3440 default:
3441 powerLevel = new String[] {"[0]", "[1]", "[2]", "[3]", "[4]"};
3442 break;
3443 }
3444 final int numTxLvls = Math.min(counter.getTxTimeCounters().length, powerLevel.length);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003445 if (numTxLvls > 1) {
Siddharth Ray3c648c42017-10-02 17:30:58 -07003446 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003447 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3448 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3449 sb.setLength(0);
3450 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003451 sb.append(" ");
3452 sb.append(powerLevel[lvl]);
3453 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003454 formatTimeMs(sb, txLvlTimeMs);
3455 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003456 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003457 sb.append(")");
3458 pw.println(sb.toString());
3459 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003460 } else {
3461 final long txLvlTimeMs = counter.getTxTimeCounters()[0].getCountLocked(which);
3462 formatTimeMs(sb, txLvlTimeMs);
3463 sb.append("(");
3464 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
3465 sb.append(")");
3466 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003467 }
3468
Siddharth Ray3c648c42017-10-02 17:30:58 -07003469 if (powerDrainMaMs > 0) {
3470 sb.setLength(0);
3471 sb.append(prefix);
3472 sb.append(" ");
3473 sb.append(controllerName);
3474 sb.append(" Battery drain: ").append(
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003475 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
Siddharth Ray3c648c42017-10-02 17:30:58 -07003476 sb.append("mAh");
3477 pw.println(sb.toString());
3478 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003479 }
3480
3481 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003482 * Temporary for settings.
3483 */
3484 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3485 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3486 }
3487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 /**
3489 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003490 *
Kweku Adams87b19ec2017-10-09 12:40:03 -07003491 * NOTE: all times are expressed in microseconds, unless specified otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003493 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3494 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 final long rawUptime = SystemClock.uptimeMillis() * 1000;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003496 final long rawRealtimeMs = SystemClock.elapsedRealtime();
3497 final long rawRealtime = rawRealtimeMs * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003498 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3500 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003501 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3502 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3503 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 final long totalRealtime = computeRealtime(rawRealtime, which);
3505 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003506 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Mike Mac2f518a2017-09-19 16:06:03 -07003507 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003508 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003509 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003510 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3511 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003512 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003513 rawRealtime, which);
3514 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3515 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003516 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003517 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003518 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003519 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003520 final long dischargeCount = getUahDischarge(which);
3521 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
3522 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Ma15313c92017-11-15 17:58:21 -08003523 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
3524 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003525
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003526 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003527
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003528 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003529 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003530
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003531 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003533 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003534 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003535 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003536 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003537 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003538 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003539 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003540 getEstimatedBatteryCapacity(),
Mike Mac2f518a2017-09-19 16:06:03 -07003541 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity(),
3542 screenDozeTime / 1000);
Adam Lesinski67c134f2016-06-10 15:15:08 -07003543
Bookatzc8c44962017-05-11 12:12:54 -07003544
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003545 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003546 long fullWakeLockTimeTotal = 0;
3547 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003548
Evan Millar22ac0432009-03-31 11:33:18 -07003549 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003550 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003551
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003552 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3553 = u.getWakelockStats();
3554 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3555 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003556
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003557 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3558 if (fullWakeTimer != null) {
3559 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3560 which);
3561 }
3562
3563 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3564 if (partialWakeTimer != null) {
3565 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3566 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003567 }
3568 }
3569 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003570
3571 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003572 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3573 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3574 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3575 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3576 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3577 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3578 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3579 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003580 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3581 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003582 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3583 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003584 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3585 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003586
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003587 // Dump Modem controller stats
3588 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3589 getModemControllerActivity(), which);
3590
Adam Lesinskie283d332015-04-16 12:29:25 -07003591 // Dump Wifi controller stats
3592 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3593 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003594 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003595 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003596
3597 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3598 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003599
3600 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003601 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3602 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003604 // Dump misc stats
3605 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003606 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003607 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003608 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003609 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003610 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003611 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3612 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003613 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003614 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3615 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3616 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3617 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003618 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003619
Dianne Hackborn617f8772009-03-31 15:04:46 -07003620 // Dump screen brightness stats
3621 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3622 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003623 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003624 }
3625 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003626
Dianne Hackborn627bba72009-03-24 22:32:56 -07003627 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003628 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3629 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003630 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003631 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003632 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003633 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003634 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003635 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003636 args[i] = getPhoneSignalStrengthCount(i, which);
3637 }
3638 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003639
Dianne Hackborn627bba72009-03-24 22:32:56 -07003640 // Dump network type stats
3641 args = new Object[NUM_DATA_CONNECTION_TYPES];
3642 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003643 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003644 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003645 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3646 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3647 args[i] = getPhoneDataConnectionCount(i, which);
3648 }
3649 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003650
3651 // Dump wifi state stats
3652 args = new Object[NUM_WIFI_STATES];
3653 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003654 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003655 }
3656 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3657 for (int i=0; i<NUM_WIFI_STATES; i++) {
3658 args[i] = getWifiStateCount(i, which);
3659 }
3660 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3661
Dianne Hackborn3251b902014-06-20 14:40:53 -07003662 // Dump wifi suppl state stats
3663 args = new Object[NUM_WIFI_SUPPL_STATES];
3664 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3665 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3666 }
3667 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3668 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3669 args[i] = getWifiSupplStateCount(i, which);
3670 }
3671 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3672
3673 // Dump wifi signal strength stats
3674 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3675 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3676 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3677 }
3678 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3679 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3680 args[i] = getWifiSignalStrengthCount(i, which);
3681 }
3682 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3683
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003684 // Dump Multicast total stats
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08003685 final long multicastWakeLockTimeTotalMicros =
3686 getWifiMulticastWakelockTime(rawRealtime, which);
3687 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07003688 dumpLine(pw, 0 /* uid */, category, WIFI_MULTICAST_TOTAL_DATA,
3689 multicastWakeLockTimeTotalMicros / 1000,
3690 multicastWakeLockCountTotal);
3691
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003692 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003693 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003694 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003695 }
Bookatzc8c44962017-05-11 12:12:54 -07003696
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003697 if (which == STATS_SINCE_UNPLUGGED) {
3698 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3699 getDischargeStartLevel()-getDischargeCurrentLevel(),
3700 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003701 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
Mike Mac2f518a2017-09-19 16:06:03 -07003702 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003703 getDischargeAmountScreenDoze(), dischargeScreenDozeCount / 1000,
3704 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003705 } else {
3706 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3707 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003708 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003709 getDischargeAmountScreenOffSinceCharge(),
Mike Mac2f518a2017-09-19 16:06:03 -07003710 dischargeCount / 1000, dischargeScreenOffCount / 1000,
Mike Ma15313c92017-11-15 17:58:21 -08003711 getDischargeAmountScreenDozeSinceCharge(), dischargeScreenDozeCount / 1000,
3712 dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003713 }
Bookatzc8c44962017-05-11 12:12:54 -07003714
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003715 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003716 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003717 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003718 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003719 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003720 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003721 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3722 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003723 }
Evan Millarc64edde2009-04-18 12:26:32 -07003724 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003725 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003726 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003727 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3728 // Not doing the regular wake lock formatting to remain compatible
3729 // with the old checkin format.
3730 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3731 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003732 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003733 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003734 }
3735 }
Evan Millarc64edde2009-04-18 12:26:32 -07003736 }
Bookatzc8c44962017-05-11 12:12:54 -07003737
Bookatz50df7112017-08-04 14:53:26 -07003738 final Map<String, ? extends Timer> rpmStats = getRpmStats();
3739 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
3740 if (rpmStats.size() > 0) {
3741 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
3742 sb.setLength(0);
3743 Timer totalTimer = ent.getValue();
3744 long timeMs = (totalTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3745 int count = totalTimer.getCountLocked(which);
3746 Timer screenOffTimer = screenOffRpmStats.get(ent.getKey());
3747 long screenOffTimeMs = screenOffTimer != null
3748 ? (screenOffTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3749 int screenOffCount = screenOffTimer != null
3750 ? screenOffTimer.getCountLocked(which) : 0;
Bookatz82b341172017-09-07 19:06:08 -07003751 if (SCREEN_OFF_RPM_STATS_ENABLED) {
3752 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3753 "\"" + ent.getKey() + "\"", timeMs, count, screenOffTimeMs,
3754 screenOffCount);
3755 } else {
3756 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3757 "\"" + ent.getKey() + "\"", timeMs, count);
3758 }
Bookatz50df7112017-08-04 14:53:26 -07003759 }
3760 }
3761
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003762 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003763 helper.create(this);
3764 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003765 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003766 if (sippers != null && sippers.size() > 0) {
3767 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3768 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003769 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003770 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3771 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
Kweku Adams87b19ec2017-10-09 12:40:03 -07003772 int uid = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003773 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003774 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003775 String label;
3776 switch (bs.drainType) {
3777 case IDLE:
3778 label="idle";
3779 break;
3780 case CELL:
3781 label="cell";
3782 break;
3783 case PHONE:
3784 label="phone";
3785 break;
3786 case WIFI:
3787 label="wifi";
3788 break;
3789 case BLUETOOTH:
3790 label="blue";
3791 break;
3792 case SCREEN:
3793 label="scrn";
3794 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003795 case FLASHLIGHT:
3796 label="flashlight";
3797 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003798 case APP:
3799 uid = bs.uidObj.getUid();
3800 label = "uid";
3801 break;
3802 case USER:
3803 uid = UserHandle.getUid(bs.userId, 0);
3804 label = "user";
3805 break;
3806 case UNACCOUNTED:
3807 label = "unacc";
3808 break;
3809 case OVERCOUNTED:
3810 label = "over";
3811 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003812 case CAMERA:
3813 label = "camera";
3814 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003815 case MEMORY:
3816 label = "memory";
3817 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003818 default:
3819 label = "???";
3820 }
3821 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07003822 BatteryStatsHelper.makemAh(bs.totalPowerMah),
3823 bs.shouldHide ? 1 : 0,
3824 BatteryStatsHelper.makemAh(bs.screenPowerMah),
3825 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003826 }
3827 }
3828
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003829 final long[] cpuFreqs = getCpuFreqs();
3830 if (cpuFreqs != null) {
3831 sb.setLength(0);
3832 for (int i = 0; i < cpuFreqs.length; ++i) {
3833 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3834 }
3835 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3836 }
3837
Kweku Adams87b19ec2017-10-09 12:40:03 -07003838 // Dump stats per UID.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839 for (int iu = 0; iu < NU; iu++) {
3840 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003841 if (reqUid >= 0 && uid != reqUid) {
3842 continue;
3843 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003844 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003846 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003847 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3848 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3849 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3850 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3851 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3852 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3853 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3854 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003855 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003856 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3857 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003858 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003859 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3860 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003861 // Background data transfers
3862 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3863 which);
3864 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3865 which);
3866 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3867 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3868 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3869 which);
3870 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3871 which);
3872 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3873 which);
3874 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3875 which);
3876
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003877 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3878 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003879 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003880 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3881 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3882 || wifiBytesBgTx > 0
3883 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3884 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003885 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3886 wifiBytesRx, wifiBytesTx,
3887 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003888 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003889 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003890 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3891 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3892 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3893 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003894 }
3895
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003896 // Dump modem controller data, per UID.
3897 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3898 u.getModemControllerActivity(), which);
3899
3900 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003901 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3902 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3903 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003904 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3905 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003906 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3907 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3908 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003909 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003910 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003911 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3912 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003913 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3914 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003915 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003916 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003918
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003919 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3920 u.getWifiControllerActivity(), which);
3921
Bookatz867c0d72017-03-07 18:23:42 -08003922 final Timer bleTimer = u.getBluetoothScanTimer();
3923 if (bleTimer != null) {
3924 // Convert from microseconds to milliseconds with rounding
3925 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3926 / 1000;
3927 if (totalTime != 0) {
3928 final int count = bleTimer.getCountLocked(which);
3929 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3930 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003931 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3932 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3933 final long actualTimeBg = bleTimerBg != null ?
3934 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003935 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003936 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3937 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003938 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3939 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3940 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3941 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3942 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
3943 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3944 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
3945 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3946 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
3947 final Timer unoptimizedScanTimerBg =
3948 u.getBluetoothUnoptimizedScanBackgroundTimer();
3949 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
3950 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3951 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
3952 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3953
Bookatz867c0d72017-03-07 18:23:42 -08003954 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07003955 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
3956 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
3957 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08003958 }
3959 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003960
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003961 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3962 u.getBluetoothControllerActivity(), which);
3963
Dianne Hackborn617f8772009-03-31 15:04:46 -07003964 if (u.hasUserActivity()) {
3965 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3966 boolean hasData = false;
3967 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3968 int val = u.getUserActivityCount(i, which);
3969 args[i] = val;
3970 if (val != 0) hasData = true;
3971 }
3972 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003973 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003974 }
3975 }
Bookatzc8c44962017-05-11 12:12:54 -07003976
3977 if (u.getAggregatedPartialWakelockTimer() != null) {
3978 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07003979 // Times are since reset (regardless of 'which')
3980 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07003981 final Timer bgTimer = timer.getSubTimer();
3982 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003983 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07003984 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
3985 }
3986
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003987 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3988 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3989 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3990 String linePrefix = "";
3991 sb.setLength(0);
3992 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3993 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003994 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3995 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003996 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003997 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
3998 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003999 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
4000 rawRealtime, "w", which, linePrefix);
4001
Kweku Adams103351f2017-10-16 14:39:34 -07004002 // Only log if we had at least one wakelock...
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004003 if (sb.length() > 0) {
4004 String name = wakelocks.keyAt(iw);
4005 if (name.indexOf(',') >= 0) {
4006 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004007 }
Yi Jin02483362017-08-04 11:30:44 -07004008 if (name.indexOf('\n') >= 0) {
4009 name = name.replace('\n', '_');
4010 }
4011 if (name.indexOf('\r') >= 0) {
4012 name = name.replace('\r', '_');
4013 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004014 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004015 }
4016 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004017
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004018 // WiFi Multicast Wakelock Statistics
4019 final Timer mcTimer = u.getMulticastWakelockStats();
4020 if (mcTimer != null) {
4021 final long totalMcWakelockTimeMs =
4022 mcTimer.getTotalTimeLocked(rawRealtime, which) / 1000 ;
4023 final int countMcWakelock = mcTimer.getCountLocked(which);
4024 if(totalMcWakelockTimeMs > 0) {
4025 dumpLine(pw, uid, category, WIFI_MULTICAST_DATA,
4026 totalMcWakelockTimeMs, countMcWakelock);
4027 }
4028 }
4029
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004030 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4031 for (int isy=syncs.size()-1; isy>=0; isy--) {
4032 final Timer timer = syncs.valueAt(isy);
4033 // Convert from microseconds to milliseconds with rounding
4034 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4035 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07004036 final Timer bgTimer = timer.getSubTimer();
4037 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004038 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07004039 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004040 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004041 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07004042 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004043 }
4044 }
4045
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004046 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4047 for (int ij=jobs.size()-1; ij>=0; ij--) {
4048 final Timer timer = jobs.valueAt(ij);
4049 // Convert from microseconds to milliseconds with rounding
4050 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4051 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07004052 final Timer bgTimer = timer.getSubTimer();
4053 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004054 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07004055 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004056 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004057 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07004058 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004059 }
4060 }
4061
Dianne Hackborn94326cb2017-06-28 16:17:20 -07004062 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
4063 for (int ic=completions.size()-1; ic>=0; ic--) {
4064 SparseIntArray types = completions.valueAt(ic);
4065 if (types != null) {
4066 dumpLine(pw, uid, category, JOB_COMPLETION_DATA,
4067 "\"" + completions.keyAt(ic) + "\"",
4068 types.get(JobParameters.REASON_CANCELED, 0),
4069 types.get(JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, 0),
4070 types.get(JobParameters.REASON_PREEMPT, 0),
4071 types.get(JobParameters.REASON_TIMEOUT, 0),
4072 types.get(JobParameters.REASON_DEVICE_IDLE, 0));
4073 }
4074 }
4075
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004076 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
4077 rawRealtime, which);
4078 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
4079 rawRealtime, which);
4080 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
4081 rawRealtime, which);
4082 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
4083 rawRealtime, which);
4084
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004085 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4086 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004087 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004088 final Uid.Sensor se = sensors.valueAt(ise);
4089 final int sensorNumber = sensors.keyAt(ise);
4090 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004091 if (timer != null) {
4092 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004093 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4094 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004095 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004096 final int count = timer.getCountLocked(which);
4097 final Timer bgTimer = se.getSensorBackgroundTime();
4098 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004099 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4100 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4101 final long bgActualTime = bgTimer != null ?
4102 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4103 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
4104 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 }
4107 }
4108
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004109 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
4110 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004111
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07004112 dumpTimer(pw, uid, category, FOREGROUND_ACTIVITY_DATA, u.getForegroundActivityTimer(),
4113 rawRealtime, which);
4114
4115 dumpTimer(pw, uid, category, FOREGROUND_SERVICE_DATA, u.getForegroundServiceTimer(),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004116 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004117
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004118 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07004119 long totalStateTime = 0;
4120 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08004121 final long time = u.getProcessStateTime(ips, rawRealtime, which);
4122 totalStateTime += time;
4123 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004124 }
4125 if (totalStateTime > 0) {
4126 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
4127 }
4128
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004129 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4130 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004131 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004132 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004133 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004134 }
4135
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004136 // If the cpuFreqs is null, then don't bother checking for cpu freq times.
4137 if (cpuFreqs != null) {
4138 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
4139 // If total cpuFreqTimes is null, then we don't need to check for
4140 // screenOffCpuFreqTimes.
4141 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
4142 sb.setLength(0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004143 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004144 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004145 }
Sudheer Shankaa87245d2017-08-10 12:02:31 -07004146 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
4147 if (screenOffCpuFreqTimeMs != null) {
4148 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
4149 sb.append("," + screenOffCpuFreqTimeMs[i]);
4150 }
4151 } else {
4152 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
4153 sb.append(",0");
4154 }
4155 }
4156 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
4157 cpuFreqTimeMs.length, sb.toString());
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004158 }
Sudheer Shankab2f83c12017-11-13 19:25:01 -08004159
4160 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
4161 final long[] timesMs = u.getCpuFreqTimes(which, procState);
4162 if (timesMs != null && timesMs.length == cpuFreqs.length) {
4163 sb.setLength(0);
4164 for (int i = 0; i < timesMs.length; ++i) {
4165 sb.append((i == 0 ? "" : ",") + timesMs[i]);
4166 }
4167 final long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(
4168 which, procState);
4169 if (screenOffTimesMs != null) {
4170 for (int i = 0; i < screenOffTimesMs.length; ++i) {
4171 sb.append("," + screenOffTimesMs[i]);
4172 }
4173 } else {
4174 for (int i = 0; i < timesMs.length; ++i) {
4175 sb.append(",0");
4176 }
4177 }
4178 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA,
4179 Uid.UID_PROCESS_TYPES[procState], timesMs.length, sb.toString());
4180 }
4181 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004182 }
4183
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004184 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4185 = u.getProcessStats();
4186 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4187 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004188
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004189 final long userMillis = ps.getUserTime(which);
4190 final long systemMillis = ps.getSystemTime(which);
4191 final long foregroundMillis = ps.getForegroundTime(which);
4192 final int starts = ps.getStarts(which);
4193 final int numCrashes = ps.getNumCrashes(which);
4194 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004195
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004196 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
4197 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08004198 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
4199 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004200 }
4201 }
4202
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004203 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4204 = u.getPackageStats();
4205 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4206 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4207 int wakeups = 0;
4208 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4209 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07004210 int count = alarms.valueAt(iwa).getCountLocked(which);
4211 wakeups += count;
4212 String name = alarms.keyAt(iwa).replace(',', '_');
4213 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004214 }
4215 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4216 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4217 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4218 final long startTime = ss.getStartTime(batteryUptime, which);
4219 final int starts = ss.getStarts(which);
4220 final int launches = ss.getLaunches(which);
4221 if (startTime != 0 || starts != 0 || launches != 0) {
4222 dumpLine(pw, uid, category, APK_DATA,
4223 wakeups, // wakeup alarms
4224 packageStats.keyAt(ipkg), // Apk
4225 serviceStats.keyAt(isvc), // service
4226 startTime / 1000, // time spent started, in ms
4227 starts,
4228 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004229 }
4230 }
4231 }
4232 }
4233 }
4234
Dianne Hackborn81038902012-11-26 17:04:09 -08004235 static final class TimerEntry {
4236 final String mName;
4237 final int mId;
4238 final BatteryStats.Timer mTimer;
4239 final long mTime;
4240 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
4241 mName = name;
4242 mId = id;
4243 mTimer = timer;
4244 mTime = time;
4245 }
4246 }
4247
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004248 private void printmAh(PrintWriter printer, double power) {
4249 printer.print(BatteryStatsHelper.makemAh(power));
4250 }
4251
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004252 private void printmAh(StringBuilder sb, double power) {
4253 sb.append(BatteryStatsHelper.makemAh(power));
4254 }
4255
Dianne Hackbornd953c532014-08-16 18:17:38 -07004256 /**
4257 * Temporary for settings.
4258 */
4259 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
4260 int reqUid) {
4261 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
4262 }
4263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004264 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004265 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07004266 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267 final long rawUptime = SystemClock.uptimeMillis() * 1000;
4268 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07004269 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004270 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004271
4272 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
4273 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
4274 final long totalRealtime = computeRealtime(rawRealtime, which);
4275 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004276 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
4277 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
4278 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004279 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
4280 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -07004281 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004282
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004283 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07004284
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004285 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07004286 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004287
Adam Lesinskif9b20a92016-06-17 17:30:01 -07004288 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
4289 if (estimatedBatteryCapacity > 0) {
4290 sb.setLength(0);
4291 sb.append(prefix);
4292 sb.append(" Estimated battery capacity: ");
4293 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
4294 sb.append(" mAh");
4295 pw.println(sb.toString());
4296 }
4297
Jocelyn Dangc627d102017-04-14 13:15:14 -07004298 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
4299 if (minLearnedBatteryCapacity > 0) {
4300 sb.setLength(0);
4301 sb.append(prefix);
4302 sb.append(" Min learned battery capacity: ");
4303 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
4304 sb.append(" mAh");
4305 pw.println(sb.toString());
4306 }
4307 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
4308 if (maxLearnedBatteryCapacity > 0) {
4309 sb.setLength(0);
4310 sb.append(prefix);
4311 sb.append(" Max learned battery capacity: ");
4312 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
4313 sb.append(" mAh");
4314 pw.println(sb.toString());
4315 }
4316
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004317 sb.setLength(0);
4318 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004319 sb.append(" Time on battery: ");
4320 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
4321 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
4322 sb.append(") realtime, ");
4323 formatTimeMs(sb, whichBatteryUptime / 1000);
4324 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, whichBatteryRealtime));
4325 sb.append(") uptime");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004326 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004327
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004328 sb.setLength(0);
4329 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004330 sb.append(" Time on battery screen off: ");
4331 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
4332 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, whichBatteryRealtime));
4333 sb.append(") realtime, ");
4334 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
4335 sb.append("(");
4336 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, whichBatteryRealtime));
4337 sb.append(") uptime");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004338 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004339
4340 sb.setLength(0);
4341 sb.append(prefix);
4342 sb.append(" Time on battery screen doze: ");
4343 formatTimeMs(sb, screenDozeTime / 1000); sb.append("(");
4344 sb.append(formatRatioLocked(screenDozeTime, whichBatteryRealtime));
4345 sb.append(")");
4346 pw.println(sb.toString());
4347
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004348 sb.setLength(0);
4349 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004350 sb.append(" Total run time: ");
4351 formatTimeMs(sb, totalRealtime / 1000);
4352 sb.append("realtime, ");
4353 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004354 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07004355 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004356 if (batteryTimeRemaining >= 0) {
4357 sb.setLength(0);
4358 sb.append(prefix);
4359 sb.append(" Battery time remaining: ");
4360 formatTimeMs(sb, batteryTimeRemaining / 1000);
4361 pw.println(sb.toString());
4362 }
4363 if (chargeTimeRemaining >= 0) {
4364 sb.setLength(0);
4365 sb.append(prefix);
4366 sb.append(" Charge time remaining: ");
4367 formatTimeMs(sb, chargeTimeRemaining / 1000);
4368 pw.println(sb.toString());
4369 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004370
Kweku Adams87b19ec2017-10-09 12:40:03 -07004371 final long dischargeCount = getUahDischarge(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004372 if (dischargeCount >= 0) {
4373 sb.setLength(0);
4374 sb.append(prefix);
4375 sb.append(" Discharge: ");
4376 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
4377 sb.append(" mAh");
4378 pw.println(sb.toString());
4379 }
4380
Kweku Adams87b19ec2017-10-09 12:40:03 -07004381 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004382 if (dischargeScreenOffCount >= 0) {
4383 sb.setLength(0);
4384 sb.append(prefix);
4385 sb.append(" Screen off discharge: ");
4386 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
4387 sb.append(" mAh");
4388 pw.println(sb.toString());
4389 }
4390
Kweku Adams87b19ec2017-10-09 12:40:03 -07004391 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Mac2f518a2017-09-19 16:06:03 -07004392 if (dischargeScreenDozeCount >= 0) {
4393 sb.setLength(0);
4394 sb.append(prefix);
4395 sb.append(" Screen doze discharge: ");
4396 sb.append(BatteryStatsHelper.makemAh(dischargeScreenDozeCount / 1000.0));
4397 sb.append(" mAh");
4398 pw.println(sb.toString());
4399 }
4400
4401 final long dischargeScreenOnCount =
4402 dischargeCount - dischargeScreenOffCount - dischargeScreenDozeCount;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004403 if (dischargeScreenOnCount >= 0) {
4404 sb.setLength(0);
4405 sb.append(prefix);
4406 sb.append(" Screen on discharge: ");
4407 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
4408 sb.append(" mAh");
4409 pw.println(sb.toString());
4410 }
4411
Mike Ma15313c92017-11-15 17:58:21 -08004412 final long dischargeLightDozeCount = getUahDischargeLightDoze(which);
4413 if (dischargeLightDozeCount >= 0) {
4414 sb.setLength(0);
4415 sb.append(prefix);
4416 sb.append(" Device light doze discharge: ");
4417 sb.append(BatteryStatsHelper.makemAh(dischargeLightDozeCount / 1000.0));
4418 sb.append(" mAh");
4419 pw.println(sb.toString());
4420 }
4421
4422 final long dischargeDeepDozeCount = getUahDischargeDeepDoze(which);
4423 if (dischargeDeepDozeCount >= 0) {
4424 sb.setLength(0);
4425 sb.append(prefix);
4426 sb.append(" Device deep doze discharge: ");
4427 sb.append(BatteryStatsHelper.makemAh(dischargeDeepDozeCount / 1000.0));
4428 sb.append(" mAh");
4429 pw.println(sb.toString());
4430 }
4431
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004432 pw.print(" Start clock time: ");
4433 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
4434
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004435 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004436 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004437 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004438 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
4439 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004440 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004441 rawRealtime, which);
4442 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
4443 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004444 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004445 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004446 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
4447 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
4448 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004449 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004450 sb.append(prefix);
4451 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
4452 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004453 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004454 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
4455 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004456 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004457 pw.println(sb.toString());
4458 sb.setLength(0);
4459 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004460 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004461 boolean didOne = false;
4462 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004463 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004464 if (time == 0) {
4465 continue;
4466 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004467 sb.append("\n ");
4468 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004469 didOne = true;
4470 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
4471 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004472 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004473 sb.append("(");
4474 sb.append(formatRatioLocked(time, screenOnTime));
4475 sb.append(")");
4476 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004477 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004478 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004479 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004480 sb.setLength(0);
4481 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004482 sb.append(" Power save mode enabled: ");
4483 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004484 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004485 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004486 sb.append(")");
4487 pw.println(sb.toString());
4488 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004489 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004490 sb.setLength(0);
4491 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004492 sb.append(" Device light idling: ");
4493 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004494 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004495 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
4496 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004497 sb.append("x");
4498 pw.println(sb.toString());
4499 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004500 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004501 sb.setLength(0);
4502 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004503 sb.append(" Idle mode light time: ");
4504 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004505 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004506 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
4507 sb.append(") ");
4508 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004509 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004510 sb.append(" -- longest ");
4511 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
4512 pw.println(sb.toString());
4513 }
4514 if (deviceIdlingTime != 0) {
4515 sb.setLength(0);
4516 sb.append(prefix);
4517 sb.append(" Device full idling: ");
4518 formatTimeMs(sb, deviceIdlingTime / 1000);
4519 sb.append("(");
4520 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004521 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004522 sb.append("x");
4523 pw.println(sb.toString());
4524 }
4525 if (deviceIdleModeFullTime != 0) {
4526 sb.setLength(0);
4527 sb.append(prefix);
4528 sb.append(" Idle mode full time: ");
4529 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
4530 sb.append("(");
4531 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
4532 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004533 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004534 sb.append("x");
4535 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004536 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004537 pw.println(sb.toString());
4538 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004539 if (phoneOnTime != 0) {
4540 sb.setLength(0);
4541 sb.append(prefix);
4542 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
4543 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004544 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004545 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004546 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004547 if (connChanges != 0) {
4548 pw.print(prefix);
4549 pw.print(" Connectivity changes: "); pw.println(connChanges);
4550 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004551
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004552 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07004553 long fullWakeLockTimeTotalMicros = 0;
4554 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08004555
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004556 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08004557
Evan Millar22ac0432009-03-31 11:33:18 -07004558 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004559 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004560
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004561 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4562 = u.getWakelockStats();
4563 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4564 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07004565
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004566 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
4567 if (fullWakeTimer != null) {
4568 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
4569 rawRealtime, which);
4570 }
4571
4572 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4573 if (partialWakeTimer != null) {
4574 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
4575 rawRealtime, which);
4576 if (totalTimeMicros > 0) {
4577 if (reqUid < 0) {
4578 // Only show the ordered list of all wake
4579 // locks if the caller is not asking for data
4580 // about a specific uid.
4581 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
4582 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08004583 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004584 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07004585 }
4586 }
4587 }
4588 }
Bookatzc8c44962017-05-11 12:12:54 -07004589
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004590 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4591 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4592 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4593 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4594 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4595 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4596 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4597 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004598 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4599 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004600
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004601 if (fullWakeLockTimeTotalMicros != 0) {
4602 sb.setLength(0);
4603 sb.append(prefix);
4604 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4605 (fullWakeLockTimeTotalMicros + 500) / 1000);
4606 pw.println(sb.toString());
4607 }
4608
4609 if (partialWakeLockTimeTotalMicros != 0) {
4610 sb.setLength(0);
4611 sb.append(prefix);
4612 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4613 (partialWakeLockTimeTotalMicros + 500) / 1000);
4614 pw.println(sb.toString());
4615 }
4616
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08004617 final long multicastWakeLockTimeTotalMicros =
4618 getWifiMulticastWakelockTime(rawRealtime, which);
4619 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07004620 if (multicastWakeLockTimeTotalMicros != 0) {
4621 sb.setLength(0);
4622 sb.append(prefix);
4623 sb.append(" Total WiFi Multicast wakelock Count: ");
4624 sb.append(multicastWakeLockCountTotal);
4625 pw.println(sb.toString());
4626
4627 sb.setLength(0);
4628 sb.append(prefix);
4629 sb.append(" Total WiFi Multicast wakelock time: ");
4630 formatTimeMsNoSpace(sb, (multicastWakeLockTimeTotalMicros + 500) / 1000);
4631 pw.println(sb.toString());
4632 }
4633
Siddharth Ray3c648c42017-10-02 17:30:58 -07004634 pw.println("");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004635 pw.print(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004636 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004637 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004638 sb.append(" CONNECTIVITY POWER SUMMARY START");
4639 pw.println(sb.toString());
4640
4641 pw.print(prefix);
4642 sb.setLength(0);
4643 sb.append(prefix);
4644 sb.append(" Logging duration for connectivity statistics: ");
4645 formatTimeMs(sb, whichBatteryRealtime / 1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004646 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004647
4648 sb.setLength(0);
4649 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004650 sb.append(" Cellular Statistics:");
Amith Yamasanif37447b2009-10-08 18:28:01 -07004651 pw.println(sb.toString());
4652
Siddharth Ray3c648c42017-10-02 17:30:58 -07004653 pw.print(prefix);
4654 sb.setLength(0);
4655 sb.append(prefix);
4656 sb.append(" Cellular kernel active time: ");
4657 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
4658 formatTimeMs(sb, mobileActiveTime / 1000);
4659 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4660 sb.append(")");
4661 pw.println(sb.toString());
4662
4663 pw.print(" Cellular data received: "); pw.println(formatBytesLocked(mobileRxTotalBytes));
4664 pw.print(" Cellular data sent: "); pw.println(formatBytesLocked(mobileTxTotalBytes));
4665 pw.print(" Cellular packets received: "); pw.println(mobileRxTotalPackets);
4666 pw.print(" Cellular packets sent: "); pw.println(mobileTxTotalPackets);
4667
Dianne Hackborn627bba72009-03-24 22:32:56 -07004668 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004669 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004670 sb.append(" Cellular Radio Access Technology:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004671 didOne = false;
4672 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004673 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004674 if (time == 0) {
4675 continue;
4676 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004677 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004678 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004679 didOne = true;
4680 sb.append(DATA_CONNECTION_NAMES[i]);
4681 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004682 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004683 sb.append("(");
4684 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004685 sb.append(") ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004686 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004687 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004688 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004689
4690 sb.setLength(0);
4691 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004692 sb.append(" Cellular Rx signal strength (RSRP):");
4693 final String[] cellularRxSignalStrengthDescription = new String[]{
4694 "very poor (less than -128dBm): ",
4695 "poor (-128dBm to -118dBm): ",
4696 "moderate (-118dBm to -108dBm): ",
4697 "good (-108dBm to -98dBm): ",
4698 "great (greater than -98dBm): "};
4699 didOne = false;
4700 final int numCellularRxBins = Math.min(SignalStrength.NUM_SIGNAL_STRENGTH_BINS,
4701 cellularRxSignalStrengthDescription.length);
4702 for (int i=0; i<numCellularRxBins; i++) {
4703 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
4704 if (time == 0) {
4705 continue;
4706 }
4707 sb.append("\n ");
4708 sb.append(prefix);
4709 didOne = true;
4710 sb.append(cellularRxSignalStrengthDescription[i]);
4711 sb.append(" ");
4712 formatTimeMs(sb, time/1000);
4713 sb.append("(");
4714 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4715 sb.append(") ");
4716 }
4717 if (!didOne) sb.append(" (no activity)");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004718 pw.println(sb.toString());
4719
Siddharth Rayb50a6842017-12-14 15:15:28 -08004720 printControllerActivity(pw, sb, prefix, CELLULAR_CONTROLLER_NAME,
Siddharth Ray3c648c42017-10-02 17:30:58 -07004721 getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004722
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004723 pw.print(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004724 sb.setLength(0);
4725 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004726 sb.append(" Wifi Statistics:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004727 pw.println(sb.toString());
4728
Siddharth Rayb50a6842017-12-14 15:15:28 -08004729 pw.print(prefix);
4730 sb.setLength(0);
4731 sb.append(prefix);
4732 sb.append(" Wifi kernel active time: ");
4733 final long wifiActiveTime = getWifiActiveTime(rawRealtime, which);
4734 formatTimeMs(sb, wifiActiveTime / 1000);
4735 sb.append("("); sb.append(formatRatioLocked(wifiActiveTime, whichBatteryRealtime));
4736 sb.append(")");
4737 pw.println(sb.toString());
4738
Siddharth Ray3c648c42017-10-02 17:30:58 -07004739 pw.print(" Wifi data received: "); pw.println(formatBytesLocked(wifiRxTotalBytes));
4740 pw.print(" Wifi data sent: "); pw.println(formatBytesLocked(wifiTxTotalBytes));
4741 pw.print(" Wifi packets received: "); pw.println(wifiRxTotalPackets);
4742 pw.print(" Wifi packets sent: "); pw.println(wifiTxTotalPackets);
4743
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004744 sb.setLength(0);
4745 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004746 sb.append(" Wifi states:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004747 didOne = false;
4748 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004749 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004750 if (time == 0) {
4751 continue;
4752 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004753 sb.append("\n ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004754 didOne = true;
4755 sb.append(WIFI_STATE_NAMES[i]);
4756 sb.append(" ");
4757 formatTimeMs(sb, time/1000);
4758 sb.append("(");
4759 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4760 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004761 }
4762 if (!didOne) sb.append(" (no activity)");
4763 pw.println(sb.toString());
4764
4765 sb.setLength(0);
4766 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004767 sb.append(" Wifi supplicant states:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004768 didOne = false;
4769 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4770 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4771 if (time == 0) {
4772 continue;
4773 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004774 sb.append("\n ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004775 didOne = true;
4776 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4777 sb.append(" ");
4778 formatTimeMs(sb, time/1000);
4779 sb.append("(");
4780 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4781 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004782 }
4783 if (!didOne) sb.append(" (no activity)");
4784 pw.println(sb.toString());
4785
4786 sb.setLength(0);
4787 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004788 sb.append(" Wifi Rx signal strength (RSSI):");
4789 final String[] wifiRxSignalStrengthDescription = new String[]{
4790 "very poor (less than -88.75dBm): ",
4791 "poor (-88.75 to -77.5dBm): ",
4792 "moderate (-77.5dBm to -66.25dBm): ",
4793 "good (-66.25dBm to -55dBm): ",
4794 "great (greater than -55dBm): "};
Dianne Hackborn3251b902014-06-20 14:40:53 -07004795 didOne = false;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004796 final int numWifiRxBins = Math.min(NUM_WIFI_SIGNAL_STRENGTH_BINS,
4797 wifiRxSignalStrengthDescription.length);
4798 for (int i=0; i<numWifiRxBins; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004799 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4800 if (time == 0) {
4801 continue;
4802 }
4803 sb.append("\n ");
4804 sb.append(prefix);
4805 didOne = true;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004806 sb.append(" ");
4807 sb.append(wifiRxSignalStrengthDescription[i]);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004808 formatTimeMs(sb, time/1000);
4809 sb.append("(");
4810 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4811 sb.append(") ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004812 }
4813 if (!didOne) sb.append(" (no activity)");
4814 pw.println(sb.toString());
4815
Siddharth Rayb50a6842017-12-14 15:15:28 -08004816 printControllerActivity(pw, sb, prefix, WIFI_CONTROLLER_NAME,
4817 getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004818
Adam Lesinski50e47602015-12-04 17:04:54 -08004819 pw.print(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004820 sb.setLength(0);
4821 sb.append(prefix);
Siddharth Ray78ccaf52017-12-23 16:16:21 -08004822 sb.append(" GPS Statistics:");
4823 pw.println(sb.toString());
4824
4825 sb.setLength(0);
4826 sb.append(prefix);
4827 sb.append(" GPS signal quality (Top 4 Average CN0):");
4828 final String[] gpsSignalQualityDescription = new String[]{
4829 "poor (less than 20 dBHz): ",
4830 "good (greater than 20 dBHz): "};
4831 final int numGpsSignalQualityBins = Math.min(GnssMetrics.NUM_GPS_SIGNAL_QUALITY_LEVELS,
4832 gpsSignalQualityDescription.length);
4833 for (int i=0; i<numGpsSignalQualityBins; i++) {
4834 final long time = getGpsSignalQualityTime(i, rawRealtime, which);
4835 sb.append("\n ");
4836 sb.append(prefix);
4837 sb.append(" ");
4838 sb.append(gpsSignalQualityDescription[i]);
4839 formatTimeMs(sb, time/1000);
4840 sb.append("(");
4841 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4842 sb.append(") ");
4843 }
4844 pw.println(sb.toString());
4845
4846 final long gpsBatteryDrainMaMs = getGpsBatteryDrainMaMs();
4847 if (gpsBatteryDrainMaMs > 0) {
4848 pw.print(prefix);
4849 sb.setLength(0);
4850 sb.append(prefix);
4851 sb.append(" Battery Drain (mAh): ");
4852 sb.append(Double.toString(((double) gpsBatteryDrainMaMs)/(3600 * 1000)));
4853 pw.println(sb.toString());
4854 }
4855
4856 pw.print(prefix);
4857 sb.setLength(0);
4858 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004859 sb.append(" CONNECTIVITY POWER SUMMARY END");
4860 pw.println(sb.toString());
4861 pw.println("");
4862
4863 pw.print(prefix);
Adam Lesinski50e47602015-12-04 17:04:54 -08004864 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4865 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4866
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004867 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4868 sb.setLength(0);
4869 sb.append(prefix);
4870 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4871 pw.println(sb.toString());
4872
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004873 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4874 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004875
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004876 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004877
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004878 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004879 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004880 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004881 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004882 pw.println(getDischargeStartLevel());
4883 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4884 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004885 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004886 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004887 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004888 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004889 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004890 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004891 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004892 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004893 pw.println(getDischargeAmountScreenOn());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004894 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004895 pw.println(getDischargeAmountScreenOff());
4896 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4897 pw.println(getDischargeAmountScreenDoze());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004898 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004899 } else {
4900 pw.print(prefix); pw.println(" Device battery use since last full charge");
4901 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004902 pw.println(getLowDischargeAmountSinceCharge());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004903 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004904 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004905 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004906 pw.println(getDischargeAmountScreenOnSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004907 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004908 pw.println(getDischargeAmountScreenOffSinceCharge());
4909 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4910 pw.println(getDischargeAmountScreenDozeSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004911 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004912 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004913
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004914 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004915 helper.create(this);
4916 helper.refreshStats(which, UserHandle.USER_ALL);
4917 List<BatterySipper> sippers = helper.getUsageList();
4918 if (sippers != null && sippers.size() > 0) {
4919 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4920 pw.print(prefix); pw.print(" Capacity: ");
4921 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004922 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004923 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4924 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4925 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4926 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004927 pw.println();
4928 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004929 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004930 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004931 switch (bs.drainType) {
4932 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004933 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004934 break;
4935 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004936 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004937 break;
4938 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004939 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004940 break;
4941 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004942 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004943 break;
4944 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004945 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004946 break;
4947 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004948 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004949 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004950 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004951 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004952 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004953 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004954 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004955 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004956 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004957 break;
4958 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004959 pw.print(" User "); pw.print(bs.userId);
4960 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004961 break;
4962 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004963 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004964 break;
4965 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004966 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004967 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004968 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004969 pw.print(" Camera: ");
4970 break;
4971 default:
4972 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004973 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004974 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004975 printmAh(pw, bs.totalPowerMah);
4976
Adam Lesinski57123002015-06-12 16:12:07 -07004977 if (bs.usagePowerMah != bs.totalPowerMah) {
4978 // If the usage (generic power) isn't the whole amount, we list out
4979 // what components are involved in the calculation.
4980
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004981 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004982 if (bs.usagePowerMah != 0) {
4983 pw.print(" usage=");
4984 printmAh(pw, bs.usagePowerMah);
4985 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004986 if (bs.cpuPowerMah != 0) {
4987 pw.print(" cpu=");
4988 printmAh(pw, bs.cpuPowerMah);
4989 }
4990 if (bs.wakeLockPowerMah != 0) {
4991 pw.print(" wake=");
4992 printmAh(pw, bs.wakeLockPowerMah);
4993 }
4994 if (bs.mobileRadioPowerMah != 0) {
4995 pw.print(" radio=");
4996 printmAh(pw, bs.mobileRadioPowerMah);
4997 }
4998 if (bs.wifiPowerMah != 0) {
4999 pw.print(" wifi=");
5000 printmAh(pw, bs.wifiPowerMah);
5001 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005002 if (bs.bluetoothPowerMah != 0) {
5003 pw.print(" bt=");
5004 printmAh(pw, bs.bluetoothPowerMah);
5005 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005006 if (bs.gpsPowerMah != 0) {
5007 pw.print(" gps=");
5008 printmAh(pw, bs.gpsPowerMah);
5009 }
5010 if (bs.sensorPowerMah != 0) {
5011 pw.print(" sensor=");
5012 printmAh(pw, bs.sensorPowerMah);
5013 }
5014 if (bs.cameraPowerMah != 0) {
5015 pw.print(" camera=");
5016 printmAh(pw, bs.cameraPowerMah);
5017 }
5018 if (bs.flashlightPowerMah != 0) {
5019 pw.print(" flash=");
5020 printmAh(pw, bs.flashlightPowerMah);
5021 }
5022 pw.print(" )");
5023 }
Bookatz17d7d9d2017-06-08 14:50:46 -07005024
5025 // If there is additional smearing information, include it.
5026 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
5027 pw.print(" Including smearing: ");
5028 printmAh(pw, bs.totalSmearedPowerMah);
5029 pw.print(" (");
5030 if (bs.screenPowerMah != 0) {
5031 pw.print(" screen=");
5032 printmAh(pw, bs.screenPowerMah);
5033 }
5034 if (bs.proportionalSmearMah != 0) {
5035 pw.print(" proportional=");
5036 printmAh(pw, bs.proportionalSmearMah);
5037 }
5038 pw.print(" )");
5039 }
5040 if (bs.shouldHide) {
5041 pw.print(" Excluded from smearing");
5042 }
5043
Adam Lesinski628ef9c2015-06-10 13:08:57 -07005044 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005045 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08005046 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08005047 }
5048
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005049 sippers = helper.getMobilemsppList();
5050 if (sippers != null && sippers.size() > 0) {
5051 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005052 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005053 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005054 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005055 sb.setLength(0);
5056 sb.append(prefix); sb.append(" Uid ");
5057 UserHandle.formatUid(sb, bs.uidObj.getUid());
5058 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
5059 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
5060 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005061 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005062 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005063 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005064 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005065 sb.setLength(0);
5066 sb.append(prefix);
5067 sb.append(" TOTAL TIME: ");
5068 formatTimeMs(sb, totalTime);
5069 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
5070 sb.append(")");
5071 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005072 pw.println();
5073 }
5074
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005075 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
5076 @Override
5077 public int compare(TimerEntry lhs, TimerEntry rhs) {
5078 long lhsTime = lhs.mTime;
5079 long rhsTime = rhs.mTime;
5080 if (lhsTime < rhsTime) {
5081 return 1;
5082 }
5083 if (lhsTime > rhsTime) {
5084 return -1;
5085 }
5086 return 0;
5087 }
5088 };
5089
5090 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005091 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
5092 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005093 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005094 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
5095 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
5096 : kernelWakelocks.entrySet()) {
5097 final BatteryStats.Timer timer = ent.getValue();
5098 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005099 if (totalTimeMillis > 0) {
5100 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
5101 }
5102 }
5103 if (ktimers.size() > 0) {
5104 Collections.sort(ktimers, timerComparator);
5105 pw.print(prefix); pw.println(" All kernel wake locks:");
5106 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005107 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005108 String linePrefix = ": ";
5109 sb.setLength(0);
5110 sb.append(prefix);
5111 sb.append(" Kernel Wake lock ");
5112 sb.append(timer.mName);
5113 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
5114 which, linePrefix);
5115 if (!linePrefix.equals(": ")) {
5116 sb.append(" realtime");
5117 // Only print out wake locks that were held
5118 pw.println(sb.toString());
5119 }
5120 }
5121 pw.println();
5122 }
5123 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005124
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005125 if (timers.size() > 0) {
5126 Collections.sort(timers, timerComparator);
5127 pw.print(prefix); pw.println(" All partial wake locks:");
5128 for (int i=0; i<timers.size(); i++) {
5129 TimerEntry timer = timers.get(i);
5130 sb.setLength(0);
5131 sb.append(" Wake lock ");
5132 UserHandle.formatUid(sb, timer.mId);
5133 sb.append(" ");
5134 sb.append(timer.mName);
5135 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5136 sb.append(" realtime");
5137 pw.println(sb.toString());
5138 }
5139 timers.clear();
5140 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08005141 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005142
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005143 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005144 if (wakeupReasons.size() > 0) {
5145 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005146 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005147 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005148 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005149 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
5150 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005151 }
5152 Collections.sort(reasons, timerComparator);
5153 for (int i=0; i<reasons.size(); i++) {
5154 TimerEntry timer = reasons.get(i);
5155 String linePrefix = ": ";
5156 sb.setLength(0);
5157 sb.append(prefix);
5158 sb.append(" Wakeup reason ");
5159 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07005160 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
5161 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005162 pw.println(sb.toString());
5163 }
5164 pw.println();
5165 }
Dianne Hackborn81038902012-11-26 17:04:09 -08005166 }
Evan Millar22ac0432009-03-31 11:33:18 -07005167
James Carr2dd7e5e2016-07-20 18:48:39 -07005168 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
Bookatz50df7112017-08-04 14:53:26 -07005169 if (mMemoryStats.size() > 0) {
5170 pw.println(" Memory Stats");
5171 for (int i = 0; i < mMemoryStats.size(); i++) {
5172 sb.setLength(0);
5173 sb.append(" Bandwidth ");
5174 sb.append(mMemoryStats.keyAt(i));
5175 sb.append(" Time ");
5176 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
5177 pw.println(sb.toString());
5178 }
5179 pw.println();
5180 }
5181
5182 final Map<String, ? extends Timer> rpmStats = getRpmStats();
5183 if (rpmStats.size() > 0) {
5184 pw.print(prefix); pw.println(" Resource Power Manager Stats");
5185 if (rpmStats.size() > 0) {
5186 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
5187 final String timerName = ent.getKey();
5188 final Timer timer = ent.getValue();
5189 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5190 }
5191 }
5192 pw.println();
5193 }
Bookatz82b341172017-09-07 19:06:08 -07005194 if (SCREEN_OFF_RPM_STATS_ENABLED) {
5195 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
Bookatz50df7112017-08-04 14:53:26 -07005196 if (screenOffRpmStats.size() > 0) {
Bookatz82b341172017-09-07 19:06:08 -07005197 pw.print(prefix);
5198 pw.println(" Resource Power Manager Stats for when screen was off");
5199 if (screenOffRpmStats.size() > 0) {
5200 for (Map.Entry<String, ? extends Timer> ent : screenOffRpmStats.entrySet()) {
5201 final String timerName = ent.getKey();
5202 final Timer timer = ent.getValue();
5203 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
5204 }
Bookatz50df7112017-08-04 14:53:26 -07005205 }
Bookatz82b341172017-09-07 19:06:08 -07005206 pw.println();
Bookatz50df7112017-08-04 14:53:26 -07005207 }
James Carr2dd7e5e2016-07-20 18:48:39 -07005208 }
5209
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005210 final long[] cpuFreqs = getCpuFreqs();
5211 if (cpuFreqs != null) {
5212 sb.setLength(0);
Bookatz50df7112017-08-04 14:53:26 -07005213 sb.append(" CPU freqs:");
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005214 for (int i = 0; i < cpuFreqs.length; ++i) {
5215 sb.append(" " + cpuFreqs[i]);
5216 }
5217 pw.println(sb.toString());
Bookatz50df7112017-08-04 14:53:26 -07005218 pw.println();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005219 }
5220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005221 for (int iu=0; iu<NU; iu++) {
5222 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005223 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005224 continue;
5225 }
Bookatzc8c44962017-05-11 12:12:54 -07005226
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005227 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07005228
5229 pw.print(prefix);
5230 pw.print(" ");
5231 UserHandle.formatUid(pw, uid);
5232 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005233 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005234
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005235 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
5236 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
5237 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
5238 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005239 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
5240 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
5241
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005242 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
5243 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005244 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
5245 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08005246
5247 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
5248 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
5249
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005250 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
5251 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
5252 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08005253 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
5254 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5255 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
5256 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005257 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005258
Adam Lesinski5f056f62016-07-14 16:56:08 -07005259 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
5260 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
5261
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005262 if (mobileRxBytes > 0 || mobileTxBytes > 0
5263 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005264 pw.print(prefix); pw.print(" Mobile network: ");
5265 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005266 pw.print(formatBytesLocked(mobileTxBytes));
5267 pw.print(" sent (packets "); pw.print(mobileRxPackets);
5268 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005269 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005270 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
5271 sb.setLength(0);
5272 sb.append(prefix); sb.append(" Mobile radio active: ");
5273 formatTimeMs(sb, uidMobileActiveTime / 1000);
5274 sb.append("(");
5275 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
5276 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
5277 long packets = mobileRxPackets + mobileTxPackets;
5278 if (packets == 0) {
5279 packets = 1;
5280 }
5281 sb.append(" @ ");
5282 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
5283 sb.append(" mspp");
5284 pw.println(sb.toString());
5285 }
5286
Adam Lesinski5f056f62016-07-14 16:56:08 -07005287 if (mobileWakeup > 0) {
5288 sb.setLength(0);
5289 sb.append(prefix);
5290 sb.append(" Mobile radio AP wakeups: ");
5291 sb.append(mobileWakeup);
5292 pw.println(sb.toString());
5293 }
5294
Siddharth Rayb50a6842017-12-14 15:15:28 -08005295 printControllerActivityIfInteresting(pw, sb, prefix + " ",
5296 CELLULAR_CONTROLLER_NAME, u.getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005297
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005298 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005299 pw.print(prefix); pw.print(" Wi-Fi network: ");
5300 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005301 pw.print(formatBytesLocked(wifiTxBytes));
5302 pw.print(" sent (packets "); pw.print(wifiRxPackets);
5303 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005304 }
5305
Dianne Hackborn62793e42015-03-09 11:15:41 -07005306 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08005307 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005308 || uidWifiRunningTime != 0) {
5309 sb.setLength(0);
5310 sb.append(prefix); sb.append(" Wifi Running: ");
5311 formatTimeMs(sb, uidWifiRunningTime / 1000);
5312 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
5313 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07005314 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005315 formatTimeMs(sb, fullWifiLockOnTime / 1000);
5316 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
5317 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08005318 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005319 formatTimeMs(sb, wifiScanTime / 1000);
5320 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07005321 whichBatteryRealtime)); sb.append(") ");
5322 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08005323 sb.append("x\n");
5324 // actual and background times are unpooled and since reset (regardless of 'which')
5325 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
5326 formatTimeMs(sb, wifiScanActualTime / 1000);
5327 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
5328 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5329 sb.append(") ");
5330 sb.append(wifiScanCount);
5331 sb.append("x\n");
5332 sb.append(prefix); sb.append(" Background Wifi Scan: ");
5333 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
5334 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
5335 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5336 sb.append(") ");
5337 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07005338 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005339 pw.println(sb.toString());
5340 }
5341
Adam Lesinski5f056f62016-07-14 16:56:08 -07005342 if (wifiWakeup > 0) {
5343 sb.setLength(0);
5344 sb.append(prefix);
5345 sb.append(" WiFi AP wakeups: ");
5346 sb.append(wifiWakeup);
5347 pw.println(sb.toString());
5348 }
5349
Siddharth Rayb50a6842017-12-14 15:15:28 -08005350 printControllerActivityIfInteresting(pw, sb, prefix + " ", WIFI_CONTROLLER_NAME,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005351 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07005352
Adam Lesinski50e47602015-12-04 17:04:54 -08005353 if (btRxBytes > 0 || btTxBytes > 0) {
5354 pw.print(prefix); pw.print(" Bluetooth network: ");
5355 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
5356 pw.print(formatBytesLocked(btTxBytes));
5357 pw.println(" sent");
5358 }
5359
Bookatz867c0d72017-03-07 18:23:42 -08005360 final Timer bleTimer = u.getBluetoothScanTimer();
5361 if (bleTimer != null) {
5362 // Convert from microseconds to milliseconds with rounding
5363 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
5364 / 1000;
5365 if (totalTimeMs != 0) {
5366 final int count = bleTimer.getCountLocked(which);
5367 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
5368 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005369 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5370 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
5371 final long actualTimeMsBg = bleTimerBg != null ?
5372 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005373 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07005374 final int resultCount = u.getBluetoothScanResultCounter() != null ?
5375 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005376 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
5377 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
5378 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
5379 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
5380 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
5381 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5382 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
5383 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
5384 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
5385 final Timer unoptimizedScanTimerBg =
5386 u.getBluetoothUnoptimizedScanBackgroundTimer();
5387 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
5388 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5389 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
5390 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005391
5392 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08005393 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07005394 sb.append(prefix);
5395 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08005396 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07005397 sb.append(" (");
5398 sb.append(count);
5399 sb.append(" times)");
5400 if (bleTimer.isRunningLocked()) {
5401 sb.append(" (currently running)");
5402 }
5403 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005404 }
Bookatzb1f04f32017-05-19 13:57:32 -07005405
5406 sb.append(prefix);
5407 sb.append(" Bluetooth Scan (total actual realtime): ");
5408 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
5409 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005410 sb.append(count);
5411 sb.append(" times)");
5412 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07005413 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08005414 }
Bookatzb1f04f32017-05-19 13:57:32 -07005415 sb.append("\n");
5416 if (actualTimeMsBg > 0 || countBg > 0) {
5417 sb.append(prefix);
5418 sb.append(" Bluetooth Scan (background realtime): ");
5419 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
5420 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005421 sb.append(countBg);
5422 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07005423 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
5424 sb.append(" (currently running in background)");
5425 }
5426 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005427 }
Bookatzb1f04f32017-05-19 13:57:32 -07005428
5429 sb.append(prefix);
5430 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07005431 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07005432 sb.append(" (");
5433 sb.append(resultCountBg);
5434 sb.append(" in background)");
5435
5436 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
5437 sb.append("\n");
5438 sb.append(prefix);
5439 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
5440 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
5441 sb.append(" (max ");
5442 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
5443 sb.append(")");
5444 if (unoptimizedScanTimer != null
5445 && unoptimizedScanTimer.isRunningLocked()) {
5446 sb.append(" (currently running unoptimized)");
5447 }
5448 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
5449 sb.append("\n");
5450 sb.append(prefix);
5451 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
5452 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
5453 sb.append(" (max ");
5454 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
5455 sb.append(")");
5456 if (unoptimizedScanTimerBg.isRunningLocked()) {
5457 sb.append(" (currently running unoptimized in background)");
5458 }
5459 }
5460 }
Bookatz867c0d72017-03-07 18:23:42 -08005461 pw.println(sb.toString());
5462 uidActivity = true;
5463 }
5464 }
5465
5466
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005467
Dianne Hackborn617f8772009-03-31 15:04:46 -07005468 if (u.hasUserActivity()) {
5469 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07005470 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005471 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005472 if (val != 0) {
5473 if (!hasData) {
5474 sb.setLength(0);
5475 sb.append(" User activity: ");
5476 hasData = true;
5477 } else {
5478 sb.append(", ");
5479 }
5480 sb.append(val);
5481 sb.append(" ");
5482 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
5483 }
5484 }
5485 if (hasData) {
5486 pw.println(sb.toString());
5487 }
5488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005489
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005490 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
5491 = u.getWakelockStats();
5492 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005493 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005494 int countWakelock = 0;
5495 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
5496 final Uid.Wakelock wl = wakelocks.valueAt(iw);
5497 String linePrefix = ": ";
5498 sb.setLength(0);
5499 sb.append(prefix);
5500 sb.append(" Wake lock ");
5501 sb.append(wakelocks.keyAt(iw));
5502 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
5503 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005504 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
5505 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005506 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005507 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
5508 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005509 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
5510 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005511 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
5512 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07005513 sb.append(" realtime");
5514 pw.println(sb.toString());
5515 uidActivity = true;
5516 countWakelock++;
5517
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005518 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
5519 rawRealtime, which);
5520 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
5521 rawRealtime, which);
5522 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
5523 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005524 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07005525 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005526 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005527 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07005528 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
5529 // pooled and therefore just a lower bound)
5530 long actualTotalPartialWakelock = 0;
5531 long actualBgPartialWakelock = 0;
5532 if (u.getAggregatedPartialWakelockTimer() != null) {
5533 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
5534 // Convert from microseconds to milliseconds with rounding
5535 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07005536 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07005537 final Timer bgAggTimer = aggTimer.getSubTimer();
5538 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005539 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07005540 }
5541
5542 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
5543 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
5544 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005545 sb.setLength(0);
5546 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005547 sb.append(" TOTAL wake: ");
5548 boolean needComma = false;
5549 if (totalFullWakelock != 0) {
5550 needComma = true;
5551 formatTimeMs(sb, totalFullWakelock);
5552 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005553 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005554 if (totalPartialWakelock != 0) {
5555 if (needComma) {
5556 sb.append(", ");
5557 }
5558 needComma = true;
5559 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07005560 sb.append("blamed partial");
5561 }
5562 if (actualTotalPartialWakelock != 0) {
5563 if (needComma) {
5564 sb.append(", ");
5565 }
5566 needComma = true;
5567 formatTimeMs(sb, actualTotalPartialWakelock);
5568 sb.append("actual partial");
5569 }
5570 if (actualBgPartialWakelock != 0) {
5571 if (needComma) {
5572 sb.append(", ");
5573 }
5574 needComma = true;
5575 formatTimeMs(sb, actualBgPartialWakelock);
5576 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005577 }
5578 if (totalWindowWakelock != 0) {
5579 if (needComma) {
5580 sb.append(", ");
5581 }
5582 needComma = true;
5583 formatTimeMs(sb, totalWindowWakelock);
5584 sb.append("window");
5585 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005586 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07005587 if (needComma) {
5588 sb.append(",");
5589 }
5590 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005591 formatTimeMs(sb, totalDrawWakelock);
5592 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07005593 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005594 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005595 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005596 }
5597 }
5598
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07005599 // Calculate multicast wakelock stats
5600 final Timer mcTimer = u.getMulticastWakelockStats();
5601 if (mcTimer != null) {
5602 final long multicastWakeLockTimeMicros = mcTimer.getTotalTimeLocked(rawRealtime, which);
5603 final int multicastWakeLockCount = mcTimer.getCountLocked(which);
5604
5605 if (multicastWakeLockTimeMicros > 0) {
5606 sb.setLength(0);
5607 sb.append(prefix);
5608 sb.append(" WiFi Multicast Wakelock");
5609 sb.append(" count = ");
5610 sb.append(multicastWakeLockCount);
5611 sb.append(" time = ");
5612 formatTimeMsNoSpace(sb, (multicastWakeLockTimeMicros + 500) / 1000);
5613 pw.println(sb.toString());
5614 }
5615 }
5616
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005617 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
5618 for (int isy=syncs.size()-1; isy>=0; isy--) {
5619 final Timer timer = syncs.valueAt(isy);
5620 // Convert from microseconds to milliseconds with rounding
5621 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5622 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07005623 final Timer bgTimer = timer.getSubTimer();
5624 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005625 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07005626 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005627 sb.setLength(0);
5628 sb.append(prefix);
5629 sb.append(" Sync ");
5630 sb.append(syncs.keyAt(isy));
5631 sb.append(": ");
5632 if (totalTime != 0) {
5633 formatTimeMs(sb, totalTime);
5634 sb.append("realtime (");
5635 sb.append(count);
5636 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07005637 if (bgTime > 0) {
5638 sb.append(", ");
5639 formatTimeMs(sb, bgTime);
5640 sb.append("background (");
5641 sb.append(bgCount);
5642 sb.append(" times)");
5643 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005644 } else {
5645 sb.append("(not used)");
5646 }
5647 pw.println(sb.toString());
5648 uidActivity = true;
5649 }
5650
5651 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
5652 for (int ij=jobs.size()-1; ij>=0; ij--) {
5653 final Timer timer = jobs.valueAt(ij);
5654 // Convert from microseconds to milliseconds with rounding
5655 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5656 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07005657 final Timer bgTimer = timer.getSubTimer();
5658 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005659 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07005660 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005661 sb.setLength(0);
5662 sb.append(prefix);
5663 sb.append(" Job ");
5664 sb.append(jobs.keyAt(ij));
5665 sb.append(": ");
5666 if (totalTime != 0) {
5667 formatTimeMs(sb, totalTime);
5668 sb.append("realtime (");
5669 sb.append(count);
5670 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07005671 if (bgTime > 0) {
5672 sb.append(", ");
5673 formatTimeMs(sb, bgTime);
5674 sb.append("background (");
5675 sb.append(bgCount);
5676 sb.append(" times)");
5677 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005678 } else {
5679 sb.append("(not used)");
5680 }
5681 pw.println(sb.toString());
5682 uidActivity = true;
5683 }
5684
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005685 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
5686 for (int ic=completions.size()-1; ic>=0; ic--) {
5687 SparseIntArray types = completions.valueAt(ic);
5688 if (types != null) {
5689 pw.print(prefix);
5690 pw.print(" Job Completions ");
5691 pw.print(completions.keyAt(ic));
5692 pw.print(":");
5693 for (int it=0; it<types.size(); it++) {
5694 pw.print(" ");
5695 pw.print(JobParameters.getReasonName(types.keyAt(it)));
5696 pw.print("(");
5697 pw.print(types.valueAt(it));
5698 pw.print("x)");
5699 }
5700 pw.println();
5701 }
5702 }
5703
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005704 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
5705 prefix, "Flashlight");
5706 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
5707 prefix, "Camera");
5708 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
5709 prefix, "Video");
5710 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
5711 prefix, "Audio");
5712
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005713 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
5714 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005715 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005716 final Uid.Sensor se = sensors.valueAt(ise);
5717 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005718 sb.setLength(0);
5719 sb.append(prefix);
5720 sb.append(" Sensor ");
5721 int handle = se.getHandle();
5722 if (handle == Uid.Sensor.GPS) {
5723 sb.append("GPS");
5724 } else {
5725 sb.append(handle);
5726 }
5727 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005728
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005729 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005730 if (timer != null) {
5731 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005732 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5733 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005734 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005735 final Timer bgTimer = se.getSensorBackgroundTime();
5736 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005737 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5738 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5739 final long bgActualTime = bgTimer != null ?
5740 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5741
Dianne Hackborn61659e52014-07-09 16:13:01 -07005742 //timer.logState();
5743 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005744 if (actualTime != totalTime) {
5745 formatTimeMs(sb, totalTime);
5746 sb.append("blamed realtime, ");
5747 }
5748
5749 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005750 sb.append("realtime (");
5751 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005752 sb.append(" times)");
5753
5754 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005755 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005756 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5757 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005758 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005759 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005760 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 } else {
5762 sb.append("(not used)");
5763 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005764 } else {
5765 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005766 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005767
5768 pw.println(sb.toString());
5769 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005770 }
5771
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005772 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5773 "Vibrator");
5774 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5775 prefix, "Foreground activities");
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005776 uidActivity |= printTimer(pw, sb, u.getForegroundServiceTimer(), rawRealtime, which,
5777 prefix, "Foreground services");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005778
Dianne Hackborn61659e52014-07-09 16:13:01 -07005779 long totalStateTime = 0;
5780 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5781 long time = u.getProcessStateTime(ips, rawRealtime, which);
5782 if (time > 0) {
5783 totalStateTime += time;
5784 sb.setLength(0);
5785 sb.append(prefix);
5786 sb.append(" ");
5787 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5788 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005789 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005790 pw.println(sb.toString());
5791 uidActivity = true;
5792 }
5793 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005794 if (totalStateTime > 0) {
5795 sb.setLength(0);
5796 sb.append(prefix);
5797 sb.append(" Total running: ");
5798 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5799 pw.println(sb.toString());
5800 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005801
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005802 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5803 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005804 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005805 sb.setLength(0);
5806 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005807 sb.append(" Total cpu time: u=");
5808 formatTimeMs(sb, userCpuTimeUs / 1000);
5809 sb.append("s=");
5810 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005811 pw.println(sb.toString());
5812 }
5813
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005814 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5815 if (cpuFreqTimes != null) {
5816 sb.setLength(0);
5817 sb.append(" Total cpu time per freq:");
5818 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5819 sb.append(" " + cpuFreqTimes[i]);
5820 }
5821 pw.println(sb.toString());
5822 }
5823 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5824 if (screenOffCpuFreqTimes != null) {
5825 sb.setLength(0);
5826 sb.append(" Total screen-off cpu time per freq:");
5827 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5828 sb.append(" " + screenOffCpuFreqTimes[i]);
5829 }
5830 pw.println(sb.toString());
5831 }
5832
Sudheer Shankab2f83c12017-11-13 19:25:01 -08005833 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
5834 final long[] cpuTimes = u.getCpuFreqTimes(which, procState);
5835 if (cpuTimes != null) {
5836 sb.setLength(0);
5837 sb.append(" Cpu times per freq at state "
5838 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5839 for (int i = 0; i < cpuTimes.length; ++i) {
5840 sb.append(" " + cpuTimes[i]);
5841 }
5842 pw.println(sb.toString());
5843 }
5844
5845 final long[] screenOffCpuTimes = u.getScreenOffCpuFreqTimes(which, procState);
5846 if (screenOffCpuTimes != null) {
5847 sb.setLength(0);
5848 sb.append(" Screen-off cpu times per freq at state "
5849 + Uid.PROCESS_STATE_NAMES[procState] + ":");
5850 for (int i = 0; i < screenOffCpuTimes.length; ++i) {
5851 sb.append(" " + screenOffCpuTimes[i]);
5852 }
5853 pw.println(sb.toString());
5854 }
5855 }
5856
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005857 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5858 = u.getProcessStats();
5859 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5860 final Uid.Proc ps = processStats.valueAt(ipr);
5861 long userTime;
5862 long systemTime;
5863 long foregroundTime;
5864 int starts;
5865 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005866
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005867 userTime = ps.getUserTime(which);
5868 systemTime = ps.getSystemTime(which);
5869 foregroundTime = ps.getForegroundTime(which);
5870 starts = ps.getStarts(which);
5871 final int numCrashes = ps.getNumCrashes(which);
5872 final int numAnrs = ps.getNumAnrs(which);
5873 numExcessive = which == STATS_SINCE_CHARGED
5874 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005875
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005876 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5877 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5878 sb.setLength(0);
5879 sb.append(prefix); sb.append(" Proc ");
5880 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5881 sb.append(prefix); sb.append(" CPU: ");
5882 formatTimeMs(sb, userTime); sb.append("usr + ");
5883 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5884 formatTimeMs(sb, foregroundTime); sb.append("fg");
5885 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5886 sb.append("\n"); sb.append(prefix); sb.append(" ");
5887 boolean hasOne = false;
5888 if (starts != 0) {
5889 hasOne = true;
5890 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005891 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005892 if (numCrashes != 0) {
5893 if (hasOne) {
5894 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005895 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005896 hasOne = true;
5897 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005898 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005899 if (numAnrs != 0) {
5900 if (hasOne) {
5901 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005902 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005903 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005904 }
5905 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005906 pw.println(sb.toString());
5907 for (int e=0; e<numExcessive; e++) {
5908 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5909 if (ew != null) {
5910 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07005911 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005912 pw.print("cpu");
5913 } else {
5914 pw.print("unknown");
5915 }
5916 pw.print(" use: ");
5917 TimeUtils.formatDuration(ew.usedTime, pw);
5918 pw.print(" over ");
5919 TimeUtils.formatDuration(ew.overTime, pw);
5920 if (ew.overTime != 0) {
5921 pw.print(" (");
5922 pw.print((ew.usedTime*100)/ew.overTime);
5923 pw.println("%)");
5924 }
5925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005926 }
5927 uidActivity = true;
5928 }
5929 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005930
5931 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5932 = u.getPackageStats();
5933 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
5934 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
5935 pw.println(":");
5936 boolean apkActivity = false;
5937 final Uid.Pkg ps = packageStats.valueAt(ipkg);
5938 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
5939 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
5940 pw.print(prefix); pw.print(" Wakeup alarm ");
5941 pw.print(alarms.keyAt(iwa)); pw.print(": ");
5942 pw.print(alarms.valueAt(iwa).getCountLocked(which));
5943 pw.println(" times");
5944 apkActivity = true;
5945 }
5946 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
5947 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
5948 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
5949 final long startTime = ss.getStartTime(batteryUptime, which);
5950 final int starts = ss.getStarts(which);
5951 final int launches = ss.getLaunches(which);
5952 if (startTime != 0 || starts != 0 || launches != 0) {
5953 sb.setLength(0);
5954 sb.append(prefix); sb.append(" Service ");
5955 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
5956 sb.append(prefix); sb.append(" Created for: ");
5957 formatTimeMs(sb, startTime / 1000);
5958 sb.append("uptime\n");
5959 sb.append(prefix); sb.append(" Starts: ");
5960 sb.append(starts);
5961 sb.append(", launches: "); sb.append(launches);
5962 pw.println(sb.toString());
5963 apkActivity = true;
5964 }
5965 }
5966 if (!apkActivity) {
5967 pw.print(prefix); pw.println(" (nothing executed)");
5968 }
5969 uidActivity = true;
5970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005971 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005972 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005973 }
5974 }
5975 }
5976
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005977 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005978 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005979 int diff = oldval ^ newval;
5980 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005981 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005982 for (int i=0; i<descriptions.length; i++) {
5983 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005984 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005985 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005986 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005987 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005988 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005989 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
5990 didWake = true;
5991 pw.print("=");
5992 if (longNames) {
5993 UserHandle.formatUid(pw, wakelockTag.uid);
5994 pw.print(":\"");
5995 pw.print(wakelockTag.string);
5996 pw.print("\"");
5997 } else {
5998 pw.print(wakelockTag.poolIdx);
5999 }
6000 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006001 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006002 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006003 pw.print("=");
6004 int val = (newval&bd.mask)>>bd.shift;
6005 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006006 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006007 } else {
6008 pw.print(val);
6009 }
6010 }
6011 }
6012 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006013 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07006014 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006015 if (longNames) {
6016 UserHandle.formatUid(pw, wakelockTag.uid);
6017 pw.print(":\"");
6018 pw.print(wakelockTag.string);
6019 pw.print("\"");
6020 } else {
6021 pw.print(wakelockTag.poolIdx);
6022 }
6023 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006024 }
Mike Mac2f518a2017-09-19 16:06:03 -07006025
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006026 public void prepareForDumpLocked() {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006027 // We don't need to require subclasses implement this.
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006028 }
6029
6030 public static class HistoryPrinter {
6031 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006032 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006033 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006034 int oldStatus = -1;
6035 int oldHealth = -1;
6036 int oldPlug = -1;
6037 int oldTemp = -1;
6038 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006039 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006040 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006041
Dianne Hackborn3251b902014-06-20 14:40:53 -07006042 void reset() {
6043 oldState = oldState2 = 0;
6044 oldLevel = -1;
6045 oldStatus = -1;
6046 oldHealth = -1;
6047 oldPlug = -1;
6048 oldTemp = -1;
6049 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006050 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07006051 }
6052
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006053 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006054 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006055 if (!checkin) {
6056 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006057 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006058 pw.print(" (");
6059 pw.print(rec.numReadInts);
6060 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006061 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006062 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6063 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006064 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006065 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006066 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006067 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006068 }
6069 lastTime = rec.time;
6070 }
6071 if (rec.cmd == HistoryItem.CMD_START) {
6072 if (checkin) {
6073 pw.print(":");
6074 }
6075 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006076 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006077 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
6078 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006079 if (checkin) {
6080 pw.print(":");
6081 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006082 if (rec.cmd == HistoryItem.CMD_RESET) {
6083 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07006084 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006085 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006086 pw.print("TIME:");
6087 if (checkin) {
6088 pw.println(rec.currentTime);
6089 } else {
6090 pw.print(" ");
6091 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6092 rec.currentTime).toString());
6093 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006094 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
6095 if (checkin) {
6096 pw.print(":");
6097 }
6098 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006099 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
6100 if (checkin) {
6101 pw.print(":");
6102 }
6103 pw.println("*OVERFLOW*");
6104 } else {
6105 if (!checkin) {
6106 if (rec.batteryLevel < 10) pw.print("00");
6107 else if (rec.batteryLevel < 100) pw.print("0");
6108 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006109 if (verbose) {
6110 pw.print(" ");
6111 if (rec.states < 0) ;
6112 else if (rec.states < 0x10) pw.print("0000000");
6113 else if (rec.states < 0x100) pw.print("000000");
6114 else if (rec.states < 0x1000) pw.print("00000");
6115 else if (rec.states < 0x10000) pw.print("0000");
6116 else if (rec.states < 0x100000) pw.print("000");
6117 else if (rec.states < 0x1000000) pw.print("00");
6118 else if (rec.states < 0x10000000) pw.print("0");
6119 pw.print(Integer.toHexString(rec.states));
6120 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006121 } else {
6122 if (oldLevel != rec.batteryLevel) {
6123 oldLevel = rec.batteryLevel;
6124 pw.print(",Bl="); pw.print(rec.batteryLevel);
6125 }
6126 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006127 if (oldStatus != rec.batteryStatus) {
6128 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006129 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006130 switch (oldStatus) {
6131 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006132 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006133 break;
6134 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006135 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006136 break;
6137 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006138 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006139 break;
6140 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006141 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006142 break;
6143 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006144 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006145 break;
6146 default:
6147 pw.print(oldStatus);
6148 break;
6149 }
6150 }
6151 if (oldHealth != rec.batteryHealth) {
6152 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006153 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006154 switch (oldHealth) {
6155 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006156 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006157 break;
6158 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006159 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006160 break;
6161 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006162 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006163 break;
6164 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006165 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006166 break;
6167 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006168 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006169 break;
6170 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006171 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006172 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006173 case BatteryManager.BATTERY_HEALTH_COLD:
6174 pw.print(checkin ? "c" : "cold");
6175 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006176 default:
6177 pw.print(oldHealth);
6178 break;
6179 }
6180 }
6181 if (oldPlug != rec.batteryPlugType) {
6182 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006183 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006184 switch (oldPlug) {
6185 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006186 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006187 break;
6188 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006189 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006190 break;
6191 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006192 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006193 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006194 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006195 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07006196 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006197 default:
6198 pw.print(oldPlug);
6199 break;
6200 }
6201 }
6202 if (oldTemp != rec.batteryTemperature) {
6203 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006204 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006205 pw.print(oldTemp);
6206 }
6207 if (oldVolt != rec.batteryVoltage) {
6208 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006209 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006210 pw.print(oldVolt);
6211 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006212 final int chargeMAh = rec.batteryChargeUAh / 1000;
6213 if (oldChargeMAh != chargeMAh) {
6214 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07006215 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07006216 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07006217 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006218 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006219 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006220 printBitDescriptions(pw, oldState2, rec.states2, null,
6221 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006222 if (rec.wakeReasonTag != null) {
6223 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006224 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006225 pw.print(rec.wakeReasonTag.poolIdx);
6226 } else {
6227 pw.print(" wake_reason=");
6228 pw.print(rec.wakeReasonTag.uid);
6229 pw.print(":\"");
6230 pw.print(rec.wakeReasonTag.string);
6231 pw.print("\"");
6232 }
6233 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08006234 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006235 pw.print(checkin ? "," : " ");
6236 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
6237 pw.print("+");
6238 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
6239 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006240 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006241 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
6242 : HISTORY_EVENT_NAMES;
6243 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
6244 | HistoryItem.EVENT_FLAG_FINISH);
6245 if (idx >= 0 && idx < eventNames.length) {
6246 pw.print(eventNames[idx]);
6247 } else {
6248 pw.print(checkin ? "Ev" : "event");
6249 pw.print(idx);
6250 }
6251 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006252 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006253 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08006254 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08006255 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
6256 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006257 pw.print(":\"");
6258 pw.print(rec.eventTag.string);
6259 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08006260 }
6261 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006262 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006263 if (rec.stepDetails != null) {
6264 if (!checkin) {
6265 pw.print(" Details: cpu=");
6266 pw.print(rec.stepDetails.userTime);
6267 pw.print("u+");
6268 pw.print(rec.stepDetails.systemTime);
6269 pw.print("s");
6270 if (rec.stepDetails.appCpuUid1 >= 0) {
6271 pw.print(" (");
6272 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
6273 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6274 if (rec.stepDetails.appCpuUid2 >= 0) {
6275 pw.print(", ");
6276 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
6277 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6278 }
6279 if (rec.stepDetails.appCpuUid3 >= 0) {
6280 pw.print(", ");
6281 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
6282 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6283 }
6284 pw.print(')');
6285 }
6286 pw.println();
6287 pw.print(" /proc/stat=");
6288 pw.print(rec.stepDetails.statUserTime);
6289 pw.print(" usr, ");
6290 pw.print(rec.stepDetails.statSystemTime);
6291 pw.print(" sys, ");
6292 pw.print(rec.stepDetails.statIOWaitTime);
6293 pw.print(" io, ");
6294 pw.print(rec.stepDetails.statIrqTime);
6295 pw.print(" irq, ");
6296 pw.print(rec.stepDetails.statSoftIrqTime);
6297 pw.print(" sirq, ");
6298 pw.print(rec.stepDetails.statIdlTime);
6299 pw.print(" idle");
6300 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
6301 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
6302 + rec.stepDetails.statSoftIrqTime;
6303 int total = totalRun + rec.stepDetails.statIdlTime;
6304 if (total > 0) {
6305 pw.print(" (");
6306 float perc = ((float)totalRun) / ((float)total) * 100;
6307 pw.print(String.format("%.1f%%", perc));
6308 pw.print(" of ");
6309 StringBuilder sb = new StringBuilder(64);
6310 formatTimeMsNoSpace(sb, total*10);
6311 pw.print(sb);
6312 pw.print(")");
6313 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006314 pw.print(", PlatformIdleStat ");
6315 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006316 pw.println();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006317
6318 pw.print(", SubsystemPowerState ");
6319 pw.print(rec.stepDetails.statSubsystemPowerState);
6320 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006321 } else {
6322 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6323 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
6324 pw.print(rec.stepDetails.userTime);
6325 pw.print(":");
6326 pw.print(rec.stepDetails.systemTime);
6327 if (rec.stepDetails.appCpuUid1 >= 0) {
6328 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
6329 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
6330 if (rec.stepDetails.appCpuUid2 >= 0) {
6331 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
6332 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
6333 }
6334 if (rec.stepDetails.appCpuUid3 >= 0) {
6335 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
6336 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
6337 }
6338 }
6339 pw.println();
6340 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6341 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
6342 pw.print(rec.stepDetails.statUserTime);
6343 pw.print(',');
6344 pw.print(rec.stepDetails.statSystemTime);
6345 pw.print(',');
6346 pw.print(rec.stepDetails.statIOWaitTime);
6347 pw.print(',');
6348 pw.print(rec.stepDetails.statIrqTime);
6349 pw.print(',');
6350 pw.print(rec.stepDetails.statSoftIrqTime);
6351 pw.print(',');
6352 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07006353 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006354 if (rec.stepDetails.statPlatformIdleState != null) {
6355 pw.print(rec.stepDetails.statPlatformIdleState);
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07006356 if (rec.stepDetails.statSubsystemPowerState != null) {
6357 pw.print(',');
6358 }
Adam Lesinski8568d8f2016-07-15 18:13:23 -07006359 }
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00006360
6361 if (rec.stepDetails.statSubsystemPowerState != null) {
6362 pw.print(rec.stepDetails.statSubsystemPowerState);
6363 }
6364 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006365 }
6366 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006367 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006368 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006369 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006370 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006371
6372 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
6373 UserHandle.formatUid(pw, uid);
6374 pw.print("=");
6375 pw.print(utime);
6376 pw.print("u+");
6377 pw.print(stime);
6378 pw.print("s");
6379 }
6380
6381 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
6382 pw.print('/');
6383 pw.print(uid);
6384 pw.print(":");
6385 pw.print(utime);
6386 pw.print(":");
6387 pw.print(stime);
6388 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006389 }
6390
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006391 private void printSizeValue(PrintWriter pw, long size) {
6392 float result = size;
6393 String suffix = "";
6394 if (result >= 10*1024) {
6395 suffix = "KB";
6396 result = result / 1024;
6397 }
6398 if (result >= 10*1024) {
6399 suffix = "MB";
6400 result = result / 1024;
6401 }
6402 if (result >= 10*1024) {
6403 suffix = "GB";
6404 result = result / 1024;
6405 }
6406 if (result >= 10*1024) {
6407 suffix = "TB";
6408 result = result / 1024;
6409 }
6410 if (result >= 10*1024) {
6411 suffix = "PB";
6412 result = result / 1024;
6413 }
6414 pw.print((int)result);
6415 pw.print(suffix);
6416 }
6417
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006418 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
6419 String label3, long estimatedTime) {
6420 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006421 return false;
6422 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006423 pw.print(label1);
6424 pw.print(label2);
6425 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006426 StringBuilder sb = new StringBuilder(64);
6427 formatTimeMs(sb, estimatedTime);
6428 pw.print(sb);
6429 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006430 return true;
6431 }
6432
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006433 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
6434 LevelStepTracker steps, boolean checkin) {
6435 if (steps == null) {
6436 return false;
6437 }
6438 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006439 if (count <= 0) {
6440 return false;
6441 }
6442 if (!checkin) {
6443 pw.println(header);
6444 }
Kweku Adams030980a2015-04-01 16:07:48 -07006445 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006446 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006447 long duration = steps.getDurationAt(i);
6448 int level = steps.getLevelAt(i);
6449 long initMode = steps.getInitModeAt(i);
6450 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006451 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006452 lineArgs[0] = Long.toString(duration);
6453 lineArgs[1] = Integer.toString(level);
6454 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6455 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6456 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
6457 case Display.STATE_ON: lineArgs[2] = "s+"; break;
6458 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
6459 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07006460 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006461 }
6462 } else {
6463 lineArgs[2] = "";
6464 }
6465 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6466 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
6467 } else {
6468 lineArgs[3] = "";
6469 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006470 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07006471 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006472 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07006473 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006474 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006475 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
6476 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006477 pw.print(prefix);
6478 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006479 TimeUtils.formatDuration(duration, pw);
6480 pw.print(" to "); pw.print(level);
6481 boolean haveModes = false;
6482 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6483 pw.print(" (");
6484 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6485 case Display.STATE_OFF: pw.print("screen-off"); break;
6486 case Display.STATE_ON: pw.print("screen-on"); break;
6487 case Display.STATE_DOZE: pw.print("screen-doze"); break;
6488 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07006489 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006490 }
6491 haveModes = true;
6492 }
6493 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6494 pw.print(haveModes ? ", " : " (");
6495 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
6496 ? "power-save-on" : "power-save-off");
6497 haveModes = true;
6498 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006499 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6500 pw.print(haveModes ? ", " : " (");
6501 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6502 ? "device-idle-on" : "device-idle-off");
6503 haveModes = true;
6504 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006505 if (haveModes) {
6506 pw.print(")");
6507 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006508 pw.println();
6509 }
6510 }
6511 return true;
6512 }
6513
Kweku Adams87b19ec2017-10-09 12:40:03 -07006514 private static void dumpDurationSteps(ProtoOutputStream proto, long fieldId,
6515 LevelStepTracker steps) {
6516 if (steps == null) {
6517 return;
6518 }
6519 int count = steps.mNumStepDurations;
Kweku Adams87b19ec2017-10-09 12:40:03 -07006520 for (int i = 0; i < count; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07006521 long token = proto.start(fieldId);
Kweku Adams87b19ec2017-10-09 12:40:03 -07006522 proto.write(SystemProto.BatteryLevelStep.DURATION_MS, steps.getDurationAt(i));
6523 proto.write(SystemProto.BatteryLevelStep.LEVEL, steps.getLevelAt(i));
6524
6525 final long initMode = steps.getInitModeAt(i);
6526 final long modMode = steps.getModModeAt(i);
6527
6528 int ds = SystemProto.BatteryLevelStep.DS_MIXED;
6529 if ((modMode & STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6530 switch ((int) (initMode & STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6531 case Display.STATE_OFF:
6532 ds = SystemProto.BatteryLevelStep.DS_OFF;
6533 break;
6534 case Display.STATE_ON:
6535 ds = SystemProto.BatteryLevelStep.DS_ON;
6536 break;
6537 case Display.STATE_DOZE:
6538 ds = SystemProto.BatteryLevelStep.DS_DOZE;
6539 break;
6540 case Display.STATE_DOZE_SUSPEND:
6541 ds = SystemProto.BatteryLevelStep.DS_DOZE_SUSPEND;
6542 break;
6543 default:
6544 ds = SystemProto.BatteryLevelStep.DS_ERROR;
6545 break;
6546 }
6547 }
6548 proto.write(SystemProto.BatteryLevelStep.DISPLAY_STATE, ds);
6549
6550 int psm = SystemProto.BatteryLevelStep.PSM_MIXED;
6551 if ((modMode & STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6552 psm = (initMode & STEP_LEVEL_MODE_POWER_SAVE) != 0
6553 ? SystemProto.BatteryLevelStep.PSM_ON : SystemProto.BatteryLevelStep.PSM_OFF;
6554 }
6555 proto.write(SystemProto.BatteryLevelStep.POWER_SAVE_MODE, psm);
6556
6557 int im = SystemProto.BatteryLevelStep.IM_MIXED;
6558 if ((modMode & STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6559 im = (initMode & STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6560 ? SystemProto.BatteryLevelStep.IM_ON : SystemProto.BatteryLevelStep.IM_OFF;
6561 }
6562 proto.write(SystemProto.BatteryLevelStep.IDLE_MODE, im);
6563
6564 proto.end(token);
6565 }
6566 }
6567
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006568 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006569 public static final int DUMP_DAILY_ONLY = 1<<2;
6570 public static final int DUMP_HISTORY_ONLY = 1<<3;
6571 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
6572 public static final int DUMP_VERBOSE = 1<<5;
6573 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006574
Dianne Hackborn37de0982014-05-09 09:32:18 -07006575 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
6576 final HistoryPrinter hprinter = new HistoryPrinter();
6577 final HistoryItem rec = new HistoryItem();
6578 long lastTime = -1;
6579 long baseTime = -1;
6580 boolean printed = false;
6581 HistoryEventTracker tracker = null;
6582 while (getNextHistoryLocked(rec)) {
6583 lastTime = rec.time;
6584 if (baseTime < 0) {
6585 baseTime = lastTime;
6586 }
6587 if (rec.time >= histStart) {
6588 if (histStart >= 0 && !printed) {
6589 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07006590 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006591 || rec.cmd == HistoryItem.CMD_START
6592 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006593 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006594 hprinter.printNextItem(pw, rec, baseTime, checkin,
6595 (flags&DUMP_VERBOSE) != 0);
6596 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006597 } else if (rec.currentTime != 0) {
6598 printed = true;
6599 byte cmd = rec.cmd;
6600 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006601 hprinter.printNextItem(pw, rec, baseTime, checkin,
6602 (flags&DUMP_VERBOSE) != 0);
6603 rec.cmd = cmd;
6604 }
6605 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006606 if (rec.cmd != HistoryItem.CMD_UPDATE) {
6607 hprinter.printNextItem(pw, rec, baseTime, checkin,
6608 (flags&DUMP_VERBOSE) != 0);
6609 rec.cmd = HistoryItem.CMD_UPDATE;
6610 }
6611 int oldEventCode = rec.eventCode;
6612 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006613 rec.eventTag = new HistoryTag();
6614 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
6615 HashMap<String, SparseIntArray> active
6616 = tracker.getStateForEvent(i);
6617 if (active == null) {
6618 continue;
6619 }
6620 for (HashMap.Entry<String, SparseIntArray> ent
6621 : active.entrySet()) {
6622 SparseIntArray uids = ent.getValue();
6623 for (int j=0; j<uids.size(); j++) {
6624 rec.eventCode = i;
6625 rec.eventTag.string = ent.getKey();
6626 rec.eventTag.uid = uids.keyAt(j);
6627 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006628 hprinter.printNextItem(pw, rec, baseTime, checkin,
6629 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006630 rec.wakeReasonTag = null;
6631 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006632 }
6633 }
6634 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006635 rec.eventCode = oldEventCode;
6636 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006637 tracker = null;
6638 }
6639 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006640 hprinter.printNextItem(pw, rec, baseTime, checkin,
6641 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07006642 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
6643 // This is an attempt to aggregate the previous state and generate
6644 // fake events to reflect that state at the point where we start
6645 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07006646 if (tracker == null) {
6647 tracker = new HistoryEventTracker();
6648 }
6649 tracker.updateState(rec.eventCode, rec.eventTag.string,
6650 rec.eventTag.uid, rec.eventTag.poolIdx);
6651 }
6652 }
6653 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07006654 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006655 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
6656 }
6657 }
6658
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006659 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
6660 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
6661 if (steps == null) {
6662 return;
6663 }
6664 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
6665 if (timeRemaining >= 0) {
6666 pw.print(prefix); pw.print(label); pw.print(" total time: ");
6667 tmpSb.setLength(0);
6668 formatTimeMs(tmpSb, timeRemaining);
6669 pw.print(tmpSb);
6670 pw.print(" (from "); pw.print(tmpOutInt[0]);
6671 pw.println(" steps)");
6672 }
6673 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6674 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6675 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
6676 if (estimatedTime > 0) {
6677 pw.print(prefix); pw.print(label); pw.print(" ");
6678 pw.print(STEP_LEVEL_MODE_LABELS[i]);
6679 pw.print(" time: ");
6680 tmpSb.setLength(0);
6681 formatTimeMs(tmpSb, estimatedTime);
6682 pw.print(tmpSb);
6683 pw.print(" (from "); pw.print(tmpOutInt[0]);
6684 pw.println(" steps)");
6685 }
6686 }
6687 }
6688
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006689 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
6690 ArrayList<PackageChange> changes) {
6691 if (changes == null) {
6692 return;
6693 }
6694 pw.print(prefix); pw.println("Package changes:");
6695 for (int i=0; i<changes.size(); i++) {
6696 PackageChange pc = changes.get(i);
6697 if (pc.mUpdate) {
6698 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
6699 pw.print(" vers="); pw.println(pc.mVersionCode);
6700 } else {
6701 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
6702 }
6703 }
6704 }
6705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006706 /**
6707 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
6708 *
6709 * @param pw a Printer to receive the dump output.
6710 */
6711 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006712 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006713 prepareForDumpLocked();
6714
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006715 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006716 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006717
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006718 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006719 final long historyTotalSize = getHistoryTotalSize();
6720 final long historyUsedSize = getHistoryUsedSize();
6721 if (startIteratingHistoryLocked()) {
6722 try {
6723 pw.print("Battery History (");
6724 pw.print((100*historyUsedSize)/historyTotalSize);
6725 pw.print("% used, ");
6726 printSizeValue(pw, historyUsedSize);
6727 pw.print(" used of ");
6728 printSizeValue(pw, historyTotalSize);
6729 pw.print(", ");
6730 pw.print(getHistoryStringPoolSize());
6731 pw.print(" strings using ");
6732 printSizeValue(pw, getHistoryStringPoolBytes());
6733 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07006734 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006735 pw.println();
6736 } finally {
6737 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006738 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006739 }
6740
6741 if (startIteratingOldHistoryLocked()) {
6742 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006743 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006744 pw.println("Old battery History:");
6745 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006746 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006747 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006748 if (baseTime < 0) {
6749 baseTime = rec.time;
6750 }
6751 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006752 }
6753 pw.println();
6754 } finally {
6755 finishIteratingOldHistoryLocked();
6756 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006757 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006758 }
6759
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006760 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006761 return;
6762 }
6763
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006764 if (!filtering) {
6765 SparseArray<? extends Uid> uidStats = getUidStats();
6766 final int NU = uidStats.size();
6767 boolean didPid = false;
6768 long nowRealtime = SystemClock.elapsedRealtime();
6769 for (int i=0; i<NU; i++) {
6770 Uid uid = uidStats.valueAt(i);
6771 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
6772 if (pids != null) {
6773 for (int j=0; j<pids.size(); j++) {
6774 Uid.Pid pid = pids.valueAt(j);
6775 if (!didPid) {
6776 pw.println("Per-PID Stats:");
6777 didPid = true;
6778 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006779 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
6780 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006781 pw.print(" PID "); pw.print(pids.keyAt(j));
6782 pw.print(" wake time: ");
6783 TimeUtils.formatDuration(time, pw);
6784 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006785 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006786 }
6787 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006788 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006789 pw.println();
6790 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006791 }
6792
6793 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006794 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
6795 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006796 long timeRemaining = computeBatteryTimeRemaining(
6797 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006798 if (timeRemaining >= 0) {
6799 pw.print(" Estimated discharge time remaining: ");
6800 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6801 pw.println();
6802 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006803 final LevelStepTracker steps = getDischargeLevelStepTracker();
6804 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6805 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
6806 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6807 STEP_LEVEL_MODE_VALUES[i], null));
6808 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006809 pw.println();
6810 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006811 if (dumpDurationSteps(pw, " ", "Charge step durations:",
6812 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006813 long timeRemaining = computeChargeTimeRemaining(
6814 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006815 if (timeRemaining >= 0) {
6816 pw.print(" Estimated charge time remaining: ");
6817 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6818 pw.println();
6819 }
6820 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006821 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006822 }
Dianne Hackbornc81983a2017-10-20 16:16:32 -07006823 if (!filtering || (flags & DUMP_DAILY_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006824 pw.println("Daily stats:");
6825 pw.print(" Current start time: ");
6826 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6827 getCurrentDailyStartTime()).toString());
6828 pw.print(" Next min deadline: ");
6829 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6830 getNextMinDailyDeadline()).toString());
6831 pw.print(" Next max deadline: ");
6832 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6833 getNextMaxDailyDeadline()).toString());
6834 StringBuilder sb = new StringBuilder(64);
6835 int[] outInt = new int[1];
6836 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
6837 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006838 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
6839 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006840 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006841 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
6842 dsteps, false)) {
6843 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6844 sb, outInt);
6845 }
6846 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6847 csteps, false)) {
6848 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6849 sb, outInt);
6850 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006851 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006852 } else {
6853 pw.println(" Current daily steps:");
6854 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6855 sb, outInt);
6856 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6857 sb, outInt);
6858 }
6859 }
6860 DailyItem dit;
6861 int curIndex = 0;
6862 while ((dit=getDailyItemLocked(curIndex)) != null) {
6863 curIndex++;
6864 if ((flags&DUMP_DAILY_ONLY) != 0) {
6865 pw.println();
6866 }
6867 pw.print(" Daily from ");
6868 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6869 pw.print(" to ");
6870 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6871 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006872 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006873 if (dumpDurationSteps(pw, " ",
6874 " Discharge step durations:", dit.mDischargeSteps, false)) {
6875 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6876 sb, outInt);
6877 }
6878 if (dumpDurationSteps(pw, " ",
6879 " Charge step durations:", dit.mChargeSteps, false)) {
6880 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6881 sb, outInt);
6882 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006883 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006884 } else {
6885 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6886 sb, outInt);
6887 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6888 sb, outInt);
6889 }
6890 }
6891 pw.println();
6892 }
6893 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006894 pw.println("Statistics since last charge:");
6895 pw.println(" System starts: " + getStartCount()
6896 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006897 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6898 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006899 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006900 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006901 }
Mike Mac2f518a2017-09-19 16:06:03 -07006902
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006903 // This is called from BatteryStatsService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006904 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006905 public void dumpCheckinLocked(Context context, PrintWriter pw,
6906 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006907 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006908
6909 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006910 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6911 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006912
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006913 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6914
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006915 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07006916 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006917 try {
6918 for (int i=0; i<getHistoryStringPoolSize(); i++) {
6919 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6920 pw.print(HISTORY_STRING_POOL); pw.print(',');
6921 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006922 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006923 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006924 pw.print(",\"");
6925 String str = getHistoryTagPoolString(i);
6926 str = str.replace("\\", "\\\\");
6927 str = str.replace("\"", "\\\"");
6928 pw.print(str);
6929 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006930 pw.println();
6931 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006932 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006933 } finally {
6934 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08006935 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006936 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006937 }
6938
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006939 if ((flags & DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006940 return;
6941 }
6942
Dianne Hackborne4a59512010-12-07 11:08:07 -08006943 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006944 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08006945 for (int i=0; i<apps.size(); i++) {
6946 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006947 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
6948 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08006949 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006950 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
6951 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006952 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006953 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006954 }
6955 SparseArray<? extends Uid> uidStats = getUidStats();
6956 final int NU = uidStats.size();
6957 String[] lineArgs = new String[2];
6958 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006959 int uid = UserHandle.getAppId(uidStats.keyAt(i));
6960 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
6961 if (pkgs != null && !pkgs.second.value) {
6962 pkgs.second.value = true;
6963 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08006964 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006965 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006966 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
6967 (Object[])lineArgs);
6968 }
6969 }
6970 }
6971 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006972 if ((flags & DUMP_DAILY_ONLY) == 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006973 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006974 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07006975 long timeRemaining = computeBatteryTimeRemaining(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 */, DISCHARGE_TIME_REMAIN_DATA,
6979 (Object[])lineArgs);
6980 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006981 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07006982 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006983 if (timeRemaining >= 0) {
6984 lineArgs[0] = Long.toString(timeRemaining);
6985 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
6986 (Object[])lineArgs);
6987 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006988 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
6989 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006990 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006991 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006992
Kweku Adams87b19ec2017-10-09 12:40:03 -07006993 /** Dump #STATS_SINCE_CHARGED batterystats data to a proto. @hide */
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006994 public void dumpProtoLocked(Context context, FileDescriptor fd, List<ApplicationInfo> apps,
Kweku Adams6ccebf22017-12-11 12:30:35 -08006995 int flags) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006996 final ProtoOutputStream proto = new ProtoOutputStream(fd);
6997 final long bToken = proto.start(BatteryStatsServiceDumpProto.BATTERYSTATS);
6998 prepareForDumpLocked();
6999
7000 proto.write(BatteryStatsProto.REPORT_VERSION, CHECKIN_VERSION);
7001 proto.write(BatteryStatsProto.PARCEL_VERSION, getParcelVersion());
7002 proto.write(BatteryStatsProto.START_PLATFORM_VERSION, getStartPlatformVersion());
7003 proto.write(BatteryStatsProto.END_PLATFORM_VERSION, getEndPlatformVersion());
7004
Kweku Adams6ccebf22017-12-11 12:30:35 -08007005 // History intentionally not included in proto dump.
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007006
7007 if ((flags & (DUMP_HISTORY_ONLY | DUMP_DAILY_ONLY)) == 0) {
Kweku Adams103351f2017-10-16 14:39:34 -07007008 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false,
7009 (flags & DUMP_DEVICE_WIFI_ONLY) != 0);
7010 helper.create(this);
7011 helper.refreshStats(STATS_SINCE_CHARGED, UserHandle.USER_ALL);
7012
7013 dumpProtoAppsLocked(proto, helper, apps);
7014 dumpProtoSystemLocked(proto, helper);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07007015 }
7016
7017 proto.end(bToken);
7018 proto.flush();
7019 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07007020
Kweku Adams103351f2017-10-16 14:39:34 -07007021 private void dumpProtoAppsLocked(ProtoOutputStream proto, BatteryStatsHelper helper,
7022 List<ApplicationInfo> apps) {
7023 final int which = STATS_SINCE_CHARGED;
7024 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7025 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7026 final long rawRealtimeUs = rawRealtimeMs * 1000;
7027 final long batteryUptimeUs = getBatteryUptime(rawUptimeUs);
7028
7029 SparseArray<ArrayList<String>> aidToPackages = new SparseArray<>();
7030 if (apps != null) {
7031 for (int i = 0; i < apps.size(); ++i) {
7032 ApplicationInfo ai = apps.get(i);
7033 int aid = UserHandle.getAppId(ai.uid);
7034 ArrayList<String> pkgs = aidToPackages.get(aid);
7035 if (pkgs == null) {
7036 pkgs = new ArrayList<String>();
7037 aidToPackages.put(aid, pkgs);
7038 }
7039 pkgs.add(ai.packageName);
7040 }
7041 }
7042
7043 SparseArray<BatterySipper> uidToSipper = new SparseArray<>();
7044 final List<BatterySipper> sippers = helper.getUsageList();
7045 if (sippers != null) {
7046 for (int i = 0; i < sippers.size(); ++i) {
7047 final BatterySipper bs = sippers.get(i);
7048 if (bs.drainType != BatterySipper.DrainType.APP) {
7049 // Others are handled by dumpProtoSystemLocked()
7050 continue;
7051 }
7052 uidToSipper.put(bs.uidObj.getUid(), bs);
7053 }
7054 }
7055
7056 SparseArray<? extends Uid> uidStats = getUidStats();
7057 final int n = uidStats.size();
7058 for (int iu = 0; iu < n; ++iu) {
7059 final long uTkn = proto.start(BatteryStatsProto.UIDS);
7060 final Uid u = uidStats.valueAt(iu);
7061
7062 final int uid = uidStats.keyAt(iu);
7063 proto.write(UidProto.UID, uid);
7064
7065 // Print packages and apk stats (UID_DATA & APK_DATA)
7066 ArrayList<String> pkgs = aidToPackages.get(UserHandle.getAppId(uid));
7067 if (pkgs == null) {
7068 pkgs = new ArrayList<String>();
7069 }
7070 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats =
7071 u.getPackageStats();
7072 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7073 String pkg = packageStats.keyAt(ipkg);
7074 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats =
7075 packageStats.valueAt(ipkg).getServiceStats();
7076 if (serviceStats.size() == 0) {
7077 // Due to the way ActivityManagerService logs wakeup alarms, some packages (for
7078 // example, "android") may be included in the packageStats that aren't part of
7079 // the UID. If they don't have any services, then they shouldn't be listed here.
7080 // These packages won't be a part in the pkgs List.
7081 continue;
7082 }
7083
7084 final long pToken = proto.start(UidProto.PACKAGES);
7085 proto.write(UidProto.Package.NAME, pkg);
7086 // Remove from the packages list since we're logging it here.
7087 pkgs.remove(pkg);
7088
7089 for (int isvc = serviceStats.size() - 1; isvc >= 0; --isvc) {
7090 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
7091 long sToken = proto.start(UidProto.Package.SERVICES);
7092
7093 proto.write(UidProto.Package.Service.NAME, serviceStats.keyAt(isvc));
7094 proto.write(UidProto.Package.Service.START_DURATION_MS,
7095 roundUsToMs(ss.getStartTime(batteryUptimeUs, which)));
7096 proto.write(UidProto.Package.Service.START_COUNT, ss.getStarts(which));
7097 proto.write(UidProto.Package.Service.LAUNCH_COUNT, ss.getLaunches(which));
7098
7099 proto.end(sToken);
7100 }
7101 proto.end(pToken);
7102 }
7103 // Print any remaining packages that weren't in the packageStats map. pkgs is pulled
7104 // from PackageManager data. Packages are only included in packageStats if there was
7105 // specific data tracked for them (services and wakeup alarms, etc.).
7106 for (String p : pkgs) {
7107 final long pToken = proto.start(UidProto.PACKAGES);
7108 proto.write(UidProto.Package.NAME, p);
7109 proto.end(pToken);
7110 }
7111
7112 // Total wakelock data (AGGREGATED_WAKELOCK_DATA)
7113 if (u.getAggregatedPartialWakelockTimer() != null) {
7114 final Timer timer = u.getAggregatedPartialWakelockTimer();
7115 // Times are since reset (regardless of 'which')
7116 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
7117 final Timer bgTimer = timer.getSubTimer();
7118 final long bgTimeMs = bgTimer != null
7119 ? bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
7120 final long awToken = proto.start(UidProto.AGGREGATED_WAKELOCK);
7121 proto.write(UidProto.AggregatedWakelock.PARTIAL_DURATION_MS, totTimeMs);
7122 proto.write(UidProto.AggregatedWakelock.BACKGROUND_PARTIAL_DURATION_MS, bgTimeMs);
7123 proto.end(awToken);
7124 }
7125
7126 // Audio (AUDIO_DATA)
7127 dumpTimer(proto, UidProto.AUDIO, u.getAudioTurnedOnTimer(), rawRealtimeUs, which);
7128
7129 // Bluetooth Controller (BLUETOOTH_CONTROLLER_DATA)
7130 dumpControllerActivityProto(proto, UidProto.BLUETOOTH_CONTROLLER,
7131 u.getBluetoothControllerActivity(), which);
7132
7133 // BLE scans (BLUETOOTH_MISC_DATA) (uses totalDurationMsLocked and MaxDurationMsLocked)
7134 final Timer bleTimer = u.getBluetoothScanTimer();
7135 if (bleTimer != null) {
7136 final long bmToken = proto.start(UidProto.BLUETOOTH_MISC);
7137
7138 dumpTimer(proto, UidProto.BluetoothMisc.APPORTIONED_BLE_SCAN, bleTimer,
7139 rawRealtimeUs, which);
7140 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN,
7141 u.getBluetoothScanBackgroundTimer(), rawRealtimeUs, which);
7142 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
7143 dumpTimer(proto, UidProto.BluetoothMisc.UNOPTIMIZED_BLE_SCAN,
7144 u.getBluetoothUnoptimizedScanTimer(), rawRealtimeUs, which);
7145 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
7146 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_UNOPTIMIZED_BLE_SCAN,
7147 u.getBluetoothUnoptimizedScanBackgroundTimer(), rawRealtimeUs, which);
7148 // Result counters
7149 proto.write(UidProto.BluetoothMisc.BLE_SCAN_RESULT_COUNT,
7150 u.getBluetoothScanResultCounter() != null
7151 ? u.getBluetoothScanResultCounter().getCountLocked(which) : 0);
7152 proto.write(UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN_RESULT_COUNT,
7153 u.getBluetoothScanResultBgCounter() != null
7154 ? u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0);
7155
7156 proto.end(bmToken);
7157 }
7158
7159 // Camera (CAMERA_DATA)
7160 dumpTimer(proto, UidProto.CAMERA, u.getCameraTurnedOnTimer(), rawRealtimeUs, which);
7161
7162 // CPU stats (CPU_DATA & CPU_TIMES_AT_FREQ_DATA)
7163 final long cpuToken = proto.start(UidProto.CPU);
7164 proto.write(UidProto.Cpu.USER_DURATION_MS, roundUsToMs(u.getUserCpuTimeUs(which)));
7165 proto.write(UidProto.Cpu.SYSTEM_DURATION_MS, roundUsToMs(u.getSystemCpuTimeUs(which)));
7166
7167 final long[] cpuFreqs = getCpuFreqs();
7168 if (cpuFreqs != null) {
7169 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
7170 // If total cpuFreqTimes is null, then we don't need to check for
7171 // screenOffCpuFreqTimes.
7172 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
7173 long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
7174 if (screenOffCpuFreqTimeMs == null) {
7175 screenOffCpuFreqTimeMs = new long[cpuFreqTimeMs.length];
7176 }
7177 for (int ic = 0; ic < cpuFreqTimeMs.length; ++ic) {
7178 long cToken = proto.start(UidProto.Cpu.BY_FREQUENCY);
7179 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7180 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7181 cpuFreqTimeMs[ic]);
7182 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7183 screenOffCpuFreqTimeMs[ic]);
7184 proto.end(cToken);
7185 }
7186 }
7187 }
Sudheer Shanka6d658d72018-01-01 01:36:49 -08007188
7189 for (int procState = 0; procState < Uid.NUM_PROCESS_STATE; ++procState) {
7190 final long[] timesMs = u.getCpuFreqTimes(which, procState);
7191 if (timesMs != null && timesMs.length == cpuFreqs.length) {
7192 long[] screenOffTimesMs = u.getScreenOffCpuFreqTimes(which, procState);
7193 if (screenOffTimesMs == null) {
7194 screenOffTimesMs = new long[timesMs.length];
7195 }
7196 final long procToken = proto.start(UidProto.Cpu.BY_PROCESS_STATE);
7197 proto.write(UidProto.Cpu.ByProcessState.PROCESS_STATE, procState);
7198 for (int ic = 0; ic < timesMs.length; ++ic) {
7199 long cToken = proto.start(UidProto.Cpu.ByProcessState.BY_FREQUENCY);
7200 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
7201 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
7202 timesMs[ic]);
7203 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
7204 screenOffTimesMs[ic]);
7205 proto.end(cToken);
7206 }
7207 proto.end(procToken);
7208 }
7209 }
Kweku Adams103351f2017-10-16 14:39:34 -07007210 proto.end(cpuToken);
7211
7212 // Flashlight (FLASHLIGHT_DATA)
7213 dumpTimer(proto, UidProto.FLASHLIGHT, u.getFlashlightTurnedOnTimer(),
7214 rawRealtimeUs, which);
7215
7216 // Foreground activity (FOREGROUND_ACTIVITY_DATA)
7217 dumpTimer(proto, UidProto.FOREGROUND_ACTIVITY, u.getForegroundActivityTimer(),
7218 rawRealtimeUs, which);
7219
7220 // Foreground service (FOREGROUND_SERVICE_DATA)
7221 dumpTimer(proto, UidProto.FOREGROUND_SERVICE, u.getForegroundServiceTimer(),
7222 rawRealtimeUs, which);
7223
7224 // Job completion (JOB_COMPLETION_DATA)
7225 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
7226 final int[] reasons = new int[]{
7227 JobParameters.REASON_CANCELED,
7228 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED,
7229 JobParameters.REASON_PREEMPT,
7230 JobParameters.REASON_TIMEOUT,
7231 JobParameters.REASON_DEVICE_IDLE,
7232 };
7233 for (int ic = 0; ic < completions.size(); ++ic) {
7234 SparseIntArray types = completions.valueAt(ic);
7235 if (types != null) {
7236 final long jcToken = proto.start(UidProto.JOB_COMPLETION);
7237
7238 proto.write(UidProto.JobCompletion.NAME, completions.keyAt(ic));
7239
7240 for (int r : reasons) {
7241 long rToken = proto.start(UidProto.JobCompletion.REASON_COUNT);
7242 proto.write(UidProto.JobCompletion.ReasonCount.NAME, r);
7243 proto.write(UidProto.JobCompletion.ReasonCount.COUNT, types.get(r, 0));
7244 proto.end(rToken);
7245 }
7246
7247 proto.end(jcToken);
7248 }
7249 }
7250
7251 // Scheduled jobs (JOB_DATA)
7252 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
7253 for (int ij = jobs.size() - 1; ij >= 0; --ij) {
7254 final Timer timer = jobs.valueAt(ij);
7255 final Timer bgTimer = timer.getSubTimer();
7256 final long jToken = proto.start(UidProto.JOBS);
7257
7258 proto.write(UidProto.Job.NAME, jobs.keyAt(ij));
7259 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7260 dumpTimer(proto, UidProto.Job.TOTAL, timer, rawRealtimeUs, which);
7261 dumpTimer(proto, UidProto.Job.BACKGROUND, bgTimer, rawRealtimeUs, which);
7262
7263 proto.end(jToken);
7264 }
7265
7266 // Modem Controller (MODEM_CONTROLLER_DATA)
7267 dumpControllerActivityProto(proto, UidProto.MODEM_CONTROLLER,
7268 u.getModemControllerActivity(), which);
7269
7270 // Network stats (NETWORK_DATA)
7271 final long nToken = proto.start(UidProto.NETWORK);
7272 proto.write(UidProto.Network.MOBILE_BYTES_RX,
7273 u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7274 proto.write(UidProto.Network.MOBILE_BYTES_TX,
7275 u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7276 proto.write(UidProto.Network.WIFI_BYTES_RX,
7277 u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7278 proto.write(UidProto.Network.WIFI_BYTES_TX,
7279 u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7280 proto.write(UidProto.Network.BT_BYTES_RX,
7281 u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7282 proto.write(UidProto.Network.BT_BYTES_TX,
7283 u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
7284 proto.write(UidProto.Network.MOBILE_PACKETS_RX,
7285 u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7286 proto.write(UidProto.Network.MOBILE_PACKETS_TX,
7287 u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7288 proto.write(UidProto.Network.WIFI_PACKETS_RX,
7289 u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7290 proto.write(UidProto.Network.WIFI_PACKETS_TX,
7291 u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7292 proto.write(UidProto.Network.MOBILE_ACTIVE_DURATION_MS,
7293 roundUsToMs(u.getMobileRadioActiveTime(which)));
7294 proto.write(UidProto.Network.MOBILE_ACTIVE_COUNT,
7295 u.getMobileRadioActiveCount(which));
7296 proto.write(UidProto.Network.MOBILE_WAKEUP_COUNT,
7297 u.getMobileRadioApWakeupCount(which));
7298 proto.write(UidProto.Network.WIFI_WAKEUP_COUNT,
7299 u.getWifiRadioApWakeupCount(which));
7300 proto.write(UidProto.Network.MOBILE_BYTES_BG_RX,
7301 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA, which));
7302 proto.write(UidProto.Network.MOBILE_BYTES_BG_TX,
7303 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA, which));
7304 proto.write(UidProto.Network.WIFI_BYTES_BG_RX,
7305 u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which));
7306 proto.write(UidProto.Network.WIFI_BYTES_BG_TX,
7307 u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which));
7308 proto.write(UidProto.Network.MOBILE_PACKETS_BG_RX,
7309 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA, which));
7310 proto.write(UidProto.Network.MOBILE_PACKETS_BG_TX,
7311 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA, which));
7312 proto.write(UidProto.Network.WIFI_PACKETS_BG_RX,
7313 u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA, which));
7314 proto.write(UidProto.Network.WIFI_PACKETS_BG_TX,
7315 u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA, which));
7316 proto.end(nToken);
7317
7318 // Power use item (POWER_USE_ITEM_DATA)
7319 BatterySipper bs = uidToSipper.get(uid);
7320 if (bs != null) {
7321 final long bsToken = proto.start(UidProto.POWER_USE_ITEM);
7322 proto.write(UidProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7323 proto.write(UidProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7324 proto.write(UidProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7325 proto.write(UidProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7326 bs.proportionalSmearMah);
7327 proto.end(bsToken);
7328 }
7329
7330 // Processes (PROCESS_DATA)
7331 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats =
7332 u.getProcessStats();
7333 for (int ipr = processStats.size() - 1; ipr >= 0; --ipr) {
7334 final Uid.Proc ps = processStats.valueAt(ipr);
7335 final long prToken = proto.start(UidProto.PROCESS);
7336
7337 proto.write(UidProto.Process.NAME, processStats.keyAt(ipr));
7338 proto.write(UidProto.Process.USER_DURATION_MS, ps.getUserTime(which));
7339 proto.write(UidProto.Process.SYSTEM_DURATION_MS, ps.getSystemTime(which));
7340 proto.write(UidProto.Process.FOREGROUND_DURATION_MS, ps.getForegroundTime(which));
7341 proto.write(UidProto.Process.START_COUNT, ps.getStarts(which));
7342 proto.write(UidProto.Process.ANR_COUNT, ps.getNumAnrs(which));
7343 proto.write(UidProto.Process.CRASH_COUNT, ps.getNumCrashes(which));
7344
7345 proto.end(prToken);
7346 }
7347
7348 // Sensors (SENSOR_DATA)
7349 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
7350 for (int ise = 0; ise < sensors.size(); ++ise) {
7351 final Uid.Sensor se = sensors.valueAt(ise);
7352 final Timer timer = se.getSensorTime();
7353 if (timer == null) {
7354 continue;
7355 }
7356 final Timer bgTimer = se.getSensorBackgroundTime();
7357 final int sensorNumber = sensors.keyAt(ise);
7358 final long seToken = proto.start(UidProto.SENSORS);
7359
7360 proto.write(UidProto.Sensor.ID, sensorNumber);
7361 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7362 dumpTimer(proto, UidProto.Sensor.APPORTIONED, timer, rawRealtimeUs, which);
7363 dumpTimer(proto, UidProto.Sensor.BACKGROUND, bgTimer, rawRealtimeUs, which);
7364
7365 proto.end(seToken);
7366 }
7367
7368 // State times (STATE_TIME_DATA)
7369 for (int ips = 0; ips < Uid.NUM_PROCESS_STATE; ++ips) {
7370 long durMs = roundUsToMs(u.getProcessStateTime(ips, rawRealtimeUs, which));
7371 if (durMs == 0) {
7372 continue;
7373 }
7374 final long stToken = proto.start(UidProto.STATES);
7375 proto.write(UidProto.StateTime.STATE, ips);
7376 proto.write(UidProto.StateTime.DURATION_MS, durMs);
7377 proto.end(stToken);
7378 }
7379
7380 // Syncs (SYNC_DATA)
7381 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
7382 for (int isy = syncs.size() - 1; isy >= 0; --isy) {
7383 final Timer timer = syncs.valueAt(isy);
7384 final Timer bgTimer = timer.getSubTimer();
7385 final long syToken = proto.start(UidProto.SYNCS);
7386
7387 proto.write(UidProto.Sync.NAME, syncs.keyAt(isy));
7388 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7389 dumpTimer(proto, UidProto.Sync.TOTAL, timer, rawRealtimeUs, which);
7390 dumpTimer(proto, UidProto.Sync.BACKGROUND, bgTimer, rawRealtimeUs, which);
7391
7392 proto.end(syToken);
7393 }
7394
7395 // User activity (USER_ACTIVITY_DATA)
7396 if (u.hasUserActivity()) {
7397 for (int i = 0; i < Uid.NUM_USER_ACTIVITY_TYPES; ++i) {
7398 int val = u.getUserActivityCount(i, which);
7399 if (val != 0) {
7400 final long uaToken = proto.start(UidProto.USER_ACTIVITY);
7401 proto.write(UidProto.UserActivity.NAME, i);
7402 proto.write(UidProto.UserActivity.COUNT, val);
7403 proto.end(uaToken);
7404 }
7405 }
7406 }
7407
7408 // Vibrator (VIBRATOR_DATA)
7409 dumpTimer(proto, UidProto.VIBRATOR, u.getVibratorOnTimer(), rawRealtimeUs, which);
7410
7411 // Video (VIDEO_DATA)
7412 dumpTimer(proto, UidProto.VIDEO, u.getVideoTurnedOnTimer(), rawRealtimeUs, which);
7413
7414 // Wakelocks (WAKELOCK_DATA)
7415 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
7416 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7417 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7418 final long wToken = proto.start(UidProto.WAKELOCKS);
7419 proto.write(UidProto.Wakelock.NAME, wakelocks.keyAt(iw));
7420 dumpTimer(proto, UidProto.Wakelock.FULL, wl.getWakeTime(WAKE_TYPE_FULL),
7421 rawRealtimeUs, which);
7422 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7423 if (pTimer != null) {
7424 dumpTimer(proto, UidProto.Wakelock.PARTIAL, pTimer, rawRealtimeUs, which);
7425 dumpTimer(proto, UidProto.Wakelock.BACKGROUND_PARTIAL, pTimer.getSubTimer(),
7426 rawRealtimeUs, which);
7427 }
7428 dumpTimer(proto, UidProto.Wakelock.WINDOW, wl.getWakeTime(WAKE_TYPE_WINDOW),
7429 rawRealtimeUs, which);
7430 proto.end(wToken);
7431 }
7432
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007433 // Wifi Multicast Wakelock (WIFI_MULTICAST_WAKELOCK_DATA)
7434 dumpTimer(proto, UidProto.WIFI_MULTICAST_WAKELOCK, u.getMulticastWakelockStats(),
7435 rawRealtimeUs, which);
7436
Kweku Adams103351f2017-10-16 14:39:34 -07007437 // Wakeup alarms (WAKEUP_ALARM_DATA)
7438 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7439 final Uid.Pkg ps = packageStats.valueAt(ipkg);
7440 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
7441 for (int iwa = alarms.size() - 1; iwa >= 0; --iwa) {
7442 final long waToken = proto.start(UidProto.WAKEUP_ALARM);
7443 proto.write(UidProto.WakeupAlarm.NAME, alarms.keyAt(iwa));
7444 proto.write(UidProto.WakeupAlarm.COUNT,
7445 alarms.valueAt(iwa).getCountLocked(which));
7446 proto.end(waToken);
7447 }
7448 }
7449
7450 // Wifi Controller (WIFI_CONTROLLER_DATA)
7451 dumpControllerActivityProto(proto, UidProto.WIFI_CONTROLLER,
7452 u.getWifiControllerActivity(), which);
7453
7454 // Wifi data (WIFI_DATA)
7455 final long wToken = proto.start(UidProto.WIFI);
7456 proto.write(UidProto.Wifi.FULL_WIFI_LOCK_DURATION_MS,
7457 roundUsToMs(u.getFullWifiLockTime(rawRealtimeUs, which)));
7458 dumpTimer(proto, UidProto.Wifi.APPORTIONED_SCAN, u.getWifiScanTimer(),
7459 rawRealtimeUs, which);
7460 proto.write(UidProto.Wifi.RUNNING_DURATION_MS,
7461 roundUsToMs(u.getWifiRunningTime(rawRealtimeUs, which)));
7462 dumpTimer(proto, UidProto.Wifi.BACKGROUND_SCAN, u.getWifiScanBackgroundTimer(),
7463 rawRealtimeUs, which);
7464 proto.end(wToken);
7465
7466 proto.end(uTkn);
7467 }
7468 }
7469
7470 private void dumpProtoSystemLocked(ProtoOutputStream proto, BatteryStatsHelper helper) {
Kweku Adams87b19ec2017-10-09 12:40:03 -07007471 final long sToken = proto.start(BatteryStatsProto.SYSTEM);
7472 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7473 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7474 final long rawRealtimeUs = rawRealtimeMs * 1000;
7475 final int which = STATS_SINCE_CHARGED;
7476
7477 // Battery data (BATTERY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007478 final long bToken = proto.start(SystemProto.BATTERY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007479 proto.write(SystemProto.Battery.START_CLOCK_TIME_MS, getStartClockTime());
7480 proto.write(SystemProto.Battery.START_COUNT, getStartCount());
7481 proto.write(SystemProto.Battery.TOTAL_REALTIME_MS,
7482 computeRealtime(rawRealtimeUs, which) / 1000);
7483 proto.write(SystemProto.Battery.TOTAL_UPTIME_MS,
7484 computeUptime(rawUptimeUs, which) / 1000);
7485 proto.write(SystemProto.Battery.BATTERY_REALTIME_MS,
7486 computeBatteryRealtime(rawRealtimeUs, which) / 1000);
7487 proto.write(SystemProto.Battery.BATTERY_UPTIME_MS,
7488 computeBatteryUptime(rawUptimeUs, which) / 1000);
7489 proto.write(SystemProto.Battery.SCREEN_OFF_REALTIME_MS,
7490 computeBatteryScreenOffRealtime(rawRealtimeUs, which) / 1000);
7491 proto.write(SystemProto.Battery.SCREEN_OFF_UPTIME_MS,
7492 computeBatteryScreenOffUptime(rawUptimeUs, which) / 1000);
7493 proto.write(SystemProto.Battery.SCREEN_DOZE_DURATION_MS,
7494 getScreenDozeTime(rawRealtimeUs, which) / 1000);
7495 proto.write(SystemProto.Battery.ESTIMATED_BATTERY_CAPACITY_MAH,
7496 getEstimatedBatteryCapacity());
7497 proto.write(SystemProto.Battery.MIN_LEARNED_BATTERY_CAPACITY_UAH,
7498 getMinLearnedBatteryCapacity());
7499 proto.write(SystemProto.Battery.MAX_LEARNED_BATTERY_CAPACITY_UAH,
7500 getMaxLearnedBatteryCapacity());
Kweku Adams103351f2017-10-16 14:39:34 -07007501 proto.end(bToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007502
7503 // Battery discharge (BATTERY_DISCHARGE_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007504 final long bdToken = proto.start(SystemProto.BATTERY_DISCHARGE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007505 proto.write(SystemProto.BatteryDischarge.LOWER_BOUND_SINCE_CHARGE,
7506 getLowDischargeAmountSinceCharge());
7507 proto.write(SystemProto.BatteryDischarge.UPPER_BOUND_SINCE_CHARGE,
7508 getHighDischargeAmountSinceCharge());
7509 proto.write(SystemProto.BatteryDischarge.SCREEN_ON_SINCE_CHARGE,
7510 getDischargeAmountScreenOnSinceCharge());
7511 proto.write(SystemProto.BatteryDischarge.SCREEN_OFF_SINCE_CHARGE,
7512 getDischargeAmountScreenOffSinceCharge());
7513 proto.write(SystemProto.BatteryDischarge.SCREEN_DOZE_SINCE_CHARGE,
7514 getDischargeAmountScreenDozeSinceCharge());
7515 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH,
7516 getUahDischarge(which) / 1000);
7517 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_OFF,
7518 getUahDischargeScreenOff(which) / 1000);
7519 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_DOZE,
7520 getUahDischargeScreenDoze(which) / 1000);
Mike Ma15313c92017-11-15 17:58:21 -08007521 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_LIGHT_DOZE,
7522 getUahDischargeLightDoze(which) / 1000);
7523 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_DEEP_DOZE,
7524 getUahDischargeDeepDoze(which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007525 proto.end(bdToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007526
7527 // Time remaining
7528 long timeRemainingUs = computeChargeTimeRemaining(rawRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -07007529 // These are part of a oneof, so we should only set one of them.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007530 if (timeRemainingUs >= 0) {
7531 // Charge time remaining (CHARGE_TIME_REMAIN_DATA)
7532 proto.write(SystemProto.CHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7533 } else {
7534 timeRemainingUs = computeBatteryTimeRemaining(rawRealtimeUs);
7535 // Discharge time remaining (DISCHARGE_TIME_REMAIN_DATA)
7536 if (timeRemainingUs >= 0) {
7537 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7538 } else {
7539 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, -1);
7540 }
7541 }
7542
7543 // Charge step (CHARGE_STEP_DATA)
7544 dumpDurationSteps(proto, SystemProto.CHARGE_STEP, getChargeLevelStepTracker());
7545
7546 // Phone data connection (DATA_CONNECTION_TIME_DATA and DATA_CONNECTION_COUNT_DATA)
7547 for (int i = 0; i < NUM_DATA_CONNECTION_TYPES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007548 final long pdcToken = proto.start(SystemProto.DATA_CONNECTION);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007549 proto.write(SystemProto.DataConnection.NAME, i);
7550 dumpTimer(proto, SystemProto.DataConnection.TOTAL, getPhoneDataConnectionTimer(i),
7551 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007552 proto.end(pdcToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007553 }
7554
7555 // Discharge step (DISCHARGE_STEP_DATA)
7556 dumpDurationSteps(proto, SystemProto.DISCHARGE_STEP, getDischargeLevelStepTracker());
7557
7558 // CPU frequencies (GLOBAL_CPU_FREQ_DATA)
7559 final long[] cpuFreqs = getCpuFreqs();
7560 if (cpuFreqs != null) {
7561 for (long i : cpuFreqs) {
7562 proto.write(SystemProto.CPU_FREQUENCY, i);
7563 }
7564 }
7565
7566 // Bluetooth controller (GLOBAL_BLUETOOTH_CONTROLLER_DATA)
7567 dumpControllerActivityProto(proto, SystemProto.GLOBAL_BLUETOOTH_CONTROLLER,
7568 getBluetoothControllerActivity(), which);
7569
7570 // Modem controller (GLOBAL_MODEM_CONTROLLER_DATA)
7571 dumpControllerActivityProto(proto, SystemProto.GLOBAL_MODEM_CONTROLLER,
7572 getModemControllerActivity(), which);
7573
7574 // Global network data (GLOBAL_NETWORK_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007575 final long gnToken = proto.start(SystemProto.GLOBAL_NETWORK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007576 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_RX,
7577 getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7578 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_TX,
7579 getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7580 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_RX,
7581 getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7582 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_TX,
7583 getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7584 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_RX,
7585 getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7586 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_TX,
7587 getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7588 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_RX,
7589 getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7590 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_TX,
7591 getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7592 proto.write(SystemProto.GlobalNetwork.BT_BYTES_RX,
7593 getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7594 proto.write(SystemProto.GlobalNetwork.BT_BYTES_TX,
7595 getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
Kweku Adams103351f2017-10-16 14:39:34 -07007596 proto.end(gnToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007597
7598 // Wifi controller (GLOBAL_WIFI_CONTROLLER_DATA)
7599 dumpControllerActivityProto(proto, SystemProto.GLOBAL_WIFI_CONTROLLER,
7600 getWifiControllerActivity(), which);
7601
7602
7603 // Global wifi (GLOBAL_WIFI_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007604 final long gwToken = proto.start(SystemProto.GLOBAL_WIFI);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007605 proto.write(SystemProto.GlobalWifi.ON_DURATION_MS,
7606 getWifiOnTime(rawRealtimeUs, which) / 1000);
7607 proto.write(SystemProto.GlobalWifi.RUNNING_DURATION_MS,
7608 getGlobalWifiRunningTime(rawRealtimeUs, which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007609 proto.end(gwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007610
7611 // Kernel wakelock (KERNEL_WAKELOCK_DATA)
7612 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
7613 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007614 final long kwToken = proto.start(SystemProto.KERNEL_WAKELOCK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007615 proto.write(SystemProto.KernelWakelock.NAME, ent.getKey());
7616 dumpTimer(proto, SystemProto.KernelWakelock.TOTAL, ent.getValue(),
7617 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007618 proto.end(kwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007619 }
7620
7621 // Misc (MISC_DATA)
7622 // Calculate wakelock times across all uids.
7623 long fullWakeLockTimeTotalUs = 0;
7624 long partialWakeLockTimeTotalUs = 0;
7625
7626 final SparseArray<? extends Uid> uidStats = getUidStats();
7627 for (int iu = 0; iu < uidStats.size(); iu++) {
7628 final Uid u = uidStats.valueAt(iu);
7629
7630 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks =
7631 u.getWakelockStats();
7632 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7633 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7634
7635 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
7636 if (fullWakeTimer != null) {
7637 fullWakeLockTimeTotalUs += fullWakeTimer.getTotalTimeLocked(rawRealtimeUs,
7638 which);
7639 }
7640
7641 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7642 if (partialWakeTimer != null) {
7643 partialWakeLockTimeTotalUs += partialWakeTimer.getTotalTimeLocked(
7644 rawRealtimeUs, which);
7645 }
7646 }
7647 }
Kweku Adams103351f2017-10-16 14:39:34 -07007648 final long mToken = proto.start(SystemProto.MISC);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007649 proto.write(SystemProto.Misc.SCREEN_ON_DURATION_MS,
7650 getScreenOnTime(rawRealtimeUs, which) / 1000);
7651 proto.write(SystemProto.Misc.PHONE_ON_DURATION_MS,
7652 getPhoneOnTime(rawRealtimeUs, which) / 1000);
7653 proto.write(SystemProto.Misc.FULL_WAKELOCK_TOTAL_DURATION_MS,
7654 fullWakeLockTimeTotalUs / 1000);
7655 proto.write(SystemProto.Misc.PARTIAL_WAKELOCK_TOTAL_DURATION_MS,
7656 partialWakeLockTimeTotalUs / 1000);
7657 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_DURATION_MS,
7658 getMobileRadioActiveTime(rawRealtimeUs, which) / 1000);
7659 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_ADJUSTED_TIME_MS,
7660 getMobileRadioActiveAdjustedTime(which) / 1000);
7661 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_COUNT,
7662 getMobileRadioActiveCount(which));
7663 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_UNKNOWN_DURATION_MS,
7664 getMobileRadioActiveUnknownTime(which) / 1000);
7665 proto.write(SystemProto.Misc.INTERACTIVE_DURATION_MS,
7666 getInteractiveTime(rawRealtimeUs, which) / 1000);
7667 proto.write(SystemProto.Misc.BATTERY_SAVER_MODE_ENABLED_DURATION_MS,
7668 getPowerSaveModeEnabledTime(rawRealtimeUs, which) / 1000);
7669 proto.write(SystemProto.Misc.NUM_CONNECTIVITY_CHANGES,
7670 getNumConnectivityChange(which));
7671 proto.write(SystemProto.Misc.DEEP_DOZE_ENABLED_DURATION_MS,
7672 getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7673 proto.write(SystemProto.Misc.DEEP_DOZE_COUNT,
7674 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
7675 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_DURATION_MS,
7676 getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7677 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_COUNT,
7678 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
7679 proto.write(SystemProto.Misc.LONGEST_DEEP_DOZE_DURATION_MS,
7680 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
7681 proto.write(SystemProto.Misc.LIGHT_DOZE_ENABLED_DURATION_MS,
7682 getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7683 proto.write(SystemProto.Misc.LIGHT_DOZE_COUNT,
7684 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
7685 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_DURATION_MS,
7686 getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7687 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_COUNT,
7688 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
7689 proto.write(SystemProto.Misc.LONGEST_LIGHT_DOZE_DURATION_MS,
7690 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
Kweku Adams103351f2017-10-16 14:39:34 -07007691 proto.end(mToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007692
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007693 // Wifi multicast wakelock total stats (WIFI_MULTICAST_WAKELOCK_TOTAL_DATA)
Ahmed ElArabawyf88571f2017-11-28 12:18:10 -08007694 final long multicastWakeLockTimeTotalUs =
7695 getWifiMulticastWakelockTime(rawRealtimeUs, which);
7696 final int multicastWakeLockCountTotal = getWifiMulticastWakelockCount(which);
Ahmed ElArabawyddd09692017-10-30 17:58:29 -07007697 final long wmctToken = proto.start(SystemProto.WIFI_MULTICAST_WAKELOCK_TOTAL);
7698 proto.write(SystemProto.WifiMulticastWakelockTotal.DURATION_MS,
7699 multicastWakeLockTimeTotalUs / 1000);
7700 proto.write(SystemProto.WifiMulticastWakelockTotal.COUNT,
7701 multicastWakeLockCountTotal);
7702 proto.end(wmctToken);
7703
Kweku Adams87b19ec2017-10-09 12:40:03 -07007704 // Power use item (POWER_USE_ITEM_DATA)
7705 final List<BatterySipper> sippers = helper.getUsageList();
7706 if (sippers != null) {
7707 for (int i = 0; i < sippers.size(); ++i) {
7708 final BatterySipper bs = sippers.get(i);
7709 int n = SystemProto.PowerUseItem.UNKNOWN_SIPPER;
7710 int uid = 0;
7711 switch (bs.drainType) {
7712 case IDLE:
7713 n = SystemProto.PowerUseItem.IDLE;
7714 break;
7715 case CELL:
7716 n = SystemProto.PowerUseItem.CELL;
7717 break;
7718 case PHONE:
7719 n = SystemProto.PowerUseItem.PHONE;
7720 break;
7721 case WIFI:
7722 n = SystemProto.PowerUseItem.WIFI;
7723 break;
7724 case BLUETOOTH:
7725 n = SystemProto.PowerUseItem.BLUETOOTH;
7726 break;
7727 case SCREEN:
7728 n = SystemProto.PowerUseItem.SCREEN;
7729 break;
7730 case FLASHLIGHT:
7731 n = SystemProto.PowerUseItem.FLASHLIGHT;
7732 break;
7733 case APP:
Kweku Adams103351f2017-10-16 14:39:34 -07007734 // dumpProtoAppsLocked will handle this.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007735 continue;
7736 case USER:
7737 n = SystemProto.PowerUseItem.USER;
7738 uid = UserHandle.getUid(bs.userId, 0);
7739 break;
7740 case UNACCOUNTED:
7741 n = SystemProto.PowerUseItem.UNACCOUNTED;
7742 break;
7743 case OVERCOUNTED:
7744 n = SystemProto.PowerUseItem.OVERCOUNTED;
7745 break;
7746 case CAMERA:
7747 n = SystemProto.PowerUseItem.CAMERA;
7748 break;
7749 case MEMORY:
7750 n = SystemProto.PowerUseItem.MEMORY;
7751 break;
7752 }
Kweku Adams103351f2017-10-16 14:39:34 -07007753 final long puiToken = proto.start(SystemProto.POWER_USE_ITEM);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007754 proto.write(SystemProto.PowerUseItem.NAME, n);
7755 proto.write(SystemProto.PowerUseItem.UID, uid);
7756 proto.write(SystemProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7757 proto.write(SystemProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7758 proto.write(SystemProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7759 proto.write(SystemProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7760 bs.proportionalSmearMah);
Kweku Adams103351f2017-10-16 14:39:34 -07007761 proto.end(puiToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007762 }
7763 }
7764
7765 // Power use summary (POWER_USE_SUMMARY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007766 final long pusToken = proto.start(SystemProto.POWER_USE_SUMMARY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007767 proto.write(SystemProto.PowerUseSummary.BATTERY_CAPACITY_MAH,
7768 helper.getPowerProfile().getBatteryCapacity());
7769 proto.write(SystemProto.PowerUseSummary.COMPUTED_POWER_MAH, helper.getComputedPower());
7770 proto.write(SystemProto.PowerUseSummary.MIN_DRAINED_POWER_MAH, helper.getMinDrainedPower());
7771 proto.write(SystemProto.PowerUseSummary.MAX_DRAINED_POWER_MAH, helper.getMaxDrainedPower());
Kweku Adams103351f2017-10-16 14:39:34 -07007772 proto.end(pusToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007773
7774 // RPM stats (RESOURCE_POWER_MANAGER_DATA)
7775 final Map<String, ? extends Timer> rpmStats = getRpmStats();
7776 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
7777 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007778 final long rpmToken = proto.start(SystemProto.RESOURCE_POWER_MANAGER);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007779 proto.write(SystemProto.ResourcePowerManager.NAME, ent.getKey());
7780 dumpTimer(proto, SystemProto.ResourcePowerManager.TOTAL,
7781 ent.getValue(), rawRealtimeUs, which);
7782 dumpTimer(proto, SystemProto.ResourcePowerManager.SCREEN_OFF,
7783 screenOffRpmStats.get(ent.getKey()), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007784 proto.end(rpmToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007785 }
7786
7787 // Screen brightness (SCREEN_BRIGHTNESS_DATA)
7788 for (int i = 0; i < NUM_SCREEN_BRIGHTNESS_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007789 final long sbToken = proto.start(SystemProto.SCREEN_BRIGHTNESS);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007790 proto.write(SystemProto.ScreenBrightness.NAME, i);
7791 dumpTimer(proto, SystemProto.ScreenBrightness.TOTAL, getScreenBrightnessTimer(i),
7792 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007793 proto.end(sbToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007794 }
7795
7796 // Signal scanning time (SIGNAL_SCANNING_TIME_DATA)
7797 dumpTimer(proto, SystemProto.SIGNAL_SCANNING, getPhoneSignalScanningTimer(), rawRealtimeUs,
7798 which);
7799
7800 // Phone signal strength (SIGNAL_STRENGTH_TIME_DATA and SIGNAL_STRENGTH_COUNT_DATA)
7801 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007802 final long pssToken = proto.start(SystemProto.PHONE_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007803 proto.write(SystemProto.PhoneSignalStrength.NAME, i);
7804 dumpTimer(proto, SystemProto.PhoneSignalStrength.TOTAL, getPhoneSignalStrengthTimer(i),
7805 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007806 proto.end(pssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007807 }
7808
7809 // Wakeup reasons (WAKEUP_REASON_DATA)
7810 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
7811 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007812 final long wrToken = proto.start(SystemProto.WAKEUP_REASON);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007813 proto.write(SystemProto.WakeupReason.NAME, ent.getKey());
7814 dumpTimer(proto, SystemProto.WakeupReason.TOTAL, ent.getValue(), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007815 proto.end(wrToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007816 }
7817
7818 // Wifi signal strength (WIFI_SIGNAL_STRENGTH_TIME_DATA and WIFI_SIGNAL_STRENGTH_COUNT_DATA)
7819 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007820 final long wssToken = proto.start(SystemProto.WIFI_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007821 proto.write(SystemProto.WifiSignalStrength.NAME, i);
7822 dumpTimer(proto, SystemProto.WifiSignalStrength.TOTAL, getWifiSignalStrengthTimer(i),
7823 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007824 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007825 }
7826
7827 // Wifi state (WIFI_STATE_TIME_DATA and WIFI_STATE_COUNT_DATA)
7828 for (int i = 0; i < NUM_WIFI_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007829 final long wsToken = proto.start(SystemProto.WIFI_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007830 proto.write(SystemProto.WifiState.NAME, i);
7831 dumpTimer(proto, SystemProto.WifiState.TOTAL, getWifiStateTimer(i),
7832 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007833 proto.end(wsToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007834 }
7835
7836 // Wifi supplicant state (WIFI_SUPPL_STATE_TIME_DATA and WIFI_SUPPL_STATE_COUNT_DATA)
7837 for (int i = 0; i < NUM_WIFI_SUPPL_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007838 final long wssToken = proto.start(SystemProto.WIFI_SUPPLICANT_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007839 proto.write(SystemProto.WifiSupplicantState.NAME, i);
7840 dumpTimer(proto, SystemProto.WifiSupplicantState.TOTAL, getWifiSupplStateTimer(i),
7841 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007842 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007843 }
7844
7845 proto.end(sToken);
7846 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007847}