blob: 988192797f5984149aa902889b320a5ea58c1337 [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
Dianne Hackborn94326cb2017-06-28 16:17:20 -070019import android.app.job.JobParameters;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080020import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080021import android.content.pm.ApplicationInfo;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070022import android.service.batterystats.BatteryStatsServiceDumpProto;
Wink Saville52840902011-02-18 12:40:47 -080023import android.telephony.SignalStrength;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080024import android.text.format.DateFormat;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070025import android.util.ArrayMap;
James Carr2dd7e5e2016-07-20 18:48:39 -070026import android.util.LongSparseArray;
Dianne Hackborn9cfba352016-03-24 17:31:28 -070027import android.util.MutableBoolean;
28import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.util.Printer;
30import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070031import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070032import android.util.TimeUtils;
Kweku Adams2f73ecd2017-09-27 16:59:19 -070033import android.util.proto.ProtoOutputStream;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070034import android.view.Display;
Amith Yamasaniab9ad192016-12-06 12:46:59 -080035
Dianne Hackborna7c837f2014-01-15 16:20:44 -080036import com.android.internal.os.BatterySipper;
37import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
Kweku Adams2f73ecd2017-09-27 16:59:19 -070039import java.io.FileDescriptor;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070040import java.io.PrintWriter;
41import java.util.ArrayList;
42import java.util.Collections;
43import java.util.Comparator;
44import java.util.Formatter;
45import java.util.HashMap;
46import java.util.List;
47import java.util.Map;
48
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049/**
50 * A class providing access to battery usage statistics, including information on
51 * wakelocks, processes, packages, and services. All times are represented in microseconds
52 * except where indicated otherwise.
53 * @hide
54 */
55public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070056 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
58 private static final boolean LOCAL_LOGV = false;
Bookatz82b341172017-09-07 19:06:08 -070059 /** Fetching RPM stats is too slow to do each time screen changes, so disable it. */
60 protected static final boolean SCREEN_OFF_RPM_STATS_ENABLED = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070061
62 /** @hide */
63 public static final String SERVICE_NAME = "batterystats";
64
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 /**
66 * A constant indicating a partial wake lock timer.
67 */
68 public static final int WAKE_TYPE_PARTIAL = 0;
69
70 /**
71 * A constant indicating a full wake lock timer.
72 */
73 public static final int WAKE_TYPE_FULL = 1;
74
75 /**
76 * A constant indicating a window wake lock timer.
77 */
78 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 /**
81 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 */
83 public static final int SENSOR = 3;
Mike Mac2f518a2017-09-19 16:06:03 -070084
The Android Open Source Project10592532009-03-18 17:39:46 -070085 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070086 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070087 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070088 public static final int WIFI_RUNNING = 4;
Mike Mac2f518a2017-09-19 16:06:03 -070089
Dianne Hackborn617f8772009-03-31 15:04:46 -070090 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070091 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070092 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070093 public static final int FULL_WIFI_LOCK = 5;
Mike Mac2f518a2017-09-19 16:06:03 -070094
The Android Open Source Project10592532009-03-18 17:39:46 -070095 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070096 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070097 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070098 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099
Dianne Hackborn62793e42015-03-09 11:15:41 -0700100 /**
101 * A constant indicating a wifi multicast timer
102 */
103 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700106 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700107 */
108 public static final int VIDEO_TURNED_ON = 8;
109
110 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800111 * A constant indicating a vibrator on timer
112 */
113 public static final int VIBRATOR_ON = 9;
114
115 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700116 * A constant indicating a foreground activity timer
117 */
118 public static final int FOREGROUND_ACTIVITY = 10;
119
120 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700121 * A constant indicating a wifi batched scan is active
122 */
123 public static final int WIFI_BATCHED_SCAN = 11;
124
125 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700126 * A constant indicating a process state timer
127 */
128 public static final int PROCESS_STATE = 12;
129
130 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700131 * A constant indicating a sync timer
132 */
133 public static final int SYNC = 13;
134
135 /**
136 * A constant indicating a job timer
137 */
138 public static final int JOB = 14;
139
140 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800141 * A constant indicating an audio turn on timer
142 */
143 public static final int AUDIO_TURNED_ON = 15;
144
145 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700146 * A constant indicating a flashlight turn on timer
147 */
148 public static final int FLASHLIGHT_TURNED_ON = 16;
149
150 /**
151 * A constant indicating a camera turn on timer
152 */
153 public static final int CAMERA_TURNED_ON = 17;
154
155 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700156 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700157 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700158 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700159
160 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800161 * A constant indicating a bluetooth scan timer.
162 */
163 public static final int BLUETOOTH_SCAN_ON = 19;
164
165 /**
Bookatzc8c44962017-05-11 12:12:54 -0700166 * A constant indicating an aggregated partial wake lock timer.
167 */
168 public static final int AGGREGATED_WAKE_TYPE_PARTIAL = 20;
169
170 /**
Bookatzb1f04f32017-05-19 13:57:32 -0700171 * A constant indicating a bluetooth scan timer for unoptimized scans.
172 */
173 public static final int BLUETOOTH_UNOPTIMIZED_SCAN_ON = 21;
174
175 /**
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700176 * A constant indicating a foreground service timer
177 */
178 public static final int FOREGROUND_SERVICE = 22;
179
180 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * Include all of the data in the stats, including previously saved data.
182 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700183 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184
185 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 * Include only the current run in the stats.
187 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700188 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189
190 /**
191 * Include only the run since the last time the device was unplugged in the stats.
192 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700193 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700194
195 // NOTE: Update this list if you add/change any stats above.
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700196 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700197 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700198 private static final String[] STAT_NAMES = { "l", "c", "u" };
199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700201 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700202 *
203 * New in version 19:
204 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800205 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700206 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Bookatz506a8182017-05-01 14:18:42 -0700207 * New in version 21:
208 * - Actual (not just apportioned) Wakelock time is also recorded.
Bookatzc8c44962017-05-11 12:12:54 -0700209 * - Aggregated partial wakelock time (per uid, instead of per wakelock) is recorded.
Bookatzb1f04f32017-05-19 13:57:32 -0700210 * - BLE scan result count
211 * - CPU frequency time per uid
212 * New in version 22:
213 * - BLE scan result background count, BLE unoptimized scan time
Bookatz98d4d5c2017-08-01 19:07:54 -0700214 * - Background partial wakelock time & count
215 * New in version 23:
216 * - Logging smeared power model values
217 * New in version 24:
218 * - Fixed bugs in background timers and BLE scan time
219 * New in version 25:
220 * - Package wakeup alarms are now on screen-off timebase
Bookatz50df7112017-08-04 14:53:26 -0700221 * New in version 26:
Bookatz82b341172017-09-07 19:06:08 -0700222 * - Resource power manager (rpm) states [but screenOffRpm is disabled from working properly]
Mike Mac2f518a2017-09-19 16:06:03 -0700223 * New in version 27:
224 * - Always On Display (screen doze mode) time and power
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700225 */
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700226 static final int CHECKIN_VERSION = 27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700227
228 /**
229 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700231 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700232
Evan Millar22ac0432009-03-31 11:33:18 -0700233 private static final long BYTES_PER_KB = 1024;
234 private static final long BYTES_PER_MB = 1048576; // 1024^2
235 private static final long BYTES_PER_GB = 1073741824; //1024^3
Bookatz506a8182017-05-01 14:18:42 -0700236
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700237 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800238 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700239 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700241 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700242 private static final String CPU_DATA = "cpu";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700243 private static final String GLOBAL_CPU_FREQ_DATA = "gcf";
244 private static final String CPU_TIMES_AT_FREQ_DATA = "ctf";
Bookatz50df7112017-08-04 14:53:26 -0700245 // rpm line is:
246 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "rpm", state/voter name, total time, total count,
247 // screen-off time, screen-off count
248 private static final String RESOURCE_POWER_MANAGER_DATA = "rpm";
Evan Millare84de8d2009-04-02 22:16:12 -0700249 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800250 private static final String VIBRATOR_DATA = "vib";
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700251 private static final String FOREGROUND_ACTIVITY_DATA = "fg";
252 // fgs line is:
253 // BATTERY_STATS_CHECKIN_VERSION, uid, category, "fgs",
254 // foreground service time, count
255 private static final String FOREGROUND_SERVICE_DATA = "fgs";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700256 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700257 // wl line is:
258 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
Bookatz5b5ec322017-05-26 09:40:38 -0700259 // full totalTime, 'f', count, current duration, max duration, total duration,
260 // partial totalTime, 'p', count, current duration, max duration, total duration,
261 // bg partial totalTime, 'bp', count, current duration, max duration, total duration,
262 // window totalTime, 'w', count, current duration, max duration, total duration
Bookatz506a8182017-05-01 14:18:42 -0700263 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700264 private static final String WAKELOCK_DATA = "wl";
Bookatzc8c44962017-05-11 12:12:54 -0700265 // awl line is:
266 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "awl",
267 // cumulative partial wakelock duration, cumulative background partial wakelock duration
268 private static final String AGGREGATED_WAKELOCK_DATA = "awl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700269 private static final String SYNC_DATA = "sy";
270 private static final String JOB_DATA = "jb";
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700271 private static final String JOB_COMPLETION_DATA = "jbc";
Evan Millarc64edde2009-04-18 12:26:32 -0700272 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700273 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700274 private static final String NETWORK_DATA = "nt";
275 private static final String USER_ACTIVITY_DATA = "ua";
276 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800277 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700278 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700279 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700280 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800281 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
282 private static final String WIFI_CONTROLLER_DATA = "wfcd";
283 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
284 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700285 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700286 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800287 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800288 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
289 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800290 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700291 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700292 private static final String SCREEN_BRIGHTNESS_DATA = "br";
293 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700294 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700295 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
296 private static final String DATA_CONNECTION_TIME_DATA = "dct";
297 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800298 private static final String WIFI_STATE_TIME_DATA = "wst";
299 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700300 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
301 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
302 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
303 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800304 private static final String POWER_USE_SUMMARY_DATA = "pws";
305 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700306 private static final String DISCHARGE_STEP_DATA = "dsd";
307 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700308 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
309 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700310 private static final String FLASHLIGHT_DATA = "fla";
311 private static final String CAMERA_DATA = "cam";
312 private static final String VIDEO_DATA = "vid";
313 private static final String AUDIO_DATA = "aud";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314
Adam Lesinski010bf372016-04-11 12:18:18 -0700315 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
316
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700317 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 private final Formatter mFormatter = new Formatter(mFormatBuilder);
319
320 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700321 * Indicates times spent by the uid at each cpu frequency in all process states.
322 *
323 * Other types might include times spent in foreground, background etc.
324 */
325 private final String UID_TIMES_TYPE_ALL = "A";
326
327 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700328 * State for keeping track of counting information.
329 */
330 public static abstract class Counter {
331
332 /**
333 * Returns the count associated with this Counter for the
334 * selected type of statistics.
335 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700336 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700337 */
Evan Millarc64edde2009-04-18 12:26:32 -0700338 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700339
340 /**
341 * Temporary for debugging.
342 */
343 public abstract void logState(Printer pw, String prefix);
344 }
345
346 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700347 * State for keeping track of long counting information.
348 */
349 public static abstract class LongCounter {
350
351 /**
352 * Returns the count associated with this Counter for the
353 * selected type of statistics.
354 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700355 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700356 */
357 public abstract long getCountLocked(int which);
358
359 /**
360 * Temporary for debugging.
361 */
362 public abstract void logState(Printer pw, String prefix);
363 }
364
365 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700366 * State for keeping track of array of long counting information.
367 */
368 public static abstract class LongCounterArray {
369 /**
370 * Returns the counts associated with this Counter for the
371 * selected type of statistics.
372 *
373 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
374 */
375 public abstract long[] getCountsLocked(int which);
376
377 /**
378 * Temporary for debugging.
379 */
380 public abstract void logState(Printer pw, String prefix);
381 }
382
383 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800384 * Container class that aggregates counters for transmit, receive, and idle state of a
385 * radio controller.
386 */
387 public static abstract class ControllerActivityCounter {
388 /**
389 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
390 * idle state.
391 */
392 public abstract LongCounter getIdleTimeCounter();
393
394 /**
395 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
396 * receive state.
397 */
398 public abstract LongCounter getRxTimeCounter();
399
400 /**
401 * An array of {@link LongCounter}, representing various transmit levels, where each level
402 * may draw a different amount of power. The levels themselves are controller-specific.
403 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
404 * various transmit level states.
405 */
406 public abstract LongCounter[] getTxTimeCounters();
407
408 /**
409 * @return a non-null {@link LongCounter} representing the power consumed by the controller
410 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
411 * yield a value of 0 if the device doesn't support power calculations.
412 */
413 public abstract LongCounter getPowerCounter();
414 }
415
416 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 * State for keeping track of timing information.
418 */
419 public static abstract class Timer {
420
421 /**
422 * Returns the count associated with this Timer for the
423 * selected type of statistics.
424 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700425 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 */
Evan Millarc64edde2009-04-18 12:26:32 -0700427 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428
429 /**
430 * Returns the total time in microseconds associated with this Timer for the
431 * selected type of statistics.
432 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800433 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700434 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 * @return a time in microseconds
436 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800437 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700440 * Returns the total time in microseconds associated with this Timer since the
441 * 'mark' was last set.
442 *
443 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
444 * @return a time in microseconds
445 */
446 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
447
448 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700449 * Returns the max duration if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800450 * Not all Timer subclasses track the max, total, current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700451
452 */
453 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
454 return -1;
455 }
456
457 /**
458 * Returns the current time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800459 * Not all Timer subclasses track the max, total, current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700460 */
461 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
462 return -1;
463 }
464
465 /**
Bookatz867c0d72017-03-07 18:23:42 -0800466 * Returns the current time the timer has been active, if it is being tracked.
467 *
468 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
469 * been on since reset.
470 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
471 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
472 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
473 * the actual total time.
474 * Not all Timer subclasses track the max, total, current durations.
475 */
476 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
477 return -1;
478 }
479
480 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700481 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
482 * used, for example, for tracking background usage. Secondary timers are never pooled.
483 *
484 * Not all Timer subclasses have a secondary timer; those that don't return null.
485 */
486 public Timer getSubTimer() {
487 return null;
488 }
489
490 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700491 * Returns whether the timer is currently running. Some types of timers
492 * (e.g. BatchTimers) don't know whether the event is currently active,
493 * and report false.
494 */
495 public boolean isRunningLocked() {
496 return false;
497 }
498
499 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 * Temporary for debugging.
501 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700502 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 }
504
505 /**
506 * The statistics associated with a particular uid.
507 */
508 public static abstract class Uid {
509
510 /**
511 * Returns a mapping containing wakelock statistics.
512 *
513 * @return a Map from Strings to Uid.Wakelock objects.
514 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700515 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516
517 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700518 * Returns a mapping containing sync statistics.
519 *
520 * @return a Map from Strings to Timer objects.
521 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700522 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700523
524 /**
525 * Returns a mapping containing scheduled job statistics.
526 *
527 * @return a Map from Strings to Timer objects.
528 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700529 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700530
531 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700532 * Returns statistics about how jobs have completed.
533 *
534 * @return A Map of String job names to completion type -> count mapping.
535 */
536 public abstract ArrayMap<String, SparseIntArray> getJobCompletionStats();
537
538 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 * The statistics associated with a particular wake lock.
540 */
541 public static abstract class Wakelock {
542 public abstract Timer getWakeTime(int type);
543 }
544
545 /**
Bookatzc8c44962017-05-11 12:12:54 -0700546 * The cumulative time the uid spent holding any partial wakelocks. This will generally
547 * differ from summing over the Wakelocks in getWakelockStats since the latter may have
548 * wakelocks that overlap in time (and therefore over-counts).
549 */
550 public abstract Timer getAggregatedPartialWakelockTimer();
551
552 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 * Returns a mapping containing sensor statistics.
554 *
555 * @return a Map from Integer sensor ids to Uid.Sensor objects.
556 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700557 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558
559 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700560 * Returns a mapping containing active process data.
561 */
562 public abstract SparseArray<? extends Pid> getPidStats();
Bookatzc8c44962017-05-11 12:12:54 -0700563
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700564 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 * Returns a mapping containing process statistics.
566 *
567 * @return a Map from Strings to Uid.Proc objects.
568 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700569 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570
571 /**
572 * Returns a mapping containing package statistics.
573 *
574 * @return a Map from Strings to Uid.Pkg objects.
575 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700576 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700577
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800578 public abstract ControllerActivityCounter getWifiControllerActivity();
579 public abstract ControllerActivityCounter getBluetoothControllerActivity();
580 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800581
582 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 * {@hide}
584 */
585 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700586
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800587 public abstract void noteWifiRunningLocked(long elapsedRealtime);
588 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
589 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
590 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
591 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
592 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
593 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
594 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
595 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
596 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800597 public abstract void noteActivityResumedLocked(long elapsedRealtime);
598 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800599 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
600 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
601 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700602 public abstract int getWifiScanCount(int which);
Bookatz867c0d72017-03-07 18:23:42 -0800603 public abstract int getWifiScanBackgroundCount(int which);
604 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
605 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800606 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700607 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800608 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700609 public abstract Timer getAudioTurnedOnTimer();
610 public abstract Timer getVideoTurnedOnTimer();
611 public abstract Timer getFlashlightTurnedOnTimer();
612 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700613 public abstract Timer getForegroundActivityTimer();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700614
615 /**
616 * Returns the timer keeping track of Foreground Service time
617 */
618 public abstract Timer getForegroundServiceTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800619 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800620 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatzb1f04f32017-05-19 13:57:32 -0700621 public abstract Timer getBluetoothUnoptimizedScanTimer();
622 public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700623 public abstract Counter getBluetoothScanResultCounter();
Bookatzb1f04f32017-05-19 13:57:32 -0700624 public abstract Counter getBluetoothScanResultBgCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700625
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700626 public abstract long[] getCpuFreqTimes(int which);
627 public abstract long[] getScreenOffCpuFreqTimes(int which);
628
Dianne Hackborna0200e32016-03-30 18:01:41 -0700629 // Note: the following times are disjoint. They can be added together to find the
630 // total time a uid has had any processes running at all.
631
632 /**
633 * Time this uid has any processes in the top state (or above such as persistent).
634 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800635 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700636 /**
637 * Time this uid has any process with a started out bound foreground service, but
638 * none in the "top" state.
639 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800640 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700641 /**
642 * Time this uid has any process that is top while the device is sleeping, but none
643 * in the "foreground service" or better state.
644 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800645 public static final int PROCESS_STATE_TOP_SLEEPING = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700646 /**
647 * Time this uid has any process in an active foreground state, but none in the
648 * "top sleeping" or better state.
649 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800650 public static final int PROCESS_STATE_FOREGROUND = 3;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700651 /**
652 * Time this uid has any process in an active background state, but none in the
653 * "foreground" or better state.
654 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800655 public static final int PROCESS_STATE_BACKGROUND = 4;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700656 /**
657 * Time this uid has any processes that are sitting around cached, not in one of the
658 * other active states.
659 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800660 public static final int PROCESS_STATE_CACHED = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700661 /**
662 * Total number of process states we track.
663 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800664 public static final int NUM_PROCESS_STATE = 6;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700665
666 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackborna8d10942015-11-19 17:55:19 -0800667 "Top", "Fg Service", "Top Sleeping", "Foreground", "Background", "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700668 };
669
670 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800671 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700672
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800673 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674
Robert Greenwalta029ea12013-09-25 16:38:12 -0700675 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
676
Dianne Hackborn617f8772009-03-31 15:04:46 -0700677 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700678 * Note that these must match the constants in android.os.PowerManager.
679 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
680 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700681 */
682 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700683 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700684 };
Bookatzc8c44962017-05-11 12:12:54 -0700685
Phil Weaverda80d672016-03-15 16:25:46 -0700686 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700687
Dianne Hackborn617f8772009-03-31 15:04:46 -0700688 public abstract void noteUserActivityLocked(int type);
689 public abstract boolean hasUserActivity();
690 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700691
692 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800693 public abstract long getNetworkActivityBytes(int type, int which);
694 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800695 public abstract long getMobileRadioActiveTime(int which);
696 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700697
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700698 /**
699 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
700 */
701 public abstract long getUserCpuTimeUs(int which);
702
703 /**
704 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
705 */
706 public abstract long getSystemCpuTimeUs(int which);
707
708 /**
Sudheer Shanka71f34b32017-07-21 00:14:24 -0700709 * Returns the approximate cpu time (in microseconds) spent at a certain CPU speed for a
Adam Lesinski6832f392015-09-05 18:05:40 -0700710 * given CPU cluster.
711 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700712 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700713 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700714 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
715 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700716 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700717 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700718
Adam Lesinski5f056f62016-07-14 16:56:08 -0700719 /**
720 * Returns the number of times this UID woke up the Application Processor to
721 * process a mobile radio packet.
722 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
723 */
724 public abstract long getMobileRadioApWakeupCount(int which);
725
726 /**
727 * Returns the number of times this UID woke up the Application Processor to
728 * process a WiFi packet.
729 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
730 */
731 public abstract long getWifiRadioApWakeupCount(int which);
732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800734 /*
735 * FIXME: it's not correct to use this magic value because it
736 * could clash with a sensor handle (which are defined by
737 * the sensor HAL, and therefore out of our control
738 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 // Magic sensor number for the GPS.
740 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800745
Bookatz867c0d72017-03-07 18:23:42 -0800746 /** Returns a Timer for sensor usage when app is in the background. */
747 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 }
749
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700750 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800751 public int mWakeNesting;
752 public long mWakeSumMs;
753 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700754 }
755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 /**
757 * The statistics associated with a particular process.
758 */
759 public static abstract class Proc {
760
Dianne Hackborn287952c2010-09-22 22:34:31 -0700761 public static class ExcessivePower {
762 public static final int TYPE_WAKE = 1;
763 public static final int TYPE_CPU = 2;
764
765 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700766 public long overTime;
767 public long usedTime;
768 }
769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800771 * Returns true if this process is still active in the battery stats.
772 */
773 public abstract boolean isActive();
774
775 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700776 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700778 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 */
780 public abstract long getUserTime(int which);
781
782 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700783 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700785 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 */
787 public abstract long getSystemTime(int which);
788
789 /**
790 * Returns the number of times the process has been started.
791 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700792 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 */
794 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700795
796 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800797 * Returns the number of times the process has crashed.
798 *
799 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
800 */
801 public abstract int getNumCrashes(int which);
802
803 /**
804 * Returns the number of times the process has ANRed.
805 *
806 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
807 */
808 public abstract int getNumAnrs(int which);
809
810 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700811 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700812 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700813 * @return foreground cpu time in microseconds
814 */
815 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700816
Dianne Hackborn287952c2010-09-22 22:34:31 -0700817 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700818
Dianne Hackborn287952c2010-09-22 22:34:31 -0700819 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 }
821
822 /**
823 * The statistics associated with a particular package.
824 */
825 public static abstract class Pkg {
826
827 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700828 * Returns information about all wakeup alarms that have been triggered for this
829 * package. The mapping keys are tag names for the alarms, the counter contains
830 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700832 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833
834 /**
835 * Returns a mapping containing service statistics.
836 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700837 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838
839 /**
840 * The statistics associated with a particular service.
841 */
Joe Onoratoabded112016-02-08 16:49:39 -0800842 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843
844 /**
845 * Returns the amount of time spent started.
846 *
847 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700848 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 * @return
850 */
851 public abstract long getStartTime(long batteryUptime, int which);
852
853 /**
854 * Returns the total number of times startService() has been called.
855 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700856 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 */
858 public abstract int getStarts(int which);
859
860 /**
861 * Returns the total number times the service has been launched.
862 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700863 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 */
865 public abstract int getLaunches(int which);
866 }
867 }
868 }
869
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800870 public static final class LevelStepTracker {
871 public long mLastStepTime = -1;
872 public int mNumStepDurations;
873 public final long[] mStepDurations;
874
875 public LevelStepTracker(int maxLevelSteps) {
876 mStepDurations = new long[maxLevelSteps];
877 }
878
879 public LevelStepTracker(int numSteps, long[] steps) {
880 mNumStepDurations = numSteps;
881 mStepDurations = new long[numSteps];
882 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
883 }
884
885 public long getDurationAt(int index) {
886 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
887 }
888
889 public int getLevelAt(int index) {
890 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
891 >> STEP_LEVEL_LEVEL_SHIFT);
892 }
893
894 public int getInitModeAt(int index) {
895 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
896 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
897 }
898
899 public int getModModeAt(int index) {
900 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
901 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
902 }
903
904 private void appendHex(long val, int topOffset, StringBuilder out) {
905 boolean hasData = false;
906 while (topOffset >= 0) {
907 int digit = (int)( (val>>topOffset) & 0xf );
908 topOffset -= 4;
909 if (!hasData && digit == 0) {
910 continue;
911 }
912 hasData = true;
913 if (digit >= 0 && digit <= 9) {
914 out.append((char)('0' + digit));
915 } else {
916 out.append((char)('a' + digit - 10));
917 }
918 }
919 }
920
921 public void encodeEntryAt(int index, StringBuilder out) {
922 long item = mStepDurations[index];
923 long duration = item & STEP_LEVEL_TIME_MASK;
924 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
925 >> STEP_LEVEL_LEVEL_SHIFT);
926 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
927 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
928 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
929 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
930 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
931 case Display.STATE_OFF: out.append('f'); break;
932 case Display.STATE_ON: out.append('o'); break;
933 case Display.STATE_DOZE: out.append('d'); break;
934 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
935 }
936 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
937 out.append('p');
938 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700939 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
940 out.append('i');
941 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800942 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
943 case Display.STATE_OFF: out.append('F'); break;
944 case Display.STATE_ON: out.append('O'); break;
945 case Display.STATE_DOZE: out.append('D'); break;
946 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
947 }
948 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
949 out.append('P');
950 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700951 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
952 out.append('I');
953 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800954 out.append('-');
955 appendHex(level, 4, out);
956 out.append('-');
957 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
958 }
959
960 public void decodeEntryAt(int index, String value) {
961 final int N = value.length();
962 int i = 0;
963 char c;
964 long out = 0;
965 while (i < N && (c=value.charAt(i)) != '-') {
966 i++;
967 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800968 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800969 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800970 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800971 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800972 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800973 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800974 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
975 << STEP_LEVEL_INITIAL_MODE_SHIFT);
976 break;
977 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
978 << STEP_LEVEL_INITIAL_MODE_SHIFT);
979 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700980 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
981 << STEP_LEVEL_INITIAL_MODE_SHIFT);
982 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800983 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
984 break;
985 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
986 break;
987 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
988 break;
989 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
990 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
991 break;
992 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
993 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800994 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700995 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
996 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
997 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800998 }
999 }
1000 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001001 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001002 while (i < N && (c=value.charAt(i)) != '-') {
1003 i++;
1004 level <<= 4;
1005 if (c >= '0' && c <= '9') {
1006 level += c - '0';
1007 } else if (c >= 'a' && c <= 'f') {
1008 level += c - 'a' + 10;
1009 } else if (c >= 'A' && c <= 'F') {
1010 level += c - 'A' + 10;
1011 }
1012 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001013 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001014 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
1015 long duration = 0;
1016 while (i < N && (c=value.charAt(i)) != '-') {
1017 i++;
1018 duration <<= 4;
1019 if (c >= '0' && c <= '9') {
1020 duration += c - '0';
1021 } else if (c >= 'a' && c <= 'f') {
1022 duration += c - 'a' + 10;
1023 } else if (c >= 'A' && c <= 'F') {
1024 duration += c - 'A' + 10;
1025 }
1026 }
1027 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
1028 }
1029
1030 public void init() {
1031 mLastStepTime = -1;
1032 mNumStepDurations = 0;
1033 }
1034
1035 public void clearTime() {
1036 mLastStepTime = -1;
1037 }
1038
1039 public long computeTimePerLevel() {
1040 final long[] steps = mStepDurations;
1041 final int numSteps = mNumStepDurations;
1042
1043 // For now we'll do a simple average across all steps.
1044 if (numSteps <= 0) {
1045 return -1;
1046 }
1047 long total = 0;
1048 for (int i=0; i<numSteps; i++) {
1049 total += steps[i] & STEP_LEVEL_TIME_MASK;
1050 }
1051 return total / numSteps;
1052 /*
1053 long[] buckets = new long[numSteps];
1054 int numBuckets = 0;
1055 int numToAverage = 4;
1056 int i = 0;
1057 while (i < numSteps) {
1058 long totalTime = 0;
1059 int num = 0;
1060 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1061 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1062 num++;
1063 }
1064 buckets[numBuckets] = totalTime / num;
1065 numBuckets++;
1066 numToAverage *= 2;
1067 i += num;
1068 }
1069 if (numBuckets < 1) {
1070 return -1;
1071 }
1072 long averageTime = buckets[numBuckets-1];
1073 for (i=numBuckets-2; i>=0; i--) {
1074 averageTime = (averageTime + buckets[i]) / 2;
1075 }
1076 return averageTime;
1077 */
1078 }
1079
1080 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1081 int[] outNumOfInterest) {
1082 final long[] steps = mStepDurations;
1083 final int count = mNumStepDurations;
1084 if (count <= 0) {
1085 return -1;
1086 }
1087 long total = 0;
1088 int numOfInterest = 0;
1089 for (int i=0; i<count; i++) {
1090 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1091 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1092 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1093 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1094 // If the modes of interest didn't change during this step period...
1095 if ((modMode&modesOfInterest) == 0) {
1096 // And the mode values during this period match those we are measuring...
1097 if ((initMode&modesOfInterest) == modeValues) {
1098 // Then this can be used to estimate the total time!
1099 numOfInterest++;
1100 total += steps[i] & STEP_LEVEL_TIME_MASK;
1101 }
1102 }
1103 }
1104 if (numOfInterest <= 0) {
1105 return -1;
1106 }
1107
1108 if (outNumOfInterest != null) {
1109 outNumOfInterest[0] = numOfInterest;
1110 }
1111
1112 // The estimated time is the average time we spend in each level, multipled
1113 // by 100 -- the total number of battery levels
1114 return (total / numOfInterest) * 100;
1115 }
1116
1117 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1118 int stepCount = mNumStepDurations;
1119 final long lastStepTime = mLastStepTime;
1120 if (lastStepTime >= 0 && numStepLevels > 0) {
1121 final long[] steps = mStepDurations;
1122 long duration = elapsedRealtime - lastStepTime;
1123 for (int i=0; i<numStepLevels; i++) {
1124 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1125 long thisDuration = duration / (numStepLevels-i);
1126 duration -= thisDuration;
1127 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1128 thisDuration = STEP_LEVEL_TIME_MASK;
1129 }
1130 steps[0] = thisDuration | modeBits;
1131 }
1132 stepCount += numStepLevels;
1133 if (stepCount > steps.length) {
1134 stepCount = steps.length;
1135 }
1136 }
1137 mNumStepDurations = stepCount;
1138 mLastStepTime = elapsedRealtime;
1139 }
1140
1141 public void readFromParcel(Parcel in) {
1142 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001143 if (N > mStepDurations.length) {
1144 throw new ParcelFormatException("more step durations than available: " + N);
1145 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001146 mNumStepDurations = N;
1147 for (int i=0; i<N; i++) {
1148 mStepDurations[i] = in.readLong();
1149 }
1150 }
1151
1152 public void writeToParcel(Parcel out) {
1153 final int N = mNumStepDurations;
1154 out.writeInt(N);
1155 for (int i=0; i<N; i++) {
1156 out.writeLong(mStepDurations[i]);
1157 }
1158 }
1159 }
1160
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001161 public static final class PackageChange {
1162 public String mPackageName;
1163 public boolean mUpdate;
1164 public int mVersionCode;
1165 }
1166
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001167 public static final class DailyItem {
1168 public long mStartTime;
1169 public long mEndTime;
1170 public LevelStepTracker mDischargeSteps;
1171 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001172 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001173 }
1174
1175 public abstract DailyItem getDailyItemLocked(int daysAgo);
1176
1177 public abstract long getCurrentDailyStartTime();
1178
1179 public abstract long getNextMinDailyDeadline();
1180
1181 public abstract long getNextMaxDailyDeadline();
1182
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001183 public abstract long[] getCpuFreqs();
1184
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001185 public final static class HistoryTag {
1186 public String string;
1187 public int uid;
1188
1189 public int poolIdx;
1190
1191 public void setTo(HistoryTag o) {
1192 string = o.string;
1193 uid = o.uid;
1194 poolIdx = o.poolIdx;
1195 }
1196
1197 public void setTo(String _string, int _uid) {
1198 string = _string;
1199 uid = _uid;
1200 poolIdx = -1;
1201 }
1202
1203 public void writeToParcel(Parcel dest, int flags) {
1204 dest.writeString(string);
1205 dest.writeInt(uid);
1206 }
1207
1208 public void readFromParcel(Parcel src) {
1209 string = src.readString();
1210 uid = src.readInt();
1211 poolIdx = -1;
1212 }
1213
1214 @Override
1215 public boolean equals(Object o) {
1216 if (this == o) return true;
1217 if (o == null || getClass() != o.getClass()) return false;
1218
1219 HistoryTag that = (HistoryTag) o;
1220
1221 if (uid != that.uid) return false;
1222 if (!string.equals(that.string)) return false;
1223
1224 return true;
1225 }
1226
1227 @Override
1228 public int hashCode() {
1229 int result = string.hashCode();
1230 result = 31 * result + uid;
1231 return result;
1232 }
1233 }
1234
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001235 /**
1236 * Optional detailed information that can go into a history step. This is typically
1237 * generated each time the battery level changes.
1238 */
1239 public final static class HistoryStepDetails {
1240 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1241 public int userTime;
1242 public int systemTime;
1243
1244 // Top three apps using CPU in the last step, with times in 1/100 second.
1245 public int appCpuUid1;
1246 public int appCpuUTime1;
1247 public int appCpuSTime1;
1248 public int appCpuUid2;
1249 public int appCpuUTime2;
1250 public int appCpuSTime2;
1251 public int appCpuUid3;
1252 public int appCpuUTime3;
1253 public int appCpuSTime3;
1254
1255 // Information from /proc/stat
1256 public int statUserTime;
1257 public int statSystemTime;
1258 public int statIOWaitTime;
1259 public int statIrqTime;
1260 public int statSoftIrqTime;
1261 public int statIdlTime;
1262
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001263 // Platform-level low power state stats
1264 public String statPlatformIdleState;
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001265 public String statSubsystemPowerState;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001266
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001267 public HistoryStepDetails() {
1268 clear();
1269 }
1270
1271 public void clear() {
1272 userTime = systemTime = 0;
1273 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1274 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1275 = appCpuUTime3 = appCpuSTime3 = 0;
1276 }
1277
1278 public void writeToParcel(Parcel out) {
1279 out.writeInt(userTime);
1280 out.writeInt(systemTime);
1281 out.writeInt(appCpuUid1);
1282 out.writeInt(appCpuUTime1);
1283 out.writeInt(appCpuSTime1);
1284 out.writeInt(appCpuUid2);
1285 out.writeInt(appCpuUTime2);
1286 out.writeInt(appCpuSTime2);
1287 out.writeInt(appCpuUid3);
1288 out.writeInt(appCpuUTime3);
1289 out.writeInt(appCpuSTime3);
1290 out.writeInt(statUserTime);
1291 out.writeInt(statSystemTime);
1292 out.writeInt(statIOWaitTime);
1293 out.writeInt(statIrqTime);
1294 out.writeInt(statSoftIrqTime);
1295 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001296 out.writeString(statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001297 out.writeString(statSubsystemPowerState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001298 }
1299
1300 public void readFromParcel(Parcel in) {
1301 userTime = in.readInt();
1302 systemTime = in.readInt();
1303 appCpuUid1 = in.readInt();
1304 appCpuUTime1 = in.readInt();
1305 appCpuSTime1 = in.readInt();
1306 appCpuUid2 = in.readInt();
1307 appCpuUTime2 = in.readInt();
1308 appCpuSTime2 = in.readInt();
1309 appCpuUid3 = in.readInt();
1310 appCpuUTime3 = in.readInt();
1311 appCpuSTime3 = in.readInt();
1312 statUserTime = in.readInt();
1313 statSystemTime = in.readInt();
1314 statIOWaitTime = in.readInt();
1315 statIrqTime = in.readInt();
1316 statSoftIrqTime = in.readInt();
1317 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001318 statPlatformIdleState = in.readString();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001319 statSubsystemPowerState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001320 }
1321 }
1322
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001323 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001324 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001325
1326 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001327 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001328
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001329 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001330 public static final byte CMD_NULL = -1;
1331 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001332 public static final byte CMD_CURRENT_TIME = 5;
1333 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001334 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001335 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001336
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001337 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001338
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001339 /**
1340 * Return whether the command code is a delta data update.
1341 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001342 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001343 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001344 }
1345
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001346 public byte batteryLevel;
1347 public byte batteryStatus;
1348 public byte batteryHealth;
1349 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001350
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001351 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001352 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001353
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001354 // The charge of the battery in micro-Ampere-hours.
1355 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001356
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001357 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001358 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001359 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001360 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001361 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1362 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001363 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001364 public static final int STATE_PHONE_STATE_SHIFT = 6;
1365 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001366 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001367 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001368 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001369
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001370 // These states always appear directly in the first int token
1371 // of a delta change; they should be ones that change relatively
1372 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001373 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1374 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001375 public static final int STATE_GPS_ON_FLAG = 1<<29;
1376 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001377 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001378 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001379 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001380 // Do not use, this is used for coulomb delta count.
1381 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001382 // These are on the lower bits used for the command; if they change
1383 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001384 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001385 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001386 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001387 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1388 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
Mike Mac2f518a2017-09-19 16:06:03 -07001389 public static final int STATE_SCREEN_DOZE_FLAG = 1 << 18;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001390 // empty slot
1391 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001392
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001393 public static final int MOST_INTERESTING_STATES =
Mike Mac2f518a2017-09-19 16:06:03 -07001394 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG | STATE_SCREEN_DOZE_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001395
1396 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001397
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001398 public int states;
1399
Dianne Hackborn3251b902014-06-20 14:40:53 -07001400 // Constants from WIFI_SUPPL_STATE_*
1401 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1402 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1403 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1404 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1405 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1406 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1407
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001408 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001409 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1410 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1411 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001412 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001413 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1414 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1415 public static final int STATE2_CHARGING_FLAG = 1<<24;
1416 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1417 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1418 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001419 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001420
1421 public static final int MOST_INTERESTING_STATES2 =
Mike Mac2f518a2017-09-19 16:06:03 -07001422 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
1423 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001424
1425 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001426
Dianne Hackborn40c87252014-03-19 16:55:40 -07001427 public int states2;
1428
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001429 // The wake lock that was acquired at this point.
1430 public HistoryTag wakelockTag;
1431
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001432 // Kernel wakeup reason at this point.
1433 public HistoryTag wakeReasonTag;
1434
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001435 // Non-null when there is more detailed information at this step.
1436 public HistoryStepDetails stepDetails;
1437
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001438 public static final int EVENT_FLAG_START = 0x8000;
1439 public static final int EVENT_FLAG_FINISH = 0x4000;
1440
1441 // No event in this item.
1442 public static final int EVENT_NONE = 0x0000;
1443 // Event is about a process that is running.
1444 public static final int EVENT_PROC = 0x0001;
1445 // Event is about an application package that is in the foreground.
1446 public static final int EVENT_FOREGROUND = 0x0002;
1447 // Event is about an application package that is at the top of the screen.
1448 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001449 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001450 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001451 // Events for all additional wake locks aquired/release within a wake block.
1452 // These are not generated by default.
1453 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001454 // Event is about an application executing a scheduled job.
1455 public static final int EVENT_JOB = 0x0006;
1456 // Events for users running.
1457 public static final int EVENT_USER_RUNNING = 0x0007;
1458 // Events for foreground user.
1459 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001460 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001461 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001462 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001463 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001464 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001465 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001466 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001467 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001468 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001469 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001470 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001471 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001472 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001473 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001474 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001475 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001476 // Event for a package being on the temporary whitelist.
1477 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001478 // Event for the screen waking up.
1479 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001480 // Event for the UID that woke up the application processor.
1481 // Used for wakeups coming from WiFi, modem, etc.
1482 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001483 // Event for reporting that a specific partial wake lock has been held for a long duration.
1484 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001485
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001486 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001487 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001488 // Mask to extract out only the type part of the event.
1489 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001490
1491 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1492 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1493 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1494 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1495 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1496 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001497 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1498 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001499 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1500 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001501 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1502 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1503 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1504 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1505 public static final int EVENT_USER_FOREGROUND_START =
1506 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1507 public static final int EVENT_USER_FOREGROUND_FINISH =
1508 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001509 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1510 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001511 public static final int EVENT_TEMP_WHITELIST_START =
1512 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1513 public static final int EVENT_TEMP_WHITELIST_FINISH =
1514 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001515 public static final int EVENT_LONG_WAKE_LOCK_START =
1516 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1517 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1518 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001519
1520 // For CMD_EVENT.
1521 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001522 public HistoryTag eventTag;
1523
Dianne Hackborn9a755432014-05-15 17:05:22 -07001524 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001525 public long currentTime;
1526
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001527 // Meta-data when reading.
1528 public int numReadInts;
1529
1530 // Pre-allocated objects.
1531 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001532 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001533 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001534
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001535 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001536 }
Bookatzc8c44962017-05-11 12:12:54 -07001537
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001538 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001539 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001540 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001541 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001542 }
Bookatzc8c44962017-05-11 12:12:54 -07001543
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001544 public int describeContents() {
1545 return 0;
1546 }
1547
1548 public void writeToParcel(Parcel dest, int flags) {
1549 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001550 int bat = (((int)cmd)&0xff)
1551 | ((((int)batteryLevel)<<8)&0xff00)
1552 | ((((int)batteryStatus)<<16)&0xf0000)
1553 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001554 | ((((int)batteryPlugType)<<24)&0xf000000)
1555 | (wakelockTag != null ? 0x10000000 : 0)
1556 | (wakeReasonTag != null ? 0x20000000 : 0)
1557 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001558 dest.writeInt(bat);
1559 bat = (((int)batteryTemperature)&0xffff)
1560 | ((((int)batteryVoltage)<<16)&0xffff0000);
1561 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001562 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001563 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001564 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001565 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001566 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001567 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001568 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001569 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001570 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001571 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001572 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001573 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001574 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001575 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001576 dest.writeLong(currentTime);
1577 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001578 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001579
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001580 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001581 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001582 int bat = src.readInt();
1583 cmd = (byte)(bat&0xff);
1584 batteryLevel = (byte)((bat>>8)&0xff);
1585 batteryStatus = (byte)((bat>>16)&0xf);
1586 batteryHealth = (byte)((bat>>20)&0xf);
1587 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001588 int bat2 = src.readInt();
1589 batteryTemperature = (short)(bat2&0xffff);
1590 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001591 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001592 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001593 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001594 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001595 wakelockTag = localWakelockTag;
1596 wakelockTag.readFromParcel(src);
1597 } else {
1598 wakelockTag = null;
1599 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001600 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001601 wakeReasonTag = localWakeReasonTag;
1602 wakeReasonTag.readFromParcel(src);
1603 } else {
1604 wakeReasonTag = null;
1605 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001606 if ((bat&0x40000000) != 0) {
1607 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001608 eventTag = localEventTag;
1609 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001610 } else {
1611 eventCode = EVENT_NONE;
1612 eventTag = null;
1613 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001614 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001615 currentTime = src.readLong();
1616 } else {
1617 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001618 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001619 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001620 }
1621
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001622 public void clear() {
1623 time = 0;
1624 cmd = CMD_NULL;
1625 batteryLevel = 0;
1626 batteryStatus = 0;
1627 batteryHealth = 0;
1628 batteryPlugType = 0;
1629 batteryTemperature = 0;
1630 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001631 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001632 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001633 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001634 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001635 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001636 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001637 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001638 }
Bookatzc8c44962017-05-11 12:12:54 -07001639
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001640 public void setTo(HistoryItem o) {
1641 time = o.time;
1642 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001643 setToCommon(o);
1644 }
1645
1646 public void setTo(long time, byte cmd, HistoryItem o) {
1647 this.time = time;
1648 this.cmd = cmd;
1649 setToCommon(o);
1650 }
1651
1652 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001653 batteryLevel = o.batteryLevel;
1654 batteryStatus = o.batteryStatus;
1655 batteryHealth = o.batteryHealth;
1656 batteryPlugType = o.batteryPlugType;
1657 batteryTemperature = o.batteryTemperature;
1658 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001659 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001660 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001661 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001662 if (o.wakelockTag != null) {
1663 wakelockTag = localWakelockTag;
1664 wakelockTag.setTo(o.wakelockTag);
1665 } else {
1666 wakelockTag = null;
1667 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001668 if (o.wakeReasonTag != null) {
1669 wakeReasonTag = localWakeReasonTag;
1670 wakeReasonTag.setTo(o.wakeReasonTag);
1671 } else {
1672 wakeReasonTag = null;
1673 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001674 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001675 if (o.eventTag != null) {
1676 eventTag = localEventTag;
1677 eventTag.setTo(o.eventTag);
1678 } else {
1679 eventTag = null;
1680 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001681 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001682 }
1683
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001684 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001685 return batteryLevel == o.batteryLevel
1686 && batteryStatus == o.batteryStatus
1687 && batteryHealth == o.batteryHealth
1688 && batteryPlugType == o.batteryPlugType
1689 && batteryTemperature == o.batteryTemperature
1690 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001691 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001692 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001693 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001694 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001695 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001696
1697 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001698 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001699 return false;
1700 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001701 if (wakelockTag != o.wakelockTag) {
1702 if (wakelockTag == null || o.wakelockTag == null) {
1703 return false;
1704 }
1705 if (!wakelockTag.equals(o.wakelockTag)) {
1706 return false;
1707 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001708 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001709 if (wakeReasonTag != o.wakeReasonTag) {
1710 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1711 return false;
1712 }
1713 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1714 return false;
1715 }
1716 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001717 if (eventTag != o.eventTag) {
1718 if (eventTag == null || o.eventTag == null) {
1719 return false;
1720 }
1721 if (!eventTag.equals(o.eventTag)) {
1722 return false;
1723 }
1724 }
1725 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001726 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001727 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001728
1729 public final static class HistoryEventTracker {
1730 private final HashMap<String, SparseIntArray>[] mActiveEvents
1731 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1732
1733 public boolean updateState(int code, String name, int uid, int poolIdx) {
1734 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1735 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1736 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1737 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001738 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001739 mActiveEvents[idx] = active;
1740 }
1741 SparseIntArray uids = active.get(name);
1742 if (uids == null) {
1743 uids = new SparseIntArray();
1744 active.put(name, uids);
1745 }
1746 if (uids.indexOfKey(uid) >= 0) {
1747 // Already set, nothing to do!
1748 return false;
1749 }
1750 uids.put(uid, poolIdx);
1751 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1752 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1753 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1754 if (active == null) {
1755 // not currently active, nothing to do.
1756 return false;
1757 }
1758 SparseIntArray uids = active.get(name);
1759 if (uids == null) {
1760 // not currently active, nothing to do.
1761 return false;
1762 }
1763 idx = uids.indexOfKey(uid);
1764 if (idx < 0) {
1765 // not currently active, nothing to do.
1766 return false;
1767 }
1768 uids.removeAt(idx);
1769 if (uids.size() <= 0) {
1770 active.remove(name);
1771 }
1772 }
1773 return true;
1774 }
1775
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001776 public void removeEvents(int code) {
1777 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1778 mActiveEvents[idx] = null;
1779 }
1780
Dianne Hackborn37de0982014-05-09 09:32:18 -07001781 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1782 return mActiveEvents[code];
1783 }
1784 }
1785
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001786 public static final class BitDescription {
1787 public final int mask;
1788 public final int shift;
1789 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001790 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001791 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001792 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07001793
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001794 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001795 this.mask = mask;
1796 this.shift = -1;
1797 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001798 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001799 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001800 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001801 }
Bookatzc8c44962017-05-11 12:12:54 -07001802
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001803 public BitDescription(int mask, int shift, String name, String shortName,
1804 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001805 this.mask = mask;
1806 this.shift = shift;
1807 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001808 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001809 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001810 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001811 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001812 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001813
Dianne Hackbornfc064132014-06-02 12:42:12 -07001814 /**
1815 * Don't allow any more batching in to the current history event. This
1816 * is called when printing partial histories, so to ensure that the next
1817 * history event will go in to a new batch after what was printed in the
1818 * last partial history.
1819 */
1820 public abstract void commitCurrentHistoryBatchLocked();
1821
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001822 public abstract int getHistoryTotalSize();
1823
1824 public abstract int getHistoryUsedSize();
1825
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001826 public abstract boolean startIteratingHistoryLocked();
1827
Dianne Hackborn099bc622014-01-22 13:39:16 -08001828 public abstract int getHistoryStringPoolSize();
1829
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001830 public abstract int getHistoryStringPoolBytes();
1831
1832 public abstract String getHistoryTagPoolString(int index);
1833
1834 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001835
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001836 public abstract boolean getNextHistoryLocked(HistoryItem out);
1837
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001838 public abstract void finishIteratingHistoryLocked();
1839
1840 public abstract boolean startIteratingOldHistoryLocked();
1841
1842 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1843
1844 public abstract void finishIteratingOldHistoryLocked();
1845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001847 * Return the base time offset for the battery history.
1848 */
1849 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07001850
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001851 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 * Returns the number of times the device has been started.
1853 */
1854 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07001855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001857 * 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 -08001858 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07001859 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 * {@hide}
1861 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001862 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07001863
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001864 /**
1865 * Returns the number of times the screen was turned on.
1866 *
1867 * {@hide}
1868 */
1869 public abstract int getScreenOnCount(int which);
1870
Mike Mac2f518a2017-09-19 16:06:03 -07001871 /**
1872 * Returns the time in microseconds that the screen has been dozing while the device was
1873 * running on battery.
1874 *
1875 * {@hide}
1876 */
1877 public abstract long getScreenDozeTime(long elapsedRealtimeUs, int which);
1878
1879 /**
1880 * Returns the number of times the screen was turned dozing.
1881 *
1882 * {@hide}
1883 */
1884 public abstract int getScreenDozeCount(int which);
1885
Jeff Browne95c3cd2014-05-02 16:59:26 -07001886 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1887
Dianne Hackborn617f8772009-03-31 15:04:46 -07001888 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1889 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1890 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1891 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1892 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07001893
Dianne Hackborn617f8772009-03-31 15:04:46 -07001894 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1895 "dark", "dim", "medium", "light", "bright"
1896 };
Bookatzc8c44962017-05-11 12:12:54 -07001897
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001898 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1899 "0", "1", "2", "3", "4"
1900 };
1901
Dianne Hackborn617f8772009-03-31 15:04:46 -07001902 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001903
Dianne Hackborn617f8772009-03-31 15:04:46 -07001904 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001905 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001906 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07001907 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07001908 * {@hide}
1909 */
1910 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001911 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001914 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001915 * running on battery.
1916 *
1917 * {@hide}
1918 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001919 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001920
1921 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001922 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001923 *
1924 * {@hide}
1925 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001926 public abstract int getPowerSaveModeEnabledCount(int which);
1927
1928 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001929 * Constant for device idle mode: not active.
1930 */
1931 public static final int DEVICE_IDLE_MODE_OFF = 0;
1932
1933 /**
1934 * Constant for device idle mode: active in lightweight mode.
1935 */
1936 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1937
1938 /**
1939 * Constant for device idle mode: active in full mode.
1940 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07001941 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001942
1943 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001944 * Returns the time in microseconds that device has been in idle mode while
1945 * running on battery.
1946 *
1947 * {@hide}
1948 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001949 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001950
1951 /**
1952 * Returns the number of times that the devie has gone in to idle mode.
1953 *
1954 * {@hide}
1955 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001956 public abstract int getDeviceIdleModeCount(int mode, int which);
1957
1958 /**
1959 * Return the longest duration we spent in a particular device idle mode (fully in the
1960 * mode, not in idle maintenance etc).
1961 */
1962 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001963
1964 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001965 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001966 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001967 * counts all of the time that we consider the device to be idle, whether or not
1968 * it is currently in the actual device idle mode.
1969 *
1970 * {@hide}
1971 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001972 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001973
1974 /**
1975 * Returns the number of times that the devie has started idling.
1976 *
1977 * {@hide}
1978 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001979 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001980
1981 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001982 * Returns the number of times that connectivity state changed.
1983 *
1984 * {@hide}
1985 */
1986 public abstract int getNumConnectivityChange(int which);
1987
1988 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001989 * 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 -08001990 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07001991 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 * {@hide}
1993 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001994 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07001995
Dianne Hackborn627bba72009-03-24 22:32:56 -07001996 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001997 * Returns the number of times a phone call was activated.
1998 *
1999 * {@hide}
2000 */
2001 public abstract int getPhoneOnCount(int which);
2002
2003 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002004 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002005 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002006 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002007 * {@hide}
2008 */
2009 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002010 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002011
Dianne Hackborn617f8772009-03-31 15:04:46 -07002012 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07002013 * Returns the time in microseconds that the phone has been trying to
2014 * acquire a signal.
2015 *
2016 * {@hide}
2017 */
2018 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002019 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002020
2021 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002022 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002023 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002024 * {@hide}
2025 */
2026 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
2027
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002028 /**
2029 * Returns the time in microseconds that the mobile network has been active
2030 * (in a high power state).
2031 *
2032 * {@hide}
2033 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002034 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002035
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002036 /**
2037 * Returns the number of times that the mobile network has transitioned to the
2038 * active state.
2039 *
2040 * {@hide}
2041 */
2042 public abstract int getMobileRadioActiveCount(int which);
2043
2044 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002045 * Returns the time in microseconds that is the difference between the mobile radio
2046 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
2047 * from the radio.
2048 *
2049 * {@hide}
2050 */
2051 public abstract long getMobileRadioActiveAdjustedTime(int which);
2052
2053 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002054 * Returns the time in microseconds that the mobile network has been active
2055 * (in a high power state) but not being able to blame on an app.
2056 *
2057 * {@hide}
2058 */
2059 public abstract long getMobileRadioActiveUnknownTime(int which);
2060
2061 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002062 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002063 *
2064 * {@hide}
2065 */
2066 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002067
Dianne Hackborn627bba72009-03-24 22:32:56 -07002068 public static final int DATA_CONNECTION_NONE = 0;
2069 public static final int DATA_CONNECTION_GPRS = 1;
2070 public static final int DATA_CONNECTION_EDGE = 2;
2071 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002072 public static final int DATA_CONNECTION_CDMA = 4;
2073 public static final int DATA_CONNECTION_EVDO_0 = 5;
2074 public static final int DATA_CONNECTION_EVDO_A = 6;
2075 public static final int DATA_CONNECTION_1xRTT = 7;
2076 public static final int DATA_CONNECTION_HSDPA = 8;
2077 public static final int DATA_CONNECTION_HSUPA = 9;
2078 public static final int DATA_CONNECTION_HSPA = 10;
2079 public static final int DATA_CONNECTION_IDEN = 11;
2080 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002081 public static final int DATA_CONNECTION_LTE = 13;
2082 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08002083 public static final int DATA_CONNECTION_HSPAP = 15;
2084 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002085
Dianne Hackborn627bba72009-03-24 22:32:56 -07002086 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002087 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002088 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08002089 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002090 };
Bookatzc8c44962017-05-11 12:12:54 -07002091
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002092 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002093
Dianne Hackborn627bba72009-03-24 22:32:56 -07002094 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002095 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002096 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002097 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002098 * {@hide}
2099 */
2100 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002101 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002104 * Returns the number of times the phone has entered the given data
2105 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002106 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002107 * {@hide}
2108 */
2109 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002110
Dianne Hackborn3251b902014-06-20 14:40:53 -07002111 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2112 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2113 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2114 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2115 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2116 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2117 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2118 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2119 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2120 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2121 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2122 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2123 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2124
2125 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2126
2127 static final String[] WIFI_SUPPL_STATE_NAMES = {
2128 "invalid", "disconn", "disabled", "inactive", "scanning",
2129 "authenticating", "associating", "associated", "4-way-handshake",
2130 "group-handshake", "completed", "dormant", "uninit"
2131 };
2132
2133 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2134 "inv", "dsc", "dis", "inact", "scan",
2135 "auth", "ascing", "asced", "4-way",
2136 "group", "compl", "dorm", "uninit"
2137 };
2138
Mike Mac2f518a2017-09-19 16:06:03 -07002139 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002140 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002141 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2142 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002143 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002144 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2145 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2146 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002147 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002148 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002149 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002150 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002151 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2152 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Mike Mac2f518a2017-09-19 16:06:03 -07002153 new BitDescription(HistoryItem.STATE_SCREEN_DOZE_FLAG, "screen_doze", "Sd"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002154 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2155 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2156 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2157 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2158 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2159 new String[] {"in", "out", "emergency", "off"},
2160 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002161 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2162 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2163 SignalStrength.SIGNAL_STRENGTH_NAMES,
2164 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002165 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2166 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2167 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002168 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002169
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002170 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2171 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002172 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002173 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002174 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002175 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002176 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002177 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2178 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2179 new String[] { "off", "light", "full", "???" },
2180 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002181 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2182 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2183 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002184 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2185 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2186 new String[] { "0", "1", "2", "3", "4" },
2187 new String[] { "0", "1", "2", "3", "4" }),
2188 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2189 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2190 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002191 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002192 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002193 };
2194
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002195 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002196 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002197 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2198 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002199 };
2200
2201 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002202 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002203 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002204 "Esw", "Ewa", "Elw", "Eec"
2205 };
2206
2207 @FunctionalInterface
2208 public interface IntToString {
2209 String applyAsString(int val);
2210 }
2211
2212 private static final IntToString sUidToString = UserHandle::formatUid;
2213 private static final IntToString sIntToString = Integer::toString;
2214
2215 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2216 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2217 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2218 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2219 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002220 };
2221
Dianne Hackborn617f8772009-03-31 15:04:46 -07002222 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002223 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002224 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002225 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002226 * {@hide}
2227 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002228 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002229
2230 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002231 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002232 * been in the running state while the device was running on battery.
2233 *
2234 * {@hide}
2235 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002236 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002237
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002238 public static final int WIFI_STATE_OFF = 0;
2239 public static final int WIFI_STATE_OFF_SCANNING = 1;
2240 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2241 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2242 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2243 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2244 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2245 public static final int WIFI_STATE_SOFT_AP = 7;
2246
2247 static final String[] WIFI_STATE_NAMES = {
2248 "off", "scanning", "no_net", "disconn",
2249 "sta", "p2p", "sta_p2p", "soft_ap"
2250 };
2251
2252 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2253
2254 /**
2255 * Returns the time in microseconds that WiFi has been running in the given state.
2256 *
2257 * {@hide}
2258 */
2259 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002260 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002261
2262 /**
2263 * Returns the number of times that WiFi has entered the given state.
2264 *
2265 * {@hide}
2266 */
2267 public abstract int getWifiStateCount(int wifiState, int which);
2268
The Android Open Source Project10592532009-03-18 17:39:46 -07002269 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002270 * Returns the time in microseconds that the wifi supplicant has been
2271 * in a given state.
2272 *
2273 * {@hide}
2274 */
2275 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2276
2277 /**
2278 * Returns the number of times that the wifi supplicant has transitioned
2279 * to a given state.
2280 *
2281 * {@hide}
2282 */
2283 public abstract int getWifiSupplStateCount(int state, int which);
2284
2285 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2286
2287 /**
2288 * Returns the time in microseconds that WIFI has been running with
2289 * the given signal strength.
2290 *
2291 * {@hide}
2292 */
2293 public abstract long getWifiSignalStrengthTime(int strengthBin,
2294 long elapsedRealtimeUs, int which);
2295
2296 /**
2297 * Returns the number of times WIFI has entered the given signal strength.
2298 *
2299 * {@hide}
2300 */
2301 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2302
2303 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002304 * Returns the time in microseconds that the flashlight has been on while the device was
2305 * running on battery.
2306 *
2307 * {@hide}
2308 */
2309 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2310
2311 /**
2312 * Returns the number of times that the flashlight has been turned on while the device was
2313 * running on battery.
2314 *
2315 * {@hide}
2316 */
2317 public abstract long getFlashlightOnCount(int which);
2318
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002319 /**
2320 * Returns the time in microseconds that the camera has been on while the device was
2321 * running on battery.
2322 *
2323 * {@hide}
2324 */
2325 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2326
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002327 /**
2328 * Returns the time in microseconds that bluetooth scans were running while the device was
2329 * on battery.
2330 *
2331 * {@hide}
2332 */
2333 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002334
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002335 public static final int NETWORK_MOBILE_RX_DATA = 0;
2336 public static final int NETWORK_MOBILE_TX_DATA = 1;
2337 public static final int NETWORK_WIFI_RX_DATA = 2;
2338 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002339 public static final int NETWORK_BT_RX_DATA = 4;
2340 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002341 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2342 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2343 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2344 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2345 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002346
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002347 public abstract long getNetworkActivityBytes(int type, int which);
2348 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002349
Adam Lesinskie08af192015-03-25 16:42:59 -07002350 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002351 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002352 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002353 * actual power data.
2354 */
2355 public abstract boolean hasWifiActivityReporting();
2356
2357 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002358 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2359 * in various radio controller states, such as transmit, receive, and idle.
2360 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002361 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002362 public abstract ControllerActivityCounter getWifiControllerActivity();
2363
2364 /**
2365 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2366 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2367 * actual power data.
2368 */
2369 public abstract boolean hasBluetoothActivityReporting();
2370
2371 /**
2372 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2373 * in various radio controller states, such as transmit, receive, and idle.
2374 * @return non-null {@link ControllerActivityCounter}
2375 */
2376 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2377
2378 /**
2379 * Returns true if the BatteryStats object has detailed modem power reports.
2380 * When true, calling {@link #getModemControllerActivity()} will yield the
2381 * actual power data.
2382 */
2383 public abstract boolean hasModemActivityReporting();
2384
2385 /**
2386 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2387 * in various radio controller states, such as transmit, receive, and idle.
2388 * @return non-null {@link ControllerActivityCounter}
2389 */
2390 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002391
The Android Open Source Project10592532009-03-18 17:39:46 -07002392 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002393 * Return the wall clock time when battery stats data collection started.
2394 */
2395 public abstract long getStartClockTime();
2396
2397 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002398 * Return platform version tag that we were running in when the battery stats started.
2399 */
2400 public abstract String getStartPlatformVersion();
2401
2402 /**
2403 * Return platform version tag that we were running in when the battery stats ended.
2404 */
2405 public abstract String getEndPlatformVersion();
2406
2407 /**
2408 * Return the internal version code of the parcelled format.
2409 */
2410 public abstract int getParcelVersion();
2411
2412 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 * Return whether we are currently running on battery.
2414 */
2415 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 /**
2418 * Returns a SparseArray containing the statistics for each uid.
2419 */
2420 public abstract SparseArray<? extends Uid> getUidStats();
2421
2422 /**
2423 * Returns the current battery uptime in microseconds.
2424 *
2425 * @param curTime the amount of elapsed realtime in microseconds.
2426 */
2427 public abstract long getBatteryUptime(long curTime);
2428
2429 /**
2430 * Returns the current battery realtime in microseconds.
2431 *
2432 * @param curTime the amount of elapsed realtime in microseconds.
2433 */
2434 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002435
The Android Open Source Project10592532009-03-18 17:39:46 -07002436 /**
Evan Millar633a1742009-04-02 16:36:33 -07002437 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002438 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002439 */
Evan Millar633a1742009-04-02 16:36:33 -07002440 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002441
The Android Open Source Project10592532009-03-18 17:39:46 -07002442 /**
Evan Millar633a1742009-04-02 16:36:33 -07002443 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2444 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002445 */
Evan Millar633a1742009-04-02 16:36:33 -07002446 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447
2448 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002449 * Get the amount the battery has discharged since the stats were
2450 * last reset after charging, as a lower-end approximation.
2451 */
2452 public abstract int getLowDischargeAmountSinceCharge();
2453
2454 /**
2455 * Get the amount the battery has discharged since the stats were
2456 * last reset after charging, as an upper-end approximation.
2457 */
2458 public abstract int getHighDischargeAmountSinceCharge();
2459
2460 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002461 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2462 */
2463 public abstract int getDischargeAmount(int which);
2464
2465 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002466 * Get the amount the battery has discharged while the screen was on,
2467 * since the last time power was unplugged.
2468 */
2469 public abstract int getDischargeAmountScreenOn();
2470
2471 /**
2472 * Get the amount the battery has discharged while the screen was on,
2473 * since the last time the device was charged.
2474 */
2475 public abstract int getDischargeAmountScreenOnSinceCharge();
2476
2477 /**
2478 * Get the amount the battery has discharged while the screen was off,
2479 * since the last time power was unplugged.
2480 */
2481 public abstract int getDischargeAmountScreenOff();
2482
2483 /**
2484 * Get the amount the battery has discharged while the screen was off,
2485 * since the last time the device was charged.
2486 */
2487 public abstract int getDischargeAmountScreenOffSinceCharge();
2488
2489 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002490 * Get the amount the battery has discharged while the screen was doze,
2491 * since the last time power was unplugged.
2492 */
2493 public abstract int getDischargeAmountScreenDoze();
2494
2495 /**
2496 * Get the amount the battery has discharged while the screen was doze,
2497 * since the last time the device was charged.
2498 */
2499 public abstract int getDischargeAmountScreenDozeSinceCharge();
2500
2501 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 * Returns the total, last, or current battery uptime in microseconds.
2503 *
2504 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002505 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 */
2507 public abstract long computeBatteryUptime(long curTime, int which);
2508
2509 /**
2510 * Returns the total, last, or current battery realtime in microseconds.
2511 *
2512 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002513 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 */
2515 public abstract long computeBatteryRealtime(long curTime, int which);
2516
2517 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002518 * Returns the total, last, or current battery screen off/doze uptime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002519 *
2520 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002521 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002522 */
2523 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2524
2525 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002526 * Returns the total, last, or current battery screen off/doze realtime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002527 *
2528 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002529 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002530 */
2531 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2532
2533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 * Returns the total, last, or current uptime in microseconds.
2535 *
2536 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002537 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 */
2539 public abstract long computeUptime(long curTime, int which);
2540
2541 /**
2542 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002543 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002545 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 */
2547 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002548
2549 /**
2550 * Compute an approximation for how much run time (in microseconds) is remaining on
2551 * the battery. Returns -1 if no time can be computed: either there is not
2552 * enough current data to make a decision, or the battery is currently
2553 * charging.
2554 *
2555 * @param curTime The current elepsed realtime in microseconds.
2556 */
2557 public abstract long computeBatteryTimeRemaining(long curTime);
2558
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002559 // The part of a step duration that is the actual time.
2560 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2561
2562 // Bits in a step duration that are the new battery level we are at.
2563 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002564 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002565
2566 // Bits in a step duration that are the initial mode we were in at that step.
2567 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002568 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002569
2570 // Bits in a step duration that indicate which modes changed during that step.
2571 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002572 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002573
2574 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2575 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2576
Santos Cordone94f0502017-02-24 12:31:20 -08002577 // The largest value for screen state that is tracked in battery states. Any values above
2578 // this should be mapped back to one of the tracked values before being tracked here.
2579 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2580
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002581 // Step duration mode: power save is on.
2582 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2583
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002584 // Step duration mode: device is currently in idle mode.
2585 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2586
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002587 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2588 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002589 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2590 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002591 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2592 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2593 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2594 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2595 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002596 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2597 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002598 };
2599 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2600 (Display.STATE_OFF-1),
2601 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002602 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002603 (Display.STATE_ON-1),
2604 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2605 (Display.STATE_DOZE-1),
2606 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2607 (Display.STATE_DOZE_SUSPEND-1),
2608 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002609 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002610 };
2611 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2612 "screen off",
2613 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002614 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002615 "screen on",
2616 "screen on power save",
2617 "screen doze",
2618 "screen doze power save",
2619 "screen doze-suspend",
2620 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002621 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002622 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002623
2624 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002625 * Return the amount of battery discharge while the screen was off, measured in
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002626 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2627 * a coulomb counter.
2628 */
Mike Mac2f518a2017-09-19 16:06:03 -07002629 public abstract long getMahDischargeScreenOff(int which);
2630
2631 /**
2632 * Return the amount of battery discharge while the screen was in doze mode, measured in
2633 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2634 * a coulomb counter.
2635 */
2636 public abstract long getMahDischargeScreenDoze(int which);
2637
2638 /**
2639 * Return the amount of battery discharge measured in micro-Ampere-hours. This will be
2640 * non-zero only if the device's battery has a coulomb counter.
2641 */
2642 public abstract long getMahDischarge(int which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002643
2644 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002645 * Returns the estimated real battery capacity, which may be less than the capacity
2646 * declared by the PowerProfile.
2647 * @return The estimated battery capacity in mAh.
2648 */
2649 public abstract int getEstimatedBatteryCapacity();
2650
2651 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002652 * @return The minimum learned battery capacity in uAh.
2653 */
2654 public abstract int getMinLearnedBatteryCapacity();
2655
2656 /**
2657 * @return The maximum learned battery capacity in uAh.
2658 */
2659 public abstract int getMaxLearnedBatteryCapacity() ;
2660
2661 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002662 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002663 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002664 public abstract LevelStepTracker getDischargeLevelStepTracker();
2665
2666 /**
2667 * Return the array of daily discharge step durations.
2668 */
2669 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002670
2671 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002672 * Compute an approximation for how much time (in microseconds) remains until the battery
2673 * is fully charged. Returns -1 if no time can be computed: either there is not
2674 * enough current data to make a decision, or the battery is currently
2675 * discharging.
2676 *
2677 * @param curTime The current elepsed realtime in microseconds.
2678 */
2679 public abstract long computeChargeTimeRemaining(long curTime);
2680
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002681 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002682 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002683 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002684 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002685
2686 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002687 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002688 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002689 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002690
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002691 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2692
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002693 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002694
Evan Millarc64edde2009-04-18 12:26:32 -07002695 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696
Bookatz50df7112017-08-04 14:53:26 -07002697 /**
2698 * Returns Timers tracking the total time of each Resource Power Manager state and voter.
2699 */
2700 public abstract Map<String, ? extends Timer> getRpmStats();
2701 /**
2702 * Returns Timers tracking the screen-off time of each Resource Power Manager state and voter.
2703 */
2704 public abstract Map<String, ? extends Timer> getScreenOffRpmStats();
2705
2706
James Carr2dd7e5e2016-07-20 18:48:39 -07002707 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2708
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002709 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2710
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002711 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 long days = seconds / (60 * 60 * 24);
2713 if (days != 0) {
2714 out.append(days);
2715 out.append("d ");
2716 }
2717 long used = days * 60 * 60 * 24;
2718
2719 long hours = (seconds - used) / (60 * 60);
2720 if (hours != 0 || used != 0) {
2721 out.append(hours);
2722 out.append("h ");
2723 }
2724 used += hours * 60 * 60;
2725
2726 long mins = (seconds-used) / 60;
2727 if (mins != 0 || used != 0) {
2728 out.append(mins);
2729 out.append("m ");
2730 }
2731 used += mins * 60;
2732
2733 if (seconds != 0 || used != 0) {
2734 out.append(seconds-used);
2735 out.append("s ");
2736 }
2737 }
2738
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002739 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002741 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 sb.append(time - (sec * 1000));
2743 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 }
2745
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002746 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002747 long sec = time / 1000;
2748 formatTimeRaw(sb, sec);
2749 sb.append(time - (sec * 1000));
2750 sb.append("ms");
2751 }
2752
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002753 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002755 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 }
2757 float perc = ((float)num) / ((float)den) * 100;
2758 mFormatBuilder.setLength(0);
2759 mFormatter.format("%.1f%%", perc);
2760 return mFormatBuilder.toString();
2761 }
2762
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002763 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002764 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07002765
Evan Millar22ac0432009-03-31 11:33:18 -07002766 if (bytes < BYTES_PER_KB) {
2767 return bytes + "B";
2768 } else if (bytes < BYTES_PER_MB) {
2769 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2770 return mFormatBuilder.toString();
2771 } else if (bytes < BYTES_PER_GB){
2772 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2773 return mFormatBuilder.toString();
2774 } else {
2775 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2776 return mFormatBuilder.toString();
2777 }
2778 }
2779
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002780 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002781 if (timer != null) {
2782 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002783 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002784 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2785 return totalTimeMillis;
2786 }
2787 return 0;
2788 }
2789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 /**
2791 *
2792 * @param sb a StringBuilder object.
2793 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002794 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002796 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 * @param linePrefix a String to be prepended to each line of output.
2798 * @return the line prefix
2799 */
2800 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002801 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002804 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002805
Evan Millarc64edde2009-04-18 12:26:32 -07002806 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 if (totalTimeMillis != 0) {
2808 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002809 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002810 if (name != null) {
2811 sb.append(name);
2812 sb.append(' ');
2813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 sb.append('(');
2815 sb.append(count);
2816 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002817 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2818 if (maxDurationMs >= 0) {
2819 sb.append(" max=");
2820 sb.append(maxDurationMs);
2821 }
Bookatz506a8182017-05-01 14:18:42 -07002822 // Put actual time if it is available and different from totalTimeMillis.
2823 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
2824 if (totalDurMs > totalTimeMillis) {
2825 sb.append(" actual=");
2826 sb.append(totalDurMs);
2827 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07002828 if (timer.isRunningLocked()) {
2829 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2830 if (currentMs >= 0) {
2831 sb.append(" (running for ");
2832 sb.append(currentMs);
2833 sb.append("ms)");
2834 } else {
2835 sb.append(" (running)");
2836 }
2837 }
2838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 return ", ";
2840 }
2841 }
2842 return linePrefix;
2843 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002844
2845 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07002846 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002847 *
2848 * @param pw a PrintWriter object to print to.
2849 * @param sb a StringBuilder object.
2850 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08002851 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002852 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2853 * @param prefix a String to be prepended to each line of output.
2854 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002855 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002856 */
2857 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07002858 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002859 if (timer != null) {
2860 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07002861 final long totalTimeMs = (timer.getTotalTimeLocked(
2862 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002863 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002864 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002865 sb.setLength(0);
2866 sb.append(prefix);
2867 sb.append(" ");
2868 sb.append(type);
2869 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002870 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002871 sb.append("realtime (");
2872 sb.append(count);
2873 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002874 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
2875 if (maxDurationMs >= 0) {
2876 sb.append(" max=");
2877 sb.append(maxDurationMs);
2878 }
2879 if (timer.isRunningLocked()) {
2880 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
2881 if (currentMs >= 0) {
2882 sb.append(" (running for ");
2883 sb.append(currentMs);
2884 sb.append("ms)");
2885 } else {
2886 sb.append(" (running)");
2887 }
2888 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002889 pw.println(sb.toString());
2890 return true;
2891 }
2892 }
2893 return false;
2894 }
Bookatzc8c44962017-05-11 12:12:54 -07002895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 /**
2897 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07002898 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 * @param sb a StringBuilder object.
2900 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002901 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002903 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 * @param linePrefix a String to be prepended to each line of output.
2905 * @return the line prefix
2906 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002907 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2908 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 long totalTimeMicros = 0;
2910 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07002911 long max = 0;
2912 long current = 0;
2913 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002915 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07002916 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002917 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2918 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07002919 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 }
2921 sb.append(linePrefix);
2922 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2923 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002924 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002926 sb.append(',');
2927 sb.append(current);
2928 sb.append(',');
2929 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07002930 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
2931 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
2932 // totalDuration independent of totalTimeMicros (since they are not pooled).
2933 if (name != null) {
2934 sb.append(',');
2935 sb.append(totalDuration);
2936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 return ",";
2938 }
Bookatz506a8182017-05-01 14:18:42 -07002939
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002940 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
2941 String type) {
2942 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2943 pw.print(',');
2944 pw.print(uid);
2945 pw.print(',');
2946 pw.print(category);
2947 pw.print(',');
2948 pw.print(type);
2949 }
2950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 /**
2952 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07002953 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 * @param pw the PageWriter to dump log to
2955 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2956 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2957 * @param args type-dependent data arguments
2958 */
Bookatzc8c44962017-05-11 12:12:54 -07002959 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002961 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002962 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002963 pw.print(',');
2964 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002966 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002968
2969 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002970 * Dump a given timer stat for terse checkin mode.
2971 *
2972 * @param pw the PageWriter to dump log to
2973 * @param uid the UID to log
2974 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2975 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2976 * @param timer a {@link Timer} to dump stats for
2977 * @param rawRealtime the current elapsed realtime of the system in microseconds
2978 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2979 */
2980 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2981 Timer timer, long rawRealtime, int which) {
2982 if (timer != null) {
2983 // Convert from microseconds to milliseconds with rounding
2984 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2985 / 1000;
2986 final int count = timer.getCountLocked(which);
2987 if (totalTime != 0) {
2988 dumpLine(pw, uid, category, type, totalTime, count);
2989 }
2990 }
2991 }
2992
2993 /**
Kweku Adams2f73ecd2017-09-27 16:59:19 -07002994 * Dump a given timer stat to the proto stream.
2995 *
2996 * @param proto the ProtoOutputStream to log to
2997 * @param fieldId type of data, the field to save to (e.g. AggregatedBatteryStats.WAKELOCK)
2998 * @param timer a {@link Timer} to dump stats for
2999 * @param rawRealtimeUs the current elapsed realtime of the system in microseconds
3000 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3001 */
3002 private static void dumpTimer(ProtoOutputStream proto, long fieldId,
3003 Timer timer, long rawRealtime, int which) {
3004 if (timer == null) {
3005 return;
3006 }
3007 // Convert from microseconds to milliseconds with rounding
3008 final long totalTimeMs = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3009 final int count = timer.getCountLocked(which);
3010 if (totalTimeMs != 0 || count != 0) {
3011 final long token = proto.start(fieldId);
3012 proto.write(TimerProto.DURATION_MS, totalTimeMs);
3013 proto.write(TimerProto.COUNT, count);
3014 proto.end(token);
3015 }
3016 }
3017
3018 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003019 * Checks if the ControllerActivityCounter has any data worth dumping.
3020 */
3021 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
3022 if (counter == null) {
3023 return false;
3024 }
3025
3026 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
3027 || counter.getRxTimeCounter().getCountLocked(which) != 0
3028 || counter.getPowerCounter().getCountLocked(which) != 0) {
3029 return true;
3030 }
3031
3032 for (LongCounter c : counter.getTxTimeCounters()) {
3033 if (c.getCountLocked(which) != 0) {
3034 return true;
3035 }
3036 }
3037 return false;
3038 }
3039
3040 /**
3041 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3042 * The order of the arguments in the final check in line is:
3043 *
3044 * idle, rx, power, tx...
3045 *
3046 * where tx... is one or more transmit level times.
3047 */
3048 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
3049 String type,
3050 ControllerActivityCounter counter,
3051 int which) {
3052 if (!controllerActivityHasData(counter, which)) {
3053 return;
3054 }
3055
3056 dumpLineHeader(pw, uid, category, type);
3057 pw.print(",");
3058 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
3059 pw.print(",");
3060 pw.print(counter.getRxTimeCounter().getCountLocked(which));
3061 pw.print(",");
3062 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3063 for (LongCounter c : counter.getTxTimeCounters()) {
3064 pw.print(",");
3065 pw.print(c.getCountLocked(which));
3066 }
3067 pw.println();
3068 }
3069
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003070 /**
3071 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3072 */
3073 private static void dumpControllerActivityProto(ProtoOutputStream proto, long fieldId,
3074 ControllerActivityCounter counter,
3075 int which) {
3076 if (!controllerActivityHasData(counter, which)) {
3077 return;
3078 }
3079
3080 final long cToken = proto.start(fieldId);
3081
3082 proto.write(ControllerActivityProto.IDLE_DURATION_MS,
3083 counter.getIdleTimeCounter().getCountLocked(which));
3084 proto.write(ControllerActivityProto.RX_DURATION_MS,
3085 counter.getRxTimeCounter().getCountLocked(which));
3086 proto.write(ControllerActivityProto.POWER_MAH,
3087 counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3088
3089 long tToken;
3090 LongCounter[] txCounters = counter.getTxTimeCounters();
3091 for (int i = 0; i < txCounters.length; ++i) {
3092 LongCounter c = txCounters[i];
3093 tToken = proto.start(ControllerActivityProto.TX);
3094 proto.write(ControllerActivityProto.TxLevel.LEVEL, i);
3095 proto.write(ControllerActivityProto.TxLevel.DURATION_MS, c.getCountLocked(which));
3096 proto.end(tToken);
3097 }
3098
3099 proto.end(cToken);
3100 }
3101
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003102 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
3103 String prefix, String controllerName,
3104 ControllerActivityCounter counter,
3105 int which) {
3106 if (controllerActivityHasData(counter, which)) {
3107 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
3108 }
3109 }
3110
3111 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
3112 String controllerName,
3113 ControllerActivityCounter counter, int which) {
3114 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
3115 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
3116 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
3117 long totalTxTimeMs = 0;
3118 for (LongCounter txState : counter.getTxTimeCounters()) {
3119 totalTxTimeMs += txState.getCountLocked(which);
3120 }
3121
3122 final long totalTimeMs = idleTimeMs + rxTimeMs + totalTxTimeMs;
3123
3124 sb.setLength(0);
3125 sb.append(prefix);
3126 sb.append(" ");
3127 sb.append(controllerName);
3128 sb.append(" Idle time: ");
3129 formatTimeMs(sb, idleTimeMs);
3130 sb.append("(");
3131 sb.append(formatRatioLocked(idleTimeMs, totalTimeMs));
3132 sb.append(")");
3133 pw.println(sb.toString());
3134
3135 sb.setLength(0);
3136 sb.append(prefix);
3137 sb.append(" ");
3138 sb.append(controllerName);
3139 sb.append(" Rx time: ");
3140 formatTimeMs(sb, rxTimeMs);
3141 sb.append("(");
3142 sb.append(formatRatioLocked(rxTimeMs, totalTimeMs));
3143 sb.append(")");
3144 pw.println(sb.toString());
3145
3146 sb.setLength(0);
3147 sb.append(prefix);
3148 sb.append(" ");
3149 sb.append(controllerName);
3150 sb.append(" Tx time: ");
3151 formatTimeMs(sb, totalTxTimeMs);
3152 sb.append("(");
3153 sb.append(formatRatioLocked(totalTxTimeMs, totalTimeMs));
3154 sb.append(")");
3155 pw.println(sb.toString());
3156
3157 final int numTxLvls = counter.getTxTimeCounters().length;
3158 if (numTxLvls > 1) {
3159 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3160 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3161 sb.setLength(0);
3162 sb.append(prefix);
3163 sb.append(" [");
3164 sb.append(lvl);
3165 sb.append("] ");
3166 formatTimeMs(sb, txLvlTimeMs);
3167 sb.append("(");
3168 sb.append(formatRatioLocked(txLvlTimeMs, totalTxTimeMs));
3169 sb.append(")");
3170 pw.println(sb.toString());
3171 }
3172 }
3173
3174 sb.setLength(0);
3175 sb.append(prefix);
3176 sb.append(" ");
3177 sb.append(controllerName);
3178 sb.append(" Power drain: ").append(
3179 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
3180 sb.append("mAh");
3181 pw.println(sb.toString());
3182 }
3183
3184 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003185 * Temporary for settings.
3186 */
3187 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3188 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3189 }
3190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 /**
3192 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003193 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003195 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003196 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3197 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3199 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07003200 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003202 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3203 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003204 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3205 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3206 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 final long totalRealtime = computeRealtime(rawRealtime, which);
3208 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003209 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Mike Mac2f518a2017-09-19 16:06:03 -07003210 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003211 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003212 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003213 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3214 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003215 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003216 rawRealtime, which);
3217 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3218 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003219 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003220 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003221 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003222 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Mike Mac2f518a2017-09-19 16:06:03 -07003223 final long dischargeCount = getMahDischarge(which);
3224 final long dischargeScreenOffCount = getMahDischargeScreenOff(which);
3225 final long dischargeScreenDozeCount = getMahDischargeScreenDoze(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003226
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003227 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003228
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003229 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003230 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003231
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003232 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003235 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003236 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003237 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003238 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003239 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003240 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003241 getEstimatedBatteryCapacity(),
Mike Mac2f518a2017-09-19 16:06:03 -07003242 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity(),
3243 screenDozeTime / 1000);
Adam Lesinski67c134f2016-06-10 15:15:08 -07003244
Bookatzc8c44962017-05-11 12:12:54 -07003245
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003246 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003247 long fullWakeLockTimeTotal = 0;
3248 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003249
Evan Millar22ac0432009-03-31 11:33:18 -07003250 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003251 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003252
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003253 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3254 = u.getWakelockStats();
3255 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3256 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003257
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003258 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3259 if (fullWakeTimer != null) {
3260 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3261 which);
3262 }
3263
3264 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3265 if (partialWakeTimer != null) {
3266 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3267 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003268 }
3269 }
3270 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003271
3272 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003273 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3274 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3275 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3276 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3277 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3278 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3279 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3280 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003281 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3282 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003283 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3284 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003285 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3286 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003287
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003288 // Dump Modem controller stats
3289 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3290 getModemControllerActivity(), which);
3291
Adam Lesinskie283d332015-04-16 12:29:25 -07003292 // Dump Wifi controller stats
3293 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3294 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003295 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003296 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003297
3298 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3299 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003300
3301 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003302 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3303 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 // Dump misc stats
3306 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003307 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003308 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003309 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003310 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003311 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003312 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3313 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003314 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003315 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3316 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3317 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3318 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003319 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003320
Dianne Hackborn617f8772009-03-31 15:04:46 -07003321 // Dump screen brightness stats
3322 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3323 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003324 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003325 }
3326 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003327
Dianne Hackborn627bba72009-03-24 22:32:56 -07003328 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003329 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3330 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003331 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003332 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003333 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003334 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003335 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003336 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003337 args[i] = getPhoneSignalStrengthCount(i, which);
3338 }
3339 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003340
Dianne Hackborn627bba72009-03-24 22:32:56 -07003341 // Dump network type stats
3342 args = new Object[NUM_DATA_CONNECTION_TYPES];
3343 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003344 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003345 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003346 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3347 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3348 args[i] = getPhoneDataConnectionCount(i, which);
3349 }
3350 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003351
3352 // Dump wifi state stats
3353 args = new Object[NUM_WIFI_STATES];
3354 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003355 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003356 }
3357 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3358 for (int i=0; i<NUM_WIFI_STATES; i++) {
3359 args[i] = getWifiStateCount(i, which);
3360 }
3361 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3362
Dianne Hackborn3251b902014-06-20 14:40:53 -07003363 // Dump wifi suppl state stats
3364 args = new Object[NUM_WIFI_SUPPL_STATES];
3365 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3366 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3367 }
3368 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3369 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3370 args[i] = getWifiSupplStateCount(i, which);
3371 }
3372 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3373
3374 // Dump wifi signal strength stats
3375 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3376 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3377 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3378 }
3379 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3380 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3381 args[i] = getWifiSignalStrengthCount(i, which);
3382 }
3383 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3384
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003385 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003386 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003387 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003388 }
Bookatzc8c44962017-05-11 12:12:54 -07003389
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003390 if (which == STATS_SINCE_UNPLUGGED) {
3391 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3392 getDischargeStartLevel()-getDischargeCurrentLevel(),
3393 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003394 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
Mike Mac2f518a2017-09-19 16:06:03 -07003395 dischargeCount / 1000, dischargeScreenOffCount / 1000,
3396 getDischargeAmountScreenDoze(), dischargeScreenDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003397 } else {
3398 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3399 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003400 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003401 getDischargeAmountScreenOffSinceCharge(),
Mike Mac2f518a2017-09-19 16:06:03 -07003402 dischargeCount / 1000, dischargeScreenOffCount / 1000,
3403 getDischargeAmountScreenDozeSinceCharge(), dischargeScreenDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003404 }
Bookatzc8c44962017-05-11 12:12:54 -07003405
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003406 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003407 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003408 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003409 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003410 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003411 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003412 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3413 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003414 }
Evan Millarc64edde2009-04-18 12:26:32 -07003415 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003416 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003417 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003418 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3419 // Not doing the regular wake lock formatting to remain compatible
3420 // with the old checkin format.
3421 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3422 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003423 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003424 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003425 }
3426 }
Evan Millarc64edde2009-04-18 12:26:32 -07003427 }
Bookatzc8c44962017-05-11 12:12:54 -07003428
Bookatz50df7112017-08-04 14:53:26 -07003429 final Map<String, ? extends Timer> rpmStats = getRpmStats();
3430 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
3431 if (rpmStats.size() > 0) {
3432 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
3433 sb.setLength(0);
3434 Timer totalTimer = ent.getValue();
3435 long timeMs = (totalTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3436 int count = totalTimer.getCountLocked(which);
3437 Timer screenOffTimer = screenOffRpmStats.get(ent.getKey());
3438 long screenOffTimeMs = screenOffTimer != null
3439 ? (screenOffTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3440 int screenOffCount = screenOffTimer != null
3441 ? screenOffTimer.getCountLocked(which) : 0;
Bookatz82b341172017-09-07 19:06:08 -07003442 if (SCREEN_OFF_RPM_STATS_ENABLED) {
3443 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3444 "\"" + ent.getKey() + "\"", timeMs, count, screenOffTimeMs,
3445 screenOffCount);
3446 } else {
3447 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3448 "\"" + ent.getKey() + "\"", timeMs, count);
3449 }
Bookatz50df7112017-08-04 14:53:26 -07003450 }
3451 }
3452
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003453 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003454 helper.create(this);
3455 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003456 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003457 if (sippers != null && sippers.size() > 0) {
3458 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3459 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003460 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003461 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3462 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
3463 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003464 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003465 int uid = 0;
3466 String label;
3467 switch (bs.drainType) {
3468 case IDLE:
3469 label="idle";
3470 break;
3471 case CELL:
3472 label="cell";
3473 break;
3474 case PHONE:
3475 label="phone";
3476 break;
3477 case WIFI:
3478 label="wifi";
3479 break;
3480 case BLUETOOTH:
3481 label="blue";
3482 break;
3483 case SCREEN:
3484 label="scrn";
3485 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003486 case FLASHLIGHT:
3487 label="flashlight";
3488 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003489 case APP:
3490 uid = bs.uidObj.getUid();
3491 label = "uid";
3492 break;
3493 case USER:
3494 uid = UserHandle.getUid(bs.userId, 0);
3495 label = "user";
3496 break;
3497 case UNACCOUNTED:
3498 label = "unacc";
3499 break;
3500 case OVERCOUNTED:
3501 label = "over";
3502 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003503 case CAMERA:
3504 label = "camera";
3505 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003506 default:
3507 label = "???";
3508 }
3509 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07003510 BatteryStatsHelper.makemAh(bs.totalPowerMah),
3511 bs.shouldHide ? 1 : 0,
3512 BatteryStatsHelper.makemAh(bs.screenPowerMah),
3513 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003514 }
3515 }
3516
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003517 final long[] cpuFreqs = getCpuFreqs();
3518 if (cpuFreqs != null) {
3519 sb.setLength(0);
3520 for (int i = 0; i < cpuFreqs.length; ++i) {
3521 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3522 }
3523 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3524 }
3525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 for (int iu = 0; iu < NU; iu++) {
3527 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003528 if (reqUid >= 0 && uid != reqUid) {
3529 continue;
3530 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003531 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003533 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003534 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3535 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3536 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3537 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3538 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3539 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3540 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3541 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003542 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003543 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3544 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003545 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003546 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3547 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003548 // Background data transfers
3549 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3550 which);
3551 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3552 which);
3553 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3554 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3555 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3556 which);
3557 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3558 which);
3559 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3560 which);
3561 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3562 which);
3563
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003564 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3565 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003566 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003567 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3568 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3569 || wifiBytesBgTx > 0
3570 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3571 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003572 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3573 wifiBytesRx, wifiBytesTx,
3574 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003575 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003576 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003577 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3578 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3579 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3580 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003581 }
3582
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003583 // Dump modem controller data, per UID.
3584 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3585 u.getModemControllerActivity(), which);
3586
3587 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003588 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3589 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3590 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003591 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3592 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003593 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3594 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3595 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003596 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003597 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003598 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3599 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003600 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3601 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003602 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003603 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003605
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003606 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3607 u.getWifiControllerActivity(), which);
3608
Bookatz867c0d72017-03-07 18:23:42 -08003609 final Timer bleTimer = u.getBluetoothScanTimer();
3610 if (bleTimer != null) {
3611 // Convert from microseconds to milliseconds with rounding
3612 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3613 / 1000;
3614 if (totalTime != 0) {
3615 final int count = bleTimer.getCountLocked(which);
3616 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3617 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003618 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3619 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3620 final long actualTimeBg = bleTimerBg != null ?
3621 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003622 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003623 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3624 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003625 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3626 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3627 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3628 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3629 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
3630 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3631 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
3632 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3633 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
3634 final Timer unoptimizedScanTimerBg =
3635 u.getBluetoothUnoptimizedScanBackgroundTimer();
3636 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
3637 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3638 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
3639 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3640
Bookatz867c0d72017-03-07 18:23:42 -08003641 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07003642 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
3643 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
3644 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08003645 }
3646 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003647
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003648 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3649 u.getBluetoothControllerActivity(), which);
3650
Dianne Hackborn617f8772009-03-31 15:04:46 -07003651 if (u.hasUserActivity()) {
3652 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3653 boolean hasData = false;
3654 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3655 int val = u.getUserActivityCount(i, which);
3656 args[i] = val;
3657 if (val != 0) hasData = true;
3658 }
3659 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003660 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003661 }
3662 }
Bookatzc8c44962017-05-11 12:12:54 -07003663
3664 if (u.getAggregatedPartialWakelockTimer() != null) {
3665 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07003666 // Times are since reset (regardless of 'which')
3667 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07003668 final Timer bgTimer = timer.getSubTimer();
3669 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003670 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07003671 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
3672 }
3673
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003674 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3675 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3676 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3677 String linePrefix = "";
3678 sb.setLength(0);
3679 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3680 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003681 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3682 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003683 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003684 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
3685 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003686 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3687 rawRealtime, "w", which, linePrefix);
3688
3689 // Only log if we had at lease one wakelock...
3690 if (sb.length() > 0) {
3691 String name = wakelocks.keyAt(iw);
3692 if (name.indexOf(',') >= 0) {
3693 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003694 }
Yi Jin02483362017-08-04 11:30:44 -07003695 if (name.indexOf('\n') >= 0) {
3696 name = name.replace('\n', '_');
3697 }
3698 if (name.indexOf('\r') >= 0) {
3699 name = name.replace('\r', '_');
3700 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003701 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 }
3703 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003704
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003705 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3706 for (int isy=syncs.size()-1; isy>=0; isy--) {
3707 final Timer timer = syncs.valueAt(isy);
3708 // Convert from microseconds to milliseconds with rounding
3709 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3710 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07003711 final Timer bgTimer = timer.getSubTimer();
3712 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003713 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07003714 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003715 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003716 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07003717 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003718 }
3719 }
3720
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003721 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3722 for (int ij=jobs.size()-1; ij>=0; ij--) {
3723 final Timer timer = jobs.valueAt(ij);
3724 // Convert from microseconds to milliseconds with rounding
3725 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3726 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07003727 final Timer bgTimer = timer.getSubTimer();
3728 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003729 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07003730 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003731 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003732 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07003733 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003734 }
3735 }
3736
Dianne Hackborn94326cb2017-06-28 16:17:20 -07003737 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
3738 for (int ic=completions.size()-1; ic>=0; ic--) {
3739 SparseIntArray types = completions.valueAt(ic);
3740 if (types != null) {
3741 dumpLine(pw, uid, category, JOB_COMPLETION_DATA,
3742 "\"" + completions.keyAt(ic) + "\"",
3743 types.get(JobParameters.REASON_CANCELED, 0),
3744 types.get(JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, 0),
3745 types.get(JobParameters.REASON_PREEMPT, 0),
3746 types.get(JobParameters.REASON_TIMEOUT, 0),
3747 types.get(JobParameters.REASON_DEVICE_IDLE, 0));
3748 }
3749 }
3750
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003751 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3752 rawRealtime, which);
3753 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3754 rawRealtime, which);
3755 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3756 rawRealtime, which);
3757 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3758 rawRealtime, which);
3759
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003760 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3761 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003762 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003763 final Uid.Sensor se = sensors.valueAt(ise);
3764 final int sensorNumber = sensors.keyAt(ise);
3765 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003766 if (timer != null) {
3767 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003768 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3769 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003770 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08003771 final int count = timer.getCountLocked(which);
3772 final Timer bgTimer = se.getSensorBackgroundTime();
3773 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003774 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3775 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
3776 final long bgActualTime = bgTimer != null ?
3777 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3778 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
3779 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003780 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003781 }
3782 }
3783
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003784 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3785 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003786
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07003787 dumpTimer(pw, uid, category, FOREGROUND_ACTIVITY_DATA, u.getForegroundActivityTimer(),
3788 rawRealtime, which);
3789
3790 dumpTimer(pw, uid, category, FOREGROUND_SERVICE_DATA, u.getForegroundServiceTimer(),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003791 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003792
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003793 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003794 long totalStateTime = 0;
3795 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003796 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3797 totalStateTime += time;
3798 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003799 }
3800 if (totalStateTime > 0) {
3801 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3802 }
3803
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003804 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3805 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003806 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003807 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003808 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003809 }
3810
Sudheer Shankaa87245d2017-08-10 12:02:31 -07003811 // If the cpuFreqs is null, then don't bother checking for cpu freq times.
3812 if (cpuFreqs != null) {
3813 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
3814 // If total cpuFreqTimes is null, then we don't need to check for
3815 // screenOffCpuFreqTimes.
3816 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
3817 sb.setLength(0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003818 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
Sudheer Shankaa87245d2017-08-10 12:02:31 -07003819 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003820 }
Sudheer Shankaa87245d2017-08-10 12:02:31 -07003821 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
3822 if (screenOffCpuFreqTimeMs != null) {
3823 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
3824 sb.append("," + screenOffCpuFreqTimeMs[i]);
3825 }
3826 } else {
3827 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
3828 sb.append(",0");
3829 }
3830 }
3831 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
3832 cpuFreqTimeMs.length, sb.toString());
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003833 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003834 }
3835
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003836 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3837 = u.getProcessStats();
3838 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3839 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003840
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003841 final long userMillis = ps.getUserTime(which);
3842 final long systemMillis = ps.getSystemTime(which);
3843 final long foregroundMillis = ps.getForegroundTime(which);
3844 final int starts = ps.getStarts(which);
3845 final int numCrashes = ps.getNumCrashes(which);
3846 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003847
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003848 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3849 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003850 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
3851 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003852 }
3853 }
3854
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003855 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3856 = u.getPackageStats();
3857 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3858 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3859 int wakeups = 0;
3860 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3861 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07003862 int count = alarms.valueAt(iwa).getCountLocked(which);
3863 wakeups += count;
3864 String name = alarms.keyAt(iwa).replace(',', '_');
3865 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003866 }
3867 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3868 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3869 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3870 final long startTime = ss.getStartTime(batteryUptime, which);
3871 final int starts = ss.getStarts(which);
3872 final int launches = ss.getLaunches(which);
3873 if (startTime != 0 || starts != 0 || launches != 0) {
3874 dumpLine(pw, uid, category, APK_DATA,
3875 wakeups, // wakeup alarms
3876 packageStats.keyAt(ipkg), // Apk
3877 serviceStats.keyAt(isvc), // service
3878 startTime / 1000, // time spent started, in ms
3879 starts,
3880 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003881 }
3882 }
3883 }
3884 }
3885 }
3886
Dianne Hackborn81038902012-11-26 17:04:09 -08003887 static final class TimerEntry {
3888 final String mName;
3889 final int mId;
3890 final BatteryStats.Timer mTimer;
3891 final long mTime;
3892 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3893 mName = name;
3894 mId = id;
3895 mTimer = timer;
3896 mTime = time;
3897 }
3898 }
3899
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003900 private void printmAh(PrintWriter printer, double power) {
3901 printer.print(BatteryStatsHelper.makemAh(power));
3902 }
3903
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003904 private void printmAh(StringBuilder sb, double power) {
3905 sb.append(BatteryStatsHelper.makemAh(power));
3906 }
3907
Dianne Hackbornd953c532014-08-16 18:17:38 -07003908 /**
3909 * Temporary for settings.
3910 */
3911 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3912 int reqUid) {
3913 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3914 }
3915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003916 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003917 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003918 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003919 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3920 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07003921 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003922 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003923
3924 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3925 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3926 final long totalRealtime = computeRealtime(rawRealtime, which);
3927 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003928 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3929 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3930 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003931 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3932 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -07003933 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003934
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003935 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003936
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003937 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003938 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003940 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
3941 if (estimatedBatteryCapacity > 0) {
3942 sb.setLength(0);
3943 sb.append(prefix);
3944 sb.append(" Estimated battery capacity: ");
3945 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
3946 sb.append(" mAh");
3947 pw.println(sb.toString());
3948 }
3949
Jocelyn Dangc627d102017-04-14 13:15:14 -07003950 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
3951 if (minLearnedBatteryCapacity > 0) {
3952 sb.setLength(0);
3953 sb.append(prefix);
3954 sb.append(" Min learned battery capacity: ");
3955 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
3956 sb.append(" mAh");
3957 pw.println(sb.toString());
3958 }
3959 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
3960 if (maxLearnedBatteryCapacity > 0) {
3961 sb.setLength(0);
3962 sb.append(prefix);
3963 sb.append(" Max learned battery capacity: ");
3964 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
3965 sb.append(" mAh");
3966 pw.println(sb.toString());
3967 }
3968
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003969 sb.setLength(0);
3970 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07003971 sb.append(" Time on battery: ");
3972 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3973 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3974 sb.append(") realtime, ");
3975 formatTimeMs(sb, whichBatteryUptime / 1000);
3976 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, whichBatteryRealtime));
3977 sb.append(") uptime");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003978 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07003979
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003980 sb.setLength(0);
3981 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07003982 sb.append(" Time on battery screen off: ");
3983 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3984 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, whichBatteryRealtime));
3985 sb.append(") realtime, ");
3986 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3987 sb.append("(");
3988 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, whichBatteryRealtime));
3989 sb.append(") uptime");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003990 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07003991
3992 sb.setLength(0);
3993 sb.append(prefix);
3994 sb.append(" Time on battery screen doze: ");
3995 formatTimeMs(sb, screenDozeTime / 1000); sb.append("(");
3996 sb.append(formatRatioLocked(screenDozeTime, whichBatteryRealtime));
3997 sb.append(")");
3998 pw.println(sb.toString());
3999
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004000 sb.setLength(0);
4001 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004002 sb.append(" Total run time: ");
4003 formatTimeMs(sb, totalRealtime / 1000);
4004 sb.append("realtime, ");
4005 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004006 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07004007 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004008 if (batteryTimeRemaining >= 0) {
4009 sb.setLength(0);
4010 sb.append(prefix);
4011 sb.append(" Battery time remaining: ");
4012 formatTimeMs(sb, batteryTimeRemaining / 1000);
4013 pw.println(sb.toString());
4014 }
4015 if (chargeTimeRemaining >= 0) {
4016 sb.setLength(0);
4017 sb.append(prefix);
4018 sb.append(" Charge time remaining: ");
4019 formatTimeMs(sb, chargeTimeRemaining / 1000);
4020 pw.println(sb.toString());
4021 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004022
Mike Mac2f518a2017-09-19 16:06:03 -07004023 final long dischargeCount = getMahDischarge(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004024 if (dischargeCount >= 0) {
4025 sb.setLength(0);
4026 sb.append(prefix);
4027 sb.append(" Discharge: ");
4028 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
4029 sb.append(" mAh");
4030 pw.println(sb.toString());
4031 }
4032
Mike Mac2f518a2017-09-19 16:06:03 -07004033 final long dischargeScreenOffCount = getMahDischargeScreenOff(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004034 if (dischargeScreenOffCount >= 0) {
4035 sb.setLength(0);
4036 sb.append(prefix);
4037 sb.append(" Screen off discharge: ");
4038 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
4039 sb.append(" mAh");
4040 pw.println(sb.toString());
4041 }
4042
Mike Mac2f518a2017-09-19 16:06:03 -07004043 final long dischargeScreenDozeCount = getMahDischargeScreenDoze(which);
4044 if (dischargeScreenDozeCount >= 0) {
4045 sb.setLength(0);
4046 sb.append(prefix);
4047 sb.append(" Screen doze discharge: ");
4048 sb.append(BatteryStatsHelper.makemAh(dischargeScreenDozeCount / 1000.0));
4049 sb.append(" mAh");
4050 pw.println(sb.toString());
4051 }
4052
4053 final long dischargeScreenOnCount =
4054 dischargeCount - dischargeScreenOffCount - dischargeScreenDozeCount;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004055 if (dischargeScreenOnCount >= 0) {
4056 sb.setLength(0);
4057 sb.append(prefix);
4058 sb.append(" Screen on discharge: ");
4059 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
4060 sb.append(" mAh");
4061 pw.println(sb.toString());
4062 }
4063
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004064 pw.print(" Start clock time: ");
4065 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
4066
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004067 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004068 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004069 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004070 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
4071 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004072 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004073 rawRealtime, which);
4074 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
4075 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004076 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004077 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004078 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
4079 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
4080 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004081 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004082 sb.append(prefix);
4083 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
4084 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004085 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004086 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
4087 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004088 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004089 pw.println(sb.toString());
4090 sb.setLength(0);
4091 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004092 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004093 boolean didOne = false;
4094 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004095 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004096 if (time == 0) {
4097 continue;
4098 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004099 sb.append("\n ");
4100 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004101 didOne = true;
4102 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
4103 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004104 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004105 sb.append("(");
4106 sb.append(formatRatioLocked(time, screenOnTime));
4107 sb.append(")");
4108 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004109 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004110 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004111 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004112 sb.setLength(0);
4113 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004114 sb.append(" Power save mode enabled: ");
4115 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004116 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004117 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004118 sb.append(")");
4119 pw.println(sb.toString());
4120 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004121 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004122 sb.setLength(0);
4123 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004124 sb.append(" Device light idling: ");
4125 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004126 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004127 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
4128 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004129 sb.append("x");
4130 pw.println(sb.toString());
4131 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004132 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004133 sb.setLength(0);
4134 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004135 sb.append(" Idle mode light time: ");
4136 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004137 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004138 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
4139 sb.append(") ");
4140 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004141 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004142 sb.append(" -- longest ");
4143 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
4144 pw.println(sb.toString());
4145 }
4146 if (deviceIdlingTime != 0) {
4147 sb.setLength(0);
4148 sb.append(prefix);
4149 sb.append(" Device full idling: ");
4150 formatTimeMs(sb, deviceIdlingTime / 1000);
4151 sb.append("(");
4152 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004153 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004154 sb.append("x");
4155 pw.println(sb.toString());
4156 }
4157 if (deviceIdleModeFullTime != 0) {
4158 sb.setLength(0);
4159 sb.append(prefix);
4160 sb.append(" Idle mode full time: ");
4161 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
4162 sb.append("(");
4163 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
4164 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004165 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004166 sb.append("x");
4167 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004168 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004169 pw.println(sb.toString());
4170 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004171 if (phoneOnTime != 0) {
4172 sb.setLength(0);
4173 sb.append(prefix);
4174 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
4175 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004176 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004177 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004178 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004179 if (connChanges != 0) {
4180 pw.print(prefix);
4181 pw.print(" Connectivity changes: "); pw.println(connChanges);
4182 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004183
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004184 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07004185 long fullWakeLockTimeTotalMicros = 0;
4186 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08004187
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004188 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08004189
Evan Millar22ac0432009-03-31 11:33:18 -07004190 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004191 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004192
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004193 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4194 = u.getWakelockStats();
4195 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4196 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07004197
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004198 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
4199 if (fullWakeTimer != null) {
4200 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
4201 rawRealtime, which);
4202 }
4203
4204 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4205 if (partialWakeTimer != null) {
4206 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
4207 rawRealtime, which);
4208 if (totalTimeMicros > 0) {
4209 if (reqUid < 0) {
4210 // Only show the ordered list of all wake
4211 // locks if the caller is not asking for data
4212 // about a specific uid.
4213 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
4214 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08004215 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004216 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07004217 }
4218 }
4219 }
4220 }
Bookatzc8c44962017-05-11 12:12:54 -07004221
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004222 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4223 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4224 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4225 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4226 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4227 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4228 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4229 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004230 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4231 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004232
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004233 if (fullWakeLockTimeTotalMicros != 0) {
4234 sb.setLength(0);
4235 sb.append(prefix);
4236 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4237 (fullWakeLockTimeTotalMicros + 500) / 1000);
4238 pw.println(sb.toString());
4239 }
4240
4241 if (partialWakeLockTimeTotalMicros != 0) {
4242 sb.setLength(0);
4243 sb.append(prefix);
4244 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4245 (partialWakeLockTimeTotalMicros + 500) / 1000);
4246 pw.println(sb.toString());
4247 }
4248
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004249 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004250 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
4251 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
4252 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
4253 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004254 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004255 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004256 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004257 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08004258 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004259 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004260 if (time == 0) {
4261 continue;
4262 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004263 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004264 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004265 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08004266 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004267 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004268 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004269 sb.append("(");
4270 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004271 sb.append(") ");
4272 sb.append(getPhoneSignalStrengthCount(i, which));
4273 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004274 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004275 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004276 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004277
4278 sb.setLength(0);
4279 sb.append(prefix);
4280 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004281 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004282 pw.println(sb.toString());
4283
Dianne Hackborn627bba72009-03-24 22:32:56 -07004284 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004285 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004286 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004287 didOne = false;
4288 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004289 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004290 if (time == 0) {
4291 continue;
4292 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004293 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004294 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004295 didOne = true;
4296 sb.append(DATA_CONNECTION_NAMES[i]);
4297 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004298 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004299 sb.append("(");
4300 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004301 sb.append(") ");
4302 sb.append(getPhoneDataConnectionCount(i, which));
4303 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004304 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004305 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004306 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004307
4308 sb.setLength(0);
4309 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004310 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004311 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004312 formatTimeMs(sb, mobileActiveTime / 1000);
4313 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4314 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
4315 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004316 pw.println(sb.toString());
4317
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004318 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
4319 if (mobileActiveUnknownTime != 0) {
4320 sb.setLength(0);
4321 sb.append(prefix);
4322 sb.append(" Mobile radio active unknown time: ");
4323 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
4324 sb.append("(");
4325 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
4326 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
4327 sb.append("x");
4328 pw.println(sb.toString());
4329 }
4330
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004331 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
4332 if (mobileActiveAdjustedTime != 0) {
4333 sb.setLength(0);
4334 sb.append(prefix);
4335 sb.append(" Mobile radio active adjusted time: ");
4336 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
4337 sb.append("(");
4338 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
4339 sb.append(")");
4340 pw.println(sb.toString());
4341 }
4342
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004343 printControllerActivity(pw, sb, prefix, "Radio", getModemControllerActivity(), which);
4344
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004345 pw.print(prefix);
4346 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
4347 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
4348 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
4349 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004350 sb.setLength(0);
4351 sb.append(prefix);
4352 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
4353 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
4354 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
4355 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004356 sb.append(")");
4357 pw.println(sb.toString());
4358
4359 sb.setLength(0);
4360 sb.append(prefix);
4361 sb.append(" Wifi states:");
4362 didOne = false;
4363 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004364 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004365 if (time == 0) {
4366 continue;
4367 }
4368 sb.append("\n ");
4369 didOne = true;
4370 sb.append(WIFI_STATE_NAMES[i]);
4371 sb.append(" ");
4372 formatTimeMs(sb, time/1000);
4373 sb.append("(");
4374 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4375 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004376 sb.append(getWifiStateCount(i, which));
4377 sb.append("x");
4378 }
4379 if (!didOne) sb.append(" (no activity)");
4380 pw.println(sb.toString());
4381
4382 sb.setLength(0);
4383 sb.append(prefix);
4384 sb.append(" Wifi supplicant states:");
4385 didOne = false;
4386 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4387 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4388 if (time == 0) {
4389 continue;
4390 }
4391 sb.append("\n ");
4392 didOne = true;
4393 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4394 sb.append(" ");
4395 formatTimeMs(sb, time/1000);
4396 sb.append("(");
4397 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4398 sb.append(") ");
4399 sb.append(getWifiSupplStateCount(i, which));
4400 sb.append("x");
4401 }
4402 if (!didOne) sb.append(" (no activity)");
4403 pw.println(sb.toString());
4404
4405 sb.setLength(0);
4406 sb.append(prefix);
4407 sb.append(" Wifi signal levels:");
4408 didOne = false;
4409 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4410 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4411 if (time == 0) {
4412 continue;
4413 }
4414 sb.append("\n ");
4415 sb.append(prefix);
4416 didOne = true;
4417 sb.append("level(");
4418 sb.append(i);
4419 sb.append(") ");
4420 formatTimeMs(sb, time/1000);
4421 sb.append("(");
4422 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4423 sb.append(") ");
4424 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004425 sb.append("x");
4426 }
4427 if (!didOne) sb.append(" (no activity)");
4428 pw.println(sb.toString());
4429
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004430 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004431
Adam Lesinski50e47602015-12-04 17:04:54 -08004432 pw.print(prefix);
4433 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4434 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4435
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004436 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4437 sb.setLength(0);
4438 sb.append(prefix);
4439 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4440 pw.println(sb.toString());
4441
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004442 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4443 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004444
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004445 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004446
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004447 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004448 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004449 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004450 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004451 pw.println(getDischargeStartLevel());
4452 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4453 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004454 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004455 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004456 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004457 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004458 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004459 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004460 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004461 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004462 pw.println(getDischargeAmountScreenOn());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004463 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004464 pw.println(getDischargeAmountScreenOff());
4465 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4466 pw.println(getDischargeAmountScreenDoze());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004467 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004468 } else {
4469 pw.print(prefix); pw.println(" Device battery use since last full charge");
4470 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004471 pw.println(getLowDischargeAmountSinceCharge());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004472 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004473 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004474 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004475 pw.println(getDischargeAmountScreenOnSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004476 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004477 pw.println(getDischargeAmountScreenOffSinceCharge());
4478 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4479 pw.println(getDischargeAmountScreenDozeSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004480 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004481 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004482
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004483 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004484 helper.create(this);
4485 helper.refreshStats(which, UserHandle.USER_ALL);
4486 List<BatterySipper> sippers = helper.getUsageList();
4487 if (sippers != null && sippers.size() > 0) {
4488 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4489 pw.print(prefix); pw.print(" Capacity: ");
4490 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004491 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004492 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4493 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4494 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4495 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004496 pw.println();
4497 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004498 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004499 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004500 switch (bs.drainType) {
4501 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004502 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004503 break;
4504 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004505 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004506 break;
4507 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004508 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004509 break;
4510 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004511 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004512 break;
4513 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004514 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004515 break;
4516 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004517 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004518 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004519 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004520 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004521 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004522 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004523 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004524 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004525 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004526 break;
4527 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004528 pw.print(" User "); pw.print(bs.userId);
4529 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004530 break;
4531 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004532 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004533 break;
4534 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004535 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004536 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004537 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004538 pw.print(" Camera: ");
4539 break;
4540 default:
4541 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004542 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004543 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004544 printmAh(pw, bs.totalPowerMah);
4545
Adam Lesinski57123002015-06-12 16:12:07 -07004546 if (bs.usagePowerMah != bs.totalPowerMah) {
4547 // If the usage (generic power) isn't the whole amount, we list out
4548 // what components are involved in the calculation.
4549
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004550 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004551 if (bs.usagePowerMah != 0) {
4552 pw.print(" usage=");
4553 printmAh(pw, bs.usagePowerMah);
4554 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004555 if (bs.cpuPowerMah != 0) {
4556 pw.print(" cpu=");
4557 printmAh(pw, bs.cpuPowerMah);
4558 }
4559 if (bs.wakeLockPowerMah != 0) {
4560 pw.print(" wake=");
4561 printmAh(pw, bs.wakeLockPowerMah);
4562 }
4563 if (bs.mobileRadioPowerMah != 0) {
4564 pw.print(" radio=");
4565 printmAh(pw, bs.mobileRadioPowerMah);
4566 }
4567 if (bs.wifiPowerMah != 0) {
4568 pw.print(" wifi=");
4569 printmAh(pw, bs.wifiPowerMah);
4570 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004571 if (bs.bluetoothPowerMah != 0) {
4572 pw.print(" bt=");
4573 printmAh(pw, bs.bluetoothPowerMah);
4574 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004575 if (bs.gpsPowerMah != 0) {
4576 pw.print(" gps=");
4577 printmAh(pw, bs.gpsPowerMah);
4578 }
4579 if (bs.sensorPowerMah != 0) {
4580 pw.print(" sensor=");
4581 printmAh(pw, bs.sensorPowerMah);
4582 }
4583 if (bs.cameraPowerMah != 0) {
4584 pw.print(" camera=");
4585 printmAh(pw, bs.cameraPowerMah);
4586 }
4587 if (bs.flashlightPowerMah != 0) {
4588 pw.print(" flash=");
4589 printmAh(pw, bs.flashlightPowerMah);
4590 }
4591 pw.print(" )");
4592 }
Bookatz17d7d9d2017-06-08 14:50:46 -07004593
4594 // If there is additional smearing information, include it.
4595 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
4596 pw.print(" Including smearing: ");
4597 printmAh(pw, bs.totalSmearedPowerMah);
4598 pw.print(" (");
4599 if (bs.screenPowerMah != 0) {
4600 pw.print(" screen=");
4601 printmAh(pw, bs.screenPowerMah);
4602 }
4603 if (bs.proportionalSmearMah != 0) {
4604 pw.print(" proportional=");
4605 printmAh(pw, bs.proportionalSmearMah);
4606 }
4607 pw.print(" )");
4608 }
4609 if (bs.shouldHide) {
4610 pw.print(" Excluded from smearing");
4611 }
4612
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004613 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004614 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004615 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004616 }
4617
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004618 sippers = helper.getMobilemsppList();
4619 if (sippers != null && sippers.size() > 0) {
4620 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004621 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004622 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004623 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004624 sb.setLength(0);
4625 sb.append(prefix); sb.append(" Uid ");
4626 UserHandle.formatUid(sb, bs.uidObj.getUid());
4627 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4628 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4629 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004630 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004631 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004632 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004633 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004634 sb.setLength(0);
4635 sb.append(prefix);
4636 sb.append(" TOTAL TIME: ");
4637 formatTimeMs(sb, totalTime);
4638 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4639 sb.append(")");
4640 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004641 pw.println();
4642 }
4643
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004644 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4645 @Override
4646 public int compare(TimerEntry lhs, TimerEntry rhs) {
4647 long lhsTime = lhs.mTime;
4648 long rhsTime = rhs.mTime;
4649 if (lhsTime < rhsTime) {
4650 return 1;
4651 }
4652 if (lhsTime > rhsTime) {
4653 return -1;
4654 }
4655 return 0;
4656 }
4657 };
4658
4659 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004660 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4661 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004662 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004663 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4664 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4665 : kernelWakelocks.entrySet()) {
4666 final BatteryStats.Timer timer = ent.getValue();
4667 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004668 if (totalTimeMillis > 0) {
4669 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4670 }
4671 }
4672 if (ktimers.size() > 0) {
4673 Collections.sort(ktimers, timerComparator);
4674 pw.print(prefix); pw.println(" All kernel wake locks:");
4675 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004676 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004677 String linePrefix = ": ";
4678 sb.setLength(0);
4679 sb.append(prefix);
4680 sb.append(" Kernel Wake lock ");
4681 sb.append(timer.mName);
4682 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4683 which, linePrefix);
4684 if (!linePrefix.equals(": ")) {
4685 sb.append(" realtime");
4686 // Only print out wake locks that were held
4687 pw.println(sb.toString());
4688 }
4689 }
4690 pw.println();
4691 }
4692 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004693
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004694 if (timers.size() > 0) {
4695 Collections.sort(timers, timerComparator);
4696 pw.print(prefix); pw.println(" All partial wake locks:");
4697 for (int i=0; i<timers.size(); i++) {
4698 TimerEntry timer = timers.get(i);
4699 sb.setLength(0);
4700 sb.append(" Wake lock ");
4701 UserHandle.formatUid(sb, timer.mId);
4702 sb.append(" ");
4703 sb.append(timer.mName);
4704 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4705 sb.append(" realtime");
4706 pw.println(sb.toString());
4707 }
4708 timers.clear();
4709 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08004710 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004711
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004712 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004713 if (wakeupReasons.size() > 0) {
4714 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004715 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004716 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004717 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004718 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
4719 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004720 }
4721 Collections.sort(reasons, timerComparator);
4722 for (int i=0; i<reasons.size(); i++) {
4723 TimerEntry timer = reasons.get(i);
4724 String linePrefix = ": ";
4725 sb.setLength(0);
4726 sb.append(prefix);
4727 sb.append(" Wakeup reason ");
4728 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004729 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4730 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004731 pw.println(sb.toString());
4732 }
4733 pw.println();
4734 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004735 }
Evan Millar22ac0432009-03-31 11:33:18 -07004736
James Carr2dd7e5e2016-07-20 18:48:39 -07004737 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
Bookatz50df7112017-08-04 14:53:26 -07004738 if (mMemoryStats.size() > 0) {
4739 pw.println(" Memory Stats");
4740 for (int i = 0; i < mMemoryStats.size(); i++) {
4741 sb.setLength(0);
4742 sb.append(" Bandwidth ");
4743 sb.append(mMemoryStats.keyAt(i));
4744 sb.append(" Time ");
4745 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
4746 pw.println(sb.toString());
4747 }
4748 pw.println();
4749 }
4750
4751 final Map<String, ? extends Timer> rpmStats = getRpmStats();
4752 if (rpmStats.size() > 0) {
4753 pw.print(prefix); pw.println(" Resource Power Manager Stats");
4754 if (rpmStats.size() > 0) {
4755 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
4756 final String timerName = ent.getKey();
4757 final Timer timer = ent.getValue();
4758 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
4759 }
4760 }
4761 pw.println();
4762 }
Bookatz82b341172017-09-07 19:06:08 -07004763 if (SCREEN_OFF_RPM_STATS_ENABLED) {
4764 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
Bookatz50df7112017-08-04 14:53:26 -07004765 if (screenOffRpmStats.size() > 0) {
Bookatz82b341172017-09-07 19:06:08 -07004766 pw.print(prefix);
4767 pw.println(" Resource Power Manager Stats for when screen was off");
4768 if (screenOffRpmStats.size() > 0) {
4769 for (Map.Entry<String, ? extends Timer> ent : screenOffRpmStats.entrySet()) {
4770 final String timerName = ent.getKey();
4771 final Timer timer = ent.getValue();
4772 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
4773 }
Bookatz50df7112017-08-04 14:53:26 -07004774 }
Bookatz82b341172017-09-07 19:06:08 -07004775 pw.println();
Bookatz50df7112017-08-04 14:53:26 -07004776 }
James Carr2dd7e5e2016-07-20 18:48:39 -07004777 }
4778
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004779 final long[] cpuFreqs = getCpuFreqs();
4780 if (cpuFreqs != null) {
4781 sb.setLength(0);
Bookatz50df7112017-08-04 14:53:26 -07004782 sb.append(" CPU freqs:");
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004783 for (int i = 0; i < cpuFreqs.length; ++i) {
4784 sb.append(" " + cpuFreqs[i]);
4785 }
4786 pw.println(sb.toString());
Bookatz50df7112017-08-04 14:53:26 -07004787 pw.println();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004788 }
4789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004790 for (int iu=0; iu<NU; iu++) {
4791 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004792 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004793 continue;
4794 }
Bookatzc8c44962017-05-11 12:12:54 -07004795
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004796 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07004797
4798 pw.print(prefix);
4799 pw.print(" ");
4800 UserHandle.formatUid(pw, uid);
4801 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004802 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004803
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004804 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4805 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4806 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4807 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004808 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4809 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
4810
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004811 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4812 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004813 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4814 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004815
4816 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
4817 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
4818
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004819 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4820 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4821 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08004822 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
4823 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4824 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
4825 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004826 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004827
Adam Lesinski5f056f62016-07-14 16:56:08 -07004828 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
4829 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
4830
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004831 if (mobileRxBytes > 0 || mobileTxBytes > 0
4832 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004833 pw.print(prefix); pw.print(" Mobile network: ");
4834 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004835 pw.print(formatBytesLocked(mobileTxBytes));
4836 pw.print(" sent (packets "); pw.print(mobileRxPackets);
4837 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004838 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004839 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
4840 sb.setLength(0);
4841 sb.append(prefix); sb.append(" Mobile radio active: ");
4842 formatTimeMs(sb, uidMobileActiveTime / 1000);
4843 sb.append("(");
4844 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
4845 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
4846 long packets = mobileRxPackets + mobileTxPackets;
4847 if (packets == 0) {
4848 packets = 1;
4849 }
4850 sb.append(" @ ");
4851 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
4852 sb.append(" mspp");
4853 pw.println(sb.toString());
4854 }
4855
Adam Lesinski5f056f62016-07-14 16:56:08 -07004856 if (mobileWakeup > 0) {
4857 sb.setLength(0);
4858 sb.append(prefix);
4859 sb.append(" Mobile radio AP wakeups: ");
4860 sb.append(mobileWakeup);
4861 pw.println(sb.toString());
4862 }
4863
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004864 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
4865 u.getModemControllerActivity(), which);
4866
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004867 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004868 pw.print(prefix); pw.print(" Wi-Fi network: ");
4869 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004870 pw.print(formatBytesLocked(wifiTxBytes));
4871 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4872 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004873 }
4874
Dianne Hackborn62793e42015-03-09 11:15:41 -07004875 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08004876 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004877 || uidWifiRunningTime != 0) {
4878 sb.setLength(0);
4879 sb.append(prefix); sb.append(" Wifi Running: ");
4880 formatTimeMs(sb, uidWifiRunningTime / 1000);
4881 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4882 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07004883 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004884 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4885 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4886 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08004887 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004888 formatTimeMs(sb, wifiScanTime / 1000);
4889 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004890 whichBatteryRealtime)); sb.append(") ");
4891 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08004892 sb.append("x\n");
4893 // actual and background times are unpooled and since reset (regardless of 'which')
4894 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
4895 formatTimeMs(sb, wifiScanActualTime / 1000);
4896 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
4897 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4898 sb.append(") ");
4899 sb.append(wifiScanCount);
4900 sb.append("x\n");
4901 sb.append(prefix); sb.append(" Background Wifi Scan: ");
4902 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
4903 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
4904 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4905 sb.append(") ");
4906 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07004907 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004908 pw.println(sb.toString());
4909 }
4910
Adam Lesinski5f056f62016-07-14 16:56:08 -07004911 if (wifiWakeup > 0) {
4912 sb.setLength(0);
4913 sb.append(prefix);
4914 sb.append(" WiFi AP wakeups: ");
4915 sb.append(wifiWakeup);
4916 pw.println(sb.toString());
4917 }
4918
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004919 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
4920 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07004921
Adam Lesinski50e47602015-12-04 17:04:54 -08004922 if (btRxBytes > 0 || btTxBytes > 0) {
4923 pw.print(prefix); pw.print(" Bluetooth network: ");
4924 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
4925 pw.print(formatBytesLocked(btTxBytes));
4926 pw.println(" sent");
4927 }
4928
Bookatz867c0d72017-03-07 18:23:42 -08004929 final Timer bleTimer = u.getBluetoothScanTimer();
4930 if (bleTimer != null) {
4931 // Convert from microseconds to milliseconds with rounding
4932 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
4933 / 1000;
4934 if (totalTimeMs != 0) {
4935 final int count = bleTimer.getCountLocked(which);
4936 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
4937 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004938 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4939 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
4940 final long actualTimeMsBg = bleTimerBg != null ?
4941 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07004942 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07004943 final int resultCount = u.getBluetoothScanResultCounter() != null ?
4944 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07004945 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
4946 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
4947 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
4948 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
4949 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
4950 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4951 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
4952 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
4953 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
4954 final Timer unoptimizedScanTimerBg =
4955 u.getBluetoothUnoptimizedScanBackgroundTimer();
4956 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
4957 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4958 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
4959 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004960
4961 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08004962 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07004963 sb.append(prefix);
4964 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08004965 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07004966 sb.append(" (");
4967 sb.append(count);
4968 sb.append(" times)");
4969 if (bleTimer.isRunningLocked()) {
4970 sb.append(" (currently running)");
4971 }
4972 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08004973 }
Bookatzb1f04f32017-05-19 13:57:32 -07004974
4975 sb.append(prefix);
4976 sb.append(" Bluetooth Scan (total actual realtime): ");
4977 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
4978 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08004979 sb.append(count);
4980 sb.append(" times)");
4981 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07004982 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08004983 }
Bookatzb1f04f32017-05-19 13:57:32 -07004984 sb.append("\n");
4985 if (actualTimeMsBg > 0 || countBg > 0) {
4986 sb.append(prefix);
4987 sb.append(" Bluetooth Scan (background realtime): ");
4988 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
4989 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08004990 sb.append(countBg);
4991 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07004992 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
4993 sb.append(" (currently running in background)");
4994 }
4995 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08004996 }
Bookatzb1f04f32017-05-19 13:57:32 -07004997
4998 sb.append(prefix);
4999 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07005000 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07005001 sb.append(" (");
5002 sb.append(resultCountBg);
5003 sb.append(" in background)");
5004
5005 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
5006 sb.append("\n");
5007 sb.append(prefix);
5008 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
5009 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
5010 sb.append(" (max ");
5011 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
5012 sb.append(")");
5013 if (unoptimizedScanTimer != null
5014 && unoptimizedScanTimer.isRunningLocked()) {
5015 sb.append(" (currently running unoptimized)");
5016 }
5017 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
5018 sb.append("\n");
5019 sb.append(prefix);
5020 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
5021 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
5022 sb.append(" (max ");
5023 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
5024 sb.append(")");
5025 if (unoptimizedScanTimerBg.isRunningLocked()) {
5026 sb.append(" (currently running unoptimized in background)");
5027 }
5028 }
5029 }
Bookatz867c0d72017-03-07 18:23:42 -08005030 pw.println(sb.toString());
5031 uidActivity = true;
5032 }
5033 }
5034
5035
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005036
Dianne Hackborn617f8772009-03-31 15:04:46 -07005037 if (u.hasUserActivity()) {
5038 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07005039 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005040 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005041 if (val != 0) {
5042 if (!hasData) {
5043 sb.setLength(0);
5044 sb.append(" User activity: ");
5045 hasData = true;
5046 } else {
5047 sb.append(", ");
5048 }
5049 sb.append(val);
5050 sb.append(" ");
5051 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
5052 }
5053 }
5054 if (hasData) {
5055 pw.println(sb.toString());
5056 }
5057 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005058
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005059 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
5060 = u.getWakelockStats();
5061 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005062 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005063 int countWakelock = 0;
5064 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
5065 final Uid.Wakelock wl = wakelocks.valueAt(iw);
5066 String linePrefix = ": ";
5067 sb.setLength(0);
5068 sb.append(prefix);
5069 sb.append(" Wake lock ");
5070 sb.append(wakelocks.keyAt(iw));
5071 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
5072 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005073 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
5074 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005075 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005076 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
5077 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005078 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
5079 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005080 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
5081 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07005082 sb.append(" realtime");
5083 pw.println(sb.toString());
5084 uidActivity = true;
5085 countWakelock++;
5086
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005087 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
5088 rawRealtime, which);
5089 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
5090 rawRealtime, which);
5091 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
5092 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005093 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07005094 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005095 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005096 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07005097 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
5098 // pooled and therefore just a lower bound)
5099 long actualTotalPartialWakelock = 0;
5100 long actualBgPartialWakelock = 0;
5101 if (u.getAggregatedPartialWakelockTimer() != null) {
5102 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
5103 // Convert from microseconds to milliseconds with rounding
5104 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07005105 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07005106 final Timer bgAggTimer = aggTimer.getSubTimer();
5107 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005108 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07005109 }
5110
5111 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
5112 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
5113 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005114 sb.setLength(0);
5115 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005116 sb.append(" TOTAL wake: ");
5117 boolean needComma = false;
5118 if (totalFullWakelock != 0) {
5119 needComma = true;
5120 formatTimeMs(sb, totalFullWakelock);
5121 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005122 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005123 if (totalPartialWakelock != 0) {
5124 if (needComma) {
5125 sb.append(", ");
5126 }
5127 needComma = true;
5128 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07005129 sb.append("blamed partial");
5130 }
5131 if (actualTotalPartialWakelock != 0) {
5132 if (needComma) {
5133 sb.append(", ");
5134 }
5135 needComma = true;
5136 formatTimeMs(sb, actualTotalPartialWakelock);
5137 sb.append("actual partial");
5138 }
5139 if (actualBgPartialWakelock != 0) {
5140 if (needComma) {
5141 sb.append(", ");
5142 }
5143 needComma = true;
5144 formatTimeMs(sb, actualBgPartialWakelock);
5145 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005146 }
5147 if (totalWindowWakelock != 0) {
5148 if (needComma) {
5149 sb.append(", ");
5150 }
5151 needComma = true;
5152 formatTimeMs(sb, totalWindowWakelock);
5153 sb.append("window");
5154 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005155 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07005156 if (needComma) {
5157 sb.append(",");
5158 }
5159 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005160 formatTimeMs(sb, totalDrawWakelock);
5161 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07005162 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005163 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005164 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005165 }
5166 }
5167
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005168 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
5169 for (int isy=syncs.size()-1; isy>=0; isy--) {
5170 final Timer timer = syncs.valueAt(isy);
5171 // Convert from microseconds to milliseconds with rounding
5172 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5173 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07005174 final Timer bgTimer = timer.getSubTimer();
5175 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005176 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07005177 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005178 sb.setLength(0);
5179 sb.append(prefix);
5180 sb.append(" Sync ");
5181 sb.append(syncs.keyAt(isy));
5182 sb.append(": ");
5183 if (totalTime != 0) {
5184 formatTimeMs(sb, totalTime);
5185 sb.append("realtime (");
5186 sb.append(count);
5187 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07005188 if (bgTime > 0) {
5189 sb.append(", ");
5190 formatTimeMs(sb, bgTime);
5191 sb.append("background (");
5192 sb.append(bgCount);
5193 sb.append(" times)");
5194 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005195 } else {
5196 sb.append("(not used)");
5197 }
5198 pw.println(sb.toString());
5199 uidActivity = true;
5200 }
5201
5202 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
5203 for (int ij=jobs.size()-1; ij>=0; ij--) {
5204 final Timer timer = jobs.valueAt(ij);
5205 // Convert from microseconds to milliseconds with rounding
5206 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5207 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07005208 final Timer bgTimer = timer.getSubTimer();
5209 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005210 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07005211 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005212 sb.setLength(0);
5213 sb.append(prefix);
5214 sb.append(" Job ");
5215 sb.append(jobs.keyAt(ij));
5216 sb.append(": ");
5217 if (totalTime != 0) {
5218 formatTimeMs(sb, totalTime);
5219 sb.append("realtime (");
5220 sb.append(count);
5221 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07005222 if (bgTime > 0) {
5223 sb.append(", ");
5224 formatTimeMs(sb, bgTime);
5225 sb.append("background (");
5226 sb.append(bgCount);
5227 sb.append(" times)");
5228 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005229 } else {
5230 sb.append("(not used)");
5231 }
5232 pw.println(sb.toString());
5233 uidActivity = true;
5234 }
5235
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005236 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
5237 for (int ic=completions.size()-1; ic>=0; ic--) {
5238 SparseIntArray types = completions.valueAt(ic);
5239 if (types != null) {
5240 pw.print(prefix);
5241 pw.print(" Job Completions ");
5242 pw.print(completions.keyAt(ic));
5243 pw.print(":");
5244 for (int it=0; it<types.size(); it++) {
5245 pw.print(" ");
5246 pw.print(JobParameters.getReasonName(types.keyAt(it)));
5247 pw.print("(");
5248 pw.print(types.valueAt(it));
5249 pw.print("x)");
5250 }
5251 pw.println();
5252 }
5253 }
5254
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005255 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
5256 prefix, "Flashlight");
5257 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
5258 prefix, "Camera");
5259 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
5260 prefix, "Video");
5261 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
5262 prefix, "Audio");
5263
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005264 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
5265 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005266 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005267 final Uid.Sensor se = sensors.valueAt(ise);
5268 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005269 sb.setLength(0);
5270 sb.append(prefix);
5271 sb.append(" Sensor ");
5272 int handle = se.getHandle();
5273 if (handle == Uid.Sensor.GPS) {
5274 sb.append("GPS");
5275 } else {
5276 sb.append(handle);
5277 }
5278 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005279
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005280 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005281 if (timer != null) {
5282 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005283 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5284 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005285 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005286 final Timer bgTimer = se.getSensorBackgroundTime();
5287 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005288 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5289 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5290 final long bgActualTime = bgTimer != null ?
5291 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5292
Dianne Hackborn61659e52014-07-09 16:13:01 -07005293 //timer.logState();
5294 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005295 if (actualTime != totalTime) {
5296 formatTimeMs(sb, totalTime);
5297 sb.append("blamed realtime, ");
5298 }
5299
5300 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005301 sb.append("realtime (");
5302 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005303 sb.append(" times)");
5304
5305 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005306 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005307 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5308 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005309 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005310 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005312 } else {
5313 sb.append("(not used)");
5314 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005315 } else {
5316 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005317 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005318
5319 pw.println(sb.toString());
5320 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005321 }
5322
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005323 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5324 "Vibrator");
5325 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5326 prefix, "Foreground activities");
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005327 uidActivity |= printTimer(pw, sb, u.getForegroundServiceTimer(), rawRealtime, which,
5328 prefix, "Foreground services");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005329
Dianne Hackborn61659e52014-07-09 16:13:01 -07005330 long totalStateTime = 0;
5331 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5332 long time = u.getProcessStateTime(ips, rawRealtime, which);
5333 if (time > 0) {
5334 totalStateTime += time;
5335 sb.setLength(0);
5336 sb.append(prefix);
5337 sb.append(" ");
5338 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5339 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005340 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005341 pw.println(sb.toString());
5342 uidActivity = true;
5343 }
5344 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005345 if (totalStateTime > 0) {
5346 sb.setLength(0);
5347 sb.append(prefix);
5348 sb.append(" Total running: ");
5349 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5350 pw.println(sb.toString());
5351 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005352
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005353 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5354 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005355 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005356 sb.setLength(0);
5357 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005358 sb.append(" Total cpu time: u=");
5359 formatTimeMs(sb, userCpuTimeUs / 1000);
5360 sb.append("s=");
5361 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005362 pw.println(sb.toString());
5363 }
5364
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005365 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5366 if (cpuFreqTimes != null) {
5367 sb.setLength(0);
5368 sb.append(" Total cpu time per freq:");
5369 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5370 sb.append(" " + cpuFreqTimes[i]);
5371 }
5372 pw.println(sb.toString());
5373 }
5374 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5375 if (screenOffCpuFreqTimes != null) {
5376 sb.setLength(0);
5377 sb.append(" Total screen-off cpu time per freq:");
5378 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5379 sb.append(" " + screenOffCpuFreqTimes[i]);
5380 }
5381 pw.println(sb.toString());
5382 }
5383
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005384 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5385 = u.getProcessStats();
5386 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5387 final Uid.Proc ps = processStats.valueAt(ipr);
5388 long userTime;
5389 long systemTime;
5390 long foregroundTime;
5391 int starts;
5392 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005393
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005394 userTime = ps.getUserTime(which);
5395 systemTime = ps.getSystemTime(which);
5396 foregroundTime = ps.getForegroundTime(which);
5397 starts = ps.getStarts(which);
5398 final int numCrashes = ps.getNumCrashes(which);
5399 final int numAnrs = ps.getNumAnrs(which);
5400 numExcessive = which == STATS_SINCE_CHARGED
5401 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005402
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005403 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5404 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5405 sb.setLength(0);
5406 sb.append(prefix); sb.append(" Proc ");
5407 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5408 sb.append(prefix); sb.append(" CPU: ");
5409 formatTimeMs(sb, userTime); sb.append("usr + ");
5410 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5411 formatTimeMs(sb, foregroundTime); sb.append("fg");
5412 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5413 sb.append("\n"); sb.append(prefix); sb.append(" ");
5414 boolean hasOne = false;
5415 if (starts != 0) {
5416 hasOne = true;
5417 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005418 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005419 if (numCrashes != 0) {
5420 if (hasOne) {
5421 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005422 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005423 hasOne = true;
5424 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005425 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005426 if (numAnrs != 0) {
5427 if (hasOne) {
5428 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005429 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005430 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005431 }
5432 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005433 pw.println(sb.toString());
5434 for (int e=0; e<numExcessive; e++) {
5435 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5436 if (ew != null) {
5437 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07005438 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005439 pw.print("cpu");
5440 } else {
5441 pw.print("unknown");
5442 }
5443 pw.print(" use: ");
5444 TimeUtils.formatDuration(ew.usedTime, pw);
5445 pw.print(" over ");
5446 TimeUtils.formatDuration(ew.overTime, pw);
5447 if (ew.overTime != 0) {
5448 pw.print(" (");
5449 pw.print((ew.usedTime*100)/ew.overTime);
5450 pw.println("%)");
5451 }
5452 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005453 }
5454 uidActivity = true;
5455 }
5456 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005457
5458 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5459 = u.getPackageStats();
5460 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
5461 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
5462 pw.println(":");
5463 boolean apkActivity = false;
5464 final Uid.Pkg ps = packageStats.valueAt(ipkg);
5465 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
5466 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
5467 pw.print(prefix); pw.print(" Wakeup alarm ");
5468 pw.print(alarms.keyAt(iwa)); pw.print(": ");
5469 pw.print(alarms.valueAt(iwa).getCountLocked(which));
5470 pw.println(" times");
5471 apkActivity = true;
5472 }
5473 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
5474 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
5475 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
5476 final long startTime = ss.getStartTime(batteryUptime, which);
5477 final int starts = ss.getStarts(which);
5478 final int launches = ss.getLaunches(which);
5479 if (startTime != 0 || starts != 0 || launches != 0) {
5480 sb.setLength(0);
5481 sb.append(prefix); sb.append(" Service ");
5482 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
5483 sb.append(prefix); sb.append(" Created for: ");
5484 formatTimeMs(sb, startTime / 1000);
5485 sb.append("uptime\n");
5486 sb.append(prefix); sb.append(" Starts: ");
5487 sb.append(starts);
5488 sb.append(", launches: "); sb.append(launches);
5489 pw.println(sb.toString());
5490 apkActivity = true;
5491 }
5492 }
5493 if (!apkActivity) {
5494 pw.print(prefix); pw.println(" (nothing executed)");
5495 }
5496 uidActivity = true;
5497 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005498 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005499 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005500 }
5501 }
5502 }
5503
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005504 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005505 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005506 int diff = oldval ^ newval;
5507 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005508 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005509 for (int i=0; i<descriptions.length; i++) {
5510 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005511 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005512 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005513 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005514 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005515 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005516 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
5517 didWake = true;
5518 pw.print("=");
5519 if (longNames) {
5520 UserHandle.formatUid(pw, wakelockTag.uid);
5521 pw.print(":\"");
5522 pw.print(wakelockTag.string);
5523 pw.print("\"");
5524 } else {
5525 pw.print(wakelockTag.poolIdx);
5526 }
5527 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005528 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005529 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005530 pw.print("=");
5531 int val = (newval&bd.mask)>>bd.shift;
5532 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005533 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005534 } else {
5535 pw.print(val);
5536 }
5537 }
5538 }
5539 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005540 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07005541 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005542 if (longNames) {
5543 UserHandle.formatUid(pw, wakelockTag.uid);
5544 pw.print(":\"");
5545 pw.print(wakelockTag.string);
5546 pw.print("\"");
5547 } else {
5548 pw.print(wakelockTag.poolIdx);
5549 }
5550 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005551 }
Mike Mac2f518a2017-09-19 16:06:03 -07005552
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005553 public void prepareForDumpLocked() {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07005554 // We don't need to require subclasses implement this.
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005555 }
5556
5557 public static class HistoryPrinter {
5558 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005559 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005560 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005561 int oldStatus = -1;
5562 int oldHealth = -1;
5563 int oldPlug = -1;
5564 int oldTemp = -1;
5565 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005566 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005567 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005568
Dianne Hackborn3251b902014-06-20 14:40:53 -07005569 void reset() {
5570 oldState = oldState2 = 0;
5571 oldLevel = -1;
5572 oldStatus = -1;
5573 oldHealth = -1;
5574 oldPlug = -1;
5575 oldTemp = -1;
5576 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005577 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07005578 }
5579
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005580 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005581 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005582 if (!checkin) {
5583 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005584 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005585 pw.print(" (");
5586 pw.print(rec.numReadInts);
5587 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005588 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005589 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5590 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005591 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005592 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005593 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005594 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005595 }
5596 lastTime = rec.time;
5597 }
5598 if (rec.cmd == HistoryItem.CMD_START) {
5599 if (checkin) {
5600 pw.print(":");
5601 }
5602 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005603 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005604 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
5605 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005606 if (checkin) {
5607 pw.print(":");
5608 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005609 if (rec.cmd == HistoryItem.CMD_RESET) {
5610 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005611 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005612 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005613 pw.print("TIME:");
5614 if (checkin) {
5615 pw.println(rec.currentTime);
5616 } else {
5617 pw.print(" ");
5618 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5619 rec.currentTime).toString());
5620 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005621 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
5622 if (checkin) {
5623 pw.print(":");
5624 }
5625 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005626 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
5627 if (checkin) {
5628 pw.print(":");
5629 }
5630 pw.println("*OVERFLOW*");
5631 } else {
5632 if (!checkin) {
5633 if (rec.batteryLevel < 10) pw.print("00");
5634 else if (rec.batteryLevel < 100) pw.print("0");
5635 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005636 if (verbose) {
5637 pw.print(" ");
5638 if (rec.states < 0) ;
5639 else if (rec.states < 0x10) pw.print("0000000");
5640 else if (rec.states < 0x100) pw.print("000000");
5641 else if (rec.states < 0x1000) pw.print("00000");
5642 else if (rec.states < 0x10000) pw.print("0000");
5643 else if (rec.states < 0x100000) pw.print("000");
5644 else if (rec.states < 0x1000000) pw.print("00");
5645 else if (rec.states < 0x10000000) pw.print("0");
5646 pw.print(Integer.toHexString(rec.states));
5647 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005648 } else {
5649 if (oldLevel != rec.batteryLevel) {
5650 oldLevel = rec.batteryLevel;
5651 pw.print(",Bl="); pw.print(rec.batteryLevel);
5652 }
5653 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005654 if (oldStatus != rec.batteryStatus) {
5655 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005656 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005657 switch (oldStatus) {
5658 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005659 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005660 break;
5661 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005662 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005663 break;
5664 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005665 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005666 break;
5667 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005668 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005669 break;
5670 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005671 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005672 break;
5673 default:
5674 pw.print(oldStatus);
5675 break;
5676 }
5677 }
5678 if (oldHealth != rec.batteryHealth) {
5679 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005680 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005681 switch (oldHealth) {
5682 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005683 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005684 break;
5685 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005686 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005687 break;
5688 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005689 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005690 break;
5691 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005692 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005693 break;
5694 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005695 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005696 break;
5697 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005698 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005699 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005700 case BatteryManager.BATTERY_HEALTH_COLD:
5701 pw.print(checkin ? "c" : "cold");
5702 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005703 default:
5704 pw.print(oldHealth);
5705 break;
5706 }
5707 }
5708 if (oldPlug != rec.batteryPlugType) {
5709 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005710 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005711 switch (oldPlug) {
5712 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005713 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005714 break;
5715 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005716 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005717 break;
5718 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005719 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005720 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005721 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005722 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005723 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005724 default:
5725 pw.print(oldPlug);
5726 break;
5727 }
5728 }
5729 if (oldTemp != rec.batteryTemperature) {
5730 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005731 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005732 pw.print(oldTemp);
5733 }
5734 if (oldVolt != rec.batteryVoltage) {
5735 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005736 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005737 pw.print(oldVolt);
5738 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005739 final int chargeMAh = rec.batteryChargeUAh / 1000;
5740 if (oldChargeMAh != chargeMAh) {
5741 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07005742 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005743 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07005744 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005745 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005746 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005747 printBitDescriptions(pw, oldState2, rec.states2, null,
5748 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005749 if (rec.wakeReasonTag != null) {
5750 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005751 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005752 pw.print(rec.wakeReasonTag.poolIdx);
5753 } else {
5754 pw.print(" wake_reason=");
5755 pw.print(rec.wakeReasonTag.uid);
5756 pw.print(":\"");
5757 pw.print(rec.wakeReasonTag.string);
5758 pw.print("\"");
5759 }
5760 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08005761 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005762 pw.print(checkin ? "," : " ");
5763 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
5764 pw.print("+");
5765 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
5766 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005767 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005768 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
5769 : HISTORY_EVENT_NAMES;
5770 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
5771 | HistoryItem.EVENT_FLAG_FINISH);
5772 if (idx >= 0 && idx < eventNames.length) {
5773 pw.print(eventNames[idx]);
5774 } else {
5775 pw.print(checkin ? "Ev" : "event");
5776 pw.print(idx);
5777 }
5778 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005779 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005780 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08005781 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08005782 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
5783 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005784 pw.print(":\"");
5785 pw.print(rec.eventTag.string);
5786 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005787 }
5788 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005789 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005790 if (rec.stepDetails != null) {
5791 if (!checkin) {
5792 pw.print(" Details: cpu=");
5793 pw.print(rec.stepDetails.userTime);
5794 pw.print("u+");
5795 pw.print(rec.stepDetails.systemTime);
5796 pw.print("s");
5797 if (rec.stepDetails.appCpuUid1 >= 0) {
5798 pw.print(" (");
5799 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
5800 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5801 if (rec.stepDetails.appCpuUid2 >= 0) {
5802 pw.print(", ");
5803 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
5804 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5805 }
5806 if (rec.stepDetails.appCpuUid3 >= 0) {
5807 pw.print(", ");
5808 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
5809 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5810 }
5811 pw.print(')');
5812 }
5813 pw.println();
5814 pw.print(" /proc/stat=");
5815 pw.print(rec.stepDetails.statUserTime);
5816 pw.print(" usr, ");
5817 pw.print(rec.stepDetails.statSystemTime);
5818 pw.print(" sys, ");
5819 pw.print(rec.stepDetails.statIOWaitTime);
5820 pw.print(" io, ");
5821 pw.print(rec.stepDetails.statIrqTime);
5822 pw.print(" irq, ");
5823 pw.print(rec.stepDetails.statSoftIrqTime);
5824 pw.print(" sirq, ");
5825 pw.print(rec.stepDetails.statIdlTime);
5826 pw.print(" idle");
5827 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
5828 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
5829 + rec.stepDetails.statSoftIrqTime;
5830 int total = totalRun + rec.stepDetails.statIdlTime;
5831 if (total > 0) {
5832 pw.print(" (");
5833 float perc = ((float)totalRun) / ((float)total) * 100;
5834 pw.print(String.format("%.1f%%", perc));
5835 pw.print(" of ");
5836 StringBuilder sb = new StringBuilder(64);
5837 formatTimeMsNoSpace(sb, total*10);
5838 pw.print(sb);
5839 pw.print(")");
5840 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005841 pw.print(", PlatformIdleStat ");
5842 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005843 pw.println();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00005844
5845 pw.print(", SubsystemPowerState ");
5846 pw.print(rec.stepDetails.statSubsystemPowerState);
5847 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005848 } else {
5849 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5850 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
5851 pw.print(rec.stepDetails.userTime);
5852 pw.print(":");
5853 pw.print(rec.stepDetails.systemTime);
5854 if (rec.stepDetails.appCpuUid1 >= 0) {
5855 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
5856 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5857 if (rec.stepDetails.appCpuUid2 >= 0) {
5858 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
5859 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5860 }
5861 if (rec.stepDetails.appCpuUid3 >= 0) {
5862 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
5863 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5864 }
5865 }
5866 pw.println();
5867 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5868 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
5869 pw.print(rec.stepDetails.statUserTime);
5870 pw.print(',');
5871 pw.print(rec.stepDetails.statSystemTime);
5872 pw.print(',');
5873 pw.print(rec.stepDetails.statIOWaitTime);
5874 pw.print(',');
5875 pw.print(rec.stepDetails.statIrqTime);
5876 pw.print(',');
5877 pw.print(rec.stepDetails.statSoftIrqTime);
5878 pw.print(',');
5879 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005880 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005881 if (rec.stepDetails.statPlatformIdleState != null) {
5882 pw.print(rec.stepDetails.statPlatformIdleState);
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07005883 if (rec.stepDetails.statSubsystemPowerState != null) {
5884 pw.print(',');
5885 }
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005886 }
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00005887
5888 if (rec.stepDetails.statSubsystemPowerState != null) {
5889 pw.print(rec.stepDetails.statSubsystemPowerState);
5890 }
5891 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005892 }
5893 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005894 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005895 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005896 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005897 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005898
5899 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
5900 UserHandle.formatUid(pw, uid);
5901 pw.print("=");
5902 pw.print(utime);
5903 pw.print("u+");
5904 pw.print(stime);
5905 pw.print("s");
5906 }
5907
5908 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
5909 pw.print('/');
5910 pw.print(uid);
5911 pw.print(":");
5912 pw.print(utime);
5913 pw.print(":");
5914 pw.print(stime);
5915 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005916 }
5917
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005918 private void printSizeValue(PrintWriter pw, long size) {
5919 float result = size;
5920 String suffix = "";
5921 if (result >= 10*1024) {
5922 suffix = "KB";
5923 result = result / 1024;
5924 }
5925 if (result >= 10*1024) {
5926 suffix = "MB";
5927 result = result / 1024;
5928 }
5929 if (result >= 10*1024) {
5930 suffix = "GB";
5931 result = result / 1024;
5932 }
5933 if (result >= 10*1024) {
5934 suffix = "TB";
5935 result = result / 1024;
5936 }
5937 if (result >= 10*1024) {
5938 suffix = "PB";
5939 result = result / 1024;
5940 }
5941 pw.print((int)result);
5942 pw.print(suffix);
5943 }
5944
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005945 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
5946 String label3, long estimatedTime) {
5947 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005948 return false;
5949 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005950 pw.print(label1);
5951 pw.print(label2);
5952 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005953 StringBuilder sb = new StringBuilder(64);
5954 formatTimeMs(sb, estimatedTime);
5955 pw.print(sb);
5956 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005957 return true;
5958 }
5959
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005960 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
5961 LevelStepTracker steps, boolean checkin) {
5962 if (steps == null) {
5963 return false;
5964 }
5965 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005966 if (count <= 0) {
5967 return false;
5968 }
5969 if (!checkin) {
5970 pw.println(header);
5971 }
Kweku Adams030980a2015-04-01 16:07:48 -07005972 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005973 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005974 long duration = steps.getDurationAt(i);
5975 int level = steps.getLevelAt(i);
5976 long initMode = steps.getInitModeAt(i);
5977 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005978 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005979 lineArgs[0] = Long.toString(duration);
5980 lineArgs[1] = Integer.toString(level);
5981 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5982 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5983 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
5984 case Display.STATE_ON: lineArgs[2] = "s+"; break;
5985 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
5986 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07005987 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005988 }
5989 } else {
5990 lineArgs[2] = "";
5991 }
5992 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5993 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
5994 } else {
5995 lineArgs[3] = "";
5996 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005997 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07005998 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005999 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07006000 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006001 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006002 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
6003 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006004 pw.print(prefix);
6005 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006006 TimeUtils.formatDuration(duration, pw);
6007 pw.print(" to "); pw.print(level);
6008 boolean haveModes = false;
6009 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6010 pw.print(" (");
6011 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6012 case Display.STATE_OFF: pw.print("screen-off"); break;
6013 case Display.STATE_ON: pw.print("screen-on"); break;
6014 case Display.STATE_DOZE: pw.print("screen-doze"); break;
6015 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07006016 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006017 }
6018 haveModes = true;
6019 }
6020 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6021 pw.print(haveModes ? ", " : " (");
6022 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
6023 ? "power-save-on" : "power-save-off");
6024 haveModes = true;
6025 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006026 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6027 pw.print(haveModes ? ", " : " (");
6028 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6029 ? "device-idle-on" : "device-idle-off");
6030 haveModes = true;
6031 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006032 if (haveModes) {
6033 pw.print(")");
6034 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006035 pw.println();
6036 }
6037 }
6038 return true;
6039 }
6040
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006041 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006042 public static final int DUMP_DAILY_ONLY = 1<<2;
6043 public static final int DUMP_HISTORY_ONLY = 1<<3;
6044 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
6045 public static final int DUMP_VERBOSE = 1<<5;
6046 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006047
Dianne Hackborn37de0982014-05-09 09:32:18 -07006048 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
6049 final HistoryPrinter hprinter = new HistoryPrinter();
6050 final HistoryItem rec = new HistoryItem();
6051 long lastTime = -1;
6052 long baseTime = -1;
6053 boolean printed = false;
6054 HistoryEventTracker tracker = null;
6055 while (getNextHistoryLocked(rec)) {
6056 lastTime = rec.time;
6057 if (baseTime < 0) {
6058 baseTime = lastTime;
6059 }
6060 if (rec.time >= histStart) {
6061 if (histStart >= 0 && !printed) {
6062 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07006063 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006064 || rec.cmd == HistoryItem.CMD_START
6065 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006066 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006067 hprinter.printNextItem(pw, rec, baseTime, checkin,
6068 (flags&DUMP_VERBOSE) != 0);
6069 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006070 } else if (rec.currentTime != 0) {
6071 printed = true;
6072 byte cmd = rec.cmd;
6073 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006074 hprinter.printNextItem(pw, rec, baseTime, checkin,
6075 (flags&DUMP_VERBOSE) != 0);
6076 rec.cmd = cmd;
6077 }
6078 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006079 if (rec.cmd != HistoryItem.CMD_UPDATE) {
6080 hprinter.printNextItem(pw, rec, baseTime, checkin,
6081 (flags&DUMP_VERBOSE) != 0);
6082 rec.cmd = HistoryItem.CMD_UPDATE;
6083 }
6084 int oldEventCode = rec.eventCode;
6085 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006086 rec.eventTag = new HistoryTag();
6087 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
6088 HashMap<String, SparseIntArray> active
6089 = tracker.getStateForEvent(i);
6090 if (active == null) {
6091 continue;
6092 }
6093 for (HashMap.Entry<String, SparseIntArray> ent
6094 : active.entrySet()) {
6095 SparseIntArray uids = ent.getValue();
6096 for (int j=0; j<uids.size(); j++) {
6097 rec.eventCode = i;
6098 rec.eventTag.string = ent.getKey();
6099 rec.eventTag.uid = uids.keyAt(j);
6100 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006101 hprinter.printNextItem(pw, rec, baseTime, checkin,
6102 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006103 rec.wakeReasonTag = null;
6104 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006105 }
6106 }
6107 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006108 rec.eventCode = oldEventCode;
6109 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006110 tracker = null;
6111 }
6112 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006113 hprinter.printNextItem(pw, rec, baseTime, checkin,
6114 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07006115 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
6116 // This is an attempt to aggregate the previous state and generate
6117 // fake events to reflect that state at the point where we start
6118 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07006119 if (tracker == null) {
6120 tracker = new HistoryEventTracker();
6121 }
6122 tracker.updateState(rec.eventCode, rec.eventTag.string,
6123 rec.eventTag.uid, rec.eventTag.poolIdx);
6124 }
6125 }
6126 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07006127 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006128 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
6129 }
6130 }
6131
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006132 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
6133 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
6134 if (steps == null) {
6135 return;
6136 }
6137 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
6138 if (timeRemaining >= 0) {
6139 pw.print(prefix); pw.print(label); pw.print(" total time: ");
6140 tmpSb.setLength(0);
6141 formatTimeMs(tmpSb, timeRemaining);
6142 pw.print(tmpSb);
6143 pw.print(" (from "); pw.print(tmpOutInt[0]);
6144 pw.println(" steps)");
6145 }
6146 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6147 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6148 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
6149 if (estimatedTime > 0) {
6150 pw.print(prefix); pw.print(label); pw.print(" ");
6151 pw.print(STEP_LEVEL_MODE_LABELS[i]);
6152 pw.print(" time: ");
6153 tmpSb.setLength(0);
6154 formatTimeMs(tmpSb, estimatedTime);
6155 pw.print(tmpSb);
6156 pw.print(" (from "); pw.print(tmpOutInt[0]);
6157 pw.println(" steps)");
6158 }
6159 }
6160 }
6161
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006162 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
6163 ArrayList<PackageChange> changes) {
6164 if (changes == null) {
6165 return;
6166 }
6167 pw.print(prefix); pw.println("Package changes:");
6168 for (int i=0; i<changes.size(); i++) {
6169 PackageChange pc = changes.get(i);
6170 if (pc.mUpdate) {
6171 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
6172 pw.print(" vers="); pw.println(pc.mVersionCode);
6173 } else {
6174 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
6175 }
6176 }
6177 }
6178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006179 /**
6180 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
6181 *
6182 * @param pw a Printer to receive the dump output.
6183 */
6184 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006185 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006186 prepareForDumpLocked();
6187
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006188 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006189 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006190
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006191 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006192 final long historyTotalSize = getHistoryTotalSize();
6193 final long historyUsedSize = getHistoryUsedSize();
6194 if (startIteratingHistoryLocked()) {
6195 try {
6196 pw.print("Battery History (");
6197 pw.print((100*historyUsedSize)/historyTotalSize);
6198 pw.print("% used, ");
6199 printSizeValue(pw, historyUsedSize);
6200 pw.print(" used of ");
6201 printSizeValue(pw, historyTotalSize);
6202 pw.print(", ");
6203 pw.print(getHistoryStringPoolSize());
6204 pw.print(" strings using ");
6205 printSizeValue(pw, getHistoryStringPoolBytes());
6206 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07006207 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006208 pw.println();
6209 } finally {
6210 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006211 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006212 }
6213
6214 if (startIteratingOldHistoryLocked()) {
6215 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006216 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006217 pw.println("Old battery History:");
6218 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006219 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006220 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006221 if (baseTime < 0) {
6222 baseTime = rec.time;
6223 }
6224 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006225 }
6226 pw.println();
6227 } finally {
6228 finishIteratingOldHistoryLocked();
6229 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006230 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006231 }
6232
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006233 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006234 return;
6235 }
6236
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006237 if (!filtering) {
6238 SparseArray<? extends Uid> uidStats = getUidStats();
6239 final int NU = uidStats.size();
6240 boolean didPid = false;
6241 long nowRealtime = SystemClock.elapsedRealtime();
6242 for (int i=0; i<NU; i++) {
6243 Uid uid = uidStats.valueAt(i);
6244 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
6245 if (pids != null) {
6246 for (int j=0; j<pids.size(); j++) {
6247 Uid.Pid pid = pids.valueAt(j);
6248 if (!didPid) {
6249 pw.println("Per-PID Stats:");
6250 didPid = true;
6251 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006252 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
6253 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006254 pw.print(" PID "); pw.print(pids.keyAt(j));
6255 pw.print(" wake time: ");
6256 TimeUtils.formatDuration(time, pw);
6257 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006258 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006259 }
6260 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006261 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006262 pw.println();
6263 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006264 }
6265
6266 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006267 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
6268 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006269 long timeRemaining = computeBatteryTimeRemaining(
6270 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006271 if (timeRemaining >= 0) {
6272 pw.print(" Estimated discharge time remaining: ");
6273 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6274 pw.println();
6275 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006276 final LevelStepTracker steps = getDischargeLevelStepTracker();
6277 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6278 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
6279 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6280 STEP_LEVEL_MODE_VALUES[i], null));
6281 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006282 pw.println();
6283 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006284 if (dumpDurationSteps(pw, " ", "Charge step durations:",
6285 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006286 long timeRemaining = computeChargeTimeRemaining(
6287 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006288 if (timeRemaining >= 0) {
6289 pw.print(" Estimated charge time remaining: ");
6290 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6291 pw.println();
6292 }
6293 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006294 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006295 }
6296 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
6297 pw.println("Daily stats:");
6298 pw.print(" Current start time: ");
6299 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6300 getCurrentDailyStartTime()).toString());
6301 pw.print(" Next min deadline: ");
6302 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6303 getNextMinDailyDeadline()).toString());
6304 pw.print(" Next max deadline: ");
6305 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6306 getNextMaxDailyDeadline()).toString());
6307 StringBuilder sb = new StringBuilder(64);
6308 int[] outInt = new int[1];
6309 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
6310 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006311 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
6312 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006313 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006314 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
6315 dsteps, false)) {
6316 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6317 sb, outInt);
6318 }
6319 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6320 csteps, false)) {
6321 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6322 sb, outInt);
6323 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006324 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006325 } else {
6326 pw.println(" Current daily steps:");
6327 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6328 sb, outInt);
6329 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6330 sb, outInt);
6331 }
6332 }
6333 DailyItem dit;
6334 int curIndex = 0;
6335 while ((dit=getDailyItemLocked(curIndex)) != null) {
6336 curIndex++;
6337 if ((flags&DUMP_DAILY_ONLY) != 0) {
6338 pw.println();
6339 }
6340 pw.print(" Daily from ");
6341 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6342 pw.print(" to ");
6343 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6344 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006345 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006346 if (dumpDurationSteps(pw, " ",
6347 " Discharge step durations:", dit.mDischargeSteps, false)) {
6348 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6349 sb, outInt);
6350 }
6351 if (dumpDurationSteps(pw, " ",
6352 " Charge step durations:", dit.mChargeSteps, false)) {
6353 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6354 sb, outInt);
6355 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006356 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006357 } else {
6358 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6359 sb, outInt);
6360 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6361 sb, outInt);
6362 }
6363 }
6364 pw.println();
6365 }
6366 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006367 pw.println("Statistics since last charge:");
6368 pw.println(" System starts: " + getStartCount()
6369 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006370 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6371 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006372 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006374 }
Mike Mac2f518a2017-09-19 16:06:03 -07006375
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006376 // This is called from BatteryStatsService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006377 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006378 public void dumpCheckinLocked(Context context, PrintWriter pw,
6379 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006380 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006381
6382 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006383 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6384 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006385
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006386 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6387
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006388 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07006389 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006390 try {
6391 for (int i=0; i<getHistoryStringPoolSize(); i++) {
6392 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6393 pw.print(HISTORY_STRING_POOL); pw.print(',');
6394 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006395 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006396 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006397 pw.print(",\"");
6398 String str = getHistoryTagPoolString(i);
6399 str = str.replace("\\", "\\\\");
6400 str = str.replace("\"", "\\\"");
6401 pw.print(str);
6402 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006403 pw.println();
6404 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006405 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006406 } finally {
6407 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08006408 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006409 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006410 }
6411
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006412 if ((flags & DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006413 return;
6414 }
6415
Dianne Hackborne4a59512010-12-07 11:08:07 -08006416 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006417 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08006418 for (int i=0; i<apps.size(); i++) {
6419 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006420 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
6421 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08006422 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006423 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
6424 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006425 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006426 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006427 }
6428 SparseArray<? extends Uid> uidStats = getUidStats();
6429 final int NU = uidStats.size();
6430 String[] lineArgs = new String[2];
6431 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006432 int uid = UserHandle.getAppId(uidStats.keyAt(i));
6433 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
6434 if (pkgs != null && !pkgs.second.value) {
6435 pkgs.second.value = true;
6436 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08006437 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006438 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006439 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
6440 (Object[])lineArgs);
6441 }
6442 }
6443 }
6444 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006445 if ((flags & DUMP_DAILY_ONLY) == 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006446 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006447 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07006448 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006449 if (timeRemaining >= 0) {
6450 lineArgs[0] = Long.toString(timeRemaining);
6451 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
6452 (Object[])lineArgs);
6453 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006454 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07006455 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006456 if (timeRemaining >= 0) {
6457 lineArgs[0] = Long.toString(timeRemaining);
6458 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
6459 (Object[])lineArgs);
6460 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006461 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
6462 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006464 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006465
6466 /** Dump batterystats data to a proto. @hide */
6467 public void dumpProtoLocked(Context context, FileDescriptor fd, List<ApplicationInfo> apps,
6468 int flags, long historyStart) {
6469 final ProtoOutputStream proto = new ProtoOutputStream(fd);
6470 final long bToken = proto.start(BatteryStatsServiceDumpProto.BATTERYSTATS);
6471 prepareForDumpLocked();
6472
6473 proto.write(BatteryStatsProto.REPORT_VERSION, CHECKIN_VERSION);
6474 proto.write(BatteryStatsProto.PARCEL_VERSION, getParcelVersion());
6475 proto.write(BatteryStatsProto.START_PLATFORM_VERSION, getStartPlatformVersion());
6476 proto.write(BatteryStatsProto.END_PLATFORM_VERSION, getEndPlatformVersion());
6477
6478 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6479
6480 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
6481 if (startIteratingHistoryLocked()) {
6482 // TODO: implement dumpProtoHistoryLocked(proto);
6483 }
6484 }
6485
6486 if ((flags & (DUMP_HISTORY_ONLY | DUMP_DAILY_ONLY)) == 0) {
6487 // TODO: implement dumpProtoAppsLocked(proto, apps);
6488 // TODO: implement dumpProtoSystemLocked(proto);
6489 }
6490
6491 proto.end(bToken);
6492 proto.flush();
6493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006494}