blob: a8bd940326d6973d760351e5ffd0440d71bf5d80 [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.
Kweku Adams103351f2017-10-16 14:39:34 -0700450 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700451 */
452 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
453 return -1;
454 }
455
456 /**
457 * Returns the current time the timer has been active, if it is being tracked.
Kweku Adams103351f2017-10-16 14:39:34 -0700458 * Not all Timer subclasses track the max, total, and current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700459 */
460 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
461 return -1;
462 }
463
464 /**
Kweku Adams103351f2017-10-16 14:39:34 -0700465 * Returns the total time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800466 *
467 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
468 * been on since reset.
469 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
470 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
471 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
472 * the actual total time.
Kweku Adams103351f2017-10-16 14:39:34 -0700473 * Not all Timer subclasses track the max, total, and current durations.
Bookatz867c0d72017-03-07 18:23:42 -0800474 */
475 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
476 return -1;
477 }
478
479 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700480 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
481 * used, for example, for tracking background usage. Secondary timers are never pooled.
482 *
483 * Not all Timer subclasses have a secondary timer; those that don't return null.
484 */
485 public Timer getSubTimer() {
486 return null;
487 }
488
489 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700490 * Returns whether the timer is currently running. Some types of timers
491 * (e.g. BatchTimers) don't know whether the event is currently active,
492 * and report false.
493 */
494 public boolean isRunningLocked() {
495 return false;
496 }
497
498 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 * Temporary for debugging.
500 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700501 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 }
503
504 /**
505 * The statistics associated with a particular uid.
506 */
507 public static abstract class Uid {
508
509 /**
510 * Returns a mapping containing wakelock statistics.
511 *
512 * @return a Map from Strings to Uid.Wakelock objects.
513 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700514 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515
516 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700517 * Returns a mapping containing sync statistics.
518 *
519 * @return a Map from Strings to Timer objects.
520 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700521 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700522
523 /**
524 * Returns a mapping containing scheduled job statistics.
525 *
526 * @return a Map from Strings to Timer objects.
527 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700528 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700529
530 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700531 * Returns statistics about how jobs have completed.
532 *
533 * @return A Map of String job names to completion type -> count mapping.
534 */
535 public abstract ArrayMap<String, SparseIntArray> getJobCompletionStats();
536
537 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 * The statistics associated with a particular wake lock.
539 */
540 public static abstract class Wakelock {
541 public abstract Timer getWakeTime(int type);
542 }
543
544 /**
Bookatzc8c44962017-05-11 12:12:54 -0700545 * The cumulative time the uid spent holding any partial wakelocks. This will generally
546 * differ from summing over the Wakelocks in getWakelockStats since the latter may have
547 * wakelocks that overlap in time (and therefore over-counts).
548 */
549 public abstract Timer getAggregatedPartialWakelockTimer();
550
551 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 * Returns a mapping containing sensor statistics.
553 *
554 * @return a Map from Integer sensor ids to Uid.Sensor objects.
555 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700556 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557
558 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700559 * Returns a mapping containing active process data.
560 */
561 public abstract SparseArray<? extends Pid> getPidStats();
Bookatzc8c44962017-05-11 12:12:54 -0700562
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700563 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 * Returns a mapping containing process statistics.
565 *
566 * @return a Map from Strings to Uid.Proc objects.
567 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700568 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569
570 /**
571 * Returns a mapping containing package statistics.
572 *
573 * @return a Map from Strings to Uid.Pkg objects.
574 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700575 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700576
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800577 public abstract ControllerActivityCounter getWifiControllerActivity();
578 public abstract ControllerActivityCounter getBluetoothControllerActivity();
579 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800580
581 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 * {@hide}
583 */
584 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700585
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800586 public abstract void noteWifiRunningLocked(long elapsedRealtime);
587 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
588 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
589 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
590 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
591 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
592 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
593 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
594 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
595 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800596 public abstract void noteActivityResumedLocked(long elapsedRealtime);
597 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800598 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
599 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
600 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700601 public abstract int getWifiScanCount(int which);
Kweku Adams103351f2017-10-16 14:39:34 -0700602 /**
603 * Returns the timer keeping track of wifi scans.
604 */
605 public abstract Timer getWifiScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800606 public abstract int getWifiScanBackgroundCount(int which);
607 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
608 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -0700609 /**
610 * Returns the timer keeping track of background wifi scans.
611 */
612 public abstract Timer getWifiScanBackgroundTimer();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800613 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700614 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800615 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700616 public abstract Timer getAudioTurnedOnTimer();
617 public abstract Timer getVideoTurnedOnTimer();
618 public abstract Timer getFlashlightTurnedOnTimer();
619 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700620 public abstract Timer getForegroundActivityTimer();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -0700621
622 /**
623 * Returns the timer keeping track of Foreground Service time
624 */
625 public abstract Timer getForegroundServiceTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800626 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800627 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatzb1f04f32017-05-19 13:57:32 -0700628 public abstract Timer getBluetoothUnoptimizedScanTimer();
629 public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700630 public abstract Counter getBluetoothScanResultCounter();
Bookatzb1f04f32017-05-19 13:57:32 -0700631 public abstract Counter getBluetoothScanResultBgCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700632
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700633 public abstract long[] getCpuFreqTimes(int which);
634 public abstract long[] getScreenOffCpuFreqTimes(int which);
635
Dianne Hackborna0200e32016-03-30 18:01:41 -0700636 // Note: the following times are disjoint. They can be added together to find the
637 // total time a uid has had any processes running at all.
638
639 /**
640 * Time this uid has any processes in the top state (or above such as persistent).
641 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800642 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700643 /**
644 * Time this uid has any process with a started out bound foreground service, but
645 * none in the "top" state.
646 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800647 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700648 /**
649 * Time this uid has any process that is top while the device is sleeping, but none
650 * in the "foreground service" or better state.
651 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800652 public static final int PROCESS_STATE_TOP_SLEEPING = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700653 /**
654 * Time this uid has any process in an active foreground state, but none in the
655 * "top sleeping" or better state.
656 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800657 public static final int PROCESS_STATE_FOREGROUND = 3;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700658 /**
659 * Time this uid has any process in an active background state, but none in the
660 * "foreground" or better state.
661 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800662 public static final int PROCESS_STATE_BACKGROUND = 4;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700663 /**
664 * Time this uid has any processes that are sitting around cached, not in one of the
665 * other active states.
666 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800667 public static final int PROCESS_STATE_CACHED = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700668 /**
669 * Total number of process states we track.
670 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800671 public static final int NUM_PROCESS_STATE = 6;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700672
673 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackborna8d10942015-11-19 17:55:19 -0800674 "Top", "Fg Service", "Top Sleeping", "Foreground", "Background", "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700675 };
676
677 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800678 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700679
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800680 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681
Robert Greenwalta029ea12013-09-25 16:38:12 -0700682 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
683
Dianne Hackborn617f8772009-03-31 15:04:46 -0700684 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700685 * Note that these must match the constants in android.os.PowerManager.
686 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
687 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700688 */
689 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700690 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700691 };
Bookatzc8c44962017-05-11 12:12:54 -0700692
Phil Weaverda80d672016-03-15 16:25:46 -0700693 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700694
Dianne Hackborn617f8772009-03-31 15:04:46 -0700695 public abstract void noteUserActivityLocked(int type);
696 public abstract boolean hasUserActivity();
697 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700698
699 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800700 public abstract long getNetworkActivityBytes(int type, int which);
701 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800702 public abstract long getMobileRadioActiveTime(int which);
703 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700704
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700705 /**
706 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
707 */
708 public abstract long getUserCpuTimeUs(int which);
709
710 /**
711 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
712 */
713 public abstract long getSystemCpuTimeUs(int which);
714
715 /**
Sudheer Shanka71f34b32017-07-21 00:14:24 -0700716 * Returns the approximate cpu time (in microseconds) spent at a certain CPU speed for a
Adam Lesinski6832f392015-09-05 18:05:40 -0700717 * given CPU cluster.
718 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700719 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700720 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700721 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
722 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700723 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700724 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700725
Adam Lesinski5f056f62016-07-14 16:56:08 -0700726 /**
727 * Returns the number of times this UID woke up the Application Processor to
728 * process a mobile radio packet.
729 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
730 */
731 public abstract long getMobileRadioApWakeupCount(int which);
732
733 /**
734 * Returns the number of times this UID woke up the Application Processor to
735 * process a WiFi packet.
736 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
737 */
738 public abstract long getWifiRadioApWakeupCount(int which);
739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800741 /*
742 * FIXME: it's not correct to use this magic value because it
743 * could clash with a sensor handle (which are defined by
744 * the sensor HAL, and therefore out of our control
745 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 // Magic sensor number for the GPS.
747 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800752
Bookatz867c0d72017-03-07 18:23:42 -0800753 /** Returns a Timer for sensor usage when app is in the background. */
754 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
756
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700757 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800758 public int mWakeNesting;
759 public long mWakeSumMs;
760 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700761 }
762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 /**
764 * The statistics associated with a particular process.
765 */
766 public static abstract class Proc {
767
Dianne Hackborn287952c2010-09-22 22:34:31 -0700768 public static class ExcessivePower {
769 public static final int TYPE_WAKE = 1;
770 public static final int TYPE_CPU = 2;
771
772 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700773 public long overTime;
774 public long usedTime;
775 }
776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800778 * Returns true if this process is still active in the battery stats.
779 */
780 public abstract boolean isActive();
781
782 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700783 * Returns the total time (in milliseconds) spent executing in user 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 getUserTime(int which);
788
789 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700790 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 *
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 long getSystemTime(int which);
795
796 /**
797 * Returns the number of times the process has been started.
798 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700799 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 */
801 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700802
803 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800804 * Returns the number of times the process has crashed.
805 *
806 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
807 */
808 public abstract int getNumCrashes(int which);
809
810 /**
811 * Returns the number of times the process has ANRed.
812 *
813 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
814 */
815 public abstract int getNumAnrs(int which);
816
817 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700818 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700819 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700820 * @return foreground cpu time in microseconds
821 */
822 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700823
Dianne Hackborn287952c2010-09-22 22:34:31 -0700824 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700825
Dianne Hackborn287952c2010-09-22 22:34:31 -0700826 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 }
828
829 /**
830 * The statistics associated with a particular package.
831 */
832 public static abstract class Pkg {
833
834 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700835 * Returns information about all wakeup alarms that have been triggered for this
836 * package. The mapping keys are tag names for the alarms, the counter contains
837 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700839 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840
841 /**
842 * Returns a mapping containing service statistics.
843 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700844 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845
846 /**
847 * The statistics associated with a particular service.
848 */
Joe Onoratoabded112016-02-08 16:49:39 -0800849 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850
851 /**
852 * Returns the amount of time spent started.
853 *
854 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700855 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 * @return
857 */
858 public abstract long getStartTime(long batteryUptime, int which);
859
860 /**
861 * Returns the total number of times startService() has been called.
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 getStarts(int which);
866
867 /**
868 * Returns the total number times the service has been launched.
869 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700870 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 */
872 public abstract int getLaunches(int which);
873 }
874 }
875 }
876
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800877 public static final class LevelStepTracker {
878 public long mLastStepTime = -1;
879 public int mNumStepDurations;
880 public final long[] mStepDurations;
881
882 public LevelStepTracker(int maxLevelSteps) {
883 mStepDurations = new long[maxLevelSteps];
884 }
885
886 public LevelStepTracker(int numSteps, long[] steps) {
887 mNumStepDurations = numSteps;
888 mStepDurations = new long[numSteps];
889 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
890 }
891
892 public long getDurationAt(int index) {
893 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
894 }
895
896 public int getLevelAt(int index) {
897 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
898 >> STEP_LEVEL_LEVEL_SHIFT);
899 }
900
901 public int getInitModeAt(int index) {
902 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
903 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
904 }
905
906 public int getModModeAt(int index) {
907 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
908 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
909 }
910
911 private void appendHex(long val, int topOffset, StringBuilder out) {
912 boolean hasData = false;
913 while (topOffset >= 0) {
914 int digit = (int)( (val>>topOffset) & 0xf );
915 topOffset -= 4;
916 if (!hasData && digit == 0) {
917 continue;
918 }
919 hasData = true;
920 if (digit >= 0 && digit <= 9) {
921 out.append((char)('0' + digit));
922 } else {
923 out.append((char)('a' + digit - 10));
924 }
925 }
926 }
927
928 public void encodeEntryAt(int index, StringBuilder out) {
929 long item = mStepDurations[index];
930 long duration = item & STEP_LEVEL_TIME_MASK;
931 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
932 >> STEP_LEVEL_LEVEL_SHIFT);
933 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
934 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
935 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
936 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
937 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
938 case Display.STATE_OFF: out.append('f'); break;
939 case Display.STATE_ON: out.append('o'); break;
940 case Display.STATE_DOZE: out.append('d'); break;
941 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
942 }
943 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
944 out.append('p');
945 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700946 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
947 out.append('i');
948 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800949 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
950 case Display.STATE_OFF: out.append('F'); break;
951 case Display.STATE_ON: out.append('O'); break;
952 case Display.STATE_DOZE: out.append('D'); break;
953 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
954 }
955 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
956 out.append('P');
957 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700958 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
959 out.append('I');
960 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800961 out.append('-');
962 appendHex(level, 4, out);
963 out.append('-');
964 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
965 }
966
967 public void decodeEntryAt(int index, String value) {
968 final int N = value.length();
969 int i = 0;
970 char c;
971 long out = 0;
972 while (i < N && (c=value.charAt(i)) != '-') {
973 i++;
974 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800975 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800976 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800977 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800978 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800979 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800980 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800981 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
982 << STEP_LEVEL_INITIAL_MODE_SHIFT);
983 break;
984 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
985 << STEP_LEVEL_INITIAL_MODE_SHIFT);
986 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700987 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
988 << STEP_LEVEL_INITIAL_MODE_SHIFT);
989 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800990 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
991 break;
992 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
993 break;
994 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
995 break;
996 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
997 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
998 break;
999 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
1000 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001001 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001002 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
1003 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
1004 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001005 }
1006 }
1007 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001008 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001009 while (i < N && (c=value.charAt(i)) != '-') {
1010 i++;
1011 level <<= 4;
1012 if (c >= '0' && c <= '9') {
1013 level += c - '0';
1014 } else if (c >= 'a' && c <= 'f') {
1015 level += c - 'a' + 10;
1016 } else if (c >= 'A' && c <= 'F') {
1017 level += c - 'A' + 10;
1018 }
1019 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -08001020 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001021 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
1022 long duration = 0;
1023 while (i < N && (c=value.charAt(i)) != '-') {
1024 i++;
1025 duration <<= 4;
1026 if (c >= '0' && c <= '9') {
1027 duration += c - '0';
1028 } else if (c >= 'a' && c <= 'f') {
1029 duration += c - 'a' + 10;
1030 } else if (c >= 'A' && c <= 'F') {
1031 duration += c - 'A' + 10;
1032 }
1033 }
1034 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
1035 }
1036
1037 public void init() {
1038 mLastStepTime = -1;
1039 mNumStepDurations = 0;
1040 }
1041
1042 public void clearTime() {
1043 mLastStepTime = -1;
1044 }
1045
1046 public long computeTimePerLevel() {
1047 final long[] steps = mStepDurations;
1048 final int numSteps = mNumStepDurations;
1049
1050 // For now we'll do a simple average across all steps.
1051 if (numSteps <= 0) {
1052 return -1;
1053 }
1054 long total = 0;
1055 for (int i=0; i<numSteps; i++) {
1056 total += steps[i] & STEP_LEVEL_TIME_MASK;
1057 }
1058 return total / numSteps;
1059 /*
1060 long[] buckets = new long[numSteps];
1061 int numBuckets = 0;
1062 int numToAverage = 4;
1063 int i = 0;
1064 while (i < numSteps) {
1065 long totalTime = 0;
1066 int num = 0;
1067 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1068 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1069 num++;
1070 }
1071 buckets[numBuckets] = totalTime / num;
1072 numBuckets++;
1073 numToAverage *= 2;
1074 i += num;
1075 }
1076 if (numBuckets < 1) {
1077 return -1;
1078 }
1079 long averageTime = buckets[numBuckets-1];
1080 for (i=numBuckets-2; i>=0; i--) {
1081 averageTime = (averageTime + buckets[i]) / 2;
1082 }
1083 return averageTime;
1084 */
1085 }
1086
1087 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1088 int[] outNumOfInterest) {
1089 final long[] steps = mStepDurations;
1090 final int count = mNumStepDurations;
1091 if (count <= 0) {
1092 return -1;
1093 }
1094 long total = 0;
1095 int numOfInterest = 0;
1096 for (int i=0; i<count; i++) {
1097 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1098 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1099 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1100 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1101 // If the modes of interest didn't change during this step period...
1102 if ((modMode&modesOfInterest) == 0) {
1103 // And the mode values during this period match those we are measuring...
1104 if ((initMode&modesOfInterest) == modeValues) {
1105 // Then this can be used to estimate the total time!
1106 numOfInterest++;
1107 total += steps[i] & STEP_LEVEL_TIME_MASK;
1108 }
1109 }
1110 }
1111 if (numOfInterest <= 0) {
1112 return -1;
1113 }
1114
1115 if (outNumOfInterest != null) {
1116 outNumOfInterest[0] = numOfInterest;
1117 }
1118
1119 // The estimated time is the average time we spend in each level, multipled
1120 // by 100 -- the total number of battery levels
1121 return (total / numOfInterest) * 100;
1122 }
1123
1124 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1125 int stepCount = mNumStepDurations;
1126 final long lastStepTime = mLastStepTime;
1127 if (lastStepTime >= 0 && numStepLevels > 0) {
1128 final long[] steps = mStepDurations;
1129 long duration = elapsedRealtime - lastStepTime;
1130 for (int i=0; i<numStepLevels; i++) {
1131 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1132 long thisDuration = duration / (numStepLevels-i);
1133 duration -= thisDuration;
1134 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1135 thisDuration = STEP_LEVEL_TIME_MASK;
1136 }
1137 steps[0] = thisDuration | modeBits;
1138 }
1139 stepCount += numStepLevels;
1140 if (stepCount > steps.length) {
1141 stepCount = steps.length;
1142 }
1143 }
1144 mNumStepDurations = stepCount;
1145 mLastStepTime = elapsedRealtime;
1146 }
1147
1148 public void readFromParcel(Parcel in) {
1149 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001150 if (N > mStepDurations.length) {
1151 throw new ParcelFormatException("more step durations than available: " + N);
1152 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001153 mNumStepDurations = N;
1154 for (int i=0; i<N; i++) {
1155 mStepDurations[i] = in.readLong();
1156 }
1157 }
1158
1159 public void writeToParcel(Parcel out) {
1160 final int N = mNumStepDurations;
1161 out.writeInt(N);
1162 for (int i=0; i<N; i++) {
1163 out.writeLong(mStepDurations[i]);
1164 }
1165 }
1166 }
1167
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001168 public static final class PackageChange {
1169 public String mPackageName;
1170 public boolean mUpdate;
1171 public int mVersionCode;
1172 }
1173
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001174 public static final class DailyItem {
1175 public long mStartTime;
1176 public long mEndTime;
1177 public LevelStepTracker mDischargeSteps;
1178 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001179 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001180 }
1181
1182 public abstract DailyItem getDailyItemLocked(int daysAgo);
1183
1184 public abstract long getCurrentDailyStartTime();
1185
1186 public abstract long getNextMinDailyDeadline();
1187
1188 public abstract long getNextMaxDailyDeadline();
1189
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001190 public abstract long[] getCpuFreqs();
1191
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001192 public final static class HistoryTag {
1193 public String string;
1194 public int uid;
1195
1196 public int poolIdx;
1197
1198 public void setTo(HistoryTag o) {
1199 string = o.string;
1200 uid = o.uid;
1201 poolIdx = o.poolIdx;
1202 }
1203
1204 public void setTo(String _string, int _uid) {
1205 string = _string;
1206 uid = _uid;
1207 poolIdx = -1;
1208 }
1209
1210 public void writeToParcel(Parcel dest, int flags) {
1211 dest.writeString(string);
1212 dest.writeInt(uid);
1213 }
1214
1215 public void readFromParcel(Parcel src) {
1216 string = src.readString();
1217 uid = src.readInt();
1218 poolIdx = -1;
1219 }
1220
1221 @Override
1222 public boolean equals(Object o) {
1223 if (this == o) return true;
1224 if (o == null || getClass() != o.getClass()) return false;
1225
1226 HistoryTag that = (HistoryTag) o;
1227
1228 if (uid != that.uid) return false;
1229 if (!string.equals(that.string)) return false;
1230
1231 return true;
1232 }
1233
1234 @Override
1235 public int hashCode() {
1236 int result = string.hashCode();
1237 result = 31 * result + uid;
1238 return result;
1239 }
1240 }
1241
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001242 /**
1243 * Optional detailed information that can go into a history step. This is typically
1244 * generated each time the battery level changes.
1245 */
1246 public final static class HistoryStepDetails {
1247 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1248 public int userTime;
1249 public int systemTime;
1250
1251 // Top three apps using CPU in the last step, with times in 1/100 second.
1252 public int appCpuUid1;
1253 public int appCpuUTime1;
1254 public int appCpuSTime1;
1255 public int appCpuUid2;
1256 public int appCpuUTime2;
1257 public int appCpuSTime2;
1258 public int appCpuUid3;
1259 public int appCpuUTime3;
1260 public int appCpuSTime3;
1261
1262 // Information from /proc/stat
1263 public int statUserTime;
1264 public int statSystemTime;
1265 public int statIOWaitTime;
1266 public int statIrqTime;
1267 public int statSoftIrqTime;
1268 public int statIdlTime;
1269
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001270 // Platform-level low power state stats
1271 public String statPlatformIdleState;
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001272 public String statSubsystemPowerState;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001273
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001274 public HistoryStepDetails() {
1275 clear();
1276 }
1277
1278 public void clear() {
1279 userTime = systemTime = 0;
1280 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1281 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1282 = appCpuUTime3 = appCpuSTime3 = 0;
1283 }
1284
1285 public void writeToParcel(Parcel out) {
1286 out.writeInt(userTime);
1287 out.writeInt(systemTime);
1288 out.writeInt(appCpuUid1);
1289 out.writeInt(appCpuUTime1);
1290 out.writeInt(appCpuSTime1);
1291 out.writeInt(appCpuUid2);
1292 out.writeInt(appCpuUTime2);
1293 out.writeInt(appCpuSTime2);
1294 out.writeInt(appCpuUid3);
1295 out.writeInt(appCpuUTime3);
1296 out.writeInt(appCpuSTime3);
1297 out.writeInt(statUserTime);
1298 out.writeInt(statSystemTime);
1299 out.writeInt(statIOWaitTime);
1300 out.writeInt(statIrqTime);
1301 out.writeInt(statSoftIrqTime);
1302 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001303 out.writeString(statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001304 out.writeString(statSubsystemPowerState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001305 }
1306
1307 public void readFromParcel(Parcel in) {
1308 userTime = in.readInt();
1309 systemTime = in.readInt();
1310 appCpuUid1 = in.readInt();
1311 appCpuUTime1 = in.readInt();
1312 appCpuSTime1 = in.readInt();
1313 appCpuUid2 = in.readInt();
1314 appCpuUTime2 = in.readInt();
1315 appCpuSTime2 = in.readInt();
1316 appCpuUid3 = in.readInt();
1317 appCpuUTime3 = in.readInt();
1318 appCpuSTime3 = in.readInt();
1319 statUserTime = in.readInt();
1320 statSystemTime = in.readInt();
1321 statIOWaitTime = in.readInt();
1322 statIrqTime = in.readInt();
1323 statSoftIrqTime = in.readInt();
1324 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001325 statPlatformIdleState = in.readString();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001326 statSubsystemPowerState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001327 }
1328 }
1329
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001330 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001331 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001332
1333 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001334 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001335
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001336 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001337 public static final byte CMD_NULL = -1;
1338 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001339 public static final byte CMD_CURRENT_TIME = 5;
1340 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001341 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001342 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001343
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001344 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001345
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001346 /**
1347 * Return whether the command code is a delta data update.
1348 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001349 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001350 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001351 }
1352
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001353 public byte batteryLevel;
1354 public byte batteryStatus;
1355 public byte batteryHealth;
1356 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001357
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001358 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001359 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001360
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001361 // The charge of the battery in micro-Ampere-hours.
1362 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001363
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001364 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001365 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001366 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001367 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001368 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1369 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001370 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001371 public static final int STATE_PHONE_STATE_SHIFT = 6;
1372 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001373 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001374 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001375 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001376
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001377 // These states always appear directly in the first int token
1378 // of a delta change; they should be ones that change relatively
1379 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001380 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1381 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001382 public static final int STATE_GPS_ON_FLAG = 1<<29;
1383 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001384 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001385 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001386 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001387 // Do not use, this is used for coulomb delta count.
1388 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001389 // These are on the lower bits used for the command; if they change
1390 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001391 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001392 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001393 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001394 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1395 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
Mike Mac2f518a2017-09-19 16:06:03 -07001396 public static final int STATE_SCREEN_DOZE_FLAG = 1 << 18;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001397 // empty slot
1398 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001399
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001400 public static final int MOST_INTERESTING_STATES =
Mike Mac2f518a2017-09-19 16:06:03 -07001401 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG | STATE_SCREEN_DOZE_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001402
1403 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001404
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001405 public int states;
1406
Dianne Hackborn3251b902014-06-20 14:40:53 -07001407 // Constants from WIFI_SUPPL_STATE_*
1408 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1409 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1410 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1411 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1412 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1413 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1414
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001415 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001416 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1417 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1418 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001419 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001420 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1421 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1422 public static final int STATE2_CHARGING_FLAG = 1<<24;
1423 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1424 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1425 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001426 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001427
1428 public static final int MOST_INTERESTING_STATES2 =
Mike Mac2f518a2017-09-19 16:06:03 -07001429 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
1430 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001431
1432 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001433
Dianne Hackborn40c87252014-03-19 16:55:40 -07001434 public int states2;
1435
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001436 // The wake lock that was acquired at this point.
1437 public HistoryTag wakelockTag;
1438
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001439 // Kernel wakeup reason at this point.
1440 public HistoryTag wakeReasonTag;
1441
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001442 // Non-null when there is more detailed information at this step.
1443 public HistoryStepDetails stepDetails;
1444
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001445 public static final int EVENT_FLAG_START = 0x8000;
1446 public static final int EVENT_FLAG_FINISH = 0x4000;
1447
1448 // No event in this item.
1449 public static final int EVENT_NONE = 0x0000;
1450 // Event is about a process that is running.
1451 public static final int EVENT_PROC = 0x0001;
1452 // Event is about an application package that is in the foreground.
1453 public static final int EVENT_FOREGROUND = 0x0002;
1454 // Event is about an application package that is at the top of the screen.
1455 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001456 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001457 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001458 // Events for all additional wake locks aquired/release within a wake block.
1459 // These are not generated by default.
1460 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001461 // Event is about an application executing a scheduled job.
1462 public static final int EVENT_JOB = 0x0006;
1463 // Events for users running.
1464 public static final int EVENT_USER_RUNNING = 0x0007;
1465 // Events for foreground user.
1466 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001467 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001468 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001469 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001470 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001471 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001472 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001473 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001474 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001475 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001476 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001477 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001478 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001479 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001480 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001481 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001482 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001483 // Event for a package being on the temporary whitelist.
1484 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001485 // Event for the screen waking up.
1486 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001487 // Event for the UID that woke up the application processor.
1488 // Used for wakeups coming from WiFi, modem, etc.
1489 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001490 // Event for reporting that a specific partial wake lock has been held for a long duration.
1491 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001492
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001493 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001494 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001495 // Mask to extract out only the type part of the event.
1496 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001497
1498 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1499 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1500 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1501 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1502 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1503 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001504 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1505 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001506 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1507 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001508 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1509 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1510 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1511 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1512 public static final int EVENT_USER_FOREGROUND_START =
1513 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1514 public static final int EVENT_USER_FOREGROUND_FINISH =
1515 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001516 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1517 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001518 public static final int EVENT_TEMP_WHITELIST_START =
1519 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1520 public static final int EVENT_TEMP_WHITELIST_FINISH =
1521 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001522 public static final int EVENT_LONG_WAKE_LOCK_START =
1523 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1524 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1525 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001526
1527 // For CMD_EVENT.
1528 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001529 public HistoryTag eventTag;
1530
Dianne Hackborn9a755432014-05-15 17:05:22 -07001531 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001532 public long currentTime;
1533
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001534 // Meta-data when reading.
1535 public int numReadInts;
1536
1537 // Pre-allocated objects.
1538 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001539 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001540 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001541
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001542 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001543 }
Bookatzc8c44962017-05-11 12:12:54 -07001544
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001545 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001546 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001547 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001548 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001549 }
Bookatzc8c44962017-05-11 12:12:54 -07001550
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001551 public int describeContents() {
1552 return 0;
1553 }
1554
1555 public void writeToParcel(Parcel dest, int flags) {
1556 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001557 int bat = (((int)cmd)&0xff)
1558 | ((((int)batteryLevel)<<8)&0xff00)
1559 | ((((int)batteryStatus)<<16)&0xf0000)
1560 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001561 | ((((int)batteryPlugType)<<24)&0xf000000)
1562 | (wakelockTag != null ? 0x10000000 : 0)
1563 | (wakeReasonTag != null ? 0x20000000 : 0)
1564 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001565 dest.writeInt(bat);
1566 bat = (((int)batteryTemperature)&0xffff)
1567 | ((((int)batteryVoltage)<<16)&0xffff0000);
1568 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001569 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001570 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001571 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001572 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001573 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001574 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001575 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001576 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001577 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001578 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001579 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001580 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001581 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001582 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001583 dest.writeLong(currentTime);
1584 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001585 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001586
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001587 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001588 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001589 int bat = src.readInt();
1590 cmd = (byte)(bat&0xff);
1591 batteryLevel = (byte)((bat>>8)&0xff);
1592 batteryStatus = (byte)((bat>>16)&0xf);
1593 batteryHealth = (byte)((bat>>20)&0xf);
1594 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001595 int bat2 = src.readInt();
1596 batteryTemperature = (short)(bat2&0xffff);
1597 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001598 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001599 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001600 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001601 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001602 wakelockTag = localWakelockTag;
1603 wakelockTag.readFromParcel(src);
1604 } else {
1605 wakelockTag = null;
1606 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001607 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001608 wakeReasonTag = localWakeReasonTag;
1609 wakeReasonTag.readFromParcel(src);
1610 } else {
1611 wakeReasonTag = null;
1612 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001613 if ((bat&0x40000000) != 0) {
1614 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001615 eventTag = localEventTag;
1616 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001617 } else {
1618 eventCode = EVENT_NONE;
1619 eventTag = null;
1620 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001621 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001622 currentTime = src.readLong();
1623 } else {
1624 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001625 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001626 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001627 }
1628
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001629 public void clear() {
1630 time = 0;
1631 cmd = CMD_NULL;
1632 batteryLevel = 0;
1633 batteryStatus = 0;
1634 batteryHealth = 0;
1635 batteryPlugType = 0;
1636 batteryTemperature = 0;
1637 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001638 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001639 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001640 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001641 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001642 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001643 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001644 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001645 }
Bookatzc8c44962017-05-11 12:12:54 -07001646
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001647 public void setTo(HistoryItem o) {
1648 time = o.time;
1649 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001650 setToCommon(o);
1651 }
1652
1653 public void setTo(long time, byte cmd, HistoryItem o) {
1654 this.time = time;
1655 this.cmd = cmd;
1656 setToCommon(o);
1657 }
1658
1659 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001660 batteryLevel = o.batteryLevel;
1661 batteryStatus = o.batteryStatus;
1662 batteryHealth = o.batteryHealth;
1663 batteryPlugType = o.batteryPlugType;
1664 batteryTemperature = o.batteryTemperature;
1665 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001666 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001667 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001668 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001669 if (o.wakelockTag != null) {
1670 wakelockTag = localWakelockTag;
1671 wakelockTag.setTo(o.wakelockTag);
1672 } else {
1673 wakelockTag = null;
1674 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001675 if (o.wakeReasonTag != null) {
1676 wakeReasonTag = localWakeReasonTag;
1677 wakeReasonTag.setTo(o.wakeReasonTag);
1678 } else {
1679 wakeReasonTag = null;
1680 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001681 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001682 if (o.eventTag != null) {
1683 eventTag = localEventTag;
1684 eventTag.setTo(o.eventTag);
1685 } else {
1686 eventTag = null;
1687 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001688 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001689 }
1690
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001691 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001692 return batteryLevel == o.batteryLevel
1693 && batteryStatus == o.batteryStatus
1694 && batteryHealth == o.batteryHealth
1695 && batteryPlugType == o.batteryPlugType
1696 && batteryTemperature == o.batteryTemperature
1697 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001698 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001699 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001700 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001701 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001702 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001703
1704 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001705 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001706 return false;
1707 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001708 if (wakelockTag != o.wakelockTag) {
1709 if (wakelockTag == null || o.wakelockTag == null) {
1710 return false;
1711 }
1712 if (!wakelockTag.equals(o.wakelockTag)) {
1713 return false;
1714 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001715 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001716 if (wakeReasonTag != o.wakeReasonTag) {
1717 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1718 return false;
1719 }
1720 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1721 return false;
1722 }
1723 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001724 if (eventTag != o.eventTag) {
1725 if (eventTag == null || o.eventTag == null) {
1726 return false;
1727 }
1728 if (!eventTag.equals(o.eventTag)) {
1729 return false;
1730 }
1731 }
1732 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001733 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001734 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001735
1736 public final static class HistoryEventTracker {
1737 private final HashMap<String, SparseIntArray>[] mActiveEvents
1738 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1739
1740 public boolean updateState(int code, String name, int uid, int poolIdx) {
1741 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1742 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1743 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1744 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001745 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001746 mActiveEvents[idx] = active;
1747 }
1748 SparseIntArray uids = active.get(name);
1749 if (uids == null) {
1750 uids = new SparseIntArray();
1751 active.put(name, uids);
1752 }
1753 if (uids.indexOfKey(uid) >= 0) {
1754 // Already set, nothing to do!
1755 return false;
1756 }
1757 uids.put(uid, poolIdx);
1758 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1759 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1760 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1761 if (active == null) {
1762 // not currently active, nothing to do.
1763 return false;
1764 }
1765 SparseIntArray uids = active.get(name);
1766 if (uids == null) {
1767 // not currently active, nothing to do.
1768 return false;
1769 }
1770 idx = uids.indexOfKey(uid);
1771 if (idx < 0) {
1772 // not currently active, nothing to do.
1773 return false;
1774 }
1775 uids.removeAt(idx);
1776 if (uids.size() <= 0) {
1777 active.remove(name);
1778 }
1779 }
1780 return true;
1781 }
1782
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001783 public void removeEvents(int code) {
1784 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1785 mActiveEvents[idx] = null;
1786 }
1787
Dianne Hackborn37de0982014-05-09 09:32:18 -07001788 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1789 return mActiveEvents[code];
1790 }
1791 }
1792
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001793 public static final class BitDescription {
1794 public final int mask;
1795 public final int shift;
1796 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001797 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001798 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001799 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07001800
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001801 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001802 this.mask = mask;
1803 this.shift = -1;
1804 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001805 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001806 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001807 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001808 }
Bookatzc8c44962017-05-11 12:12:54 -07001809
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001810 public BitDescription(int mask, int shift, String name, String shortName,
1811 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001812 this.mask = mask;
1813 this.shift = shift;
1814 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001815 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001816 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001817 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001818 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001819 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001820
Dianne Hackbornfc064132014-06-02 12:42:12 -07001821 /**
1822 * Don't allow any more batching in to the current history event. This
1823 * is called when printing partial histories, so to ensure that the next
1824 * history event will go in to a new batch after what was printed in the
1825 * last partial history.
1826 */
1827 public abstract void commitCurrentHistoryBatchLocked();
1828
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001829 public abstract int getHistoryTotalSize();
1830
1831 public abstract int getHistoryUsedSize();
1832
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001833 public abstract boolean startIteratingHistoryLocked();
1834
Dianne Hackborn099bc622014-01-22 13:39:16 -08001835 public abstract int getHistoryStringPoolSize();
1836
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001837 public abstract int getHistoryStringPoolBytes();
1838
1839 public abstract String getHistoryTagPoolString(int index);
1840
1841 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001842
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001843 public abstract boolean getNextHistoryLocked(HistoryItem out);
1844
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001845 public abstract void finishIteratingHistoryLocked();
1846
1847 public abstract boolean startIteratingOldHistoryLocked();
1848
1849 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1850
1851 public abstract void finishIteratingOldHistoryLocked();
1852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001854 * Return the base time offset for the battery history.
1855 */
1856 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07001857
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001858 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 * Returns the number of times the device has been started.
1860 */
1861 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07001862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001864 * 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 -08001865 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07001866 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 * {@hide}
1868 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001869 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07001870
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001871 /**
1872 * Returns the number of times the screen was turned on.
1873 *
1874 * {@hide}
1875 */
1876 public abstract int getScreenOnCount(int which);
1877
Mike Mac2f518a2017-09-19 16:06:03 -07001878 /**
1879 * Returns the time in microseconds that the screen has been dozing while the device was
1880 * running on battery.
1881 *
1882 * {@hide}
1883 */
1884 public abstract long getScreenDozeTime(long elapsedRealtimeUs, int which);
1885
1886 /**
1887 * Returns the number of times the screen was turned dozing.
1888 *
1889 * {@hide}
1890 */
1891 public abstract int getScreenDozeCount(int which);
1892
Jeff Browne95c3cd2014-05-02 16:59:26 -07001893 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1894
Dianne Hackborn617f8772009-03-31 15:04:46 -07001895 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1896 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1897 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1898 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1899 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07001900
Dianne Hackborn617f8772009-03-31 15:04:46 -07001901 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1902 "dark", "dim", "medium", "light", "bright"
1903 };
Bookatzc8c44962017-05-11 12:12:54 -07001904
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001905 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1906 "0", "1", "2", "3", "4"
1907 };
1908
Dianne Hackborn617f8772009-03-31 15:04:46 -07001909 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001910
Dianne Hackborn617f8772009-03-31 15:04:46 -07001911 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001912 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001913 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07001914 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07001915 * {@hide}
1916 */
1917 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001918 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07001921 * Returns the {@link Timer} object that tracks the given screen brightness.
1922 *
1923 * {@hide}
1924 */
1925 public abstract Timer getScreenBrightnessTimer(int brightnessBin);
1926
1927 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001928 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001929 * running on battery.
1930 *
1931 * {@hide}
1932 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001933 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001934
1935 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001936 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001937 *
1938 * {@hide}
1939 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001940 public abstract int getPowerSaveModeEnabledCount(int which);
1941
1942 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001943 * Constant for device idle mode: not active.
1944 */
1945 public static final int DEVICE_IDLE_MODE_OFF = 0;
1946
1947 /**
1948 * Constant for device idle mode: active in lightweight mode.
1949 */
1950 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1951
1952 /**
1953 * Constant for device idle mode: active in full mode.
1954 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07001955 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001956
1957 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001958 * Returns the time in microseconds that device has been in idle mode while
1959 * running on battery.
1960 *
1961 * {@hide}
1962 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001963 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001964
1965 /**
1966 * Returns the number of times that the devie has gone in to idle mode.
1967 *
1968 * {@hide}
1969 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001970 public abstract int getDeviceIdleModeCount(int mode, int which);
1971
1972 /**
1973 * Return the longest duration we spent in a particular device idle mode (fully in the
1974 * mode, not in idle maintenance etc).
1975 */
1976 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001977
1978 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001979 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001980 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001981 * counts all of the time that we consider the device to be idle, whether or not
1982 * it is currently in the actual device idle mode.
1983 *
1984 * {@hide}
1985 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001986 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001987
1988 /**
Bookatz8c6571b2017-10-24 15:04:41 -07001989 * Returns the number of times that the device has started idling.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001990 *
1991 * {@hide}
1992 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001993 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001994
1995 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001996 * Returns the number of times that connectivity state changed.
1997 *
1998 * {@hide}
1999 */
2000 public abstract int getNumConnectivityChange(int which);
2001
2002 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002003 * 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 -08002004 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002005 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 * {@hide}
2007 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002008 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07002009
Dianne Hackborn627bba72009-03-24 22:32:56 -07002010 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002011 * Returns the number of times a phone call was activated.
2012 *
2013 * {@hide}
2014 */
2015 public abstract int getPhoneOnCount(int which);
2016
2017 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002018 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002019 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002020 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002021 * {@hide}
2022 */
2023 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002024 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002025
Dianne Hackborn617f8772009-03-31 15:04:46 -07002026 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07002027 * Returns the time in microseconds that the phone has been trying to
2028 * acquire a signal.
2029 *
2030 * {@hide}
2031 */
2032 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002033 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002034
2035 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002036 * Returns the {@link Timer} object that tracks how much the phone has been trying to
2037 * acquire a signal.
2038 *
2039 * {@hide}
2040 */
2041 public abstract Timer getPhoneSignalScanningTimer();
2042
2043 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002044 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07002045 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002046 * {@hide}
2047 */
2048 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
2049
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002050 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002051 * Return the {@link Timer} object used to track the given signal strength's duration and
2052 * counts.
2053 */
2054 protected abstract Timer getPhoneSignalStrengthTimer(int strengthBin);
2055
2056 /**
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002057 * Returns the time in microseconds that the mobile network has been active
2058 * (in a high power state).
2059 *
2060 * {@hide}
2061 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002062 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002063
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002064 /**
2065 * Returns the number of times that the mobile network has transitioned to the
2066 * active state.
2067 *
2068 * {@hide}
2069 */
2070 public abstract int getMobileRadioActiveCount(int which);
2071
2072 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002073 * Returns the time in microseconds that is the difference between the mobile radio
2074 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
2075 * from the radio.
2076 *
2077 * {@hide}
2078 */
2079 public abstract long getMobileRadioActiveAdjustedTime(int which);
2080
2081 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002082 * Returns the time in microseconds that the mobile network has been active
2083 * (in a high power state) but not being able to blame on an app.
2084 *
2085 * {@hide}
2086 */
2087 public abstract long getMobileRadioActiveUnknownTime(int which);
2088
2089 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002090 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002091 *
2092 * {@hide}
2093 */
2094 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002095
Dianne Hackborn627bba72009-03-24 22:32:56 -07002096 public static final int DATA_CONNECTION_NONE = 0;
2097 public static final int DATA_CONNECTION_GPRS = 1;
2098 public static final int DATA_CONNECTION_EDGE = 2;
2099 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002100 public static final int DATA_CONNECTION_CDMA = 4;
2101 public static final int DATA_CONNECTION_EVDO_0 = 5;
2102 public static final int DATA_CONNECTION_EVDO_A = 6;
2103 public static final int DATA_CONNECTION_1xRTT = 7;
2104 public static final int DATA_CONNECTION_HSDPA = 8;
2105 public static final int DATA_CONNECTION_HSUPA = 9;
2106 public static final int DATA_CONNECTION_HSPA = 10;
2107 public static final int DATA_CONNECTION_IDEN = 11;
2108 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002109 public static final int DATA_CONNECTION_LTE = 13;
2110 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08002111 public static final int DATA_CONNECTION_HSPAP = 15;
2112 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002113
Dianne Hackborn627bba72009-03-24 22:32:56 -07002114 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002115 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002116 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08002117 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002118 };
Bookatzc8c44962017-05-11 12:12:54 -07002119
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002120 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002121
Dianne Hackborn627bba72009-03-24 22:32:56 -07002122 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002123 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002124 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002125 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002126 * {@hide}
2127 */
2128 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002129 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002132 * Returns the number of times the phone has entered the given data
2133 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002134 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002135 * {@hide}
2136 */
2137 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002138
Kweku Adams87b19ec2017-10-09 12:40:03 -07002139 /**
2140 * Returns the {@link Timer} object that tracks the phone's data connection type stats.
2141 */
2142 public abstract Timer getPhoneDataConnectionTimer(int dataType);
2143
Dianne Hackborn3251b902014-06-20 14:40:53 -07002144 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2145 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2146 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2147 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2148 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2149 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2150 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2151 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2152 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2153 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2154 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2155 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2156 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2157
2158 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2159
2160 static final String[] WIFI_SUPPL_STATE_NAMES = {
2161 "invalid", "disconn", "disabled", "inactive", "scanning",
2162 "authenticating", "associating", "associated", "4-way-handshake",
2163 "group-handshake", "completed", "dormant", "uninit"
2164 };
2165
2166 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2167 "inv", "dsc", "dis", "inact", "scan",
2168 "auth", "ascing", "asced", "4-way",
2169 "group", "compl", "dorm", "uninit"
2170 };
2171
Mike Mac2f518a2017-09-19 16:06:03 -07002172 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002173 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002174 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2175 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002176 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002177 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2178 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2179 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002180 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002181 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002182 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002183 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002184 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2185 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Mike Mac2f518a2017-09-19 16:06:03 -07002186 new BitDescription(HistoryItem.STATE_SCREEN_DOZE_FLAG, "screen_doze", "Sd"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002187 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2188 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2189 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2190 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2191 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2192 new String[] {"in", "out", "emergency", "off"},
2193 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002194 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2195 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2196 SignalStrength.SIGNAL_STRENGTH_NAMES,
2197 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002198 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2199 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2200 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002201 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002202
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002203 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2204 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002205 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002206 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002207 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002208 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002209 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002210 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2211 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2212 new String[] { "off", "light", "full", "???" },
2213 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002214 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2215 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2216 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002217 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2218 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2219 new String[] { "0", "1", "2", "3", "4" },
2220 new String[] { "0", "1", "2", "3", "4" }),
2221 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2222 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2223 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002224 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002225 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002226 };
2227
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002228 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002229 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002230 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2231 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002232 };
2233
2234 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002235 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002236 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002237 "Esw", "Ewa", "Elw", "Eec"
2238 };
2239
2240 @FunctionalInterface
2241 public interface IntToString {
2242 String applyAsString(int val);
2243 }
2244
2245 private static final IntToString sUidToString = UserHandle::formatUid;
2246 private static final IntToString sIntToString = Integer::toString;
2247
2248 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2249 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2250 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2251 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2252 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002253 };
2254
Dianne Hackborn617f8772009-03-31 15:04:46 -07002255 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002256 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002257 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002258 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002259 * {@hide}
2260 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002261 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002262
2263 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002264 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002265 * been in the running state while the device was running on battery.
2266 *
2267 * {@hide}
2268 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002269 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002270
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002271 public static final int WIFI_STATE_OFF = 0;
2272 public static final int WIFI_STATE_OFF_SCANNING = 1;
2273 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2274 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2275 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2276 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2277 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2278 public static final int WIFI_STATE_SOFT_AP = 7;
2279
2280 static final String[] WIFI_STATE_NAMES = {
2281 "off", "scanning", "no_net", "disconn",
2282 "sta", "p2p", "sta_p2p", "soft_ap"
2283 };
2284
2285 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2286
2287 /**
2288 * Returns the time in microseconds that WiFi has been running in the given state.
2289 *
2290 * {@hide}
2291 */
2292 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002293 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002294
2295 /**
2296 * Returns the number of times that WiFi has entered the given state.
2297 *
2298 * {@hide}
2299 */
2300 public abstract int getWifiStateCount(int wifiState, int which);
2301
The Android Open Source Project10592532009-03-18 17:39:46 -07002302 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002303 * Returns the {@link Timer} object that tracks the given WiFi state.
2304 *
2305 * {@hide}
2306 */
2307 public abstract Timer getWifiStateTimer(int wifiState);
2308
2309 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002310 * Returns the time in microseconds that the wifi supplicant has been
2311 * in a given state.
2312 *
2313 * {@hide}
2314 */
2315 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2316
2317 /**
2318 * Returns the number of times that the wifi supplicant has transitioned
2319 * to a given state.
2320 *
2321 * {@hide}
2322 */
2323 public abstract int getWifiSupplStateCount(int state, int which);
2324
Kweku Adams87b19ec2017-10-09 12:40:03 -07002325 /**
2326 * Returns the {@link Timer} object that tracks the given wifi supplicant state.
2327 *
2328 * {@hide}
2329 */
2330 public abstract Timer getWifiSupplStateTimer(int state);
2331
Dianne Hackborn3251b902014-06-20 14:40:53 -07002332 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2333
2334 /**
2335 * Returns the time in microseconds that WIFI has been running with
2336 * the given signal strength.
2337 *
2338 * {@hide}
2339 */
2340 public abstract long getWifiSignalStrengthTime(int strengthBin,
2341 long elapsedRealtimeUs, int which);
2342
2343 /**
2344 * Returns the number of times WIFI has entered the given signal strength.
2345 *
2346 * {@hide}
2347 */
2348 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2349
2350 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002351 * Returns the {@link Timer} object that tracks the given WIFI signal strength.
2352 *
2353 * {@hide}
2354 */
2355 public abstract Timer getWifiSignalStrengthTimer(int strengthBin);
2356
2357 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002358 * Returns the time in microseconds that the flashlight has been on while the device was
2359 * running on battery.
2360 *
2361 * {@hide}
2362 */
2363 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2364
2365 /**
2366 * Returns the number of times that the flashlight has been turned on while the device was
2367 * running on battery.
2368 *
2369 * {@hide}
2370 */
2371 public abstract long getFlashlightOnCount(int which);
2372
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002373 /**
2374 * Returns the time in microseconds that the camera has been on while the device was
2375 * running on battery.
2376 *
2377 * {@hide}
2378 */
2379 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2380
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002381 /**
2382 * Returns the time in microseconds that bluetooth scans were running while the device was
2383 * on battery.
2384 *
2385 * {@hide}
2386 */
2387 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002388
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002389 public static final int NETWORK_MOBILE_RX_DATA = 0;
2390 public static final int NETWORK_MOBILE_TX_DATA = 1;
2391 public static final int NETWORK_WIFI_RX_DATA = 2;
2392 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002393 public static final int NETWORK_BT_RX_DATA = 4;
2394 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002395 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2396 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2397 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2398 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2399 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002400
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002401 public abstract long getNetworkActivityBytes(int type, int which);
2402 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002403
Adam Lesinskie08af192015-03-25 16:42:59 -07002404 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002405 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002406 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002407 * actual power data.
2408 */
2409 public abstract boolean hasWifiActivityReporting();
2410
2411 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002412 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2413 * in various radio controller states, such as transmit, receive, and idle.
2414 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002415 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002416 public abstract ControllerActivityCounter getWifiControllerActivity();
2417
2418 /**
2419 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2420 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2421 * actual power data.
2422 */
2423 public abstract boolean hasBluetoothActivityReporting();
2424
2425 /**
2426 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2427 * in various radio controller states, such as transmit, receive, and idle.
2428 * @return non-null {@link ControllerActivityCounter}
2429 */
2430 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2431
2432 /**
2433 * Returns true if the BatteryStats object has detailed modem power reports.
2434 * When true, calling {@link #getModemControllerActivity()} will yield the
2435 * actual power data.
2436 */
2437 public abstract boolean hasModemActivityReporting();
2438
2439 /**
2440 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2441 * in various radio controller states, such as transmit, receive, and idle.
2442 * @return non-null {@link ControllerActivityCounter}
2443 */
2444 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002445
The Android Open Source Project10592532009-03-18 17:39:46 -07002446 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002447 * Return the wall clock time when battery stats data collection started.
2448 */
2449 public abstract long getStartClockTime();
2450
2451 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002452 * Return platform version tag that we were running in when the battery stats started.
2453 */
2454 public abstract String getStartPlatformVersion();
2455
2456 /**
2457 * Return platform version tag that we were running in when the battery stats ended.
2458 */
2459 public abstract String getEndPlatformVersion();
2460
2461 /**
2462 * Return the internal version code of the parcelled format.
2463 */
2464 public abstract int getParcelVersion();
2465
2466 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 * Return whether we are currently running on battery.
2468 */
2469 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 /**
2472 * Returns a SparseArray containing the statistics for each uid.
2473 */
2474 public abstract SparseArray<? extends Uid> getUidStats();
2475
2476 /**
2477 * Returns the current battery uptime in microseconds.
2478 *
2479 * @param curTime the amount of elapsed realtime in microseconds.
2480 */
2481 public abstract long getBatteryUptime(long curTime);
2482
2483 /**
2484 * Returns the current battery realtime in microseconds.
2485 *
2486 * @param curTime the amount of elapsed realtime in microseconds.
2487 */
2488 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002489
The Android Open Source Project10592532009-03-18 17:39:46 -07002490 /**
Evan Millar633a1742009-04-02 16:36:33 -07002491 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002492 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002493 */
Evan Millar633a1742009-04-02 16:36:33 -07002494 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002495
The Android Open Source Project10592532009-03-18 17:39:46 -07002496 /**
Evan Millar633a1742009-04-02 16:36:33 -07002497 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2498 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002499 */
Evan Millar633a1742009-04-02 16:36:33 -07002500 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501
2502 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002503 * Get the amount the battery has discharged since the stats were
2504 * last reset after charging, as a lower-end approximation.
2505 */
2506 public abstract int getLowDischargeAmountSinceCharge();
2507
2508 /**
2509 * Get the amount the battery has discharged since the stats were
2510 * last reset after charging, as an upper-end approximation.
2511 */
2512 public abstract int getHighDischargeAmountSinceCharge();
2513
2514 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002515 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2516 */
2517 public abstract int getDischargeAmount(int which);
2518
2519 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002520 * Get the amount the battery has discharged while the screen was on,
2521 * since the last time power was unplugged.
2522 */
2523 public abstract int getDischargeAmountScreenOn();
2524
2525 /**
2526 * Get the amount the battery has discharged while the screen was on,
2527 * since the last time the device was charged.
2528 */
2529 public abstract int getDischargeAmountScreenOnSinceCharge();
2530
2531 /**
2532 * Get the amount the battery has discharged while the screen was off,
2533 * since the last time power was unplugged.
2534 */
2535 public abstract int getDischargeAmountScreenOff();
2536
2537 /**
2538 * Get the amount the battery has discharged while the screen was off,
2539 * since the last time the device was charged.
2540 */
2541 public abstract int getDischargeAmountScreenOffSinceCharge();
2542
2543 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002544 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002545 * since the last time power was unplugged.
2546 */
2547 public abstract int getDischargeAmountScreenDoze();
2548
2549 /**
Kweku Adams87b19ec2017-10-09 12:40:03 -07002550 * Get the amount the battery has discharged while the screen was dozing,
Mike Mac2f518a2017-09-19 16:06:03 -07002551 * since the last time the device was charged.
2552 */
2553 public abstract int getDischargeAmountScreenDozeSinceCharge();
2554
2555 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 * Returns the total, last, or current battery uptime in microseconds.
2557 *
2558 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002559 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 */
2561 public abstract long computeBatteryUptime(long curTime, int which);
2562
2563 /**
2564 * Returns the total, last, or current battery realtime in microseconds.
2565 *
2566 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002567 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 */
2569 public abstract long computeBatteryRealtime(long curTime, int which);
2570
2571 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002572 * Returns the total, last, or current battery screen off/doze uptime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002573 *
2574 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002575 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002576 */
2577 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2578
2579 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002580 * Returns the total, last, or current battery screen off/doze realtime in microseconds.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002581 *
2582 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002583 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002584 */
2585 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2586
2587 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 * Returns the total, last, or current uptime in microseconds.
2589 *
2590 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002591 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 */
2593 public abstract long computeUptime(long curTime, int which);
2594
2595 /**
2596 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002597 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002599 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 */
2601 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002602
2603 /**
2604 * Compute an approximation for how much run time (in microseconds) is remaining on
2605 * the battery. Returns -1 if no time can be computed: either there is not
2606 * enough current data to make a decision, or the battery is currently
2607 * charging.
2608 *
2609 * @param curTime The current elepsed realtime in microseconds.
2610 */
2611 public abstract long computeBatteryTimeRemaining(long curTime);
2612
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002613 // The part of a step duration that is the actual time.
2614 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2615
2616 // Bits in a step duration that are the new battery level we are at.
2617 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002618 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002619
2620 // Bits in a step duration that are the initial mode we were in at that step.
2621 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002622 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002623
2624 // Bits in a step duration that indicate which modes changed during that step.
2625 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002626 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002627
2628 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2629 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2630
Santos Cordone94f0502017-02-24 12:31:20 -08002631 // The largest value for screen state that is tracked in battery states. Any values above
2632 // this should be mapped back to one of the tracked values before being tracked here.
2633 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2634
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002635 // Step duration mode: power save is on.
2636 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2637
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002638 // Step duration mode: device is currently in idle mode.
2639 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2640
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002641 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2642 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002643 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2644 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002645 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2646 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2647 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2648 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2649 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002650 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2651 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002652 };
2653 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2654 (Display.STATE_OFF-1),
2655 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002656 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002657 (Display.STATE_ON-1),
2658 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2659 (Display.STATE_DOZE-1),
2660 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2661 (Display.STATE_DOZE_SUSPEND-1),
2662 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002663 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002664 };
2665 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2666 "screen off",
2667 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002668 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002669 "screen on",
2670 "screen on power save",
2671 "screen doze",
2672 "screen doze power save",
2673 "screen doze-suspend",
2674 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002675 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002676 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002677
2678 /**
Mike Mac2f518a2017-09-19 16:06:03 -07002679 * Return the amount of battery discharge while the screen was off, measured in
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002680 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2681 * a coulomb counter.
2682 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002683 public abstract long getUahDischargeScreenOff(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002684
2685 /**
2686 * Return the amount of battery discharge while the screen was in doze mode, measured in
2687 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2688 * a coulomb counter.
2689 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002690 public abstract long getUahDischargeScreenDoze(int which);
Mike Mac2f518a2017-09-19 16:06:03 -07002691
2692 /**
2693 * Return the amount of battery discharge measured in micro-Ampere-hours. This will be
2694 * non-zero only if the device's battery has a coulomb counter.
2695 */
Kweku Adams87b19ec2017-10-09 12:40:03 -07002696 public abstract long getUahDischarge(int which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002697
2698 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002699 * Returns the estimated real battery capacity, which may be less than the capacity
2700 * declared by the PowerProfile.
2701 * @return The estimated battery capacity in mAh.
2702 */
2703 public abstract int getEstimatedBatteryCapacity();
2704
2705 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002706 * @return The minimum learned battery capacity in uAh.
2707 */
2708 public abstract int getMinLearnedBatteryCapacity();
2709
2710 /**
2711 * @return The maximum learned battery capacity in uAh.
2712 */
2713 public abstract int getMaxLearnedBatteryCapacity() ;
2714
2715 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002716 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002717 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002718 public abstract LevelStepTracker getDischargeLevelStepTracker();
2719
2720 /**
2721 * Return the array of daily discharge step durations.
2722 */
2723 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002724
2725 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002726 * Compute an approximation for how much time (in microseconds) remains until the battery
2727 * is fully charged. Returns -1 if no time can be computed: either there is not
2728 * enough current data to make a decision, or the battery is currently
2729 * discharging.
2730 *
2731 * @param curTime The current elepsed realtime in microseconds.
2732 */
2733 public abstract long computeChargeTimeRemaining(long curTime);
2734
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002735 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002736 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002737 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002738 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002739
2740 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002741 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002742 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002743 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002744
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002745 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2746
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002747 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002748
Evan Millarc64edde2009-04-18 12:26:32 -07002749 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750
Bookatz50df7112017-08-04 14:53:26 -07002751 /**
2752 * Returns Timers tracking the total time of each Resource Power Manager state and voter.
2753 */
2754 public abstract Map<String, ? extends Timer> getRpmStats();
2755 /**
2756 * Returns Timers tracking the screen-off time of each Resource Power Manager state and voter.
2757 */
2758 public abstract Map<String, ? extends Timer> getScreenOffRpmStats();
2759
2760
James Carr2dd7e5e2016-07-20 18:48:39 -07002761 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2762
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002763 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2764
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002765 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 long days = seconds / (60 * 60 * 24);
2767 if (days != 0) {
2768 out.append(days);
2769 out.append("d ");
2770 }
2771 long used = days * 60 * 60 * 24;
2772
2773 long hours = (seconds - used) / (60 * 60);
2774 if (hours != 0 || used != 0) {
2775 out.append(hours);
2776 out.append("h ");
2777 }
2778 used += hours * 60 * 60;
2779
2780 long mins = (seconds-used) / 60;
2781 if (mins != 0 || used != 0) {
2782 out.append(mins);
2783 out.append("m ");
2784 }
2785 used += mins * 60;
2786
2787 if (seconds != 0 || used != 0) {
2788 out.append(seconds-used);
2789 out.append("s ");
2790 }
2791 }
2792
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002793 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002795 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 sb.append(time - (sec * 1000));
2797 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 }
2799
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002800 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002801 long sec = time / 1000;
2802 formatTimeRaw(sb, sec);
2803 sb.append(time - (sec * 1000));
2804 sb.append("ms");
2805 }
2806
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002807 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002809 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 }
2811 float perc = ((float)num) / ((float)den) * 100;
2812 mFormatBuilder.setLength(0);
2813 mFormatter.format("%.1f%%", perc);
2814 return mFormatBuilder.toString();
2815 }
2816
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002817 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002818 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07002819
Evan Millar22ac0432009-03-31 11:33:18 -07002820 if (bytes < BYTES_PER_KB) {
2821 return bytes + "B";
2822 } else if (bytes < BYTES_PER_MB) {
2823 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2824 return mFormatBuilder.toString();
2825 } else if (bytes < BYTES_PER_GB){
2826 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2827 return mFormatBuilder.toString();
2828 } else {
2829 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2830 return mFormatBuilder.toString();
2831 }
2832 }
2833
Kweku Adams103351f2017-10-16 14:39:34 -07002834 private static long roundUsToMs(long timeUs) {
2835 return (timeUs + 500) / 1000;
2836 }
2837
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002838 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002839 if (timer != null) {
2840 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002841 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002842 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2843 return totalTimeMillis;
2844 }
2845 return 0;
2846 }
2847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 /**
2849 *
2850 * @param sb a StringBuilder object.
2851 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002852 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002854 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 * @param linePrefix a String to be prepended to each line of output.
2856 * @return the line prefix
2857 */
2858 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002859 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002862 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002863
Evan Millarc64edde2009-04-18 12:26:32 -07002864 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 if (totalTimeMillis != 0) {
2866 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002867 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002868 if (name != null) {
2869 sb.append(name);
2870 sb.append(' ');
2871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 sb.append('(');
2873 sb.append(count);
2874 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002875 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2876 if (maxDurationMs >= 0) {
2877 sb.append(" max=");
2878 sb.append(maxDurationMs);
2879 }
Bookatz506a8182017-05-01 14:18:42 -07002880 // Put actual time if it is available and different from totalTimeMillis.
2881 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
2882 if (totalDurMs > totalTimeMillis) {
2883 sb.append(" actual=");
2884 sb.append(totalDurMs);
2885 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07002886 if (timer.isRunningLocked()) {
2887 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2888 if (currentMs >= 0) {
2889 sb.append(" (running for ");
2890 sb.append(currentMs);
2891 sb.append("ms)");
2892 } else {
2893 sb.append(" (running)");
2894 }
2895 }
2896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 return ", ";
2898 }
2899 }
2900 return linePrefix;
2901 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002902
2903 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07002904 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002905 *
2906 * @param pw a PrintWriter object to print to.
2907 * @param sb a StringBuilder object.
2908 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08002909 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002910 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2911 * @param prefix a String to be prepended to each line of output.
2912 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002913 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002914 */
2915 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07002916 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002917 if (timer != null) {
2918 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07002919 final long totalTimeMs = (timer.getTotalTimeLocked(
2920 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002921 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002922 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002923 sb.setLength(0);
2924 sb.append(prefix);
2925 sb.append(" ");
2926 sb.append(type);
2927 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002928 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002929 sb.append("realtime (");
2930 sb.append(count);
2931 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002932 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
2933 if (maxDurationMs >= 0) {
2934 sb.append(" max=");
2935 sb.append(maxDurationMs);
2936 }
2937 if (timer.isRunningLocked()) {
2938 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
2939 if (currentMs >= 0) {
2940 sb.append(" (running for ");
2941 sb.append(currentMs);
2942 sb.append("ms)");
2943 } else {
2944 sb.append(" (running)");
2945 }
2946 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002947 pw.println(sb.toString());
2948 return true;
2949 }
2950 }
2951 return false;
2952 }
Bookatzc8c44962017-05-11 12:12:54 -07002953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 /**
2955 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07002956 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 * @param sb a StringBuilder object.
2958 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002959 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002961 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 * @param linePrefix a String to be prepended to each line of output.
2963 * @return the line prefix
2964 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002965 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2966 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 long totalTimeMicros = 0;
2968 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07002969 long max = 0;
2970 long current = 0;
2971 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002973 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07002974 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002975 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2976 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07002977 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 }
2979 sb.append(linePrefix);
2980 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2981 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002982 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002984 sb.append(',');
2985 sb.append(current);
2986 sb.append(',');
2987 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07002988 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
2989 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
2990 // totalDuration independent of totalTimeMicros (since they are not pooled).
2991 if (name != null) {
2992 sb.append(',');
2993 sb.append(totalDuration);
2994 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 return ",";
2996 }
Bookatz506a8182017-05-01 14:18:42 -07002997
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002998 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
2999 String type) {
3000 pw.print(BATTERY_STATS_CHECKIN_VERSION);
3001 pw.print(',');
3002 pw.print(uid);
3003 pw.print(',');
3004 pw.print(category);
3005 pw.print(',');
3006 pw.print(type);
3007 }
3008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 /**
3010 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07003011 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012 * @param pw the PageWriter to dump log to
3013 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3014 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3015 * @param args type-dependent data arguments
3016 */
Bookatzc8c44962017-05-11 12:12:54 -07003017 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003019 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003020 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003021 pw.print(',');
3022 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003024 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07003026
3027 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003028 * Dump a given timer stat for terse checkin mode.
3029 *
3030 * @param pw the PageWriter to dump log to
3031 * @param uid the UID to log
3032 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
3033 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
3034 * @param timer a {@link Timer} to dump stats for
3035 * @param rawRealtime the current elapsed realtime of the system in microseconds
3036 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3037 */
3038 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
3039 Timer timer, long rawRealtime, int which) {
3040 if (timer != null) {
3041 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003042 final long totalTime = roundUsToMs(timer.getTotalTimeLocked(rawRealtime, which));
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003043 final int count = timer.getCountLocked(which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003044 if (totalTime != 0 || count != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003045 dumpLine(pw, uid, category, type, totalTime, count);
3046 }
3047 }
3048 }
3049
3050 /**
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003051 * Dump a given timer stat to the proto stream.
3052 *
3053 * @param proto the ProtoOutputStream to log to
3054 * @param fieldId type of data, the field to save to (e.g. AggregatedBatteryStats.WAKELOCK)
3055 * @param timer a {@link Timer} to dump stats for
3056 * @param rawRealtimeUs the current elapsed realtime of the system in microseconds
3057 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
3058 */
3059 private static void dumpTimer(ProtoOutputStream proto, long fieldId,
Kweku Adams87b19ec2017-10-09 12:40:03 -07003060 Timer timer, long rawRealtimeUs, int which) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003061 if (timer == null) {
3062 return;
3063 }
3064 // Convert from microseconds to milliseconds with rounding
Kweku Adams103351f2017-10-16 14:39:34 -07003065 final long timeMs = roundUsToMs(timer.getTotalTimeLocked(rawRealtimeUs, which));
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003066 final int count = timer.getCountLocked(which);
Kweku Adams103351f2017-10-16 14:39:34 -07003067 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs / 1000);
3068 final long curDurationMs = timer.getCurrentDurationMsLocked(rawRealtimeUs / 1000);
3069 final long totalDurationMs = timer.getTotalDurationMsLocked(rawRealtimeUs / 1000);
3070 if (timeMs != 0 || count != 0 || maxDurationMs != -1 || curDurationMs != -1
3071 || totalDurationMs != -1) {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003072 final long token = proto.start(fieldId);
Kweku Adams103351f2017-10-16 14:39:34 -07003073 proto.write(TimerProto.DURATION_MS, timeMs);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003074 proto.write(TimerProto.COUNT, count);
Kweku Adams103351f2017-10-16 14:39:34 -07003075 // These values will be -1 for timers that don't implement the functionality.
3076 if (maxDurationMs != -1) {
3077 proto.write(TimerProto.MAX_DURATION_MS, maxDurationMs);
3078 }
3079 if (curDurationMs != -1) {
3080 proto.write(TimerProto.CURRENT_DURATION_MS, curDurationMs);
3081 }
3082 if (totalDurationMs != -1) {
3083 proto.write(TimerProto.TOTAL_DURATION_MS, totalDurationMs);
3084 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003085 proto.end(token);
3086 }
3087 }
3088
3089 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003090 * Checks if the ControllerActivityCounter has any data worth dumping.
3091 */
3092 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
3093 if (counter == null) {
3094 return false;
3095 }
3096
3097 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
3098 || counter.getRxTimeCounter().getCountLocked(which) != 0
3099 || counter.getPowerCounter().getCountLocked(which) != 0) {
3100 return true;
3101 }
3102
3103 for (LongCounter c : counter.getTxTimeCounters()) {
3104 if (c.getCountLocked(which) != 0) {
3105 return true;
3106 }
3107 }
3108 return false;
3109 }
3110
3111 /**
3112 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3113 * The order of the arguments in the final check in line is:
3114 *
3115 * idle, rx, power, tx...
3116 *
3117 * where tx... is one or more transmit level times.
3118 */
3119 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
3120 String type,
3121 ControllerActivityCounter counter,
3122 int which) {
3123 if (!controllerActivityHasData(counter, which)) {
3124 return;
3125 }
3126
3127 dumpLineHeader(pw, uid, category, type);
3128 pw.print(",");
3129 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
3130 pw.print(",");
3131 pw.print(counter.getRxTimeCounter().getCountLocked(which));
3132 pw.print(",");
3133 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3134 for (LongCounter c : counter.getTxTimeCounters()) {
3135 pw.print(",");
3136 pw.print(c.getCountLocked(which));
3137 }
3138 pw.println();
3139 }
3140
Kweku Adams2f73ecd2017-09-27 16:59:19 -07003141 /**
3142 * Dumps the ControllerActivityCounter if it has any data worth dumping.
3143 */
3144 private static void dumpControllerActivityProto(ProtoOutputStream proto, long fieldId,
3145 ControllerActivityCounter counter,
3146 int which) {
3147 if (!controllerActivityHasData(counter, which)) {
3148 return;
3149 }
3150
3151 final long cToken = proto.start(fieldId);
3152
3153 proto.write(ControllerActivityProto.IDLE_DURATION_MS,
3154 counter.getIdleTimeCounter().getCountLocked(which));
3155 proto.write(ControllerActivityProto.RX_DURATION_MS,
3156 counter.getRxTimeCounter().getCountLocked(which));
3157 proto.write(ControllerActivityProto.POWER_MAH,
3158 counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
3159
3160 long tToken;
3161 LongCounter[] txCounters = counter.getTxTimeCounters();
3162 for (int i = 0; i < txCounters.length; ++i) {
3163 LongCounter c = txCounters[i];
3164 tToken = proto.start(ControllerActivityProto.TX);
3165 proto.write(ControllerActivityProto.TxLevel.LEVEL, i);
3166 proto.write(ControllerActivityProto.TxLevel.DURATION_MS, c.getCountLocked(which));
3167 proto.end(tToken);
3168 }
3169
3170 proto.end(cToken);
3171 }
3172
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003173 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
3174 String prefix, String controllerName,
3175 ControllerActivityCounter counter,
3176 int which) {
3177 if (controllerActivityHasData(counter, which)) {
3178 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
3179 }
3180 }
3181
3182 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
3183 String controllerName,
3184 ControllerActivityCounter counter, int which) {
3185 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
3186 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
3187 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003188 // Battery real time
3189 final long totalControllerActivityTimeMs
3190 = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003191 long totalTxTimeMs = 0;
3192 for (LongCounter txState : counter.getTxTimeCounters()) {
3193 totalTxTimeMs += txState.getCountLocked(which);
3194 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003195 final long sleepTimeMs
3196 = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + totalTxTimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003197
3198 sb.setLength(0);
3199 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003200 sb.append(" ");
3201 sb.append(controllerName);
3202 sb.append(" Sleep time: ");
3203 formatTimeMs(sb, sleepTimeMs);
3204 sb.append("(");
3205 sb.append(formatRatioLocked(sleepTimeMs, totalControllerActivityTimeMs));
3206 sb.append(")");
3207 pw.println(sb.toString());
3208
3209 sb.setLength(0);
3210 sb.append(prefix);
3211 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003212 sb.append(controllerName);
3213 sb.append(" Idle time: ");
3214 formatTimeMs(sb, idleTimeMs);
3215 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003216 sb.append(formatRatioLocked(idleTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003217 sb.append(")");
3218 pw.println(sb.toString());
3219
3220 sb.setLength(0);
3221 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003222 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003223 sb.append(controllerName);
3224 sb.append(" Rx time: ");
3225 formatTimeMs(sb, rxTimeMs);
3226 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003227 sb.append(formatRatioLocked(rxTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003228 sb.append(")");
3229 pw.println(sb.toString());
3230
3231 sb.setLength(0);
3232 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003233 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003234 sb.append(controllerName);
3235 sb.append(" Tx time: ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003236
Siddharth Ray3c648c42017-10-02 17:30:58 -07003237 String [] powerLevel;
3238 switch(controllerName) {
3239 case "Cellular":
3240 powerLevel = new String[] {
3241 " less than 0dBm: ",
3242 " 0dBm to 8dBm: ",
3243 " 8dBm to 15dBm: ",
3244 " 15dBm to 20dBm: ",
3245 " above 20dBm: "};
3246 break;
3247 default:
3248 powerLevel = new String[] {"[0]", "[1]", "[2]", "[3]", "[4]"};
3249 break;
3250 }
3251 final int numTxLvls = Math.min(counter.getTxTimeCounters().length, powerLevel.length);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003252 if (numTxLvls > 1) {
Siddharth Ray3c648c42017-10-02 17:30:58 -07003253 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003254 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3255 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3256 sb.setLength(0);
3257 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07003258 sb.append(" ");
3259 sb.append(powerLevel[lvl]);
3260 sb.append(" ");
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003261 formatTimeMs(sb, txLvlTimeMs);
3262 sb.append("(");
Siddharth Ray3c648c42017-10-02 17:30:58 -07003263 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003264 sb.append(")");
3265 pw.println(sb.toString());
3266 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07003267 } else {
3268 final long txLvlTimeMs = counter.getTxTimeCounters()[0].getCountLocked(which);
3269 formatTimeMs(sb, txLvlTimeMs);
3270 sb.append("(");
3271 sb.append(formatRatioLocked(txLvlTimeMs, totalControllerActivityTimeMs));
3272 sb.append(")");
3273 pw.println(sb.toString());
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003274 }
3275
Siddharth Ray3c648c42017-10-02 17:30:58 -07003276 if (powerDrainMaMs > 0) {
3277 sb.setLength(0);
3278 sb.append(prefix);
3279 sb.append(" ");
3280 sb.append(controllerName);
3281 sb.append(" Battery drain: ").append(
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003282 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
Siddharth Ray3c648c42017-10-02 17:30:58 -07003283 sb.append("mAh");
3284 pw.println(sb.toString());
3285 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003286 }
3287
3288 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003289 * Temporary for settings.
3290 */
3291 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3292 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3293 }
3294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 /**
3296 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003297 *
Kweku Adams87b19ec2017-10-09 12:40:03 -07003298 * NOTE: all times are expressed in microseconds, unless specified otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003300 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3301 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 final long rawUptime = SystemClock.uptimeMillis() * 1000;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003303 final long rawRealtimeMs = SystemClock.elapsedRealtime();
3304 final long rawRealtime = rawRealtimeMs * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3307 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003308 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3309 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3310 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 final long totalRealtime = computeRealtime(rawRealtime, which);
3312 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003313 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Mike Mac2f518a2017-09-19 16:06:03 -07003314 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003315 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003316 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003317 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3318 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003319 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003320 rawRealtime, which);
3321 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3322 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003323 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003324 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003325 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003326 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Kweku Adams87b19ec2017-10-09 12:40:03 -07003327 final long dischargeCount = getUahDischarge(which);
3328 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
3329 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003330
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003331 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003332
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003333 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003334 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003335
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003336 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003339 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003340 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003341 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003342 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003343 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003344 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003345 getEstimatedBatteryCapacity(),
Mike Mac2f518a2017-09-19 16:06:03 -07003346 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity(),
3347 screenDozeTime / 1000);
Adam Lesinski67c134f2016-06-10 15:15:08 -07003348
Bookatzc8c44962017-05-11 12:12:54 -07003349
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003350 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003351 long fullWakeLockTimeTotal = 0;
3352 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003353
Evan Millar22ac0432009-03-31 11:33:18 -07003354 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003355 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003356
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003357 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3358 = u.getWakelockStats();
3359 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3360 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003361
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003362 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3363 if (fullWakeTimer != null) {
3364 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3365 which);
3366 }
3367
3368 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3369 if (partialWakeTimer != null) {
3370 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3371 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003372 }
3373 }
3374 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003375
3376 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003377 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3378 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3379 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3380 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3381 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3382 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3383 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3384 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003385 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3386 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003387 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3388 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003389 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3390 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003391
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003392 // Dump Modem controller stats
3393 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3394 getModemControllerActivity(), which);
3395
Adam Lesinskie283d332015-04-16 12:29:25 -07003396 // Dump Wifi controller stats
3397 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3398 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003399 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003400 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003401
3402 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3403 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003404
3405 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003406 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3407 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 // Dump misc stats
3410 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003411 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003412 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003413 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003414 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003415 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003416 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3417 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003418 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003419 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3420 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3421 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3422 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003423 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003424
Dianne Hackborn617f8772009-03-31 15:04:46 -07003425 // Dump screen brightness stats
3426 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3427 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003428 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003429 }
3430 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003431
Dianne Hackborn627bba72009-03-24 22:32:56 -07003432 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003433 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3434 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003435 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003436 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003437 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003438 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003439 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003440 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003441 args[i] = getPhoneSignalStrengthCount(i, which);
3442 }
3443 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003444
Dianne Hackborn627bba72009-03-24 22:32:56 -07003445 // Dump network type stats
3446 args = new Object[NUM_DATA_CONNECTION_TYPES];
3447 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003448 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003449 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003450 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3451 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3452 args[i] = getPhoneDataConnectionCount(i, which);
3453 }
3454 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003455
3456 // Dump wifi state stats
3457 args = new Object[NUM_WIFI_STATES];
3458 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003459 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003460 }
3461 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3462 for (int i=0; i<NUM_WIFI_STATES; i++) {
3463 args[i] = getWifiStateCount(i, which);
3464 }
3465 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3466
Dianne Hackborn3251b902014-06-20 14:40:53 -07003467 // Dump wifi suppl state stats
3468 args = new Object[NUM_WIFI_SUPPL_STATES];
3469 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3470 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3471 }
3472 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3473 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3474 args[i] = getWifiSupplStateCount(i, which);
3475 }
3476 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3477
3478 // Dump wifi signal strength stats
3479 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3480 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3481 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3482 }
3483 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3484 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3485 args[i] = getWifiSignalStrengthCount(i, which);
3486 }
3487 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3488
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003489 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003490 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003491 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003492 }
Bookatzc8c44962017-05-11 12:12:54 -07003493
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003494 if (which == STATS_SINCE_UNPLUGGED) {
3495 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3496 getDischargeStartLevel()-getDischargeCurrentLevel(),
3497 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003498 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
Mike Mac2f518a2017-09-19 16:06:03 -07003499 dischargeCount / 1000, dischargeScreenOffCount / 1000,
3500 getDischargeAmountScreenDoze(), dischargeScreenDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003501 } else {
3502 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3503 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003504 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003505 getDischargeAmountScreenOffSinceCharge(),
Mike Mac2f518a2017-09-19 16:06:03 -07003506 dischargeCount / 1000, dischargeScreenOffCount / 1000,
3507 getDischargeAmountScreenDozeSinceCharge(), dischargeScreenDozeCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003508 }
Bookatzc8c44962017-05-11 12:12:54 -07003509
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003510 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003511 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003512 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003513 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003514 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003515 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003516 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3517 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003518 }
Evan Millarc64edde2009-04-18 12:26:32 -07003519 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003520 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003521 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003522 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3523 // Not doing the regular wake lock formatting to remain compatible
3524 // with the old checkin format.
3525 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3526 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003527 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003528 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003529 }
3530 }
Evan Millarc64edde2009-04-18 12:26:32 -07003531 }
Bookatzc8c44962017-05-11 12:12:54 -07003532
Bookatz50df7112017-08-04 14:53:26 -07003533 final Map<String, ? extends Timer> rpmStats = getRpmStats();
3534 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
3535 if (rpmStats.size() > 0) {
3536 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
3537 sb.setLength(0);
3538 Timer totalTimer = ent.getValue();
3539 long timeMs = (totalTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3540 int count = totalTimer.getCountLocked(which);
3541 Timer screenOffTimer = screenOffRpmStats.get(ent.getKey());
3542 long screenOffTimeMs = screenOffTimer != null
3543 ? (screenOffTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3544 int screenOffCount = screenOffTimer != null
3545 ? screenOffTimer.getCountLocked(which) : 0;
Bookatz82b341172017-09-07 19:06:08 -07003546 if (SCREEN_OFF_RPM_STATS_ENABLED) {
3547 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3548 "\"" + ent.getKey() + "\"", timeMs, count, screenOffTimeMs,
3549 screenOffCount);
3550 } else {
3551 dumpLine(pw, 0 /* uid */, category, RESOURCE_POWER_MANAGER_DATA,
3552 "\"" + ent.getKey() + "\"", timeMs, count);
3553 }
Bookatz50df7112017-08-04 14:53:26 -07003554 }
3555 }
3556
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003557 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003558 helper.create(this);
3559 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003560 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003561 if (sippers != null && sippers.size() > 0) {
3562 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3563 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003564 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003565 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3566 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
Kweku Adams87b19ec2017-10-09 12:40:03 -07003567 int uid = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003568 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003569 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003570 String label;
3571 switch (bs.drainType) {
3572 case IDLE:
3573 label="idle";
3574 break;
3575 case CELL:
3576 label="cell";
3577 break;
3578 case PHONE:
3579 label="phone";
3580 break;
3581 case WIFI:
3582 label="wifi";
3583 break;
3584 case BLUETOOTH:
3585 label="blue";
3586 break;
3587 case SCREEN:
3588 label="scrn";
3589 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003590 case FLASHLIGHT:
3591 label="flashlight";
3592 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003593 case APP:
3594 uid = bs.uidObj.getUid();
3595 label = "uid";
3596 break;
3597 case USER:
3598 uid = UserHandle.getUid(bs.userId, 0);
3599 label = "user";
3600 break;
3601 case UNACCOUNTED:
3602 label = "unacc";
3603 break;
3604 case OVERCOUNTED:
3605 label = "over";
3606 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003607 case CAMERA:
3608 label = "camera";
3609 break;
Kweku Adams87b19ec2017-10-09 12:40:03 -07003610 case MEMORY:
3611 label = "memory";
3612 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003613 default:
3614 label = "???";
3615 }
3616 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07003617 BatteryStatsHelper.makemAh(bs.totalPowerMah),
3618 bs.shouldHide ? 1 : 0,
3619 BatteryStatsHelper.makemAh(bs.screenPowerMah),
3620 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003621 }
3622 }
3623
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003624 final long[] cpuFreqs = getCpuFreqs();
3625 if (cpuFreqs != null) {
3626 sb.setLength(0);
3627 for (int i = 0; i < cpuFreqs.length; ++i) {
3628 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3629 }
3630 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3631 }
3632
Kweku Adams87b19ec2017-10-09 12:40:03 -07003633 // Dump stats per UID.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 for (int iu = 0; iu < NU; iu++) {
3635 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003636 if (reqUid >= 0 && uid != reqUid) {
3637 continue;
3638 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003639 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003641 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003642 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3643 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3644 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3645 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3646 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3647 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3648 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3649 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003650 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003651 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3652 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003653 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003654 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3655 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003656 // Background data transfers
3657 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3658 which);
3659 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3660 which);
3661 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3662 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3663 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3664 which);
3665 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3666 which);
3667 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3668 which);
3669 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3670 which);
3671
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003672 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3673 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003674 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003675 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3676 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3677 || wifiBytesBgTx > 0
3678 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3679 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003680 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3681 wifiBytesRx, wifiBytesTx,
3682 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003683 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003684 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003685 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3686 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3687 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3688 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003689 }
3690
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003691 // Dump modem controller data, per UID.
3692 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3693 u.getModemControllerActivity(), which);
3694
3695 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003696 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3697 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3698 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003699 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3700 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003701 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3702 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3703 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003704 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003705 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003706 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3707 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003708 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3709 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003710 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003711 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003712 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003713
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003714 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3715 u.getWifiControllerActivity(), which);
3716
Bookatz867c0d72017-03-07 18:23:42 -08003717 final Timer bleTimer = u.getBluetoothScanTimer();
3718 if (bleTimer != null) {
3719 // Convert from microseconds to milliseconds with rounding
3720 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3721 / 1000;
3722 if (totalTime != 0) {
3723 final int count = bleTimer.getCountLocked(which);
3724 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3725 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003726 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3727 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3728 final long actualTimeBg = bleTimerBg != null ?
3729 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003730 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003731 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3732 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003733 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3734 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3735 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3736 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3737 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
3738 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3739 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
3740 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3741 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
3742 final Timer unoptimizedScanTimerBg =
3743 u.getBluetoothUnoptimizedScanBackgroundTimer();
3744 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
3745 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3746 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
3747 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3748
Bookatz867c0d72017-03-07 18:23:42 -08003749 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07003750 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
3751 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
3752 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08003753 }
3754 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003755
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003756 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3757 u.getBluetoothControllerActivity(), which);
3758
Dianne Hackborn617f8772009-03-31 15:04:46 -07003759 if (u.hasUserActivity()) {
3760 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3761 boolean hasData = false;
3762 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3763 int val = u.getUserActivityCount(i, which);
3764 args[i] = val;
3765 if (val != 0) hasData = true;
3766 }
3767 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003768 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003769 }
3770 }
Bookatzc8c44962017-05-11 12:12:54 -07003771
3772 if (u.getAggregatedPartialWakelockTimer() != null) {
3773 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07003774 // Times are since reset (regardless of 'which')
3775 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07003776 final Timer bgTimer = timer.getSubTimer();
3777 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003778 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07003779 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
3780 }
3781
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003782 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3783 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3784 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3785 String linePrefix = "";
3786 sb.setLength(0);
3787 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3788 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003789 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3790 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003791 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003792 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
3793 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003794 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3795 rawRealtime, "w", which, linePrefix);
3796
Kweku Adams103351f2017-10-16 14:39:34 -07003797 // Only log if we had at least one wakelock...
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003798 if (sb.length() > 0) {
3799 String name = wakelocks.keyAt(iw);
3800 if (name.indexOf(',') >= 0) {
3801 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003802 }
Yi Jin02483362017-08-04 11:30:44 -07003803 if (name.indexOf('\n') >= 0) {
3804 name = name.replace('\n', '_');
3805 }
3806 if (name.indexOf('\r') >= 0) {
3807 name = name.replace('\r', '_');
3808 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003809 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 }
3811 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003812
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003813 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3814 for (int isy=syncs.size()-1; isy>=0; isy--) {
3815 final Timer timer = syncs.valueAt(isy);
3816 // Convert from microseconds to milliseconds with rounding
3817 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3818 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07003819 final Timer bgTimer = timer.getSubTimer();
3820 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003821 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07003822 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003823 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003824 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07003825 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003826 }
3827 }
3828
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003829 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3830 for (int ij=jobs.size()-1; ij>=0; ij--) {
3831 final Timer timer = jobs.valueAt(ij);
3832 // Convert from microseconds to milliseconds with rounding
3833 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3834 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07003835 final Timer bgTimer = timer.getSubTimer();
3836 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003837 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07003838 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003839 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003840 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07003841 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003842 }
3843 }
3844
Dianne Hackborn94326cb2017-06-28 16:17:20 -07003845 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
3846 for (int ic=completions.size()-1; ic>=0; ic--) {
3847 SparseIntArray types = completions.valueAt(ic);
3848 if (types != null) {
3849 dumpLine(pw, uid, category, JOB_COMPLETION_DATA,
3850 "\"" + completions.keyAt(ic) + "\"",
3851 types.get(JobParameters.REASON_CANCELED, 0),
3852 types.get(JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, 0),
3853 types.get(JobParameters.REASON_PREEMPT, 0),
3854 types.get(JobParameters.REASON_TIMEOUT, 0),
3855 types.get(JobParameters.REASON_DEVICE_IDLE, 0));
3856 }
3857 }
3858
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003859 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3860 rawRealtime, which);
3861 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3862 rawRealtime, which);
3863 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3864 rawRealtime, which);
3865 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3866 rawRealtime, which);
3867
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003868 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3869 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003870 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003871 final Uid.Sensor se = sensors.valueAt(ise);
3872 final int sensorNumber = sensors.keyAt(ise);
3873 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003874 if (timer != null) {
3875 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003876 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3877 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003878 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08003879 final int count = timer.getCountLocked(which);
3880 final Timer bgTimer = se.getSensorBackgroundTime();
3881 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003882 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3883 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
3884 final long bgActualTime = bgTimer != null ?
3885 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3886 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
3887 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003889 }
3890 }
3891
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003892 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3893 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003894
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07003895 dumpTimer(pw, uid, category, FOREGROUND_ACTIVITY_DATA, u.getForegroundActivityTimer(),
3896 rawRealtime, which);
3897
3898 dumpTimer(pw, uid, category, FOREGROUND_SERVICE_DATA, u.getForegroundServiceTimer(),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003899 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003900
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003901 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003902 long totalStateTime = 0;
3903 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003904 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3905 totalStateTime += time;
3906 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003907 }
3908 if (totalStateTime > 0) {
3909 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3910 }
3911
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003912 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3913 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003914 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003915 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003916 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003917 }
3918
Sudheer Shankaa87245d2017-08-10 12:02:31 -07003919 // If the cpuFreqs is null, then don't bother checking for cpu freq times.
3920 if (cpuFreqs != null) {
3921 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
3922 // If total cpuFreqTimes is null, then we don't need to check for
3923 // screenOffCpuFreqTimes.
3924 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
3925 sb.setLength(0);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003926 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
Sudheer Shankaa87245d2017-08-10 12:02:31 -07003927 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003928 }
Sudheer Shankaa87245d2017-08-10 12:02:31 -07003929 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
3930 if (screenOffCpuFreqTimeMs != null) {
3931 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
3932 sb.append("," + screenOffCpuFreqTimeMs[i]);
3933 }
3934 } else {
3935 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
3936 sb.append(",0");
3937 }
3938 }
3939 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
3940 cpuFreqTimeMs.length, sb.toString());
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003941 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003942 }
3943
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003944 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3945 = u.getProcessStats();
3946 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3947 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003948
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003949 final long userMillis = ps.getUserTime(which);
3950 final long systemMillis = ps.getSystemTime(which);
3951 final long foregroundMillis = ps.getForegroundTime(which);
3952 final int starts = ps.getStarts(which);
3953 final int numCrashes = ps.getNumCrashes(which);
3954 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003955
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003956 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3957 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003958 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
3959 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003960 }
3961 }
3962
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003963 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3964 = u.getPackageStats();
3965 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3966 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3967 int wakeups = 0;
3968 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3969 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07003970 int count = alarms.valueAt(iwa).getCountLocked(which);
3971 wakeups += count;
3972 String name = alarms.keyAt(iwa).replace(',', '_');
3973 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003974 }
3975 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3976 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3977 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3978 final long startTime = ss.getStartTime(batteryUptime, which);
3979 final int starts = ss.getStarts(which);
3980 final int launches = ss.getLaunches(which);
3981 if (startTime != 0 || starts != 0 || launches != 0) {
3982 dumpLine(pw, uid, category, APK_DATA,
3983 wakeups, // wakeup alarms
3984 packageStats.keyAt(ipkg), // Apk
3985 serviceStats.keyAt(isvc), // service
3986 startTime / 1000, // time spent started, in ms
3987 starts,
3988 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003989 }
3990 }
3991 }
3992 }
3993 }
3994
Dianne Hackborn81038902012-11-26 17:04:09 -08003995 static final class TimerEntry {
3996 final String mName;
3997 final int mId;
3998 final BatteryStats.Timer mTimer;
3999 final long mTime;
4000 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
4001 mName = name;
4002 mId = id;
4003 mTimer = timer;
4004 mTime = time;
4005 }
4006 }
4007
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004008 private void printmAh(PrintWriter printer, double power) {
4009 printer.print(BatteryStatsHelper.makemAh(power));
4010 }
4011
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004012 private void printmAh(StringBuilder sb, double power) {
4013 sb.append(BatteryStatsHelper.makemAh(power));
4014 }
4015
Dianne Hackbornd953c532014-08-16 18:17:38 -07004016 /**
4017 * Temporary for settings.
4018 */
4019 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
4020 int reqUid) {
4021 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
4022 }
4023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004025 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07004026 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004027 final long rawUptime = SystemClock.uptimeMillis() * 1000;
4028 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07004029 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004030 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004031
4032 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
4033 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
4034 final long totalRealtime = computeRealtime(rawRealtime, which);
4035 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004036 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
4037 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
4038 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004039 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
4040 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -07004041 final long screenDozeTime = getScreenDozeTime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004042
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004043 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07004044
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004045 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07004046 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004047
Adam Lesinskif9b20a92016-06-17 17:30:01 -07004048 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
4049 if (estimatedBatteryCapacity > 0) {
4050 sb.setLength(0);
4051 sb.append(prefix);
4052 sb.append(" Estimated battery capacity: ");
4053 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
4054 sb.append(" mAh");
4055 pw.println(sb.toString());
4056 }
4057
Jocelyn Dangc627d102017-04-14 13:15:14 -07004058 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
4059 if (minLearnedBatteryCapacity > 0) {
4060 sb.setLength(0);
4061 sb.append(prefix);
4062 sb.append(" Min learned battery capacity: ");
4063 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
4064 sb.append(" mAh");
4065 pw.println(sb.toString());
4066 }
4067 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
4068 if (maxLearnedBatteryCapacity > 0) {
4069 sb.setLength(0);
4070 sb.append(prefix);
4071 sb.append(" Max learned battery capacity: ");
4072 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
4073 sb.append(" mAh");
4074 pw.println(sb.toString());
4075 }
4076
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004077 sb.setLength(0);
4078 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004079 sb.append(" Time on battery: ");
4080 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
4081 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
4082 sb.append(") realtime, ");
4083 formatTimeMs(sb, whichBatteryUptime / 1000);
4084 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, whichBatteryRealtime));
4085 sb.append(") uptime");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004086 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004087
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004088 sb.setLength(0);
4089 sb.append(prefix);
Mike Mac2f518a2017-09-19 16:06:03 -07004090 sb.append(" Time on battery screen off: ");
4091 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
4092 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, whichBatteryRealtime));
4093 sb.append(") realtime, ");
4094 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
4095 sb.append("(");
4096 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, whichBatteryRealtime));
4097 sb.append(") uptime");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004098 pw.println(sb.toString());
Mike Mac2f518a2017-09-19 16:06:03 -07004099
4100 sb.setLength(0);
4101 sb.append(prefix);
4102 sb.append(" Time on battery screen doze: ");
4103 formatTimeMs(sb, screenDozeTime / 1000); sb.append("(");
4104 sb.append(formatRatioLocked(screenDozeTime, whichBatteryRealtime));
4105 sb.append(")");
4106 pw.println(sb.toString());
4107
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004108 sb.setLength(0);
4109 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004110 sb.append(" Total run time: ");
4111 formatTimeMs(sb, totalRealtime / 1000);
4112 sb.append("realtime, ");
4113 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004114 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07004115 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004116 if (batteryTimeRemaining >= 0) {
4117 sb.setLength(0);
4118 sb.append(prefix);
4119 sb.append(" Battery time remaining: ");
4120 formatTimeMs(sb, batteryTimeRemaining / 1000);
4121 pw.println(sb.toString());
4122 }
4123 if (chargeTimeRemaining >= 0) {
4124 sb.setLength(0);
4125 sb.append(prefix);
4126 sb.append(" Charge time remaining: ");
4127 formatTimeMs(sb, chargeTimeRemaining / 1000);
4128 pw.println(sb.toString());
4129 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004130
Kweku Adams87b19ec2017-10-09 12:40:03 -07004131 final long dischargeCount = getUahDischarge(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004132 if (dischargeCount >= 0) {
4133 sb.setLength(0);
4134 sb.append(prefix);
4135 sb.append(" Discharge: ");
4136 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
4137 sb.append(" mAh");
4138 pw.println(sb.toString());
4139 }
4140
Kweku Adams87b19ec2017-10-09 12:40:03 -07004141 final long dischargeScreenOffCount = getUahDischargeScreenOff(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004142 if (dischargeScreenOffCount >= 0) {
4143 sb.setLength(0);
4144 sb.append(prefix);
4145 sb.append(" Screen off discharge: ");
4146 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
4147 sb.append(" mAh");
4148 pw.println(sb.toString());
4149 }
4150
Kweku Adams87b19ec2017-10-09 12:40:03 -07004151 final long dischargeScreenDozeCount = getUahDischargeScreenDoze(which);
Mike Mac2f518a2017-09-19 16:06:03 -07004152 if (dischargeScreenDozeCount >= 0) {
4153 sb.setLength(0);
4154 sb.append(prefix);
4155 sb.append(" Screen doze discharge: ");
4156 sb.append(BatteryStatsHelper.makemAh(dischargeScreenDozeCount / 1000.0));
4157 sb.append(" mAh");
4158 pw.println(sb.toString());
4159 }
4160
4161 final long dischargeScreenOnCount =
4162 dischargeCount - dischargeScreenOffCount - dischargeScreenDozeCount;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07004163 if (dischargeScreenOnCount >= 0) {
4164 sb.setLength(0);
4165 sb.append(prefix);
4166 sb.append(" Screen on discharge: ");
4167 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
4168 sb.append(" mAh");
4169 pw.println(sb.toString());
4170 }
4171
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004172 pw.print(" Start clock time: ");
4173 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
4174
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004175 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004176 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004177 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004178 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
4179 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004180 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004181 rawRealtime, which);
4182 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
4183 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004184 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004185 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004186 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
4187 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
4188 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004189 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004190 sb.append(prefix);
4191 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
4192 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004193 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004194 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
4195 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004196 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004197 pw.println(sb.toString());
4198 sb.setLength(0);
4199 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004200 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004201 boolean didOne = false;
4202 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004203 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004204 if (time == 0) {
4205 continue;
4206 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004207 sb.append("\n ");
4208 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004209 didOne = true;
4210 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
4211 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004212 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004213 sb.append("(");
4214 sb.append(formatRatioLocked(time, screenOnTime));
4215 sb.append(")");
4216 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004217 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004218 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004219 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004220 sb.setLength(0);
4221 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004222 sb.append(" Power save mode enabled: ");
4223 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004224 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004225 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004226 sb.append(")");
4227 pw.println(sb.toString());
4228 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004229 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004230 sb.setLength(0);
4231 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004232 sb.append(" Device light idling: ");
4233 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004234 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004235 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
4236 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004237 sb.append("x");
4238 pw.println(sb.toString());
4239 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004240 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004241 sb.setLength(0);
4242 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004243 sb.append(" Idle mode light time: ");
4244 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004245 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004246 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
4247 sb.append(") ");
4248 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004249 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004250 sb.append(" -- longest ");
4251 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
4252 pw.println(sb.toString());
4253 }
4254 if (deviceIdlingTime != 0) {
4255 sb.setLength(0);
4256 sb.append(prefix);
4257 sb.append(" Device full idling: ");
4258 formatTimeMs(sb, deviceIdlingTime / 1000);
4259 sb.append("(");
4260 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004261 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004262 sb.append("x");
4263 pw.println(sb.toString());
4264 }
4265 if (deviceIdleModeFullTime != 0) {
4266 sb.setLength(0);
4267 sb.append(prefix);
4268 sb.append(" Idle mode full time: ");
4269 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
4270 sb.append("(");
4271 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
4272 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004273 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004274 sb.append("x");
4275 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004276 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004277 pw.println(sb.toString());
4278 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004279 if (phoneOnTime != 0) {
4280 sb.setLength(0);
4281 sb.append(prefix);
4282 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
4283 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004284 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004285 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004286 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004287 if (connChanges != 0) {
4288 pw.print(prefix);
4289 pw.print(" Connectivity changes: "); pw.println(connChanges);
4290 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004291
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004292 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07004293 long fullWakeLockTimeTotalMicros = 0;
4294 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08004295
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004296 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08004297
Evan Millar22ac0432009-03-31 11:33:18 -07004298 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004299 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004300
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004301 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4302 = u.getWakelockStats();
4303 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4304 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07004305
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004306 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
4307 if (fullWakeTimer != null) {
4308 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
4309 rawRealtime, which);
4310 }
4311
4312 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4313 if (partialWakeTimer != null) {
4314 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
4315 rawRealtime, which);
4316 if (totalTimeMicros > 0) {
4317 if (reqUid < 0) {
4318 // Only show the ordered list of all wake
4319 // locks if the caller is not asking for data
4320 // about a specific uid.
4321 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
4322 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08004323 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004324 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07004325 }
4326 }
4327 }
4328 }
Bookatzc8c44962017-05-11 12:12:54 -07004329
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004330 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4331 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4332 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4333 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4334 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4335 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4336 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4337 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004338 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4339 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004340
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004341 if (fullWakeLockTimeTotalMicros != 0) {
4342 sb.setLength(0);
4343 sb.append(prefix);
4344 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4345 (fullWakeLockTimeTotalMicros + 500) / 1000);
4346 pw.println(sb.toString());
4347 }
4348
4349 if (partialWakeLockTimeTotalMicros != 0) {
4350 sb.setLength(0);
4351 sb.append(prefix);
4352 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4353 (partialWakeLockTimeTotalMicros + 500) / 1000);
4354 pw.println(sb.toString());
4355 }
4356
Siddharth Ray3c648c42017-10-02 17:30:58 -07004357 pw.println("");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004358 pw.print(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004359 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004360 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004361 sb.append(" CONNECTIVITY POWER SUMMARY START");
4362 pw.println(sb.toString());
4363
4364 pw.print(prefix);
4365 sb.setLength(0);
4366 sb.append(prefix);
4367 sb.append(" Logging duration for connectivity statistics: ");
4368 formatTimeMs(sb, whichBatteryRealtime / 1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004369 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004370
4371 sb.setLength(0);
4372 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004373 sb.append(" Cellular Statistics:");
Amith Yamasanif37447b2009-10-08 18:28:01 -07004374 pw.println(sb.toString());
4375
Siddharth Ray3c648c42017-10-02 17:30:58 -07004376 pw.print(prefix);
4377 sb.setLength(0);
4378 sb.append(prefix);
4379 sb.append(" Cellular kernel active time: ");
4380 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
4381 formatTimeMs(sb, mobileActiveTime / 1000);
4382 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4383 sb.append(")");
4384 pw.println(sb.toString());
4385
4386 pw.print(" Cellular data received: "); pw.println(formatBytesLocked(mobileRxTotalBytes));
4387 pw.print(" Cellular data sent: "); pw.println(formatBytesLocked(mobileTxTotalBytes));
4388 pw.print(" Cellular packets received: "); pw.println(mobileRxTotalPackets);
4389 pw.print(" Cellular packets sent: "); pw.println(mobileTxTotalPackets);
4390
Dianne Hackborn627bba72009-03-24 22:32:56 -07004391 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004392 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004393 sb.append(" Cellular Radio Access Technology:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004394 didOne = false;
4395 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004396 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004397 if (time == 0) {
4398 continue;
4399 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004400 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004401 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004402 didOne = true;
4403 sb.append(DATA_CONNECTION_NAMES[i]);
4404 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004405 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004406 sb.append("(");
4407 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004408 sb.append(") ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004409 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004410 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004411 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004412
4413 sb.setLength(0);
4414 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004415 sb.append(" Cellular Rx signal strength (RSRP):");
4416 final String[] cellularRxSignalStrengthDescription = new String[]{
4417 "very poor (less than -128dBm): ",
4418 "poor (-128dBm to -118dBm): ",
4419 "moderate (-118dBm to -108dBm): ",
4420 "good (-108dBm to -98dBm): ",
4421 "great (greater than -98dBm): "};
4422 didOne = false;
4423 final int numCellularRxBins = Math.min(SignalStrength.NUM_SIGNAL_STRENGTH_BINS,
4424 cellularRxSignalStrengthDescription.length);
4425 for (int i=0; i<numCellularRxBins; i++) {
4426 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
4427 if (time == 0) {
4428 continue;
4429 }
4430 sb.append("\n ");
4431 sb.append(prefix);
4432 didOne = true;
4433 sb.append(cellularRxSignalStrengthDescription[i]);
4434 sb.append(" ");
4435 formatTimeMs(sb, time/1000);
4436 sb.append("(");
4437 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4438 sb.append(") ");
4439 }
4440 if (!didOne) sb.append(" (no activity)");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004441 pw.println(sb.toString());
4442
Siddharth Ray3c648c42017-10-02 17:30:58 -07004443 printControllerActivity(pw, sb, prefix, "Cellular",
4444 getModemControllerActivity(), which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004445
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004446 pw.print(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004447 sb.setLength(0);
4448 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004449 sb.append(" Wifi Statistics:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004450 pw.println(sb.toString());
4451
Siddharth Ray3c648c42017-10-02 17:30:58 -07004452 pw.print(" Wifi data received: "); pw.println(formatBytesLocked(wifiRxTotalBytes));
4453 pw.print(" Wifi data sent: "); pw.println(formatBytesLocked(wifiTxTotalBytes));
4454 pw.print(" Wifi packets received: "); pw.println(wifiRxTotalPackets);
4455 pw.print(" Wifi packets sent: "); pw.println(wifiTxTotalPackets);
4456
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004457 sb.setLength(0);
4458 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004459 sb.append(" Wifi states:");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004460 didOne = false;
4461 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004462 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004463 if (time == 0) {
4464 continue;
4465 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004466 sb.append("\n ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004467 didOne = true;
4468 sb.append(WIFI_STATE_NAMES[i]);
4469 sb.append(" ");
4470 formatTimeMs(sb, time/1000);
4471 sb.append("(");
4472 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4473 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004474 }
4475 if (!didOne) sb.append(" (no activity)");
4476 pw.println(sb.toString());
4477
4478 sb.setLength(0);
4479 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004480 sb.append(" Wifi supplicant states:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004481 didOne = false;
4482 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4483 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4484 if (time == 0) {
4485 continue;
4486 }
Siddharth Ray3c648c42017-10-02 17:30:58 -07004487 sb.append("\n ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004488 didOne = true;
4489 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4490 sb.append(" ");
4491 formatTimeMs(sb, time/1000);
4492 sb.append("(");
4493 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4494 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004495 }
4496 if (!didOne) sb.append(" (no activity)");
4497 pw.println(sb.toString());
4498
4499 sb.setLength(0);
4500 sb.append(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004501 sb.append(" Wifi Rx signal strength (RSSI):");
4502 final String[] wifiRxSignalStrengthDescription = new String[]{
4503 "very poor (less than -88.75dBm): ",
4504 "poor (-88.75 to -77.5dBm): ",
4505 "moderate (-77.5dBm to -66.25dBm): ",
4506 "good (-66.25dBm to -55dBm): ",
4507 "great (greater than -55dBm): "};
Dianne Hackborn3251b902014-06-20 14:40:53 -07004508 didOne = false;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004509 final int numWifiRxBins = Math.min(NUM_WIFI_SIGNAL_STRENGTH_BINS,
4510 wifiRxSignalStrengthDescription.length);
4511 for (int i=0; i<numWifiRxBins; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004512 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4513 if (time == 0) {
4514 continue;
4515 }
4516 sb.append("\n ");
4517 sb.append(prefix);
4518 didOne = true;
Siddharth Ray3c648c42017-10-02 17:30:58 -07004519 sb.append(" ");
4520 sb.append(wifiRxSignalStrengthDescription[i]);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004521 formatTimeMs(sb, time/1000);
4522 sb.append("(");
4523 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4524 sb.append(") ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004525 }
4526 if (!didOne) sb.append(" (no activity)");
4527 pw.println(sb.toString());
4528
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004529 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004530
Adam Lesinski50e47602015-12-04 17:04:54 -08004531 pw.print(prefix);
Siddharth Ray3c648c42017-10-02 17:30:58 -07004532 sb.setLength(0);
4533 sb.append(prefix);
4534 sb.append(" CONNECTIVITY POWER SUMMARY END");
4535 pw.println(sb.toString());
4536 pw.println("");
4537
4538 pw.print(prefix);
Adam Lesinski50e47602015-12-04 17:04:54 -08004539 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4540 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4541
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004542 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4543 sb.setLength(0);
4544 sb.append(prefix);
4545 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4546 pw.println(sb.toString());
4547
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004548 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4549 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004550
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004551 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004552
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004553 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004554 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004555 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004556 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004557 pw.println(getDischargeStartLevel());
4558 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4559 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004560 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004561 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004562 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004563 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004564 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004565 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004566 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004567 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004568 pw.println(getDischargeAmountScreenOn());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004569 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004570 pw.println(getDischargeAmountScreenOff());
4571 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4572 pw.println(getDischargeAmountScreenDoze());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004573 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004574 } else {
4575 pw.print(prefix); pw.println(" Device battery use since last full charge");
4576 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004577 pw.println(getLowDischargeAmountSinceCharge());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004578 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
Mike Mac2f518a2017-09-19 16:06:03 -07004579 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004580 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004581 pw.println(getDischargeAmountScreenOnSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004582 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
Mike Mac2f518a2017-09-19 16:06:03 -07004583 pw.println(getDischargeAmountScreenOffSinceCharge());
4584 pw.print(prefix); pw.print(" Amount discharged while screen doze: ");
4585 pw.println(getDischargeAmountScreenDozeSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004586 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004587 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004588
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004589 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004590 helper.create(this);
4591 helper.refreshStats(which, UserHandle.USER_ALL);
4592 List<BatterySipper> sippers = helper.getUsageList();
4593 if (sippers != null && sippers.size() > 0) {
4594 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4595 pw.print(prefix); pw.print(" Capacity: ");
4596 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004597 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004598 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4599 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4600 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4601 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004602 pw.println();
4603 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004604 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004605 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004606 switch (bs.drainType) {
4607 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004608 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004609 break;
4610 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004611 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004612 break;
4613 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004614 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004615 break;
4616 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004617 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004618 break;
4619 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004620 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004621 break;
4622 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004623 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004624 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004625 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004626 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004627 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004628 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004629 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004630 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004631 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004632 break;
4633 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004634 pw.print(" User "); pw.print(bs.userId);
4635 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004636 break;
4637 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004638 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004639 break;
4640 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004641 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004642 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004643 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004644 pw.print(" Camera: ");
4645 break;
4646 default:
4647 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004648 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004649 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004650 printmAh(pw, bs.totalPowerMah);
4651
Adam Lesinski57123002015-06-12 16:12:07 -07004652 if (bs.usagePowerMah != bs.totalPowerMah) {
4653 // If the usage (generic power) isn't the whole amount, we list out
4654 // what components are involved in the calculation.
4655
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004656 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004657 if (bs.usagePowerMah != 0) {
4658 pw.print(" usage=");
4659 printmAh(pw, bs.usagePowerMah);
4660 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004661 if (bs.cpuPowerMah != 0) {
4662 pw.print(" cpu=");
4663 printmAh(pw, bs.cpuPowerMah);
4664 }
4665 if (bs.wakeLockPowerMah != 0) {
4666 pw.print(" wake=");
4667 printmAh(pw, bs.wakeLockPowerMah);
4668 }
4669 if (bs.mobileRadioPowerMah != 0) {
4670 pw.print(" radio=");
4671 printmAh(pw, bs.mobileRadioPowerMah);
4672 }
4673 if (bs.wifiPowerMah != 0) {
4674 pw.print(" wifi=");
4675 printmAh(pw, bs.wifiPowerMah);
4676 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004677 if (bs.bluetoothPowerMah != 0) {
4678 pw.print(" bt=");
4679 printmAh(pw, bs.bluetoothPowerMah);
4680 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004681 if (bs.gpsPowerMah != 0) {
4682 pw.print(" gps=");
4683 printmAh(pw, bs.gpsPowerMah);
4684 }
4685 if (bs.sensorPowerMah != 0) {
4686 pw.print(" sensor=");
4687 printmAh(pw, bs.sensorPowerMah);
4688 }
4689 if (bs.cameraPowerMah != 0) {
4690 pw.print(" camera=");
4691 printmAh(pw, bs.cameraPowerMah);
4692 }
4693 if (bs.flashlightPowerMah != 0) {
4694 pw.print(" flash=");
4695 printmAh(pw, bs.flashlightPowerMah);
4696 }
4697 pw.print(" )");
4698 }
Bookatz17d7d9d2017-06-08 14:50:46 -07004699
4700 // If there is additional smearing information, include it.
4701 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
4702 pw.print(" Including smearing: ");
4703 printmAh(pw, bs.totalSmearedPowerMah);
4704 pw.print(" (");
4705 if (bs.screenPowerMah != 0) {
4706 pw.print(" screen=");
4707 printmAh(pw, bs.screenPowerMah);
4708 }
4709 if (bs.proportionalSmearMah != 0) {
4710 pw.print(" proportional=");
4711 printmAh(pw, bs.proportionalSmearMah);
4712 }
4713 pw.print(" )");
4714 }
4715 if (bs.shouldHide) {
4716 pw.print(" Excluded from smearing");
4717 }
4718
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004719 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004720 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004721 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004722 }
4723
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004724 sippers = helper.getMobilemsppList();
4725 if (sippers != null && sippers.size() > 0) {
4726 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004727 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004728 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004729 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004730 sb.setLength(0);
4731 sb.append(prefix); sb.append(" Uid ");
4732 UserHandle.formatUid(sb, bs.uidObj.getUid());
4733 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4734 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4735 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004736 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004737 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004738 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004739 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004740 sb.setLength(0);
4741 sb.append(prefix);
4742 sb.append(" TOTAL TIME: ");
4743 formatTimeMs(sb, totalTime);
4744 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4745 sb.append(")");
4746 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004747 pw.println();
4748 }
4749
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004750 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4751 @Override
4752 public int compare(TimerEntry lhs, TimerEntry rhs) {
4753 long lhsTime = lhs.mTime;
4754 long rhsTime = rhs.mTime;
4755 if (lhsTime < rhsTime) {
4756 return 1;
4757 }
4758 if (lhsTime > rhsTime) {
4759 return -1;
4760 }
4761 return 0;
4762 }
4763 };
4764
4765 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004766 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4767 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004768 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004769 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4770 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4771 : kernelWakelocks.entrySet()) {
4772 final BatteryStats.Timer timer = ent.getValue();
4773 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004774 if (totalTimeMillis > 0) {
4775 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4776 }
4777 }
4778 if (ktimers.size() > 0) {
4779 Collections.sort(ktimers, timerComparator);
4780 pw.print(prefix); pw.println(" All kernel wake locks:");
4781 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004782 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004783 String linePrefix = ": ";
4784 sb.setLength(0);
4785 sb.append(prefix);
4786 sb.append(" Kernel Wake lock ");
4787 sb.append(timer.mName);
4788 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4789 which, linePrefix);
4790 if (!linePrefix.equals(": ")) {
4791 sb.append(" realtime");
4792 // Only print out wake locks that were held
4793 pw.println(sb.toString());
4794 }
4795 }
4796 pw.println();
4797 }
4798 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004799
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004800 if (timers.size() > 0) {
4801 Collections.sort(timers, timerComparator);
4802 pw.print(prefix); pw.println(" All partial wake locks:");
4803 for (int i=0; i<timers.size(); i++) {
4804 TimerEntry timer = timers.get(i);
4805 sb.setLength(0);
4806 sb.append(" Wake lock ");
4807 UserHandle.formatUid(sb, timer.mId);
4808 sb.append(" ");
4809 sb.append(timer.mName);
4810 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4811 sb.append(" realtime");
4812 pw.println(sb.toString());
4813 }
4814 timers.clear();
4815 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08004816 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004817
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004818 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004819 if (wakeupReasons.size() > 0) {
4820 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004821 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004822 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004823 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004824 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
4825 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004826 }
4827 Collections.sort(reasons, timerComparator);
4828 for (int i=0; i<reasons.size(); i++) {
4829 TimerEntry timer = reasons.get(i);
4830 String linePrefix = ": ";
4831 sb.setLength(0);
4832 sb.append(prefix);
4833 sb.append(" Wakeup reason ");
4834 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004835 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4836 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004837 pw.println(sb.toString());
4838 }
4839 pw.println();
4840 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004841 }
Evan Millar22ac0432009-03-31 11:33:18 -07004842
James Carr2dd7e5e2016-07-20 18:48:39 -07004843 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
Bookatz50df7112017-08-04 14:53:26 -07004844 if (mMemoryStats.size() > 0) {
4845 pw.println(" Memory Stats");
4846 for (int i = 0; i < mMemoryStats.size(); i++) {
4847 sb.setLength(0);
4848 sb.append(" Bandwidth ");
4849 sb.append(mMemoryStats.keyAt(i));
4850 sb.append(" Time ");
4851 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
4852 pw.println(sb.toString());
4853 }
4854 pw.println();
4855 }
4856
4857 final Map<String, ? extends Timer> rpmStats = getRpmStats();
4858 if (rpmStats.size() > 0) {
4859 pw.print(prefix); pw.println(" Resource Power Manager Stats");
4860 if (rpmStats.size() > 0) {
4861 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
4862 final String timerName = ent.getKey();
4863 final Timer timer = ent.getValue();
4864 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
4865 }
4866 }
4867 pw.println();
4868 }
Bookatz82b341172017-09-07 19:06:08 -07004869 if (SCREEN_OFF_RPM_STATS_ENABLED) {
4870 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
Bookatz50df7112017-08-04 14:53:26 -07004871 if (screenOffRpmStats.size() > 0) {
Bookatz82b341172017-09-07 19:06:08 -07004872 pw.print(prefix);
4873 pw.println(" Resource Power Manager Stats for when screen was off");
4874 if (screenOffRpmStats.size() > 0) {
4875 for (Map.Entry<String, ? extends Timer> ent : screenOffRpmStats.entrySet()) {
4876 final String timerName = ent.getKey();
4877 final Timer timer = ent.getValue();
4878 printTimer(pw, sb, timer, rawRealtime, which, prefix, timerName);
4879 }
Bookatz50df7112017-08-04 14:53:26 -07004880 }
Bookatz82b341172017-09-07 19:06:08 -07004881 pw.println();
Bookatz50df7112017-08-04 14:53:26 -07004882 }
James Carr2dd7e5e2016-07-20 18:48:39 -07004883 }
4884
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004885 final long[] cpuFreqs = getCpuFreqs();
4886 if (cpuFreqs != null) {
4887 sb.setLength(0);
Bookatz50df7112017-08-04 14:53:26 -07004888 sb.append(" CPU freqs:");
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004889 for (int i = 0; i < cpuFreqs.length; ++i) {
4890 sb.append(" " + cpuFreqs[i]);
4891 }
4892 pw.println(sb.toString());
Bookatz50df7112017-08-04 14:53:26 -07004893 pw.println();
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004894 }
4895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004896 for (int iu=0; iu<NU; iu++) {
4897 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004898 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004899 continue;
4900 }
Bookatzc8c44962017-05-11 12:12:54 -07004901
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004902 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07004903
4904 pw.print(prefix);
4905 pw.print(" ");
4906 UserHandle.formatUid(pw, uid);
4907 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004908 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004909
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004910 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4911 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4912 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4913 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004914 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4915 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
4916
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004917 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4918 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004919 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4920 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004921
4922 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
4923 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
4924
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004925 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4926 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4927 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08004928 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
4929 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4930 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
4931 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004932 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004933
Adam Lesinski5f056f62016-07-14 16:56:08 -07004934 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
4935 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
4936
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004937 if (mobileRxBytes > 0 || mobileTxBytes > 0
4938 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004939 pw.print(prefix); pw.print(" Mobile network: ");
4940 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004941 pw.print(formatBytesLocked(mobileTxBytes));
4942 pw.print(" sent (packets "); pw.print(mobileRxPackets);
4943 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004944 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004945 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
4946 sb.setLength(0);
4947 sb.append(prefix); sb.append(" Mobile radio active: ");
4948 formatTimeMs(sb, uidMobileActiveTime / 1000);
4949 sb.append("(");
4950 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
4951 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
4952 long packets = mobileRxPackets + mobileTxPackets;
4953 if (packets == 0) {
4954 packets = 1;
4955 }
4956 sb.append(" @ ");
4957 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
4958 sb.append(" mspp");
4959 pw.println(sb.toString());
4960 }
4961
Adam Lesinski5f056f62016-07-14 16:56:08 -07004962 if (mobileWakeup > 0) {
4963 sb.setLength(0);
4964 sb.append(prefix);
4965 sb.append(" Mobile radio AP wakeups: ");
4966 sb.append(mobileWakeup);
4967 pw.println(sb.toString());
4968 }
4969
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004970 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
4971 u.getModemControllerActivity(), which);
4972
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004973 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004974 pw.print(prefix); pw.print(" Wi-Fi network: ");
4975 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004976 pw.print(formatBytesLocked(wifiTxBytes));
4977 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4978 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004979 }
4980
Dianne Hackborn62793e42015-03-09 11:15:41 -07004981 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08004982 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004983 || uidWifiRunningTime != 0) {
4984 sb.setLength(0);
4985 sb.append(prefix); sb.append(" Wifi Running: ");
4986 formatTimeMs(sb, uidWifiRunningTime / 1000);
4987 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4988 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07004989 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004990 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4991 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4992 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08004993 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004994 formatTimeMs(sb, wifiScanTime / 1000);
4995 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004996 whichBatteryRealtime)); sb.append(") ");
4997 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08004998 sb.append("x\n");
4999 // actual and background times are unpooled and since reset (regardless of 'which')
5000 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
5001 formatTimeMs(sb, wifiScanActualTime / 1000);
5002 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
5003 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5004 sb.append(") ");
5005 sb.append(wifiScanCount);
5006 sb.append("x\n");
5007 sb.append(prefix); sb.append(" Background Wifi Scan: ");
5008 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
5009 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
5010 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
5011 sb.append(") ");
5012 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07005013 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005014 pw.println(sb.toString());
5015 }
5016
Adam Lesinski5f056f62016-07-14 16:56:08 -07005017 if (wifiWakeup > 0) {
5018 sb.setLength(0);
5019 sb.append(prefix);
5020 sb.append(" WiFi AP wakeups: ");
5021 sb.append(wifiWakeup);
5022 pw.println(sb.toString());
5023 }
5024
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005025 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
5026 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07005027
Adam Lesinski50e47602015-12-04 17:04:54 -08005028 if (btRxBytes > 0 || btTxBytes > 0) {
5029 pw.print(prefix); pw.print(" Bluetooth network: ");
5030 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
5031 pw.print(formatBytesLocked(btTxBytes));
5032 pw.println(" sent");
5033 }
5034
Bookatz867c0d72017-03-07 18:23:42 -08005035 final Timer bleTimer = u.getBluetoothScanTimer();
5036 if (bleTimer != null) {
5037 // Convert from microseconds to milliseconds with rounding
5038 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
5039 / 1000;
5040 if (totalTimeMs != 0) {
5041 final int count = bleTimer.getCountLocked(which);
5042 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
5043 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005044 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5045 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
5046 final long actualTimeMsBg = bleTimerBg != null ?
5047 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005048 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07005049 final int resultCount = u.getBluetoothScanResultCounter() != null ?
5050 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07005051 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
5052 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
5053 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
5054 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
5055 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
5056 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5057 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
5058 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
5059 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
5060 final Timer unoptimizedScanTimerBg =
5061 u.getBluetoothUnoptimizedScanBackgroundTimer();
5062 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
5063 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5064 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
5065 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005066
5067 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08005068 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07005069 sb.append(prefix);
5070 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08005071 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07005072 sb.append(" (");
5073 sb.append(count);
5074 sb.append(" times)");
5075 if (bleTimer.isRunningLocked()) {
5076 sb.append(" (currently running)");
5077 }
5078 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005079 }
Bookatzb1f04f32017-05-19 13:57:32 -07005080
5081 sb.append(prefix);
5082 sb.append(" Bluetooth Scan (total actual realtime): ");
5083 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
5084 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005085 sb.append(count);
5086 sb.append(" times)");
5087 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07005088 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08005089 }
Bookatzb1f04f32017-05-19 13:57:32 -07005090 sb.append("\n");
5091 if (actualTimeMsBg > 0 || countBg > 0) {
5092 sb.append(prefix);
5093 sb.append(" Bluetooth Scan (background realtime): ");
5094 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
5095 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08005096 sb.append(countBg);
5097 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07005098 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
5099 sb.append(" (currently running in background)");
5100 }
5101 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08005102 }
Bookatzb1f04f32017-05-19 13:57:32 -07005103
5104 sb.append(prefix);
5105 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07005106 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07005107 sb.append(" (");
5108 sb.append(resultCountBg);
5109 sb.append(" in background)");
5110
5111 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
5112 sb.append("\n");
5113 sb.append(prefix);
5114 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
5115 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
5116 sb.append(" (max ");
5117 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
5118 sb.append(")");
5119 if (unoptimizedScanTimer != null
5120 && unoptimizedScanTimer.isRunningLocked()) {
5121 sb.append(" (currently running unoptimized)");
5122 }
5123 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
5124 sb.append("\n");
5125 sb.append(prefix);
5126 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
5127 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
5128 sb.append(" (max ");
5129 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
5130 sb.append(")");
5131 if (unoptimizedScanTimerBg.isRunningLocked()) {
5132 sb.append(" (currently running unoptimized in background)");
5133 }
5134 }
5135 }
Bookatz867c0d72017-03-07 18:23:42 -08005136 pw.println(sb.toString());
5137 uidActivity = true;
5138 }
5139 }
5140
5141
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005142
Dianne Hackborn617f8772009-03-31 15:04:46 -07005143 if (u.hasUserActivity()) {
5144 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07005145 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005146 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005147 if (val != 0) {
5148 if (!hasData) {
5149 sb.setLength(0);
5150 sb.append(" User activity: ");
5151 hasData = true;
5152 } else {
5153 sb.append(", ");
5154 }
5155 sb.append(val);
5156 sb.append(" ");
5157 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
5158 }
5159 }
5160 if (hasData) {
5161 pw.println(sb.toString());
5162 }
5163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005164
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005165 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
5166 = u.getWakelockStats();
5167 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005168 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005169 int countWakelock = 0;
5170 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
5171 final Uid.Wakelock wl = wakelocks.valueAt(iw);
5172 String linePrefix = ": ";
5173 sb.setLength(0);
5174 sb.append(prefix);
5175 sb.append(" Wake lock ");
5176 sb.append(wakelocks.keyAt(iw));
5177 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
5178 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005179 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
5180 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005181 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07005182 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
5183 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005184 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
5185 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005186 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
5187 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07005188 sb.append(" realtime");
5189 pw.println(sb.toString());
5190 uidActivity = true;
5191 countWakelock++;
5192
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005193 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
5194 rawRealtime, which);
5195 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
5196 rawRealtime, which);
5197 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
5198 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005199 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07005200 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005201 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005202 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07005203 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
5204 // pooled and therefore just a lower bound)
5205 long actualTotalPartialWakelock = 0;
5206 long actualBgPartialWakelock = 0;
5207 if (u.getAggregatedPartialWakelockTimer() != null) {
5208 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
5209 // Convert from microseconds to milliseconds with rounding
5210 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07005211 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07005212 final Timer bgAggTimer = aggTimer.getSubTimer();
5213 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005214 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07005215 }
5216
5217 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
5218 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
5219 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005220 sb.setLength(0);
5221 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005222 sb.append(" TOTAL wake: ");
5223 boolean needComma = false;
5224 if (totalFullWakelock != 0) {
5225 needComma = true;
5226 formatTimeMs(sb, totalFullWakelock);
5227 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005228 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005229 if (totalPartialWakelock != 0) {
5230 if (needComma) {
5231 sb.append(", ");
5232 }
5233 needComma = true;
5234 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07005235 sb.append("blamed partial");
5236 }
5237 if (actualTotalPartialWakelock != 0) {
5238 if (needComma) {
5239 sb.append(", ");
5240 }
5241 needComma = true;
5242 formatTimeMs(sb, actualTotalPartialWakelock);
5243 sb.append("actual partial");
5244 }
5245 if (actualBgPartialWakelock != 0) {
5246 if (needComma) {
5247 sb.append(", ");
5248 }
5249 needComma = true;
5250 formatTimeMs(sb, actualBgPartialWakelock);
5251 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005252 }
5253 if (totalWindowWakelock != 0) {
5254 if (needComma) {
5255 sb.append(", ");
5256 }
5257 needComma = true;
5258 formatTimeMs(sb, totalWindowWakelock);
5259 sb.append("window");
5260 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005261 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07005262 if (needComma) {
5263 sb.append(",");
5264 }
5265 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07005266 formatTimeMs(sb, totalDrawWakelock);
5267 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07005268 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005269 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005270 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005271 }
5272 }
5273
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005274 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
5275 for (int isy=syncs.size()-1; isy>=0; isy--) {
5276 final Timer timer = syncs.valueAt(isy);
5277 // Convert from microseconds to milliseconds with rounding
5278 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5279 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07005280 final Timer bgTimer = timer.getSubTimer();
5281 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005282 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07005283 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005284 sb.setLength(0);
5285 sb.append(prefix);
5286 sb.append(" Sync ");
5287 sb.append(syncs.keyAt(isy));
5288 sb.append(": ");
5289 if (totalTime != 0) {
5290 formatTimeMs(sb, totalTime);
5291 sb.append("realtime (");
5292 sb.append(count);
5293 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07005294 if (bgTime > 0) {
5295 sb.append(", ");
5296 formatTimeMs(sb, bgTime);
5297 sb.append("background (");
5298 sb.append(bgCount);
5299 sb.append(" times)");
5300 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005301 } else {
5302 sb.append("(not used)");
5303 }
5304 pw.println(sb.toString());
5305 uidActivity = true;
5306 }
5307
5308 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
5309 for (int ij=jobs.size()-1; ij>=0; ij--) {
5310 final Timer timer = jobs.valueAt(ij);
5311 // Convert from microseconds to milliseconds with rounding
5312 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
5313 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07005314 final Timer bgTimer = timer.getSubTimer();
5315 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07005316 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07005317 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005318 sb.setLength(0);
5319 sb.append(prefix);
5320 sb.append(" Job ");
5321 sb.append(jobs.keyAt(ij));
5322 sb.append(": ");
5323 if (totalTime != 0) {
5324 formatTimeMs(sb, totalTime);
5325 sb.append("realtime (");
5326 sb.append(count);
5327 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07005328 if (bgTime > 0) {
5329 sb.append(", ");
5330 formatTimeMs(sb, bgTime);
5331 sb.append("background (");
5332 sb.append(bgCount);
5333 sb.append(" times)");
5334 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005335 } else {
5336 sb.append("(not used)");
5337 }
5338 pw.println(sb.toString());
5339 uidActivity = true;
5340 }
5341
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005342 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
5343 for (int ic=completions.size()-1; ic>=0; ic--) {
5344 SparseIntArray types = completions.valueAt(ic);
5345 if (types != null) {
5346 pw.print(prefix);
5347 pw.print(" Job Completions ");
5348 pw.print(completions.keyAt(ic));
5349 pw.print(":");
5350 for (int it=0; it<types.size(); it++) {
5351 pw.print(" ");
5352 pw.print(JobParameters.getReasonName(types.keyAt(it)));
5353 pw.print("(");
5354 pw.print(types.valueAt(it));
5355 pw.print("x)");
5356 }
5357 pw.println();
5358 }
5359 }
5360
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005361 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
5362 prefix, "Flashlight");
5363 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
5364 prefix, "Camera");
5365 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
5366 prefix, "Video");
5367 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
5368 prefix, "Audio");
5369
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005370 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
5371 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005372 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005373 final Uid.Sensor se = sensors.valueAt(ise);
5374 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005375 sb.setLength(0);
5376 sb.append(prefix);
5377 sb.append(" Sensor ");
5378 int handle = se.getHandle();
5379 if (handle == Uid.Sensor.GPS) {
5380 sb.append("GPS");
5381 } else {
5382 sb.append(handle);
5383 }
5384 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005385
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005386 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005387 if (timer != null) {
5388 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005389 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5390 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005391 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005392 final Timer bgTimer = se.getSensorBackgroundTime();
5393 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005394 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5395 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5396 final long bgActualTime = bgTimer != null ?
5397 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5398
Dianne Hackborn61659e52014-07-09 16:13:01 -07005399 //timer.logState();
5400 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005401 if (actualTime != totalTime) {
5402 formatTimeMs(sb, totalTime);
5403 sb.append("blamed realtime, ");
5404 }
5405
5406 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005407 sb.append("realtime (");
5408 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005409 sb.append(" times)");
5410
5411 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005412 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005413 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5414 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005415 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005416 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005418 } else {
5419 sb.append("(not used)");
5420 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005421 } else {
5422 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005423 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005424
5425 pw.println(sb.toString());
5426 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005427 }
5428
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005429 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5430 "Vibrator");
5431 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5432 prefix, "Foreground activities");
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005433 uidActivity |= printTimer(pw, sb, u.getForegroundServiceTimer(), rawRealtime, which,
5434 prefix, "Foreground services");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005435
Dianne Hackborn61659e52014-07-09 16:13:01 -07005436 long totalStateTime = 0;
5437 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5438 long time = u.getProcessStateTime(ips, rawRealtime, which);
5439 if (time > 0) {
5440 totalStateTime += time;
5441 sb.setLength(0);
5442 sb.append(prefix);
5443 sb.append(" ");
5444 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5445 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005446 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005447 pw.println(sb.toString());
5448 uidActivity = true;
5449 }
5450 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005451 if (totalStateTime > 0) {
5452 sb.setLength(0);
5453 sb.append(prefix);
5454 sb.append(" Total running: ");
5455 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5456 pw.println(sb.toString());
5457 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005458
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005459 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5460 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005461 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005462 sb.setLength(0);
5463 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005464 sb.append(" Total cpu time: u=");
5465 formatTimeMs(sb, userCpuTimeUs / 1000);
5466 sb.append("s=");
5467 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005468 pw.println(sb.toString());
5469 }
5470
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005471 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5472 if (cpuFreqTimes != null) {
5473 sb.setLength(0);
5474 sb.append(" Total cpu time per freq:");
5475 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5476 sb.append(" " + cpuFreqTimes[i]);
5477 }
5478 pw.println(sb.toString());
5479 }
5480 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5481 if (screenOffCpuFreqTimes != null) {
5482 sb.setLength(0);
5483 sb.append(" Total screen-off cpu time per freq:");
5484 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5485 sb.append(" " + screenOffCpuFreqTimes[i]);
5486 }
5487 pw.println(sb.toString());
5488 }
5489
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005490 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5491 = u.getProcessStats();
5492 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5493 final Uid.Proc ps = processStats.valueAt(ipr);
5494 long userTime;
5495 long systemTime;
5496 long foregroundTime;
5497 int starts;
5498 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005499
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005500 userTime = ps.getUserTime(which);
5501 systemTime = ps.getSystemTime(which);
5502 foregroundTime = ps.getForegroundTime(which);
5503 starts = ps.getStarts(which);
5504 final int numCrashes = ps.getNumCrashes(which);
5505 final int numAnrs = ps.getNumAnrs(which);
5506 numExcessive = which == STATS_SINCE_CHARGED
5507 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005508
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005509 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5510 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5511 sb.setLength(0);
5512 sb.append(prefix); sb.append(" Proc ");
5513 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5514 sb.append(prefix); sb.append(" CPU: ");
5515 formatTimeMs(sb, userTime); sb.append("usr + ");
5516 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5517 formatTimeMs(sb, foregroundTime); sb.append("fg");
5518 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5519 sb.append("\n"); sb.append(prefix); sb.append(" ");
5520 boolean hasOne = false;
5521 if (starts != 0) {
5522 hasOne = true;
5523 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005524 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005525 if (numCrashes != 0) {
5526 if (hasOne) {
5527 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005528 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005529 hasOne = true;
5530 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005531 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005532 if (numAnrs != 0) {
5533 if (hasOne) {
5534 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005535 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005536 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005537 }
5538 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005539 pw.println(sb.toString());
5540 for (int e=0; e<numExcessive; e++) {
5541 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5542 if (ew != null) {
5543 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07005544 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005545 pw.print("cpu");
5546 } else {
5547 pw.print("unknown");
5548 }
5549 pw.print(" use: ");
5550 TimeUtils.formatDuration(ew.usedTime, pw);
5551 pw.print(" over ");
5552 TimeUtils.formatDuration(ew.overTime, pw);
5553 if (ew.overTime != 0) {
5554 pw.print(" (");
5555 pw.print((ew.usedTime*100)/ew.overTime);
5556 pw.println("%)");
5557 }
5558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005559 }
5560 uidActivity = true;
5561 }
5562 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005563
5564 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5565 = u.getPackageStats();
5566 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
5567 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
5568 pw.println(":");
5569 boolean apkActivity = false;
5570 final Uid.Pkg ps = packageStats.valueAt(ipkg);
5571 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
5572 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
5573 pw.print(prefix); pw.print(" Wakeup alarm ");
5574 pw.print(alarms.keyAt(iwa)); pw.print(": ");
5575 pw.print(alarms.valueAt(iwa).getCountLocked(which));
5576 pw.println(" times");
5577 apkActivity = true;
5578 }
5579 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
5580 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
5581 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
5582 final long startTime = ss.getStartTime(batteryUptime, which);
5583 final int starts = ss.getStarts(which);
5584 final int launches = ss.getLaunches(which);
5585 if (startTime != 0 || starts != 0 || launches != 0) {
5586 sb.setLength(0);
5587 sb.append(prefix); sb.append(" Service ");
5588 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
5589 sb.append(prefix); sb.append(" Created for: ");
5590 formatTimeMs(sb, startTime / 1000);
5591 sb.append("uptime\n");
5592 sb.append(prefix); sb.append(" Starts: ");
5593 sb.append(starts);
5594 sb.append(", launches: "); sb.append(launches);
5595 pw.println(sb.toString());
5596 apkActivity = true;
5597 }
5598 }
5599 if (!apkActivity) {
5600 pw.print(prefix); pw.println(" (nothing executed)");
5601 }
5602 uidActivity = true;
5603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005604 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005605 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005606 }
5607 }
5608 }
5609
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005610 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005611 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005612 int diff = oldval ^ newval;
5613 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005614 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005615 for (int i=0; i<descriptions.length; i++) {
5616 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005617 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005618 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005619 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005620 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005621 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005622 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
5623 didWake = true;
5624 pw.print("=");
5625 if (longNames) {
5626 UserHandle.formatUid(pw, wakelockTag.uid);
5627 pw.print(":\"");
5628 pw.print(wakelockTag.string);
5629 pw.print("\"");
5630 } else {
5631 pw.print(wakelockTag.poolIdx);
5632 }
5633 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005634 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005635 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005636 pw.print("=");
5637 int val = (newval&bd.mask)>>bd.shift;
5638 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005639 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005640 } else {
5641 pw.print(val);
5642 }
5643 }
5644 }
5645 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005646 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07005647 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005648 if (longNames) {
5649 UserHandle.formatUid(pw, wakelockTag.uid);
5650 pw.print(":\"");
5651 pw.print(wakelockTag.string);
5652 pw.print("\"");
5653 } else {
5654 pw.print(wakelockTag.poolIdx);
5655 }
5656 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005657 }
Mike Mac2f518a2017-09-19 16:06:03 -07005658
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005659 public void prepareForDumpLocked() {
Kweku Adams2f73ecd2017-09-27 16:59:19 -07005660 // We don't need to require subclasses implement this.
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005661 }
5662
5663 public static class HistoryPrinter {
5664 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005665 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005666 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005667 int oldStatus = -1;
5668 int oldHealth = -1;
5669 int oldPlug = -1;
5670 int oldTemp = -1;
5671 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005672 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005673 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005674
Dianne Hackborn3251b902014-06-20 14:40:53 -07005675 void reset() {
5676 oldState = oldState2 = 0;
5677 oldLevel = -1;
5678 oldStatus = -1;
5679 oldHealth = -1;
5680 oldPlug = -1;
5681 oldTemp = -1;
5682 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005683 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07005684 }
5685
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005686 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005687 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005688 if (!checkin) {
5689 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005690 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005691 pw.print(" (");
5692 pw.print(rec.numReadInts);
5693 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005694 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005695 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5696 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005697 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005698 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005699 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005700 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005701 }
5702 lastTime = rec.time;
5703 }
5704 if (rec.cmd == HistoryItem.CMD_START) {
5705 if (checkin) {
5706 pw.print(":");
5707 }
5708 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005709 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005710 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
5711 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005712 if (checkin) {
5713 pw.print(":");
5714 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005715 if (rec.cmd == HistoryItem.CMD_RESET) {
5716 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005717 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005718 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005719 pw.print("TIME:");
5720 if (checkin) {
5721 pw.println(rec.currentTime);
5722 } else {
5723 pw.print(" ");
5724 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5725 rec.currentTime).toString());
5726 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005727 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
5728 if (checkin) {
5729 pw.print(":");
5730 }
5731 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005732 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
5733 if (checkin) {
5734 pw.print(":");
5735 }
5736 pw.println("*OVERFLOW*");
5737 } else {
5738 if (!checkin) {
5739 if (rec.batteryLevel < 10) pw.print("00");
5740 else if (rec.batteryLevel < 100) pw.print("0");
5741 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005742 if (verbose) {
5743 pw.print(" ");
5744 if (rec.states < 0) ;
5745 else if (rec.states < 0x10) pw.print("0000000");
5746 else if (rec.states < 0x100) pw.print("000000");
5747 else if (rec.states < 0x1000) pw.print("00000");
5748 else if (rec.states < 0x10000) pw.print("0000");
5749 else if (rec.states < 0x100000) pw.print("000");
5750 else if (rec.states < 0x1000000) pw.print("00");
5751 else if (rec.states < 0x10000000) pw.print("0");
5752 pw.print(Integer.toHexString(rec.states));
5753 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005754 } else {
5755 if (oldLevel != rec.batteryLevel) {
5756 oldLevel = rec.batteryLevel;
5757 pw.print(",Bl="); pw.print(rec.batteryLevel);
5758 }
5759 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005760 if (oldStatus != rec.batteryStatus) {
5761 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005762 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005763 switch (oldStatus) {
5764 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005765 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005766 break;
5767 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005768 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005769 break;
5770 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005771 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005772 break;
5773 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005774 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005775 break;
5776 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005777 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005778 break;
5779 default:
5780 pw.print(oldStatus);
5781 break;
5782 }
5783 }
5784 if (oldHealth != rec.batteryHealth) {
5785 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005786 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005787 switch (oldHealth) {
5788 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005789 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005790 break;
5791 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005792 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005793 break;
5794 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005795 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005796 break;
5797 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005798 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005799 break;
5800 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005801 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005802 break;
5803 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005804 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005805 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005806 case BatteryManager.BATTERY_HEALTH_COLD:
5807 pw.print(checkin ? "c" : "cold");
5808 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005809 default:
5810 pw.print(oldHealth);
5811 break;
5812 }
5813 }
5814 if (oldPlug != rec.batteryPlugType) {
5815 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005816 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005817 switch (oldPlug) {
5818 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005819 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005820 break;
5821 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005822 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005823 break;
5824 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005825 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005826 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005827 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005828 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005829 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005830 default:
5831 pw.print(oldPlug);
5832 break;
5833 }
5834 }
5835 if (oldTemp != rec.batteryTemperature) {
5836 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005837 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005838 pw.print(oldTemp);
5839 }
5840 if (oldVolt != rec.batteryVoltage) {
5841 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005842 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005843 pw.print(oldVolt);
5844 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005845 final int chargeMAh = rec.batteryChargeUAh / 1000;
5846 if (oldChargeMAh != chargeMAh) {
5847 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07005848 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005849 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07005850 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005851 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005852 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005853 printBitDescriptions(pw, oldState2, rec.states2, null,
5854 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005855 if (rec.wakeReasonTag != null) {
5856 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005857 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005858 pw.print(rec.wakeReasonTag.poolIdx);
5859 } else {
5860 pw.print(" wake_reason=");
5861 pw.print(rec.wakeReasonTag.uid);
5862 pw.print(":\"");
5863 pw.print(rec.wakeReasonTag.string);
5864 pw.print("\"");
5865 }
5866 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08005867 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005868 pw.print(checkin ? "," : " ");
5869 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
5870 pw.print("+");
5871 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
5872 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005873 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005874 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
5875 : HISTORY_EVENT_NAMES;
5876 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
5877 | HistoryItem.EVENT_FLAG_FINISH);
5878 if (idx >= 0 && idx < eventNames.length) {
5879 pw.print(eventNames[idx]);
5880 } else {
5881 pw.print(checkin ? "Ev" : "event");
5882 pw.print(idx);
5883 }
5884 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005885 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005886 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08005887 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08005888 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
5889 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005890 pw.print(":\"");
5891 pw.print(rec.eventTag.string);
5892 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005893 }
5894 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005895 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005896 if (rec.stepDetails != null) {
5897 if (!checkin) {
5898 pw.print(" Details: cpu=");
5899 pw.print(rec.stepDetails.userTime);
5900 pw.print("u+");
5901 pw.print(rec.stepDetails.systemTime);
5902 pw.print("s");
5903 if (rec.stepDetails.appCpuUid1 >= 0) {
5904 pw.print(" (");
5905 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
5906 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5907 if (rec.stepDetails.appCpuUid2 >= 0) {
5908 pw.print(", ");
5909 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
5910 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5911 }
5912 if (rec.stepDetails.appCpuUid3 >= 0) {
5913 pw.print(", ");
5914 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
5915 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5916 }
5917 pw.print(')');
5918 }
5919 pw.println();
5920 pw.print(" /proc/stat=");
5921 pw.print(rec.stepDetails.statUserTime);
5922 pw.print(" usr, ");
5923 pw.print(rec.stepDetails.statSystemTime);
5924 pw.print(" sys, ");
5925 pw.print(rec.stepDetails.statIOWaitTime);
5926 pw.print(" io, ");
5927 pw.print(rec.stepDetails.statIrqTime);
5928 pw.print(" irq, ");
5929 pw.print(rec.stepDetails.statSoftIrqTime);
5930 pw.print(" sirq, ");
5931 pw.print(rec.stepDetails.statIdlTime);
5932 pw.print(" idle");
5933 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
5934 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
5935 + rec.stepDetails.statSoftIrqTime;
5936 int total = totalRun + rec.stepDetails.statIdlTime;
5937 if (total > 0) {
5938 pw.print(" (");
5939 float perc = ((float)totalRun) / ((float)total) * 100;
5940 pw.print(String.format("%.1f%%", perc));
5941 pw.print(" of ");
5942 StringBuilder sb = new StringBuilder(64);
5943 formatTimeMsNoSpace(sb, total*10);
5944 pw.print(sb);
5945 pw.print(")");
5946 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005947 pw.print(", PlatformIdleStat ");
5948 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005949 pw.println();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00005950
5951 pw.print(", SubsystemPowerState ");
5952 pw.print(rec.stepDetails.statSubsystemPowerState);
5953 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005954 } else {
5955 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5956 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
5957 pw.print(rec.stepDetails.userTime);
5958 pw.print(":");
5959 pw.print(rec.stepDetails.systemTime);
5960 if (rec.stepDetails.appCpuUid1 >= 0) {
5961 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
5962 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5963 if (rec.stepDetails.appCpuUid2 >= 0) {
5964 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
5965 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5966 }
5967 if (rec.stepDetails.appCpuUid3 >= 0) {
5968 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
5969 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5970 }
5971 }
5972 pw.println();
5973 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5974 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
5975 pw.print(rec.stepDetails.statUserTime);
5976 pw.print(',');
5977 pw.print(rec.stepDetails.statSystemTime);
5978 pw.print(',');
5979 pw.print(rec.stepDetails.statIOWaitTime);
5980 pw.print(',');
5981 pw.print(rec.stepDetails.statIrqTime);
5982 pw.print(',');
5983 pw.print(rec.stepDetails.statSoftIrqTime);
5984 pw.print(',');
5985 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005986 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005987 if (rec.stepDetails.statPlatformIdleState != null) {
5988 pw.print(rec.stepDetails.statPlatformIdleState);
Ahmed ElArabawy307edcd2017-07-07 17:48:13 -07005989 if (rec.stepDetails.statSubsystemPowerState != null) {
5990 pw.print(',');
5991 }
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005992 }
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00005993
5994 if (rec.stepDetails.statSubsystemPowerState != null) {
5995 pw.print(rec.stepDetails.statSubsystemPowerState);
5996 }
5997 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005998 }
5999 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006000 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006001 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006002 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006003 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006004
6005 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
6006 UserHandle.formatUid(pw, uid);
6007 pw.print("=");
6008 pw.print(utime);
6009 pw.print("u+");
6010 pw.print(stime);
6011 pw.print("s");
6012 }
6013
6014 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
6015 pw.print('/');
6016 pw.print(uid);
6017 pw.print(":");
6018 pw.print(utime);
6019 pw.print(":");
6020 pw.print(stime);
6021 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006022 }
6023
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006024 private void printSizeValue(PrintWriter pw, long size) {
6025 float result = size;
6026 String suffix = "";
6027 if (result >= 10*1024) {
6028 suffix = "KB";
6029 result = result / 1024;
6030 }
6031 if (result >= 10*1024) {
6032 suffix = "MB";
6033 result = result / 1024;
6034 }
6035 if (result >= 10*1024) {
6036 suffix = "GB";
6037 result = result / 1024;
6038 }
6039 if (result >= 10*1024) {
6040 suffix = "TB";
6041 result = result / 1024;
6042 }
6043 if (result >= 10*1024) {
6044 suffix = "PB";
6045 result = result / 1024;
6046 }
6047 pw.print((int)result);
6048 pw.print(suffix);
6049 }
6050
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006051 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
6052 String label3, long estimatedTime) {
6053 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006054 return false;
6055 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006056 pw.print(label1);
6057 pw.print(label2);
6058 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006059 StringBuilder sb = new StringBuilder(64);
6060 formatTimeMs(sb, estimatedTime);
6061 pw.print(sb);
6062 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08006063 return true;
6064 }
6065
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006066 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
6067 LevelStepTracker steps, boolean checkin) {
6068 if (steps == null) {
6069 return false;
6070 }
6071 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006072 if (count <= 0) {
6073 return false;
6074 }
6075 if (!checkin) {
6076 pw.println(header);
6077 }
Kweku Adams030980a2015-04-01 16:07:48 -07006078 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006079 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006080 long duration = steps.getDurationAt(i);
6081 int level = steps.getLevelAt(i);
6082 long initMode = steps.getInitModeAt(i);
6083 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006084 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006085 lineArgs[0] = Long.toString(duration);
6086 lineArgs[1] = Integer.toString(level);
6087 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6088 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6089 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
6090 case Display.STATE_ON: lineArgs[2] = "s+"; break;
6091 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
6092 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07006093 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006094 }
6095 } else {
6096 lineArgs[2] = "";
6097 }
6098 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6099 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
6100 } else {
6101 lineArgs[3] = "";
6102 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006103 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07006104 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006105 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07006106 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006107 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006108 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
6109 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006110 pw.print(prefix);
6111 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006112 TimeUtils.formatDuration(duration, pw);
6113 pw.print(" to "); pw.print(level);
6114 boolean haveModes = false;
6115 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6116 pw.print(" (");
6117 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6118 case Display.STATE_OFF: pw.print("screen-off"); break;
6119 case Display.STATE_ON: pw.print("screen-on"); break;
6120 case Display.STATE_DOZE: pw.print("screen-doze"); break;
6121 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07006122 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006123 }
6124 haveModes = true;
6125 }
6126 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6127 pw.print(haveModes ? ", " : " (");
6128 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
6129 ? "power-save-on" : "power-save-off");
6130 haveModes = true;
6131 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006132 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6133 pw.print(haveModes ? ", " : " (");
6134 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6135 ? "device-idle-on" : "device-idle-off");
6136 haveModes = true;
6137 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006138 if (haveModes) {
6139 pw.print(")");
6140 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006141 pw.println();
6142 }
6143 }
6144 return true;
6145 }
6146
Kweku Adams87b19ec2017-10-09 12:40:03 -07006147 private static void dumpDurationSteps(ProtoOutputStream proto, long fieldId,
6148 LevelStepTracker steps) {
6149 if (steps == null) {
6150 return;
6151 }
6152 int count = steps.mNumStepDurations;
Kweku Adams87b19ec2017-10-09 12:40:03 -07006153 for (int i = 0; i < count; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07006154 long token = proto.start(fieldId);
Kweku Adams87b19ec2017-10-09 12:40:03 -07006155 proto.write(SystemProto.BatteryLevelStep.DURATION_MS, steps.getDurationAt(i));
6156 proto.write(SystemProto.BatteryLevelStep.LEVEL, steps.getLevelAt(i));
6157
6158 final long initMode = steps.getInitModeAt(i);
6159 final long modMode = steps.getModModeAt(i);
6160
6161 int ds = SystemProto.BatteryLevelStep.DS_MIXED;
6162 if ((modMode & STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
6163 switch ((int) (initMode & STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
6164 case Display.STATE_OFF:
6165 ds = SystemProto.BatteryLevelStep.DS_OFF;
6166 break;
6167 case Display.STATE_ON:
6168 ds = SystemProto.BatteryLevelStep.DS_ON;
6169 break;
6170 case Display.STATE_DOZE:
6171 ds = SystemProto.BatteryLevelStep.DS_DOZE;
6172 break;
6173 case Display.STATE_DOZE_SUSPEND:
6174 ds = SystemProto.BatteryLevelStep.DS_DOZE_SUSPEND;
6175 break;
6176 default:
6177 ds = SystemProto.BatteryLevelStep.DS_ERROR;
6178 break;
6179 }
6180 }
6181 proto.write(SystemProto.BatteryLevelStep.DISPLAY_STATE, ds);
6182
6183 int psm = SystemProto.BatteryLevelStep.PSM_MIXED;
6184 if ((modMode & STEP_LEVEL_MODE_POWER_SAVE) == 0) {
6185 psm = (initMode & STEP_LEVEL_MODE_POWER_SAVE) != 0
6186 ? SystemProto.BatteryLevelStep.PSM_ON : SystemProto.BatteryLevelStep.PSM_OFF;
6187 }
6188 proto.write(SystemProto.BatteryLevelStep.POWER_SAVE_MODE, psm);
6189
6190 int im = SystemProto.BatteryLevelStep.IM_MIXED;
6191 if ((modMode & STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
6192 im = (initMode & STEP_LEVEL_MODE_DEVICE_IDLE) != 0
6193 ? SystemProto.BatteryLevelStep.IM_ON : SystemProto.BatteryLevelStep.IM_OFF;
6194 }
6195 proto.write(SystemProto.BatteryLevelStep.IDLE_MODE, im);
6196
6197 proto.end(token);
6198 }
6199 }
6200
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006201 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006202 public static final int DUMP_DAILY_ONLY = 1<<2;
6203 public static final int DUMP_HISTORY_ONLY = 1<<3;
6204 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
6205 public static final int DUMP_VERBOSE = 1<<5;
6206 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006207
Dianne Hackborn37de0982014-05-09 09:32:18 -07006208 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
6209 final HistoryPrinter hprinter = new HistoryPrinter();
6210 final HistoryItem rec = new HistoryItem();
6211 long lastTime = -1;
6212 long baseTime = -1;
6213 boolean printed = false;
6214 HistoryEventTracker tracker = null;
6215 while (getNextHistoryLocked(rec)) {
6216 lastTime = rec.time;
6217 if (baseTime < 0) {
6218 baseTime = lastTime;
6219 }
6220 if (rec.time >= histStart) {
6221 if (histStart >= 0 && !printed) {
6222 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07006223 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08006224 || rec.cmd == HistoryItem.CMD_START
6225 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006226 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006227 hprinter.printNextItem(pw, rec, baseTime, checkin,
6228 (flags&DUMP_VERBOSE) != 0);
6229 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006230 } else if (rec.currentTime != 0) {
6231 printed = true;
6232 byte cmd = rec.cmd;
6233 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006234 hprinter.printNextItem(pw, rec, baseTime, checkin,
6235 (flags&DUMP_VERBOSE) != 0);
6236 rec.cmd = cmd;
6237 }
6238 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006239 if (rec.cmd != HistoryItem.CMD_UPDATE) {
6240 hprinter.printNextItem(pw, rec, baseTime, checkin,
6241 (flags&DUMP_VERBOSE) != 0);
6242 rec.cmd = HistoryItem.CMD_UPDATE;
6243 }
6244 int oldEventCode = rec.eventCode;
6245 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006246 rec.eventTag = new HistoryTag();
6247 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
6248 HashMap<String, SparseIntArray> active
6249 = tracker.getStateForEvent(i);
6250 if (active == null) {
6251 continue;
6252 }
6253 for (HashMap.Entry<String, SparseIntArray> ent
6254 : active.entrySet()) {
6255 SparseIntArray uids = ent.getValue();
6256 for (int j=0; j<uids.size(); j++) {
6257 rec.eventCode = i;
6258 rec.eventTag.string = ent.getKey();
6259 rec.eventTag.uid = uids.keyAt(j);
6260 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006261 hprinter.printNextItem(pw, rec, baseTime, checkin,
6262 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006263 rec.wakeReasonTag = null;
6264 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006265 }
6266 }
6267 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006268 rec.eventCode = oldEventCode;
6269 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07006270 tracker = null;
6271 }
6272 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006273 hprinter.printNextItem(pw, rec, baseTime, checkin,
6274 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07006275 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
6276 // This is an attempt to aggregate the previous state and generate
6277 // fake events to reflect that state at the point where we start
6278 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07006279 if (tracker == null) {
6280 tracker = new HistoryEventTracker();
6281 }
6282 tracker.updateState(rec.eventCode, rec.eventTag.string,
6283 rec.eventTag.uid, rec.eventTag.poolIdx);
6284 }
6285 }
6286 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07006287 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006288 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
6289 }
6290 }
6291
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006292 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
6293 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
6294 if (steps == null) {
6295 return;
6296 }
6297 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
6298 if (timeRemaining >= 0) {
6299 pw.print(prefix); pw.print(label); pw.print(" total time: ");
6300 tmpSb.setLength(0);
6301 formatTimeMs(tmpSb, timeRemaining);
6302 pw.print(tmpSb);
6303 pw.print(" (from "); pw.print(tmpOutInt[0]);
6304 pw.println(" steps)");
6305 }
6306 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6307 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6308 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
6309 if (estimatedTime > 0) {
6310 pw.print(prefix); pw.print(label); pw.print(" ");
6311 pw.print(STEP_LEVEL_MODE_LABELS[i]);
6312 pw.print(" time: ");
6313 tmpSb.setLength(0);
6314 formatTimeMs(tmpSb, estimatedTime);
6315 pw.print(tmpSb);
6316 pw.print(" (from "); pw.print(tmpOutInt[0]);
6317 pw.println(" steps)");
6318 }
6319 }
6320 }
6321
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006322 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
6323 ArrayList<PackageChange> changes) {
6324 if (changes == null) {
6325 return;
6326 }
6327 pw.print(prefix); pw.println("Package changes:");
6328 for (int i=0; i<changes.size(); i++) {
6329 PackageChange pc = changes.get(i);
6330 if (pc.mUpdate) {
6331 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
6332 pw.print(" vers="); pw.println(pc.mVersionCode);
6333 } else {
6334 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
6335 }
6336 }
6337 }
6338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006339 /**
6340 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
6341 *
6342 * @param pw a Printer to receive the dump output.
6343 */
6344 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006345 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006346 prepareForDumpLocked();
6347
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006348 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006349 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006350
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006351 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006352 final long historyTotalSize = getHistoryTotalSize();
6353 final long historyUsedSize = getHistoryUsedSize();
6354 if (startIteratingHistoryLocked()) {
6355 try {
6356 pw.print("Battery History (");
6357 pw.print((100*historyUsedSize)/historyTotalSize);
6358 pw.print("% used, ");
6359 printSizeValue(pw, historyUsedSize);
6360 pw.print(" used of ");
6361 printSizeValue(pw, historyTotalSize);
6362 pw.print(", ");
6363 pw.print(getHistoryStringPoolSize());
6364 pw.print(" strings using ");
6365 printSizeValue(pw, getHistoryStringPoolBytes());
6366 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07006367 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006368 pw.println();
6369 } finally {
6370 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006371 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006372 }
6373
6374 if (startIteratingOldHistoryLocked()) {
6375 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006376 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006377 pw.println("Old battery History:");
6378 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006379 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006380 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006381 if (baseTime < 0) {
6382 baseTime = rec.time;
6383 }
6384 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006385 }
6386 pw.println();
6387 } finally {
6388 finishIteratingOldHistoryLocked();
6389 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006390 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006391 }
6392
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006393 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006394 return;
6395 }
6396
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006397 if (!filtering) {
6398 SparseArray<? extends Uid> uidStats = getUidStats();
6399 final int NU = uidStats.size();
6400 boolean didPid = false;
6401 long nowRealtime = SystemClock.elapsedRealtime();
6402 for (int i=0; i<NU; i++) {
6403 Uid uid = uidStats.valueAt(i);
6404 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
6405 if (pids != null) {
6406 for (int j=0; j<pids.size(); j++) {
6407 Uid.Pid pid = pids.valueAt(j);
6408 if (!didPid) {
6409 pw.println("Per-PID Stats:");
6410 didPid = true;
6411 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006412 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
6413 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006414 pw.print(" PID "); pw.print(pids.keyAt(j));
6415 pw.print(" wake time: ");
6416 TimeUtils.formatDuration(time, pw);
6417 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006418 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006419 }
6420 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006421 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006422 pw.println();
6423 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006424 }
6425
6426 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006427 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
6428 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006429 long timeRemaining = computeBatteryTimeRemaining(
6430 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006431 if (timeRemaining >= 0) {
6432 pw.print(" Estimated discharge time remaining: ");
6433 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6434 pw.println();
6435 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006436 final LevelStepTracker steps = getDischargeLevelStepTracker();
6437 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6438 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
6439 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6440 STEP_LEVEL_MODE_VALUES[i], null));
6441 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006442 pw.println();
6443 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006444 if (dumpDurationSteps(pw, " ", "Charge step durations:",
6445 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006446 long timeRemaining = computeChargeTimeRemaining(
6447 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006448 if (timeRemaining >= 0) {
6449 pw.print(" Estimated charge time remaining: ");
6450 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6451 pw.println();
6452 }
6453 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006454 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006455 }
Dianne Hackbornc81983a2017-10-20 16:16:32 -07006456 if (!filtering || (flags & DUMP_DAILY_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006457 pw.println("Daily stats:");
6458 pw.print(" Current start time: ");
6459 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6460 getCurrentDailyStartTime()).toString());
6461 pw.print(" Next min deadline: ");
6462 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6463 getNextMinDailyDeadline()).toString());
6464 pw.print(" Next max deadline: ");
6465 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6466 getNextMaxDailyDeadline()).toString());
6467 StringBuilder sb = new StringBuilder(64);
6468 int[] outInt = new int[1];
6469 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
6470 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006471 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
6472 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006473 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006474 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
6475 dsteps, false)) {
6476 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6477 sb, outInt);
6478 }
6479 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6480 csteps, false)) {
6481 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6482 sb, outInt);
6483 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006484 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006485 } else {
6486 pw.println(" Current daily steps:");
6487 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6488 sb, outInt);
6489 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6490 sb, outInt);
6491 }
6492 }
6493 DailyItem dit;
6494 int curIndex = 0;
6495 while ((dit=getDailyItemLocked(curIndex)) != null) {
6496 curIndex++;
6497 if ((flags&DUMP_DAILY_ONLY) != 0) {
6498 pw.println();
6499 }
6500 pw.print(" Daily from ");
6501 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6502 pw.print(" to ");
6503 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6504 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006505 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006506 if (dumpDurationSteps(pw, " ",
6507 " Discharge step durations:", dit.mDischargeSteps, false)) {
6508 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6509 sb, outInt);
6510 }
6511 if (dumpDurationSteps(pw, " ",
6512 " Charge step durations:", dit.mChargeSteps, false)) {
6513 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6514 sb, outInt);
6515 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006516 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006517 } else {
6518 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6519 sb, outInt);
6520 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6521 sb, outInt);
6522 }
6523 }
6524 pw.println();
6525 }
6526 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006527 pw.println("Statistics since last charge:");
6528 pw.println(" System starts: " + getStartCount()
6529 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006530 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6531 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006532 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006534 }
Mike Mac2f518a2017-09-19 16:06:03 -07006535
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006536 // This is called from BatteryStatsService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006537 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006538 public void dumpCheckinLocked(Context context, PrintWriter pw,
6539 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006540 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006541
6542 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006543 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6544 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006545
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006546 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6547
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006548 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07006549 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006550 try {
6551 for (int i=0; i<getHistoryStringPoolSize(); i++) {
6552 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6553 pw.print(HISTORY_STRING_POOL); pw.print(',');
6554 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006555 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006556 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006557 pw.print(",\"");
6558 String str = getHistoryTagPoolString(i);
6559 str = str.replace("\\", "\\\\");
6560 str = str.replace("\"", "\\\"");
6561 pw.print(str);
6562 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006563 pw.println();
6564 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006565 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006566 } finally {
6567 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08006568 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006569 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006570 }
6571
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006572 if ((flags & DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006573 return;
6574 }
6575
Dianne Hackborne4a59512010-12-07 11:08:07 -08006576 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006577 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08006578 for (int i=0; i<apps.size(); i++) {
6579 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006580 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
6581 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08006582 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006583 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
6584 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006585 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006586 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006587 }
6588 SparseArray<? extends Uid> uidStats = getUidStats();
6589 final int NU = uidStats.size();
6590 String[] lineArgs = new String[2];
6591 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006592 int uid = UserHandle.getAppId(uidStats.keyAt(i));
6593 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
6594 if (pkgs != null && !pkgs.second.value) {
6595 pkgs.second.value = true;
6596 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08006597 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006598 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006599 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
6600 (Object[])lineArgs);
6601 }
6602 }
6603 }
6604 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006605 if ((flags & DUMP_DAILY_ONLY) == 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006606 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006607 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07006608 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006609 if (timeRemaining >= 0) {
6610 lineArgs[0] = Long.toString(timeRemaining);
6611 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
6612 (Object[])lineArgs);
6613 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006614 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07006615 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006616 if (timeRemaining >= 0) {
6617 lineArgs[0] = Long.toString(timeRemaining);
6618 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
6619 (Object[])lineArgs);
6620 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006621 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
6622 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006624 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006625
Kweku Adams87b19ec2017-10-09 12:40:03 -07006626 /** Dump #STATS_SINCE_CHARGED batterystats data to a proto. @hide */
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006627 public void dumpProtoLocked(Context context, FileDescriptor fd, List<ApplicationInfo> apps,
6628 int flags, long historyStart) {
6629 final ProtoOutputStream proto = new ProtoOutputStream(fd);
6630 final long bToken = proto.start(BatteryStatsServiceDumpProto.BATTERYSTATS);
6631 prepareForDumpLocked();
6632
6633 proto.write(BatteryStatsProto.REPORT_VERSION, CHECKIN_VERSION);
6634 proto.write(BatteryStatsProto.PARCEL_VERSION, getParcelVersion());
6635 proto.write(BatteryStatsProto.START_PLATFORM_VERSION, getStartPlatformVersion());
6636 proto.write(BatteryStatsProto.END_PLATFORM_VERSION, getEndPlatformVersion());
6637
6638 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6639
6640 if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
6641 if (startIteratingHistoryLocked()) {
6642 // TODO: implement dumpProtoHistoryLocked(proto);
6643 }
6644 }
6645
6646 if ((flags & (DUMP_HISTORY_ONLY | DUMP_DAILY_ONLY)) == 0) {
Kweku Adams103351f2017-10-16 14:39:34 -07006647 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false,
6648 (flags & DUMP_DEVICE_WIFI_ONLY) != 0);
6649 helper.create(this);
6650 helper.refreshStats(STATS_SINCE_CHARGED, UserHandle.USER_ALL);
6651
6652 dumpProtoAppsLocked(proto, helper, apps);
6653 dumpProtoSystemLocked(proto, helper);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07006654 }
6655
6656 proto.end(bToken);
6657 proto.flush();
6658 }
Kweku Adams87b19ec2017-10-09 12:40:03 -07006659
Kweku Adams103351f2017-10-16 14:39:34 -07006660 private void dumpProtoAppsLocked(ProtoOutputStream proto, BatteryStatsHelper helper,
6661 List<ApplicationInfo> apps) {
6662 final int which = STATS_SINCE_CHARGED;
6663 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
6664 final long rawRealtimeMs = SystemClock.elapsedRealtime();
6665 final long rawRealtimeUs = rawRealtimeMs * 1000;
6666 final long batteryUptimeUs = getBatteryUptime(rawUptimeUs);
6667
6668 SparseArray<ArrayList<String>> aidToPackages = new SparseArray<>();
6669 if (apps != null) {
6670 for (int i = 0; i < apps.size(); ++i) {
6671 ApplicationInfo ai = apps.get(i);
6672 int aid = UserHandle.getAppId(ai.uid);
6673 ArrayList<String> pkgs = aidToPackages.get(aid);
6674 if (pkgs == null) {
6675 pkgs = new ArrayList<String>();
6676 aidToPackages.put(aid, pkgs);
6677 }
6678 pkgs.add(ai.packageName);
6679 }
6680 }
6681
6682 SparseArray<BatterySipper> uidToSipper = new SparseArray<>();
6683 final List<BatterySipper> sippers = helper.getUsageList();
6684 if (sippers != null) {
6685 for (int i = 0; i < sippers.size(); ++i) {
6686 final BatterySipper bs = sippers.get(i);
6687 if (bs.drainType != BatterySipper.DrainType.APP) {
6688 // Others are handled by dumpProtoSystemLocked()
6689 continue;
6690 }
6691 uidToSipper.put(bs.uidObj.getUid(), bs);
6692 }
6693 }
6694
6695 SparseArray<? extends Uid> uidStats = getUidStats();
6696 final int n = uidStats.size();
6697 for (int iu = 0; iu < n; ++iu) {
6698 final long uTkn = proto.start(BatteryStatsProto.UIDS);
6699 final Uid u = uidStats.valueAt(iu);
6700
6701 final int uid = uidStats.keyAt(iu);
6702 proto.write(UidProto.UID, uid);
6703
6704 // Print packages and apk stats (UID_DATA & APK_DATA)
6705 ArrayList<String> pkgs = aidToPackages.get(UserHandle.getAppId(uid));
6706 if (pkgs == null) {
6707 pkgs = new ArrayList<String>();
6708 }
6709 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats =
6710 u.getPackageStats();
6711 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
6712 String pkg = packageStats.keyAt(ipkg);
6713 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats =
6714 packageStats.valueAt(ipkg).getServiceStats();
6715 if (serviceStats.size() == 0) {
6716 // Due to the way ActivityManagerService logs wakeup alarms, some packages (for
6717 // example, "android") may be included in the packageStats that aren't part of
6718 // the UID. If they don't have any services, then they shouldn't be listed here.
6719 // These packages won't be a part in the pkgs List.
6720 continue;
6721 }
6722
6723 final long pToken = proto.start(UidProto.PACKAGES);
6724 proto.write(UidProto.Package.NAME, pkg);
6725 // Remove from the packages list since we're logging it here.
6726 pkgs.remove(pkg);
6727
6728 for (int isvc = serviceStats.size() - 1; isvc >= 0; --isvc) {
6729 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
6730 long sToken = proto.start(UidProto.Package.SERVICES);
6731
6732 proto.write(UidProto.Package.Service.NAME, serviceStats.keyAt(isvc));
6733 proto.write(UidProto.Package.Service.START_DURATION_MS,
6734 roundUsToMs(ss.getStartTime(batteryUptimeUs, which)));
6735 proto.write(UidProto.Package.Service.START_COUNT, ss.getStarts(which));
6736 proto.write(UidProto.Package.Service.LAUNCH_COUNT, ss.getLaunches(which));
6737
6738 proto.end(sToken);
6739 }
6740 proto.end(pToken);
6741 }
6742 // Print any remaining packages that weren't in the packageStats map. pkgs is pulled
6743 // from PackageManager data. Packages are only included in packageStats if there was
6744 // specific data tracked for them (services and wakeup alarms, etc.).
6745 for (String p : pkgs) {
6746 final long pToken = proto.start(UidProto.PACKAGES);
6747 proto.write(UidProto.Package.NAME, p);
6748 proto.end(pToken);
6749 }
6750
6751 // Total wakelock data (AGGREGATED_WAKELOCK_DATA)
6752 if (u.getAggregatedPartialWakelockTimer() != null) {
6753 final Timer timer = u.getAggregatedPartialWakelockTimer();
6754 // Times are since reset (regardless of 'which')
6755 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
6756 final Timer bgTimer = timer.getSubTimer();
6757 final long bgTimeMs = bgTimer != null
6758 ? bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
6759 final long awToken = proto.start(UidProto.AGGREGATED_WAKELOCK);
6760 proto.write(UidProto.AggregatedWakelock.PARTIAL_DURATION_MS, totTimeMs);
6761 proto.write(UidProto.AggregatedWakelock.BACKGROUND_PARTIAL_DURATION_MS, bgTimeMs);
6762 proto.end(awToken);
6763 }
6764
6765 // Audio (AUDIO_DATA)
6766 dumpTimer(proto, UidProto.AUDIO, u.getAudioTurnedOnTimer(), rawRealtimeUs, which);
6767
6768 // Bluetooth Controller (BLUETOOTH_CONTROLLER_DATA)
6769 dumpControllerActivityProto(proto, UidProto.BLUETOOTH_CONTROLLER,
6770 u.getBluetoothControllerActivity(), which);
6771
6772 // BLE scans (BLUETOOTH_MISC_DATA) (uses totalDurationMsLocked and MaxDurationMsLocked)
6773 final Timer bleTimer = u.getBluetoothScanTimer();
6774 if (bleTimer != null) {
6775 final long bmToken = proto.start(UidProto.BLUETOOTH_MISC);
6776
6777 dumpTimer(proto, UidProto.BluetoothMisc.APPORTIONED_BLE_SCAN, bleTimer,
6778 rawRealtimeUs, which);
6779 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN,
6780 u.getBluetoothScanBackgroundTimer(), rawRealtimeUs, which);
6781 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
6782 dumpTimer(proto, UidProto.BluetoothMisc.UNOPTIMIZED_BLE_SCAN,
6783 u.getBluetoothUnoptimizedScanTimer(), rawRealtimeUs, which);
6784 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
6785 dumpTimer(proto, UidProto.BluetoothMisc.BACKGROUND_UNOPTIMIZED_BLE_SCAN,
6786 u.getBluetoothUnoptimizedScanBackgroundTimer(), rawRealtimeUs, which);
6787 // Result counters
6788 proto.write(UidProto.BluetoothMisc.BLE_SCAN_RESULT_COUNT,
6789 u.getBluetoothScanResultCounter() != null
6790 ? u.getBluetoothScanResultCounter().getCountLocked(which) : 0);
6791 proto.write(UidProto.BluetoothMisc.BACKGROUND_BLE_SCAN_RESULT_COUNT,
6792 u.getBluetoothScanResultBgCounter() != null
6793 ? u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0);
6794
6795 proto.end(bmToken);
6796 }
6797
6798 // Camera (CAMERA_DATA)
6799 dumpTimer(proto, UidProto.CAMERA, u.getCameraTurnedOnTimer(), rawRealtimeUs, which);
6800
6801 // CPU stats (CPU_DATA & CPU_TIMES_AT_FREQ_DATA)
6802 final long cpuToken = proto.start(UidProto.CPU);
6803 proto.write(UidProto.Cpu.USER_DURATION_MS, roundUsToMs(u.getUserCpuTimeUs(which)));
6804 proto.write(UidProto.Cpu.SYSTEM_DURATION_MS, roundUsToMs(u.getSystemCpuTimeUs(which)));
6805
6806 final long[] cpuFreqs = getCpuFreqs();
6807 if (cpuFreqs != null) {
6808 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
6809 // If total cpuFreqTimes is null, then we don't need to check for
6810 // screenOffCpuFreqTimes.
6811 if (cpuFreqTimeMs != null && cpuFreqTimeMs.length == cpuFreqs.length) {
6812 long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
6813 if (screenOffCpuFreqTimeMs == null) {
6814 screenOffCpuFreqTimeMs = new long[cpuFreqTimeMs.length];
6815 }
6816 for (int ic = 0; ic < cpuFreqTimeMs.length; ++ic) {
6817 long cToken = proto.start(UidProto.Cpu.BY_FREQUENCY);
6818 proto.write(UidProto.Cpu.ByFrequency.FREQUENCY_INDEX, ic + 1);
6819 proto.write(UidProto.Cpu.ByFrequency.TOTAL_DURATION_MS,
6820 cpuFreqTimeMs[ic]);
6821 proto.write(UidProto.Cpu.ByFrequency.SCREEN_OFF_DURATION_MS,
6822 screenOffCpuFreqTimeMs[ic]);
6823 proto.end(cToken);
6824 }
6825 }
6826 }
6827 proto.end(cpuToken);
6828
6829 // Flashlight (FLASHLIGHT_DATA)
6830 dumpTimer(proto, UidProto.FLASHLIGHT, u.getFlashlightTurnedOnTimer(),
6831 rawRealtimeUs, which);
6832
6833 // Foreground activity (FOREGROUND_ACTIVITY_DATA)
6834 dumpTimer(proto, UidProto.FOREGROUND_ACTIVITY, u.getForegroundActivityTimer(),
6835 rawRealtimeUs, which);
6836
6837 // Foreground service (FOREGROUND_SERVICE_DATA)
6838 dumpTimer(proto, UidProto.FOREGROUND_SERVICE, u.getForegroundServiceTimer(),
6839 rawRealtimeUs, which);
6840
6841 // Job completion (JOB_COMPLETION_DATA)
6842 final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats();
6843 final int[] reasons = new int[]{
6844 JobParameters.REASON_CANCELED,
6845 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED,
6846 JobParameters.REASON_PREEMPT,
6847 JobParameters.REASON_TIMEOUT,
6848 JobParameters.REASON_DEVICE_IDLE,
6849 };
6850 for (int ic = 0; ic < completions.size(); ++ic) {
6851 SparseIntArray types = completions.valueAt(ic);
6852 if (types != null) {
6853 final long jcToken = proto.start(UidProto.JOB_COMPLETION);
6854
6855 proto.write(UidProto.JobCompletion.NAME, completions.keyAt(ic));
6856
6857 for (int r : reasons) {
6858 long rToken = proto.start(UidProto.JobCompletion.REASON_COUNT);
6859 proto.write(UidProto.JobCompletion.ReasonCount.NAME, r);
6860 proto.write(UidProto.JobCompletion.ReasonCount.COUNT, types.get(r, 0));
6861 proto.end(rToken);
6862 }
6863
6864 proto.end(jcToken);
6865 }
6866 }
6867
6868 // Scheduled jobs (JOB_DATA)
6869 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
6870 for (int ij = jobs.size() - 1; ij >= 0; --ij) {
6871 final Timer timer = jobs.valueAt(ij);
6872 final Timer bgTimer = timer.getSubTimer();
6873 final long jToken = proto.start(UidProto.JOBS);
6874
6875 proto.write(UidProto.Job.NAME, jobs.keyAt(ij));
6876 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
6877 dumpTimer(proto, UidProto.Job.TOTAL, timer, rawRealtimeUs, which);
6878 dumpTimer(proto, UidProto.Job.BACKGROUND, bgTimer, rawRealtimeUs, which);
6879
6880 proto.end(jToken);
6881 }
6882
6883 // Modem Controller (MODEM_CONTROLLER_DATA)
6884 dumpControllerActivityProto(proto, UidProto.MODEM_CONTROLLER,
6885 u.getModemControllerActivity(), which);
6886
6887 // Network stats (NETWORK_DATA)
6888 final long nToken = proto.start(UidProto.NETWORK);
6889 proto.write(UidProto.Network.MOBILE_BYTES_RX,
6890 u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
6891 proto.write(UidProto.Network.MOBILE_BYTES_TX,
6892 u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
6893 proto.write(UidProto.Network.WIFI_BYTES_RX,
6894 u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
6895 proto.write(UidProto.Network.WIFI_BYTES_TX,
6896 u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
6897 proto.write(UidProto.Network.BT_BYTES_RX,
6898 u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
6899 proto.write(UidProto.Network.BT_BYTES_TX,
6900 u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
6901 proto.write(UidProto.Network.MOBILE_PACKETS_RX,
6902 u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
6903 proto.write(UidProto.Network.MOBILE_PACKETS_TX,
6904 u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
6905 proto.write(UidProto.Network.WIFI_PACKETS_RX,
6906 u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
6907 proto.write(UidProto.Network.WIFI_PACKETS_TX,
6908 u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
6909 proto.write(UidProto.Network.MOBILE_ACTIVE_DURATION_MS,
6910 roundUsToMs(u.getMobileRadioActiveTime(which)));
6911 proto.write(UidProto.Network.MOBILE_ACTIVE_COUNT,
6912 u.getMobileRadioActiveCount(which));
6913 proto.write(UidProto.Network.MOBILE_WAKEUP_COUNT,
6914 u.getMobileRadioApWakeupCount(which));
6915 proto.write(UidProto.Network.WIFI_WAKEUP_COUNT,
6916 u.getWifiRadioApWakeupCount(which));
6917 proto.write(UidProto.Network.MOBILE_BYTES_BG_RX,
6918 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA, which));
6919 proto.write(UidProto.Network.MOBILE_BYTES_BG_TX,
6920 u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA, which));
6921 proto.write(UidProto.Network.WIFI_BYTES_BG_RX,
6922 u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which));
6923 proto.write(UidProto.Network.WIFI_BYTES_BG_TX,
6924 u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which));
6925 proto.write(UidProto.Network.MOBILE_PACKETS_BG_RX,
6926 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA, which));
6927 proto.write(UidProto.Network.MOBILE_PACKETS_BG_TX,
6928 u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA, which));
6929 proto.write(UidProto.Network.WIFI_PACKETS_BG_RX,
6930 u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA, which));
6931 proto.write(UidProto.Network.WIFI_PACKETS_BG_TX,
6932 u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA, which));
6933 proto.end(nToken);
6934
6935 // Power use item (POWER_USE_ITEM_DATA)
6936 BatterySipper bs = uidToSipper.get(uid);
6937 if (bs != null) {
6938 final long bsToken = proto.start(UidProto.POWER_USE_ITEM);
6939 proto.write(UidProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
6940 proto.write(UidProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
6941 proto.write(UidProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
6942 proto.write(UidProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
6943 bs.proportionalSmearMah);
6944 proto.end(bsToken);
6945 }
6946
6947 // Processes (PROCESS_DATA)
6948 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats =
6949 u.getProcessStats();
6950 for (int ipr = processStats.size() - 1; ipr >= 0; --ipr) {
6951 final Uid.Proc ps = processStats.valueAt(ipr);
6952 final long prToken = proto.start(UidProto.PROCESS);
6953
6954 proto.write(UidProto.Process.NAME, processStats.keyAt(ipr));
6955 proto.write(UidProto.Process.USER_DURATION_MS, ps.getUserTime(which));
6956 proto.write(UidProto.Process.SYSTEM_DURATION_MS, ps.getSystemTime(which));
6957 proto.write(UidProto.Process.FOREGROUND_DURATION_MS, ps.getForegroundTime(which));
6958 proto.write(UidProto.Process.START_COUNT, ps.getStarts(which));
6959 proto.write(UidProto.Process.ANR_COUNT, ps.getNumAnrs(which));
6960 proto.write(UidProto.Process.CRASH_COUNT, ps.getNumCrashes(which));
6961
6962 proto.end(prToken);
6963 }
6964
6965 // Sensors (SENSOR_DATA)
6966 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
6967 for (int ise = 0; ise < sensors.size(); ++ise) {
6968 final Uid.Sensor se = sensors.valueAt(ise);
6969 final Timer timer = se.getSensorTime();
6970 if (timer == null) {
6971 continue;
6972 }
6973 final Timer bgTimer = se.getSensorBackgroundTime();
6974 final int sensorNumber = sensors.keyAt(ise);
6975 final long seToken = proto.start(UidProto.SENSORS);
6976
6977 proto.write(UidProto.Sensor.ID, sensorNumber);
6978 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
6979 dumpTimer(proto, UidProto.Sensor.APPORTIONED, timer, rawRealtimeUs, which);
6980 dumpTimer(proto, UidProto.Sensor.BACKGROUND, bgTimer, rawRealtimeUs, which);
6981
6982 proto.end(seToken);
6983 }
6984
6985 // State times (STATE_TIME_DATA)
6986 for (int ips = 0; ips < Uid.NUM_PROCESS_STATE; ++ips) {
6987 long durMs = roundUsToMs(u.getProcessStateTime(ips, rawRealtimeUs, which));
6988 if (durMs == 0) {
6989 continue;
6990 }
6991 final long stToken = proto.start(UidProto.STATES);
6992 proto.write(UidProto.StateTime.STATE, ips);
6993 proto.write(UidProto.StateTime.DURATION_MS, durMs);
6994 proto.end(stToken);
6995 }
6996
6997 // Syncs (SYNC_DATA)
6998 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
6999 for (int isy = syncs.size() - 1; isy >= 0; --isy) {
7000 final Timer timer = syncs.valueAt(isy);
7001 final Timer bgTimer = timer.getSubTimer();
7002 final long syToken = proto.start(UidProto.SYNCS);
7003
7004 proto.write(UidProto.Sync.NAME, syncs.keyAt(isy));
7005 // Background uses totalDurationMsLocked, while total uses totalTimeLocked
7006 dumpTimer(proto, UidProto.Sync.TOTAL, timer, rawRealtimeUs, which);
7007 dumpTimer(proto, UidProto.Sync.BACKGROUND, bgTimer, rawRealtimeUs, which);
7008
7009 proto.end(syToken);
7010 }
7011
7012 // User activity (USER_ACTIVITY_DATA)
7013 if (u.hasUserActivity()) {
7014 for (int i = 0; i < Uid.NUM_USER_ACTIVITY_TYPES; ++i) {
7015 int val = u.getUserActivityCount(i, which);
7016 if (val != 0) {
7017 final long uaToken = proto.start(UidProto.USER_ACTIVITY);
7018 proto.write(UidProto.UserActivity.NAME, i);
7019 proto.write(UidProto.UserActivity.COUNT, val);
7020 proto.end(uaToken);
7021 }
7022 }
7023 }
7024
7025 // Vibrator (VIBRATOR_DATA)
7026 dumpTimer(proto, UidProto.VIBRATOR, u.getVibratorOnTimer(), rawRealtimeUs, which);
7027
7028 // Video (VIDEO_DATA)
7029 dumpTimer(proto, UidProto.VIDEO, u.getVideoTurnedOnTimer(), rawRealtimeUs, which);
7030
7031 // Wakelocks (WAKELOCK_DATA)
7032 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
7033 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7034 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7035 final long wToken = proto.start(UidProto.WAKELOCKS);
7036 proto.write(UidProto.Wakelock.NAME, wakelocks.keyAt(iw));
7037 dumpTimer(proto, UidProto.Wakelock.FULL, wl.getWakeTime(WAKE_TYPE_FULL),
7038 rawRealtimeUs, which);
7039 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7040 if (pTimer != null) {
7041 dumpTimer(proto, UidProto.Wakelock.PARTIAL, pTimer, rawRealtimeUs, which);
7042 dumpTimer(proto, UidProto.Wakelock.BACKGROUND_PARTIAL, pTimer.getSubTimer(),
7043 rawRealtimeUs, which);
7044 }
7045 dumpTimer(proto, UidProto.Wakelock.WINDOW, wl.getWakeTime(WAKE_TYPE_WINDOW),
7046 rawRealtimeUs, which);
7047 proto.end(wToken);
7048 }
7049
7050 // Wakeup alarms (WAKEUP_ALARM_DATA)
7051 for (int ipkg = packageStats.size() - 1; ipkg >= 0; --ipkg) {
7052 final Uid.Pkg ps = packageStats.valueAt(ipkg);
7053 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
7054 for (int iwa = alarms.size() - 1; iwa >= 0; --iwa) {
7055 final long waToken = proto.start(UidProto.WAKEUP_ALARM);
7056 proto.write(UidProto.WakeupAlarm.NAME, alarms.keyAt(iwa));
7057 proto.write(UidProto.WakeupAlarm.COUNT,
7058 alarms.valueAt(iwa).getCountLocked(which));
7059 proto.end(waToken);
7060 }
7061 }
7062
7063 // Wifi Controller (WIFI_CONTROLLER_DATA)
7064 dumpControllerActivityProto(proto, UidProto.WIFI_CONTROLLER,
7065 u.getWifiControllerActivity(), which);
7066
7067 // Wifi data (WIFI_DATA)
7068 final long wToken = proto.start(UidProto.WIFI);
7069 proto.write(UidProto.Wifi.FULL_WIFI_LOCK_DURATION_MS,
7070 roundUsToMs(u.getFullWifiLockTime(rawRealtimeUs, which)));
7071 dumpTimer(proto, UidProto.Wifi.APPORTIONED_SCAN, u.getWifiScanTimer(),
7072 rawRealtimeUs, which);
7073 proto.write(UidProto.Wifi.RUNNING_DURATION_MS,
7074 roundUsToMs(u.getWifiRunningTime(rawRealtimeUs, which)));
7075 dumpTimer(proto, UidProto.Wifi.BACKGROUND_SCAN, u.getWifiScanBackgroundTimer(),
7076 rawRealtimeUs, which);
7077 proto.end(wToken);
7078
7079 proto.end(uTkn);
7080 }
7081 }
7082
7083 private void dumpProtoSystemLocked(ProtoOutputStream proto, BatteryStatsHelper helper) {
Kweku Adams87b19ec2017-10-09 12:40:03 -07007084 final long sToken = proto.start(BatteryStatsProto.SYSTEM);
7085 final long rawUptimeUs = SystemClock.uptimeMillis() * 1000;
7086 final long rawRealtimeMs = SystemClock.elapsedRealtime();
7087 final long rawRealtimeUs = rawRealtimeMs * 1000;
7088 final int which = STATS_SINCE_CHARGED;
7089
7090 // Battery data (BATTERY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007091 final long bToken = proto.start(SystemProto.BATTERY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007092 proto.write(SystemProto.Battery.START_CLOCK_TIME_MS, getStartClockTime());
7093 proto.write(SystemProto.Battery.START_COUNT, getStartCount());
7094 proto.write(SystemProto.Battery.TOTAL_REALTIME_MS,
7095 computeRealtime(rawRealtimeUs, which) / 1000);
7096 proto.write(SystemProto.Battery.TOTAL_UPTIME_MS,
7097 computeUptime(rawUptimeUs, which) / 1000);
7098 proto.write(SystemProto.Battery.BATTERY_REALTIME_MS,
7099 computeBatteryRealtime(rawRealtimeUs, which) / 1000);
7100 proto.write(SystemProto.Battery.BATTERY_UPTIME_MS,
7101 computeBatteryUptime(rawUptimeUs, which) / 1000);
7102 proto.write(SystemProto.Battery.SCREEN_OFF_REALTIME_MS,
7103 computeBatteryScreenOffRealtime(rawRealtimeUs, which) / 1000);
7104 proto.write(SystemProto.Battery.SCREEN_OFF_UPTIME_MS,
7105 computeBatteryScreenOffUptime(rawUptimeUs, which) / 1000);
7106 proto.write(SystemProto.Battery.SCREEN_DOZE_DURATION_MS,
7107 getScreenDozeTime(rawRealtimeUs, which) / 1000);
7108 proto.write(SystemProto.Battery.ESTIMATED_BATTERY_CAPACITY_MAH,
7109 getEstimatedBatteryCapacity());
7110 proto.write(SystemProto.Battery.MIN_LEARNED_BATTERY_CAPACITY_UAH,
7111 getMinLearnedBatteryCapacity());
7112 proto.write(SystemProto.Battery.MAX_LEARNED_BATTERY_CAPACITY_UAH,
7113 getMaxLearnedBatteryCapacity());
Kweku Adams103351f2017-10-16 14:39:34 -07007114 proto.end(bToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007115
7116 // Battery discharge (BATTERY_DISCHARGE_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007117 final long bdToken = proto.start(SystemProto.BATTERY_DISCHARGE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007118 proto.write(SystemProto.BatteryDischarge.LOWER_BOUND_SINCE_CHARGE,
7119 getLowDischargeAmountSinceCharge());
7120 proto.write(SystemProto.BatteryDischarge.UPPER_BOUND_SINCE_CHARGE,
7121 getHighDischargeAmountSinceCharge());
7122 proto.write(SystemProto.BatteryDischarge.SCREEN_ON_SINCE_CHARGE,
7123 getDischargeAmountScreenOnSinceCharge());
7124 proto.write(SystemProto.BatteryDischarge.SCREEN_OFF_SINCE_CHARGE,
7125 getDischargeAmountScreenOffSinceCharge());
7126 proto.write(SystemProto.BatteryDischarge.SCREEN_DOZE_SINCE_CHARGE,
7127 getDischargeAmountScreenDozeSinceCharge());
7128 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH,
7129 getUahDischarge(which) / 1000);
7130 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_OFF,
7131 getUahDischargeScreenOff(which) / 1000);
7132 proto.write(SystemProto.BatteryDischarge.TOTAL_MAH_SCREEN_DOZE,
7133 getUahDischargeScreenDoze(which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007134 proto.end(bdToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007135
7136 // Time remaining
7137 long timeRemainingUs = computeChargeTimeRemaining(rawRealtimeUs);
Kweku Adams103351f2017-10-16 14:39:34 -07007138 // These are part of a oneof, so we should only set one of them.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007139 if (timeRemainingUs >= 0) {
7140 // Charge time remaining (CHARGE_TIME_REMAIN_DATA)
7141 proto.write(SystemProto.CHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7142 } else {
7143 timeRemainingUs = computeBatteryTimeRemaining(rawRealtimeUs);
7144 // Discharge time remaining (DISCHARGE_TIME_REMAIN_DATA)
7145 if (timeRemainingUs >= 0) {
7146 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, timeRemainingUs / 1000);
7147 } else {
7148 proto.write(SystemProto.DISCHARGE_TIME_REMAINING_MS, -1);
7149 }
7150 }
7151
7152 // Charge step (CHARGE_STEP_DATA)
7153 dumpDurationSteps(proto, SystemProto.CHARGE_STEP, getChargeLevelStepTracker());
7154
7155 // Phone data connection (DATA_CONNECTION_TIME_DATA and DATA_CONNECTION_COUNT_DATA)
7156 for (int i = 0; i < NUM_DATA_CONNECTION_TYPES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007157 final long pdcToken = proto.start(SystemProto.DATA_CONNECTION);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007158 proto.write(SystemProto.DataConnection.NAME, i);
7159 dumpTimer(proto, SystemProto.DataConnection.TOTAL, getPhoneDataConnectionTimer(i),
7160 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007161 proto.end(pdcToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007162 }
7163
7164 // Discharge step (DISCHARGE_STEP_DATA)
7165 dumpDurationSteps(proto, SystemProto.DISCHARGE_STEP, getDischargeLevelStepTracker());
7166
7167 // CPU frequencies (GLOBAL_CPU_FREQ_DATA)
7168 final long[] cpuFreqs = getCpuFreqs();
7169 if (cpuFreqs != null) {
7170 for (long i : cpuFreqs) {
7171 proto.write(SystemProto.CPU_FREQUENCY, i);
7172 }
7173 }
7174
7175 // Bluetooth controller (GLOBAL_BLUETOOTH_CONTROLLER_DATA)
7176 dumpControllerActivityProto(proto, SystemProto.GLOBAL_BLUETOOTH_CONTROLLER,
7177 getBluetoothControllerActivity(), which);
7178
7179 // Modem controller (GLOBAL_MODEM_CONTROLLER_DATA)
7180 dumpControllerActivityProto(proto, SystemProto.GLOBAL_MODEM_CONTROLLER,
7181 getModemControllerActivity(), which);
7182
7183 // Global network data (GLOBAL_NETWORK_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007184 final long gnToken = proto.start(SystemProto.GLOBAL_NETWORK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007185 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_RX,
7186 getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which));
7187 proto.write(SystemProto.GlobalNetwork.MOBILE_BYTES_TX,
7188 getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which));
7189 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_RX,
7190 getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which));
7191 proto.write(SystemProto.GlobalNetwork.MOBILE_PACKETS_TX,
7192 getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which));
7193 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_RX,
7194 getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which));
7195 proto.write(SystemProto.GlobalNetwork.WIFI_BYTES_TX,
7196 getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which));
7197 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_RX,
7198 getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which));
7199 proto.write(SystemProto.GlobalNetwork.WIFI_PACKETS_TX,
7200 getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which));
7201 proto.write(SystemProto.GlobalNetwork.BT_BYTES_RX,
7202 getNetworkActivityBytes(NETWORK_BT_RX_DATA, which));
7203 proto.write(SystemProto.GlobalNetwork.BT_BYTES_TX,
7204 getNetworkActivityBytes(NETWORK_BT_TX_DATA, which));
Kweku Adams103351f2017-10-16 14:39:34 -07007205 proto.end(gnToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007206
7207 // Wifi controller (GLOBAL_WIFI_CONTROLLER_DATA)
7208 dumpControllerActivityProto(proto, SystemProto.GLOBAL_WIFI_CONTROLLER,
7209 getWifiControllerActivity(), which);
7210
7211
7212 // Global wifi (GLOBAL_WIFI_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007213 final long gwToken = proto.start(SystemProto.GLOBAL_WIFI);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007214 proto.write(SystemProto.GlobalWifi.ON_DURATION_MS,
7215 getWifiOnTime(rawRealtimeUs, which) / 1000);
7216 proto.write(SystemProto.GlobalWifi.RUNNING_DURATION_MS,
7217 getGlobalWifiRunningTime(rawRealtimeUs, which) / 1000);
Kweku Adams103351f2017-10-16 14:39:34 -07007218 proto.end(gwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007219
7220 // Kernel wakelock (KERNEL_WAKELOCK_DATA)
7221 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
7222 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007223 final long kwToken = proto.start(SystemProto.KERNEL_WAKELOCK);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007224 proto.write(SystemProto.KernelWakelock.NAME, ent.getKey());
7225 dumpTimer(proto, SystemProto.KernelWakelock.TOTAL, ent.getValue(),
7226 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007227 proto.end(kwToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007228 }
7229
7230 // Misc (MISC_DATA)
7231 // Calculate wakelock times across all uids.
7232 long fullWakeLockTimeTotalUs = 0;
7233 long partialWakeLockTimeTotalUs = 0;
7234
7235 final SparseArray<? extends Uid> uidStats = getUidStats();
7236 for (int iu = 0; iu < uidStats.size(); iu++) {
7237 final Uid u = uidStats.valueAt(iu);
7238
7239 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks =
7240 u.getWakelockStats();
7241 for (int iw = wakelocks.size() - 1; iw >= 0; --iw) {
7242 final Uid.Wakelock wl = wakelocks.valueAt(iw);
7243
7244 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
7245 if (fullWakeTimer != null) {
7246 fullWakeLockTimeTotalUs += fullWakeTimer.getTotalTimeLocked(rawRealtimeUs,
7247 which);
7248 }
7249
7250 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
7251 if (partialWakeTimer != null) {
7252 partialWakeLockTimeTotalUs += partialWakeTimer.getTotalTimeLocked(
7253 rawRealtimeUs, which);
7254 }
7255 }
7256 }
Kweku Adams103351f2017-10-16 14:39:34 -07007257 final long mToken = proto.start(SystemProto.MISC);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007258 proto.write(SystemProto.Misc.SCREEN_ON_DURATION_MS,
7259 getScreenOnTime(rawRealtimeUs, which) / 1000);
7260 proto.write(SystemProto.Misc.PHONE_ON_DURATION_MS,
7261 getPhoneOnTime(rawRealtimeUs, which) / 1000);
7262 proto.write(SystemProto.Misc.FULL_WAKELOCK_TOTAL_DURATION_MS,
7263 fullWakeLockTimeTotalUs / 1000);
7264 proto.write(SystemProto.Misc.PARTIAL_WAKELOCK_TOTAL_DURATION_MS,
7265 partialWakeLockTimeTotalUs / 1000);
7266 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_DURATION_MS,
7267 getMobileRadioActiveTime(rawRealtimeUs, which) / 1000);
7268 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_ADJUSTED_TIME_MS,
7269 getMobileRadioActiveAdjustedTime(which) / 1000);
7270 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_COUNT,
7271 getMobileRadioActiveCount(which));
7272 proto.write(SystemProto.Misc.MOBILE_RADIO_ACTIVE_UNKNOWN_DURATION_MS,
7273 getMobileRadioActiveUnknownTime(which) / 1000);
7274 proto.write(SystemProto.Misc.INTERACTIVE_DURATION_MS,
7275 getInteractiveTime(rawRealtimeUs, which) / 1000);
7276 proto.write(SystemProto.Misc.BATTERY_SAVER_MODE_ENABLED_DURATION_MS,
7277 getPowerSaveModeEnabledTime(rawRealtimeUs, which) / 1000);
7278 proto.write(SystemProto.Misc.NUM_CONNECTIVITY_CHANGES,
7279 getNumConnectivityChange(which));
7280 proto.write(SystemProto.Misc.DEEP_DOZE_ENABLED_DURATION_MS,
7281 getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7282 proto.write(SystemProto.Misc.DEEP_DOZE_COUNT,
7283 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
7284 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_DURATION_MS,
7285 getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP, rawRealtimeUs, which) / 1000);
7286 proto.write(SystemProto.Misc.DEEP_DOZE_IDLING_COUNT,
7287 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
7288 proto.write(SystemProto.Misc.LONGEST_DEEP_DOZE_DURATION_MS,
7289 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
7290 proto.write(SystemProto.Misc.LIGHT_DOZE_ENABLED_DURATION_MS,
7291 getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7292 proto.write(SystemProto.Misc.LIGHT_DOZE_COUNT,
7293 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
7294 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_DURATION_MS,
7295 getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT, rawRealtimeUs, which) / 1000);
7296 proto.write(SystemProto.Misc.LIGHT_DOZE_IDLING_COUNT,
7297 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
7298 proto.write(SystemProto.Misc.LONGEST_LIGHT_DOZE_DURATION_MS,
7299 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
Kweku Adams103351f2017-10-16 14:39:34 -07007300 proto.end(mToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007301
7302 // Power use item (POWER_USE_ITEM_DATA)
7303 final List<BatterySipper> sippers = helper.getUsageList();
7304 if (sippers != null) {
7305 for (int i = 0; i < sippers.size(); ++i) {
7306 final BatterySipper bs = sippers.get(i);
7307 int n = SystemProto.PowerUseItem.UNKNOWN_SIPPER;
7308 int uid = 0;
7309 switch (bs.drainType) {
7310 case IDLE:
7311 n = SystemProto.PowerUseItem.IDLE;
7312 break;
7313 case CELL:
7314 n = SystemProto.PowerUseItem.CELL;
7315 break;
7316 case PHONE:
7317 n = SystemProto.PowerUseItem.PHONE;
7318 break;
7319 case WIFI:
7320 n = SystemProto.PowerUseItem.WIFI;
7321 break;
7322 case BLUETOOTH:
7323 n = SystemProto.PowerUseItem.BLUETOOTH;
7324 break;
7325 case SCREEN:
7326 n = SystemProto.PowerUseItem.SCREEN;
7327 break;
7328 case FLASHLIGHT:
7329 n = SystemProto.PowerUseItem.FLASHLIGHT;
7330 break;
7331 case APP:
Kweku Adams103351f2017-10-16 14:39:34 -07007332 // dumpProtoAppsLocked will handle this.
Kweku Adams87b19ec2017-10-09 12:40:03 -07007333 continue;
7334 case USER:
7335 n = SystemProto.PowerUseItem.USER;
7336 uid = UserHandle.getUid(bs.userId, 0);
7337 break;
7338 case UNACCOUNTED:
7339 n = SystemProto.PowerUseItem.UNACCOUNTED;
7340 break;
7341 case OVERCOUNTED:
7342 n = SystemProto.PowerUseItem.OVERCOUNTED;
7343 break;
7344 case CAMERA:
7345 n = SystemProto.PowerUseItem.CAMERA;
7346 break;
7347 case MEMORY:
7348 n = SystemProto.PowerUseItem.MEMORY;
7349 break;
7350 }
Kweku Adams103351f2017-10-16 14:39:34 -07007351 final long puiToken = proto.start(SystemProto.POWER_USE_ITEM);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007352 proto.write(SystemProto.PowerUseItem.NAME, n);
7353 proto.write(SystemProto.PowerUseItem.UID, uid);
7354 proto.write(SystemProto.PowerUseItem.COMPUTED_POWER_MAH, bs.totalPowerMah);
7355 proto.write(SystemProto.PowerUseItem.SHOULD_HIDE, bs.shouldHide);
7356 proto.write(SystemProto.PowerUseItem.SCREEN_POWER_MAH, bs.screenPowerMah);
7357 proto.write(SystemProto.PowerUseItem.PROPORTIONAL_SMEAR_MAH,
7358 bs.proportionalSmearMah);
Kweku Adams103351f2017-10-16 14:39:34 -07007359 proto.end(puiToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007360 }
7361 }
7362
7363 // Power use summary (POWER_USE_SUMMARY_DATA)
Kweku Adams103351f2017-10-16 14:39:34 -07007364 final long pusToken = proto.start(SystemProto.POWER_USE_SUMMARY);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007365 proto.write(SystemProto.PowerUseSummary.BATTERY_CAPACITY_MAH,
7366 helper.getPowerProfile().getBatteryCapacity());
7367 proto.write(SystemProto.PowerUseSummary.COMPUTED_POWER_MAH, helper.getComputedPower());
7368 proto.write(SystemProto.PowerUseSummary.MIN_DRAINED_POWER_MAH, helper.getMinDrainedPower());
7369 proto.write(SystemProto.PowerUseSummary.MAX_DRAINED_POWER_MAH, helper.getMaxDrainedPower());
Kweku Adams103351f2017-10-16 14:39:34 -07007370 proto.end(pusToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007371
7372 // RPM stats (RESOURCE_POWER_MANAGER_DATA)
7373 final Map<String, ? extends Timer> rpmStats = getRpmStats();
7374 final Map<String, ? extends Timer> screenOffRpmStats = getScreenOffRpmStats();
7375 for (Map.Entry<String, ? extends Timer> ent : rpmStats.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007376 final long rpmToken = proto.start(SystemProto.RESOURCE_POWER_MANAGER);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007377 proto.write(SystemProto.ResourcePowerManager.NAME, ent.getKey());
7378 dumpTimer(proto, SystemProto.ResourcePowerManager.TOTAL,
7379 ent.getValue(), rawRealtimeUs, which);
7380 dumpTimer(proto, SystemProto.ResourcePowerManager.SCREEN_OFF,
7381 screenOffRpmStats.get(ent.getKey()), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007382 proto.end(rpmToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007383 }
7384
7385 // Screen brightness (SCREEN_BRIGHTNESS_DATA)
7386 for (int i = 0; i < NUM_SCREEN_BRIGHTNESS_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007387 final long sbToken = proto.start(SystemProto.SCREEN_BRIGHTNESS);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007388 proto.write(SystemProto.ScreenBrightness.NAME, i);
7389 dumpTimer(proto, SystemProto.ScreenBrightness.TOTAL, getScreenBrightnessTimer(i),
7390 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007391 proto.end(sbToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007392 }
7393
7394 // Signal scanning time (SIGNAL_SCANNING_TIME_DATA)
7395 dumpTimer(proto, SystemProto.SIGNAL_SCANNING, getPhoneSignalScanningTimer(), rawRealtimeUs,
7396 which);
7397
7398 // Phone signal strength (SIGNAL_STRENGTH_TIME_DATA and SIGNAL_STRENGTH_COUNT_DATA)
7399 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007400 final long pssToken = proto.start(SystemProto.PHONE_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007401 proto.write(SystemProto.PhoneSignalStrength.NAME, i);
7402 dumpTimer(proto, SystemProto.PhoneSignalStrength.TOTAL, getPhoneSignalStrengthTimer(i),
7403 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007404 proto.end(pssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007405 }
7406
7407 // Wakeup reasons (WAKEUP_REASON_DATA)
7408 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
7409 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Kweku Adams103351f2017-10-16 14:39:34 -07007410 final long wrToken = proto.start(SystemProto.WAKEUP_REASON);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007411 proto.write(SystemProto.WakeupReason.NAME, ent.getKey());
7412 dumpTimer(proto, SystemProto.WakeupReason.TOTAL, ent.getValue(), rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007413 proto.end(wrToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007414 }
7415
7416 // Wifi signal strength (WIFI_SIGNAL_STRENGTH_TIME_DATA and WIFI_SIGNAL_STRENGTH_COUNT_DATA)
7417 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007418 final long wssToken = proto.start(SystemProto.WIFI_SIGNAL_STRENGTH);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007419 proto.write(SystemProto.WifiSignalStrength.NAME, i);
7420 dumpTimer(proto, SystemProto.WifiSignalStrength.TOTAL, getWifiSignalStrengthTimer(i),
7421 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007422 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007423 }
7424
7425 // Wifi state (WIFI_STATE_TIME_DATA and WIFI_STATE_COUNT_DATA)
7426 for (int i = 0; i < NUM_WIFI_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007427 final long wsToken = proto.start(SystemProto.WIFI_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007428 proto.write(SystemProto.WifiState.NAME, i);
7429 dumpTimer(proto, SystemProto.WifiState.TOTAL, getWifiStateTimer(i),
7430 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007431 proto.end(wsToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007432 }
7433
7434 // Wifi supplicant state (WIFI_SUPPL_STATE_TIME_DATA and WIFI_SUPPL_STATE_COUNT_DATA)
7435 for (int i = 0; i < NUM_WIFI_SUPPL_STATES; ++i) {
Kweku Adams103351f2017-10-16 14:39:34 -07007436 final long wssToken = proto.start(SystemProto.WIFI_SUPPLICANT_STATE);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007437 proto.write(SystemProto.WifiSupplicantState.NAME, i);
7438 dumpTimer(proto, SystemProto.WifiSupplicantState.TOTAL, getWifiSupplStateTimer(i),
7439 rawRealtimeUs, which);
Kweku Adams103351f2017-10-16 14:39:34 -07007440 proto.end(wssToken);
Kweku Adams87b19ec2017-10-09 12:40:03 -07007441 }
7442
7443 proto.end(sToken);
7444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007445}