blob: 8d73544602baa0d0273fde25c5cae0329f1941ea [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;
Joe Onorato92fd23f2016-07-25 11:18:42 -070033import android.util.Log;
James Carr2dd7e5e2016-07-20 18:48:39 -070034import android.util.LongSparseArray;
Dianne Hackborn9cfba352016-03-24 17:31:28 -070035import android.util.MutableBoolean;
36import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.util.Printer;
38import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070039import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070040import android.util.TimeUtils;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070041import android.view.Display;
Amith Yamasaniab9ad192016-12-06 12:46:59 -080042
Dianne Hackborna7c837f2014-01-15 16:20:44 -080043import com.android.internal.os.BatterySipper;
44import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
46/**
47 * A class providing access to battery usage statistics, including information on
48 * wakelocks, processes, packages, and services. All times are represented in microseconds
49 * except where indicated otherwise.
50 * @hide
51 */
52public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070053 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
55 private static final boolean LOCAL_LOGV = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070056
57 /** @hide */
58 public static final String SERVICE_NAME = "batterystats";
59
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 /**
61 * A constant indicating a partial wake lock timer.
62 */
63 public static final int WAKE_TYPE_PARTIAL = 0;
64
65 /**
66 * A constant indicating a full wake lock timer.
67 */
68 public static final int WAKE_TYPE_FULL = 1;
69
70 /**
71 * A constant indicating a window wake lock timer.
72 */
73 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 /**
76 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 */
78 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070079
80 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070081 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070082 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070083 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070084
85 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070086 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070087 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070088 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070089
90 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070091 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070092 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070093 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
Dianne Hackborn62793e42015-03-09 11:15:41 -070095 /**
96 * A constant indicating a wifi multicast timer
97 */
98 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -070099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700101 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700102 */
103 public static final int VIDEO_TURNED_ON = 8;
104
105 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800106 * A constant indicating a vibrator on timer
107 */
108 public static final int VIBRATOR_ON = 9;
109
110 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700111 * A constant indicating a foreground activity timer
112 */
113 public static final int FOREGROUND_ACTIVITY = 10;
114
115 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700116 * A constant indicating a wifi batched scan is active
117 */
118 public static final int WIFI_BATCHED_SCAN = 11;
119
120 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700121 * A constant indicating a process state timer
122 */
123 public static final int PROCESS_STATE = 12;
124
125 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700126 * A constant indicating a sync timer
127 */
128 public static final int SYNC = 13;
129
130 /**
131 * A constant indicating a job timer
132 */
133 public static final int JOB = 14;
134
135 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800136 * A constant indicating an audio turn on timer
137 */
138 public static final int AUDIO_TURNED_ON = 15;
139
140 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700141 * A constant indicating a flashlight turn on timer
142 */
143 public static final int FLASHLIGHT_TURNED_ON = 16;
144
145 /**
146 * A constant indicating a camera turn on timer
147 */
148 public static final int CAMERA_TURNED_ON = 17;
149
150 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700151 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700152 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700153 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700154
155 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800156 * A constant indicating a bluetooth scan timer.
157 */
158 public static final int BLUETOOTH_SCAN_ON = 19;
159
160 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 * Include all of the data in the stats, including previously saved data.
162 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700163 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164
165 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 * Include only the current run in the stats.
167 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700168 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169
170 /**
171 * Include only the run since the last time the device was unplugged in the stats.
172 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700173 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700174
175 // NOTE: Update this list if you add/change any stats above.
176 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700177 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700178 private static final String[] STAT_NAMES = { "l", "c", "u" };
179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700181 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700182 *
183 * New in version 19:
184 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800185 * New in version 20:
186 * - Sensor gets a background counter.
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700187 */
Kweku Adams47db5a82016-12-09 19:04:50 -0800188 static final String CHECKIN_VERSION = "20";
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700189
190 /**
191 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700193 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700194
Evan Millar22ac0432009-03-31 11:33:18 -0700195 private static final long BYTES_PER_KB = 1024;
196 private static final long BYTES_PER_MB = 1048576; // 1024^2
197 private static final long BYTES_PER_GB = 1073741824; //1024^3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700199 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800200 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700201 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700203 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700204 private static final String CPU_DATA = "cpu";
Evan Millare84de8d2009-04-02 22:16:12 -0700205 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800206 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700207 private static final String FOREGROUND_DATA = "fg";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700208 private static final String STATE_TIME_DATA = "st";
Evan Millare84de8d2009-04-02 22:16:12 -0700209 private static final String WAKELOCK_DATA = "wl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700210 private static final String SYNC_DATA = "sy";
211 private static final String JOB_DATA = "jb";
Evan Millarc64edde2009-04-18 12:26:32 -0700212 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700213 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700214 private static final String NETWORK_DATA = "nt";
215 private static final String USER_ACTIVITY_DATA = "ua";
216 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800217 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700218 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700219 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700220 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800221 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
222 private static final String WIFI_CONTROLLER_DATA = "wfcd";
223 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
224 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700225 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700226 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800227 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800228 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
229 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800230 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700231 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700232 private static final String SCREEN_BRIGHTNESS_DATA = "br";
233 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700234 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700235 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
236 private static final String DATA_CONNECTION_TIME_DATA = "dct";
237 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800238 private static final String WIFI_STATE_TIME_DATA = "wst";
239 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700240 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
241 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
242 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
243 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800244 private static final String POWER_USE_SUMMARY_DATA = "pws";
245 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700246 private static final String DISCHARGE_STEP_DATA = "dsd";
247 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700248 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
249 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700250 private static final String FLASHLIGHT_DATA = "fla";
251 private static final String CAMERA_DATA = "cam";
252 private static final String VIDEO_DATA = "vid";
253 private static final String AUDIO_DATA = "aud";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254
Adam Lesinski010bf372016-04-11 12:18:18 -0700255 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
256
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700257 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 private final Formatter mFormatter = new Formatter(mFormatBuilder);
259
260 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700261 * State for keeping track of counting information.
262 */
263 public static abstract class Counter {
264
265 /**
266 * Returns the count associated with this Counter for the
267 * selected type of statistics.
268 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700269 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700270 */
Evan Millarc64edde2009-04-18 12:26:32 -0700271 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700272
273 /**
274 * Temporary for debugging.
275 */
276 public abstract void logState(Printer pw, String prefix);
277 }
278
279 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700280 * State for keeping track of long counting information.
281 */
282 public static abstract class LongCounter {
283
284 /**
285 * Returns the count associated with this Counter for the
286 * selected type of statistics.
287 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700288 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700289 */
290 public abstract long getCountLocked(int which);
291
292 /**
293 * Temporary for debugging.
294 */
295 public abstract void logState(Printer pw, String prefix);
296 }
297
298 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800299 * Container class that aggregates counters for transmit, receive, and idle state of a
300 * radio controller.
301 */
302 public static abstract class ControllerActivityCounter {
303 /**
304 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
305 * idle state.
306 */
307 public abstract LongCounter getIdleTimeCounter();
308
309 /**
310 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
311 * receive state.
312 */
313 public abstract LongCounter getRxTimeCounter();
314
315 /**
316 * An array of {@link LongCounter}, representing various transmit levels, where each level
317 * may draw a different amount of power. The levels themselves are controller-specific.
318 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
319 * various transmit level states.
320 */
321 public abstract LongCounter[] getTxTimeCounters();
322
323 /**
324 * @return a non-null {@link LongCounter} representing the power consumed by the controller
325 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
326 * yield a value of 0 if the device doesn't support power calculations.
327 */
328 public abstract LongCounter getPowerCounter();
329 }
330
331 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 * State for keeping track of timing information.
333 */
334 public static abstract class Timer {
335
336 /**
337 * Returns the count associated with this Timer for the
338 * selected type of statistics.
339 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700340 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 */
Evan Millarc64edde2009-04-18 12:26:32 -0700342 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343
344 /**
345 * Returns the total time in microseconds associated with this Timer for the
346 * selected type of statistics.
347 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800348 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700349 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 * @return a time in microseconds
351 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800352 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700355 * Returns the total time in microseconds associated with this Timer since the
356 * 'mark' was last set.
357 *
358 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
359 * @return a time in microseconds
360 */
361 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
362
363 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700364 * Returns the max duration if it is being tracked.
365 * Not all Timer subclasses track the max duration and the current duration.
366
367 */
368 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
369 return -1;
370 }
371
372 /**
373 * Returns the current time the timer has been active, if it is being tracked.
374 * Not all Timer subclasses track the max duration and the current duration.
375 */
376 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
377 return -1;
378 }
379
380 /**
381 * Returns whether the timer is currently running. Some types of timers
382 * (e.g. BatchTimers) don't know whether the event is currently active,
383 * and report false.
384 */
385 public boolean isRunningLocked() {
386 return false;
387 }
388
389 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 * Temporary for debugging.
391 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700392 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 }
394
395 /**
396 * The statistics associated with a particular uid.
397 */
398 public static abstract class Uid {
399
400 /**
401 * Returns a mapping containing wakelock statistics.
402 *
403 * @return a Map from Strings to Uid.Wakelock objects.
404 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700405 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406
407 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700408 * Returns a mapping containing sync statistics.
409 *
410 * @return a Map from Strings to Timer objects.
411 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700412 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700413
414 /**
415 * Returns a mapping containing scheduled job statistics.
416 *
417 * @return a Map from Strings to Timer objects.
418 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700419 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700420
421 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 * The statistics associated with a particular wake lock.
423 */
424 public static abstract class Wakelock {
425 public abstract Timer getWakeTime(int type);
426 }
427
428 /**
429 * Returns a mapping containing sensor statistics.
430 *
431 * @return a Map from Integer sensor ids to Uid.Sensor objects.
432 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700433 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434
435 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700436 * Returns a mapping containing active process data.
437 */
438 public abstract SparseArray<? extends Pid> getPidStats();
439
440 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 * Returns a mapping containing process statistics.
442 *
443 * @return a Map from Strings to Uid.Proc objects.
444 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700445 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446
447 /**
448 * Returns a mapping containing package statistics.
449 *
450 * @return a Map from Strings to Uid.Pkg objects.
451 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700452 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700453
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800454 public abstract ControllerActivityCounter getWifiControllerActivity();
455 public abstract ControllerActivityCounter getBluetoothControllerActivity();
456 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800457
458 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 * {@hide}
460 */
461 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700462
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800463 public abstract void noteWifiRunningLocked(long elapsedRealtime);
464 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
465 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
466 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
467 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
468 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
469 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
470 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
471 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
472 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800473 public abstract void noteActivityResumedLocked(long elapsedRealtime);
474 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800475 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
476 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
477 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700478 public abstract int getWifiScanCount(int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800479 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700480 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800481 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700482 public abstract Timer getAudioTurnedOnTimer();
483 public abstract Timer getVideoTurnedOnTimer();
484 public abstract Timer getFlashlightTurnedOnTimer();
485 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700486 public abstract Timer getForegroundActivityTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800487 public abstract Timer getBluetoothScanTimer();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700488
Dianne Hackborna0200e32016-03-30 18:01:41 -0700489 // Note: the following times are disjoint. They can be added together to find the
490 // total time a uid has had any processes running at all.
491
492 /**
493 * Time this uid has any processes in the top state (or above such as persistent).
494 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800495 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700496 /**
497 * Time this uid has any process with a started out bound foreground service, but
498 * none in the "top" state.
499 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800500 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700501 /**
502 * Time this uid has any process that is top while the device is sleeping, but none
503 * in the "foreground service" or better state.
504 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800505 public static final int PROCESS_STATE_TOP_SLEEPING = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700506 /**
507 * Time this uid has any process in an active foreground state, but none in the
508 * "top sleeping" or better state.
509 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800510 public static final int PROCESS_STATE_FOREGROUND = 3;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700511 /**
512 * Time this uid has any process in an active background state, but none in the
513 * "foreground" or better state.
514 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800515 public static final int PROCESS_STATE_BACKGROUND = 4;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700516 /**
517 * Time this uid has any processes that are sitting around cached, not in one of the
518 * other active states.
519 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800520 public static final int PROCESS_STATE_CACHED = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700521 /**
522 * Total number of process states we track.
523 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800524 public static final int NUM_PROCESS_STATE = 6;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700525
526 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackborna8d10942015-11-19 17:55:19 -0800527 "Top", "Fg Service", "Top Sleeping", "Foreground", "Background", "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700528 };
529
530 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800531 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700532
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800533 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534
Robert Greenwalta029ea12013-09-25 16:38:12 -0700535 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
536
Dianne Hackborn617f8772009-03-31 15:04:46 -0700537 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700538 * Note that these must match the constants in android.os.PowerManager.
539 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
540 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700541 */
542 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700543 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700544 };
545
Phil Weaverda80d672016-03-15 16:25:46 -0700546 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700547
Dianne Hackborn617f8772009-03-31 15:04:46 -0700548 public abstract void noteUserActivityLocked(int type);
549 public abstract boolean hasUserActivity();
550 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700551
552 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800553 public abstract long getNetworkActivityBytes(int type, int which);
554 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800555 public abstract long getMobileRadioActiveTime(int which);
556 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700557
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700558 /**
559 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
560 */
561 public abstract long getUserCpuTimeUs(int which);
562
563 /**
564 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
565 */
566 public abstract long getSystemCpuTimeUs(int which);
567
568 /**
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700569 * Get the total cpu power consumed (in milli-ampere-microseconds).
570 */
571 public abstract long getCpuPowerMaUs(int which);
572
573 /**
Adam Lesinski6832f392015-09-05 18:05:40 -0700574 * Returns the approximate cpu time (in milliseconds) spent at a certain CPU speed for a
575 * given CPU cluster.
576 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700577 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700578 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700579 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
580 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700581 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700582 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700583
Adam Lesinski5f056f62016-07-14 16:56:08 -0700584 /**
585 * Returns the number of times this UID woke up the Application Processor to
586 * process a mobile radio packet.
587 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
588 */
589 public abstract long getMobileRadioApWakeupCount(int which);
590
591 /**
592 * Returns the number of times this UID woke up the Application Processor to
593 * process a WiFi packet.
594 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
595 */
596 public abstract long getWifiRadioApWakeupCount(int which);
597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800599 /*
600 * FIXME: it's not correct to use this magic value because it
601 * could clash with a sensor handle (which are defined by
602 * the sensor HAL, and therefore out of our control
603 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 // Magic sensor number for the GPS.
605 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800610
611 /** Returns a counter for usage count when in the background. */
612 public abstract Counter getSensorBgCount();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 }
614
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700615 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800616 public int mWakeNesting;
617 public long mWakeSumMs;
618 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700619 }
620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 /**
622 * The statistics associated with a particular process.
623 */
624 public static abstract class Proc {
625
Dianne Hackborn287952c2010-09-22 22:34:31 -0700626 public static class ExcessivePower {
627 public static final int TYPE_WAKE = 1;
628 public static final int TYPE_CPU = 2;
629
630 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700631 public long overTime;
632 public long usedTime;
633 }
634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800636 * Returns true if this process is still active in the battery stats.
637 */
638 public abstract boolean isActive();
639
640 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700641 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700643 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 */
645 public abstract long getUserTime(int which);
646
647 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700648 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700650 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 */
652 public abstract long getSystemTime(int which);
653
654 /**
655 * Returns the number of times the process has been started.
656 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700657 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 */
659 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700660
661 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800662 * Returns the number of times the process has crashed.
663 *
664 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
665 */
666 public abstract int getNumCrashes(int which);
667
668 /**
669 * Returns the number of times the process has ANRed.
670 *
671 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
672 */
673 public abstract int getNumAnrs(int which);
674
675 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700676 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700677 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700678 * @return foreground cpu time in microseconds
679 */
680 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700681
Dianne Hackborn287952c2010-09-22 22:34:31 -0700682 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700683
Dianne Hackborn287952c2010-09-22 22:34:31 -0700684 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 }
686
687 /**
688 * The statistics associated with a particular package.
689 */
690 public static abstract class Pkg {
691
692 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700693 * Returns information about all wakeup alarms that have been triggered for this
694 * package. The mapping keys are tag names for the alarms, the counter contains
695 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700697 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698
699 /**
700 * Returns a mapping containing service statistics.
701 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700702 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703
704 /**
705 * The statistics associated with a particular service.
706 */
Joe Onoratoabded112016-02-08 16:49:39 -0800707 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708
709 /**
710 * Returns the amount of time spent started.
711 *
712 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700713 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 * @return
715 */
716 public abstract long getStartTime(long batteryUptime, int which);
717
718 /**
719 * Returns the total number of times startService() has been called.
720 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700721 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 */
723 public abstract int getStarts(int which);
724
725 /**
726 * Returns the total number times the service has been launched.
727 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700728 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 */
730 public abstract int getLaunches(int which);
731 }
732 }
733 }
734
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800735 public static final class LevelStepTracker {
736 public long mLastStepTime = -1;
737 public int mNumStepDurations;
738 public final long[] mStepDurations;
739
740 public LevelStepTracker(int maxLevelSteps) {
741 mStepDurations = new long[maxLevelSteps];
742 }
743
744 public LevelStepTracker(int numSteps, long[] steps) {
745 mNumStepDurations = numSteps;
746 mStepDurations = new long[numSteps];
747 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
748 }
749
750 public long getDurationAt(int index) {
751 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
752 }
753
754 public int getLevelAt(int index) {
755 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
756 >> STEP_LEVEL_LEVEL_SHIFT);
757 }
758
759 public int getInitModeAt(int index) {
760 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
761 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
762 }
763
764 public int getModModeAt(int index) {
765 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
766 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
767 }
768
769 private void appendHex(long val, int topOffset, StringBuilder out) {
770 boolean hasData = false;
771 while (topOffset >= 0) {
772 int digit = (int)( (val>>topOffset) & 0xf );
773 topOffset -= 4;
774 if (!hasData && digit == 0) {
775 continue;
776 }
777 hasData = true;
778 if (digit >= 0 && digit <= 9) {
779 out.append((char)('0' + digit));
780 } else {
781 out.append((char)('a' + digit - 10));
782 }
783 }
784 }
785
786 public void encodeEntryAt(int index, StringBuilder out) {
787 long item = mStepDurations[index];
788 long duration = item & STEP_LEVEL_TIME_MASK;
789 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
790 >> STEP_LEVEL_LEVEL_SHIFT);
791 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
792 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
793 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
794 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
795 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
796 case Display.STATE_OFF: out.append('f'); break;
797 case Display.STATE_ON: out.append('o'); break;
798 case Display.STATE_DOZE: out.append('d'); break;
799 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
800 }
801 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
802 out.append('p');
803 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700804 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
805 out.append('i');
806 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800807 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
808 case Display.STATE_OFF: out.append('F'); break;
809 case Display.STATE_ON: out.append('O'); break;
810 case Display.STATE_DOZE: out.append('D'); break;
811 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
812 }
813 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
814 out.append('P');
815 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700816 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
817 out.append('I');
818 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800819 out.append('-');
820 appendHex(level, 4, out);
821 out.append('-');
822 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
823 }
824
825 public void decodeEntryAt(int index, String value) {
826 final int N = value.length();
827 int i = 0;
828 char c;
829 long out = 0;
830 while (i < N && (c=value.charAt(i)) != '-') {
831 i++;
832 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800833 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800834 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800835 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800836 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800837 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800838 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800839 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
840 << STEP_LEVEL_INITIAL_MODE_SHIFT);
841 break;
842 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
843 << STEP_LEVEL_INITIAL_MODE_SHIFT);
844 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700845 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
846 << STEP_LEVEL_INITIAL_MODE_SHIFT);
847 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800848 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
849 break;
850 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
851 break;
852 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
853 break;
854 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
855 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
856 break;
857 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
858 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800859 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700860 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
861 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
862 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800863 }
864 }
865 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800866 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800867 while (i < N && (c=value.charAt(i)) != '-') {
868 i++;
869 level <<= 4;
870 if (c >= '0' && c <= '9') {
871 level += c - '0';
872 } else if (c >= 'a' && c <= 'f') {
873 level += c - 'a' + 10;
874 } else if (c >= 'A' && c <= 'F') {
875 level += c - 'A' + 10;
876 }
877 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800878 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800879 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
880 long duration = 0;
881 while (i < N && (c=value.charAt(i)) != '-') {
882 i++;
883 duration <<= 4;
884 if (c >= '0' && c <= '9') {
885 duration += c - '0';
886 } else if (c >= 'a' && c <= 'f') {
887 duration += c - 'a' + 10;
888 } else if (c >= 'A' && c <= 'F') {
889 duration += c - 'A' + 10;
890 }
891 }
892 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
893 }
894
895 public void init() {
896 mLastStepTime = -1;
897 mNumStepDurations = 0;
898 }
899
900 public void clearTime() {
901 mLastStepTime = -1;
902 }
903
904 public long computeTimePerLevel() {
905 final long[] steps = mStepDurations;
906 final int numSteps = mNumStepDurations;
907
908 // For now we'll do a simple average across all steps.
909 if (numSteps <= 0) {
910 return -1;
911 }
912 long total = 0;
913 for (int i=0; i<numSteps; i++) {
914 total += steps[i] & STEP_LEVEL_TIME_MASK;
915 }
916 return total / numSteps;
917 /*
918 long[] buckets = new long[numSteps];
919 int numBuckets = 0;
920 int numToAverage = 4;
921 int i = 0;
922 while (i < numSteps) {
923 long totalTime = 0;
924 int num = 0;
925 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
926 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
927 num++;
928 }
929 buckets[numBuckets] = totalTime / num;
930 numBuckets++;
931 numToAverage *= 2;
932 i += num;
933 }
934 if (numBuckets < 1) {
935 return -1;
936 }
937 long averageTime = buckets[numBuckets-1];
938 for (i=numBuckets-2; i>=0; i--) {
939 averageTime = (averageTime + buckets[i]) / 2;
940 }
941 return averageTime;
942 */
943 }
944
945 public long computeTimeEstimate(long modesOfInterest, long modeValues,
946 int[] outNumOfInterest) {
947 final long[] steps = mStepDurations;
948 final int count = mNumStepDurations;
949 if (count <= 0) {
950 return -1;
951 }
952 long total = 0;
953 int numOfInterest = 0;
954 for (int i=0; i<count; i++) {
955 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
956 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
957 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
958 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
959 // If the modes of interest didn't change during this step period...
960 if ((modMode&modesOfInterest) == 0) {
961 // And the mode values during this period match those we are measuring...
962 if ((initMode&modesOfInterest) == modeValues) {
963 // Then this can be used to estimate the total time!
964 numOfInterest++;
965 total += steps[i] & STEP_LEVEL_TIME_MASK;
966 }
967 }
968 }
969 if (numOfInterest <= 0) {
970 return -1;
971 }
972
973 if (outNumOfInterest != null) {
974 outNumOfInterest[0] = numOfInterest;
975 }
976
977 // The estimated time is the average time we spend in each level, multipled
978 // by 100 -- the total number of battery levels
979 return (total / numOfInterest) * 100;
980 }
981
982 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
983 int stepCount = mNumStepDurations;
984 final long lastStepTime = mLastStepTime;
985 if (lastStepTime >= 0 && numStepLevels > 0) {
986 final long[] steps = mStepDurations;
987 long duration = elapsedRealtime - lastStepTime;
988 for (int i=0; i<numStepLevels; i++) {
989 System.arraycopy(steps, 0, steps, 1, steps.length-1);
990 long thisDuration = duration / (numStepLevels-i);
991 duration -= thisDuration;
992 if (thisDuration > STEP_LEVEL_TIME_MASK) {
993 thisDuration = STEP_LEVEL_TIME_MASK;
994 }
995 steps[0] = thisDuration | modeBits;
996 }
997 stepCount += numStepLevels;
998 if (stepCount > steps.length) {
999 stepCount = steps.length;
1000 }
1001 }
1002 mNumStepDurations = stepCount;
1003 mLastStepTime = elapsedRealtime;
1004 }
1005
1006 public void readFromParcel(Parcel in) {
1007 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001008 if (N > mStepDurations.length) {
1009 throw new ParcelFormatException("more step durations than available: " + N);
1010 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001011 mNumStepDurations = N;
1012 for (int i=0; i<N; i++) {
1013 mStepDurations[i] = in.readLong();
1014 }
1015 }
1016
1017 public void writeToParcel(Parcel out) {
1018 final int N = mNumStepDurations;
1019 out.writeInt(N);
1020 for (int i=0; i<N; i++) {
1021 out.writeLong(mStepDurations[i]);
1022 }
1023 }
1024 }
1025
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001026 public static final class PackageChange {
1027 public String mPackageName;
1028 public boolean mUpdate;
1029 public int mVersionCode;
1030 }
1031
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001032 public static final class DailyItem {
1033 public long mStartTime;
1034 public long mEndTime;
1035 public LevelStepTracker mDischargeSteps;
1036 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001037 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001038 }
1039
1040 public abstract DailyItem getDailyItemLocked(int daysAgo);
1041
1042 public abstract long getCurrentDailyStartTime();
1043
1044 public abstract long getNextMinDailyDeadline();
1045
1046 public abstract long getNextMaxDailyDeadline();
1047
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001048 public final static class HistoryTag {
1049 public String string;
1050 public int uid;
1051
1052 public int poolIdx;
1053
1054 public void setTo(HistoryTag o) {
1055 string = o.string;
1056 uid = o.uid;
1057 poolIdx = o.poolIdx;
1058 }
1059
1060 public void setTo(String _string, int _uid) {
1061 string = _string;
1062 uid = _uid;
1063 poolIdx = -1;
1064 }
1065
1066 public void writeToParcel(Parcel dest, int flags) {
1067 dest.writeString(string);
1068 dest.writeInt(uid);
1069 }
1070
1071 public void readFromParcel(Parcel src) {
1072 string = src.readString();
1073 uid = src.readInt();
1074 poolIdx = -1;
1075 }
1076
1077 @Override
1078 public boolean equals(Object o) {
1079 if (this == o) return true;
1080 if (o == null || getClass() != o.getClass()) return false;
1081
1082 HistoryTag that = (HistoryTag) o;
1083
1084 if (uid != that.uid) return false;
1085 if (!string.equals(that.string)) return false;
1086
1087 return true;
1088 }
1089
1090 @Override
1091 public int hashCode() {
1092 int result = string.hashCode();
1093 result = 31 * result + uid;
1094 return result;
1095 }
1096 }
1097
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001098 /**
1099 * Optional detailed information that can go into a history step. This is typically
1100 * generated each time the battery level changes.
1101 */
1102 public final static class HistoryStepDetails {
1103 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1104 public int userTime;
1105 public int systemTime;
1106
1107 // Top three apps using CPU in the last step, with times in 1/100 second.
1108 public int appCpuUid1;
1109 public int appCpuUTime1;
1110 public int appCpuSTime1;
1111 public int appCpuUid2;
1112 public int appCpuUTime2;
1113 public int appCpuSTime2;
1114 public int appCpuUid3;
1115 public int appCpuUTime3;
1116 public int appCpuSTime3;
1117
1118 // Information from /proc/stat
1119 public int statUserTime;
1120 public int statSystemTime;
1121 public int statIOWaitTime;
1122 public int statIrqTime;
1123 public int statSoftIrqTime;
1124 public int statIdlTime;
1125
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001126 // Platform-level low power state stats
1127 public String statPlatformIdleState;
1128
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001129 public HistoryStepDetails() {
1130 clear();
1131 }
1132
1133 public void clear() {
1134 userTime = systemTime = 0;
1135 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1136 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1137 = appCpuUTime3 = appCpuSTime3 = 0;
1138 }
1139
1140 public void writeToParcel(Parcel out) {
1141 out.writeInt(userTime);
1142 out.writeInt(systemTime);
1143 out.writeInt(appCpuUid1);
1144 out.writeInt(appCpuUTime1);
1145 out.writeInt(appCpuSTime1);
1146 out.writeInt(appCpuUid2);
1147 out.writeInt(appCpuUTime2);
1148 out.writeInt(appCpuSTime2);
1149 out.writeInt(appCpuUid3);
1150 out.writeInt(appCpuUTime3);
1151 out.writeInt(appCpuSTime3);
1152 out.writeInt(statUserTime);
1153 out.writeInt(statSystemTime);
1154 out.writeInt(statIOWaitTime);
1155 out.writeInt(statIrqTime);
1156 out.writeInt(statSoftIrqTime);
1157 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001158 out.writeString(statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001159 }
1160
1161 public void readFromParcel(Parcel in) {
1162 userTime = in.readInt();
1163 systemTime = in.readInt();
1164 appCpuUid1 = in.readInt();
1165 appCpuUTime1 = in.readInt();
1166 appCpuSTime1 = in.readInt();
1167 appCpuUid2 = in.readInt();
1168 appCpuUTime2 = in.readInt();
1169 appCpuSTime2 = in.readInt();
1170 appCpuUid3 = in.readInt();
1171 appCpuUTime3 = in.readInt();
1172 appCpuSTime3 = in.readInt();
1173 statUserTime = in.readInt();
1174 statSystemTime = in.readInt();
1175 statIOWaitTime = in.readInt();
1176 statIrqTime = in.readInt();
1177 statSoftIrqTime = in.readInt();
1178 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001179 statPlatformIdleState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001180 }
1181 }
1182
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001183 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001184 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001185
1186 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001187 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001188
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001189 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001190 public static final byte CMD_NULL = -1;
1191 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001192 public static final byte CMD_CURRENT_TIME = 5;
1193 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001194 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001195 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001196
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001197 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001198
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001199 /**
1200 * Return whether the command code is a delta data update.
1201 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001202 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001203 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001204 }
1205
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001206 public byte batteryLevel;
1207 public byte batteryStatus;
1208 public byte batteryHealth;
1209 public byte batteryPlugType;
1210
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001211 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001212 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001213
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001214 // The charge of the battery in micro-Ampere-hours.
1215 public int batteryChargeUAh;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001216
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001217 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001218 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001219 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001220 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001221 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1222 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001223 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001224 public static final int STATE_PHONE_STATE_SHIFT = 6;
1225 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001226 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001227 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001228 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001229
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001230 // These states always appear directly in the first int token
1231 // of a delta change; they should be ones that change relatively
1232 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001233 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1234 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001235 public static final int STATE_GPS_ON_FLAG = 1<<29;
1236 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001237 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001238 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001239 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001240 // Do not use, this is used for coulomb delta count.
1241 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001242 // These are on the lower bits used for the command; if they change
1243 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001244 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001245 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001246 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001247 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1248 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1249 // empty slot
1250 // empty slot
1251 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001252
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001253 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001254 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1255
1256 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001257
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001258 public int states;
1259
Dianne Hackborn3251b902014-06-20 14:40:53 -07001260 // Constants from WIFI_SUPPL_STATE_*
1261 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1262 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1263 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1264 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1265 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1266 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1267
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001268 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001269 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1270 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1271 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001272 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001273 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1274 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1275 public static final int STATE2_CHARGING_FLAG = 1<<24;
1276 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1277 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1278 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001279 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001280
1281 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001282 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001283 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1284
1285 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001286
Dianne Hackborn40c87252014-03-19 16:55:40 -07001287 public int states2;
1288
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001289 // The wake lock that was acquired at this point.
1290 public HistoryTag wakelockTag;
1291
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001292 // Kernel wakeup reason at this point.
1293 public HistoryTag wakeReasonTag;
1294
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001295 // Non-null when there is more detailed information at this step.
1296 public HistoryStepDetails stepDetails;
1297
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001298 public static final int EVENT_FLAG_START = 0x8000;
1299 public static final int EVENT_FLAG_FINISH = 0x4000;
1300
1301 // No event in this item.
1302 public static final int EVENT_NONE = 0x0000;
1303 // Event is about a process that is running.
1304 public static final int EVENT_PROC = 0x0001;
1305 // Event is about an application package that is in the foreground.
1306 public static final int EVENT_FOREGROUND = 0x0002;
1307 // Event is about an application package that is at the top of the screen.
1308 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001309 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001310 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001311 // Events for all additional wake locks aquired/release within a wake block.
1312 // These are not generated by default.
1313 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001314 // Event is about an application executing a scheduled job.
1315 public static final int EVENT_JOB = 0x0006;
1316 // Events for users running.
1317 public static final int EVENT_USER_RUNNING = 0x0007;
1318 // Events for foreground user.
1319 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001320 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001321 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001322 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001323 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001324 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001325 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001326 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001327 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001328 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001329 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001330 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001331 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001332 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001333 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001334 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001335 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001336 // Event for a package being on the temporary whitelist.
1337 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001338 // Event for the screen waking up.
1339 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001340 // Event for the UID that woke up the application processor.
1341 // Used for wakeups coming from WiFi, modem, etc.
1342 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001343 // Event for reporting that a specific partial wake lock has been held for a long duration.
1344 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001345
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001346 // Number of event types.
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001347 public static final int EVENT_COUNT = 0x0015;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001348 // Mask to extract out only the type part of the event.
1349 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001350
1351 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1352 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1353 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1354 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1355 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1356 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001357 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1358 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001359 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1360 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001361 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1362 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1363 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1364 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1365 public static final int EVENT_USER_FOREGROUND_START =
1366 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1367 public static final int EVENT_USER_FOREGROUND_FINISH =
1368 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001369 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1370 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001371 public static final int EVENT_TEMP_WHITELIST_START =
1372 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1373 public static final int EVENT_TEMP_WHITELIST_FINISH =
1374 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001375 public static final int EVENT_LONG_WAKE_LOCK_START =
1376 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1377 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1378 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001379
1380 // For CMD_EVENT.
1381 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001382 public HistoryTag eventTag;
1383
Dianne Hackborn9a755432014-05-15 17:05:22 -07001384 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001385 public long currentTime;
1386
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001387 // Meta-data when reading.
1388 public int numReadInts;
1389
1390 // Pre-allocated objects.
1391 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001392 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001393 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001394
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001395 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001396 }
1397
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001398 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001399 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001400 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001401 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001402 }
1403
1404 public int describeContents() {
1405 return 0;
1406 }
1407
1408 public void writeToParcel(Parcel dest, int flags) {
1409 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001410 int bat = (((int)cmd)&0xff)
1411 | ((((int)batteryLevel)<<8)&0xff00)
1412 | ((((int)batteryStatus)<<16)&0xf0000)
1413 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001414 | ((((int)batteryPlugType)<<24)&0xf000000)
1415 | (wakelockTag != null ? 0x10000000 : 0)
1416 | (wakeReasonTag != null ? 0x20000000 : 0)
1417 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001418 dest.writeInt(bat);
1419 bat = (((int)batteryTemperature)&0xffff)
1420 | ((((int)batteryVoltage)<<16)&0xffff0000);
1421 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001422 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001423 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001424 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001425 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001426 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001427 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001428 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001429 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001430 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001431 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001432 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001433 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001434 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001435 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001436 dest.writeLong(currentTime);
1437 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001438 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001439
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001440 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001441 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001442 int bat = src.readInt();
1443 cmd = (byte)(bat&0xff);
1444 batteryLevel = (byte)((bat>>8)&0xff);
1445 batteryStatus = (byte)((bat>>16)&0xf);
1446 batteryHealth = (byte)((bat>>20)&0xf);
1447 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001448 int bat2 = src.readInt();
1449 batteryTemperature = (short)(bat2&0xffff);
1450 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001451 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001452 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001453 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001454 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001455 wakelockTag = localWakelockTag;
1456 wakelockTag.readFromParcel(src);
1457 } else {
1458 wakelockTag = null;
1459 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001460 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001461 wakeReasonTag = localWakeReasonTag;
1462 wakeReasonTag.readFromParcel(src);
1463 } else {
1464 wakeReasonTag = null;
1465 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001466 if ((bat&0x40000000) != 0) {
1467 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001468 eventTag = localEventTag;
1469 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001470 } else {
1471 eventCode = EVENT_NONE;
1472 eventTag = null;
1473 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001474 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001475 currentTime = src.readLong();
1476 } else {
1477 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001478 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001479 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001480 }
1481
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001482 public void clear() {
1483 time = 0;
1484 cmd = CMD_NULL;
1485 batteryLevel = 0;
1486 batteryStatus = 0;
1487 batteryHealth = 0;
1488 batteryPlugType = 0;
1489 batteryTemperature = 0;
1490 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001491 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001492 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001493 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001494 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001495 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001496 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001497 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001498 }
1499
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001500 public void setTo(HistoryItem o) {
1501 time = o.time;
1502 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001503 setToCommon(o);
1504 }
1505
1506 public void setTo(long time, byte cmd, HistoryItem o) {
1507 this.time = time;
1508 this.cmd = cmd;
1509 setToCommon(o);
1510 }
1511
1512 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001513 batteryLevel = o.batteryLevel;
1514 batteryStatus = o.batteryStatus;
1515 batteryHealth = o.batteryHealth;
1516 batteryPlugType = o.batteryPlugType;
1517 batteryTemperature = o.batteryTemperature;
1518 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001519 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001520 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001521 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001522 if (o.wakelockTag != null) {
1523 wakelockTag = localWakelockTag;
1524 wakelockTag.setTo(o.wakelockTag);
1525 } else {
1526 wakelockTag = null;
1527 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001528 if (o.wakeReasonTag != null) {
1529 wakeReasonTag = localWakeReasonTag;
1530 wakeReasonTag.setTo(o.wakeReasonTag);
1531 } else {
1532 wakeReasonTag = null;
1533 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001534 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001535 if (o.eventTag != null) {
1536 eventTag = localEventTag;
1537 eventTag.setTo(o.eventTag);
1538 } else {
1539 eventTag = null;
1540 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001541 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001542 }
1543
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001544 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001545 return batteryLevel == o.batteryLevel
1546 && batteryStatus == o.batteryStatus
1547 && batteryHealth == o.batteryHealth
1548 && batteryPlugType == o.batteryPlugType
1549 && batteryTemperature == o.batteryTemperature
1550 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001551 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001552 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001553 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001554 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001555 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001556
1557 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001558 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001559 return false;
1560 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001561 if (wakelockTag != o.wakelockTag) {
1562 if (wakelockTag == null || o.wakelockTag == null) {
1563 return false;
1564 }
1565 if (!wakelockTag.equals(o.wakelockTag)) {
1566 return false;
1567 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001568 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001569 if (wakeReasonTag != o.wakeReasonTag) {
1570 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1571 return false;
1572 }
1573 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1574 return false;
1575 }
1576 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001577 if (eventTag != o.eventTag) {
1578 if (eventTag == null || o.eventTag == null) {
1579 return false;
1580 }
1581 if (!eventTag.equals(o.eventTag)) {
1582 return false;
1583 }
1584 }
1585 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001586 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001587 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001588
1589 public final static class HistoryEventTracker {
1590 private final HashMap<String, SparseIntArray>[] mActiveEvents
1591 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1592
1593 public boolean updateState(int code, String name, int uid, int poolIdx) {
1594 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1595 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1596 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1597 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001598 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001599 mActiveEvents[idx] = active;
1600 }
1601 SparseIntArray uids = active.get(name);
1602 if (uids == null) {
1603 uids = new SparseIntArray();
1604 active.put(name, uids);
1605 }
1606 if (uids.indexOfKey(uid) >= 0) {
1607 // Already set, nothing to do!
1608 return false;
1609 }
1610 uids.put(uid, poolIdx);
1611 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1612 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1613 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1614 if (active == null) {
1615 // not currently active, nothing to do.
1616 return false;
1617 }
1618 SparseIntArray uids = active.get(name);
1619 if (uids == null) {
1620 // not currently active, nothing to do.
1621 return false;
1622 }
1623 idx = uids.indexOfKey(uid);
1624 if (idx < 0) {
1625 // not currently active, nothing to do.
1626 return false;
1627 }
1628 uids.removeAt(idx);
1629 if (uids.size() <= 0) {
1630 active.remove(name);
1631 }
1632 }
1633 return true;
1634 }
1635
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001636 public void removeEvents(int code) {
1637 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1638 mActiveEvents[idx] = null;
1639 }
1640
Dianne Hackborn37de0982014-05-09 09:32:18 -07001641 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1642 return mActiveEvents[code];
1643 }
1644 }
1645
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001646 public static final class BitDescription {
1647 public final int mask;
1648 public final int shift;
1649 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001650 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001651 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001652 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001653
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001654 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001655 this.mask = mask;
1656 this.shift = -1;
1657 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001658 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001659 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001660 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001661 }
1662
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001663 public BitDescription(int mask, int shift, String name, String shortName,
1664 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001665 this.mask = mask;
1666 this.shift = shift;
1667 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001668 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001669 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001670 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001671 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001672 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001673
Dianne Hackbornfc064132014-06-02 12:42:12 -07001674 /**
1675 * Don't allow any more batching in to the current history event. This
1676 * is called when printing partial histories, so to ensure that the next
1677 * history event will go in to a new batch after what was printed in the
1678 * last partial history.
1679 */
1680 public abstract void commitCurrentHistoryBatchLocked();
1681
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001682 public abstract int getHistoryTotalSize();
1683
1684 public abstract int getHistoryUsedSize();
1685
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001686 public abstract boolean startIteratingHistoryLocked();
1687
Dianne Hackborn099bc622014-01-22 13:39:16 -08001688 public abstract int getHistoryStringPoolSize();
1689
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001690 public abstract int getHistoryStringPoolBytes();
1691
1692 public abstract String getHistoryTagPoolString(int index);
1693
1694 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001695
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001696 public abstract boolean getNextHistoryLocked(HistoryItem out);
1697
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001698 public abstract void finishIteratingHistoryLocked();
1699
1700 public abstract boolean startIteratingOldHistoryLocked();
1701
1702 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1703
1704 public abstract void finishIteratingOldHistoryLocked();
1705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001707 * Return the base time offset for the battery history.
1708 */
1709 public abstract long getHistoryBaseTime();
1710
1711 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 * Returns the number of times the device has been started.
1713 */
1714 public abstract int getStartCount();
1715
1716 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001717 * 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 -08001718 * running on battery.
1719 *
1720 * {@hide}
1721 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001722 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001724 /**
1725 * Returns the number of times the screen was turned on.
1726 *
1727 * {@hide}
1728 */
1729 public abstract int getScreenOnCount(int which);
1730
Jeff Browne95c3cd2014-05-02 16:59:26 -07001731 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1732
Dianne Hackborn617f8772009-03-31 15:04:46 -07001733 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1734 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1735 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1736 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1737 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
1738
1739 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1740 "dark", "dim", "medium", "light", "bright"
1741 };
1742
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001743 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1744 "0", "1", "2", "3", "4"
1745 };
1746
Dianne Hackborn617f8772009-03-31 15:04:46 -07001747 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001748
Dianne Hackborn617f8772009-03-31 15:04:46 -07001749 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001750 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001751 * the given brightness
1752 *
1753 * {@hide}
1754 */
1755 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001756 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001759 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001760 * running on battery.
1761 *
1762 * {@hide}
1763 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001764 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001765
1766 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001767 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001768 *
1769 * {@hide}
1770 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001771 public abstract int getPowerSaveModeEnabledCount(int which);
1772
1773 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001774 * Constant for device idle mode: not active.
1775 */
1776 public static final int DEVICE_IDLE_MODE_OFF = 0;
1777
1778 /**
1779 * Constant for device idle mode: active in lightweight mode.
1780 */
1781 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1782
1783 /**
1784 * Constant for device idle mode: active in full mode.
1785 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07001786 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001787
1788 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001789 * Returns the time in microseconds that device has been in idle mode while
1790 * running on battery.
1791 *
1792 * {@hide}
1793 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001794 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001795
1796 /**
1797 * Returns the number of times that the devie has gone in to idle mode.
1798 *
1799 * {@hide}
1800 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001801 public abstract int getDeviceIdleModeCount(int mode, int which);
1802
1803 /**
1804 * Return the longest duration we spent in a particular device idle mode (fully in the
1805 * mode, not in idle maintenance etc).
1806 */
1807 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001808
1809 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001810 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001811 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001812 * counts all of the time that we consider the device to be idle, whether or not
1813 * it is currently in the actual device idle mode.
1814 *
1815 * {@hide}
1816 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001817 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001818
1819 /**
1820 * Returns the number of times that the devie has started idling.
1821 *
1822 * {@hide}
1823 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001824 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001825
1826 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001827 * Returns the number of times that connectivity state changed.
1828 *
1829 * {@hide}
1830 */
1831 public abstract int getNumConnectivityChange(int which);
1832
1833 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001834 * 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 -08001835 * running on battery.
1836 *
1837 * {@hide}
1838 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001839 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001840
1841 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001842 * Returns the number of times a phone call was activated.
1843 *
1844 * {@hide}
1845 */
1846 public abstract int getPhoneOnCount(int which);
1847
1848 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001849 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001850 * the given signal strength.
1851 *
1852 * {@hide}
1853 */
1854 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001855 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001856
Dianne Hackborn617f8772009-03-31 15:04:46 -07001857 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001858 * Returns the time in microseconds that the phone has been trying to
1859 * acquire a signal.
1860 *
1861 * {@hide}
1862 */
1863 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001864 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001865
1866 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001867 * Returns the number of times the phone has entered the given signal strength.
1868 *
1869 * {@hide}
1870 */
1871 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1872
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001873 /**
1874 * Returns the time in microseconds that the mobile network has been active
1875 * (in a high power state).
1876 *
1877 * {@hide}
1878 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001879 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001880
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001881 /**
1882 * Returns the number of times that the mobile network has transitioned to the
1883 * active state.
1884 *
1885 * {@hide}
1886 */
1887 public abstract int getMobileRadioActiveCount(int which);
1888
1889 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001890 * Returns the time in microseconds that is the difference between the mobile radio
1891 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1892 * from the radio.
1893 *
1894 * {@hide}
1895 */
1896 public abstract long getMobileRadioActiveAdjustedTime(int which);
1897
1898 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001899 * Returns the time in microseconds that the mobile network has been active
1900 * (in a high power state) but not being able to blame on an app.
1901 *
1902 * {@hide}
1903 */
1904 public abstract long getMobileRadioActiveUnknownTime(int which);
1905
1906 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001907 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001908 *
1909 * {@hide}
1910 */
1911 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001912
Dianne Hackborn627bba72009-03-24 22:32:56 -07001913 public static final int DATA_CONNECTION_NONE = 0;
1914 public static final int DATA_CONNECTION_GPRS = 1;
1915 public static final int DATA_CONNECTION_EDGE = 2;
1916 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001917 public static final int DATA_CONNECTION_CDMA = 4;
1918 public static final int DATA_CONNECTION_EVDO_0 = 5;
1919 public static final int DATA_CONNECTION_EVDO_A = 6;
1920 public static final int DATA_CONNECTION_1xRTT = 7;
1921 public static final int DATA_CONNECTION_HSDPA = 8;
1922 public static final int DATA_CONNECTION_HSUPA = 9;
1923 public static final int DATA_CONNECTION_HSPA = 10;
1924 public static final int DATA_CONNECTION_IDEN = 11;
1925 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001926 public static final int DATA_CONNECTION_LTE = 13;
1927 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001928 public static final int DATA_CONNECTION_HSPAP = 15;
1929 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001930
Dianne Hackborn627bba72009-03-24 22:32:56 -07001931 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001932 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001933 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001934 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001935 };
1936
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001937 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001938
1939 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001940 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001941 * the given data connection.
1942 *
1943 * {@hide}
1944 */
1945 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001946 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001949 * Returns the number of times the phone has entered the given data
1950 * connection type.
1951 *
1952 * {@hide}
1953 */
1954 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001955
Dianne Hackborn3251b902014-06-20 14:40:53 -07001956 public static final int WIFI_SUPPL_STATE_INVALID = 0;
1957 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
1958 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
1959 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
1960 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
1961 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
1962 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
1963 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
1964 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
1965 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
1966 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
1967 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
1968 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
1969
1970 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
1971
1972 static final String[] WIFI_SUPPL_STATE_NAMES = {
1973 "invalid", "disconn", "disabled", "inactive", "scanning",
1974 "authenticating", "associating", "associated", "4-way-handshake",
1975 "group-handshake", "completed", "dormant", "uninit"
1976 };
1977
1978 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
1979 "inv", "dsc", "dis", "inact", "scan",
1980 "auth", "ascing", "asced", "4-way",
1981 "group", "compl", "dorm", "uninit"
1982 };
1983
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001984 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
1985 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001986 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001987 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
1988 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001989 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001990 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
1991 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
1992 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001993 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001994 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001995 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001996 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001997 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
1998 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001999 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2000 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2001 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2002 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2003 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2004 new String[] {"in", "out", "emergency", "off"},
2005 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002006 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2007 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2008 SignalStrength.SIGNAL_STRENGTH_NAMES,
2009 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002010 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2011 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2012 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002013 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002014
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002015 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2016 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002017 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002018 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002019 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002020 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002021 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002022 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2023 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2024 new String[] { "off", "light", "full", "???" },
2025 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002026 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2027 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2028 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002029 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2030 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2031 new String[] { "0", "1", "2", "3", "4" },
2032 new String[] { "0", "1", "2", "3", "4" }),
2033 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2034 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2035 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002036 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002037 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002038 };
2039
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002040 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002041 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002042 "active", "pkginst", "pkgunin", "alarm", "stats", "inactive", "active", "tmpwhitelist",
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07002043 "screenwake", "wakeupap", "longwake"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002044 };
2045
2046 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002047 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002048 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07002049 "Esw", "Ewa", "Elw"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002050 };
2051
Dianne Hackborn617f8772009-03-31 15:04:46 -07002052 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002053 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002054 * running on battery.
2055 *
2056 * {@hide}
2057 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002058 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002059
2060 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002061 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002062 * been in the running state while the device was running on battery.
2063 *
2064 * {@hide}
2065 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002066 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002067
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002068 public static final int WIFI_STATE_OFF = 0;
2069 public static final int WIFI_STATE_OFF_SCANNING = 1;
2070 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2071 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2072 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2073 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2074 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2075 public static final int WIFI_STATE_SOFT_AP = 7;
2076
2077 static final String[] WIFI_STATE_NAMES = {
2078 "off", "scanning", "no_net", "disconn",
2079 "sta", "p2p", "sta_p2p", "soft_ap"
2080 };
2081
2082 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2083
2084 /**
2085 * Returns the time in microseconds that WiFi has been running in the given state.
2086 *
2087 * {@hide}
2088 */
2089 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002090 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002091
2092 /**
2093 * Returns the number of times that WiFi has entered the given state.
2094 *
2095 * {@hide}
2096 */
2097 public abstract int getWifiStateCount(int wifiState, int which);
2098
The Android Open Source Project10592532009-03-18 17:39:46 -07002099 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002100 * Returns the time in microseconds that the wifi supplicant has been
2101 * in a given state.
2102 *
2103 * {@hide}
2104 */
2105 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2106
2107 /**
2108 * Returns the number of times that the wifi supplicant has transitioned
2109 * to a given state.
2110 *
2111 * {@hide}
2112 */
2113 public abstract int getWifiSupplStateCount(int state, int which);
2114
2115 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2116
2117 /**
2118 * Returns the time in microseconds that WIFI has been running with
2119 * the given signal strength.
2120 *
2121 * {@hide}
2122 */
2123 public abstract long getWifiSignalStrengthTime(int strengthBin,
2124 long elapsedRealtimeUs, int which);
2125
2126 /**
2127 * Returns the number of times WIFI has entered the given signal strength.
2128 *
2129 * {@hide}
2130 */
2131 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2132
2133 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002134 * Returns the time in microseconds that the flashlight has been on while the device was
2135 * running on battery.
2136 *
2137 * {@hide}
2138 */
2139 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2140
2141 /**
2142 * Returns the number of times that the flashlight has been turned on while the device was
2143 * running on battery.
2144 *
2145 * {@hide}
2146 */
2147 public abstract long getFlashlightOnCount(int which);
2148
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002149 /**
2150 * Returns the time in microseconds that the camera has been on while the device was
2151 * running on battery.
2152 *
2153 * {@hide}
2154 */
2155 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2156
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002157 /**
2158 * Returns the time in microseconds that bluetooth scans were running while the device was
2159 * on battery.
2160 *
2161 * {@hide}
2162 */
2163 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002164
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002165 public static final int NETWORK_MOBILE_RX_DATA = 0;
2166 public static final int NETWORK_MOBILE_TX_DATA = 1;
2167 public static final int NETWORK_WIFI_RX_DATA = 2;
2168 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002169 public static final int NETWORK_BT_RX_DATA = 4;
2170 public static final int NETWORK_BT_TX_DATA = 5;
2171 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_BT_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002172
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002173 public abstract long getNetworkActivityBytes(int type, int which);
2174 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002175
Adam Lesinskie08af192015-03-25 16:42:59 -07002176 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002177 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002178 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002179 * actual power data.
2180 */
2181 public abstract boolean hasWifiActivityReporting();
2182
2183 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002184 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2185 * in various radio controller states, such as transmit, receive, and idle.
2186 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002187 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002188 public abstract ControllerActivityCounter getWifiControllerActivity();
2189
2190 /**
2191 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2192 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2193 * actual power data.
2194 */
2195 public abstract boolean hasBluetoothActivityReporting();
2196
2197 /**
2198 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2199 * in various radio controller states, such as transmit, receive, and idle.
2200 * @return non-null {@link ControllerActivityCounter}
2201 */
2202 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2203
2204 /**
2205 * Returns true if the BatteryStats object has detailed modem power reports.
2206 * When true, calling {@link #getModemControllerActivity()} will yield the
2207 * actual power data.
2208 */
2209 public abstract boolean hasModemActivityReporting();
2210
2211 /**
2212 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2213 * in various radio controller states, such as transmit, receive, and idle.
2214 * @return non-null {@link ControllerActivityCounter}
2215 */
2216 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002217
The Android Open Source Project10592532009-03-18 17:39:46 -07002218 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002219 * Return the wall clock time when battery stats data collection started.
2220 */
2221 public abstract long getStartClockTime();
2222
2223 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002224 * Return platform version tag that we were running in when the battery stats started.
2225 */
2226 public abstract String getStartPlatformVersion();
2227
2228 /**
2229 * Return platform version tag that we were running in when the battery stats ended.
2230 */
2231 public abstract String getEndPlatformVersion();
2232
2233 /**
2234 * Return the internal version code of the parcelled format.
2235 */
2236 public abstract int getParcelVersion();
2237
2238 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 * Return whether we are currently running on battery.
2240 */
2241 public abstract boolean getIsOnBattery();
2242
2243 /**
2244 * Returns a SparseArray containing the statistics for each uid.
2245 */
2246 public abstract SparseArray<? extends Uid> getUidStats();
2247
2248 /**
2249 * Returns the current battery uptime in microseconds.
2250 *
2251 * @param curTime the amount of elapsed realtime in microseconds.
2252 */
2253 public abstract long getBatteryUptime(long curTime);
2254
2255 /**
2256 * Returns the current battery realtime in microseconds.
2257 *
2258 * @param curTime the amount of elapsed realtime in microseconds.
2259 */
2260 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07002261
2262 /**
Evan Millar633a1742009-04-02 16:36:33 -07002263 * Returns the battery percentage level at the last time the device was unplugged from power, or
2264 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002265 */
Evan Millar633a1742009-04-02 16:36:33 -07002266 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07002267
2268 /**
Evan Millar633a1742009-04-02 16:36:33 -07002269 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2270 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002271 */
Evan Millar633a1742009-04-02 16:36:33 -07002272 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273
2274 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002275 * Get the amount the battery has discharged since the stats were
2276 * last reset after charging, as a lower-end approximation.
2277 */
2278 public abstract int getLowDischargeAmountSinceCharge();
2279
2280 /**
2281 * Get the amount the battery has discharged since the stats were
2282 * last reset after charging, as an upper-end approximation.
2283 */
2284 public abstract int getHighDischargeAmountSinceCharge();
2285
2286 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002287 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2288 */
2289 public abstract int getDischargeAmount(int which);
2290
2291 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002292 * Get the amount the battery has discharged while the screen was on,
2293 * since the last time power was unplugged.
2294 */
2295 public abstract int getDischargeAmountScreenOn();
2296
2297 /**
2298 * Get the amount the battery has discharged while the screen was on,
2299 * since the last time the device was charged.
2300 */
2301 public abstract int getDischargeAmountScreenOnSinceCharge();
2302
2303 /**
2304 * Get the amount the battery has discharged while the screen was off,
2305 * since the last time power was unplugged.
2306 */
2307 public abstract int getDischargeAmountScreenOff();
2308
2309 /**
2310 * Get the amount the battery has discharged while the screen was off,
2311 * since the last time the device was charged.
2312 */
2313 public abstract int getDischargeAmountScreenOffSinceCharge();
2314
2315 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 * Returns the total, last, or current battery uptime in microseconds.
2317 *
2318 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002319 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 */
2321 public abstract long computeBatteryUptime(long curTime, int which);
2322
2323 /**
2324 * Returns the total, last, or current battery realtime in microseconds.
2325 *
2326 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002327 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 */
2329 public abstract long computeBatteryRealtime(long curTime, int which);
2330
2331 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002332 * Returns the total, last, or current battery screen off uptime in microseconds.
2333 *
2334 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002335 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002336 */
2337 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2338
2339 /**
2340 * Returns the total, last, or current battery screen off realtime in microseconds.
2341 *
2342 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002343 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002344 */
2345 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2346
2347 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 * Returns the total, last, or current uptime in microseconds.
2349 *
2350 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002351 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 */
2353 public abstract long computeUptime(long curTime, int which);
2354
2355 /**
2356 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002357 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002359 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 */
2361 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002362
2363 /**
2364 * Compute an approximation for how much run time (in microseconds) is remaining on
2365 * the battery. Returns -1 if no time can be computed: either there is not
2366 * enough current data to make a decision, or the battery is currently
2367 * charging.
2368 *
2369 * @param curTime The current elepsed realtime in microseconds.
2370 */
2371 public abstract long computeBatteryTimeRemaining(long curTime);
2372
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002373 // The part of a step duration that is the actual time.
2374 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2375
2376 // Bits in a step duration that are the new battery level we are at.
2377 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002378 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002379
2380 // Bits in a step duration that are the initial mode we were in at that step.
2381 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002382 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002383
2384 // Bits in a step duration that indicate which modes changed during that step.
2385 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002386 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002387
2388 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2389 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2390
2391 // Step duration mode: power save is on.
2392 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2393
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002394 // Step duration mode: device is currently in idle mode.
2395 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2396
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002397 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2398 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002399 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2400 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002401 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2402 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2403 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2404 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2405 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002406 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2407 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002408 };
2409 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2410 (Display.STATE_OFF-1),
2411 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002412 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002413 (Display.STATE_ON-1),
2414 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2415 (Display.STATE_DOZE-1),
2416 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2417 (Display.STATE_DOZE_SUSPEND-1),
2418 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002419 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002420 };
2421 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2422 "screen off",
2423 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002424 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002425 "screen on",
2426 "screen on power save",
2427 "screen doze",
2428 "screen doze power save",
2429 "screen doze-suspend",
2430 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002431 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002432 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002433
2434 /**
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002435 * Return the counter keeping track of the amount of battery discharge while the screen was off,
2436 * measured in micro-Ampere-hours. This will be non-zero only if the device's battery has
2437 * a coulomb counter.
2438 */
2439 public abstract LongCounter getDischargeScreenOffCoulombCounter();
2440
2441 /**
2442 * Return the counter keeping track of the amount of battery discharge measured in
2443 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2444 * a coulomb counter.
2445 */
2446 public abstract LongCounter getDischargeCoulombCounter();
2447
2448 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002449 * Returns the estimated real battery capacity, which may be less than the capacity
2450 * declared by the PowerProfile.
2451 * @return The estimated battery capacity in mAh.
2452 */
2453 public abstract int getEstimatedBatteryCapacity();
2454
2455 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002456 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002457 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002458 public abstract LevelStepTracker getDischargeLevelStepTracker();
2459
2460 /**
2461 * Return the array of daily discharge step durations.
2462 */
2463 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002464
2465 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002466 * Compute an approximation for how much time (in microseconds) remains until the battery
2467 * is fully charged. Returns -1 if no time can be computed: either there is not
2468 * enough current data to make a decision, or the battery is currently
2469 * discharging.
2470 *
2471 * @param curTime The current elepsed realtime in microseconds.
2472 */
2473 public abstract long computeChargeTimeRemaining(long curTime);
2474
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002475 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002476 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002477 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002478 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002479
2480 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002481 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002482 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002483 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002484
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002485 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2486
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002487 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002488
Evan Millarc64edde2009-04-18 12:26:32 -07002489 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490
James Carr2dd7e5e2016-07-20 18:48:39 -07002491 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2492
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002493 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2494
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002495 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 long days = seconds / (60 * 60 * 24);
2497 if (days != 0) {
2498 out.append(days);
2499 out.append("d ");
2500 }
2501 long used = days * 60 * 60 * 24;
2502
2503 long hours = (seconds - used) / (60 * 60);
2504 if (hours != 0 || used != 0) {
2505 out.append(hours);
2506 out.append("h ");
2507 }
2508 used += hours * 60 * 60;
2509
2510 long mins = (seconds-used) / 60;
2511 if (mins != 0 || used != 0) {
2512 out.append(mins);
2513 out.append("m ");
2514 }
2515 used += mins * 60;
2516
2517 if (seconds != 0 || used != 0) {
2518 out.append(seconds-used);
2519 out.append("s ");
2520 }
2521 }
2522
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002523 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002525 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 sb.append(time - (sec * 1000));
2527 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 }
2529
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002530 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002531 long sec = time / 1000;
2532 formatTimeRaw(sb, sec);
2533 sb.append(time - (sec * 1000));
2534 sb.append("ms");
2535 }
2536
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002537 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002539 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 }
2541 float perc = ((float)num) / ((float)den) * 100;
2542 mFormatBuilder.setLength(0);
2543 mFormatter.format("%.1f%%", perc);
2544 return mFormatBuilder.toString();
2545 }
2546
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002547 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002548 mFormatBuilder.setLength(0);
2549
2550 if (bytes < BYTES_PER_KB) {
2551 return bytes + "B";
2552 } else if (bytes < BYTES_PER_MB) {
2553 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2554 return mFormatBuilder.toString();
2555 } else if (bytes < BYTES_PER_GB){
2556 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2557 return mFormatBuilder.toString();
2558 } else {
2559 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2560 return mFormatBuilder.toString();
2561 }
2562 }
2563
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002564 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002565 if (timer != null) {
2566 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002567 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002568 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2569 return totalTimeMillis;
2570 }
2571 return 0;
2572 }
2573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 /**
2575 *
2576 * @param sb a StringBuilder object.
2577 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002578 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002580 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 * @param linePrefix a String to be prepended to each line of output.
2582 * @return the line prefix
2583 */
2584 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002585 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002588 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002589
Evan Millarc64edde2009-04-18 12:26:32 -07002590 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 if (totalTimeMillis != 0) {
2592 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002593 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002594 if (name != null) {
2595 sb.append(name);
2596 sb.append(' ');
2597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 sb.append('(');
2599 sb.append(count);
2600 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002601 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2602 if (maxDurationMs >= 0) {
2603 sb.append(" max=");
2604 sb.append(maxDurationMs);
2605 }
2606 if (timer.isRunningLocked()) {
2607 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2608 if (currentMs >= 0) {
2609 sb.append(" (running for ");
2610 sb.append(currentMs);
2611 sb.append("ms)");
2612 } else {
2613 sb.append(" (running)");
2614 }
2615 }
2616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 return ", ";
2618 }
2619 }
2620 return linePrefix;
2621 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002622
2623 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07002624 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002625 *
2626 * @param pw a PrintWriter object to print to.
2627 * @param sb a StringBuilder object.
2628 * @param timer a Timer object contining the wakelock times.
2629 * @param rawRealtime the current on-battery time in microseconds.
2630 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2631 * @param prefix a String to be prepended to each line of output.
2632 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002633 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002634 */
2635 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07002636 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002637 if (timer != null) {
2638 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07002639 final long totalTimeMs = (timer.getTotalTimeLocked(
2640 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002641 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002642 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002643 sb.setLength(0);
2644 sb.append(prefix);
2645 sb.append(" ");
2646 sb.append(type);
2647 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002648 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002649 sb.append("realtime (");
2650 sb.append(count);
2651 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002652 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
2653 if (maxDurationMs >= 0) {
2654 sb.append(" max=");
2655 sb.append(maxDurationMs);
2656 }
2657 if (timer.isRunningLocked()) {
2658 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
2659 if (currentMs >= 0) {
2660 sb.append(" (running for ");
2661 sb.append(currentMs);
2662 sb.append("ms)");
2663 } else {
2664 sb.append(" (running)");
2665 }
2666 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002667 pw.println(sb.toString());
2668 return true;
2669 }
2670 }
2671 return false;
2672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673
2674 /**
2675 * Checkin version of wakelock printer. Prints simple comma-separated list.
2676 *
2677 * @param sb a StringBuilder object.
2678 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002679 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002681 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 * @param linePrefix a String to be prepended to each line of output.
2683 * @return the line prefix
2684 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002685 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2686 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 long totalTimeMicros = 0;
2688 int count = 0;
Joe Onorato92fd23f2016-07-25 11:18:42 -07002689 long max = -1;
2690 long current = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002692 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07002693 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002694 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2695 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 }
2697 sb.append(linePrefix);
2698 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2699 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002700 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002702 sb.append(',');
2703 sb.append(current);
2704 sb.append(',');
2705 sb.append(max);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 return ",";
2707 }
2708
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002709 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
2710 String type) {
2711 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2712 pw.print(',');
2713 pw.print(uid);
2714 pw.print(',');
2715 pw.print(category);
2716 pw.print(',');
2717 pw.print(type);
2718 }
2719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 /**
2721 * Dump a comma-separated line of values for terse checkin mode.
2722 *
2723 * @param pw the PageWriter to dump log to
2724 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2725 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2726 * @param args type-dependent data arguments
2727 */
2728 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
2729 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002730 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002731 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002732 pw.print(',');
2733 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002735 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002737
2738 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002739 * Dump a given timer stat for terse checkin mode.
2740 *
2741 * @param pw the PageWriter to dump log to
2742 * @param uid the UID to log
2743 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2744 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2745 * @param timer a {@link Timer} to dump stats for
2746 * @param rawRealtime the current elapsed realtime of the system in microseconds
2747 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2748 */
2749 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2750 Timer timer, long rawRealtime, int which) {
2751 if (timer != null) {
2752 // Convert from microseconds to milliseconds with rounding
2753 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2754 / 1000;
2755 final int count = timer.getCountLocked(which);
2756 if (totalTime != 0) {
2757 dumpLine(pw, uid, category, type, totalTime, count);
2758 }
2759 }
2760 }
2761
2762 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002763 * Checks if the ControllerActivityCounter has any data worth dumping.
2764 */
2765 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
2766 if (counter == null) {
2767 return false;
2768 }
2769
2770 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
2771 || counter.getRxTimeCounter().getCountLocked(which) != 0
2772 || counter.getPowerCounter().getCountLocked(which) != 0) {
2773 return true;
2774 }
2775
2776 for (LongCounter c : counter.getTxTimeCounters()) {
2777 if (c.getCountLocked(which) != 0) {
2778 return true;
2779 }
2780 }
2781 return false;
2782 }
2783
2784 /**
2785 * Dumps the ControllerActivityCounter if it has any data worth dumping.
2786 * The order of the arguments in the final check in line is:
2787 *
2788 * idle, rx, power, tx...
2789 *
2790 * where tx... is one or more transmit level times.
2791 */
2792 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
2793 String type,
2794 ControllerActivityCounter counter,
2795 int which) {
2796 if (!controllerActivityHasData(counter, which)) {
2797 return;
2798 }
2799
2800 dumpLineHeader(pw, uid, category, type);
2801 pw.print(",");
2802 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
2803 pw.print(",");
2804 pw.print(counter.getRxTimeCounter().getCountLocked(which));
2805 pw.print(",");
2806 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
2807 for (LongCounter c : counter.getTxTimeCounters()) {
2808 pw.print(",");
2809 pw.print(c.getCountLocked(which));
2810 }
2811 pw.println();
2812 }
2813
2814 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
2815 String prefix, String controllerName,
2816 ControllerActivityCounter counter,
2817 int which) {
2818 if (controllerActivityHasData(counter, which)) {
2819 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
2820 }
2821 }
2822
2823 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
2824 String controllerName,
2825 ControllerActivityCounter counter, int which) {
2826 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
2827 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
2828 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
2829 long totalTxTimeMs = 0;
2830 for (LongCounter txState : counter.getTxTimeCounters()) {
2831 totalTxTimeMs += txState.getCountLocked(which);
2832 }
2833
2834 final long totalTimeMs = idleTimeMs + rxTimeMs + totalTxTimeMs;
2835
2836 sb.setLength(0);
2837 sb.append(prefix);
2838 sb.append(" ");
2839 sb.append(controllerName);
2840 sb.append(" Idle time: ");
2841 formatTimeMs(sb, idleTimeMs);
2842 sb.append("(");
2843 sb.append(formatRatioLocked(idleTimeMs, totalTimeMs));
2844 sb.append(")");
2845 pw.println(sb.toString());
2846
2847 sb.setLength(0);
2848 sb.append(prefix);
2849 sb.append(" ");
2850 sb.append(controllerName);
2851 sb.append(" Rx time: ");
2852 formatTimeMs(sb, rxTimeMs);
2853 sb.append("(");
2854 sb.append(formatRatioLocked(rxTimeMs, totalTimeMs));
2855 sb.append(")");
2856 pw.println(sb.toString());
2857
2858 sb.setLength(0);
2859 sb.append(prefix);
2860 sb.append(" ");
2861 sb.append(controllerName);
2862 sb.append(" Tx time: ");
2863 formatTimeMs(sb, totalTxTimeMs);
2864 sb.append("(");
2865 sb.append(formatRatioLocked(totalTxTimeMs, totalTimeMs));
2866 sb.append(")");
2867 pw.println(sb.toString());
2868
2869 final int numTxLvls = counter.getTxTimeCounters().length;
2870 if (numTxLvls > 1) {
2871 for (int lvl = 0; lvl < numTxLvls; lvl++) {
2872 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
2873 sb.setLength(0);
2874 sb.append(prefix);
2875 sb.append(" [");
2876 sb.append(lvl);
2877 sb.append("] ");
2878 formatTimeMs(sb, txLvlTimeMs);
2879 sb.append("(");
2880 sb.append(formatRatioLocked(txLvlTimeMs, totalTxTimeMs));
2881 sb.append(")");
2882 pw.println(sb.toString());
2883 }
2884 }
2885
2886 sb.setLength(0);
2887 sb.append(prefix);
2888 sb.append(" ");
2889 sb.append(controllerName);
2890 sb.append(" Power drain: ").append(
2891 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
2892 sb.append("mAh");
2893 pw.println(sb.toString());
2894 }
2895
2896 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07002897 * Temporary for settings.
2898 */
2899 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
2900 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2901 }
2902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 /**
2904 * Checkin server version of dump to produce more compact, computer-readable log.
2905 *
2906 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07002908 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
2909 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2911 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2912 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2914 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002915 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2916 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2917 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 final long totalRealtime = computeRealtime(rawRealtime, which);
2919 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002920 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002921 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002922 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002923 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
2924 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002925 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002926 rawRealtime, which);
2927 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
2928 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002929 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002930 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002931 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002932 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002933 final long dischargeCount = getDischargeCoulombCounter().getCountLocked(which);
2934 final long dischargeScreenOffCount = getDischargeScreenOffCoulombCounter()
2935 .getCountLocked(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002936
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002937 final StringBuilder sb = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002939 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07002940 final int NU = uidStats.size();
2941
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002942 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 // Dump "battery" stat
2945 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002946 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07002947 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002948 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002949 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002950 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
2951 getEstimatedBatteryCapacity());
Adam Lesinski67c134f2016-06-10 15:15:08 -07002952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002954 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07002955 long fullWakeLockTimeTotal = 0;
2956 long partialWakeLockTimeTotal = 0;
2957
2958 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002959 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002960
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002961 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
2962 = u.getWakelockStats();
2963 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
2964 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07002965
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002966 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
2967 if (fullWakeTimer != null) {
2968 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
2969 which);
2970 }
2971
2972 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
2973 if (partialWakeTimer != null) {
2974 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
2975 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07002976 }
2977 }
2978 }
Adam Lesinskie283d332015-04-16 12:29:25 -07002979
2980 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002981 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2982 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2983 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2984 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2985 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2986 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2987 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2988 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002989 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
2990 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002991 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
2992 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002993 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
2994 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002995
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002996 // Dump Modem controller stats
2997 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
2998 getModemControllerActivity(), which);
2999
Adam Lesinskie283d332015-04-16 12:29:25 -07003000 // Dump Wifi controller stats
3001 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3002 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003003 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003004 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003005
3006 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3007 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003008
3009 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003010 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3011 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 // Dump misc stats
3014 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003015 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003016 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003017 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003018 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003019 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003020 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3021 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003022 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003023 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3024 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3025 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3026 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003027 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003028
3029 // Dump screen brightness stats
3030 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3031 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003032 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003033 }
3034 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07003035
Dianne Hackborn627bba72009-03-24 22:32:56 -07003036 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003037 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3038 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003039 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003040 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003041 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003042 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003043 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003044 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003045 args[i] = getPhoneSignalStrengthCount(i, which);
3046 }
3047 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003048
Dianne Hackborn627bba72009-03-24 22:32:56 -07003049 // Dump network type stats
3050 args = new Object[NUM_DATA_CONNECTION_TYPES];
3051 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003052 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003053 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003054 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3055 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3056 args[i] = getPhoneDataConnectionCount(i, which);
3057 }
3058 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003059
3060 // Dump wifi state stats
3061 args = new Object[NUM_WIFI_STATES];
3062 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003063 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003064 }
3065 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3066 for (int i=0; i<NUM_WIFI_STATES; i++) {
3067 args[i] = getWifiStateCount(i, which);
3068 }
3069 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3070
Dianne Hackborn3251b902014-06-20 14:40:53 -07003071 // Dump wifi suppl state stats
3072 args = new Object[NUM_WIFI_SUPPL_STATES];
3073 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3074 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3075 }
3076 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3077 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3078 args[i] = getWifiSupplStateCount(i, which);
3079 }
3080 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3081
3082 // Dump wifi signal strength stats
3083 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3084 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3085 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3086 }
3087 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3088 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3089 args[i] = getWifiSignalStrengthCount(i, which);
3090 }
3091 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3092
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003093 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003094 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003095 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003098 if (which == STATS_SINCE_UNPLUGGED) {
3099 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3100 getDischargeStartLevel()-getDischargeCurrentLevel(),
3101 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003102 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
3103 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003104 } else {
3105 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3106 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003107 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003108 getDischargeAmountScreenOffSinceCharge(),
3109 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003110 }
3111
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003112 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003113 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003114 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003115 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003116 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003117 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003118 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003119 sb.toString());
3120 }
Evan Millarc64edde2009-04-18 12:26:32 -07003121 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003122 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003123 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003124 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3125 // Not doing the regular wake lock formatting to remain compatible
3126 // with the old checkin format.
3127 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3128 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003129 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003130 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003131 }
3132 }
Evan Millarc64edde2009-04-18 12:26:32 -07003133 }
3134
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003135 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003136 helper.create(this);
3137 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003138 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003139 if (sippers != null && sippers.size() > 0) {
3140 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3141 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003142 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003143 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3144 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
3145 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003146 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003147 int uid = 0;
3148 String label;
3149 switch (bs.drainType) {
3150 case IDLE:
3151 label="idle";
3152 break;
3153 case CELL:
3154 label="cell";
3155 break;
3156 case PHONE:
3157 label="phone";
3158 break;
3159 case WIFI:
3160 label="wifi";
3161 break;
3162 case BLUETOOTH:
3163 label="blue";
3164 break;
3165 case SCREEN:
3166 label="scrn";
3167 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003168 case FLASHLIGHT:
3169 label="flashlight";
3170 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003171 case APP:
3172 uid = bs.uidObj.getUid();
3173 label = "uid";
3174 break;
3175 case USER:
3176 uid = UserHandle.getUid(bs.userId, 0);
3177 label = "user";
3178 break;
3179 case UNACCOUNTED:
3180 label = "unacc";
3181 break;
3182 case OVERCOUNTED:
3183 label = "over";
3184 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003185 case CAMERA:
3186 label = "camera";
3187 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003188 default:
3189 label = "???";
3190 }
3191 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Adam Lesinskie08af192015-03-25 16:42:59 -07003192 BatteryStatsHelper.makemAh(bs.totalPowerMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003193 }
3194 }
3195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 for (int iu = 0; iu < NU; iu++) {
3197 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003198 if (reqUid >= 0 && uid != reqUid) {
3199 continue;
3200 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003201 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003203 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003204 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3205 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3206 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3207 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3208 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3209 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3210 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3211 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003212 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003213 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3214 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003215 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003216 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3217 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003218 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3219 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003220 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Adam Lesinski5f056f62016-07-14 16:56:08 -07003221 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003222 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3223 wifiBytesRx, wifiBytesTx,
3224 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003225 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003226 mobileActiveTime, mobileActiveCount,
Adam Lesinski5f056f62016-07-14 16:56:08 -07003227 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003228 }
3229
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003230 // Dump modem controller data, per UID.
3231 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3232 u.getModemControllerActivity(), which);
3233
3234 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003235 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3236 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3237 final int wifiScanCount = u.getWifiScanCount(which);
3238 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003239 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003240 || uidWifiRunningTime != 0) {
3241 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3242 uidWifiRunningTime, wifiScanCount,
Kweku Adams5b2747e2016-04-25 10:44:05 -07003243 /* legacy fields follow, keep at 0 */ 0, 0, 0);
The Android Open Source Project10592532009-03-18 17:39:46 -07003244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003246 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3247 u.getWifiControllerActivity(), which);
3248
Adam Lesinskiba9d8932016-04-12 12:06:03 -07003249 dumpTimer(pw, uid, category, BLUETOOTH_MISC_DATA, u.getBluetoothScanTimer(),
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003250 rawRealtime, which);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003251
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003252 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3253 u.getBluetoothControllerActivity(), which);
3254
Dianne Hackborn617f8772009-03-31 15:04:46 -07003255 if (u.hasUserActivity()) {
3256 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3257 boolean hasData = false;
3258 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3259 int val = u.getUserActivityCount(i, which);
3260 args[i] = val;
3261 if (val != 0) hasData = true;
3262 }
3263 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003264 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003265 }
3266 }
3267
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003268 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3269 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3270 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3271 String linePrefix = "";
3272 sb.setLength(0);
3273 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3274 rawRealtime, "f", which, linePrefix);
3275 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
3276 rawRealtime, "p", which, linePrefix);
3277 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3278 rawRealtime, "w", which, linePrefix);
3279
3280 // Only log if we had at lease one wakelock...
3281 if (sb.length() > 0) {
3282 String name = wakelocks.keyAt(iw);
3283 if (name.indexOf(',') >= 0) {
3284 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003285 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003286 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 }
3288 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003289
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003290 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3291 for (int isy=syncs.size()-1; isy>=0; isy--) {
3292 final Timer timer = syncs.valueAt(isy);
3293 // Convert from microseconds to milliseconds with rounding
3294 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3295 final int count = timer.getCountLocked(which);
3296 if (totalTime != 0) {
3297 dumpLine(pw, uid, category, SYNC_DATA, syncs.keyAt(isy), totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003298 }
3299 }
3300
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003301 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3302 for (int ij=jobs.size()-1; ij>=0; ij--) {
3303 final Timer timer = jobs.valueAt(ij);
3304 // Convert from microseconds to milliseconds with rounding
3305 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3306 final int count = timer.getCountLocked(which);
3307 if (totalTime != 0) {
3308 dumpLine(pw, uid, category, JOB_DATA, jobs.keyAt(ij), totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003309 }
3310 }
3311
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003312 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3313 rawRealtime, which);
3314 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3315 rawRealtime, which);
3316 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3317 rawRealtime, which);
3318 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3319 rawRealtime, which);
3320
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003321 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3322 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003323 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003324 final Uid.Sensor se = sensors.valueAt(ise);
3325 final int sensorNumber = sensors.keyAt(ise);
3326 final Timer timer = se.getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -08003327 final Counter bgCounter = se.getSensorBgCount();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003328 if (timer != null) {
3329 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003330 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3331 / 1000;
3332 final int count = timer.getCountLocked(which);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08003333 final int bgCount = bgCounter != null ? bgCounter.getCountLocked(which) : 0;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003334 if (totalTime != 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08003335 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count,
3336 bgCount);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003337 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 }
3339 }
3340
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003341 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3342 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003343
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003344 dumpTimer(pw, uid, category, FOREGROUND_DATA, u.getForegroundActivityTimer(),
3345 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003346
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003347 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003348 long totalStateTime = 0;
3349 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003350 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3351 totalStateTime += time;
3352 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003353 }
3354 if (totalStateTime > 0) {
3355 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3356 }
3357
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003358 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3359 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
3360 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
3361 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
3362 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
3363 powerCpuMaUs / 1000);
3364 }
3365
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003366 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3367 = u.getProcessStats();
3368 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3369 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003370
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003371 final long userMillis = ps.getUserTime(which);
3372 final long systemMillis = ps.getSystemTime(which);
3373 final long foregroundMillis = ps.getForegroundTime(which);
3374 final int starts = ps.getStarts(which);
3375 final int numCrashes = ps.getNumCrashes(which);
3376 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003377
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003378 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3379 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
3380 dumpLine(pw, uid, category, PROCESS_DATA, processStats.keyAt(ipr), userMillis,
3381 systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003382 }
3383 }
3384
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003385 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3386 = u.getPackageStats();
3387 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3388 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3389 int wakeups = 0;
3390 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3391 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07003392 int count = alarms.valueAt(iwa).getCountLocked(which);
3393 wakeups += count;
3394 String name = alarms.keyAt(iwa).replace(',', '_');
3395 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003396 }
3397 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3398 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3399 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3400 final long startTime = ss.getStartTime(batteryUptime, which);
3401 final int starts = ss.getStarts(which);
3402 final int launches = ss.getLaunches(which);
3403 if (startTime != 0 || starts != 0 || launches != 0) {
3404 dumpLine(pw, uid, category, APK_DATA,
3405 wakeups, // wakeup alarms
3406 packageStats.keyAt(ipkg), // Apk
3407 serviceStats.keyAt(isvc), // service
3408 startTime / 1000, // time spent started, in ms
3409 starts,
3410 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 }
3412 }
3413 }
3414 }
3415 }
3416
Dianne Hackborn81038902012-11-26 17:04:09 -08003417 static final class TimerEntry {
3418 final String mName;
3419 final int mId;
3420 final BatteryStats.Timer mTimer;
3421 final long mTime;
3422 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3423 mName = name;
3424 mId = id;
3425 mTimer = timer;
3426 mTime = time;
3427 }
3428 }
3429
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003430 private void printmAh(PrintWriter printer, double power) {
3431 printer.print(BatteryStatsHelper.makemAh(power));
3432 }
3433
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003434 private void printmAh(StringBuilder sb, double power) {
3435 sb.append(BatteryStatsHelper.makemAh(power));
3436 }
3437
Dianne Hackbornd953c532014-08-16 18:17:38 -07003438 /**
3439 * Temporary for settings.
3440 */
3441 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3442 int reqUid) {
3443 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3444 }
3445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003447 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003448 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003449 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3450 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
3451 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452
3453 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3454 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3455 final long totalRealtime = computeRealtime(rawRealtime, which);
3456 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003457 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3458 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3459 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003460 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3461 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003462
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003463 final StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07003464
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003465 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003466 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003467
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003468 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
3469 if (estimatedBatteryCapacity > 0) {
3470 sb.setLength(0);
3471 sb.append(prefix);
3472 sb.append(" Estimated battery capacity: ");
3473 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
3474 sb.append(" mAh");
3475 pw.println(sb.toString());
3476 }
3477
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003478 sb.setLength(0);
3479 sb.append(prefix);
3480 sb.append(" Time on battery: ");
3481 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3482 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3483 sb.append(") realtime, ");
3484 formatTimeMs(sb, whichBatteryUptime / 1000);
3485 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3486 sb.append(") uptime");
3487 pw.println(sb.toString());
3488 sb.setLength(0);
3489 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003490 sb.append(" Time on battery screen off: ");
3491 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3492 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3493 sb.append(") realtime, ");
3494 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3495 sb.append("(");
3496 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3497 sb.append(") uptime");
3498 pw.println(sb.toString());
3499 sb.setLength(0);
3500 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003501 sb.append(" Total run time: ");
3502 formatTimeMs(sb, totalRealtime / 1000);
3503 sb.append("realtime, ");
3504 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003505 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003506 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003507 if (batteryTimeRemaining >= 0) {
3508 sb.setLength(0);
3509 sb.append(prefix);
3510 sb.append(" Battery time remaining: ");
3511 formatTimeMs(sb, batteryTimeRemaining / 1000);
3512 pw.println(sb.toString());
3513 }
3514 if (chargeTimeRemaining >= 0) {
3515 sb.setLength(0);
3516 sb.append(prefix);
3517 sb.append(" Charge time remaining: ");
3518 formatTimeMs(sb, chargeTimeRemaining / 1000);
3519 pw.println(sb.toString());
3520 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003521
3522 final LongCounter dischargeCounter = getDischargeCoulombCounter();
3523 final long dischargeCount = dischargeCounter.getCountLocked(which);
3524 if (dischargeCount >= 0) {
3525 sb.setLength(0);
3526 sb.append(prefix);
3527 sb.append(" Discharge: ");
3528 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
3529 sb.append(" mAh");
3530 pw.println(sb.toString());
3531 }
3532
3533 final LongCounter dischargeScreenOffCounter = getDischargeScreenOffCoulombCounter();
3534 final long dischargeScreenOffCount = dischargeScreenOffCounter.getCountLocked(which);
3535 if (dischargeScreenOffCount >= 0) {
3536 sb.setLength(0);
3537 sb.append(prefix);
3538 sb.append(" Screen off discharge: ");
3539 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
3540 sb.append(" mAh");
3541 pw.println(sb.toString());
3542 }
3543
3544 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
3545 if (dischargeScreenOnCount >= 0) {
3546 sb.setLength(0);
3547 sb.append(prefix);
3548 sb.append(" Screen on discharge: ");
3549 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
3550 sb.append(" mAh");
3551 pw.println(sb.toString());
3552 }
3553
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003554 pw.print(" Start clock time: ");
3555 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3556
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003557 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003558 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003559 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003560 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3561 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003562 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003563 rawRealtime, which);
3564 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3565 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003566 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003567 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003568 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3569 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3570 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003571 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003572 sb.append(prefix);
3573 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3574 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003575 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003576 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3577 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003578 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003579 pw.println(sb.toString());
3580 sb.setLength(0);
3581 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003582 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003583 boolean didOne = false;
3584 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003585 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003586 if (time == 0) {
3587 continue;
3588 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003589 sb.append("\n ");
3590 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003591 didOne = true;
3592 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3593 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003594 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003595 sb.append("(");
3596 sb.append(formatRatioLocked(time, screenOnTime));
3597 sb.append(")");
3598 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003599 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003600 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003601 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003602 sb.setLength(0);
3603 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003604 sb.append(" Power save mode enabled: ");
3605 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003606 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003607 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003608 sb.append(")");
3609 pw.println(sb.toString());
3610 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003611 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003612 sb.setLength(0);
3613 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003614 sb.append(" Device light idling: ");
3615 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003616 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003617 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
3618 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003619 sb.append("x");
3620 pw.println(sb.toString());
3621 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003622 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003623 sb.setLength(0);
3624 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003625 sb.append(" Idle mode light time: ");
3626 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003627 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003628 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
3629 sb.append(") ");
3630 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003631 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003632 sb.append(" -- longest ");
3633 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
3634 pw.println(sb.toString());
3635 }
3636 if (deviceIdlingTime != 0) {
3637 sb.setLength(0);
3638 sb.append(prefix);
3639 sb.append(" Device full idling: ");
3640 formatTimeMs(sb, deviceIdlingTime / 1000);
3641 sb.append("(");
3642 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003643 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003644 sb.append("x");
3645 pw.println(sb.toString());
3646 }
3647 if (deviceIdleModeFullTime != 0) {
3648 sb.setLength(0);
3649 sb.append(prefix);
3650 sb.append(" Idle mode full time: ");
3651 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
3652 sb.append("(");
3653 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
3654 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003655 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003656 sb.append("x");
3657 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003658 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003659 pw.println(sb.toString());
3660 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003661 if (phoneOnTime != 0) {
3662 sb.setLength(0);
3663 sb.append(prefix);
3664 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3665 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003666 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003667 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003668 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003669 if (connChanges != 0) {
3670 pw.print(prefix);
3671 pw.print(" Connectivity changes: "); pw.println(connChanges);
3672 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003673
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003674 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003675 long fullWakeLockTimeTotalMicros = 0;
3676 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003677
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003678 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003679
Evan Millar22ac0432009-03-31 11:33:18 -07003680 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003681 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003682
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003683 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3684 = u.getWakelockStats();
3685 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3686 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003687
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003688 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3689 if (fullWakeTimer != null) {
3690 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3691 rawRealtime, which);
3692 }
3693
3694 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3695 if (partialWakeTimer != null) {
3696 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3697 rawRealtime, which);
3698 if (totalTimeMicros > 0) {
3699 if (reqUid < 0) {
3700 // Only show the ordered list of all wake
3701 // locks if the caller is not asking for data
3702 // about a specific uid.
3703 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3704 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003705 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003706 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003707 }
3708 }
3709 }
3710 }
3711
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003712 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3713 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3714 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3715 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3716 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3717 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3718 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3719 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003720 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3721 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003722
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003723 if (fullWakeLockTimeTotalMicros != 0) {
3724 sb.setLength(0);
3725 sb.append(prefix);
3726 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
3727 (fullWakeLockTimeTotalMicros + 500) / 1000);
3728 pw.println(sb.toString());
3729 }
3730
3731 if (partialWakeLockTimeTotalMicros != 0) {
3732 sb.setLength(0);
3733 sb.append(prefix);
3734 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
3735 (partialWakeLockTimeTotalMicros + 500) / 1000);
3736 pw.println(sb.toString());
3737 }
3738
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003739 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003740 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
3741 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
3742 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
3743 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003744 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003745 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07003746 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003747 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08003748 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003749 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003750 if (time == 0) {
3751 continue;
3752 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003753 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003754 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003755 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08003756 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003757 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003758 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003759 sb.append("(");
3760 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003761 sb.append(") ");
3762 sb.append(getPhoneSignalStrengthCount(i, which));
3763 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003764 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003765 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003766 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07003767
3768 sb.setLength(0);
3769 sb.append(prefix);
3770 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003771 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003772 pw.println(sb.toString());
3773
Dianne Hackborn627bba72009-03-24 22:32:56 -07003774 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003775 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003776 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003777 didOne = false;
3778 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003779 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003780 if (time == 0) {
3781 continue;
3782 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003783 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003784 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003785 didOne = true;
3786 sb.append(DATA_CONNECTION_NAMES[i]);
3787 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003788 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003789 sb.append("(");
3790 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003791 sb.append(") ");
3792 sb.append(getPhoneDataConnectionCount(i, which));
3793 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003794 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003795 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003796 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003797
3798 sb.setLength(0);
3799 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003800 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003801 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003802 formatTimeMs(sb, mobileActiveTime / 1000);
3803 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
3804 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
3805 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003806 pw.println(sb.toString());
3807
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003808 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
3809 if (mobileActiveUnknownTime != 0) {
3810 sb.setLength(0);
3811 sb.append(prefix);
3812 sb.append(" Mobile radio active unknown time: ");
3813 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
3814 sb.append("(");
3815 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
3816 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
3817 sb.append("x");
3818 pw.println(sb.toString());
3819 }
3820
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003821 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
3822 if (mobileActiveAdjustedTime != 0) {
3823 sb.setLength(0);
3824 sb.append(prefix);
3825 sb.append(" Mobile radio active adjusted time: ");
3826 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
3827 sb.append("(");
3828 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
3829 sb.append(")");
3830 pw.println(sb.toString());
3831 }
3832
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003833 printControllerActivity(pw, sb, prefix, "Radio", getModemControllerActivity(), which);
3834
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003835 pw.print(prefix);
3836 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
3837 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
3838 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
3839 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003840 sb.setLength(0);
3841 sb.append(prefix);
3842 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
3843 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
3844 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
3845 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003846 sb.append(")");
3847 pw.println(sb.toString());
3848
3849 sb.setLength(0);
3850 sb.append(prefix);
3851 sb.append(" Wifi states:");
3852 didOne = false;
3853 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003854 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003855 if (time == 0) {
3856 continue;
3857 }
3858 sb.append("\n ");
3859 didOne = true;
3860 sb.append(WIFI_STATE_NAMES[i]);
3861 sb.append(" ");
3862 formatTimeMs(sb, time/1000);
3863 sb.append("(");
3864 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3865 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07003866 sb.append(getWifiStateCount(i, which));
3867 sb.append("x");
3868 }
3869 if (!didOne) sb.append(" (no activity)");
3870 pw.println(sb.toString());
3871
3872 sb.setLength(0);
3873 sb.append(prefix);
3874 sb.append(" Wifi supplicant states:");
3875 didOne = false;
3876 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3877 final long time = getWifiSupplStateTime(i, rawRealtime, which);
3878 if (time == 0) {
3879 continue;
3880 }
3881 sb.append("\n ");
3882 didOne = true;
3883 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
3884 sb.append(" ");
3885 formatTimeMs(sb, time/1000);
3886 sb.append("(");
3887 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3888 sb.append(") ");
3889 sb.append(getWifiSupplStateCount(i, which));
3890 sb.append("x");
3891 }
3892 if (!didOne) sb.append(" (no activity)");
3893 pw.println(sb.toString());
3894
3895 sb.setLength(0);
3896 sb.append(prefix);
3897 sb.append(" Wifi signal levels:");
3898 didOne = false;
3899 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3900 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
3901 if (time == 0) {
3902 continue;
3903 }
3904 sb.append("\n ");
3905 sb.append(prefix);
3906 didOne = true;
3907 sb.append("level(");
3908 sb.append(i);
3909 sb.append(") ");
3910 formatTimeMs(sb, time/1000);
3911 sb.append("(");
3912 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3913 sb.append(") ");
3914 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003915 sb.append("x");
3916 }
3917 if (!didOne) sb.append(" (no activity)");
3918 pw.println(sb.toString());
3919
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003920 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07003921
Adam Lesinski50e47602015-12-04 17:04:54 -08003922 pw.print(prefix);
3923 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
3924 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
3925
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003926 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
3927 sb.setLength(0);
3928 sb.append(prefix);
3929 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
3930 pw.println(sb.toString());
3931
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003932 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
3933 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003934
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003935 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07003936
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003937 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003938 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003939 pw.print(prefix); pw.println(" Device is currently unplugged");
3940 pw.print(prefix); pw.print(" Discharge cycle start level: ");
3941 pw.println(getDischargeStartLevel());
3942 pw.print(prefix); pw.print(" Discharge cycle current level: ");
3943 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07003944 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003945 pw.print(prefix); pw.println(" Device is currently plugged into power");
3946 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
3947 pw.println(getDischargeStartLevel());
3948 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
3949 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003950 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003951 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
3952 pw.println(getDischargeAmountScreenOn());
3953 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
3954 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07003955 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003956 } else {
3957 pw.print(prefix); pw.println(" Device battery use since last full charge");
3958 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
3959 pw.println(getLowDischargeAmountSinceCharge());
3960 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
3961 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003962 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
3963 pw.println(getDischargeAmountScreenOnSinceCharge());
3964 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
3965 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08003966 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07003967 }
Dianne Hackborn81038902012-11-26 17:04:09 -08003968
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003969 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003970 helper.create(this);
3971 helper.refreshStats(which, UserHandle.USER_ALL);
3972 List<BatterySipper> sippers = helper.getUsageList();
3973 if (sippers != null && sippers.size() > 0) {
3974 pw.print(prefix); pw.println(" Estimated power use (mAh):");
3975 pw.print(prefix); pw.print(" Capacity: ");
3976 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08003977 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07003978 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
3979 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
3980 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
3981 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003982 pw.println();
3983 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003984 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003985 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003986 switch (bs.drainType) {
3987 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003988 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003989 break;
3990 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003991 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003992 break;
3993 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003994 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003995 break;
3996 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003997 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003998 break;
3999 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004000 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004001 break;
4002 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004003 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004004 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004005 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004006 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004007 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004008 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004009 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004010 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004011 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004012 break;
4013 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004014 pw.print(" User "); pw.print(bs.userId);
4015 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004016 break;
4017 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004018 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004019 break;
4020 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004021 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004022 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004023 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004024 pw.print(" Camera: ");
4025 break;
4026 default:
4027 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004028 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004029 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004030 printmAh(pw, bs.totalPowerMah);
4031
Adam Lesinski57123002015-06-12 16:12:07 -07004032 if (bs.usagePowerMah != bs.totalPowerMah) {
4033 // If the usage (generic power) isn't the whole amount, we list out
4034 // what components are involved in the calculation.
4035
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004036 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004037 if (bs.usagePowerMah != 0) {
4038 pw.print(" usage=");
4039 printmAh(pw, bs.usagePowerMah);
4040 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004041 if (bs.cpuPowerMah != 0) {
4042 pw.print(" cpu=");
4043 printmAh(pw, bs.cpuPowerMah);
4044 }
4045 if (bs.wakeLockPowerMah != 0) {
4046 pw.print(" wake=");
4047 printmAh(pw, bs.wakeLockPowerMah);
4048 }
4049 if (bs.mobileRadioPowerMah != 0) {
4050 pw.print(" radio=");
4051 printmAh(pw, bs.mobileRadioPowerMah);
4052 }
4053 if (bs.wifiPowerMah != 0) {
4054 pw.print(" wifi=");
4055 printmAh(pw, bs.wifiPowerMah);
4056 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004057 if (bs.bluetoothPowerMah != 0) {
4058 pw.print(" bt=");
4059 printmAh(pw, bs.bluetoothPowerMah);
4060 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004061 if (bs.gpsPowerMah != 0) {
4062 pw.print(" gps=");
4063 printmAh(pw, bs.gpsPowerMah);
4064 }
4065 if (bs.sensorPowerMah != 0) {
4066 pw.print(" sensor=");
4067 printmAh(pw, bs.sensorPowerMah);
4068 }
4069 if (bs.cameraPowerMah != 0) {
4070 pw.print(" camera=");
4071 printmAh(pw, bs.cameraPowerMah);
4072 }
4073 if (bs.flashlightPowerMah != 0) {
4074 pw.print(" flash=");
4075 printmAh(pw, bs.flashlightPowerMah);
4076 }
4077 pw.print(" )");
4078 }
4079 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004080 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004081 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004082 }
4083
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004084 sippers = helper.getMobilemsppList();
4085 if (sippers != null && sippers.size() > 0) {
4086 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004087 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004088 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004089 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004090 sb.setLength(0);
4091 sb.append(prefix); sb.append(" Uid ");
4092 UserHandle.formatUid(sb, bs.uidObj.getUid());
4093 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4094 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4095 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004096 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004097 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004098 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004099 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004100 sb.setLength(0);
4101 sb.append(prefix);
4102 sb.append(" TOTAL TIME: ");
4103 formatTimeMs(sb, totalTime);
4104 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4105 sb.append(")");
4106 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004107 pw.println();
4108 }
4109
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004110 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4111 @Override
4112 public int compare(TimerEntry lhs, TimerEntry rhs) {
4113 long lhsTime = lhs.mTime;
4114 long rhsTime = rhs.mTime;
4115 if (lhsTime < rhsTime) {
4116 return 1;
4117 }
4118 if (lhsTime > rhsTime) {
4119 return -1;
4120 }
4121 return 0;
4122 }
4123 };
4124
4125 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004126 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4127 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004128 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004129 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4130 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4131 : kernelWakelocks.entrySet()) {
4132 final BatteryStats.Timer timer = ent.getValue();
4133 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004134 if (totalTimeMillis > 0) {
4135 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4136 }
4137 }
4138 if (ktimers.size() > 0) {
4139 Collections.sort(ktimers, timerComparator);
4140 pw.print(prefix); pw.println(" All kernel wake locks:");
4141 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004142 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004143 String linePrefix = ": ";
4144 sb.setLength(0);
4145 sb.append(prefix);
4146 sb.append(" Kernel Wake lock ");
4147 sb.append(timer.mName);
4148 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4149 which, linePrefix);
4150 if (!linePrefix.equals(": ")) {
4151 sb.append(" realtime");
4152 // Only print out wake locks that were held
4153 pw.println(sb.toString());
4154 }
4155 }
4156 pw.println();
4157 }
4158 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004159
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004160 if (timers.size() > 0) {
4161 Collections.sort(timers, timerComparator);
4162 pw.print(prefix); pw.println(" All partial wake locks:");
4163 for (int i=0; i<timers.size(); i++) {
4164 TimerEntry timer = timers.get(i);
4165 sb.setLength(0);
4166 sb.append(" Wake lock ");
4167 UserHandle.formatUid(sb, timer.mId);
4168 sb.append(" ");
4169 sb.append(timer.mName);
4170 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4171 sb.append(" realtime");
4172 pw.println(sb.toString());
4173 }
4174 timers.clear();
4175 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08004176 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004177
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004178 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004179 if (wakeupReasons.size() > 0) {
4180 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004181 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004182 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004183 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004184 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
4185 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004186 }
4187 Collections.sort(reasons, timerComparator);
4188 for (int i=0; i<reasons.size(); i++) {
4189 TimerEntry timer = reasons.get(i);
4190 String linePrefix = ": ";
4191 sb.setLength(0);
4192 sb.append(prefix);
4193 sb.append(" Wakeup reason ");
4194 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004195 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4196 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004197 pw.println(sb.toString());
4198 }
4199 pw.println();
4200 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004201 }
Evan Millar22ac0432009-03-31 11:33:18 -07004202
James Carr2dd7e5e2016-07-20 18:48:39 -07004203 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
4204 pw.println("Memory Stats");
4205 for (int i = 0; i < mMemoryStats.size(); i++) {
4206 sb.setLength(0);
4207 sb.append("Bandwidth ");
4208 sb.append(mMemoryStats.keyAt(i));
4209 sb.append(" Time ");
4210 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
4211 pw.println(sb.toString());
4212 }
4213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004214 for (int iu=0; iu<NU; iu++) {
4215 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004216 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004217 continue;
4218 }
4219
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004220 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07004221
4222 pw.print(prefix);
4223 pw.print(" ");
4224 UserHandle.formatUid(pw, uid);
4225 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004226 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004227
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004228 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4229 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4230 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4231 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004232 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4233 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
4234
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004235 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4236 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004237 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4238 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004239
4240 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
4241 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
4242
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004243 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4244 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4245 final int wifiScanCount = u.getWifiScanCount(which);
4246 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004247
Adam Lesinski5f056f62016-07-14 16:56:08 -07004248 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
4249 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
4250
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004251 if (mobileRxBytes > 0 || mobileTxBytes > 0
4252 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004253 pw.print(prefix); pw.print(" Mobile network: ");
4254 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004255 pw.print(formatBytesLocked(mobileTxBytes));
4256 pw.print(" sent (packets "); pw.print(mobileRxPackets);
4257 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004258 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004259 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
4260 sb.setLength(0);
4261 sb.append(prefix); sb.append(" Mobile radio active: ");
4262 formatTimeMs(sb, uidMobileActiveTime / 1000);
4263 sb.append("(");
4264 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
4265 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
4266 long packets = mobileRxPackets + mobileTxPackets;
4267 if (packets == 0) {
4268 packets = 1;
4269 }
4270 sb.append(" @ ");
4271 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
4272 sb.append(" mspp");
4273 pw.println(sb.toString());
4274 }
4275
Adam Lesinski5f056f62016-07-14 16:56:08 -07004276 if (mobileWakeup > 0) {
4277 sb.setLength(0);
4278 sb.append(prefix);
4279 sb.append(" Mobile radio AP wakeups: ");
4280 sb.append(mobileWakeup);
4281 pw.println(sb.toString());
4282 }
4283
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004284 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
4285 u.getModemControllerActivity(), which);
4286
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004287 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004288 pw.print(prefix); pw.print(" Wi-Fi network: ");
4289 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004290 pw.print(formatBytesLocked(wifiTxBytes));
4291 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4292 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004293 }
4294
Dianne Hackborn62793e42015-03-09 11:15:41 -07004295 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004296 || uidWifiRunningTime != 0) {
4297 sb.setLength(0);
4298 sb.append(prefix); sb.append(" Wifi Running: ");
4299 formatTimeMs(sb, uidWifiRunningTime / 1000);
4300 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4301 whichBatteryRealtime)); sb.append(")\n");
4302 sb.append(prefix); sb.append(" Full Wifi Lock: ");
4303 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4304 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4305 whichBatteryRealtime)); sb.append(")\n");
4306 sb.append(prefix); sb.append(" Wifi Scan: ");
4307 formatTimeMs(sb, wifiScanTime / 1000);
4308 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004309 whichBatteryRealtime)); sb.append(") ");
4310 sb.append(wifiScanCount);
4311 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004312 pw.println(sb.toString());
4313 }
4314
Adam Lesinski5f056f62016-07-14 16:56:08 -07004315 if (wifiWakeup > 0) {
4316 sb.setLength(0);
4317 sb.append(prefix);
4318 sb.append(" WiFi AP wakeups: ");
4319 sb.append(wifiWakeup);
4320 pw.println(sb.toString());
4321 }
4322
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004323 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
4324 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07004325
Adam Lesinski50e47602015-12-04 17:04:54 -08004326 if (btRxBytes > 0 || btTxBytes > 0) {
4327 pw.print(prefix); pw.print(" Bluetooth network: ");
4328 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
4329 pw.print(formatBytesLocked(btTxBytes));
4330 pw.println(" sent");
4331 }
4332
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004333 uidActivity |= printTimer(pw, sb, u.getBluetoothScanTimer(), rawRealtime, which, prefix,
4334 "Bluetooth Scan");
4335
Dianne Hackborn617f8772009-03-31 15:04:46 -07004336 if (u.hasUserActivity()) {
4337 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07004338 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004339 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004340 if (val != 0) {
4341 if (!hasData) {
4342 sb.setLength(0);
4343 sb.append(" User activity: ");
4344 hasData = true;
4345 } else {
4346 sb.append(", ");
4347 }
4348 sb.append(val);
4349 sb.append(" ");
4350 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
4351 }
4352 }
4353 if (hasData) {
4354 pw.println(sb.toString());
4355 }
4356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004358 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4359 = u.getWakelockStats();
4360 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004361 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004362 int countWakelock = 0;
4363 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4364 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4365 String linePrefix = ": ";
4366 sb.setLength(0);
4367 sb.append(prefix);
4368 sb.append(" Wake lock ");
4369 sb.append(wakelocks.keyAt(iw));
4370 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
4371 "full", which, linePrefix);
4372 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
4373 "partial", which, linePrefix);
4374 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
4375 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004376 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
4377 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07004378 sb.append(" realtime");
4379 pw.println(sb.toString());
4380 uidActivity = true;
4381 countWakelock++;
4382
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004383 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
4384 rawRealtime, which);
4385 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
4386 rawRealtime, which);
4387 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
4388 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004389 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07004390 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004391 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004392 if (countWakelock > 1) {
4393 if (totalFullWakelock != 0 || totalPartialWakelock != 0
4394 || totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004395 sb.setLength(0);
4396 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004397 sb.append(" TOTAL wake: ");
4398 boolean needComma = false;
4399 if (totalFullWakelock != 0) {
4400 needComma = true;
4401 formatTimeMs(sb, totalFullWakelock);
4402 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004403 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004404 if (totalPartialWakelock != 0) {
4405 if (needComma) {
4406 sb.append(", ");
4407 }
4408 needComma = true;
4409 formatTimeMs(sb, totalPartialWakelock);
4410 sb.append("partial");
4411 }
4412 if (totalWindowWakelock != 0) {
4413 if (needComma) {
4414 sb.append(", ");
4415 }
4416 needComma = true;
4417 formatTimeMs(sb, totalWindowWakelock);
4418 sb.append("window");
4419 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004420 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07004421 if (needComma) {
4422 sb.append(",");
4423 }
4424 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004425 formatTimeMs(sb, totalDrawWakelock);
4426 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07004427 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004428 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004429 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004430 }
4431 }
4432
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004433 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4434 for (int isy=syncs.size()-1; isy>=0; isy--) {
4435 final Timer timer = syncs.valueAt(isy);
4436 // Convert from microseconds to milliseconds with rounding
4437 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4438 final int count = timer.getCountLocked(which);
4439 sb.setLength(0);
4440 sb.append(prefix);
4441 sb.append(" Sync ");
4442 sb.append(syncs.keyAt(isy));
4443 sb.append(": ");
4444 if (totalTime != 0) {
4445 formatTimeMs(sb, totalTime);
4446 sb.append("realtime (");
4447 sb.append(count);
4448 sb.append(" times)");
4449 } else {
4450 sb.append("(not used)");
4451 }
4452 pw.println(sb.toString());
4453 uidActivity = true;
4454 }
4455
4456 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4457 for (int ij=jobs.size()-1; ij>=0; ij--) {
4458 final Timer timer = jobs.valueAt(ij);
4459 // Convert from microseconds to milliseconds with rounding
4460 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4461 final int count = timer.getCountLocked(which);
4462 sb.setLength(0);
4463 sb.append(prefix);
4464 sb.append(" Job ");
4465 sb.append(jobs.keyAt(ij));
4466 sb.append(": ");
4467 if (totalTime != 0) {
4468 formatTimeMs(sb, totalTime);
4469 sb.append("realtime (");
4470 sb.append(count);
4471 sb.append(" times)");
4472 } else {
4473 sb.append("(not used)");
4474 }
4475 pw.println(sb.toString());
4476 uidActivity = true;
4477 }
4478
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004479 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
4480 prefix, "Flashlight");
4481 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
4482 prefix, "Camera");
4483 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
4484 prefix, "Video");
4485 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
4486 prefix, "Audio");
4487
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004488 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4489 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004490 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004491 final Uid.Sensor se = sensors.valueAt(ise);
4492 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004493 sb.setLength(0);
4494 sb.append(prefix);
4495 sb.append(" Sensor ");
4496 int handle = se.getHandle();
4497 if (handle == Uid.Sensor.GPS) {
4498 sb.append("GPS");
4499 } else {
4500 sb.append(handle);
4501 }
4502 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004503
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004504 final Timer timer = se.getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004505 final Counter bgCounter = se.getSensorBgCount();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004506 if (timer != null) {
4507 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004508 final long totalTime = (timer.getTotalTimeLocked(
Dianne Hackborn61659e52014-07-09 16:13:01 -07004509 rawRealtime, which) + 500) / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004510 final int count = timer.getCountLocked(which);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004511 final int bgCount = bgCounter != null ? bgCounter.getCountLocked(which) : 0;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004512 //timer.logState();
4513 if (totalTime != 0) {
4514 formatTimeMs(sb, totalTime);
4515 sb.append("realtime (");
4516 sb.append(count);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004517 sb.append(" times");
4518 if (bgCount > 0) {
4519 sb.append(", ");
4520 sb.append(bgCount);
4521 sb.append(" bg");
4522 }
4523 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004524 } else {
4525 sb.append("(not used)");
4526 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004527 } else {
4528 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004529 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004530
4531 pw.println(sb.toString());
4532 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004533 }
4534
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004535 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
4536 "Vibrator");
4537 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
4538 prefix, "Foreground activities");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004539
Dianne Hackborn61659e52014-07-09 16:13:01 -07004540 long totalStateTime = 0;
4541 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
4542 long time = u.getProcessStateTime(ips, rawRealtime, which);
4543 if (time > 0) {
4544 totalStateTime += time;
4545 sb.setLength(0);
4546 sb.append(prefix);
4547 sb.append(" ");
4548 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
4549 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08004550 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004551 pw.println(sb.toString());
4552 uidActivity = true;
4553 }
4554 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08004555 if (totalStateTime > 0) {
4556 sb.setLength(0);
4557 sb.append(prefix);
4558 sb.append(" Total running: ");
4559 formatTimeMs(sb, (totalStateTime + 500) / 1000);
4560 pw.println(sb.toString());
4561 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004562
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004563 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4564 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004565 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
4566 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004567 sb.setLength(0);
4568 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07004569 sb.append(" Total cpu time: u=");
4570 formatTimeMs(sb, userCpuTimeUs / 1000);
4571 sb.append("s=");
4572 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004573 sb.append("p=");
4574 printmAh(sb, powerCpuMaUs / (1000.0 * 1000.0 * 60.0 * 60.0));
4575 sb.append("mAh");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004576 pw.println(sb.toString());
4577 }
4578
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004579 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4580 = u.getProcessStats();
4581 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4582 final Uid.Proc ps = processStats.valueAt(ipr);
4583 long userTime;
4584 long systemTime;
4585 long foregroundTime;
4586 int starts;
4587 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004588
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004589 userTime = ps.getUserTime(which);
4590 systemTime = ps.getSystemTime(which);
4591 foregroundTime = ps.getForegroundTime(which);
4592 starts = ps.getStarts(which);
4593 final int numCrashes = ps.getNumCrashes(which);
4594 final int numAnrs = ps.getNumAnrs(which);
4595 numExcessive = which == STATS_SINCE_CHARGED
4596 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004597
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004598 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
4599 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
4600 sb.setLength(0);
4601 sb.append(prefix); sb.append(" Proc ");
4602 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
4603 sb.append(prefix); sb.append(" CPU: ");
4604 formatTimeMs(sb, userTime); sb.append("usr + ");
4605 formatTimeMs(sb, systemTime); sb.append("krn ; ");
4606 formatTimeMs(sb, foregroundTime); sb.append("fg");
4607 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
4608 sb.append("\n"); sb.append(prefix); sb.append(" ");
4609 boolean hasOne = false;
4610 if (starts != 0) {
4611 hasOne = true;
4612 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004613 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004614 if (numCrashes != 0) {
4615 if (hasOne) {
4616 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004617 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004618 hasOne = true;
4619 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004620 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004621 if (numAnrs != 0) {
4622 if (hasOne) {
4623 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004624 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004625 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004626 }
4627 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004628 pw.println(sb.toString());
4629 for (int e=0; e<numExcessive; e++) {
4630 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
4631 if (ew != null) {
4632 pw.print(prefix); pw.print(" * Killed for ");
4633 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
4634 pw.print("wake lock");
4635 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
4636 pw.print("cpu");
4637 } else {
4638 pw.print("unknown");
4639 }
4640 pw.print(" use: ");
4641 TimeUtils.formatDuration(ew.usedTime, pw);
4642 pw.print(" over ");
4643 TimeUtils.formatDuration(ew.overTime, pw);
4644 if (ew.overTime != 0) {
4645 pw.print(" (");
4646 pw.print((ew.usedTime*100)/ew.overTime);
4647 pw.println("%)");
4648 }
4649 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004650 }
4651 uidActivity = true;
4652 }
4653 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004654
4655 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4656 = u.getPackageStats();
4657 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4658 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
4659 pw.println(":");
4660 boolean apkActivity = false;
4661 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4662 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4663 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
4664 pw.print(prefix); pw.print(" Wakeup alarm ");
4665 pw.print(alarms.keyAt(iwa)); pw.print(": ");
4666 pw.print(alarms.valueAt(iwa).getCountLocked(which));
4667 pw.println(" times");
4668 apkActivity = true;
4669 }
4670 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4671 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4672 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4673 final long startTime = ss.getStartTime(batteryUptime, which);
4674 final int starts = ss.getStarts(which);
4675 final int launches = ss.getLaunches(which);
4676 if (startTime != 0 || starts != 0 || launches != 0) {
4677 sb.setLength(0);
4678 sb.append(prefix); sb.append(" Service ");
4679 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
4680 sb.append(prefix); sb.append(" Created for: ");
4681 formatTimeMs(sb, startTime / 1000);
4682 sb.append("uptime\n");
4683 sb.append(prefix); sb.append(" Starts: ");
4684 sb.append(starts);
4685 sb.append(", launches: "); sb.append(launches);
4686 pw.println(sb.toString());
4687 apkActivity = true;
4688 }
4689 }
4690 if (!apkActivity) {
4691 pw.print(prefix); pw.println(" (nothing executed)");
4692 }
4693 uidActivity = true;
4694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004695 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004696 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004697 }
4698 }
4699 }
4700
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004701 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004702 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004703 int diff = oldval ^ newval;
4704 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004705 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004706 for (int i=0; i<descriptions.length; i++) {
4707 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004708 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004709 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004710 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004711 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004712 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004713 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
4714 didWake = true;
4715 pw.print("=");
4716 if (longNames) {
4717 UserHandle.formatUid(pw, wakelockTag.uid);
4718 pw.print(":\"");
4719 pw.print(wakelockTag.string);
4720 pw.print("\"");
4721 } else {
4722 pw.print(wakelockTag.poolIdx);
4723 }
4724 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004725 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004726 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004727 pw.print("=");
4728 int val = (newval&bd.mask)>>bd.shift;
4729 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004730 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004731 } else {
4732 pw.print(val);
4733 }
4734 }
4735 }
4736 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004737 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07004738 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004739 if (longNames) {
4740 UserHandle.formatUid(pw, wakelockTag.uid);
4741 pw.print(":\"");
4742 pw.print(wakelockTag.string);
4743 pw.print("\"");
4744 } else {
4745 pw.print(wakelockTag.poolIdx);
4746 }
4747 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004748 }
4749
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004750 public void prepareForDumpLocked() {
4751 }
4752
4753 public static class HistoryPrinter {
4754 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004755 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004756 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004757 int oldStatus = -1;
4758 int oldHealth = -1;
4759 int oldPlug = -1;
4760 int oldTemp = -1;
4761 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004762 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004763 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004764
Dianne Hackborn3251b902014-06-20 14:40:53 -07004765 void reset() {
4766 oldState = oldState2 = 0;
4767 oldLevel = -1;
4768 oldStatus = -1;
4769 oldHealth = -1;
4770 oldPlug = -1;
4771 oldTemp = -1;
4772 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004773 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07004774 }
4775
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004776 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004777 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004778 if (!checkin) {
4779 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004780 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004781 pw.print(" (");
4782 pw.print(rec.numReadInts);
4783 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004784 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004785 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4786 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004787 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004788 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004789 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004790 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004791 }
4792 lastTime = rec.time;
4793 }
4794 if (rec.cmd == HistoryItem.CMD_START) {
4795 if (checkin) {
4796 pw.print(":");
4797 }
4798 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004799 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004800 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
4801 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004802 if (checkin) {
4803 pw.print(":");
4804 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07004805 if (rec.cmd == HistoryItem.CMD_RESET) {
4806 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004807 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004808 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004809 pw.print("TIME:");
4810 if (checkin) {
4811 pw.println(rec.currentTime);
4812 } else {
4813 pw.print(" ");
4814 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
4815 rec.currentTime).toString());
4816 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08004817 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
4818 if (checkin) {
4819 pw.print(":");
4820 }
4821 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004822 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
4823 if (checkin) {
4824 pw.print(":");
4825 }
4826 pw.println("*OVERFLOW*");
4827 } else {
4828 if (!checkin) {
4829 if (rec.batteryLevel < 10) pw.print("00");
4830 else if (rec.batteryLevel < 100) pw.print("0");
4831 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004832 if (verbose) {
4833 pw.print(" ");
4834 if (rec.states < 0) ;
4835 else if (rec.states < 0x10) pw.print("0000000");
4836 else if (rec.states < 0x100) pw.print("000000");
4837 else if (rec.states < 0x1000) pw.print("00000");
4838 else if (rec.states < 0x10000) pw.print("0000");
4839 else if (rec.states < 0x100000) pw.print("000");
4840 else if (rec.states < 0x1000000) pw.print("00");
4841 else if (rec.states < 0x10000000) pw.print("0");
4842 pw.print(Integer.toHexString(rec.states));
4843 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004844 } else {
4845 if (oldLevel != rec.batteryLevel) {
4846 oldLevel = rec.batteryLevel;
4847 pw.print(",Bl="); pw.print(rec.batteryLevel);
4848 }
4849 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004850 if (oldStatus != rec.batteryStatus) {
4851 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004852 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004853 switch (oldStatus) {
4854 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004855 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004856 break;
4857 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004858 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004859 break;
4860 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004861 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004862 break;
4863 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004864 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004865 break;
4866 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004867 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004868 break;
4869 default:
4870 pw.print(oldStatus);
4871 break;
4872 }
4873 }
4874 if (oldHealth != rec.batteryHealth) {
4875 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004876 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004877 switch (oldHealth) {
4878 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004879 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004880 break;
4881 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004882 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004883 break;
4884 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004885 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004886 break;
4887 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004888 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004889 break;
4890 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004891 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004892 break;
4893 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004894 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004895 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004896 case BatteryManager.BATTERY_HEALTH_COLD:
4897 pw.print(checkin ? "c" : "cold");
4898 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004899 default:
4900 pw.print(oldHealth);
4901 break;
4902 }
4903 }
4904 if (oldPlug != rec.batteryPlugType) {
4905 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004906 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004907 switch (oldPlug) {
4908 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004909 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004910 break;
4911 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004912 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004913 break;
4914 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004915 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004916 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07004917 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004918 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07004919 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004920 default:
4921 pw.print(oldPlug);
4922 break;
4923 }
4924 }
4925 if (oldTemp != rec.batteryTemperature) {
4926 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004927 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004928 pw.print(oldTemp);
4929 }
4930 if (oldVolt != rec.batteryVoltage) {
4931 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004932 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004933 pw.print(oldVolt);
4934 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004935 final int chargeMAh = rec.batteryChargeUAh / 1000;
4936 if (oldChargeMAh != chargeMAh) {
4937 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07004938 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004939 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07004940 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004941 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004942 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004943 printBitDescriptions(pw, oldState2, rec.states2, null,
4944 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004945 if (rec.wakeReasonTag != null) {
4946 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07004947 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004948 pw.print(rec.wakeReasonTag.poolIdx);
4949 } else {
4950 pw.print(" wake_reason=");
4951 pw.print(rec.wakeReasonTag.uid);
4952 pw.print(":\"");
4953 pw.print(rec.wakeReasonTag.string);
4954 pw.print("\"");
4955 }
4956 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004957 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004958 pw.print(checkin ? "," : " ");
4959 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
4960 pw.print("+");
4961 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
4962 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004963 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004964 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
4965 : HISTORY_EVENT_NAMES;
4966 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
4967 | HistoryItem.EVENT_FLAG_FINISH);
4968 if (idx >= 0 && idx < eventNames.length) {
4969 pw.print(eventNames[idx]);
4970 } else {
4971 pw.print(checkin ? "Ev" : "event");
4972 pw.print(idx);
4973 }
4974 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004975 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004976 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08004977 } else {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004978 UserHandle.formatUid(pw, rec.eventTag.uid);
4979 pw.print(":\"");
4980 pw.print(rec.eventTag.string);
4981 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004982 }
4983 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004984 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004985 if (rec.stepDetails != null) {
4986 if (!checkin) {
4987 pw.print(" Details: cpu=");
4988 pw.print(rec.stepDetails.userTime);
4989 pw.print("u+");
4990 pw.print(rec.stepDetails.systemTime);
4991 pw.print("s");
4992 if (rec.stepDetails.appCpuUid1 >= 0) {
4993 pw.print(" (");
4994 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
4995 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
4996 if (rec.stepDetails.appCpuUid2 >= 0) {
4997 pw.print(", ");
4998 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
4999 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5000 }
5001 if (rec.stepDetails.appCpuUid3 >= 0) {
5002 pw.print(", ");
5003 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
5004 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5005 }
5006 pw.print(')');
5007 }
5008 pw.println();
5009 pw.print(" /proc/stat=");
5010 pw.print(rec.stepDetails.statUserTime);
5011 pw.print(" usr, ");
5012 pw.print(rec.stepDetails.statSystemTime);
5013 pw.print(" sys, ");
5014 pw.print(rec.stepDetails.statIOWaitTime);
5015 pw.print(" io, ");
5016 pw.print(rec.stepDetails.statIrqTime);
5017 pw.print(" irq, ");
5018 pw.print(rec.stepDetails.statSoftIrqTime);
5019 pw.print(" sirq, ");
5020 pw.print(rec.stepDetails.statIdlTime);
5021 pw.print(" idle");
5022 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
5023 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
5024 + rec.stepDetails.statSoftIrqTime;
5025 int total = totalRun + rec.stepDetails.statIdlTime;
5026 if (total > 0) {
5027 pw.print(" (");
5028 float perc = ((float)totalRun) / ((float)total) * 100;
5029 pw.print(String.format("%.1f%%", perc));
5030 pw.print(" of ");
5031 StringBuilder sb = new StringBuilder(64);
5032 formatTimeMsNoSpace(sb, total*10);
5033 pw.print(sb);
5034 pw.print(")");
5035 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005036 pw.print(", PlatformIdleStat ");
5037 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005038 pw.println();
5039 } else {
5040 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5041 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
5042 pw.print(rec.stepDetails.userTime);
5043 pw.print(":");
5044 pw.print(rec.stepDetails.systemTime);
5045 if (rec.stepDetails.appCpuUid1 >= 0) {
5046 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
5047 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5048 if (rec.stepDetails.appCpuUid2 >= 0) {
5049 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
5050 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5051 }
5052 if (rec.stepDetails.appCpuUid3 >= 0) {
5053 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
5054 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5055 }
5056 }
5057 pw.println();
5058 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5059 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
5060 pw.print(rec.stepDetails.statUserTime);
5061 pw.print(',');
5062 pw.print(rec.stepDetails.statSystemTime);
5063 pw.print(',');
5064 pw.print(rec.stepDetails.statIOWaitTime);
5065 pw.print(',');
5066 pw.print(rec.stepDetails.statIrqTime);
5067 pw.print(',');
5068 pw.print(rec.stepDetails.statSoftIrqTime);
5069 pw.print(',');
5070 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005071 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005072 if (rec.stepDetails.statPlatformIdleState != null) {
5073 pw.print(rec.stepDetails.statPlatformIdleState);
5074 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005075 pw.println();
5076 }
5077 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005078 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005079 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005080 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005081 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005082
5083 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
5084 UserHandle.formatUid(pw, uid);
5085 pw.print("=");
5086 pw.print(utime);
5087 pw.print("u+");
5088 pw.print(stime);
5089 pw.print("s");
5090 }
5091
5092 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
5093 pw.print('/');
5094 pw.print(uid);
5095 pw.print(":");
5096 pw.print(utime);
5097 pw.print(":");
5098 pw.print(stime);
5099 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005100 }
5101
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005102 private void printSizeValue(PrintWriter pw, long size) {
5103 float result = size;
5104 String suffix = "";
5105 if (result >= 10*1024) {
5106 suffix = "KB";
5107 result = result / 1024;
5108 }
5109 if (result >= 10*1024) {
5110 suffix = "MB";
5111 result = result / 1024;
5112 }
5113 if (result >= 10*1024) {
5114 suffix = "GB";
5115 result = result / 1024;
5116 }
5117 if (result >= 10*1024) {
5118 suffix = "TB";
5119 result = result / 1024;
5120 }
5121 if (result >= 10*1024) {
5122 suffix = "PB";
5123 result = result / 1024;
5124 }
5125 pw.print((int)result);
5126 pw.print(suffix);
5127 }
5128
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005129 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
5130 String label3, long estimatedTime) {
5131 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005132 return false;
5133 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005134 pw.print(label1);
5135 pw.print(label2);
5136 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005137 StringBuilder sb = new StringBuilder(64);
5138 formatTimeMs(sb, estimatedTime);
5139 pw.print(sb);
5140 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005141 return true;
5142 }
5143
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005144 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
5145 LevelStepTracker steps, boolean checkin) {
5146 if (steps == null) {
5147 return false;
5148 }
5149 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005150 if (count <= 0) {
5151 return false;
5152 }
5153 if (!checkin) {
5154 pw.println(header);
5155 }
Kweku Adams030980a2015-04-01 16:07:48 -07005156 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005157 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005158 long duration = steps.getDurationAt(i);
5159 int level = steps.getLevelAt(i);
5160 long initMode = steps.getInitModeAt(i);
5161 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005162 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005163 lineArgs[0] = Long.toString(duration);
5164 lineArgs[1] = Integer.toString(level);
5165 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5166 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5167 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
5168 case Display.STATE_ON: lineArgs[2] = "s+"; break;
5169 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
5170 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07005171 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005172 }
5173 } else {
5174 lineArgs[2] = "";
5175 }
5176 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5177 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
5178 } else {
5179 lineArgs[3] = "";
5180 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005181 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07005182 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005183 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07005184 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005185 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005186 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
5187 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005188 pw.print(prefix);
5189 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005190 TimeUtils.formatDuration(duration, pw);
5191 pw.print(" to "); pw.print(level);
5192 boolean haveModes = false;
5193 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5194 pw.print(" (");
5195 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5196 case Display.STATE_OFF: pw.print("screen-off"); break;
5197 case Display.STATE_ON: pw.print("screen-on"); break;
5198 case Display.STATE_DOZE: pw.print("screen-doze"); break;
5199 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07005200 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005201 }
5202 haveModes = true;
5203 }
5204 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5205 pw.print(haveModes ? ", " : " (");
5206 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
5207 ? "power-save-on" : "power-save-off");
5208 haveModes = true;
5209 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005210 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
5211 pw.print(haveModes ? ", " : " (");
5212 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
5213 ? "device-idle-on" : "device-idle-off");
5214 haveModes = true;
5215 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005216 if (haveModes) {
5217 pw.print(")");
5218 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005219 pw.println();
5220 }
5221 }
5222 return true;
5223 }
5224
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005225 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005226 public static final int DUMP_DAILY_ONLY = 1<<2;
5227 public static final int DUMP_HISTORY_ONLY = 1<<3;
5228 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
5229 public static final int DUMP_VERBOSE = 1<<5;
5230 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005231
Dianne Hackborn37de0982014-05-09 09:32:18 -07005232 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
5233 final HistoryPrinter hprinter = new HistoryPrinter();
5234 final HistoryItem rec = new HistoryItem();
5235 long lastTime = -1;
5236 long baseTime = -1;
5237 boolean printed = false;
5238 HistoryEventTracker tracker = null;
5239 while (getNextHistoryLocked(rec)) {
5240 lastTime = rec.time;
5241 if (baseTime < 0) {
5242 baseTime = lastTime;
5243 }
5244 if (rec.time >= histStart) {
5245 if (histStart >= 0 && !printed) {
5246 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07005247 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005248 || rec.cmd == HistoryItem.CMD_START
5249 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005250 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005251 hprinter.printNextItem(pw, rec, baseTime, checkin,
5252 (flags&DUMP_VERBOSE) != 0);
5253 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005254 } else if (rec.currentTime != 0) {
5255 printed = true;
5256 byte cmd = rec.cmd;
5257 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005258 hprinter.printNextItem(pw, rec, baseTime, checkin,
5259 (flags&DUMP_VERBOSE) != 0);
5260 rec.cmd = cmd;
5261 }
5262 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005263 if (rec.cmd != HistoryItem.CMD_UPDATE) {
5264 hprinter.printNextItem(pw, rec, baseTime, checkin,
5265 (flags&DUMP_VERBOSE) != 0);
5266 rec.cmd = HistoryItem.CMD_UPDATE;
5267 }
5268 int oldEventCode = rec.eventCode;
5269 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005270 rec.eventTag = new HistoryTag();
5271 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
5272 HashMap<String, SparseIntArray> active
5273 = tracker.getStateForEvent(i);
5274 if (active == null) {
5275 continue;
5276 }
5277 for (HashMap.Entry<String, SparseIntArray> ent
5278 : active.entrySet()) {
5279 SparseIntArray uids = ent.getValue();
5280 for (int j=0; j<uids.size(); j++) {
5281 rec.eventCode = i;
5282 rec.eventTag.string = ent.getKey();
5283 rec.eventTag.uid = uids.keyAt(j);
5284 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07005285 hprinter.printNextItem(pw, rec, baseTime, checkin,
5286 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005287 rec.wakeReasonTag = null;
5288 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005289 }
5290 }
5291 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005292 rec.eventCode = oldEventCode;
5293 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005294 tracker = null;
5295 }
5296 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005297 hprinter.printNextItem(pw, rec, baseTime, checkin,
5298 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07005299 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
5300 // This is an attempt to aggregate the previous state and generate
5301 // fake events to reflect that state at the point where we start
5302 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07005303 if (tracker == null) {
5304 tracker = new HistoryEventTracker();
5305 }
5306 tracker.updateState(rec.eventCode, rec.eventTag.string,
5307 rec.eventTag.uid, rec.eventTag.poolIdx);
5308 }
5309 }
5310 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07005311 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005312 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
5313 }
5314 }
5315
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005316 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
5317 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
5318 if (steps == null) {
5319 return;
5320 }
5321 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
5322 if (timeRemaining >= 0) {
5323 pw.print(prefix); pw.print(label); pw.print(" total time: ");
5324 tmpSb.setLength(0);
5325 formatTimeMs(tmpSb, timeRemaining);
5326 pw.print(tmpSb);
5327 pw.print(" (from "); pw.print(tmpOutInt[0]);
5328 pw.println(" steps)");
5329 }
5330 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5331 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5332 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
5333 if (estimatedTime > 0) {
5334 pw.print(prefix); pw.print(label); pw.print(" ");
5335 pw.print(STEP_LEVEL_MODE_LABELS[i]);
5336 pw.print(" time: ");
5337 tmpSb.setLength(0);
5338 formatTimeMs(tmpSb, estimatedTime);
5339 pw.print(tmpSb);
5340 pw.print(" (from "); pw.print(tmpOutInt[0]);
5341 pw.println(" steps)");
5342 }
5343 }
5344 }
5345
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005346 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
5347 ArrayList<PackageChange> changes) {
5348 if (changes == null) {
5349 return;
5350 }
5351 pw.print(prefix); pw.println("Package changes:");
5352 for (int i=0; i<changes.size(); i++) {
5353 PackageChange pc = changes.get(i);
5354 if (pc.mUpdate) {
5355 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
5356 pw.print(" vers="); pw.println(pc.mVersionCode);
5357 } else {
5358 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
5359 }
5360 }
5361 }
5362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 /**
5364 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
5365 *
5366 * @param pw a Printer to receive the dump output.
5367 */
5368 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005369 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005370 prepareForDumpLocked();
5371
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005372 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005373 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005374
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005375 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005376 final long historyTotalSize = getHistoryTotalSize();
5377 final long historyUsedSize = getHistoryUsedSize();
5378 if (startIteratingHistoryLocked()) {
5379 try {
5380 pw.print("Battery History (");
5381 pw.print((100*historyUsedSize)/historyTotalSize);
5382 pw.print("% used, ");
5383 printSizeValue(pw, historyUsedSize);
5384 pw.print(" used of ");
5385 printSizeValue(pw, historyTotalSize);
5386 pw.print(", ");
5387 pw.print(getHistoryStringPoolSize());
5388 pw.print(" strings using ");
5389 printSizeValue(pw, getHistoryStringPoolBytes());
5390 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07005391 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005392 pw.println();
5393 } finally {
5394 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005395 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005396 }
5397
5398 if (startIteratingOldHistoryLocked()) {
5399 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005400 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005401 pw.println("Old battery History:");
5402 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005403 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005404 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005405 if (baseTime < 0) {
5406 baseTime = rec.time;
5407 }
5408 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005409 }
5410 pw.println();
5411 } finally {
5412 finishIteratingOldHistoryLocked();
5413 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005414 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005415 }
5416
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005417 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005418 return;
5419 }
5420
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005421 if (!filtering) {
5422 SparseArray<? extends Uid> uidStats = getUidStats();
5423 final int NU = uidStats.size();
5424 boolean didPid = false;
5425 long nowRealtime = SystemClock.elapsedRealtime();
5426 for (int i=0; i<NU; i++) {
5427 Uid uid = uidStats.valueAt(i);
5428 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
5429 if (pids != null) {
5430 for (int j=0; j<pids.size(); j++) {
5431 Uid.Pid pid = pids.valueAt(j);
5432 if (!didPid) {
5433 pw.println("Per-PID Stats:");
5434 didPid = true;
5435 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005436 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
5437 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005438 pw.print(" PID "); pw.print(pids.keyAt(j));
5439 pw.print(" wake time: ");
5440 TimeUtils.formatDuration(time, pw);
5441 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005442 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005443 }
5444 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005445 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005446 pw.println();
5447 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005448 }
5449
5450 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005451 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
5452 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005453 long timeRemaining = computeBatteryTimeRemaining(
5454 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005455 if (timeRemaining >= 0) {
5456 pw.print(" Estimated discharge time remaining: ");
5457 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5458 pw.println();
5459 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005460 final LevelStepTracker steps = getDischargeLevelStepTracker();
5461 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5462 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
5463 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5464 STEP_LEVEL_MODE_VALUES[i], null));
5465 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005466 pw.println();
5467 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005468 if (dumpDurationSteps(pw, " ", "Charge step durations:",
5469 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005470 long timeRemaining = computeChargeTimeRemaining(
5471 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005472 if (timeRemaining >= 0) {
5473 pw.print(" Estimated charge time remaining: ");
5474 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5475 pw.println();
5476 }
5477 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005478 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005479 }
5480 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
5481 pw.println("Daily stats:");
5482 pw.print(" Current start time: ");
5483 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5484 getCurrentDailyStartTime()).toString());
5485 pw.print(" Next min deadline: ");
5486 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5487 getNextMinDailyDeadline()).toString());
5488 pw.print(" Next max deadline: ");
5489 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5490 getNextMaxDailyDeadline()).toString());
5491 StringBuilder sb = new StringBuilder(64);
5492 int[] outInt = new int[1];
5493 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
5494 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005495 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
5496 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005497 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005498 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
5499 dsteps, false)) {
5500 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5501 sb, outInt);
5502 }
5503 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
5504 csteps, false)) {
5505 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5506 sb, outInt);
5507 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005508 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005509 } else {
5510 pw.println(" Current daily steps:");
5511 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5512 sb, outInt);
5513 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5514 sb, outInt);
5515 }
5516 }
5517 DailyItem dit;
5518 int curIndex = 0;
5519 while ((dit=getDailyItemLocked(curIndex)) != null) {
5520 curIndex++;
5521 if ((flags&DUMP_DAILY_ONLY) != 0) {
5522 pw.println();
5523 }
5524 pw.print(" Daily from ");
5525 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
5526 pw.print(" to ");
5527 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
5528 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005529 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005530 if (dumpDurationSteps(pw, " ",
5531 " Discharge step durations:", dit.mDischargeSteps, false)) {
5532 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5533 sb, outInt);
5534 }
5535 if (dumpDurationSteps(pw, " ",
5536 " Charge step durations:", dit.mChargeSteps, false)) {
5537 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5538 sb, outInt);
5539 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005540 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005541 } else {
5542 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5543 sb, outInt);
5544 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5545 sb, outInt);
5546 }
5547 }
5548 pw.println();
5549 }
5550 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005551 pw.println("Statistics since last charge:");
5552 pw.println(" System starts: " + getStartCount()
5553 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07005554 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
5555 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005556 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005558 }
5559
5560 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005561 public void dumpCheckinLocked(Context context, PrintWriter pw,
5562 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005563 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005564
5565 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005566 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
5567 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005568
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005569 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
5570
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005571 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005572 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005573
5574 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07005575 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005576 try {
5577 for (int i=0; i<getHistoryStringPoolSize(); i++) {
5578 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5579 pw.print(HISTORY_STRING_POOL); pw.print(',');
5580 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005581 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005582 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005583 pw.print(",\"");
5584 String str = getHistoryTagPoolString(i);
5585 str = str.replace("\\", "\\\\");
5586 str = str.replace("\"", "\\\"");
5587 pw.print(str);
5588 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005589 pw.println();
5590 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005591 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005592 } finally {
5593 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08005594 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005595 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005596 }
5597
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005598 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005599 return;
5600 }
5601
Dianne Hackborne4a59512010-12-07 11:08:07 -08005602 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005603 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08005604 for (int i=0; i<apps.size(); i++) {
5605 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005606 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
5607 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08005608 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005609 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
5610 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005611 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005612 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005613 }
5614 SparseArray<? extends Uid> uidStats = getUidStats();
5615 final int NU = uidStats.size();
5616 String[] lineArgs = new String[2];
5617 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005618 int uid = UserHandle.getAppId(uidStats.keyAt(i));
5619 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
5620 if (pkgs != null && !pkgs.second.value) {
5621 pkgs.second.value = true;
5622 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08005623 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005624 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005625 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
5626 (Object[])lineArgs);
5627 }
5628 }
5629 }
5630 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005631 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005632 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005633 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07005634 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005635 if (timeRemaining >= 0) {
5636 lineArgs[0] = Long.toString(timeRemaining);
5637 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
5638 (Object[])lineArgs);
5639 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005640 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07005641 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005642 if (timeRemaining >= 0) {
5643 lineArgs[0] = Long.toString(timeRemaining);
5644 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
5645 (Object[])lineArgs);
5646 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005647 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
5648 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005649 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005651}