blob: 580223dd2b671b6577d067d08671615f2bce87b9 [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
19import java.io.PrintWriter;
Dianne Hackborne4a59512010-12-07 11:08:07 -080020import java.util.ArrayList;
Dianne Hackborn81038902012-11-26 17:04:09 -080021import java.util.Collections;
22import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import java.util.Formatter;
Dianne Hackborn37de0982014-05-09 09:32:18 -070024import java.util.HashMap;
Dianne Hackborne4a59512010-12-07 11:08:07 -080025import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import java.util.Map;
27
Dianne Hackborna7c837f2014-01-15 16:20:44 -080028import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080029import android.content.pm.ApplicationInfo;
Wink Saville52840902011-02-18 12:40:47 -080030import android.telephony.SignalStrength;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080031import android.text.format.DateFormat;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070032import android.util.ArrayMap;
James Carr2dd7e5e2016-07-20 18:48:39 -070033import android.util.LongSparseArray;
Dianne Hackborn9cfba352016-03-24 17:31:28 -070034import android.util.MutableBoolean;
35import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.util.Printer;
37import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070038import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070039import android.util.TimeUtils;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070040import android.view.Display;
Amith Yamasaniab9ad192016-12-06 12:46:59 -080041
Dianne Hackborna7c837f2014-01-15 16:20:44 -080042import com.android.internal.os.BatterySipper;
43import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
45/**
46 * A class providing access to battery usage statistics, including information on
47 * wakelocks, processes, packages, and services. All times are represented in microseconds
48 * except where indicated otherwise.
49 * @hide
50 */
51public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070052 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
54 private static final boolean LOCAL_LOGV = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070055
56 /** @hide */
57 public static final String SERVICE_NAME = "batterystats";
58
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 /**
60 * A constant indicating a partial wake lock timer.
61 */
62 public static final int WAKE_TYPE_PARTIAL = 0;
63
64 /**
65 * A constant indicating a full wake lock timer.
66 */
67 public static final int WAKE_TYPE_FULL = 1;
68
69 /**
70 * A constant indicating a window wake lock timer.
71 */
72 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 /**
75 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 */
77 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070078
79 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070080 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070081 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070082 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070083
84 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070085 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070086 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070087 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070088
89 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070090 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070091 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070092 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
Dianne Hackborn62793e42015-03-09 11:15:41 -070094 /**
95 * A constant indicating a wifi multicast timer
96 */
97 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -070098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700100 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700101 */
102 public static final int VIDEO_TURNED_ON = 8;
103
104 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800105 * A constant indicating a vibrator on timer
106 */
107 public static final int VIBRATOR_ON = 9;
108
109 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700110 * A constant indicating a foreground activity timer
111 */
112 public static final int FOREGROUND_ACTIVITY = 10;
113
114 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700115 * A constant indicating a wifi batched scan is active
116 */
117 public static final int WIFI_BATCHED_SCAN = 11;
118
119 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700120 * A constant indicating a process state timer
121 */
122 public static final int PROCESS_STATE = 12;
123
124 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700125 * A constant indicating a sync timer
126 */
127 public static final int SYNC = 13;
128
129 /**
130 * A constant indicating a job timer
131 */
132 public static final int JOB = 14;
133
134 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800135 * A constant indicating an audio turn on timer
136 */
137 public static final int AUDIO_TURNED_ON = 15;
138
139 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700140 * A constant indicating a flashlight turn on timer
141 */
142 public static final int FLASHLIGHT_TURNED_ON = 16;
143
144 /**
145 * A constant indicating a camera turn on timer
146 */
147 public static final int CAMERA_TURNED_ON = 17;
148
149 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700150 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700151 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700152 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700153
154 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800155 * A constant indicating a bluetooth scan timer.
156 */
157 public static final int BLUETOOTH_SCAN_ON = 19;
158
159 /**
Bookatzc8c44962017-05-11 12:12:54 -0700160 * A constant indicating an aggregated partial wake lock timer.
161 */
162 public static final int AGGREGATED_WAKE_TYPE_PARTIAL = 20;
163
164 /**
Bookatzb1f04f32017-05-19 13:57:32 -0700165 * A constant indicating a bluetooth scan timer for unoptimized scans.
166 */
167 public static final int BLUETOOTH_UNOPTIMIZED_SCAN_ON = 21;
168
169 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 * Include all of the data in the stats, including previously saved data.
171 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700172 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173
174 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 * Include only the current run in the stats.
176 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700177 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178
179 /**
180 * Include only the run since the last time the device was unplugged in the stats.
181 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700182 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700183
184 // NOTE: Update this list if you add/change any stats above.
185 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700186 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700187 private static final String[] STAT_NAMES = { "l", "c", "u" };
188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700190 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700191 *
192 * New in version 19:
193 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800194 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700195 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Bookatz506a8182017-05-01 14:18:42 -0700196 * New in version 21:
197 * - Actual (not just apportioned) Wakelock time is also recorded.
Bookatzc8c44962017-05-11 12:12:54 -0700198 * - Aggregated partial wakelock time (per uid, instead of per wakelock) is recorded.
Bookatzb1f04f32017-05-19 13:57:32 -0700199 * - BLE scan result count
200 * - CPU frequency time per uid
201 * New in version 22:
202 * - BLE scan result background count, BLE unoptimized scan time
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700203 */
Bookatzb1f04f32017-05-19 13:57:32 -0700204 static final String CHECKIN_VERSION = "22";
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700205
206 /**
207 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700209 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700210
Evan Millar22ac0432009-03-31 11:33:18 -0700211 private static final long BYTES_PER_KB = 1024;
212 private static final long BYTES_PER_MB = 1048576; // 1024^2
213 private static final long BYTES_PER_GB = 1073741824; //1024^3
Bookatz506a8182017-05-01 14:18:42 -0700214
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700215 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800216 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700217 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700219 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700220 private static final String CPU_DATA = "cpu";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700221 private static final String GLOBAL_CPU_FREQ_DATA = "gcf";
222 private static final String CPU_TIMES_AT_FREQ_DATA = "ctf";
Evan Millare84de8d2009-04-02 22:16:12 -0700223 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800224 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700225 private static final String FOREGROUND_DATA = "fg";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700226 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700227 // wl line is:
228 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
229 // full totalTime, 'f', count, current duration, max duration, total duration,
230 // partial totalTime, 'p', count, current duration, max duration, total duration,
231 // window totalTime, 'w', count, current duration, max duration, total duration
232 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700233 private static final String WAKELOCK_DATA = "wl";
Bookatzc8c44962017-05-11 12:12:54 -0700234 // awl line is:
235 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "awl",
236 // cumulative partial wakelock duration, cumulative background partial wakelock duration
237 private static final String AGGREGATED_WAKELOCK_DATA = "awl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700238 private static final String SYNC_DATA = "sy";
239 private static final String JOB_DATA = "jb";
Evan Millarc64edde2009-04-18 12:26:32 -0700240 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700241 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700242 private static final String NETWORK_DATA = "nt";
243 private static final String USER_ACTIVITY_DATA = "ua";
244 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800245 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700246 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700247 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700248 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800249 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
250 private static final String WIFI_CONTROLLER_DATA = "wfcd";
251 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
252 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700253 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700254 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800255 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800256 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
257 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800258 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700259 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700260 private static final String SCREEN_BRIGHTNESS_DATA = "br";
261 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700262 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700263 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
264 private static final String DATA_CONNECTION_TIME_DATA = "dct";
265 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800266 private static final String WIFI_STATE_TIME_DATA = "wst";
267 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700268 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
269 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
270 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
271 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800272 private static final String POWER_USE_SUMMARY_DATA = "pws";
273 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700274 private static final String DISCHARGE_STEP_DATA = "dsd";
275 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700276 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
277 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700278 private static final String FLASHLIGHT_DATA = "fla";
279 private static final String CAMERA_DATA = "cam";
280 private static final String VIDEO_DATA = "vid";
281 private static final String AUDIO_DATA = "aud";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282
Adam Lesinski010bf372016-04-11 12:18:18 -0700283 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
284
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700285 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 private final Formatter mFormatter = new Formatter(mFormatBuilder);
287
288 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700289 * Indicates times spent by the uid at each cpu frequency in all process states.
290 *
291 * Other types might include times spent in foreground, background etc.
292 */
293 private final String UID_TIMES_TYPE_ALL = "A";
294
295 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700296 * State for keeping track of counting information.
297 */
298 public static abstract class Counter {
299
300 /**
301 * Returns the count associated with this Counter for the
302 * selected type of statistics.
303 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700304 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700305 */
Evan Millarc64edde2009-04-18 12:26:32 -0700306 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700307
308 /**
309 * Temporary for debugging.
310 */
311 public abstract void logState(Printer pw, String prefix);
312 }
313
314 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700315 * State for keeping track of long counting information.
316 */
317 public static abstract class LongCounter {
318
319 /**
320 * Returns the count associated with this Counter for the
321 * selected type of statistics.
322 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700323 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700324 */
325 public abstract long getCountLocked(int which);
326
327 /**
328 * Temporary for debugging.
329 */
330 public abstract void logState(Printer pw, String prefix);
331 }
332
333 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700334 * State for keeping track of array of long counting information.
335 */
336 public static abstract class LongCounterArray {
337 /**
338 * Returns the counts associated with this Counter for the
339 * selected type of statistics.
340 *
341 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
342 */
343 public abstract long[] getCountsLocked(int which);
344
345 /**
346 * Temporary for debugging.
347 */
348 public abstract void logState(Printer pw, String prefix);
349 }
350
351 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800352 * Container class that aggregates counters for transmit, receive, and idle state of a
353 * radio controller.
354 */
355 public static abstract class ControllerActivityCounter {
356 /**
357 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
358 * idle state.
359 */
360 public abstract LongCounter getIdleTimeCounter();
361
362 /**
363 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
364 * receive state.
365 */
366 public abstract LongCounter getRxTimeCounter();
367
368 /**
369 * An array of {@link LongCounter}, representing various transmit levels, where each level
370 * may draw a different amount of power. The levels themselves are controller-specific.
371 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
372 * various transmit level states.
373 */
374 public abstract LongCounter[] getTxTimeCounters();
375
376 /**
377 * @return a non-null {@link LongCounter} representing the power consumed by the controller
378 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
379 * yield a value of 0 if the device doesn't support power calculations.
380 */
381 public abstract LongCounter getPowerCounter();
382 }
383
384 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 * State for keeping track of timing information.
386 */
387 public static abstract class Timer {
388
389 /**
390 * Returns the count associated with this Timer for the
391 * selected type of statistics.
392 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700393 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 */
Evan Millarc64edde2009-04-18 12:26:32 -0700395 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396
397 /**
398 * Returns the total time in microseconds associated with this Timer for the
399 * selected type of statistics.
400 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800401 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700402 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 * @return a time in microseconds
404 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800405 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700408 * Returns the total time in microseconds associated with this Timer since the
409 * 'mark' was last set.
410 *
411 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
412 * @return a time in microseconds
413 */
414 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
415
416 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700417 * Returns the max duration if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800418 * Not all Timer subclasses track the max, total, current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700419
420 */
421 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
422 return -1;
423 }
424
425 /**
426 * Returns the current time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800427 * Not all Timer subclasses track the max, total, current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700428 */
429 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
430 return -1;
431 }
432
433 /**
Bookatz867c0d72017-03-07 18:23:42 -0800434 * Returns the current time the timer has been active, if it is being tracked.
435 *
436 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
437 * been on since reset.
438 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
439 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
440 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
441 * the actual total time.
442 * Not all Timer subclasses track the max, total, current durations.
443 */
444 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
445 return -1;
446 }
447
448 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700449 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
450 * used, for example, for tracking background usage. Secondary timers are never pooled.
451 *
452 * Not all Timer subclasses have a secondary timer; those that don't return null.
453 */
454 public Timer getSubTimer() {
455 return null;
456 }
457
458 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700459 * Returns whether the timer is currently running. Some types of timers
460 * (e.g. BatchTimers) don't know whether the event is currently active,
461 * and report false.
462 */
463 public boolean isRunningLocked() {
464 return false;
465 }
466
467 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 * Temporary for debugging.
469 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700470 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 }
472
473 /**
474 * The statistics associated with a particular uid.
475 */
476 public static abstract class Uid {
477
478 /**
479 * Returns a mapping containing wakelock statistics.
480 *
481 * @return a Map from Strings to Uid.Wakelock objects.
482 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700483 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484
485 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700486 * Returns a mapping containing sync statistics.
487 *
488 * @return a Map from Strings to Timer objects.
489 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700490 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700491
492 /**
493 * Returns a mapping containing scheduled job statistics.
494 *
495 * @return a Map from Strings to Timer objects.
496 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700497 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700498
499 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 * The statistics associated with a particular wake lock.
501 */
502 public static abstract class Wakelock {
503 public abstract Timer getWakeTime(int type);
504 }
505
506 /**
Bookatzc8c44962017-05-11 12:12:54 -0700507 * The cumulative time the uid spent holding any partial wakelocks. This will generally
508 * differ from summing over the Wakelocks in getWakelockStats since the latter may have
509 * wakelocks that overlap in time (and therefore over-counts).
510 */
511 public abstract Timer getAggregatedPartialWakelockTimer();
512
513 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 * Returns a mapping containing sensor statistics.
515 *
516 * @return a Map from Integer sensor ids to Uid.Sensor objects.
517 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700518 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519
520 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700521 * Returns a mapping containing active process data.
522 */
523 public abstract SparseArray<? extends Pid> getPidStats();
Bookatzc8c44962017-05-11 12:12:54 -0700524
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700525 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 * Returns a mapping containing process statistics.
527 *
528 * @return a Map from Strings to Uid.Proc objects.
529 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700530 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531
532 /**
533 * Returns a mapping containing package statistics.
534 *
535 * @return a Map from Strings to Uid.Pkg objects.
536 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700537 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700538
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800539 public abstract ControllerActivityCounter getWifiControllerActivity();
540 public abstract ControllerActivityCounter getBluetoothControllerActivity();
541 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800542
543 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 * {@hide}
545 */
546 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700547
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800548 public abstract void noteWifiRunningLocked(long elapsedRealtime);
549 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
550 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
551 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
552 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
553 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
554 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
555 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
556 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
557 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800558 public abstract void noteActivityResumedLocked(long elapsedRealtime);
559 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800560 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
561 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
562 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700563 public abstract int getWifiScanCount(int which);
Bookatz867c0d72017-03-07 18:23:42 -0800564 public abstract int getWifiScanBackgroundCount(int which);
565 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
566 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800567 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700568 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800569 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700570 public abstract Timer getAudioTurnedOnTimer();
571 public abstract Timer getVideoTurnedOnTimer();
572 public abstract Timer getFlashlightTurnedOnTimer();
573 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700574 public abstract Timer getForegroundActivityTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800575 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800576 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatzb1f04f32017-05-19 13:57:32 -0700577 public abstract Timer getBluetoothUnoptimizedScanTimer();
578 public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700579 public abstract Counter getBluetoothScanResultCounter();
Bookatzb1f04f32017-05-19 13:57:32 -0700580 public abstract Counter getBluetoothScanResultBgCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700581
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700582 public abstract long[] getCpuFreqTimes(int which);
583 public abstract long[] getScreenOffCpuFreqTimes(int which);
584
Dianne Hackborna0200e32016-03-30 18:01:41 -0700585 // Note: the following times are disjoint. They can be added together to find the
586 // total time a uid has had any processes running at all.
587
588 /**
589 * Time this uid has any processes in the top state (or above such as persistent).
590 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800591 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700592 /**
593 * Time this uid has any process with a started out bound foreground service, but
594 * none in the "top" state.
595 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800596 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700597 /**
598 * Time this uid has any process that is top while the device is sleeping, but none
599 * in the "foreground service" or better state.
600 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800601 public static final int PROCESS_STATE_TOP_SLEEPING = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700602 /**
603 * Time this uid has any process in an active foreground state, but none in the
604 * "top sleeping" or better state.
605 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800606 public static final int PROCESS_STATE_FOREGROUND = 3;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700607 /**
608 * Time this uid has any process in an active background state, but none in the
609 * "foreground" or better state.
610 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800611 public static final int PROCESS_STATE_BACKGROUND = 4;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700612 /**
613 * Time this uid has any processes that are sitting around cached, not in one of the
614 * other active states.
615 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800616 public static final int PROCESS_STATE_CACHED = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700617 /**
618 * Total number of process states we track.
619 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800620 public static final int NUM_PROCESS_STATE = 6;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700621
622 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackborna8d10942015-11-19 17:55:19 -0800623 "Top", "Fg Service", "Top Sleeping", "Foreground", "Background", "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700624 };
625
626 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800627 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700628
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800629 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630
Robert Greenwalta029ea12013-09-25 16:38:12 -0700631 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
632
Dianne Hackborn617f8772009-03-31 15:04:46 -0700633 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700634 * Note that these must match the constants in android.os.PowerManager.
635 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
636 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700637 */
638 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700639 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700640 };
Bookatzc8c44962017-05-11 12:12:54 -0700641
Phil Weaverda80d672016-03-15 16:25:46 -0700642 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700643
Dianne Hackborn617f8772009-03-31 15:04:46 -0700644 public abstract void noteUserActivityLocked(int type);
645 public abstract boolean hasUserActivity();
646 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700647
648 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800649 public abstract long getNetworkActivityBytes(int type, int which);
650 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800651 public abstract long getMobileRadioActiveTime(int which);
652 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700653
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700654 /**
655 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
656 */
657 public abstract long getUserCpuTimeUs(int which);
658
659 /**
660 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
661 */
662 public abstract long getSystemCpuTimeUs(int which);
663
664 /**
Adam Lesinski6832f392015-09-05 18:05:40 -0700665 * Returns the approximate cpu time (in milliseconds) spent at a certain CPU speed for a
666 * given CPU cluster.
667 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700668 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700669 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700670 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
671 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700672 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700673 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700674
Adam Lesinski5f056f62016-07-14 16:56:08 -0700675 /**
676 * Returns the number of times this UID woke up the Application Processor to
677 * process a mobile radio packet.
678 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
679 */
680 public abstract long getMobileRadioApWakeupCount(int which);
681
682 /**
683 * Returns the number of times this UID woke up the Application Processor to
684 * process a WiFi packet.
685 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
686 */
687 public abstract long getWifiRadioApWakeupCount(int which);
688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800690 /*
691 * FIXME: it's not correct to use this magic value because it
692 * could clash with a sensor handle (which are defined by
693 * the sensor HAL, and therefore out of our control
694 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 // Magic sensor number for the GPS.
696 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800701
Bookatz867c0d72017-03-07 18:23:42 -0800702 /** Returns a Timer for sensor usage when app is in the background. */
703 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 }
705
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700706 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800707 public int mWakeNesting;
708 public long mWakeSumMs;
709 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700710 }
711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 /**
713 * The statistics associated with a particular process.
714 */
715 public static abstract class Proc {
716
Dianne Hackborn287952c2010-09-22 22:34:31 -0700717 public static class ExcessivePower {
718 public static final int TYPE_WAKE = 1;
719 public static final int TYPE_CPU = 2;
720
721 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700722 public long overTime;
723 public long usedTime;
724 }
725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800727 * Returns true if this process is still active in the battery stats.
728 */
729 public abstract boolean isActive();
730
731 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700732 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700734 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 */
736 public abstract long getUserTime(int which);
737
738 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700739 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700741 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 */
743 public abstract long getSystemTime(int which);
744
745 /**
746 * Returns the number of times the process has been started.
747 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700748 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 */
750 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700751
752 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800753 * Returns the number of times the process has crashed.
754 *
755 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
756 */
757 public abstract int getNumCrashes(int which);
758
759 /**
760 * Returns the number of times the process has ANRed.
761 *
762 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
763 */
764 public abstract int getNumAnrs(int which);
765
766 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700767 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700768 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700769 * @return foreground cpu time in microseconds
770 */
771 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700772
Dianne Hackborn287952c2010-09-22 22:34:31 -0700773 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700774
Dianne Hackborn287952c2010-09-22 22:34:31 -0700775 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 }
777
778 /**
779 * The statistics associated with a particular package.
780 */
781 public static abstract class Pkg {
782
783 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700784 * Returns information about all wakeup alarms that have been triggered for this
785 * package. The mapping keys are tag names for the alarms, the counter contains
786 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700788 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789
790 /**
791 * Returns a mapping containing service statistics.
792 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700793 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794
795 /**
796 * The statistics associated with a particular service.
797 */
Joe Onoratoabded112016-02-08 16:49:39 -0800798 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799
800 /**
801 * Returns the amount of time spent started.
802 *
803 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700804 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 * @return
806 */
807 public abstract long getStartTime(long batteryUptime, int which);
808
809 /**
810 * Returns the total number of times startService() has been called.
811 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700812 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 */
814 public abstract int getStarts(int which);
815
816 /**
817 * Returns the total number times the service has been launched.
818 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700819 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 */
821 public abstract int getLaunches(int which);
822 }
823 }
824 }
825
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800826 public static final class LevelStepTracker {
827 public long mLastStepTime = -1;
828 public int mNumStepDurations;
829 public final long[] mStepDurations;
830
831 public LevelStepTracker(int maxLevelSteps) {
832 mStepDurations = new long[maxLevelSteps];
833 }
834
835 public LevelStepTracker(int numSteps, long[] steps) {
836 mNumStepDurations = numSteps;
837 mStepDurations = new long[numSteps];
838 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
839 }
840
841 public long getDurationAt(int index) {
842 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
843 }
844
845 public int getLevelAt(int index) {
846 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
847 >> STEP_LEVEL_LEVEL_SHIFT);
848 }
849
850 public int getInitModeAt(int index) {
851 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
852 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
853 }
854
855 public int getModModeAt(int index) {
856 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
857 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
858 }
859
860 private void appendHex(long val, int topOffset, StringBuilder out) {
861 boolean hasData = false;
862 while (topOffset >= 0) {
863 int digit = (int)( (val>>topOffset) & 0xf );
864 topOffset -= 4;
865 if (!hasData && digit == 0) {
866 continue;
867 }
868 hasData = true;
869 if (digit >= 0 && digit <= 9) {
870 out.append((char)('0' + digit));
871 } else {
872 out.append((char)('a' + digit - 10));
873 }
874 }
875 }
876
877 public void encodeEntryAt(int index, StringBuilder out) {
878 long item = mStepDurations[index];
879 long duration = item & STEP_LEVEL_TIME_MASK;
880 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
881 >> STEP_LEVEL_LEVEL_SHIFT);
882 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
883 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
884 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
885 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
886 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
887 case Display.STATE_OFF: out.append('f'); break;
888 case Display.STATE_ON: out.append('o'); break;
889 case Display.STATE_DOZE: out.append('d'); break;
890 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
891 }
892 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
893 out.append('p');
894 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700895 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
896 out.append('i');
897 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800898 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
899 case Display.STATE_OFF: out.append('F'); break;
900 case Display.STATE_ON: out.append('O'); break;
901 case Display.STATE_DOZE: out.append('D'); break;
902 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
903 }
904 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
905 out.append('P');
906 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700907 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
908 out.append('I');
909 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800910 out.append('-');
911 appendHex(level, 4, out);
912 out.append('-');
913 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
914 }
915
916 public void decodeEntryAt(int index, String value) {
917 final int N = value.length();
918 int i = 0;
919 char c;
920 long out = 0;
921 while (i < N && (c=value.charAt(i)) != '-') {
922 i++;
923 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800924 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800925 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800926 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800927 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800928 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800929 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800930 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
931 << STEP_LEVEL_INITIAL_MODE_SHIFT);
932 break;
933 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
934 << STEP_LEVEL_INITIAL_MODE_SHIFT);
935 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700936 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
937 << STEP_LEVEL_INITIAL_MODE_SHIFT);
938 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800939 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
940 break;
941 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
942 break;
943 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
944 break;
945 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
946 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
947 break;
948 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
949 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800950 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700951 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
952 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
953 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800954 }
955 }
956 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800957 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800958 while (i < N && (c=value.charAt(i)) != '-') {
959 i++;
960 level <<= 4;
961 if (c >= '0' && c <= '9') {
962 level += c - '0';
963 } else if (c >= 'a' && c <= 'f') {
964 level += c - 'a' + 10;
965 } else if (c >= 'A' && c <= 'F') {
966 level += c - 'A' + 10;
967 }
968 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800969 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800970 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
971 long duration = 0;
972 while (i < N && (c=value.charAt(i)) != '-') {
973 i++;
974 duration <<= 4;
975 if (c >= '0' && c <= '9') {
976 duration += c - '0';
977 } else if (c >= 'a' && c <= 'f') {
978 duration += c - 'a' + 10;
979 } else if (c >= 'A' && c <= 'F') {
980 duration += c - 'A' + 10;
981 }
982 }
983 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
984 }
985
986 public void init() {
987 mLastStepTime = -1;
988 mNumStepDurations = 0;
989 }
990
991 public void clearTime() {
992 mLastStepTime = -1;
993 }
994
995 public long computeTimePerLevel() {
996 final long[] steps = mStepDurations;
997 final int numSteps = mNumStepDurations;
998
999 // For now we'll do a simple average across all steps.
1000 if (numSteps <= 0) {
1001 return -1;
1002 }
1003 long total = 0;
1004 for (int i=0; i<numSteps; i++) {
1005 total += steps[i] & STEP_LEVEL_TIME_MASK;
1006 }
1007 return total / numSteps;
1008 /*
1009 long[] buckets = new long[numSteps];
1010 int numBuckets = 0;
1011 int numToAverage = 4;
1012 int i = 0;
1013 while (i < numSteps) {
1014 long totalTime = 0;
1015 int num = 0;
1016 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1017 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1018 num++;
1019 }
1020 buckets[numBuckets] = totalTime / num;
1021 numBuckets++;
1022 numToAverage *= 2;
1023 i += num;
1024 }
1025 if (numBuckets < 1) {
1026 return -1;
1027 }
1028 long averageTime = buckets[numBuckets-1];
1029 for (i=numBuckets-2; i>=0; i--) {
1030 averageTime = (averageTime + buckets[i]) / 2;
1031 }
1032 return averageTime;
1033 */
1034 }
1035
1036 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1037 int[] outNumOfInterest) {
1038 final long[] steps = mStepDurations;
1039 final int count = mNumStepDurations;
1040 if (count <= 0) {
1041 return -1;
1042 }
1043 long total = 0;
1044 int numOfInterest = 0;
1045 for (int i=0; i<count; i++) {
1046 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1047 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1048 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1049 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1050 // If the modes of interest didn't change during this step period...
1051 if ((modMode&modesOfInterest) == 0) {
1052 // And the mode values during this period match those we are measuring...
1053 if ((initMode&modesOfInterest) == modeValues) {
1054 // Then this can be used to estimate the total time!
1055 numOfInterest++;
1056 total += steps[i] & STEP_LEVEL_TIME_MASK;
1057 }
1058 }
1059 }
1060 if (numOfInterest <= 0) {
1061 return -1;
1062 }
1063
1064 if (outNumOfInterest != null) {
1065 outNumOfInterest[0] = numOfInterest;
1066 }
1067
1068 // The estimated time is the average time we spend in each level, multipled
1069 // by 100 -- the total number of battery levels
1070 return (total / numOfInterest) * 100;
1071 }
1072
1073 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1074 int stepCount = mNumStepDurations;
1075 final long lastStepTime = mLastStepTime;
1076 if (lastStepTime >= 0 && numStepLevels > 0) {
1077 final long[] steps = mStepDurations;
1078 long duration = elapsedRealtime - lastStepTime;
1079 for (int i=0; i<numStepLevels; i++) {
1080 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1081 long thisDuration = duration / (numStepLevels-i);
1082 duration -= thisDuration;
1083 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1084 thisDuration = STEP_LEVEL_TIME_MASK;
1085 }
1086 steps[0] = thisDuration | modeBits;
1087 }
1088 stepCount += numStepLevels;
1089 if (stepCount > steps.length) {
1090 stepCount = steps.length;
1091 }
1092 }
1093 mNumStepDurations = stepCount;
1094 mLastStepTime = elapsedRealtime;
1095 }
1096
1097 public void readFromParcel(Parcel in) {
1098 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001099 if (N > mStepDurations.length) {
1100 throw new ParcelFormatException("more step durations than available: " + N);
1101 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001102 mNumStepDurations = N;
1103 for (int i=0; i<N; i++) {
1104 mStepDurations[i] = in.readLong();
1105 }
1106 }
1107
1108 public void writeToParcel(Parcel out) {
1109 final int N = mNumStepDurations;
1110 out.writeInt(N);
1111 for (int i=0; i<N; i++) {
1112 out.writeLong(mStepDurations[i]);
1113 }
1114 }
1115 }
1116
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001117 public static final class PackageChange {
1118 public String mPackageName;
1119 public boolean mUpdate;
1120 public int mVersionCode;
1121 }
1122
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001123 public static final class DailyItem {
1124 public long mStartTime;
1125 public long mEndTime;
1126 public LevelStepTracker mDischargeSteps;
1127 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001128 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001129 }
1130
1131 public abstract DailyItem getDailyItemLocked(int daysAgo);
1132
1133 public abstract long getCurrentDailyStartTime();
1134
1135 public abstract long getNextMinDailyDeadline();
1136
1137 public abstract long getNextMaxDailyDeadline();
1138
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001139 public abstract long[] getCpuFreqs();
1140
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001141 public final static class HistoryTag {
1142 public String string;
1143 public int uid;
1144
1145 public int poolIdx;
1146
1147 public void setTo(HistoryTag o) {
1148 string = o.string;
1149 uid = o.uid;
1150 poolIdx = o.poolIdx;
1151 }
1152
1153 public void setTo(String _string, int _uid) {
1154 string = _string;
1155 uid = _uid;
1156 poolIdx = -1;
1157 }
1158
1159 public void writeToParcel(Parcel dest, int flags) {
1160 dest.writeString(string);
1161 dest.writeInt(uid);
1162 }
1163
1164 public void readFromParcel(Parcel src) {
1165 string = src.readString();
1166 uid = src.readInt();
1167 poolIdx = -1;
1168 }
1169
1170 @Override
1171 public boolean equals(Object o) {
1172 if (this == o) return true;
1173 if (o == null || getClass() != o.getClass()) return false;
1174
1175 HistoryTag that = (HistoryTag) o;
1176
1177 if (uid != that.uid) return false;
1178 if (!string.equals(that.string)) return false;
1179
1180 return true;
1181 }
1182
1183 @Override
1184 public int hashCode() {
1185 int result = string.hashCode();
1186 result = 31 * result + uid;
1187 return result;
1188 }
1189 }
1190
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001191 /**
1192 * Optional detailed information that can go into a history step. This is typically
1193 * generated each time the battery level changes.
1194 */
1195 public final static class HistoryStepDetails {
1196 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1197 public int userTime;
1198 public int systemTime;
1199
1200 // Top three apps using CPU in the last step, with times in 1/100 second.
1201 public int appCpuUid1;
1202 public int appCpuUTime1;
1203 public int appCpuSTime1;
1204 public int appCpuUid2;
1205 public int appCpuUTime2;
1206 public int appCpuSTime2;
1207 public int appCpuUid3;
1208 public int appCpuUTime3;
1209 public int appCpuSTime3;
1210
1211 // Information from /proc/stat
1212 public int statUserTime;
1213 public int statSystemTime;
1214 public int statIOWaitTime;
1215 public int statIrqTime;
1216 public int statSoftIrqTime;
1217 public int statIdlTime;
1218
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001219 // Platform-level low power state stats
1220 public String statPlatformIdleState;
1221
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001222 public HistoryStepDetails() {
1223 clear();
1224 }
1225
1226 public void clear() {
1227 userTime = systemTime = 0;
1228 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1229 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1230 = appCpuUTime3 = appCpuSTime3 = 0;
1231 }
1232
1233 public void writeToParcel(Parcel out) {
1234 out.writeInt(userTime);
1235 out.writeInt(systemTime);
1236 out.writeInt(appCpuUid1);
1237 out.writeInt(appCpuUTime1);
1238 out.writeInt(appCpuSTime1);
1239 out.writeInt(appCpuUid2);
1240 out.writeInt(appCpuUTime2);
1241 out.writeInt(appCpuSTime2);
1242 out.writeInt(appCpuUid3);
1243 out.writeInt(appCpuUTime3);
1244 out.writeInt(appCpuSTime3);
1245 out.writeInt(statUserTime);
1246 out.writeInt(statSystemTime);
1247 out.writeInt(statIOWaitTime);
1248 out.writeInt(statIrqTime);
1249 out.writeInt(statSoftIrqTime);
1250 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001251 out.writeString(statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001252 }
1253
1254 public void readFromParcel(Parcel in) {
1255 userTime = in.readInt();
1256 systemTime = in.readInt();
1257 appCpuUid1 = in.readInt();
1258 appCpuUTime1 = in.readInt();
1259 appCpuSTime1 = in.readInt();
1260 appCpuUid2 = in.readInt();
1261 appCpuUTime2 = in.readInt();
1262 appCpuSTime2 = in.readInt();
1263 appCpuUid3 = in.readInt();
1264 appCpuUTime3 = in.readInt();
1265 appCpuSTime3 = in.readInt();
1266 statUserTime = in.readInt();
1267 statSystemTime = in.readInt();
1268 statIOWaitTime = in.readInt();
1269 statIrqTime = in.readInt();
1270 statSoftIrqTime = in.readInt();
1271 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001272 statPlatformIdleState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001273 }
1274 }
1275
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001276 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001277 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001278
1279 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001280 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001281
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001282 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001283 public static final byte CMD_NULL = -1;
1284 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001285 public static final byte CMD_CURRENT_TIME = 5;
1286 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001287 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001288 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001289
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001290 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001291
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001292 /**
1293 * Return whether the command code is a delta data update.
1294 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001295 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001296 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001297 }
1298
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001299 public byte batteryLevel;
1300 public byte batteryStatus;
1301 public byte batteryHealth;
1302 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001303
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001304 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001305 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001306
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001307 // The charge of the battery in micro-Ampere-hours.
1308 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001309
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001310 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001311 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001312 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001313 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001314 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1315 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001316 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001317 public static final int STATE_PHONE_STATE_SHIFT = 6;
1318 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001319 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001320 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001321 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001322
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001323 // These states always appear directly in the first int token
1324 // of a delta change; they should be ones that change relatively
1325 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001326 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1327 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001328 public static final int STATE_GPS_ON_FLAG = 1<<29;
1329 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001330 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001331 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001332 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001333 // Do not use, this is used for coulomb delta count.
1334 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001335 // These are on the lower bits used for the command; if they change
1336 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001337 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001338 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001339 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001340 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1341 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1342 // empty slot
1343 // empty slot
1344 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001345
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001346 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001347 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1348
1349 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001350
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001351 public int states;
1352
Dianne Hackborn3251b902014-06-20 14:40:53 -07001353 // Constants from WIFI_SUPPL_STATE_*
1354 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1355 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1356 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1357 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1358 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1359 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1360
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001361 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001362 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1363 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1364 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001365 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001366 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1367 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1368 public static final int STATE2_CHARGING_FLAG = 1<<24;
1369 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1370 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1371 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001372 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001373
1374 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001375 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001376 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1377
1378 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001379
Dianne Hackborn40c87252014-03-19 16:55:40 -07001380 public int states2;
1381
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001382 // The wake lock that was acquired at this point.
1383 public HistoryTag wakelockTag;
1384
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001385 // Kernel wakeup reason at this point.
1386 public HistoryTag wakeReasonTag;
1387
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001388 // Non-null when there is more detailed information at this step.
1389 public HistoryStepDetails stepDetails;
1390
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001391 public static final int EVENT_FLAG_START = 0x8000;
1392 public static final int EVENT_FLAG_FINISH = 0x4000;
1393
1394 // No event in this item.
1395 public static final int EVENT_NONE = 0x0000;
1396 // Event is about a process that is running.
1397 public static final int EVENT_PROC = 0x0001;
1398 // Event is about an application package that is in the foreground.
1399 public static final int EVENT_FOREGROUND = 0x0002;
1400 // Event is about an application package that is at the top of the screen.
1401 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001402 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001403 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001404 // Events for all additional wake locks aquired/release within a wake block.
1405 // These are not generated by default.
1406 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001407 // Event is about an application executing a scheduled job.
1408 public static final int EVENT_JOB = 0x0006;
1409 // Events for users running.
1410 public static final int EVENT_USER_RUNNING = 0x0007;
1411 // Events for foreground user.
1412 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001413 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001414 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001415 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001416 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001417 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001418 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001419 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001420 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001421 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001422 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001423 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001424 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001425 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001426 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001427 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001428 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001429 // Event for a package being on the temporary whitelist.
1430 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001431 // Event for the screen waking up.
1432 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001433 // Event for the UID that woke up the application processor.
1434 // Used for wakeups coming from WiFi, modem, etc.
1435 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001436 // Event for reporting that a specific partial wake lock has been held for a long duration.
1437 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001438
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001439 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001440 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001441 // Mask to extract out only the type part of the event.
1442 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001443
1444 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1445 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1446 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1447 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1448 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1449 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001450 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1451 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001452 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1453 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001454 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1455 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1456 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1457 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1458 public static final int EVENT_USER_FOREGROUND_START =
1459 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1460 public static final int EVENT_USER_FOREGROUND_FINISH =
1461 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001462 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1463 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001464 public static final int EVENT_TEMP_WHITELIST_START =
1465 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1466 public static final int EVENT_TEMP_WHITELIST_FINISH =
1467 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001468 public static final int EVENT_LONG_WAKE_LOCK_START =
1469 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1470 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1471 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001472
1473 // For CMD_EVENT.
1474 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001475 public HistoryTag eventTag;
1476
Dianne Hackborn9a755432014-05-15 17:05:22 -07001477 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001478 public long currentTime;
1479
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001480 // Meta-data when reading.
1481 public int numReadInts;
1482
1483 // Pre-allocated objects.
1484 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001485 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001486 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001487
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001488 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001489 }
Bookatzc8c44962017-05-11 12:12:54 -07001490
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001491 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001492 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001493 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001494 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001495 }
Bookatzc8c44962017-05-11 12:12:54 -07001496
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001497 public int describeContents() {
1498 return 0;
1499 }
1500
1501 public void writeToParcel(Parcel dest, int flags) {
1502 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001503 int bat = (((int)cmd)&0xff)
1504 | ((((int)batteryLevel)<<8)&0xff00)
1505 | ((((int)batteryStatus)<<16)&0xf0000)
1506 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001507 | ((((int)batteryPlugType)<<24)&0xf000000)
1508 | (wakelockTag != null ? 0x10000000 : 0)
1509 | (wakeReasonTag != null ? 0x20000000 : 0)
1510 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001511 dest.writeInt(bat);
1512 bat = (((int)batteryTemperature)&0xffff)
1513 | ((((int)batteryVoltage)<<16)&0xffff0000);
1514 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001515 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001516 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001517 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001518 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001519 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001520 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001521 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001522 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001523 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001524 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001525 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001526 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001527 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001528 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001529 dest.writeLong(currentTime);
1530 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001531 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001532
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001533 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001534 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001535 int bat = src.readInt();
1536 cmd = (byte)(bat&0xff);
1537 batteryLevel = (byte)((bat>>8)&0xff);
1538 batteryStatus = (byte)((bat>>16)&0xf);
1539 batteryHealth = (byte)((bat>>20)&0xf);
1540 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001541 int bat2 = src.readInt();
1542 batteryTemperature = (short)(bat2&0xffff);
1543 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001544 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001545 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001546 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001547 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001548 wakelockTag = localWakelockTag;
1549 wakelockTag.readFromParcel(src);
1550 } else {
1551 wakelockTag = null;
1552 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001553 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001554 wakeReasonTag = localWakeReasonTag;
1555 wakeReasonTag.readFromParcel(src);
1556 } else {
1557 wakeReasonTag = null;
1558 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001559 if ((bat&0x40000000) != 0) {
1560 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001561 eventTag = localEventTag;
1562 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001563 } else {
1564 eventCode = EVENT_NONE;
1565 eventTag = null;
1566 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001567 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001568 currentTime = src.readLong();
1569 } else {
1570 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001571 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001572 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001573 }
1574
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001575 public void clear() {
1576 time = 0;
1577 cmd = CMD_NULL;
1578 batteryLevel = 0;
1579 batteryStatus = 0;
1580 batteryHealth = 0;
1581 batteryPlugType = 0;
1582 batteryTemperature = 0;
1583 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001584 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001585 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001586 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001587 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001588 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001589 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001590 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001591 }
Bookatzc8c44962017-05-11 12:12:54 -07001592
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001593 public void setTo(HistoryItem o) {
1594 time = o.time;
1595 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001596 setToCommon(o);
1597 }
1598
1599 public void setTo(long time, byte cmd, HistoryItem o) {
1600 this.time = time;
1601 this.cmd = cmd;
1602 setToCommon(o);
1603 }
1604
1605 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001606 batteryLevel = o.batteryLevel;
1607 batteryStatus = o.batteryStatus;
1608 batteryHealth = o.batteryHealth;
1609 batteryPlugType = o.batteryPlugType;
1610 batteryTemperature = o.batteryTemperature;
1611 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001612 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001613 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001614 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001615 if (o.wakelockTag != null) {
1616 wakelockTag = localWakelockTag;
1617 wakelockTag.setTo(o.wakelockTag);
1618 } else {
1619 wakelockTag = null;
1620 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001621 if (o.wakeReasonTag != null) {
1622 wakeReasonTag = localWakeReasonTag;
1623 wakeReasonTag.setTo(o.wakeReasonTag);
1624 } else {
1625 wakeReasonTag = null;
1626 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001627 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001628 if (o.eventTag != null) {
1629 eventTag = localEventTag;
1630 eventTag.setTo(o.eventTag);
1631 } else {
1632 eventTag = null;
1633 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001634 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001635 }
1636
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001637 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001638 return batteryLevel == o.batteryLevel
1639 && batteryStatus == o.batteryStatus
1640 && batteryHealth == o.batteryHealth
1641 && batteryPlugType == o.batteryPlugType
1642 && batteryTemperature == o.batteryTemperature
1643 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001644 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001645 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001646 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001647 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001648 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001649
1650 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001651 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001652 return false;
1653 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001654 if (wakelockTag != o.wakelockTag) {
1655 if (wakelockTag == null || o.wakelockTag == null) {
1656 return false;
1657 }
1658 if (!wakelockTag.equals(o.wakelockTag)) {
1659 return false;
1660 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001661 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001662 if (wakeReasonTag != o.wakeReasonTag) {
1663 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1664 return false;
1665 }
1666 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1667 return false;
1668 }
1669 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001670 if (eventTag != o.eventTag) {
1671 if (eventTag == null || o.eventTag == null) {
1672 return false;
1673 }
1674 if (!eventTag.equals(o.eventTag)) {
1675 return false;
1676 }
1677 }
1678 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001679 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001680 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001681
1682 public final static class HistoryEventTracker {
1683 private final HashMap<String, SparseIntArray>[] mActiveEvents
1684 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1685
1686 public boolean updateState(int code, String name, int uid, int poolIdx) {
1687 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1688 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1689 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1690 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001691 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001692 mActiveEvents[idx] = active;
1693 }
1694 SparseIntArray uids = active.get(name);
1695 if (uids == null) {
1696 uids = new SparseIntArray();
1697 active.put(name, uids);
1698 }
1699 if (uids.indexOfKey(uid) >= 0) {
1700 // Already set, nothing to do!
1701 return false;
1702 }
1703 uids.put(uid, poolIdx);
1704 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1705 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1706 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1707 if (active == null) {
1708 // not currently active, nothing to do.
1709 return false;
1710 }
1711 SparseIntArray uids = active.get(name);
1712 if (uids == null) {
1713 // not currently active, nothing to do.
1714 return false;
1715 }
1716 idx = uids.indexOfKey(uid);
1717 if (idx < 0) {
1718 // not currently active, nothing to do.
1719 return false;
1720 }
1721 uids.removeAt(idx);
1722 if (uids.size() <= 0) {
1723 active.remove(name);
1724 }
1725 }
1726 return true;
1727 }
1728
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001729 public void removeEvents(int code) {
1730 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1731 mActiveEvents[idx] = null;
1732 }
1733
Dianne Hackborn37de0982014-05-09 09:32:18 -07001734 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1735 return mActiveEvents[code];
1736 }
1737 }
1738
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001739 public static final class BitDescription {
1740 public final int mask;
1741 public final int shift;
1742 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001743 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001744 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001745 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07001746
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001747 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001748 this.mask = mask;
1749 this.shift = -1;
1750 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001751 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001752 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001753 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001754 }
Bookatzc8c44962017-05-11 12:12:54 -07001755
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001756 public BitDescription(int mask, int shift, String name, String shortName,
1757 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001758 this.mask = mask;
1759 this.shift = shift;
1760 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001761 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001762 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001763 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001764 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001765 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001766
Dianne Hackbornfc064132014-06-02 12:42:12 -07001767 /**
1768 * Don't allow any more batching in to the current history event. This
1769 * is called when printing partial histories, so to ensure that the next
1770 * history event will go in to a new batch after what was printed in the
1771 * last partial history.
1772 */
1773 public abstract void commitCurrentHistoryBatchLocked();
1774
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001775 public abstract int getHistoryTotalSize();
1776
1777 public abstract int getHistoryUsedSize();
1778
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001779 public abstract boolean startIteratingHistoryLocked();
1780
Dianne Hackborn099bc622014-01-22 13:39:16 -08001781 public abstract int getHistoryStringPoolSize();
1782
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001783 public abstract int getHistoryStringPoolBytes();
1784
1785 public abstract String getHistoryTagPoolString(int index);
1786
1787 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001788
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001789 public abstract boolean getNextHistoryLocked(HistoryItem out);
1790
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001791 public abstract void finishIteratingHistoryLocked();
1792
1793 public abstract boolean startIteratingOldHistoryLocked();
1794
1795 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1796
1797 public abstract void finishIteratingOldHistoryLocked();
1798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001800 * Return the base time offset for the battery history.
1801 */
1802 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07001803
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001804 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 * Returns the number of times the device has been started.
1806 */
1807 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07001808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001810 * 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 -08001811 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07001812 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 * {@hide}
1814 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001815 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07001816
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001817 /**
1818 * Returns the number of times the screen was turned on.
1819 *
1820 * {@hide}
1821 */
1822 public abstract int getScreenOnCount(int which);
1823
Jeff Browne95c3cd2014-05-02 16:59:26 -07001824 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1825
Dianne Hackborn617f8772009-03-31 15:04:46 -07001826 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1827 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1828 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1829 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1830 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07001831
Dianne Hackborn617f8772009-03-31 15:04:46 -07001832 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1833 "dark", "dim", "medium", "light", "bright"
1834 };
Bookatzc8c44962017-05-11 12:12:54 -07001835
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001836 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1837 "0", "1", "2", "3", "4"
1838 };
1839
Dianne Hackborn617f8772009-03-31 15:04:46 -07001840 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001841
Dianne Hackborn617f8772009-03-31 15:04:46 -07001842 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001843 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001844 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07001845 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07001846 * {@hide}
1847 */
1848 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001849 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001852 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001853 * running on battery.
1854 *
1855 * {@hide}
1856 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001857 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001858
1859 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001860 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001861 *
1862 * {@hide}
1863 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001864 public abstract int getPowerSaveModeEnabledCount(int which);
1865
1866 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001867 * Constant for device idle mode: not active.
1868 */
1869 public static final int DEVICE_IDLE_MODE_OFF = 0;
1870
1871 /**
1872 * Constant for device idle mode: active in lightweight mode.
1873 */
1874 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1875
1876 /**
1877 * Constant for device idle mode: active in full mode.
1878 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07001879 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001880
1881 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001882 * Returns the time in microseconds that device has been in idle mode while
1883 * running on battery.
1884 *
1885 * {@hide}
1886 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001887 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001888
1889 /**
1890 * Returns the number of times that the devie has gone in to idle mode.
1891 *
1892 * {@hide}
1893 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001894 public abstract int getDeviceIdleModeCount(int mode, int which);
1895
1896 /**
1897 * Return the longest duration we spent in a particular device idle mode (fully in the
1898 * mode, not in idle maintenance etc).
1899 */
1900 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001901
1902 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001903 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001904 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001905 * counts all of the time that we consider the device to be idle, whether or not
1906 * it is currently in the actual device idle mode.
1907 *
1908 * {@hide}
1909 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001910 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001911
1912 /**
1913 * Returns the number of times that the devie has started idling.
1914 *
1915 * {@hide}
1916 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001917 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001918
1919 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001920 * Returns the number of times that connectivity state changed.
1921 *
1922 * {@hide}
1923 */
1924 public abstract int getNumConnectivityChange(int which);
1925
1926 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001927 * 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 -08001928 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07001929 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 * {@hide}
1931 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001932 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07001933
Dianne Hackborn627bba72009-03-24 22:32:56 -07001934 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001935 * Returns the number of times a phone call was activated.
1936 *
1937 * {@hide}
1938 */
1939 public abstract int getPhoneOnCount(int which);
1940
1941 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001942 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001943 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07001944 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07001945 * {@hide}
1946 */
1947 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001948 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001949
Dianne Hackborn617f8772009-03-31 15:04:46 -07001950 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001951 * Returns the time in microseconds that the phone has been trying to
1952 * acquire a signal.
1953 *
1954 * {@hide}
1955 */
1956 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001957 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001958
1959 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001960 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07001961 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07001962 * {@hide}
1963 */
1964 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1965
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001966 /**
1967 * Returns the time in microseconds that the mobile network has been active
1968 * (in a high power state).
1969 *
1970 * {@hide}
1971 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001972 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001973
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001974 /**
1975 * Returns the number of times that the mobile network has transitioned to the
1976 * active state.
1977 *
1978 * {@hide}
1979 */
1980 public abstract int getMobileRadioActiveCount(int which);
1981
1982 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001983 * Returns the time in microseconds that is the difference between the mobile radio
1984 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1985 * from the radio.
1986 *
1987 * {@hide}
1988 */
1989 public abstract long getMobileRadioActiveAdjustedTime(int which);
1990
1991 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001992 * Returns the time in microseconds that the mobile network has been active
1993 * (in a high power state) but not being able to blame on an app.
1994 *
1995 * {@hide}
1996 */
1997 public abstract long getMobileRadioActiveUnknownTime(int which);
1998
1999 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002000 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002001 *
2002 * {@hide}
2003 */
2004 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002005
Dianne Hackborn627bba72009-03-24 22:32:56 -07002006 public static final int DATA_CONNECTION_NONE = 0;
2007 public static final int DATA_CONNECTION_GPRS = 1;
2008 public static final int DATA_CONNECTION_EDGE = 2;
2009 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002010 public static final int DATA_CONNECTION_CDMA = 4;
2011 public static final int DATA_CONNECTION_EVDO_0 = 5;
2012 public static final int DATA_CONNECTION_EVDO_A = 6;
2013 public static final int DATA_CONNECTION_1xRTT = 7;
2014 public static final int DATA_CONNECTION_HSDPA = 8;
2015 public static final int DATA_CONNECTION_HSUPA = 9;
2016 public static final int DATA_CONNECTION_HSPA = 10;
2017 public static final int DATA_CONNECTION_IDEN = 11;
2018 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002019 public static final int DATA_CONNECTION_LTE = 13;
2020 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08002021 public static final int DATA_CONNECTION_HSPAP = 15;
2022 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002023
Dianne Hackborn627bba72009-03-24 22:32:56 -07002024 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002025 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002026 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08002027 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002028 };
Bookatzc8c44962017-05-11 12:12:54 -07002029
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002030 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002031
Dianne Hackborn627bba72009-03-24 22:32:56 -07002032 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002033 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002034 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002035 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002036 * {@hide}
2037 */
2038 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002039 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002042 * Returns the number of times the phone has entered the given data
2043 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002044 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002045 * {@hide}
2046 */
2047 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002048
Dianne Hackborn3251b902014-06-20 14:40:53 -07002049 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2050 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2051 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2052 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2053 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2054 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2055 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2056 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2057 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2058 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2059 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2060 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2061 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2062
2063 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2064
2065 static final String[] WIFI_SUPPL_STATE_NAMES = {
2066 "invalid", "disconn", "disabled", "inactive", "scanning",
2067 "authenticating", "associating", "associated", "4-way-handshake",
2068 "group-handshake", "completed", "dormant", "uninit"
2069 };
2070
2071 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2072 "inv", "dsc", "dis", "inact", "scan",
2073 "auth", "ascing", "asced", "4-way",
2074 "group", "compl", "dorm", "uninit"
2075 };
2076
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002077 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
2078 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002079 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002080 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2081 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002082 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002083 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2084 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2085 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002086 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002087 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002088 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002089 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002090 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2091 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002092 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2093 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2094 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2095 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2096 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2097 new String[] {"in", "out", "emergency", "off"},
2098 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002099 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2100 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2101 SignalStrength.SIGNAL_STRENGTH_NAMES,
2102 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002103 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2104 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2105 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002106 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002107
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002108 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2109 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002110 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002111 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002112 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002113 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002114 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002115 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2116 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2117 new String[] { "off", "light", "full", "???" },
2118 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002119 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2120 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2121 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002122 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2123 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2124 new String[] { "0", "1", "2", "3", "4" },
2125 new String[] { "0", "1", "2", "3", "4" }),
2126 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2127 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2128 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002129 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002130 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002131 };
2132
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002133 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002134 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002135 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2136 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002137 };
2138
2139 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002140 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002141 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002142 "Esw", "Ewa", "Elw", "Eec"
2143 };
2144
2145 @FunctionalInterface
2146 public interface IntToString {
2147 String applyAsString(int val);
2148 }
2149
2150 private static final IntToString sUidToString = UserHandle::formatUid;
2151 private static final IntToString sIntToString = Integer::toString;
2152
2153 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2154 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2155 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2156 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2157 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002158 };
2159
Dianne Hackborn617f8772009-03-31 15:04:46 -07002160 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002161 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002162 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002163 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002164 * {@hide}
2165 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002166 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002167
2168 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002169 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002170 * been in the running state while the device was running on battery.
2171 *
2172 * {@hide}
2173 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002174 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002175
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002176 public static final int WIFI_STATE_OFF = 0;
2177 public static final int WIFI_STATE_OFF_SCANNING = 1;
2178 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2179 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2180 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2181 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2182 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2183 public static final int WIFI_STATE_SOFT_AP = 7;
2184
2185 static final String[] WIFI_STATE_NAMES = {
2186 "off", "scanning", "no_net", "disconn",
2187 "sta", "p2p", "sta_p2p", "soft_ap"
2188 };
2189
2190 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2191
2192 /**
2193 * Returns the time in microseconds that WiFi has been running in the given state.
2194 *
2195 * {@hide}
2196 */
2197 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002198 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002199
2200 /**
2201 * Returns the number of times that WiFi has entered the given state.
2202 *
2203 * {@hide}
2204 */
2205 public abstract int getWifiStateCount(int wifiState, int which);
2206
The Android Open Source Project10592532009-03-18 17:39:46 -07002207 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002208 * Returns the time in microseconds that the wifi supplicant has been
2209 * in a given state.
2210 *
2211 * {@hide}
2212 */
2213 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2214
2215 /**
2216 * Returns the number of times that the wifi supplicant has transitioned
2217 * to a given state.
2218 *
2219 * {@hide}
2220 */
2221 public abstract int getWifiSupplStateCount(int state, int which);
2222
2223 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2224
2225 /**
2226 * Returns the time in microseconds that WIFI has been running with
2227 * the given signal strength.
2228 *
2229 * {@hide}
2230 */
2231 public abstract long getWifiSignalStrengthTime(int strengthBin,
2232 long elapsedRealtimeUs, int which);
2233
2234 /**
2235 * Returns the number of times WIFI has entered the given signal strength.
2236 *
2237 * {@hide}
2238 */
2239 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2240
2241 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002242 * Returns the time in microseconds that the flashlight has been on while the device was
2243 * running on battery.
2244 *
2245 * {@hide}
2246 */
2247 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2248
2249 /**
2250 * Returns the number of times that the flashlight has been turned on while the device was
2251 * running on battery.
2252 *
2253 * {@hide}
2254 */
2255 public abstract long getFlashlightOnCount(int which);
2256
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002257 /**
2258 * Returns the time in microseconds that the camera has been on while the device was
2259 * running on battery.
2260 *
2261 * {@hide}
2262 */
2263 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2264
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002265 /**
2266 * Returns the time in microseconds that bluetooth scans were running while the device was
2267 * on battery.
2268 *
2269 * {@hide}
2270 */
2271 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002272
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002273 public static final int NETWORK_MOBILE_RX_DATA = 0;
2274 public static final int NETWORK_MOBILE_TX_DATA = 1;
2275 public static final int NETWORK_WIFI_RX_DATA = 2;
2276 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002277 public static final int NETWORK_BT_RX_DATA = 4;
2278 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002279 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2280 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2281 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2282 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2283 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002284
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002285 public abstract long getNetworkActivityBytes(int type, int which);
2286 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002287
Adam Lesinskie08af192015-03-25 16:42:59 -07002288 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002289 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002290 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002291 * actual power data.
2292 */
2293 public abstract boolean hasWifiActivityReporting();
2294
2295 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002296 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2297 * in various radio controller states, such as transmit, receive, and idle.
2298 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002299 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002300 public abstract ControllerActivityCounter getWifiControllerActivity();
2301
2302 /**
2303 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2304 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2305 * actual power data.
2306 */
2307 public abstract boolean hasBluetoothActivityReporting();
2308
2309 /**
2310 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2311 * in various radio controller states, such as transmit, receive, and idle.
2312 * @return non-null {@link ControllerActivityCounter}
2313 */
2314 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2315
2316 /**
2317 * Returns true if the BatteryStats object has detailed modem power reports.
2318 * When true, calling {@link #getModemControllerActivity()} will yield the
2319 * actual power data.
2320 */
2321 public abstract boolean hasModemActivityReporting();
2322
2323 /**
2324 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2325 * in various radio controller states, such as transmit, receive, and idle.
2326 * @return non-null {@link ControllerActivityCounter}
2327 */
2328 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002329
The Android Open Source Project10592532009-03-18 17:39:46 -07002330 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002331 * Return the wall clock time when battery stats data collection started.
2332 */
2333 public abstract long getStartClockTime();
2334
2335 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002336 * Return platform version tag that we were running in when the battery stats started.
2337 */
2338 public abstract String getStartPlatformVersion();
2339
2340 /**
2341 * Return platform version tag that we were running in when the battery stats ended.
2342 */
2343 public abstract String getEndPlatformVersion();
2344
2345 /**
2346 * Return the internal version code of the parcelled format.
2347 */
2348 public abstract int getParcelVersion();
2349
2350 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 * Return whether we are currently running on battery.
2352 */
2353 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 /**
2356 * Returns a SparseArray containing the statistics for each uid.
2357 */
2358 public abstract SparseArray<? extends Uid> getUidStats();
2359
2360 /**
2361 * Returns the current battery uptime in microseconds.
2362 *
2363 * @param curTime the amount of elapsed realtime in microseconds.
2364 */
2365 public abstract long getBatteryUptime(long curTime);
2366
2367 /**
2368 * Returns the current battery realtime in microseconds.
2369 *
2370 * @param curTime the amount of elapsed realtime in microseconds.
2371 */
2372 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002373
The Android Open Source Project10592532009-03-18 17:39:46 -07002374 /**
Evan Millar633a1742009-04-02 16:36:33 -07002375 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002376 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002377 */
Evan Millar633a1742009-04-02 16:36:33 -07002378 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002379
The Android Open Source Project10592532009-03-18 17:39:46 -07002380 /**
Evan Millar633a1742009-04-02 16:36:33 -07002381 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2382 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002383 */
Evan Millar633a1742009-04-02 16:36:33 -07002384 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385
2386 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002387 * Get the amount the battery has discharged since the stats were
2388 * last reset after charging, as a lower-end approximation.
2389 */
2390 public abstract int getLowDischargeAmountSinceCharge();
2391
2392 /**
2393 * Get the amount the battery has discharged since the stats were
2394 * last reset after charging, as an upper-end approximation.
2395 */
2396 public abstract int getHighDischargeAmountSinceCharge();
2397
2398 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002399 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2400 */
2401 public abstract int getDischargeAmount(int which);
2402
2403 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002404 * Get the amount the battery has discharged while the screen was on,
2405 * since the last time power was unplugged.
2406 */
2407 public abstract int getDischargeAmountScreenOn();
2408
2409 /**
2410 * Get the amount the battery has discharged while the screen was on,
2411 * since the last time the device was charged.
2412 */
2413 public abstract int getDischargeAmountScreenOnSinceCharge();
2414
2415 /**
2416 * Get the amount the battery has discharged while the screen was off,
2417 * since the last time power was unplugged.
2418 */
2419 public abstract int getDischargeAmountScreenOff();
2420
2421 /**
2422 * Get the amount the battery has discharged while the screen was off,
2423 * since the last time the device was charged.
2424 */
2425 public abstract int getDischargeAmountScreenOffSinceCharge();
2426
2427 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 * Returns the total, last, or current battery uptime in microseconds.
2429 *
2430 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002431 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 */
2433 public abstract long computeBatteryUptime(long curTime, int which);
2434
2435 /**
2436 * Returns the total, last, or current battery realtime in microseconds.
2437 *
2438 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002439 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 */
2441 public abstract long computeBatteryRealtime(long curTime, int which);
2442
2443 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002444 * Returns the total, last, or current battery screen off uptime in microseconds.
2445 *
2446 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002447 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002448 */
2449 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2450
2451 /**
2452 * Returns the total, last, or current battery screen off realtime in microseconds.
2453 *
2454 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002455 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002456 */
2457 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2458
2459 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 * Returns the total, last, or current uptime in microseconds.
2461 *
2462 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002463 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 */
2465 public abstract long computeUptime(long curTime, int which);
2466
2467 /**
2468 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002469 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002471 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 */
2473 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002474
2475 /**
2476 * Compute an approximation for how much run time (in microseconds) is remaining on
2477 * the battery. Returns -1 if no time can be computed: either there is not
2478 * enough current data to make a decision, or the battery is currently
2479 * charging.
2480 *
2481 * @param curTime The current elepsed realtime in microseconds.
2482 */
2483 public abstract long computeBatteryTimeRemaining(long curTime);
2484
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002485 // The part of a step duration that is the actual time.
2486 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2487
2488 // Bits in a step duration that are the new battery level we are at.
2489 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002490 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002491
2492 // Bits in a step duration that are the initial mode we were in at that step.
2493 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002494 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002495
2496 // Bits in a step duration that indicate which modes changed during that step.
2497 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002498 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002499
2500 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2501 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2502
Santos Cordone94f0502017-02-24 12:31:20 -08002503 // The largest value for screen state that is tracked in battery states. Any values above
2504 // this should be mapped back to one of the tracked values before being tracked here.
2505 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2506
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002507 // Step duration mode: power save is on.
2508 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2509
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002510 // Step duration mode: device is currently in idle mode.
2511 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2512
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002513 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2514 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002515 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2516 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002517 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2518 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2519 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2520 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2521 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002522 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2523 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002524 };
2525 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2526 (Display.STATE_OFF-1),
2527 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002528 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002529 (Display.STATE_ON-1),
2530 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2531 (Display.STATE_DOZE-1),
2532 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2533 (Display.STATE_DOZE_SUSPEND-1),
2534 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002535 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002536 };
2537 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2538 "screen off",
2539 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002540 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002541 "screen on",
2542 "screen on power save",
2543 "screen doze",
2544 "screen doze power save",
2545 "screen doze-suspend",
2546 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002547 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002548 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002549
2550 /**
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002551 * Return the counter keeping track of the amount of battery discharge while the screen was off,
2552 * measured in micro-Ampere-hours. This will be non-zero only if the device's battery has
2553 * a coulomb counter.
2554 */
2555 public abstract LongCounter getDischargeScreenOffCoulombCounter();
2556
2557 /**
2558 * Return the counter keeping track of the amount of battery discharge measured in
2559 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2560 * a coulomb counter.
2561 */
2562 public abstract LongCounter getDischargeCoulombCounter();
2563
2564 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002565 * Returns the estimated real battery capacity, which may be less than the capacity
2566 * declared by the PowerProfile.
2567 * @return The estimated battery capacity in mAh.
2568 */
2569 public abstract int getEstimatedBatteryCapacity();
2570
2571 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002572 * @return The minimum learned battery capacity in uAh.
2573 */
2574 public abstract int getMinLearnedBatteryCapacity();
2575
2576 /**
2577 * @return The maximum learned battery capacity in uAh.
2578 */
2579 public abstract int getMaxLearnedBatteryCapacity() ;
2580
2581 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002582 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002583 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002584 public abstract LevelStepTracker getDischargeLevelStepTracker();
2585
2586 /**
2587 * Return the array of daily discharge step durations.
2588 */
2589 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002590
2591 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002592 * Compute an approximation for how much time (in microseconds) remains until the battery
2593 * is fully charged. Returns -1 if no time can be computed: either there is not
2594 * enough current data to make a decision, or the battery is currently
2595 * discharging.
2596 *
2597 * @param curTime The current elepsed realtime in microseconds.
2598 */
2599 public abstract long computeChargeTimeRemaining(long curTime);
2600
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002601 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002602 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002603 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002604 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002605
2606 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002607 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002608 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002609 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002610
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002611 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2612
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002613 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002614
Evan Millarc64edde2009-04-18 12:26:32 -07002615 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616
James Carr2dd7e5e2016-07-20 18:48:39 -07002617 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2618
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002619 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2620
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002621 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 long days = seconds / (60 * 60 * 24);
2623 if (days != 0) {
2624 out.append(days);
2625 out.append("d ");
2626 }
2627 long used = days * 60 * 60 * 24;
2628
2629 long hours = (seconds - used) / (60 * 60);
2630 if (hours != 0 || used != 0) {
2631 out.append(hours);
2632 out.append("h ");
2633 }
2634 used += hours * 60 * 60;
2635
2636 long mins = (seconds-used) / 60;
2637 if (mins != 0 || used != 0) {
2638 out.append(mins);
2639 out.append("m ");
2640 }
2641 used += mins * 60;
2642
2643 if (seconds != 0 || used != 0) {
2644 out.append(seconds-used);
2645 out.append("s ");
2646 }
2647 }
2648
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002649 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002651 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 sb.append(time - (sec * 1000));
2653 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 }
2655
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002656 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002657 long sec = time / 1000;
2658 formatTimeRaw(sb, sec);
2659 sb.append(time - (sec * 1000));
2660 sb.append("ms");
2661 }
2662
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002663 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002665 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 }
2667 float perc = ((float)num) / ((float)den) * 100;
2668 mFormatBuilder.setLength(0);
2669 mFormatter.format("%.1f%%", perc);
2670 return mFormatBuilder.toString();
2671 }
2672
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002673 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002674 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07002675
Evan Millar22ac0432009-03-31 11:33:18 -07002676 if (bytes < BYTES_PER_KB) {
2677 return bytes + "B";
2678 } else if (bytes < BYTES_PER_MB) {
2679 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2680 return mFormatBuilder.toString();
2681 } else if (bytes < BYTES_PER_GB){
2682 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2683 return mFormatBuilder.toString();
2684 } else {
2685 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2686 return mFormatBuilder.toString();
2687 }
2688 }
2689
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002690 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002691 if (timer != null) {
2692 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002693 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002694 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2695 return totalTimeMillis;
2696 }
2697 return 0;
2698 }
2699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 /**
2701 *
2702 * @param sb a StringBuilder object.
2703 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002704 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002706 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 * @param linePrefix a String to be prepended to each line of output.
2708 * @return the line prefix
2709 */
2710 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002711 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002714 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002715
Evan Millarc64edde2009-04-18 12:26:32 -07002716 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 if (totalTimeMillis != 0) {
2718 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002719 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002720 if (name != null) {
2721 sb.append(name);
2722 sb.append(' ');
2723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 sb.append('(');
2725 sb.append(count);
2726 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002727 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2728 if (maxDurationMs >= 0) {
2729 sb.append(" max=");
2730 sb.append(maxDurationMs);
2731 }
Bookatz506a8182017-05-01 14:18:42 -07002732 // Put actual time if it is available and different from totalTimeMillis.
2733 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
2734 if (totalDurMs > totalTimeMillis) {
2735 sb.append(" actual=");
2736 sb.append(totalDurMs);
2737 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07002738 if (timer.isRunningLocked()) {
2739 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2740 if (currentMs >= 0) {
2741 sb.append(" (running for ");
2742 sb.append(currentMs);
2743 sb.append("ms)");
2744 } else {
2745 sb.append(" (running)");
2746 }
2747 }
2748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 return ", ";
2750 }
2751 }
2752 return linePrefix;
2753 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002754
2755 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07002756 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002757 *
2758 * @param pw a PrintWriter object to print to.
2759 * @param sb a StringBuilder object.
2760 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08002761 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002762 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2763 * @param prefix a String to be prepended to each line of output.
2764 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002765 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002766 */
2767 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07002768 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002769 if (timer != null) {
2770 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07002771 final long totalTimeMs = (timer.getTotalTimeLocked(
2772 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002773 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002774 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002775 sb.setLength(0);
2776 sb.append(prefix);
2777 sb.append(" ");
2778 sb.append(type);
2779 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002780 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002781 sb.append("realtime (");
2782 sb.append(count);
2783 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002784 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
2785 if (maxDurationMs >= 0) {
2786 sb.append(" max=");
2787 sb.append(maxDurationMs);
2788 }
2789 if (timer.isRunningLocked()) {
2790 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
2791 if (currentMs >= 0) {
2792 sb.append(" (running for ");
2793 sb.append(currentMs);
2794 sb.append("ms)");
2795 } else {
2796 sb.append(" (running)");
2797 }
2798 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002799 pw.println(sb.toString());
2800 return true;
2801 }
2802 }
2803 return false;
2804 }
Bookatzc8c44962017-05-11 12:12:54 -07002805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 /**
2807 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07002808 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 * @param sb a StringBuilder object.
2810 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002811 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002813 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 * @param linePrefix a String to be prepended to each line of output.
2815 * @return the line prefix
2816 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002817 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2818 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 long totalTimeMicros = 0;
2820 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07002821 long max = 0;
2822 long current = 0;
2823 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002825 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07002826 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002827 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2828 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07002829 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 }
2831 sb.append(linePrefix);
2832 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2833 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002834 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002836 sb.append(',');
2837 sb.append(current);
2838 sb.append(',');
2839 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07002840 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
2841 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
2842 // totalDuration independent of totalTimeMicros (since they are not pooled).
2843 if (name != null) {
2844 sb.append(',');
2845 sb.append(totalDuration);
2846 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 return ",";
2848 }
Bookatz506a8182017-05-01 14:18:42 -07002849
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002850 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
2851 String type) {
2852 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2853 pw.print(',');
2854 pw.print(uid);
2855 pw.print(',');
2856 pw.print(category);
2857 pw.print(',');
2858 pw.print(type);
2859 }
2860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 /**
2862 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07002863 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 * @param pw the PageWriter to dump log to
2865 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2866 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2867 * @param args type-dependent data arguments
2868 */
Bookatzc8c44962017-05-11 12:12:54 -07002869 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002871 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002872 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002873 pw.print(',');
2874 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002876 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002878
2879 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002880 * Dump a given timer stat for terse checkin mode.
2881 *
2882 * @param pw the PageWriter to dump log to
2883 * @param uid the UID to log
2884 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2885 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2886 * @param timer a {@link Timer} to dump stats for
2887 * @param rawRealtime the current elapsed realtime of the system in microseconds
2888 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2889 */
2890 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2891 Timer timer, long rawRealtime, int which) {
2892 if (timer != null) {
2893 // Convert from microseconds to milliseconds with rounding
2894 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2895 / 1000;
2896 final int count = timer.getCountLocked(which);
2897 if (totalTime != 0) {
2898 dumpLine(pw, uid, category, type, totalTime, count);
2899 }
2900 }
2901 }
2902
2903 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002904 * Checks if the ControllerActivityCounter has any data worth dumping.
2905 */
2906 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
2907 if (counter == null) {
2908 return false;
2909 }
2910
2911 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
2912 || counter.getRxTimeCounter().getCountLocked(which) != 0
2913 || counter.getPowerCounter().getCountLocked(which) != 0) {
2914 return true;
2915 }
2916
2917 for (LongCounter c : counter.getTxTimeCounters()) {
2918 if (c.getCountLocked(which) != 0) {
2919 return true;
2920 }
2921 }
2922 return false;
2923 }
2924
2925 /**
2926 * Dumps the ControllerActivityCounter if it has any data worth dumping.
2927 * The order of the arguments in the final check in line is:
2928 *
2929 * idle, rx, power, tx...
2930 *
2931 * where tx... is one or more transmit level times.
2932 */
2933 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
2934 String type,
2935 ControllerActivityCounter counter,
2936 int which) {
2937 if (!controllerActivityHasData(counter, which)) {
2938 return;
2939 }
2940
2941 dumpLineHeader(pw, uid, category, type);
2942 pw.print(",");
2943 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
2944 pw.print(",");
2945 pw.print(counter.getRxTimeCounter().getCountLocked(which));
2946 pw.print(",");
2947 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
2948 for (LongCounter c : counter.getTxTimeCounters()) {
2949 pw.print(",");
2950 pw.print(c.getCountLocked(which));
2951 }
2952 pw.println();
2953 }
2954
2955 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
2956 String prefix, String controllerName,
2957 ControllerActivityCounter counter,
2958 int which) {
2959 if (controllerActivityHasData(counter, which)) {
2960 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
2961 }
2962 }
2963
2964 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
2965 String controllerName,
2966 ControllerActivityCounter counter, int which) {
2967 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
2968 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
2969 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
2970 long totalTxTimeMs = 0;
2971 for (LongCounter txState : counter.getTxTimeCounters()) {
2972 totalTxTimeMs += txState.getCountLocked(which);
2973 }
2974
2975 final long totalTimeMs = idleTimeMs + rxTimeMs + totalTxTimeMs;
2976
2977 sb.setLength(0);
2978 sb.append(prefix);
2979 sb.append(" ");
2980 sb.append(controllerName);
2981 sb.append(" Idle time: ");
2982 formatTimeMs(sb, idleTimeMs);
2983 sb.append("(");
2984 sb.append(formatRatioLocked(idleTimeMs, totalTimeMs));
2985 sb.append(")");
2986 pw.println(sb.toString());
2987
2988 sb.setLength(0);
2989 sb.append(prefix);
2990 sb.append(" ");
2991 sb.append(controllerName);
2992 sb.append(" Rx time: ");
2993 formatTimeMs(sb, rxTimeMs);
2994 sb.append("(");
2995 sb.append(formatRatioLocked(rxTimeMs, totalTimeMs));
2996 sb.append(")");
2997 pw.println(sb.toString());
2998
2999 sb.setLength(0);
3000 sb.append(prefix);
3001 sb.append(" ");
3002 sb.append(controllerName);
3003 sb.append(" Tx time: ");
3004 formatTimeMs(sb, totalTxTimeMs);
3005 sb.append("(");
3006 sb.append(formatRatioLocked(totalTxTimeMs, totalTimeMs));
3007 sb.append(")");
3008 pw.println(sb.toString());
3009
3010 final int numTxLvls = counter.getTxTimeCounters().length;
3011 if (numTxLvls > 1) {
3012 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3013 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3014 sb.setLength(0);
3015 sb.append(prefix);
3016 sb.append(" [");
3017 sb.append(lvl);
3018 sb.append("] ");
3019 formatTimeMs(sb, txLvlTimeMs);
3020 sb.append("(");
3021 sb.append(formatRatioLocked(txLvlTimeMs, totalTxTimeMs));
3022 sb.append(")");
3023 pw.println(sb.toString());
3024 }
3025 }
3026
3027 sb.setLength(0);
3028 sb.append(prefix);
3029 sb.append(" ");
3030 sb.append(controllerName);
3031 sb.append(" Power drain: ").append(
3032 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
3033 sb.append("mAh");
3034 pw.println(sb.toString());
3035 }
3036
3037 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003038 * Temporary for settings.
3039 */
3040 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3041 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3042 }
3043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 /**
3045 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003046 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003049 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3050 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3052 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
3053 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003054 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3055 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003056 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3057 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3058 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 final long totalRealtime = computeRealtime(rawRealtime, which);
3060 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003061 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003062 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003063 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003064 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3065 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003066 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003067 rawRealtime, which);
3068 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3069 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003070 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003071 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003072 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003073 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003074 final long dischargeCount = getDischargeCoulombCounter().getCountLocked(which);
3075 final long dischargeScreenOffCount = getDischargeScreenOffCoulombCounter()
3076 .getCountLocked(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003077
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003078 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003079
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003080 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003081 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003082
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003083 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003086 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003087 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003088 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003089 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003090 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003091 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003092 getEstimatedBatteryCapacity(),
3093 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity());
Adam Lesinski67c134f2016-06-10 15:15:08 -07003094
Bookatzc8c44962017-05-11 12:12:54 -07003095
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003096 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003097 long fullWakeLockTimeTotal = 0;
3098 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003099
Evan Millar22ac0432009-03-31 11:33:18 -07003100 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003101 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003102
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003103 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3104 = u.getWakelockStats();
3105 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3106 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003107
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003108 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3109 if (fullWakeTimer != null) {
3110 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3111 which);
3112 }
3113
3114 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3115 if (partialWakeTimer != null) {
3116 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3117 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003118 }
3119 }
3120 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003121
3122 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003123 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3124 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3125 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3126 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3127 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3128 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3129 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3130 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003131 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3132 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003133 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3134 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003135 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3136 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003137
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003138 // Dump Modem controller stats
3139 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3140 getModemControllerActivity(), which);
3141
Adam Lesinskie283d332015-04-16 12:29:25 -07003142 // Dump Wifi controller stats
3143 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3144 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003145 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003146 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003147
3148 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3149 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003150
3151 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003152 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3153 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003155 // Dump misc stats
3156 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003157 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003158 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003159 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003160 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003161 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003162 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3163 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003164 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003165 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3166 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3167 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3168 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003169 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003170
Dianne Hackborn617f8772009-03-31 15:04:46 -07003171 // Dump screen brightness stats
3172 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3173 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003174 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003175 }
3176 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003177
Dianne Hackborn627bba72009-03-24 22:32:56 -07003178 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003179 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3180 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003181 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003182 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003183 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003184 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003185 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003186 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003187 args[i] = getPhoneSignalStrengthCount(i, which);
3188 }
3189 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003190
Dianne Hackborn627bba72009-03-24 22:32:56 -07003191 // Dump network type stats
3192 args = new Object[NUM_DATA_CONNECTION_TYPES];
3193 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003194 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003195 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003196 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3197 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3198 args[i] = getPhoneDataConnectionCount(i, which);
3199 }
3200 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003201
3202 // Dump wifi state stats
3203 args = new Object[NUM_WIFI_STATES];
3204 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003205 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003206 }
3207 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3208 for (int i=0; i<NUM_WIFI_STATES; i++) {
3209 args[i] = getWifiStateCount(i, which);
3210 }
3211 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3212
Dianne Hackborn3251b902014-06-20 14:40:53 -07003213 // Dump wifi suppl state stats
3214 args = new Object[NUM_WIFI_SUPPL_STATES];
3215 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3216 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3217 }
3218 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3219 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3220 args[i] = getWifiSupplStateCount(i, which);
3221 }
3222 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3223
3224 // Dump wifi signal strength stats
3225 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3226 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3227 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3228 }
3229 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3230 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3231 args[i] = getWifiSignalStrengthCount(i, which);
3232 }
3233 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3234
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003235 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003236 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003237 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003238 }
Bookatzc8c44962017-05-11 12:12:54 -07003239
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003240 if (which == STATS_SINCE_UNPLUGGED) {
3241 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3242 getDischargeStartLevel()-getDischargeCurrentLevel(),
3243 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003244 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
3245 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003246 } else {
3247 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3248 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003249 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003250 getDischargeAmountScreenOffSinceCharge(),
3251 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003252 }
Bookatzc8c44962017-05-11 12:12:54 -07003253
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003254 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003255 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003256 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003257 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003258 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003259 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003260 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3261 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003262 }
Evan Millarc64edde2009-04-18 12:26:32 -07003263 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003264 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003265 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003266 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3267 // Not doing the regular wake lock formatting to remain compatible
3268 // with the old checkin format.
3269 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3270 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003271 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003272 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003273 }
3274 }
Evan Millarc64edde2009-04-18 12:26:32 -07003275 }
Bookatzc8c44962017-05-11 12:12:54 -07003276
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003277 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003278 helper.create(this);
3279 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003280 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003281 if (sippers != null && sippers.size() > 0) {
3282 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3283 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003284 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003285 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3286 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
3287 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003288 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003289 int uid = 0;
3290 String label;
3291 switch (bs.drainType) {
3292 case IDLE:
3293 label="idle";
3294 break;
3295 case CELL:
3296 label="cell";
3297 break;
3298 case PHONE:
3299 label="phone";
3300 break;
3301 case WIFI:
3302 label="wifi";
3303 break;
3304 case BLUETOOTH:
3305 label="blue";
3306 break;
3307 case SCREEN:
3308 label="scrn";
3309 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003310 case FLASHLIGHT:
3311 label="flashlight";
3312 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003313 case APP:
3314 uid = bs.uidObj.getUid();
3315 label = "uid";
3316 break;
3317 case USER:
3318 uid = UserHandle.getUid(bs.userId, 0);
3319 label = "user";
3320 break;
3321 case UNACCOUNTED:
3322 label = "unacc";
3323 break;
3324 case OVERCOUNTED:
3325 label = "over";
3326 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003327 case CAMERA:
3328 label = "camera";
3329 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003330 default:
3331 label = "???";
3332 }
3333 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Adam Lesinskie08af192015-03-25 16:42:59 -07003334 BatteryStatsHelper.makemAh(bs.totalPowerMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003335 }
3336 }
3337
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003338 final long[] cpuFreqs = getCpuFreqs();
3339 if (cpuFreqs != null) {
3340 sb.setLength(0);
3341 for (int i = 0; i < cpuFreqs.length; ++i) {
3342 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3343 }
3344 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3345 }
3346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 for (int iu = 0; iu < NU; iu++) {
3348 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003349 if (reqUid >= 0 && uid != reqUid) {
3350 continue;
3351 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003352 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003354 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003355 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3356 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3357 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3358 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3359 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3360 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3361 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3362 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003363 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003364 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3365 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003366 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003367 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3368 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003369 // Background data transfers
3370 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3371 which);
3372 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3373 which);
3374 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3375 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3376 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3377 which);
3378 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3379 which);
3380 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3381 which);
3382 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3383 which);
3384
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003385 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3386 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003387 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003388 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3389 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3390 || wifiBytesBgTx > 0
3391 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3392 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003393 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3394 wifiBytesRx, wifiBytesTx,
3395 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003396 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003397 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003398 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3399 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3400 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3401 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003402 }
3403
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003404 // Dump modem controller data, per UID.
3405 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3406 u.getModemControllerActivity(), which);
3407
3408 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003409 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3410 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3411 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003412 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3413 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003414 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3415 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3416 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003417 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003418 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003419 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3420 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003421 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3422 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003423 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003424 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003427 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3428 u.getWifiControllerActivity(), which);
3429
Bookatz867c0d72017-03-07 18:23:42 -08003430 final Timer bleTimer = u.getBluetoothScanTimer();
3431 if (bleTimer != null) {
3432 // Convert from microseconds to milliseconds with rounding
3433 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3434 / 1000;
3435 if (totalTime != 0) {
3436 final int count = bleTimer.getCountLocked(which);
3437 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3438 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
3439 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
3440 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3441 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3442 final long actualTimeBg = bleTimerBg != null ?
3443 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003444 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003445 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3446 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003447 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3448 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3449 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3450 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3451 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
3452 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3453 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
3454 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3455 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
3456 final Timer unoptimizedScanTimerBg =
3457 u.getBluetoothUnoptimizedScanBackgroundTimer();
3458 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
3459 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3460 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
3461 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3462
Bookatz867c0d72017-03-07 18:23:42 -08003463 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07003464 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
3465 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
3466 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08003467 }
3468 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003469
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003470 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3471 u.getBluetoothControllerActivity(), which);
3472
Dianne Hackborn617f8772009-03-31 15:04:46 -07003473 if (u.hasUserActivity()) {
3474 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3475 boolean hasData = false;
3476 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3477 int val = u.getUserActivityCount(i, which);
3478 args[i] = val;
3479 if (val != 0) hasData = true;
3480 }
3481 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003482 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003483 }
3484 }
Bookatzc8c44962017-05-11 12:12:54 -07003485
3486 if (u.getAggregatedPartialWakelockTimer() != null) {
3487 final Timer timer = u.getAggregatedPartialWakelockTimer();
3488 // Convert from microseconds to milliseconds with rounding
3489 final long totTimeMs = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3490 final Timer bgTimer = timer.getSubTimer();
3491 final long bgTimeMs = bgTimer != null ?
3492 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
3493 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
3494 }
3495
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003496 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3497 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3498 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3499 String linePrefix = "";
3500 sb.setLength(0);
3501 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3502 rawRealtime, "f", which, linePrefix);
3503 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
3504 rawRealtime, "p", which, linePrefix);
3505 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3506 rawRealtime, "w", which, linePrefix);
3507
3508 // Only log if we had at lease one wakelock...
3509 if (sb.length() > 0) {
3510 String name = wakelocks.keyAt(iw);
3511 if (name.indexOf(',') >= 0) {
3512 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003514 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 }
3516 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003517
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003518 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3519 for (int isy=syncs.size()-1; isy>=0; isy--) {
3520 final Timer timer = syncs.valueAt(isy);
3521 // Convert from microseconds to milliseconds with rounding
3522 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3523 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07003524 final Timer bgTimer = timer.getSubTimer();
3525 final long bgTime = bgTimer != null ?
3526 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
3527 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003528 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003529 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07003530 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003531 }
3532 }
3533
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003534 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3535 for (int ij=jobs.size()-1; ij>=0; ij--) {
3536 final Timer timer = jobs.valueAt(ij);
3537 // Convert from microseconds to milliseconds with rounding
3538 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3539 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07003540 final Timer bgTimer = timer.getSubTimer();
3541 final long bgTime = bgTimer != null ?
3542 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
3543 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003544 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003545 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07003546 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003547 }
3548 }
3549
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003550 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3551 rawRealtime, which);
3552 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3553 rawRealtime, which);
3554 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3555 rawRealtime, which);
3556 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3557 rawRealtime, which);
3558
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003559 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3560 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003561 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003562 final Uid.Sensor se = sensors.valueAt(ise);
3563 final int sensorNumber = sensors.keyAt(ise);
3564 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003565 if (timer != null) {
3566 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003567 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3568 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003569 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08003570 final int count = timer.getCountLocked(which);
3571 final Timer bgTimer = se.getSensorBackgroundTime();
3572 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
3573 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
3574 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3575 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
3576 final long bgActualTime = bgTimer != null ?
3577 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3578 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
3579 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003581 }
3582 }
3583
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003584 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3585 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003586
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003587 dumpTimer(pw, uid, category, FOREGROUND_DATA, u.getForegroundActivityTimer(),
3588 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003589
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003590 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003591 long totalStateTime = 0;
3592 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003593 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3594 totalStateTime += time;
3595 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003596 }
3597 if (totalStateTime > 0) {
3598 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3599 }
3600
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003601 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3602 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003603 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003604 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003605 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003606 }
3607
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003608 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
3609 // If total cpuFreqTimes is null, then we don't need to check for screenOffCpuFreqTimes.
3610 if (cpuFreqTimeMs != null) {
3611 sb.setLength(0);
3612 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
3613 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
3614 }
3615 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
3616 if (screenOffCpuFreqTimeMs != null) {
3617 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
3618 sb.append("," + screenOffCpuFreqTimeMs[i]);
3619 }
3620 } else {
3621 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
3622 sb.append(",0");
3623 }
3624 }
3625 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
3626 cpuFreqTimeMs.length, sb.toString());
3627 }
3628
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003629 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3630 = u.getProcessStats();
3631 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3632 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003633
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003634 final long userMillis = ps.getUserTime(which);
3635 final long systemMillis = ps.getSystemTime(which);
3636 final long foregroundMillis = ps.getForegroundTime(which);
3637 final int starts = ps.getStarts(which);
3638 final int numCrashes = ps.getNumCrashes(which);
3639 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003640
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003641 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3642 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003643 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
3644 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003645 }
3646 }
3647
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003648 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3649 = u.getPackageStats();
3650 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3651 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3652 int wakeups = 0;
3653 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3654 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07003655 int count = alarms.valueAt(iwa).getCountLocked(which);
3656 wakeups += count;
3657 String name = alarms.keyAt(iwa).replace(',', '_');
3658 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003659 }
3660 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3661 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3662 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3663 final long startTime = ss.getStartTime(batteryUptime, which);
3664 final int starts = ss.getStarts(which);
3665 final int launches = ss.getLaunches(which);
3666 if (startTime != 0 || starts != 0 || launches != 0) {
3667 dumpLine(pw, uid, category, APK_DATA,
3668 wakeups, // wakeup alarms
3669 packageStats.keyAt(ipkg), // Apk
3670 serviceStats.keyAt(isvc), // service
3671 startTime / 1000, // time spent started, in ms
3672 starts,
3673 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003674 }
3675 }
3676 }
3677 }
3678 }
3679
Dianne Hackborn81038902012-11-26 17:04:09 -08003680 static final class TimerEntry {
3681 final String mName;
3682 final int mId;
3683 final BatteryStats.Timer mTimer;
3684 final long mTime;
3685 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3686 mName = name;
3687 mId = id;
3688 mTimer = timer;
3689 mTime = time;
3690 }
3691 }
3692
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003693 private void printmAh(PrintWriter printer, double power) {
3694 printer.print(BatteryStatsHelper.makemAh(power));
3695 }
3696
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003697 private void printmAh(StringBuilder sb, double power) {
3698 sb.append(BatteryStatsHelper.makemAh(power));
3699 }
3700
Dianne Hackbornd953c532014-08-16 18:17:38 -07003701 /**
3702 * Temporary for settings.
3703 */
3704 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3705 int reqUid) {
3706 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3707 }
3708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003710 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003711 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003712 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3713 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
3714 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003715
3716 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3717 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3718 final long totalRealtime = computeRealtime(rawRealtime, which);
3719 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003720 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3721 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3722 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003723 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3724 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003725
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003726 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003727
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003728 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003729 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003731 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
3732 if (estimatedBatteryCapacity > 0) {
3733 sb.setLength(0);
3734 sb.append(prefix);
3735 sb.append(" Estimated battery capacity: ");
3736 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
3737 sb.append(" mAh");
3738 pw.println(sb.toString());
3739 }
3740
Jocelyn Dangc627d102017-04-14 13:15:14 -07003741 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
3742 if (minLearnedBatteryCapacity > 0) {
3743 sb.setLength(0);
3744 sb.append(prefix);
3745 sb.append(" Min learned battery capacity: ");
3746 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
3747 sb.append(" mAh");
3748 pw.println(sb.toString());
3749 }
3750 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
3751 if (maxLearnedBatteryCapacity > 0) {
3752 sb.setLength(0);
3753 sb.append(prefix);
3754 sb.append(" Max learned battery capacity: ");
3755 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
3756 sb.append(" mAh");
3757 pw.println(sb.toString());
3758 }
3759
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003760 sb.setLength(0);
3761 sb.append(prefix);
3762 sb.append(" Time on battery: ");
3763 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3764 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3765 sb.append(") realtime, ");
3766 formatTimeMs(sb, whichBatteryUptime / 1000);
3767 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3768 sb.append(") uptime");
3769 pw.println(sb.toString());
3770 sb.setLength(0);
3771 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003772 sb.append(" Time on battery screen off: ");
3773 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3774 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3775 sb.append(") realtime, ");
3776 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3777 sb.append("(");
3778 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3779 sb.append(") uptime");
3780 pw.println(sb.toString());
3781 sb.setLength(0);
3782 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003783 sb.append(" Total run time: ");
3784 formatTimeMs(sb, totalRealtime / 1000);
3785 sb.append("realtime, ");
3786 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003787 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003788 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003789 if (batteryTimeRemaining >= 0) {
3790 sb.setLength(0);
3791 sb.append(prefix);
3792 sb.append(" Battery time remaining: ");
3793 formatTimeMs(sb, batteryTimeRemaining / 1000);
3794 pw.println(sb.toString());
3795 }
3796 if (chargeTimeRemaining >= 0) {
3797 sb.setLength(0);
3798 sb.append(prefix);
3799 sb.append(" Charge time remaining: ");
3800 formatTimeMs(sb, chargeTimeRemaining / 1000);
3801 pw.println(sb.toString());
3802 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003803
3804 final LongCounter dischargeCounter = getDischargeCoulombCounter();
3805 final long dischargeCount = dischargeCounter.getCountLocked(which);
3806 if (dischargeCount >= 0) {
3807 sb.setLength(0);
3808 sb.append(prefix);
3809 sb.append(" Discharge: ");
3810 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
3811 sb.append(" mAh");
3812 pw.println(sb.toString());
3813 }
3814
3815 final LongCounter dischargeScreenOffCounter = getDischargeScreenOffCoulombCounter();
3816 final long dischargeScreenOffCount = dischargeScreenOffCounter.getCountLocked(which);
3817 if (dischargeScreenOffCount >= 0) {
3818 sb.setLength(0);
3819 sb.append(prefix);
3820 sb.append(" Screen off discharge: ");
3821 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
3822 sb.append(" mAh");
3823 pw.println(sb.toString());
3824 }
3825
3826 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
3827 if (dischargeScreenOnCount >= 0) {
3828 sb.setLength(0);
3829 sb.append(prefix);
3830 sb.append(" Screen on discharge: ");
3831 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
3832 sb.append(" mAh");
3833 pw.println(sb.toString());
3834 }
3835
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003836 pw.print(" Start clock time: ");
3837 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3838
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003839 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003840 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003841 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003842 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3843 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003844 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003845 rawRealtime, which);
3846 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3847 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003848 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003849 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003850 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3851 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3852 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003853 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003854 sb.append(prefix);
3855 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3856 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003857 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003858 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3859 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003860 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003861 pw.println(sb.toString());
3862 sb.setLength(0);
3863 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003864 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003865 boolean didOne = false;
3866 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003867 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003868 if (time == 0) {
3869 continue;
3870 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003871 sb.append("\n ");
3872 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003873 didOne = true;
3874 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3875 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003876 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003877 sb.append("(");
3878 sb.append(formatRatioLocked(time, screenOnTime));
3879 sb.append(")");
3880 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003881 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003882 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003883 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003884 sb.setLength(0);
3885 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003886 sb.append(" Power save mode enabled: ");
3887 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003888 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003889 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003890 sb.append(")");
3891 pw.println(sb.toString());
3892 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003893 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003894 sb.setLength(0);
3895 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003896 sb.append(" Device light idling: ");
3897 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003898 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003899 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
3900 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003901 sb.append("x");
3902 pw.println(sb.toString());
3903 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003904 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003905 sb.setLength(0);
3906 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003907 sb.append(" Idle mode light time: ");
3908 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003909 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003910 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
3911 sb.append(") ");
3912 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003913 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003914 sb.append(" -- longest ");
3915 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
3916 pw.println(sb.toString());
3917 }
3918 if (deviceIdlingTime != 0) {
3919 sb.setLength(0);
3920 sb.append(prefix);
3921 sb.append(" Device full idling: ");
3922 formatTimeMs(sb, deviceIdlingTime / 1000);
3923 sb.append("(");
3924 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003925 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003926 sb.append("x");
3927 pw.println(sb.toString());
3928 }
3929 if (deviceIdleModeFullTime != 0) {
3930 sb.setLength(0);
3931 sb.append(prefix);
3932 sb.append(" Idle mode full time: ");
3933 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
3934 sb.append("(");
3935 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
3936 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003937 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003938 sb.append("x");
3939 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003940 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003941 pw.println(sb.toString());
3942 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003943 if (phoneOnTime != 0) {
3944 sb.setLength(0);
3945 sb.append(prefix);
3946 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3947 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003948 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003949 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003950 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003951 if (connChanges != 0) {
3952 pw.print(prefix);
3953 pw.print(" Connectivity changes: "); pw.println(connChanges);
3954 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003955
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003956 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003957 long fullWakeLockTimeTotalMicros = 0;
3958 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003959
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003960 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003961
Evan Millar22ac0432009-03-31 11:33:18 -07003962 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003963 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003964
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003965 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3966 = u.getWakelockStats();
3967 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3968 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003969
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003970 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3971 if (fullWakeTimer != null) {
3972 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3973 rawRealtime, which);
3974 }
3975
3976 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3977 if (partialWakeTimer != null) {
3978 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3979 rawRealtime, which);
3980 if (totalTimeMicros > 0) {
3981 if (reqUid < 0) {
3982 // Only show the ordered list of all wake
3983 // locks if the caller is not asking for data
3984 // about a specific uid.
3985 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3986 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003987 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003988 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003989 }
3990 }
3991 }
3992 }
Bookatzc8c44962017-05-11 12:12:54 -07003993
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003994 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3995 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3996 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3997 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3998 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3999 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4000 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4001 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004002 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4003 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004004
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004005 if (fullWakeLockTimeTotalMicros != 0) {
4006 sb.setLength(0);
4007 sb.append(prefix);
4008 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4009 (fullWakeLockTimeTotalMicros + 500) / 1000);
4010 pw.println(sb.toString());
4011 }
4012
4013 if (partialWakeLockTimeTotalMicros != 0) {
4014 sb.setLength(0);
4015 sb.append(prefix);
4016 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4017 (partialWakeLockTimeTotalMicros + 500) / 1000);
4018 pw.println(sb.toString());
4019 }
4020
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004021 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004022 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
4023 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
4024 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
4025 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004026 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004027 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004028 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004029 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08004030 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004031 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004032 if (time == 0) {
4033 continue;
4034 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004035 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004036 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004037 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08004038 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004039 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004040 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004041 sb.append("(");
4042 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004043 sb.append(") ");
4044 sb.append(getPhoneSignalStrengthCount(i, which));
4045 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004046 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004047 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004048 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004049
4050 sb.setLength(0);
4051 sb.append(prefix);
4052 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004053 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004054 pw.println(sb.toString());
4055
Dianne Hackborn627bba72009-03-24 22:32:56 -07004056 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004057 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004058 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004059 didOne = false;
4060 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004061 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004062 if (time == 0) {
4063 continue;
4064 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004065 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004066 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004067 didOne = true;
4068 sb.append(DATA_CONNECTION_NAMES[i]);
4069 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004070 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004071 sb.append("(");
4072 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004073 sb.append(") ");
4074 sb.append(getPhoneDataConnectionCount(i, which));
4075 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004076 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004077 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004078 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004079
4080 sb.setLength(0);
4081 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004082 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004083 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004084 formatTimeMs(sb, mobileActiveTime / 1000);
4085 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4086 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
4087 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004088 pw.println(sb.toString());
4089
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004090 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
4091 if (mobileActiveUnknownTime != 0) {
4092 sb.setLength(0);
4093 sb.append(prefix);
4094 sb.append(" Mobile radio active unknown time: ");
4095 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
4096 sb.append("(");
4097 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
4098 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
4099 sb.append("x");
4100 pw.println(sb.toString());
4101 }
4102
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004103 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
4104 if (mobileActiveAdjustedTime != 0) {
4105 sb.setLength(0);
4106 sb.append(prefix);
4107 sb.append(" Mobile radio active adjusted time: ");
4108 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
4109 sb.append("(");
4110 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
4111 sb.append(")");
4112 pw.println(sb.toString());
4113 }
4114
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004115 printControllerActivity(pw, sb, prefix, "Radio", getModemControllerActivity(), which);
4116
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004117 pw.print(prefix);
4118 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
4119 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
4120 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
4121 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004122 sb.setLength(0);
4123 sb.append(prefix);
4124 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
4125 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
4126 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
4127 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004128 sb.append(")");
4129 pw.println(sb.toString());
4130
4131 sb.setLength(0);
4132 sb.append(prefix);
4133 sb.append(" Wifi states:");
4134 didOne = false;
4135 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004136 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004137 if (time == 0) {
4138 continue;
4139 }
4140 sb.append("\n ");
4141 didOne = true;
4142 sb.append(WIFI_STATE_NAMES[i]);
4143 sb.append(" ");
4144 formatTimeMs(sb, time/1000);
4145 sb.append("(");
4146 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4147 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004148 sb.append(getWifiStateCount(i, which));
4149 sb.append("x");
4150 }
4151 if (!didOne) sb.append(" (no activity)");
4152 pw.println(sb.toString());
4153
4154 sb.setLength(0);
4155 sb.append(prefix);
4156 sb.append(" Wifi supplicant states:");
4157 didOne = false;
4158 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4159 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4160 if (time == 0) {
4161 continue;
4162 }
4163 sb.append("\n ");
4164 didOne = true;
4165 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4166 sb.append(" ");
4167 formatTimeMs(sb, time/1000);
4168 sb.append("(");
4169 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4170 sb.append(") ");
4171 sb.append(getWifiSupplStateCount(i, which));
4172 sb.append("x");
4173 }
4174 if (!didOne) sb.append(" (no activity)");
4175 pw.println(sb.toString());
4176
4177 sb.setLength(0);
4178 sb.append(prefix);
4179 sb.append(" Wifi signal levels:");
4180 didOne = false;
4181 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4182 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4183 if (time == 0) {
4184 continue;
4185 }
4186 sb.append("\n ");
4187 sb.append(prefix);
4188 didOne = true;
4189 sb.append("level(");
4190 sb.append(i);
4191 sb.append(") ");
4192 formatTimeMs(sb, time/1000);
4193 sb.append("(");
4194 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4195 sb.append(") ");
4196 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004197 sb.append("x");
4198 }
4199 if (!didOne) sb.append(" (no activity)");
4200 pw.println(sb.toString());
4201
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004202 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004203
Adam Lesinski50e47602015-12-04 17:04:54 -08004204 pw.print(prefix);
4205 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4206 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4207
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004208 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4209 sb.setLength(0);
4210 sb.append(prefix);
4211 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4212 pw.println(sb.toString());
4213
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004214 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4215 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004216
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004217 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004218
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004219 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004220 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004221 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004222 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004223 pw.println(getDischargeStartLevel());
4224 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4225 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004226 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004227 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004228 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004229 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004230 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004231 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004232 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004233 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4234 pw.println(getDischargeAmountScreenOn());
4235 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4236 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004237 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004238 } else {
4239 pw.print(prefix); pw.println(" Device battery use since last full charge");
4240 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
4241 pw.println(getLowDischargeAmountSinceCharge());
4242 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
4243 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004244 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4245 pw.println(getDischargeAmountScreenOnSinceCharge());
4246 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4247 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004248 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004249 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004250
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004251 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004252 helper.create(this);
4253 helper.refreshStats(which, UserHandle.USER_ALL);
4254 List<BatterySipper> sippers = helper.getUsageList();
4255 if (sippers != null && sippers.size() > 0) {
4256 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4257 pw.print(prefix); pw.print(" Capacity: ");
4258 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004259 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004260 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4261 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4262 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4263 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004264 pw.println();
4265 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004266 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004267 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004268 switch (bs.drainType) {
4269 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004270 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004271 break;
4272 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004273 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004274 break;
4275 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004276 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004277 break;
4278 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004279 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004280 break;
4281 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004282 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004283 break;
4284 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004285 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004286 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004287 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004288 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004289 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004290 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004291 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004292 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004293 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004294 break;
4295 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004296 pw.print(" User "); pw.print(bs.userId);
4297 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004298 break;
4299 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004300 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004301 break;
4302 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004303 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004304 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004305 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004306 pw.print(" Camera: ");
4307 break;
4308 default:
4309 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004310 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004311 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004312 printmAh(pw, bs.totalPowerMah);
4313
Adam Lesinski57123002015-06-12 16:12:07 -07004314 if (bs.usagePowerMah != bs.totalPowerMah) {
4315 // If the usage (generic power) isn't the whole amount, we list out
4316 // what components are involved in the calculation.
4317
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004318 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004319 if (bs.usagePowerMah != 0) {
4320 pw.print(" usage=");
4321 printmAh(pw, bs.usagePowerMah);
4322 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004323 if (bs.cpuPowerMah != 0) {
4324 pw.print(" cpu=");
4325 printmAh(pw, bs.cpuPowerMah);
4326 }
4327 if (bs.wakeLockPowerMah != 0) {
4328 pw.print(" wake=");
4329 printmAh(pw, bs.wakeLockPowerMah);
4330 }
4331 if (bs.mobileRadioPowerMah != 0) {
4332 pw.print(" radio=");
4333 printmAh(pw, bs.mobileRadioPowerMah);
4334 }
4335 if (bs.wifiPowerMah != 0) {
4336 pw.print(" wifi=");
4337 printmAh(pw, bs.wifiPowerMah);
4338 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004339 if (bs.bluetoothPowerMah != 0) {
4340 pw.print(" bt=");
4341 printmAh(pw, bs.bluetoothPowerMah);
4342 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004343 if (bs.gpsPowerMah != 0) {
4344 pw.print(" gps=");
4345 printmAh(pw, bs.gpsPowerMah);
4346 }
4347 if (bs.sensorPowerMah != 0) {
4348 pw.print(" sensor=");
4349 printmAh(pw, bs.sensorPowerMah);
4350 }
4351 if (bs.cameraPowerMah != 0) {
4352 pw.print(" camera=");
4353 printmAh(pw, bs.cameraPowerMah);
4354 }
4355 if (bs.flashlightPowerMah != 0) {
4356 pw.print(" flash=");
4357 printmAh(pw, bs.flashlightPowerMah);
4358 }
4359 pw.print(" )");
4360 }
4361 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004362 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004363 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004364 }
4365
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004366 sippers = helper.getMobilemsppList();
4367 if (sippers != null && sippers.size() > 0) {
4368 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004369 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004370 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004371 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004372 sb.setLength(0);
4373 sb.append(prefix); sb.append(" Uid ");
4374 UserHandle.formatUid(sb, bs.uidObj.getUid());
4375 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4376 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4377 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004378 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004379 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004380 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004381 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004382 sb.setLength(0);
4383 sb.append(prefix);
4384 sb.append(" TOTAL TIME: ");
4385 formatTimeMs(sb, totalTime);
4386 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4387 sb.append(")");
4388 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004389 pw.println();
4390 }
4391
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004392 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4393 @Override
4394 public int compare(TimerEntry lhs, TimerEntry rhs) {
4395 long lhsTime = lhs.mTime;
4396 long rhsTime = rhs.mTime;
4397 if (lhsTime < rhsTime) {
4398 return 1;
4399 }
4400 if (lhsTime > rhsTime) {
4401 return -1;
4402 }
4403 return 0;
4404 }
4405 };
4406
4407 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004408 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4409 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004410 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004411 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4412 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4413 : kernelWakelocks.entrySet()) {
4414 final BatteryStats.Timer timer = ent.getValue();
4415 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004416 if (totalTimeMillis > 0) {
4417 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4418 }
4419 }
4420 if (ktimers.size() > 0) {
4421 Collections.sort(ktimers, timerComparator);
4422 pw.print(prefix); pw.println(" All kernel wake locks:");
4423 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004424 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004425 String linePrefix = ": ";
4426 sb.setLength(0);
4427 sb.append(prefix);
4428 sb.append(" Kernel Wake lock ");
4429 sb.append(timer.mName);
4430 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4431 which, linePrefix);
4432 if (!linePrefix.equals(": ")) {
4433 sb.append(" realtime");
4434 // Only print out wake locks that were held
4435 pw.println(sb.toString());
4436 }
4437 }
4438 pw.println();
4439 }
4440 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004441
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004442 if (timers.size() > 0) {
4443 Collections.sort(timers, timerComparator);
4444 pw.print(prefix); pw.println(" All partial wake locks:");
4445 for (int i=0; i<timers.size(); i++) {
4446 TimerEntry timer = timers.get(i);
4447 sb.setLength(0);
4448 sb.append(" Wake lock ");
4449 UserHandle.formatUid(sb, timer.mId);
4450 sb.append(" ");
4451 sb.append(timer.mName);
4452 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4453 sb.append(" realtime");
4454 pw.println(sb.toString());
4455 }
4456 timers.clear();
4457 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08004458 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004459
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004460 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004461 if (wakeupReasons.size() > 0) {
4462 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004463 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004464 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004465 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004466 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
4467 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004468 }
4469 Collections.sort(reasons, timerComparator);
4470 for (int i=0; i<reasons.size(); i++) {
4471 TimerEntry timer = reasons.get(i);
4472 String linePrefix = ": ";
4473 sb.setLength(0);
4474 sb.append(prefix);
4475 sb.append(" Wakeup reason ");
4476 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004477 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4478 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004479 pw.println(sb.toString());
4480 }
4481 pw.println();
4482 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004483 }
Evan Millar22ac0432009-03-31 11:33:18 -07004484
James Carr2dd7e5e2016-07-20 18:48:39 -07004485 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
4486 pw.println("Memory Stats");
4487 for (int i = 0; i < mMemoryStats.size(); i++) {
4488 sb.setLength(0);
4489 sb.append("Bandwidth ");
4490 sb.append(mMemoryStats.keyAt(i));
4491 sb.append(" Time ");
4492 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
4493 pw.println(sb.toString());
4494 }
4495
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004496 final long[] cpuFreqs = getCpuFreqs();
4497 if (cpuFreqs != null) {
4498 sb.setLength(0);
4499 sb.append("CPU freqs:");
4500 for (int i = 0; i < cpuFreqs.length; ++i) {
4501 sb.append(" " + cpuFreqs[i]);
4502 }
4503 pw.println(sb.toString());
4504 }
4505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004506 for (int iu=0; iu<NU; iu++) {
4507 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004508 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004509 continue;
4510 }
Bookatzc8c44962017-05-11 12:12:54 -07004511
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004512 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07004513
4514 pw.print(prefix);
4515 pw.print(" ");
4516 UserHandle.formatUid(pw, uid);
4517 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004518 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004519
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004520 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4521 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4522 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4523 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004524 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4525 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
4526
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004527 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4528 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004529 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4530 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004531
4532 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
4533 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
4534
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004535 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4536 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4537 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08004538 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
4539 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4540 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
4541 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004542 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004543
Adam Lesinski5f056f62016-07-14 16:56:08 -07004544 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
4545 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
4546
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004547 if (mobileRxBytes > 0 || mobileTxBytes > 0
4548 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004549 pw.print(prefix); pw.print(" Mobile network: ");
4550 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004551 pw.print(formatBytesLocked(mobileTxBytes));
4552 pw.print(" sent (packets "); pw.print(mobileRxPackets);
4553 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004554 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004555 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
4556 sb.setLength(0);
4557 sb.append(prefix); sb.append(" Mobile radio active: ");
4558 formatTimeMs(sb, uidMobileActiveTime / 1000);
4559 sb.append("(");
4560 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
4561 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
4562 long packets = mobileRxPackets + mobileTxPackets;
4563 if (packets == 0) {
4564 packets = 1;
4565 }
4566 sb.append(" @ ");
4567 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
4568 sb.append(" mspp");
4569 pw.println(sb.toString());
4570 }
4571
Adam Lesinski5f056f62016-07-14 16:56:08 -07004572 if (mobileWakeup > 0) {
4573 sb.setLength(0);
4574 sb.append(prefix);
4575 sb.append(" Mobile radio AP wakeups: ");
4576 sb.append(mobileWakeup);
4577 pw.println(sb.toString());
4578 }
4579
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004580 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
4581 u.getModemControllerActivity(), which);
4582
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004583 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004584 pw.print(prefix); pw.print(" Wi-Fi network: ");
4585 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004586 pw.print(formatBytesLocked(wifiTxBytes));
4587 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4588 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004589 }
4590
Dianne Hackborn62793e42015-03-09 11:15:41 -07004591 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08004592 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004593 || uidWifiRunningTime != 0) {
4594 sb.setLength(0);
4595 sb.append(prefix); sb.append(" Wifi Running: ");
4596 formatTimeMs(sb, uidWifiRunningTime / 1000);
4597 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4598 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07004599 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004600 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4601 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4602 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08004603 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004604 formatTimeMs(sb, wifiScanTime / 1000);
4605 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004606 whichBatteryRealtime)); sb.append(") ");
4607 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08004608 sb.append("x\n");
4609 // actual and background times are unpooled and since reset (regardless of 'which')
4610 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
4611 formatTimeMs(sb, wifiScanActualTime / 1000);
4612 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
4613 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4614 sb.append(") ");
4615 sb.append(wifiScanCount);
4616 sb.append("x\n");
4617 sb.append(prefix); sb.append(" Background Wifi Scan: ");
4618 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
4619 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
4620 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4621 sb.append(") ");
4622 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07004623 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004624 pw.println(sb.toString());
4625 }
4626
Adam Lesinski5f056f62016-07-14 16:56:08 -07004627 if (wifiWakeup > 0) {
4628 sb.setLength(0);
4629 sb.append(prefix);
4630 sb.append(" WiFi AP wakeups: ");
4631 sb.append(wifiWakeup);
4632 pw.println(sb.toString());
4633 }
4634
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004635 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
4636 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07004637
Adam Lesinski50e47602015-12-04 17:04:54 -08004638 if (btRxBytes > 0 || btTxBytes > 0) {
4639 pw.print(prefix); pw.print(" Bluetooth network: ");
4640 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
4641 pw.print(formatBytesLocked(btTxBytes));
4642 pw.println(" sent");
4643 }
4644
Bookatz867c0d72017-03-07 18:23:42 -08004645 final Timer bleTimer = u.getBluetoothScanTimer();
4646 if (bleTimer != null) {
4647 // Convert from microseconds to milliseconds with rounding
4648 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
4649 / 1000;
4650 if (totalTimeMs != 0) {
4651 final int count = bleTimer.getCountLocked(which);
4652 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
4653 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
4654 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
4655 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4656 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
4657 final long actualTimeMsBg = bleTimerBg != null ?
4658 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07004659 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07004660 final int resultCount = u.getBluetoothScanResultCounter() != null ?
4661 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07004662 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
4663 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
4664 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
4665 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
4666 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
4667 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4668 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
4669 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
4670 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
4671 final Timer unoptimizedScanTimerBg =
4672 u.getBluetoothUnoptimizedScanBackgroundTimer();
4673 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
4674 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4675 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
4676 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004677
4678 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08004679 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07004680 sb.append(prefix);
4681 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08004682 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07004683 sb.append(" (");
4684 sb.append(count);
4685 sb.append(" times)");
4686 if (bleTimer.isRunningLocked()) {
4687 sb.append(" (currently running)");
4688 }
4689 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08004690 }
Bookatzb1f04f32017-05-19 13:57:32 -07004691
4692 sb.append(prefix);
4693 sb.append(" Bluetooth Scan (total actual realtime): ");
4694 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
4695 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08004696 sb.append(count);
4697 sb.append(" times)");
4698 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07004699 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08004700 }
Bookatzb1f04f32017-05-19 13:57:32 -07004701 sb.append("\n");
4702 if (actualTimeMsBg > 0 || countBg > 0) {
4703 sb.append(prefix);
4704 sb.append(" Bluetooth Scan (background realtime): ");
4705 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
4706 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08004707 sb.append(countBg);
4708 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07004709 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
4710 sb.append(" (currently running in background)");
4711 }
4712 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08004713 }
Bookatzb1f04f32017-05-19 13:57:32 -07004714
4715 sb.append(prefix);
4716 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07004717 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07004718 sb.append(" (");
4719 sb.append(resultCountBg);
4720 sb.append(" in background)");
4721
4722 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
4723 sb.append("\n");
4724 sb.append(prefix);
4725 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
4726 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
4727 sb.append(" (max ");
4728 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
4729 sb.append(")");
4730 if (unoptimizedScanTimer != null
4731 && unoptimizedScanTimer.isRunningLocked()) {
4732 sb.append(" (currently running unoptimized)");
4733 }
4734 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
4735 sb.append("\n");
4736 sb.append(prefix);
4737 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
4738 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
4739 sb.append(" (max ");
4740 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
4741 sb.append(")");
4742 if (unoptimizedScanTimerBg.isRunningLocked()) {
4743 sb.append(" (currently running unoptimized in background)");
4744 }
4745 }
4746 }
Bookatz867c0d72017-03-07 18:23:42 -08004747 pw.println(sb.toString());
4748 uidActivity = true;
4749 }
4750 }
4751
4752
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004753
Dianne Hackborn617f8772009-03-31 15:04:46 -07004754 if (u.hasUserActivity()) {
4755 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07004756 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004757 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004758 if (val != 0) {
4759 if (!hasData) {
4760 sb.setLength(0);
4761 sb.append(" User activity: ");
4762 hasData = true;
4763 } else {
4764 sb.append(", ");
4765 }
4766 sb.append(val);
4767 sb.append(" ");
4768 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
4769 }
4770 }
4771 if (hasData) {
4772 pw.println(sb.toString());
4773 }
4774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004775
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004776 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4777 = u.getWakelockStats();
4778 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004779 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004780 int countWakelock = 0;
4781 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4782 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4783 String linePrefix = ": ";
4784 sb.setLength(0);
4785 sb.append(prefix);
4786 sb.append(" Wake lock ");
4787 sb.append(wakelocks.keyAt(iw));
4788 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
4789 "full", which, linePrefix);
4790 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
4791 "partial", which, linePrefix);
4792 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
4793 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004794 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
4795 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07004796 sb.append(" realtime");
4797 pw.println(sb.toString());
4798 uidActivity = true;
4799 countWakelock++;
4800
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004801 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
4802 rawRealtime, which);
4803 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
4804 rawRealtime, which);
4805 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
4806 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004807 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07004808 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004809 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004810 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07004811 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
4812 // pooled and therefore just a lower bound)
4813 long actualTotalPartialWakelock = 0;
4814 long actualBgPartialWakelock = 0;
4815 if (u.getAggregatedPartialWakelockTimer() != null) {
4816 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
4817 // Convert from microseconds to milliseconds with rounding
4818 actualTotalPartialWakelock =
4819 (aggTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4820 final Timer bgAggTimer = aggTimer.getSubTimer();
4821 actualBgPartialWakelock = bgAggTimer != null ?
4822 (bgAggTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : 0;
4823 }
4824
4825 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
4826 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
4827 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004828 sb.setLength(0);
4829 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004830 sb.append(" TOTAL wake: ");
4831 boolean needComma = false;
4832 if (totalFullWakelock != 0) {
4833 needComma = true;
4834 formatTimeMs(sb, totalFullWakelock);
4835 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004836 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004837 if (totalPartialWakelock != 0) {
4838 if (needComma) {
4839 sb.append(", ");
4840 }
4841 needComma = true;
4842 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07004843 sb.append("blamed partial");
4844 }
4845 if (actualTotalPartialWakelock != 0) {
4846 if (needComma) {
4847 sb.append(", ");
4848 }
4849 needComma = true;
4850 formatTimeMs(sb, actualTotalPartialWakelock);
4851 sb.append("actual partial");
4852 }
4853 if (actualBgPartialWakelock != 0) {
4854 if (needComma) {
4855 sb.append(", ");
4856 }
4857 needComma = true;
4858 formatTimeMs(sb, actualBgPartialWakelock);
4859 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004860 }
4861 if (totalWindowWakelock != 0) {
4862 if (needComma) {
4863 sb.append(", ");
4864 }
4865 needComma = true;
4866 formatTimeMs(sb, totalWindowWakelock);
4867 sb.append("window");
4868 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004869 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07004870 if (needComma) {
4871 sb.append(",");
4872 }
4873 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004874 formatTimeMs(sb, totalDrawWakelock);
4875 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07004876 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004877 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004878 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004879 }
4880 }
4881
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004882 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4883 for (int isy=syncs.size()-1; isy>=0; isy--) {
4884 final Timer timer = syncs.valueAt(isy);
4885 // Convert from microseconds to milliseconds with rounding
4886 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4887 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07004888 final Timer bgTimer = timer.getSubTimer();
4889 final long bgTime = bgTimer != null ?
4890 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
4891 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004892 sb.setLength(0);
4893 sb.append(prefix);
4894 sb.append(" Sync ");
4895 sb.append(syncs.keyAt(isy));
4896 sb.append(": ");
4897 if (totalTime != 0) {
4898 formatTimeMs(sb, totalTime);
4899 sb.append("realtime (");
4900 sb.append(count);
4901 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07004902 if (bgTime > 0) {
4903 sb.append(", ");
4904 formatTimeMs(sb, bgTime);
4905 sb.append("background (");
4906 sb.append(bgCount);
4907 sb.append(" times)");
4908 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004909 } else {
4910 sb.append("(not used)");
4911 }
4912 pw.println(sb.toString());
4913 uidActivity = true;
4914 }
4915
4916 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4917 for (int ij=jobs.size()-1; ij>=0; ij--) {
4918 final Timer timer = jobs.valueAt(ij);
4919 // Convert from microseconds to milliseconds with rounding
4920 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4921 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07004922 final Timer bgTimer = timer.getSubTimer();
4923 final long bgTime = bgTimer != null ?
4924 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
4925 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004926 sb.setLength(0);
4927 sb.append(prefix);
4928 sb.append(" Job ");
4929 sb.append(jobs.keyAt(ij));
4930 sb.append(": ");
4931 if (totalTime != 0) {
4932 formatTimeMs(sb, totalTime);
4933 sb.append("realtime (");
4934 sb.append(count);
4935 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07004936 if (bgTime > 0) {
4937 sb.append(", ");
4938 formatTimeMs(sb, bgTime);
4939 sb.append("background (");
4940 sb.append(bgCount);
4941 sb.append(" times)");
4942 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004943 } else {
4944 sb.append("(not used)");
4945 }
4946 pw.println(sb.toString());
4947 uidActivity = true;
4948 }
4949
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004950 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
4951 prefix, "Flashlight");
4952 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
4953 prefix, "Camera");
4954 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
4955 prefix, "Video");
4956 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
4957 prefix, "Audio");
4958
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004959 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4960 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004961 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004962 final Uid.Sensor se = sensors.valueAt(ise);
4963 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004964 sb.setLength(0);
4965 sb.append(prefix);
4966 sb.append(" Sensor ");
4967 int handle = se.getHandle();
4968 if (handle == Uid.Sensor.GPS) {
4969 sb.append("GPS");
4970 } else {
4971 sb.append(handle);
4972 }
4973 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004974
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004975 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004976 if (timer != null) {
4977 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08004978 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4979 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004980 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08004981 final Timer bgTimer = se.getSensorBackgroundTime();
4982 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
4983 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
4984 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4985 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4986 final long bgActualTime = bgTimer != null ?
4987 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4988
Dianne Hackborn61659e52014-07-09 16:13:01 -07004989 //timer.logState();
4990 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004991 if (actualTime != totalTime) {
4992 formatTimeMs(sb, totalTime);
4993 sb.append("blamed realtime, ");
4994 }
4995
4996 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07004997 sb.append("realtime (");
4998 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08004999 sb.append(" times)");
5000
5001 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005002 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005003 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5004 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005005 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005006 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005007 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005008 } else {
5009 sb.append("(not used)");
5010 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005011 } else {
5012 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005013 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005014
5015 pw.println(sb.toString());
5016 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005017 }
5018
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005019 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5020 "Vibrator");
5021 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5022 prefix, "Foreground activities");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005023
Dianne Hackborn61659e52014-07-09 16:13:01 -07005024 long totalStateTime = 0;
5025 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5026 long time = u.getProcessStateTime(ips, rawRealtime, which);
5027 if (time > 0) {
5028 totalStateTime += time;
5029 sb.setLength(0);
5030 sb.append(prefix);
5031 sb.append(" ");
5032 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5033 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005034 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005035 pw.println(sb.toString());
5036 uidActivity = true;
5037 }
5038 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005039 if (totalStateTime > 0) {
5040 sb.setLength(0);
5041 sb.append(prefix);
5042 sb.append(" Total running: ");
5043 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5044 pw.println(sb.toString());
5045 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005046
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005047 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5048 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005049 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005050 sb.setLength(0);
5051 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005052 sb.append(" Total cpu time: u=");
5053 formatTimeMs(sb, userCpuTimeUs / 1000);
5054 sb.append("s=");
5055 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005056 pw.println(sb.toString());
5057 }
5058
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005059 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5060 if (cpuFreqTimes != null) {
5061 sb.setLength(0);
5062 sb.append(" Total cpu time per freq:");
5063 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5064 sb.append(" " + cpuFreqTimes[i]);
5065 }
5066 pw.println(sb.toString());
5067 }
5068 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5069 if (screenOffCpuFreqTimes != null) {
5070 sb.setLength(0);
5071 sb.append(" Total screen-off cpu time per freq:");
5072 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5073 sb.append(" " + screenOffCpuFreqTimes[i]);
5074 }
5075 pw.println(sb.toString());
5076 }
5077
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005078 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5079 = u.getProcessStats();
5080 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5081 final Uid.Proc ps = processStats.valueAt(ipr);
5082 long userTime;
5083 long systemTime;
5084 long foregroundTime;
5085 int starts;
5086 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005088 userTime = ps.getUserTime(which);
5089 systemTime = ps.getSystemTime(which);
5090 foregroundTime = ps.getForegroundTime(which);
5091 starts = ps.getStarts(which);
5092 final int numCrashes = ps.getNumCrashes(which);
5093 final int numAnrs = ps.getNumAnrs(which);
5094 numExcessive = which == STATS_SINCE_CHARGED
5095 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005096
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005097 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5098 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5099 sb.setLength(0);
5100 sb.append(prefix); sb.append(" Proc ");
5101 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5102 sb.append(prefix); sb.append(" CPU: ");
5103 formatTimeMs(sb, userTime); sb.append("usr + ");
5104 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5105 formatTimeMs(sb, foregroundTime); sb.append("fg");
5106 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5107 sb.append("\n"); sb.append(prefix); sb.append(" ");
5108 boolean hasOne = false;
5109 if (starts != 0) {
5110 hasOne = true;
5111 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005112 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005113 if (numCrashes != 0) {
5114 if (hasOne) {
5115 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005116 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005117 hasOne = true;
5118 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005119 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005120 if (numAnrs != 0) {
5121 if (hasOne) {
5122 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005123 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005124 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005125 }
5126 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005127 pw.println(sb.toString());
5128 for (int e=0; e<numExcessive; e++) {
5129 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5130 if (ew != null) {
5131 pw.print(prefix); pw.print(" * Killed for ");
5132 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
5133 pw.print("wake lock");
5134 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
5135 pw.print("cpu");
5136 } else {
5137 pw.print("unknown");
5138 }
5139 pw.print(" use: ");
5140 TimeUtils.formatDuration(ew.usedTime, pw);
5141 pw.print(" over ");
5142 TimeUtils.formatDuration(ew.overTime, pw);
5143 if (ew.overTime != 0) {
5144 pw.print(" (");
5145 pw.print((ew.usedTime*100)/ew.overTime);
5146 pw.println("%)");
5147 }
5148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005149 }
5150 uidActivity = true;
5151 }
5152 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005153
5154 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5155 = u.getPackageStats();
5156 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
5157 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
5158 pw.println(":");
5159 boolean apkActivity = false;
5160 final Uid.Pkg ps = packageStats.valueAt(ipkg);
5161 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
5162 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
5163 pw.print(prefix); pw.print(" Wakeup alarm ");
5164 pw.print(alarms.keyAt(iwa)); pw.print(": ");
5165 pw.print(alarms.valueAt(iwa).getCountLocked(which));
5166 pw.println(" times");
5167 apkActivity = true;
5168 }
5169 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
5170 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
5171 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
5172 final long startTime = ss.getStartTime(batteryUptime, which);
5173 final int starts = ss.getStarts(which);
5174 final int launches = ss.getLaunches(which);
5175 if (startTime != 0 || starts != 0 || launches != 0) {
5176 sb.setLength(0);
5177 sb.append(prefix); sb.append(" Service ");
5178 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
5179 sb.append(prefix); sb.append(" Created for: ");
5180 formatTimeMs(sb, startTime / 1000);
5181 sb.append("uptime\n");
5182 sb.append(prefix); sb.append(" Starts: ");
5183 sb.append(starts);
5184 sb.append(", launches: "); sb.append(launches);
5185 pw.println(sb.toString());
5186 apkActivity = true;
5187 }
5188 }
5189 if (!apkActivity) {
5190 pw.print(prefix); pw.println(" (nothing executed)");
5191 }
5192 uidActivity = true;
5193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005194 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005195 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005196 }
5197 }
5198 }
5199
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005200 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005201 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005202 int diff = oldval ^ newval;
5203 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005204 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005205 for (int i=0; i<descriptions.length; i++) {
5206 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005207 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005208 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005209 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005210 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005211 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005212 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
5213 didWake = true;
5214 pw.print("=");
5215 if (longNames) {
5216 UserHandle.formatUid(pw, wakelockTag.uid);
5217 pw.print(":\"");
5218 pw.print(wakelockTag.string);
5219 pw.print("\"");
5220 } else {
5221 pw.print(wakelockTag.poolIdx);
5222 }
5223 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005224 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005225 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005226 pw.print("=");
5227 int val = (newval&bd.mask)>>bd.shift;
5228 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005229 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005230 } else {
5231 pw.print(val);
5232 }
5233 }
5234 }
5235 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005236 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07005237 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005238 if (longNames) {
5239 UserHandle.formatUid(pw, wakelockTag.uid);
5240 pw.print(":\"");
5241 pw.print(wakelockTag.string);
5242 pw.print("\"");
5243 } else {
5244 pw.print(wakelockTag.poolIdx);
5245 }
5246 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005247 }
5248
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005249 public void prepareForDumpLocked() {
5250 }
5251
5252 public static class HistoryPrinter {
5253 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005254 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005255 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005256 int oldStatus = -1;
5257 int oldHealth = -1;
5258 int oldPlug = -1;
5259 int oldTemp = -1;
5260 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005261 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005262 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005263
Dianne Hackborn3251b902014-06-20 14:40:53 -07005264 void reset() {
5265 oldState = oldState2 = 0;
5266 oldLevel = -1;
5267 oldStatus = -1;
5268 oldHealth = -1;
5269 oldPlug = -1;
5270 oldTemp = -1;
5271 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005272 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07005273 }
5274
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005275 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005276 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005277 if (!checkin) {
5278 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005279 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005280 pw.print(" (");
5281 pw.print(rec.numReadInts);
5282 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005283 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005284 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5285 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005286 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005287 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005288 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005289 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005290 }
5291 lastTime = rec.time;
5292 }
5293 if (rec.cmd == HistoryItem.CMD_START) {
5294 if (checkin) {
5295 pw.print(":");
5296 }
5297 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005298 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005299 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
5300 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005301 if (checkin) {
5302 pw.print(":");
5303 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005304 if (rec.cmd == HistoryItem.CMD_RESET) {
5305 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005306 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005307 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005308 pw.print("TIME:");
5309 if (checkin) {
5310 pw.println(rec.currentTime);
5311 } else {
5312 pw.print(" ");
5313 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5314 rec.currentTime).toString());
5315 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005316 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
5317 if (checkin) {
5318 pw.print(":");
5319 }
5320 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005321 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
5322 if (checkin) {
5323 pw.print(":");
5324 }
5325 pw.println("*OVERFLOW*");
5326 } else {
5327 if (!checkin) {
5328 if (rec.batteryLevel < 10) pw.print("00");
5329 else if (rec.batteryLevel < 100) pw.print("0");
5330 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005331 if (verbose) {
5332 pw.print(" ");
5333 if (rec.states < 0) ;
5334 else if (rec.states < 0x10) pw.print("0000000");
5335 else if (rec.states < 0x100) pw.print("000000");
5336 else if (rec.states < 0x1000) pw.print("00000");
5337 else if (rec.states < 0x10000) pw.print("0000");
5338 else if (rec.states < 0x100000) pw.print("000");
5339 else if (rec.states < 0x1000000) pw.print("00");
5340 else if (rec.states < 0x10000000) pw.print("0");
5341 pw.print(Integer.toHexString(rec.states));
5342 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005343 } else {
5344 if (oldLevel != rec.batteryLevel) {
5345 oldLevel = rec.batteryLevel;
5346 pw.print(",Bl="); pw.print(rec.batteryLevel);
5347 }
5348 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005349 if (oldStatus != rec.batteryStatus) {
5350 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005351 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005352 switch (oldStatus) {
5353 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005354 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005355 break;
5356 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005357 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005358 break;
5359 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005360 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005361 break;
5362 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005363 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005364 break;
5365 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005366 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005367 break;
5368 default:
5369 pw.print(oldStatus);
5370 break;
5371 }
5372 }
5373 if (oldHealth != rec.batteryHealth) {
5374 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005375 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005376 switch (oldHealth) {
5377 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005378 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005379 break;
5380 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005381 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005382 break;
5383 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005384 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005385 break;
5386 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005387 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005388 break;
5389 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005390 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005391 break;
5392 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005393 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005394 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005395 case BatteryManager.BATTERY_HEALTH_COLD:
5396 pw.print(checkin ? "c" : "cold");
5397 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005398 default:
5399 pw.print(oldHealth);
5400 break;
5401 }
5402 }
5403 if (oldPlug != rec.batteryPlugType) {
5404 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005405 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005406 switch (oldPlug) {
5407 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005408 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005409 break;
5410 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005411 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005412 break;
5413 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005414 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005415 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005416 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005417 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005418 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005419 default:
5420 pw.print(oldPlug);
5421 break;
5422 }
5423 }
5424 if (oldTemp != rec.batteryTemperature) {
5425 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005426 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005427 pw.print(oldTemp);
5428 }
5429 if (oldVolt != rec.batteryVoltage) {
5430 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005431 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005432 pw.print(oldVolt);
5433 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005434 final int chargeMAh = rec.batteryChargeUAh / 1000;
5435 if (oldChargeMAh != chargeMAh) {
5436 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07005437 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005438 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07005439 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005440 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005441 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005442 printBitDescriptions(pw, oldState2, rec.states2, null,
5443 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005444 if (rec.wakeReasonTag != null) {
5445 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005446 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005447 pw.print(rec.wakeReasonTag.poolIdx);
5448 } else {
5449 pw.print(" wake_reason=");
5450 pw.print(rec.wakeReasonTag.uid);
5451 pw.print(":\"");
5452 pw.print(rec.wakeReasonTag.string);
5453 pw.print("\"");
5454 }
5455 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08005456 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005457 pw.print(checkin ? "," : " ");
5458 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
5459 pw.print("+");
5460 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
5461 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005462 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005463 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
5464 : HISTORY_EVENT_NAMES;
5465 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
5466 | HistoryItem.EVENT_FLAG_FINISH);
5467 if (idx >= 0 && idx < eventNames.length) {
5468 pw.print(eventNames[idx]);
5469 } else {
5470 pw.print(checkin ? "Ev" : "event");
5471 pw.print(idx);
5472 }
5473 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005474 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005475 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08005476 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08005477 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
5478 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005479 pw.print(":\"");
5480 pw.print(rec.eventTag.string);
5481 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005482 }
5483 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005484 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005485 if (rec.stepDetails != null) {
5486 if (!checkin) {
5487 pw.print(" Details: cpu=");
5488 pw.print(rec.stepDetails.userTime);
5489 pw.print("u+");
5490 pw.print(rec.stepDetails.systemTime);
5491 pw.print("s");
5492 if (rec.stepDetails.appCpuUid1 >= 0) {
5493 pw.print(" (");
5494 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
5495 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5496 if (rec.stepDetails.appCpuUid2 >= 0) {
5497 pw.print(", ");
5498 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
5499 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5500 }
5501 if (rec.stepDetails.appCpuUid3 >= 0) {
5502 pw.print(", ");
5503 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
5504 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5505 }
5506 pw.print(')');
5507 }
5508 pw.println();
5509 pw.print(" /proc/stat=");
5510 pw.print(rec.stepDetails.statUserTime);
5511 pw.print(" usr, ");
5512 pw.print(rec.stepDetails.statSystemTime);
5513 pw.print(" sys, ");
5514 pw.print(rec.stepDetails.statIOWaitTime);
5515 pw.print(" io, ");
5516 pw.print(rec.stepDetails.statIrqTime);
5517 pw.print(" irq, ");
5518 pw.print(rec.stepDetails.statSoftIrqTime);
5519 pw.print(" sirq, ");
5520 pw.print(rec.stepDetails.statIdlTime);
5521 pw.print(" idle");
5522 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
5523 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
5524 + rec.stepDetails.statSoftIrqTime;
5525 int total = totalRun + rec.stepDetails.statIdlTime;
5526 if (total > 0) {
5527 pw.print(" (");
5528 float perc = ((float)totalRun) / ((float)total) * 100;
5529 pw.print(String.format("%.1f%%", perc));
5530 pw.print(" of ");
5531 StringBuilder sb = new StringBuilder(64);
5532 formatTimeMsNoSpace(sb, total*10);
5533 pw.print(sb);
5534 pw.print(")");
5535 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005536 pw.print(", PlatformIdleStat ");
5537 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005538 pw.println();
5539 } else {
5540 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5541 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
5542 pw.print(rec.stepDetails.userTime);
5543 pw.print(":");
5544 pw.print(rec.stepDetails.systemTime);
5545 if (rec.stepDetails.appCpuUid1 >= 0) {
5546 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
5547 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5548 if (rec.stepDetails.appCpuUid2 >= 0) {
5549 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
5550 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5551 }
5552 if (rec.stepDetails.appCpuUid3 >= 0) {
5553 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
5554 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5555 }
5556 }
5557 pw.println();
5558 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5559 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
5560 pw.print(rec.stepDetails.statUserTime);
5561 pw.print(',');
5562 pw.print(rec.stepDetails.statSystemTime);
5563 pw.print(',');
5564 pw.print(rec.stepDetails.statIOWaitTime);
5565 pw.print(',');
5566 pw.print(rec.stepDetails.statIrqTime);
5567 pw.print(',');
5568 pw.print(rec.stepDetails.statSoftIrqTime);
5569 pw.print(',');
5570 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005571 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005572 if (rec.stepDetails.statPlatformIdleState != null) {
5573 pw.print(rec.stepDetails.statPlatformIdleState);
5574 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005575 pw.println();
5576 }
5577 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005578 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005579 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005580 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005581 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005582
5583 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
5584 UserHandle.formatUid(pw, uid);
5585 pw.print("=");
5586 pw.print(utime);
5587 pw.print("u+");
5588 pw.print(stime);
5589 pw.print("s");
5590 }
5591
5592 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
5593 pw.print('/');
5594 pw.print(uid);
5595 pw.print(":");
5596 pw.print(utime);
5597 pw.print(":");
5598 pw.print(stime);
5599 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005600 }
5601
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005602 private void printSizeValue(PrintWriter pw, long size) {
5603 float result = size;
5604 String suffix = "";
5605 if (result >= 10*1024) {
5606 suffix = "KB";
5607 result = result / 1024;
5608 }
5609 if (result >= 10*1024) {
5610 suffix = "MB";
5611 result = result / 1024;
5612 }
5613 if (result >= 10*1024) {
5614 suffix = "GB";
5615 result = result / 1024;
5616 }
5617 if (result >= 10*1024) {
5618 suffix = "TB";
5619 result = result / 1024;
5620 }
5621 if (result >= 10*1024) {
5622 suffix = "PB";
5623 result = result / 1024;
5624 }
5625 pw.print((int)result);
5626 pw.print(suffix);
5627 }
5628
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005629 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
5630 String label3, long estimatedTime) {
5631 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005632 return false;
5633 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005634 pw.print(label1);
5635 pw.print(label2);
5636 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005637 StringBuilder sb = new StringBuilder(64);
5638 formatTimeMs(sb, estimatedTime);
5639 pw.print(sb);
5640 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005641 return true;
5642 }
5643
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005644 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
5645 LevelStepTracker steps, boolean checkin) {
5646 if (steps == null) {
5647 return false;
5648 }
5649 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005650 if (count <= 0) {
5651 return false;
5652 }
5653 if (!checkin) {
5654 pw.println(header);
5655 }
Kweku Adams030980a2015-04-01 16:07:48 -07005656 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005657 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005658 long duration = steps.getDurationAt(i);
5659 int level = steps.getLevelAt(i);
5660 long initMode = steps.getInitModeAt(i);
5661 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005662 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005663 lineArgs[0] = Long.toString(duration);
5664 lineArgs[1] = Integer.toString(level);
5665 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5666 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5667 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
5668 case Display.STATE_ON: lineArgs[2] = "s+"; break;
5669 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
5670 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07005671 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005672 }
5673 } else {
5674 lineArgs[2] = "";
5675 }
5676 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5677 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
5678 } else {
5679 lineArgs[3] = "";
5680 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005681 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07005682 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005683 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07005684 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005685 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005686 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
5687 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005688 pw.print(prefix);
5689 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005690 TimeUtils.formatDuration(duration, pw);
5691 pw.print(" to "); pw.print(level);
5692 boolean haveModes = false;
5693 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5694 pw.print(" (");
5695 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5696 case Display.STATE_OFF: pw.print("screen-off"); break;
5697 case Display.STATE_ON: pw.print("screen-on"); break;
5698 case Display.STATE_DOZE: pw.print("screen-doze"); break;
5699 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07005700 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005701 }
5702 haveModes = true;
5703 }
5704 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5705 pw.print(haveModes ? ", " : " (");
5706 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
5707 ? "power-save-on" : "power-save-off");
5708 haveModes = true;
5709 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005710 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
5711 pw.print(haveModes ? ", " : " (");
5712 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
5713 ? "device-idle-on" : "device-idle-off");
5714 haveModes = true;
5715 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005716 if (haveModes) {
5717 pw.print(")");
5718 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005719 pw.println();
5720 }
5721 }
5722 return true;
5723 }
5724
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005725 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005726 public static final int DUMP_DAILY_ONLY = 1<<2;
5727 public static final int DUMP_HISTORY_ONLY = 1<<3;
5728 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
5729 public static final int DUMP_VERBOSE = 1<<5;
5730 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005731
Dianne Hackborn37de0982014-05-09 09:32:18 -07005732 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
5733 final HistoryPrinter hprinter = new HistoryPrinter();
5734 final HistoryItem rec = new HistoryItem();
5735 long lastTime = -1;
5736 long baseTime = -1;
5737 boolean printed = false;
5738 HistoryEventTracker tracker = null;
5739 while (getNextHistoryLocked(rec)) {
5740 lastTime = rec.time;
5741 if (baseTime < 0) {
5742 baseTime = lastTime;
5743 }
5744 if (rec.time >= histStart) {
5745 if (histStart >= 0 && !printed) {
5746 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07005747 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005748 || rec.cmd == HistoryItem.CMD_START
5749 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005750 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005751 hprinter.printNextItem(pw, rec, baseTime, checkin,
5752 (flags&DUMP_VERBOSE) != 0);
5753 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005754 } else if (rec.currentTime != 0) {
5755 printed = true;
5756 byte cmd = rec.cmd;
5757 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005758 hprinter.printNextItem(pw, rec, baseTime, checkin,
5759 (flags&DUMP_VERBOSE) != 0);
5760 rec.cmd = cmd;
5761 }
5762 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005763 if (rec.cmd != HistoryItem.CMD_UPDATE) {
5764 hprinter.printNextItem(pw, rec, baseTime, checkin,
5765 (flags&DUMP_VERBOSE) != 0);
5766 rec.cmd = HistoryItem.CMD_UPDATE;
5767 }
5768 int oldEventCode = rec.eventCode;
5769 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005770 rec.eventTag = new HistoryTag();
5771 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
5772 HashMap<String, SparseIntArray> active
5773 = tracker.getStateForEvent(i);
5774 if (active == null) {
5775 continue;
5776 }
5777 for (HashMap.Entry<String, SparseIntArray> ent
5778 : active.entrySet()) {
5779 SparseIntArray uids = ent.getValue();
5780 for (int j=0; j<uids.size(); j++) {
5781 rec.eventCode = i;
5782 rec.eventTag.string = ent.getKey();
5783 rec.eventTag.uid = uids.keyAt(j);
5784 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07005785 hprinter.printNextItem(pw, rec, baseTime, checkin,
5786 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005787 rec.wakeReasonTag = null;
5788 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005789 }
5790 }
5791 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005792 rec.eventCode = oldEventCode;
5793 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005794 tracker = null;
5795 }
5796 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005797 hprinter.printNextItem(pw, rec, baseTime, checkin,
5798 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07005799 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
5800 // This is an attempt to aggregate the previous state and generate
5801 // fake events to reflect that state at the point where we start
5802 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07005803 if (tracker == null) {
5804 tracker = new HistoryEventTracker();
5805 }
5806 tracker.updateState(rec.eventCode, rec.eventTag.string,
5807 rec.eventTag.uid, rec.eventTag.poolIdx);
5808 }
5809 }
5810 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07005811 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005812 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
5813 }
5814 }
5815
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005816 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
5817 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
5818 if (steps == null) {
5819 return;
5820 }
5821 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
5822 if (timeRemaining >= 0) {
5823 pw.print(prefix); pw.print(label); pw.print(" total time: ");
5824 tmpSb.setLength(0);
5825 formatTimeMs(tmpSb, timeRemaining);
5826 pw.print(tmpSb);
5827 pw.print(" (from "); pw.print(tmpOutInt[0]);
5828 pw.println(" steps)");
5829 }
5830 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5831 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5832 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
5833 if (estimatedTime > 0) {
5834 pw.print(prefix); pw.print(label); pw.print(" ");
5835 pw.print(STEP_LEVEL_MODE_LABELS[i]);
5836 pw.print(" time: ");
5837 tmpSb.setLength(0);
5838 formatTimeMs(tmpSb, estimatedTime);
5839 pw.print(tmpSb);
5840 pw.print(" (from "); pw.print(tmpOutInt[0]);
5841 pw.println(" steps)");
5842 }
5843 }
5844 }
5845
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005846 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
5847 ArrayList<PackageChange> changes) {
5848 if (changes == null) {
5849 return;
5850 }
5851 pw.print(prefix); pw.println("Package changes:");
5852 for (int i=0; i<changes.size(); i++) {
5853 PackageChange pc = changes.get(i);
5854 if (pc.mUpdate) {
5855 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
5856 pw.print(" vers="); pw.println(pc.mVersionCode);
5857 } else {
5858 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
5859 }
5860 }
5861 }
5862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005863 /**
5864 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
5865 *
5866 * @param pw a Printer to receive the dump output.
5867 */
5868 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005869 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005870 prepareForDumpLocked();
5871
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005872 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005873 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005874
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005875 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005876 final long historyTotalSize = getHistoryTotalSize();
5877 final long historyUsedSize = getHistoryUsedSize();
5878 if (startIteratingHistoryLocked()) {
5879 try {
5880 pw.print("Battery History (");
5881 pw.print((100*historyUsedSize)/historyTotalSize);
5882 pw.print("% used, ");
5883 printSizeValue(pw, historyUsedSize);
5884 pw.print(" used of ");
5885 printSizeValue(pw, historyTotalSize);
5886 pw.print(", ");
5887 pw.print(getHistoryStringPoolSize());
5888 pw.print(" strings using ");
5889 printSizeValue(pw, getHistoryStringPoolBytes());
5890 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07005891 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005892 pw.println();
5893 } finally {
5894 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005895 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005896 }
5897
5898 if (startIteratingOldHistoryLocked()) {
5899 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005900 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005901 pw.println("Old battery History:");
5902 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005903 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005904 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005905 if (baseTime < 0) {
5906 baseTime = rec.time;
5907 }
5908 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005909 }
5910 pw.println();
5911 } finally {
5912 finishIteratingOldHistoryLocked();
5913 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005914 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005915 }
5916
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005917 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005918 return;
5919 }
5920
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005921 if (!filtering) {
5922 SparseArray<? extends Uid> uidStats = getUidStats();
5923 final int NU = uidStats.size();
5924 boolean didPid = false;
5925 long nowRealtime = SystemClock.elapsedRealtime();
5926 for (int i=0; i<NU; i++) {
5927 Uid uid = uidStats.valueAt(i);
5928 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
5929 if (pids != null) {
5930 for (int j=0; j<pids.size(); j++) {
5931 Uid.Pid pid = pids.valueAt(j);
5932 if (!didPid) {
5933 pw.println("Per-PID Stats:");
5934 didPid = true;
5935 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005936 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
5937 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005938 pw.print(" PID "); pw.print(pids.keyAt(j));
5939 pw.print(" wake time: ");
5940 TimeUtils.formatDuration(time, pw);
5941 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005942 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005943 }
5944 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005945 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005946 pw.println();
5947 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005948 }
5949
5950 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005951 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
5952 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005953 long timeRemaining = computeBatteryTimeRemaining(
5954 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005955 if (timeRemaining >= 0) {
5956 pw.print(" Estimated discharge time remaining: ");
5957 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5958 pw.println();
5959 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005960 final LevelStepTracker steps = getDischargeLevelStepTracker();
5961 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5962 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
5963 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5964 STEP_LEVEL_MODE_VALUES[i], null));
5965 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005966 pw.println();
5967 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005968 if (dumpDurationSteps(pw, " ", "Charge step durations:",
5969 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005970 long timeRemaining = computeChargeTimeRemaining(
5971 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005972 if (timeRemaining >= 0) {
5973 pw.print(" Estimated charge time remaining: ");
5974 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5975 pw.println();
5976 }
5977 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005978 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005979 }
5980 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
5981 pw.println("Daily stats:");
5982 pw.print(" Current start time: ");
5983 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5984 getCurrentDailyStartTime()).toString());
5985 pw.print(" Next min deadline: ");
5986 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5987 getNextMinDailyDeadline()).toString());
5988 pw.print(" Next max deadline: ");
5989 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5990 getNextMaxDailyDeadline()).toString());
5991 StringBuilder sb = new StringBuilder(64);
5992 int[] outInt = new int[1];
5993 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
5994 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005995 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
5996 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005997 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005998 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
5999 dsteps, false)) {
6000 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6001 sb, outInt);
6002 }
6003 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6004 csteps, false)) {
6005 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6006 sb, outInt);
6007 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006008 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006009 } else {
6010 pw.println(" Current daily steps:");
6011 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6012 sb, outInt);
6013 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6014 sb, outInt);
6015 }
6016 }
6017 DailyItem dit;
6018 int curIndex = 0;
6019 while ((dit=getDailyItemLocked(curIndex)) != null) {
6020 curIndex++;
6021 if ((flags&DUMP_DAILY_ONLY) != 0) {
6022 pw.println();
6023 }
6024 pw.print(" Daily from ");
6025 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6026 pw.print(" to ");
6027 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6028 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006029 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006030 if (dumpDurationSteps(pw, " ",
6031 " Discharge step durations:", dit.mDischargeSteps, false)) {
6032 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6033 sb, outInt);
6034 }
6035 if (dumpDurationSteps(pw, " ",
6036 " Charge step durations:", dit.mChargeSteps, false)) {
6037 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6038 sb, outInt);
6039 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006040 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006041 } else {
6042 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6043 sb, outInt);
6044 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6045 sb, outInt);
6046 }
6047 }
6048 pw.println();
6049 }
6050 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006051 pw.println("Statistics since last charge:");
6052 pw.println(" System starts: " + getStartCount()
6053 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006054 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6055 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006056 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006057 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006058 }
6059
6060 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006061 public void dumpCheckinLocked(Context context, PrintWriter pw,
6062 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006063 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006064
6065 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006066 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6067 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006068
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006069 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6070
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006071 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006072 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006073
6074 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07006075 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006076 try {
6077 for (int i=0; i<getHistoryStringPoolSize(); i++) {
6078 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6079 pw.print(HISTORY_STRING_POOL); pw.print(',');
6080 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006081 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006082 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006083 pw.print(",\"");
6084 String str = getHistoryTagPoolString(i);
6085 str = str.replace("\\", "\\\\");
6086 str = str.replace("\"", "\\\"");
6087 pw.print(str);
6088 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006089 pw.println();
6090 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006091 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006092 } finally {
6093 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08006094 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006095 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006096 }
6097
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006098 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006099 return;
6100 }
6101
Dianne Hackborne4a59512010-12-07 11:08:07 -08006102 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006103 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08006104 for (int i=0; i<apps.size(); i++) {
6105 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006106 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
6107 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08006108 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006109 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
6110 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006111 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006112 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006113 }
6114 SparseArray<? extends Uid> uidStats = getUidStats();
6115 final int NU = uidStats.size();
6116 String[] lineArgs = new String[2];
6117 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006118 int uid = UserHandle.getAppId(uidStats.keyAt(i));
6119 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
6120 if (pkgs != null && !pkgs.second.value) {
6121 pkgs.second.value = true;
6122 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08006123 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006124 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006125 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
6126 (Object[])lineArgs);
6127 }
6128 }
6129 }
6130 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006131 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006132 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006133 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07006134 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006135 if (timeRemaining >= 0) {
6136 lineArgs[0] = Long.toString(timeRemaining);
6137 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
6138 (Object[])lineArgs);
6139 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006140 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07006141 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006142 if (timeRemaining >= 0) {
6143 lineArgs[0] = Long.toString(timeRemaining);
6144 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
6145 (Object[])lineArgs);
6146 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006147 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
6148 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006151}