blob: 763072f264fc7447f9d863ec0798f322d4c46971 [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;
Adam Lesinski041d9172016-12-12 12:03:56 -080027import java.util.function.Predicate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
Dianne Hackborna7c837f2014-01-15 16:20:44 -080029import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080030import android.content.pm.ApplicationInfo;
Wink Saville52840902011-02-18 12:40:47 -080031import android.telephony.SignalStrength;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080032import android.text.format.DateFormat;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070033import android.util.ArrayMap;
Joe Onorato92fd23f2016-07-25 11:18:42 -070034import android.util.Log;
James Carr2dd7e5e2016-07-20 18:48:39 -070035import android.util.LongSparseArray;
Dianne Hackborn9cfba352016-03-24 17:31:28 -070036import android.util.MutableBoolean;
37import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.util.Printer;
39import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070040import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070041import android.util.TimeUtils;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070042import android.view.Display;
Amith Yamasaniab9ad192016-12-06 12:46:59 -080043
Dianne Hackborna7c837f2014-01-15 16:20:44 -080044import com.android.internal.os.BatterySipper;
45import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
47/**
48 * A class providing access to battery usage statistics, including information on
49 * wakelocks, processes, packages, and services. All times are represented in microseconds
50 * except where indicated otherwise.
51 * @hide
52 */
53public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070054 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
56 private static final boolean LOCAL_LOGV = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070057
58 /** @hide */
59 public static final String SERVICE_NAME = "batterystats";
60
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 /**
62 * A constant indicating a partial wake lock timer.
63 */
64 public static final int WAKE_TYPE_PARTIAL = 0;
65
66 /**
67 * A constant indicating a full wake lock timer.
68 */
69 public static final int WAKE_TYPE_FULL = 1;
70
71 /**
72 * A constant indicating a window wake lock timer.
73 */
74 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 /**
77 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 */
79 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070080
81 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070082 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070083 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070084 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070085
86 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070087 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070088 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070089 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070090
91 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070092 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070093 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070094 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
Dianne Hackborn62793e42015-03-09 11:15:41 -070096 /**
97 * A constant indicating a wifi multicast timer
98 */
99 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700102 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700103 */
104 public static final int VIDEO_TURNED_ON = 8;
105
106 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800107 * A constant indicating a vibrator on timer
108 */
109 public static final int VIBRATOR_ON = 9;
110
111 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700112 * A constant indicating a foreground activity timer
113 */
114 public static final int FOREGROUND_ACTIVITY = 10;
115
116 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700117 * A constant indicating a wifi batched scan is active
118 */
119 public static final int WIFI_BATCHED_SCAN = 11;
120
121 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700122 * A constant indicating a process state timer
123 */
124 public static final int PROCESS_STATE = 12;
125
126 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700127 * A constant indicating a sync timer
128 */
129 public static final int SYNC = 13;
130
131 /**
132 * A constant indicating a job timer
133 */
134 public static final int JOB = 14;
135
136 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800137 * A constant indicating an audio turn on timer
138 */
139 public static final int AUDIO_TURNED_ON = 15;
140
141 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700142 * A constant indicating a flashlight turn on timer
143 */
144 public static final int FLASHLIGHT_TURNED_ON = 16;
145
146 /**
147 * A constant indicating a camera turn on timer
148 */
149 public static final int CAMERA_TURNED_ON = 17;
150
151 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700152 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700153 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700154 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700155
156 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800157 * A constant indicating a bluetooth scan timer.
158 */
159 public static final int BLUETOOTH_SCAN_ON = 19;
160
161 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 * Include all of the data in the stats, including previously saved data.
163 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700164 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165
166 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 * Include only the current run in the stats.
168 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700169 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170
171 /**
172 * Include only the run since the last time the device was unplugged in the stats.
173 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700174 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700175
176 // NOTE: Update this list if you add/change any stats above.
177 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700178 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700179 private static final String[] STAT_NAMES = { "l", "c", "u" };
180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700182 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700183 *
184 * New in version 19:
185 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800186 * New in version 20:
187 * - Sensor gets a background counter.
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700188 */
Kweku Adams47db5a82016-12-09 19:04:50 -0800189 static final String CHECKIN_VERSION = "20";
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700190
191 /**
192 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700194 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700195
Evan Millar22ac0432009-03-31 11:33:18 -0700196 private static final long BYTES_PER_KB = 1024;
197 private static final long BYTES_PER_MB = 1048576; // 1024^2
198 private static final long BYTES_PER_GB = 1073741824; //1024^3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700200 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800201 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700202 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700204 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700205 private static final String CPU_DATA = "cpu";
Evan Millare84de8d2009-04-02 22:16:12 -0700206 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800207 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700208 private static final String FOREGROUND_DATA = "fg";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700209 private static final String STATE_TIME_DATA = "st";
Evan Millare84de8d2009-04-02 22:16:12 -0700210 private static final String WAKELOCK_DATA = "wl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700211 private static final String SYNC_DATA = "sy";
212 private static final String JOB_DATA = "jb";
Evan Millarc64edde2009-04-18 12:26:32 -0700213 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700214 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700215 private static final String NETWORK_DATA = "nt";
216 private static final String USER_ACTIVITY_DATA = "ua";
217 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800218 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700219 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700220 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700221 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800222 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
223 private static final String WIFI_CONTROLLER_DATA = "wfcd";
224 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
225 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700226 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700227 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800228 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800229 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
230 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800231 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700232 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700233 private static final String SCREEN_BRIGHTNESS_DATA = "br";
234 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700235 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700236 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
237 private static final String DATA_CONNECTION_TIME_DATA = "dct";
238 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800239 private static final String WIFI_STATE_TIME_DATA = "wst";
240 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700241 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
242 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
243 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
244 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800245 private static final String POWER_USE_SUMMARY_DATA = "pws";
246 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700247 private static final String DISCHARGE_STEP_DATA = "dsd";
248 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700249 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
250 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700251 private static final String FLASHLIGHT_DATA = "fla";
252 private static final String CAMERA_DATA = "cam";
253 private static final String VIDEO_DATA = "vid";
254 private static final String AUDIO_DATA = "aud";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255
Adam Lesinski010bf372016-04-11 12:18:18 -0700256 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
257
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700258 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 private final Formatter mFormatter = new Formatter(mFormatBuilder);
260
261 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700262 * State for keeping track of counting information.
263 */
264 public static abstract class Counter {
265
266 /**
267 * Returns the count associated with this Counter for the
268 * selected type of statistics.
269 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700270 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700271 */
Evan Millarc64edde2009-04-18 12:26:32 -0700272 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700273
274 /**
275 * Temporary for debugging.
276 */
277 public abstract void logState(Printer pw, String prefix);
278 }
279
280 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700281 * State for keeping track of long counting information.
282 */
283 public static abstract class LongCounter {
284
285 /**
286 * Returns the count associated with this Counter for the
287 * selected type of statistics.
288 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700289 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700290 */
291 public abstract long getCountLocked(int which);
292
293 /**
294 * Temporary for debugging.
295 */
296 public abstract void logState(Printer pw, String prefix);
297 }
298
299 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800300 * Container class that aggregates counters for transmit, receive, and idle state of a
301 * radio controller.
302 */
303 public static abstract class ControllerActivityCounter {
304 /**
305 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
306 * idle state.
307 */
308 public abstract LongCounter getIdleTimeCounter();
309
310 /**
311 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
312 * receive state.
313 */
314 public abstract LongCounter getRxTimeCounter();
315
316 /**
317 * An array of {@link LongCounter}, representing various transmit levels, where each level
318 * may draw a different amount of power. The levels themselves are controller-specific.
319 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
320 * various transmit level states.
321 */
322 public abstract LongCounter[] getTxTimeCounters();
323
324 /**
325 * @return a non-null {@link LongCounter} representing the power consumed by the controller
326 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
327 * yield a value of 0 if the device doesn't support power calculations.
328 */
329 public abstract LongCounter getPowerCounter();
330 }
331
332 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 * State for keeping track of timing information.
334 */
335 public static abstract class Timer {
336
337 /**
338 * Returns the count associated with this Timer for the
339 * selected type of statistics.
340 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700341 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 */
Evan Millarc64edde2009-04-18 12:26:32 -0700343 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344
345 /**
346 * Returns the total time in microseconds associated with this Timer for the
347 * selected type of statistics.
348 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800349 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700350 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 * @return a time in microseconds
352 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800353 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700356 * Returns the total time in microseconds associated with this Timer since the
357 * 'mark' was last set.
358 *
359 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
360 * @return a time in microseconds
361 */
362 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
363
364 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700365 * Returns the max duration if it is being tracked.
366 * Not all Timer subclasses track the max duration and the current duration.
367
368 */
369 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
370 return -1;
371 }
372
373 /**
374 * Returns the current time the timer has been active, if it is being tracked.
375 * Not all Timer subclasses track the max duration and the current duration.
376 */
377 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
378 return -1;
379 }
380
381 /**
382 * Returns whether the timer is currently running. Some types of timers
383 * (e.g. BatchTimers) don't know whether the event is currently active,
384 * and report false.
385 */
386 public boolean isRunningLocked() {
387 return false;
388 }
389
390 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 * Temporary for debugging.
392 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700393 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 }
395
396 /**
397 * The statistics associated with a particular uid.
398 */
399 public static abstract class Uid {
400
401 /**
402 * Returns a mapping containing wakelock statistics.
403 *
404 * @return a Map from Strings to Uid.Wakelock objects.
405 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700406 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407
408 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700409 * Returns a mapping containing sync statistics.
410 *
411 * @return a Map from Strings to Timer objects.
412 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700413 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700414
415 /**
416 * Returns a mapping containing scheduled job statistics.
417 *
418 * @return a Map from Strings to Timer objects.
419 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700420 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700421
422 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 * The statistics associated with a particular wake lock.
424 */
425 public static abstract class Wakelock {
426 public abstract Timer getWakeTime(int type);
427 }
428
429 /**
430 * Returns a mapping containing sensor statistics.
431 *
432 * @return a Map from Integer sensor ids to Uid.Sensor objects.
433 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700434 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435
436 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700437 * Returns a mapping containing active process data.
438 */
439 public abstract SparseArray<? extends Pid> getPidStats();
440
441 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 * Returns a mapping containing process statistics.
443 *
444 * @return a Map from Strings to Uid.Proc objects.
445 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700446 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447
448 /**
449 * Returns a mapping containing package statistics.
450 *
451 * @return a Map from Strings to Uid.Pkg objects.
452 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700453 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700454
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800455 public abstract ControllerActivityCounter getWifiControllerActivity();
456 public abstract ControllerActivityCounter getBluetoothControllerActivity();
457 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800458
459 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 * {@hide}
461 */
462 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700463
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800464 public abstract void noteWifiRunningLocked(long elapsedRealtime);
465 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
466 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
467 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
468 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
469 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
470 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
471 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
472 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
473 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800474 public abstract void noteActivityResumedLocked(long elapsedRealtime);
475 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800476 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
477 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
478 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700479 public abstract int getWifiScanCount(int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800480 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700481 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800482 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700483 public abstract Timer getAudioTurnedOnTimer();
484 public abstract Timer getVideoTurnedOnTimer();
485 public abstract Timer getFlashlightTurnedOnTimer();
486 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700487 public abstract Timer getForegroundActivityTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800488 public abstract Timer getBluetoothScanTimer();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700489
Dianne Hackborna0200e32016-03-30 18:01:41 -0700490 // Note: the following times are disjoint. They can be added together to find the
491 // total time a uid has had any processes running at all.
492
493 /**
494 * Time this uid has any processes in the top state (or above such as persistent).
495 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800496 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700497 /**
498 * Time this uid has any process with a started out bound foreground service, but
499 * none in the "top" state.
500 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800501 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700502 /**
503 * Time this uid has any process that is top while the device is sleeping, but none
504 * in the "foreground service" or better state.
505 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800506 public static final int PROCESS_STATE_TOP_SLEEPING = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700507 /**
508 * Time this uid has any process in an active foreground state, but none in the
509 * "top sleeping" or better state.
510 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800511 public static final int PROCESS_STATE_FOREGROUND = 3;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700512 /**
513 * Time this uid has any process in an active background state, but none in the
514 * "foreground" or better state.
515 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800516 public static final int PROCESS_STATE_BACKGROUND = 4;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700517 /**
518 * Time this uid has any processes that are sitting around cached, not in one of the
519 * other active states.
520 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800521 public static final int PROCESS_STATE_CACHED = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700522 /**
523 * Total number of process states we track.
524 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800525 public static final int NUM_PROCESS_STATE = 6;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700526
527 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackborna8d10942015-11-19 17:55:19 -0800528 "Top", "Fg Service", "Top Sleeping", "Foreground", "Background", "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700529 };
530
531 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800532 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700533
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800534 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535
Robert Greenwalta029ea12013-09-25 16:38:12 -0700536 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
537
Dianne Hackborn617f8772009-03-31 15:04:46 -0700538 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700539 * Note that these must match the constants in android.os.PowerManager.
540 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
541 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700542 */
543 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700544 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700545 };
546
Phil Weaverda80d672016-03-15 16:25:46 -0700547 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700548
Dianne Hackborn617f8772009-03-31 15:04:46 -0700549 public abstract void noteUserActivityLocked(int type);
550 public abstract boolean hasUserActivity();
551 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700552
553 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800554 public abstract long getNetworkActivityBytes(int type, int which);
555 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800556 public abstract long getMobileRadioActiveTime(int which);
557 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700558
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700559 /**
560 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
561 */
562 public abstract long getUserCpuTimeUs(int which);
563
564 /**
565 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
566 */
567 public abstract long getSystemCpuTimeUs(int which);
568
569 /**
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700570 * Get the total cpu power consumed (in milli-ampere-microseconds).
571 */
572 public abstract long getCpuPowerMaUs(int which);
573
574 /**
Adam Lesinski6832f392015-09-05 18:05:40 -0700575 * Returns the approximate cpu time (in milliseconds) spent at a certain CPU speed for a
576 * given CPU cluster.
577 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700578 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700579 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700580 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
581 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700582 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700583 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700584
Adam Lesinski5f056f62016-07-14 16:56:08 -0700585 /**
586 * Returns the number of times this UID woke up the Application Processor to
587 * process a mobile radio packet.
588 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
589 */
590 public abstract long getMobileRadioApWakeupCount(int which);
591
592 /**
593 * Returns the number of times this UID woke up the Application Processor to
594 * process a WiFi packet.
595 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
596 */
597 public abstract long getWifiRadioApWakeupCount(int which);
598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800600 /*
601 * FIXME: it's not correct to use this magic value because it
602 * could clash with a sensor handle (which are defined by
603 * the sensor HAL, and therefore out of our control
604 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 // Magic sensor number for the GPS.
606 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800611
612 /** Returns a counter for usage count when in the background. */
613 public abstract Counter getSensorBgCount();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 }
615
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700616 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800617 public int mWakeNesting;
618 public long mWakeSumMs;
619 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700620 }
621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 /**
623 * The statistics associated with a particular process.
624 */
625 public static abstract class Proc {
626
Dianne Hackborn287952c2010-09-22 22:34:31 -0700627 public static class ExcessivePower {
628 public static final int TYPE_WAKE = 1;
629 public static final int TYPE_CPU = 2;
630
631 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700632 public long overTime;
633 public long usedTime;
634 }
635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800637 * Returns true if this process is still active in the battery stats.
638 */
639 public abstract boolean isActive();
640
641 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700642 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700644 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 */
646 public abstract long getUserTime(int which);
647
648 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700649 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700651 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 */
653 public abstract long getSystemTime(int which);
654
655 /**
656 * Returns the number of times the process has been started.
657 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700658 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 */
660 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700661
662 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800663 * Returns the number of times the process has crashed.
664 *
665 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
666 */
667 public abstract int getNumCrashes(int which);
668
669 /**
670 * Returns the number of times the process has ANRed.
671 *
672 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
673 */
674 public abstract int getNumAnrs(int which);
675
676 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700677 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700678 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700679 * @return foreground cpu time in microseconds
680 */
681 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700682
Dianne Hackborn287952c2010-09-22 22:34:31 -0700683 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700684
Dianne Hackborn287952c2010-09-22 22:34:31 -0700685 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 }
687
688 /**
689 * The statistics associated with a particular package.
690 */
691 public static abstract class Pkg {
692
693 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700694 * Returns information about all wakeup alarms that have been triggered for this
695 * package. The mapping keys are tag names for the alarms, the counter contains
696 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700698 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699
700 /**
701 * Returns a mapping containing service statistics.
702 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700703 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704
705 /**
706 * The statistics associated with a particular service.
707 */
Joe Onoratoabded112016-02-08 16:49:39 -0800708 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709
710 /**
711 * Returns the amount of time spent started.
712 *
713 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700714 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 * @return
716 */
717 public abstract long getStartTime(long batteryUptime, int which);
718
719 /**
720 * Returns the total number of times startService() has been called.
721 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700722 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 */
724 public abstract int getStarts(int which);
725
726 /**
727 * Returns the total number times the service has been launched.
728 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700729 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 */
731 public abstract int getLaunches(int which);
732 }
733 }
734 }
735
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800736 public static final class LevelStepTracker {
737 public long mLastStepTime = -1;
738 public int mNumStepDurations;
739 public final long[] mStepDurations;
740
741 public LevelStepTracker(int maxLevelSteps) {
742 mStepDurations = new long[maxLevelSteps];
743 }
744
745 public LevelStepTracker(int numSteps, long[] steps) {
746 mNumStepDurations = numSteps;
747 mStepDurations = new long[numSteps];
748 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
749 }
750
751 public long getDurationAt(int index) {
752 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
753 }
754
755 public int getLevelAt(int index) {
756 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
757 >> STEP_LEVEL_LEVEL_SHIFT);
758 }
759
760 public int getInitModeAt(int index) {
761 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
762 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
763 }
764
765 public int getModModeAt(int index) {
766 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
767 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
768 }
769
770 private void appendHex(long val, int topOffset, StringBuilder out) {
771 boolean hasData = false;
772 while (topOffset >= 0) {
773 int digit = (int)( (val>>topOffset) & 0xf );
774 topOffset -= 4;
775 if (!hasData && digit == 0) {
776 continue;
777 }
778 hasData = true;
779 if (digit >= 0 && digit <= 9) {
780 out.append((char)('0' + digit));
781 } else {
782 out.append((char)('a' + digit - 10));
783 }
784 }
785 }
786
787 public void encodeEntryAt(int index, StringBuilder out) {
788 long item = mStepDurations[index];
789 long duration = item & STEP_LEVEL_TIME_MASK;
790 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
791 >> STEP_LEVEL_LEVEL_SHIFT);
792 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
793 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
794 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
795 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
796 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
797 case Display.STATE_OFF: out.append('f'); break;
798 case Display.STATE_ON: out.append('o'); break;
799 case Display.STATE_DOZE: out.append('d'); break;
800 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
801 }
802 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
803 out.append('p');
804 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700805 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
806 out.append('i');
807 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800808 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
809 case Display.STATE_OFF: out.append('F'); break;
810 case Display.STATE_ON: out.append('O'); break;
811 case Display.STATE_DOZE: out.append('D'); break;
812 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
813 }
814 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
815 out.append('P');
816 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700817 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
818 out.append('I');
819 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800820 out.append('-');
821 appendHex(level, 4, out);
822 out.append('-');
823 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
824 }
825
826 public void decodeEntryAt(int index, String value) {
827 final int N = value.length();
828 int i = 0;
829 char c;
830 long out = 0;
831 while (i < N && (c=value.charAt(i)) != '-') {
832 i++;
833 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800834 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800835 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800836 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800837 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800838 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800839 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800840 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
841 << STEP_LEVEL_INITIAL_MODE_SHIFT);
842 break;
843 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
844 << STEP_LEVEL_INITIAL_MODE_SHIFT);
845 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700846 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
847 << STEP_LEVEL_INITIAL_MODE_SHIFT);
848 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800849 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
850 break;
851 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
852 break;
853 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
854 break;
855 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
856 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
857 break;
858 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
859 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800860 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700861 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
862 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
863 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800864 }
865 }
866 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800867 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800868 while (i < N && (c=value.charAt(i)) != '-') {
869 i++;
870 level <<= 4;
871 if (c >= '0' && c <= '9') {
872 level += c - '0';
873 } else if (c >= 'a' && c <= 'f') {
874 level += c - 'a' + 10;
875 } else if (c >= 'A' && c <= 'F') {
876 level += c - 'A' + 10;
877 }
878 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800879 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800880 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
881 long duration = 0;
882 while (i < N && (c=value.charAt(i)) != '-') {
883 i++;
884 duration <<= 4;
885 if (c >= '0' && c <= '9') {
886 duration += c - '0';
887 } else if (c >= 'a' && c <= 'f') {
888 duration += c - 'a' + 10;
889 } else if (c >= 'A' && c <= 'F') {
890 duration += c - 'A' + 10;
891 }
892 }
893 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
894 }
895
896 public void init() {
897 mLastStepTime = -1;
898 mNumStepDurations = 0;
899 }
900
901 public void clearTime() {
902 mLastStepTime = -1;
903 }
904
905 public long computeTimePerLevel() {
906 final long[] steps = mStepDurations;
907 final int numSteps = mNumStepDurations;
908
909 // For now we'll do a simple average across all steps.
910 if (numSteps <= 0) {
911 return -1;
912 }
913 long total = 0;
914 for (int i=0; i<numSteps; i++) {
915 total += steps[i] & STEP_LEVEL_TIME_MASK;
916 }
917 return total / numSteps;
918 /*
919 long[] buckets = new long[numSteps];
920 int numBuckets = 0;
921 int numToAverage = 4;
922 int i = 0;
923 while (i < numSteps) {
924 long totalTime = 0;
925 int num = 0;
926 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
927 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
928 num++;
929 }
930 buckets[numBuckets] = totalTime / num;
931 numBuckets++;
932 numToAverage *= 2;
933 i += num;
934 }
935 if (numBuckets < 1) {
936 return -1;
937 }
938 long averageTime = buckets[numBuckets-1];
939 for (i=numBuckets-2; i>=0; i--) {
940 averageTime = (averageTime + buckets[i]) / 2;
941 }
942 return averageTime;
943 */
944 }
945
946 public long computeTimeEstimate(long modesOfInterest, long modeValues,
947 int[] outNumOfInterest) {
948 final long[] steps = mStepDurations;
949 final int count = mNumStepDurations;
950 if (count <= 0) {
951 return -1;
952 }
953 long total = 0;
954 int numOfInterest = 0;
955 for (int i=0; i<count; i++) {
956 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
957 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
958 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
959 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
960 // If the modes of interest didn't change during this step period...
961 if ((modMode&modesOfInterest) == 0) {
962 // And the mode values during this period match those we are measuring...
963 if ((initMode&modesOfInterest) == modeValues) {
964 // Then this can be used to estimate the total time!
965 numOfInterest++;
966 total += steps[i] & STEP_LEVEL_TIME_MASK;
967 }
968 }
969 }
970 if (numOfInterest <= 0) {
971 return -1;
972 }
973
974 if (outNumOfInterest != null) {
975 outNumOfInterest[0] = numOfInterest;
976 }
977
978 // The estimated time is the average time we spend in each level, multipled
979 // by 100 -- the total number of battery levels
980 return (total / numOfInterest) * 100;
981 }
982
983 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
984 int stepCount = mNumStepDurations;
985 final long lastStepTime = mLastStepTime;
986 if (lastStepTime >= 0 && numStepLevels > 0) {
987 final long[] steps = mStepDurations;
988 long duration = elapsedRealtime - lastStepTime;
989 for (int i=0; i<numStepLevels; i++) {
990 System.arraycopy(steps, 0, steps, 1, steps.length-1);
991 long thisDuration = duration / (numStepLevels-i);
992 duration -= thisDuration;
993 if (thisDuration > STEP_LEVEL_TIME_MASK) {
994 thisDuration = STEP_LEVEL_TIME_MASK;
995 }
996 steps[0] = thisDuration | modeBits;
997 }
998 stepCount += numStepLevels;
999 if (stepCount > steps.length) {
1000 stepCount = steps.length;
1001 }
1002 }
1003 mNumStepDurations = stepCount;
1004 mLastStepTime = elapsedRealtime;
1005 }
1006
1007 public void readFromParcel(Parcel in) {
1008 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001009 if (N > mStepDurations.length) {
1010 throw new ParcelFormatException("more step durations than available: " + N);
1011 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001012 mNumStepDurations = N;
1013 for (int i=0; i<N; i++) {
1014 mStepDurations[i] = in.readLong();
1015 }
1016 }
1017
1018 public void writeToParcel(Parcel out) {
1019 final int N = mNumStepDurations;
1020 out.writeInt(N);
1021 for (int i=0; i<N; i++) {
1022 out.writeLong(mStepDurations[i]);
1023 }
1024 }
1025 }
1026
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001027 public static final class PackageChange {
1028 public String mPackageName;
1029 public boolean mUpdate;
1030 public int mVersionCode;
1031 }
1032
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001033 public static final class DailyItem {
1034 public long mStartTime;
1035 public long mEndTime;
1036 public LevelStepTracker mDischargeSteps;
1037 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001038 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001039 }
1040
1041 public abstract DailyItem getDailyItemLocked(int daysAgo);
1042
1043 public abstract long getCurrentDailyStartTime();
1044
1045 public abstract long getNextMinDailyDeadline();
1046
1047 public abstract long getNextMaxDailyDeadline();
1048
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001049 public final static class HistoryTag {
1050 public String string;
1051 public int uid;
1052
1053 public int poolIdx;
1054
1055 public void setTo(HistoryTag o) {
1056 string = o.string;
1057 uid = o.uid;
1058 poolIdx = o.poolIdx;
1059 }
1060
1061 public void setTo(String _string, int _uid) {
1062 string = _string;
1063 uid = _uid;
1064 poolIdx = -1;
1065 }
1066
1067 public void writeToParcel(Parcel dest, int flags) {
1068 dest.writeString(string);
1069 dest.writeInt(uid);
1070 }
1071
1072 public void readFromParcel(Parcel src) {
1073 string = src.readString();
1074 uid = src.readInt();
1075 poolIdx = -1;
1076 }
1077
1078 @Override
1079 public boolean equals(Object o) {
1080 if (this == o) return true;
1081 if (o == null || getClass() != o.getClass()) return false;
1082
1083 HistoryTag that = (HistoryTag) o;
1084
1085 if (uid != that.uid) return false;
1086 if (!string.equals(that.string)) return false;
1087
1088 return true;
1089 }
1090
1091 @Override
1092 public int hashCode() {
1093 int result = string.hashCode();
1094 result = 31 * result + uid;
1095 return result;
1096 }
1097 }
1098
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001099 /**
1100 * Optional detailed information that can go into a history step. This is typically
1101 * generated each time the battery level changes.
1102 */
1103 public final static class HistoryStepDetails {
1104 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1105 public int userTime;
1106 public int systemTime;
1107
1108 // Top three apps using CPU in the last step, with times in 1/100 second.
1109 public int appCpuUid1;
1110 public int appCpuUTime1;
1111 public int appCpuSTime1;
1112 public int appCpuUid2;
1113 public int appCpuUTime2;
1114 public int appCpuSTime2;
1115 public int appCpuUid3;
1116 public int appCpuUTime3;
1117 public int appCpuSTime3;
1118
1119 // Information from /proc/stat
1120 public int statUserTime;
1121 public int statSystemTime;
1122 public int statIOWaitTime;
1123 public int statIrqTime;
1124 public int statSoftIrqTime;
1125 public int statIdlTime;
1126
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001127 // Platform-level low power state stats
1128 public String statPlatformIdleState;
1129
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001130 public HistoryStepDetails() {
1131 clear();
1132 }
1133
1134 public void clear() {
1135 userTime = systemTime = 0;
1136 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1137 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1138 = appCpuUTime3 = appCpuSTime3 = 0;
1139 }
1140
1141 public void writeToParcel(Parcel out) {
1142 out.writeInt(userTime);
1143 out.writeInt(systemTime);
1144 out.writeInt(appCpuUid1);
1145 out.writeInt(appCpuUTime1);
1146 out.writeInt(appCpuSTime1);
1147 out.writeInt(appCpuUid2);
1148 out.writeInt(appCpuUTime2);
1149 out.writeInt(appCpuSTime2);
1150 out.writeInt(appCpuUid3);
1151 out.writeInt(appCpuUTime3);
1152 out.writeInt(appCpuSTime3);
1153 out.writeInt(statUserTime);
1154 out.writeInt(statSystemTime);
1155 out.writeInt(statIOWaitTime);
1156 out.writeInt(statIrqTime);
1157 out.writeInt(statSoftIrqTime);
1158 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001159 out.writeString(statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001160 }
1161
1162 public void readFromParcel(Parcel in) {
1163 userTime = in.readInt();
1164 systemTime = in.readInt();
1165 appCpuUid1 = in.readInt();
1166 appCpuUTime1 = in.readInt();
1167 appCpuSTime1 = in.readInt();
1168 appCpuUid2 = in.readInt();
1169 appCpuUTime2 = in.readInt();
1170 appCpuSTime2 = in.readInt();
1171 appCpuUid3 = in.readInt();
1172 appCpuUTime3 = in.readInt();
1173 appCpuSTime3 = in.readInt();
1174 statUserTime = in.readInt();
1175 statSystemTime = in.readInt();
1176 statIOWaitTime = in.readInt();
1177 statIrqTime = in.readInt();
1178 statSoftIrqTime = in.readInt();
1179 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001180 statPlatformIdleState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001181 }
1182 }
1183
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001184 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001185 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001186
1187 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001188 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001189
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001190 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001191 public static final byte CMD_NULL = -1;
1192 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001193 public static final byte CMD_CURRENT_TIME = 5;
1194 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001195 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001196 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001197
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001198 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001199
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001200 /**
1201 * Return whether the command code is a delta data update.
1202 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001203 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001204 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001205 }
1206
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001207 public byte batteryLevel;
1208 public byte batteryStatus;
1209 public byte batteryHealth;
1210 public byte batteryPlugType;
1211
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001212 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001213 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001214
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001215 // The charge of the battery in micro-Ampere-hours.
1216 public int batteryChargeUAh;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001217
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001218 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001219 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001220 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001221 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001222 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1223 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001224 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001225 public static final int STATE_PHONE_STATE_SHIFT = 6;
1226 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001227 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001228 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001229 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001230
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001231 // These states always appear directly in the first int token
1232 // of a delta change; they should be ones that change relatively
1233 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001234 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1235 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001236 public static final int STATE_GPS_ON_FLAG = 1<<29;
1237 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001238 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001239 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001240 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001241 // Do not use, this is used for coulomb delta count.
1242 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001243 // These are on the lower bits used for the command; if they change
1244 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001245 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001246 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001247 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001248 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1249 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1250 // empty slot
1251 // empty slot
1252 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001253
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001254 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001255 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1256
1257 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001258
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001259 public int states;
1260
Dianne Hackborn3251b902014-06-20 14:40:53 -07001261 // Constants from WIFI_SUPPL_STATE_*
1262 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1263 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1264 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1265 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1266 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1267 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1268
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001269 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001270 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1271 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1272 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001273 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001274 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1275 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1276 public static final int STATE2_CHARGING_FLAG = 1<<24;
1277 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1278 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1279 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001280 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001281
1282 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001283 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001284 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1285
1286 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001287
Dianne Hackborn40c87252014-03-19 16:55:40 -07001288 public int states2;
1289
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001290 // The wake lock that was acquired at this point.
1291 public HistoryTag wakelockTag;
1292
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001293 // Kernel wakeup reason at this point.
1294 public HistoryTag wakeReasonTag;
1295
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001296 // Non-null when there is more detailed information at this step.
1297 public HistoryStepDetails stepDetails;
1298
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001299 public static final int EVENT_FLAG_START = 0x8000;
1300 public static final int EVENT_FLAG_FINISH = 0x4000;
1301
1302 // No event in this item.
1303 public static final int EVENT_NONE = 0x0000;
1304 // Event is about a process that is running.
1305 public static final int EVENT_PROC = 0x0001;
1306 // Event is about an application package that is in the foreground.
1307 public static final int EVENT_FOREGROUND = 0x0002;
1308 // Event is about an application package that is at the top of the screen.
1309 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001310 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001311 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001312 // Events for all additional wake locks aquired/release within a wake block.
1313 // These are not generated by default.
1314 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001315 // Event is about an application executing a scheduled job.
1316 public static final int EVENT_JOB = 0x0006;
1317 // Events for users running.
1318 public static final int EVENT_USER_RUNNING = 0x0007;
1319 // Events for foreground user.
1320 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001321 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001322 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001323 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001324 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001325 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001326 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001327 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001328 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001329 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001330 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001331 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001332 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001333 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001334 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001335 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001336 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001337 // Event for a package being on the temporary whitelist.
1338 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001339 // Event for the screen waking up.
1340 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001341 // Event for the UID that woke up the application processor.
1342 // Used for wakeups coming from WiFi, modem, etc.
1343 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001344 // Event for reporting that a specific partial wake lock has been held for a long duration.
1345 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Adam Lesinski041d9172016-12-12 12:03:56 -08001346 // Event reporting the new estimated (learned) capacity of the battery in mAh.
1347 public static final int EVENT_ESTIMATED_BATTERY_CAP = 0x0015;
Amith Yamasani67768492015-06-09 12:23:58 -07001348
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001349 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001350 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001351 // Mask to extract out only the type part of the event.
1352 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001353
1354 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1355 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1356 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1357 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1358 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1359 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001360 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1361 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001362 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1363 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001364 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1365 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1366 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1367 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1368 public static final int EVENT_USER_FOREGROUND_START =
1369 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1370 public static final int EVENT_USER_FOREGROUND_FINISH =
1371 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001372 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1373 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001374 public static final int EVENT_TEMP_WHITELIST_START =
1375 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1376 public static final int EVENT_TEMP_WHITELIST_FINISH =
1377 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001378 public static final int EVENT_LONG_WAKE_LOCK_START =
1379 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1380 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1381 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001382
1383 // For CMD_EVENT.
1384 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001385 public HistoryTag eventTag;
1386
Dianne Hackborn9a755432014-05-15 17:05:22 -07001387 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001388 public long currentTime;
1389
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001390 // Meta-data when reading.
1391 public int numReadInts;
1392
1393 // Pre-allocated objects.
1394 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001395 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001396 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001397
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001398 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001399 }
1400
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001401 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001402 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001403 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001404 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001405 }
1406
1407 public int describeContents() {
1408 return 0;
1409 }
1410
1411 public void writeToParcel(Parcel dest, int flags) {
1412 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001413 int bat = (((int)cmd)&0xff)
1414 | ((((int)batteryLevel)<<8)&0xff00)
1415 | ((((int)batteryStatus)<<16)&0xf0000)
1416 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001417 | ((((int)batteryPlugType)<<24)&0xf000000)
1418 | (wakelockTag != null ? 0x10000000 : 0)
1419 | (wakeReasonTag != null ? 0x20000000 : 0)
1420 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001421 dest.writeInt(bat);
1422 bat = (((int)batteryTemperature)&0xffff)
1423 | ((((int)batteryVoltage)<<16)&0xffff0000);
1424 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001425 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001426 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001427 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001428 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001429 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001430 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001431 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001432 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001433 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001434 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001435 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001436 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001437 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001438 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001439 dest.writeLong(currentTime);
1440 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001441 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001442
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001443 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001444 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001445 int bat = src.readInt();
1446 cmd = (byte)(bat&0xff);
1447 batteryLevel = (byte)((bat>>8)&0xff);
1448 batteryStatus = (byte)((bat>>16)&0xf);
1449 batteryHealth = (byte)((bat>>20)&0xf);
1450 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001451 int bat2 = src.readInt();
1452 batteryTemperature = (short)(bat2&0xffff);
1453 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001454 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001455 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001456 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001457 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001458 wakelockTag = localWakelockTag;
1459 wakelockTag.readFromParcel(src);
1460 } else {
1461 wakelockTag = null;
1462 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001463 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001464 wakeReasonTag = localWakeReasonTag;
1465 wakeReasonTag.readFromParcel(src);
1466 } else {
1467 wakeReasonTag = null;
1468 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001469 if ((bat&0x40000000) != 0) {
1470 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001471 eventTag = localEventTag;
1472 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001473 } else {
1474 eventCode = EVENT_NONE;
1475 eventTag = null;
1476 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001477 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001478 currentTime = src.readLong();
1479 } else {
1480 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001481 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001482 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001483 }
1484
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001485 public void clear() {
1486 time = 0;
1487 cmd = CMD_NULL;
1488 batteryLevel = 0;
1489 batteryStatus = 0;
1490 batteryHealth = 0;
1491 batteryPlugType = 0;
1492 batteryTemperature = 0;
1493 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001494 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001495 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001496 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001497 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001498 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001499 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001500 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001501 }
1502
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001503 public void setTo(HistoryItem o) {
1504 time = o.time;
1505 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001506 setToCommon(o);
1507 }
1508
1509 public void setTo(long time, byte cmd, HistoryItem o) {
1510 this.time = time;
1511 this.cmd = cmd;
1512 setToCommon(o);
1513 }
1514
1515 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001516 batteryLevel = o.batteryLevel;
1517 batteryStatus = o.batteryStatus;
1518 batteryHealth = o.batteryHealth;
1519 batteryPlugType = o.batteryPlugType;
1520 batteryTemperature = o.batteryTemperature;
1521 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001522 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001523 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001524 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001525 if (o.wakelockTag != null) {
1526 wakelockTag = localWakelockTag;
1527 wakelockTag.setTo(o.wakelockTag);
1528 } else {
1529 wakelockTag = null;
1530 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001531 if (o.wakeReasonTag != null) {
1532 wakeReasonTag = localWakeReasonTag;
1533 wakeReasonTag.setTo(o.wakeReasonTag);
1534 } else {
1535 wakeReasonTag = null;
1536 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001537 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001538 if (o.eventTag != null) {
1539 eventTag = localEventTag;
1540 eventTag.setTo(o.eventTag);
1541 } else {
1542 eventTag = null;
1543 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001544 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001545 }
1546
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001547 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001548 return batteryLevel == o.batteryLevel
1549 && batteryStatus == o.batteryStatus
1550 && batteryHealth == o.batteryHealth
1551 && batteryPlugType == o.batteryPlugType
1552 && batteryTemperature == o.batteryTemperature
1553 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001554 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001555 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001556 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001557 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001558 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001559
1560 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001561 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001562 return false;
1563 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001564 if (wakelockTag != o.wakelockTag) {
1565 if (wakelockTag == null || o.wakelockTag == null) {
1566 return false;
1567 }
1568 if (!wakelockTag.equals(o.wakelockTag)) {
1569 return false;
1570 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001571 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001572 if (wakeReasonTag != o.wakeReasonTag) {
1573 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1574 return false;
1575 }
1576 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1577 return false;
1578 }
1579 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001580 if (eventTag != o.eventTag) {
1581 if (eventTag == null || o.eventTag == null) {
1582 return false;
1583 }
1584 if (!eventTag.equals(o.eventTag)) {
1585 return false;
1586 }
1587 }
1588 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001589 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001590 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001591
1592 public final static class HistoryEventTracker {
1593 private final HashMap<String, SparseIntArray>[] mActiveEvents
1594 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1595
1596 public boolean updateState(int code, String name, int uid, int poolIdx) {
1597 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1598 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1599 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1600 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001601 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001602 mActiveEvents[idx] = active;
1603 }
1604 SparseIntArray uids = active.get(name);
1605 if (uids == null) {
1606 uids = new SparseIntArray();
1607 active.put(name, uids);
1608 }
1609 if (uids.indexOfKey(uid) >= 0) {
1610 // Already set, nothing to do!
1611 return false;
1612 }
1613 uids.put(uid, poolIdx);
1614 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1615 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1616 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1617 if (active == null) {
1618 // not currently active, nothing to do.
1619 return false;
1620 }
1621 SparseIntArray uids = active.get(name);
1622 if (uids == null) {
1623 // not currently active, nothing to do.
1624 return false;
1625 }
1626 idx = uids.indexOfKey(uid);
1627 if (idx < 0) {
1628 // not currently active, nothing to do.
1629 return false;
1630 }
1631 uids.removeAt(idx);
1632 if (uids.size() <= 0) {
1633 active.remove(name);
1634 }
1635 }
1636 return true;
1637 }
1638
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001639 public void removeEvents(int code) {
1640 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1641 mActiveEvents[idx] = null;
1642 }
1643
Dianne Hackborn37de0982014-05-09 09:32:18 -07001644 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1645 return mActiveEvents[code];
1646 }
1647 }
1648
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001649 public static final class BitDescription {
1650 public final int mask;
1651 public final int shift;
1652 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001653 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001654 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001655 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001656
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001657 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001658 this.mask = mask;
1659 this.shift = -1;
1660 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001661 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001662 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001663 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001664 }
1665
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001666 public BitDescription(int mask, int shift, String name, String shortName,
1667 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001668 this.mask = mask;
1669 this.shift = shift;
1670 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001671 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001672 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001673 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001674 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001675 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001676
Dianne Hackbornfc064132014-06-02 12:42:12 -07001677 /**
1678 * Don't allow any more batching in to the current history event. This
1679 * is called when printing partial histories, so to ensure that the next
1680 * history event will go in to a new batch after what was printed in the
1681 * last partial history.
1682 */
1683 public abstract void commitCurrentHistoryBatchLocked();
1684
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001685 public abstract int getHistoryTotalSize();
1686
1687 public abstract int getHistoryUsedSize();
1688
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001689 public abstract boolean startIteratingHistoryLocked();
1690
Dianne Hackborn099bc622014-01-22 13:39:16 -08001691 public abstract int getHistoryStringPoolSize();
1692
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001693 public abstract int getHistoryStringPoolBytes();
1694
1695 public abstract String getHistoryTagPoolString(int index);
1696
1697 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001698
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001699 public abstract boolean getNextHistoryLocked(HistoryItem out);
1700
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001701 public abstract void finishIteratingHistoryLocked();
1702
1703 public abstract boolean startIteratingOldHistoryLocked();
1704
1705 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1706
1707 public abstract void finishIteratingOldHistoryLocked();
1708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001710 * Return the base time offset for the battery history.
1711 */
1712 public abstract long getHistoryBaseTime();
1713
1714 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 * Returns the number of times the device has been started.
1716 */
1717 public abstract int getStartCount();
1718
1719 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001720 * 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 -08001721 * running on battery.
1722 *
1723 * {@hide}
1724 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001725 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001727 /**
1728 * Returns the number of times the screen was turned on.
1729 *
1730 * {@hide}
1731 */
1732 public abstract int getScreenOnCount(int which);
1733
Jeff Browne95c3cd2014-05-02 16:59:26 -07001734 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1735
Dianne Hackborn617f8772009-03-31 15:04:46 -07001736 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1737 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1738 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1739 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1740 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
1741
1742 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1743 "dark", "dim", "medium", "light", "bright"
1744 };
1745
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001746 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1747 "0", "1", "2", "3", "4"
1748 };
1749
Dianne Hackborn617f8772009-03-31 15:04:46 -07001750 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001751
Dianne Hackborn617f8772009-03-31 15:04:46 -07001752 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001753 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001754 * the given brightness
1755 *
1756 * {@hide}
1757 */
1758 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001759 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001762 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001763 * running on battery.
1764 *
1765 * {@hide}
1766 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001767 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001768
1769 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001770 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001771 *
1772 * {@hide}
1773 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001774 public abstract int getPowerSaveModeEnabledCount(int which);
1775
1776 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001777 * Constant for device idle mode: not active.
1778 */
1779 public static final int DEVICE_IDLE_MODE_OFF = 0;
1780
1781 /**
1782 * Constant for device idle mode: active in lightweight mode.
1783 */
1784 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1785
1786 /**
1787 * Constant for device idle mode: active in full mode.
1788 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07001789 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001790
1791 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001792 * Returns the time in microseconds that device has been in idle mode while
1793 * running on battery.
1794 *
1795 * {@hide}
1796 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001797 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001798
1799 /**
1800 * Returns the number of times that the devie has gone in to idle mode.
1801 *
1802 * {@hide}
1803 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001804 public abstract int getDeviceIdleModeCount(int mode, int which);
1805
1806 /**
1807 * Return the longest duration we spent in a particular device idle mode (fully in the
1808 * mode, not in idle maintenance etc).
1809 */
1810 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001811
1812 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001813 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001814 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001815 * counts all of the time that we consider the device to be idle, whether or not
1816 * it is currently in the actual device idle mode.
1817 *
1818 * {@hide}
1819 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001820 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001821
1822 /**
1823 * Returns the number of times that the devie has started idling.
1824 *
1825 * {@hide}
1826 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001827 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001828
1829 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001830 * Returns the number of times that connectivity state changed.
1831 *
1832 * {@hide}
1833 */
1834 public abstract int getNumConnectivityChange(int which);
1835
1836 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001837 * 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 -08001838 * running on battery.
1839 *
1840 * {@hide}
1841 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001842 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001843
1844 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001845 * Returns the number of times a phone call was activated.
1846 *
1847 * {@hide}
1848 */
1849 public abstract int getPhoneOnCount(int which);
1850
1851 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001852 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001853 * the given signal strength.
1854 *
1855 * {@hide}
1856 */
1857 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001858 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001859
Dianne Hackborn617f8772009-03-31 15:04:46 -07001860 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001861 * Returns the time in microseconds that the phone has been trying to
1862 * acquire a signal.
1863 *
1864 * {@hide}
1865 */
1866 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001867 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001868
1869 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001870 * Returns the number of times the phone has entered the given signal strength.
1871 *
1872 * {@hide}
1873 */
1874 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1875
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001876 /**
1877 * Returns the time in microseconds that the mobile network has been active
1878 * (in a high power state).
1879 *
1880 * {@hide}
1881 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001882 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001883
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001884 /**
1885 * Returns the number of times that the mobile network has transitioned to the
1886 * active state.
1887 *
1888 * {@hide}
1889 */
1890 public abstract int getMobileRadioActiveCount(int which);
1891
1892 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001893 * Returns the time in microseconds that is the difference between the mobile radio
1894 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1895 * from the radio.
1896 *
1897 * {@hide}
1898 */
1899 public abstract long getMobileRadioActiveAdjustedTime(int which);
1900
1901 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001902 * Returns the time in microseconds that the mobile network has been active
1903 * (in a high power state) but not being able to blame on an app.
1904 *
1905 * {@hide}
1906 */
1907 public abstract long getMobileRadioActiveUnknownTime(int which);
1908
1909 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001910 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001911 *
1912 * {@hide}
1913 */
1914 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001915
Dianne Hackborn627bba72009-03-24 22:32:56 -07001916 public static final int DATA_CONNECTION_NONE = 0;
1917 public static final int DATA_CONNECTION_GPRS = 1;
1918 public static final int DATA_CONNECTION_EDGE = 2;
1919 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001920 public static final int DATA_CONNECTION_CDMA = 4;
1921 public static final int DATA_CONNECTION_EVDO_0 = 5;
1922 public static final int DATA_CONNECTION_EVDO_A = 6;
1923 public static final int DATA_CONNECTION_1xRTT = 7;
1924 public static final int DATA_CONNECTION_HSDPA = 8;
1925 public static final int DATA_CONNECTION_HSUPA = 9;
1926 public static final int DATA_CONNECTION_HSPA = 10;
1927 public static final int DATA_CONNECTION_IDEN = 11;
1928 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001929 public static final int DATA_CONNECTION_LTE = 13;
1930 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001931 public static final int DATA_CONNECTION_HSPAP = 15;
1932 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001933
Dianne Hackborn627bba72009-03-24 22:32:56 -07001934 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001935 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001936 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001937 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001938 };
1939
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001940 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001941
1942 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001943 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001944 * the given data connection.
1945 *
1946 * {@hide}
1947 */
1948 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001949 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001952 * Returns the number of times the phone has entered the given data
1953 * connection type.
1954 *
1955 * {@hide}
1956 */
1957 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001958
Dianne Hackborn3251b902014-06-20 14:40:53 -07001959 public static final int WIFI_SUPPL_STATE_INVALID = 0;
1960 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
1961 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
1962 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
1963 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
1964 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
1965 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
1966 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
1967 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
1968 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
1969 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
1970 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
1971 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
1972
1973 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
1974
1975 static final String[] WIFI_SUPPL_STATE_NAMES = {
1976 "invalid", "disconn", "disabled", "inactive", "scanning",
1977 "authenticating", "associating", "associated", "4-way-handshake",
1978 "group-handshake", "completed", "dormant", "uninit"
1979 };
1980
1981 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
1982 "inv", "dsc", "dis", "inact", "scan",
1983 "auth", "ascing", "asced", "4-way",
1984 "group", "compl", "dorm", "uninit"
1985 };
1986
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001987 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
1988 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001989 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001990 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
1991 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001992 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001993 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
1994 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
1995 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001996 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001997 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001998 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001999 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002000 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2001 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002002 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2003 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2004 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2005 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2006 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2007 new String[] {"in", "out", "emergency", "off"},
2008 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002009 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2010 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2011 SignalStrength.SIGNAL_STRENGTH_NAMES,
2012 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002013 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2014 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2015 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002016 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002017
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002018 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2019 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002020 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002021 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002022 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002023 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002024 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002025 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2026 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2027 new String[] { "off", "light", "full", "???" },
2028 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002029 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2030 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2031 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002032 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2033 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2034 new String[] { "0", "1", "2", "3", "4" },
2035 new String[] { "0", "1", "2", "3", "4" }),
2036 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2037 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2038 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002039 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002040 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002041 };
2042
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002043 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002044 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002045 "active", "pkginst", "pkgunin", "alarm", "stats", "inactive", "active", "tmpwhitelist",
Adam Lesinski041d9172016-12-12 12:03:56 -08002046 "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002047 };
2048
2049 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002050 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002051 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002052 "Esw", "Ewa", "Elw", "Eec"
2053 };
2054
2055 @FunctionalInterface
2056 public interface IntToString {
2057 String applyAsString(int val);
2058 }
2059
2060 private static final IntToString sUidToString = UserHandle::formatUid;
2061 private static final IntToString sIntToString = Integer::toString;
2062
2063 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2064 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2065 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2066 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2067 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002068 };
2069
Dianne Hackborn617f8772009-03-31 15:04:46 -07002070 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002071 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002072 * running on battery.
2073 *
2074 * {@hide}
2075 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002076 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002077
2078 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002079 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002080 * been in the running state while the device was running on battery.
2081 *
2082 * {@hide}
2083 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002084 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002085
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002086 public static final int WIFI_STATE_OFF = 0;
2087 public static final int WIFI_STATE_OFF_SCANNING = 1;
2088 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2089 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2090 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2091 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2092 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2093 public static final int WIFI_STATE_SOFT_AP = 7;
2094
2095 static final String[] WIFI_STATE_NAMES = {
2096 "off", "scanning", "no_net", "disconn",
2097 "sta", "p2p", "sta_p2p", "soft_ap"
2098 };
2099
2100 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2101
2102 /**
2103 * Returns the time in microseconds that WiFi has been running in the given state.
2104 *
2105 * {@hide}
2106 */
2107 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002108 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002109
2110 /**
2111 * Returns the number of times that WiFi has entered the given state.
2112 *
2113 * {@hide}
2114 */
2115 public abstract int getWifiStateCount(int wifiState, int which);
2116
The Android Open Source Project10592532009-03-18 17:39:46 -07002117 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002118 * Returns the time in microseconds that the wifi supplicant has been
2119 * in a given state.
2120 *
2121 * {@hide}
2122 */
2123 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2124
2125 /**
2126 * Returns the number of times that the wifi supplicant has transitioned
2127 * to a given state.
2128 *
2129 * {@hide}
2130 */
2131 public abstract int getWifiSupplStateCount(int state, int which);
2132
2133 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2134
2135 /**
2136 * Returns the time in microseconds that WIFI has been running with
2137 * the given signal strength.
2138 *
2139 * {@hide}
2140 */
2141 public abstract long getWifiSignalStrengthTime(int strengthBin,
2142 long elapsedRealtimeUs, int which);
2143
2144 /**
2145 * Returns the number of times WIFI has entered the given signal strength.
2146 *
2147 * {@hide}
2148 */
2149 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2150
2151 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002152 * Returns the time in microseconds that the flashlight has been on while the device was
2153 * running on battery.
2154 *
2155 * {@hide}
2156 */
2157 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2158
2159 /**
2160 * Returns the number of times that the flashlight has been turned on while the device was
2161 * running on battery.
2162 *
2163 * {@hide}
2164 */
2165 public abstract long getFlashlightOnCount(int which);
2166
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002167 /**
2168 * Returns the time in microseconds that the camera has been on while the device was
2169 * running on battery.
2170 *
2171 * {@hide}
2172 */
2173 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2174
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002175 /**
2176 * Returns the time in microseconds that bluetooth scans were running while the device was
2177 * on battery.
2178 *
2179 * {@hide}
2180 */
2181 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002182
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002183 public static final int NETWORK_MOBILE_RX_DATA = 0;
2184 public static final int NETWORK_MOBILE_TX_DATA = 1;
2185 public static final int NETWORK_WIFI_RX_DATA = 2;
2186 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002187 public static final int NETWORK_BT_RX_DATA = 4;
2188 public static final int NETWORK_BT_TX_DATA = 5;
2189 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_BT_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002190
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002191 public abstract long getNetworkActivityBytes(int type, int which);
2192 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002193
Adam Lesinskie08af192015-03-25 16:42:59 -07002194 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002195 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002196 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002197 * actual power data.
2198 */
2199 public abstract boolean hasWifiActivityReporting();
2200
2201 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002202 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2203 * in various radio controller states, such as transmit, receive, and idle.
2204 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002205 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002206 public abstract ControllerActivityCounter getWifiControllerActivity();
2207
2208 /**
2209 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2210 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2211 * actual power data.
2212 */
2213 public abstract boolean hasBluetoothActivityReporting();
2214
2215 /**
2216 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2217 * in various radio controller states, such as transmit, receive, and idle.
2218 * @return non-null {@link ControllerActivityCounter}
2219 */
2220 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2221
2222 /**
2223 * Returns true if the BatteryStats object has detailed modem power reports.
2224 * When true, calling {@link #getModemControllerActivity()} will yield the
2225 * actual power data.
2226 */
2227 public abstract boolean hasModemActivityReporting();
2228
2229 /**
2230 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2231 * in various radio controller states, such as transmit, receive, and idle.
2232 * @return non-null {@link ControllerActivityCounter}
2233 */
2234 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002235
The Android Open Source Project10592532009-03-18 17:39:46 -07002236 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002237 * Return the wall clock time when battery stats data collection started.
2238 */
2239 public abstract long getStartClockTime();
2240
2241 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002242 * Return platform version tag that we were running in when the battery stats started.
2243 */
2244 public abstract String getStartPlatformVersion();
2245
2246 /**
2247 * Return platform version tag that we were running in when the battery stats ended.
2248 */
2249 public abstract String getEndPlatformVersion();
2250
2251 /**
2252 * Return the internal version code of the parcelled format.
2253 */
2254 public abstract int getParcelVersion();
2255
2256 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 * Return whether we are currently running on battery.
2258 */
2259 public abstract boolean getIsOnBattery();
2260
2261 /**
2262 * Returns a SparseArray containing the statistics for each uid.
2263 */
2264 public abstract SparseArray<? extends Uid> getUidStats();
2265
2266 /**
2267 * Returns the current battery uptime in microseconds.
2268 *
2269 * @param curTime the amount of elapsed realtime in microseconds.
2270 */
2271 public abstract long getBatteryUptime(long curTime);
2272
2273 /**
2274 * Returns the current battery realtime in microseconds.
2275 *
2276 * @param curTime the amount of elapsed realtime in microseconds.
2277 */
2278 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07002279
2280 /**
Evan Millar633a1742009-04-02 16:36:33 -07002281 * Returns the battery percentage level at the last time the device was unplugged from power, or
2282 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002283 */
Evan Millar633a1742009-04-02 16:36:33 -07002284 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07002285
2286 /**
Evan Millar633a1742009-04-02 16:36:33 -07002287 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2288 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002289 */
Evan Millar633a1742009-04-02 16:36:33 -07002290 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291
2292 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002293 * Get the amount the battery has discharged since the stats were
2294 * last reset after charging, as a lower-end approximation.
2295 */
2296 public abstract int getLowDischargeAmountSinceCharge();
2297
2298 /**
2299 * Get the amount the battery has discharged since the stats were
2300 * last reset after charging, as an upper-end approximation.
2301 */
2302 public abstract int getHighDischargeAmountSinceCharge();
2303
2304 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002305 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2306 */
2307 public abstract int getDischargeAmount(int which);
2308
2309 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002310 * Get the amount the battery has discharged while the screen was on,
2311 * since the last time power was unplugged.
2312 */
2313 public abstract int getDischargeAmountScreenOn();
2314
2315 /**
2316 * Get the amount the battery has discharged while the screen was on,
2317 * since the last time the device was charged.
2318 */
2319 public abstract int getDischargeAmountScreenOnSinceCharge();
2320
2321 /**
2322 * Get the amount the battery has discharged while the screen was off,
2323 * since the last time power was unplugged.
2324 */
2325 public abstract int getDischargeAmountScreenOff();
2326
2327 /**
2328 * Get the amount the battery has discharged while the screen was off,
2329 * since the last time the device was charged.
2330 */
2331 public abstract int getDischargeAmountScreenOffSinceCharge();
2332
2333 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 * Returns the total, last, or current battery uptime in microseconds.
2335 *
2336 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002337 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 */
2339 public abstract long computeBatteryUptime(long curTime, int which);
2340
2341 /**
2342 * Returns the total, last, or current battery realtime in microseconds.
2343 *
2344 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002345 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 */
2347 public abstract long computeBatteryRealtime(long curTime, int which);
2348
2349 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002350 * Returns the total, last, or current battery screen off uptime in microseconds.
2351 *
2352 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002353 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002354 */
2355 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2356
2357 /**
2358 * Returns the total, last, or current battery screen off realtime in microseconds.
2359 *
2360 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002361 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002362 */
2363 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2364
2365 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 * Returns the total, last, or current uptime in microseconds.
2367 *
2368 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002369 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 */
2371 public abstract long computeUptime(long curTime, int which);
2372
2373 /**
2374 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002375 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002377 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 */
2379 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002380
2381 /**
2382 * Compute an approximation for how much run time (in microseconds) is remaining on
2383 * the battery. Returns -1 if no time can be computed: either there is not
2384 * enough current data to make a decision, or the battery is currently
2385 * charging.
2386 *
2387 * @param curTime The current elepsed realtime in microseconds.
2388 */
2389 public abstract long computeBatteryTimeRemaining(long curTime);
2390
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002391 // The part of a step duration that is the actual time.
2392 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2393
2394 // Bits in a step duration that are the new battery level we are at.
2395 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002396 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002397
2398 // Bits in a step duration that are the initial mode we were in at that step.
2399 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002400 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002401
2402 // Bits in a step duration that indicate which modes changed during that step.
2403 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002404 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002405
2406 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2407 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2408
Santos Cordone94f0502017-02-24 12:31:20 -08002409 // The largest value for screen state that is tracked in battery states. Any values above
2410 // this should be mapped back to one of the tracked values before being tracked here.
2411 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2412
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002413 // Step duration mode: power save is on.
2414 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2415
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002416 // Step duration mode: device is currently in idle mode.
2417 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2418
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002419 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2420 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002421 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2422 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002423 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2424 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2425 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2426 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2427 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002428 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2429 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002430 };
2431 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2432 (Display.STATE_OFF-1),
2433 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002434 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002435 (Display.STATE_ON-1),
2436 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2437 (Display.STATE_DOZE-1),
2438 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2439 (Display.STATE_DOZE_SUSPEND-1),
2440 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002441 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002442 };
2443 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2444 "screen off",
2445 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002446 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002447 "screen on",
2448 "screen on power save",
2449 "screen doze",
2450 "screen doze power save",
2451 "screen doze-suspend",
2452 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002453 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002454 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002455
2456 /**
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002457 * Return the counter keeping track of the amount of battery discharge while the screen was off,
2458 * measured in micro-Ampere-hours. This will be non-zero only if the device's battery has
2459 * a coulomb counter.
2460 */
2461 public abstract LongCounter getDischargeScreenOffCoulombCounter();
2462
2463 /**
2464 * Return the counter keeping track of the amount of battery discharge measured in
2465 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2466 * a coulomb counter.
2467 */
2468 public abstract LongCounter getDischargeCoulombCounter();
2469
2470 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002471 * Returns the estimated real battery capacity, which may be less than the capacity
2472 * declared by the PowerProfile.
2473 * @return The estimated battery capacity in mAh.
2474 */
2475 public abstract int getEstimatedBatteryCapacity();
2476
2477 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002478 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002479 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002480 public abstract LevelStepTracker getDischargeLevelStepTracker();
2481
2482 /**
2483 * Return the array of daily discharge step durations.
2484 */
2485 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002486
2487 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002488 * Compute an approximation for how much time (in microseconds) remains until the battery
2489 * is fully charged. Returns -1 if no time can be computed: either there is not
2490 * enough current data to make a decision, or the battery is currently
2491 * discharging.
2492 *
2493 * @param curTime The current elepsed realtime in microseconds.
2494 */
2495 public abstract long computeChargeTimeRemaining(long curTime);
2496
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002497 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002498 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002499 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002500 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002501
2502 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002503 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002504 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002505 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002506
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002507 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2508
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002509 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002510
Evan Millarc64edde2009-04-18 12:26:32 -07002511 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512
James Carr2dd7e5e2016-07-20 18:48:39 -07002513 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2514
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002515 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2516
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002517 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 long days = seconds / (60 * 60 * 24);
2519 if (days != 0) {
2520 out.append(days);
2521 out.append("d ");
2522 }
2523 long used = days * 60 * 60 * 24;
2524
2525 long hours = (seconds - used) / (60 * 60);
2526 if (hours != 0 || used != 0) {
2527 out.append(hours);
2528 out.append("h ");
2529 }
2530 used += hours * 60 * 60;
2531
2532 long mins = (seconds-used) / 60;
2533 if (mins != 0 || used != 0) {
2534 out.append(mins);
2535 out.append("m ");
2536 }
2537 used += mins * 60;
2538
2539 if (seconds != 0 || used != 0) {
2540 out.append(seconds-used);
2541 out.append("s ");
2542 }
2543 }
2544
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002545 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002547 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 sb.append(time - (sec * 1000));
2549 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 }
2551
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002552 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002553 long sec = time / 1000;
2554 formatTimeRaw(sb, sec);
2555 sb.append(time - (sec * 1000));
2556 sb.append("ms");
2557 }
2558
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002559 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002561 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 }
2563 float perc = ((float)num) / ((float)den) * 100;
2564 mFormatBuilder.setLength(0);
2565 mFormatter.format("%.1f%%", perc);
2566 return mFormatBuilder.toString();
2567 }
2568
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002569 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002570 mFormatBuilder.setLength(0);
2571
2572 if (bytes < BYTES_PER_KB) {
2573 return bytes + "B";
2574 } else if (bytes < BYTES_PER_MB) {
2575 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2576 return mFormatBuilder.toString();
2577 } else if (bytes < BYTES_PER_GB){
2578 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2579 return mFormatBuilder.toString();
2580 } else {
2581 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2582 return mFormatBuilder.toString();
2583 }
2584 }
2585
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002586 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002587 if (timer != null) {
2588 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002589 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002590 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2591 return totalTimeMillis;
2592 }
2593 return 0;
2594 }
2595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 /**
2597 *
2598 * @param sb a StringBuilder object.
2599 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002600 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002602 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 * @param linePrefix a String to be prepended to each line of output.
2604 * @return the line prefix
2605 */
2606 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002607 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002610 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002611
Evan Millarc64edde2009-04-18 12:26:32 -07002612 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 if (totalTimeMillis != 0) {
2614 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002615 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002616 if (name != null) {
2617 sb.append(name);
2618 sb.append(' ');
2619 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 sb.append('(');
2621 sb.append(count);
2622 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002623 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2624 if (maxDurationMs >= 0) {
2625 sb.append(" max=");
2626 sb.append(maxDurationMs);
2627 }
2628 if (timer.isRunningLocked()) {
2629 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2630 if (currentMs >= 0) {
2631 sb.append(" (running for ");
2632 sb.append(currentMs);
2633 sb.append("ms)");
2634 } else {
2635 sb.append(" (running)");
2636 }
2637 }
2638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639 return ", ";
2640 }
2641 }
2642 return linePrefix;
2643 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002644
2645 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07002646 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002647 *
2648 * @param pw a PrintWriter object to print to.
2649 * @param sb a StringBuilder object.
2650 * @param timer a Timer object contining the wakelock times.
2651 * @param rawRealtime the current on-battery time in microseconds.
2652 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2653 * @param prefix a String to be prepended to each line of output.
2654 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002655 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002656 */
2657 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07002658 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002659 if (timer != null) {
2660 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07002661 final long totalTimeMs = (timer.getTotalTimeLocked(
2662 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002663 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002664 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002665 sb.setLength(0);
2666 sb.append(prefix);
2667 sb.append(" ");
2668 sb.append(type);
2669 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002670 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002671 sb.append("realtime (");
2672 sb.append(count);
2673 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002674 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
2675 if (maxDurationMs >= 0) {
2676 sb.append(" max=");
2677 sb.append(maxDurationMs);
2678 }
2679 if (timer.isRunningLocked()) {
2680 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
2681 if (currentMs >= 0) {
2682 sb.append(" (running for ");
2683 sb.append(currentMs);
2684 sb.append("ms)");
2685 } else {
2686 sb.append(" (running)");
2687 }
2688 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002689 pw.println(sb.toString());
2690 return true;
2691 }
2692 }
2693 return false;
2694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695
2696 /**
2697 * Checkin version of wakelock printer. Prints simple comma-separated list.
2698 *
2699 * @param sb a StringBuilder object.
2700 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002701 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002703 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 * @param linePrefix a String to be prepended to each line of output.
2705 * @return the line prefix
2706 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002707 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2708 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 long totalTimeMicros = 0;
2710 int count = 0;
Joe Onorato92fd23f2016-07-25 11:18:42 -07002711 long max = -1;
2712 long current = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002714 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07002715 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002716 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2717 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 }
2719 sb.append(linePrefix);
2720 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2721 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002722 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002724 sb.append(',');
2725 sb.append(current);
2726 sb.append(',');
2727 sb.append(max);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 return ",";
2729 }
2730
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002731 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
2732 String type) {
2733 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2734 pw.print(',');
2735 pw.print(uid);
2736 pw.print(',');
2737 pw.print(category);
2738 pw.print(',');
2739 pw.print(type);
2740 }
2741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 /**
2743 * Dump a comma-separated line of values for terse checkin mode.
2744 *
2745 * @param pw the PageWriter to dump log to
2746 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2747 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2748 * @param args type-dependent data arguments
2749 */
2750 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
2751 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002752 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002753 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002754 pw.print(',');
2755 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002757 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002759
2760 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002761 * Dump a given timer stat for terse checkin mode.
2762 *
2763 * @param pw the PageWriter to dump log to
2764 * @param uid the UID to log
2765 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2766 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2767 * @param timer a {@link Timer} to dump stats for
2768 * @param rawRealtime the current elapsed realtime of the system in microseconds
2769 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2770 */
2771 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2772 Timer timer, long rawRealtime, int which) {
2773 if (timer != null) {
2774 // Convert from microseconds to milliseconds with rounding
2775 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2776 / 1000;
2777 final int count = timer.getCountLocked(which);
2778 if (totalTime != 0) {
2779 dumpLine(pw, uid, category, type, totalTime, count);
2780 }
2781 }
2782 }
2783
2784 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002785 * Checks if the ControllerActivityCounter has any data worth dumping.
2786 */
2787 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
2788 if (counter == null) {
2789 return false;
2790 }
2791
2792 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
2793 || counter.getRxTimeCounter().getCountLocked(which) != 0
2794 || counter.getPowerCounter().getCountLocked(which) != 0) {
2795 return true;
2796 }
2797
2798 for (LongCounter c : counter.getTxTimeCounters()) {
2799 if (c.getCountLocked(which) != 0) {
2800 return true;
2801 }
2802 }
2803 return false;
2804 }
2805
2806 /**
2807 * Dumps the ControllerActivityCounter if it has any data worth dumping.
2808 * The order of the arguments in the final check in line is:
2809 *
2810 * idle, rx, power, tx...
2811 *
2812 * where tx... is one or more transmit level times.
2813 */
2814 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
2815 String type,
2816 ControllerActivityCounter counter,
2817 int which) {
2818 if (!controllerActivityHasData(counter, which)) {
2819 return;
2820 }
2821
2822 dumpLineHeader(pw, uid, category, type);
2823 pw.print(",");
2824 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
2825 pw.print(",");
2826 pw.print(counter.getRxTimeCounter().getCountLocked(which));
2827 pw.print(",");
2828 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
2829 for (LongCounter c : counter.getTxTimeCounters()) {
2830 pw.print(",");
2831 pw.print(c.getCountLocked(which));
2832 }
2833 pw.println();
2834 }
2835
2836 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
2837 String prefix, String controllerName,
2838 ControllerActivityCounter counter,
2839 int which) {
2840 if (controllerActivityHasData(counter, which)) {
2841 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
2842 }
2843 }
2844
2845 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
2846 String controllerName,
2847 ControllerActivityCounter counter, int which) {
2848 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
2849 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
2850 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
2851 long totalTxTimeMs = 0;
2852 for (LongCounter txState : counter.getTxTimeCounters()) {
2853 totalTxTimeMs += txState.getCountLocked(which);
2854 }
2855
2856 final long totalTimeMs = idleTimeMs + rxTimeMs + totalTxTimeMs;
2857
2858 sb.setLength(0);
2859 sb.append(prefix);
2860 sb.append(" ");
2861 sb.append(controllerName);
2862 sb.append(" Idle time: ");
2863 formatTimeMs(sb, idleTimeMs);
2864 sb.append("(");
2865 sb.append(formatRatioLocked(idleTimeMs, totalTimeMs));
2866 sb.append(")");
2867 pw.println(sb.toString());
2868
2869 sb.setLength(0);
2870 sb.append(prefix);
2871 sb.append(" ");
2872 sb.append(controllerName);
2873 sb.append(" Rx time: ");
2874 formatTimeMs(sb, rxTimeMs);
2875 sb.append("(");
2876 sb.append(formatRatioLocked(rxTimeMs, totalTimeMs));
2877 sb.append(")");
2878 pw.println(sb.toString());
2879
2880 sb.setLength(0);
2881 sb.append(prefix);
2882 sb.append(" ");
2883 sb.append(controllerName);
2884 sb.append(" Tx time: ");
2885 formatTimeMs(sb, totalTxTimeMs);
2886 sb.append("(");
2887 sb.append(formatRatioLocked(totalTxTimeMs, totalTimeMs));
2888 sb.append(")");
2889 pw.println(sb.toString());
2890
2891 final int numTxLvls = counter.getTxTimeCounters().length;
2892 if (numTxLvls > 1) {
2893 for (int lvl = 0; lvl < numTxLvls; lvl++) {
2894 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
2895 sb.setLength(0);
2896 sb.append(prefix);
2897 sb.append(" [");
2898 sb.append(lvl);
2899 sb.append("] ");
2900 formatTimeMs(sb, txLvlTimeMs);
2901 sb.append("(");
2902 sb.append(formatRatioLocked(txLvlTimeMs, totalTxTimeMs));
2903 sb.append(")");
2904 pw.println(sb.toString());
2905 }
2906 }
2907
2908 sb.setLength(0);
2909 sb.append(prefix);
2910 sb.append(" ");
2911 sb.append(controllerName);
2912 sb.append(" Power drain: ").append(
2913 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
2914 sb.append("mAh");
2915 pw.println(sb.toString());
2916 }
2917
2918 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07002919 * Temporary for settings.
2920 */
2921 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
2922 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2923 }
2924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 /**
2926 * Checkin server version of dump to produce more compact, computer-readable log.
2927 *
2928 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07002930 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
2931 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2933 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2934 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2936 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002937 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2938 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2939 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 final long totalRealtime = computeRealtime(rawRealtime, which);
2941 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002942 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002943 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002944 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002945 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
2946 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002947 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002948 rawRealtime, which);
2949 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
2950 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002951 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002952 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002953 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002954 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002955 final long dischargeCount = getDischargeCoulombCounter().getCountLocked(which);
2956 final long dischargeScreenOffCount = getDischargeScreenOffCoulombCounter()
2957 .getCountLocked(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002958
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002959 final StringBuilder sb = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002961 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07002962 final int NU = uidStats.size();
2963
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002964 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 // Dump "battery" stat
2967 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002968 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07002969 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002970 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002971 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002972 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
2973 getEstimatedBatteryCapacity());
Adam Lesinski67c134f2016-06-10 15:15:08 -07002974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002976 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07002977 long fullWakeLockTimeTotal = 0;
2978 long partialWakeLockTimeTotal = 0;
2979
2980 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002981 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002982
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002983 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
2984 = u.getWakelockStats();
2985 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
2986 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07002987
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002988 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
2989 if (fullWakeTimer != null) {
2990 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
2991 which);
2992 }
2993
2994 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
2995 if (partialWakeTimer != null) {
2996 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
2997 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07002998 }
2999 }
3000 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003001
3002 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003003 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3004 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3005 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3006 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3007 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3008 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3009 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3010 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003011 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3012 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003013 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3014 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003015 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3016 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003017
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003018 // Dump Modem controller stats
3019 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3020 getModemControllerActivity(), which);
3021
Adam Lesinskie283d332015-04-16 12:29:25 -07003022 // Dump Wifi controller stats
3023 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3024 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003025 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003026 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003027
3028 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3029 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003030
3031 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003032 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3033 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 // Dump misc stats
3036 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003037 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003038 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003039 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003040 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003041 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003042 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3043 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003044 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003045 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3046 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3047 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3048 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003049 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003050
3051 // Dump screen brightness stats
3052 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3053 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003054 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003055 }
3056 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07003057
Dianne Hackborn627bba72009-03-24 22:32:56 -07003058 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003059 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3060 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003061 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003062 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003063 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003064 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003065 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003066 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003067 args[i] = getPhoneSignalStrengthCount(i, which);
3068 }
3069 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003070
Dianne Hackborn627bba72009-03-24 22:32:56 -07003071 // Dump network type stats
3072 args = new Object[NUM_DATA_CONNECTION_TYPES];
3073 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003074 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003075 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003076 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3077 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3078 args[i] = getPhoneDataConnectionCount(i, which);
3079 }
3080 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003081
3082 // Dump wifi state stats
3083 args = new Object[NUM_WIFI_STATES];
3084 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003085 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003086 }
3087 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3088 for (int i=0; i<NUM_WIFI_STATES; i++) {
3089 args[i] = getWifiStateCount(i, which);
3090 }
3091 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3092
Dianne Hackborn3251b902014-06-20 14:40:53 -07003093 // Dump wifi suppl state stats
3094 args = new Object[NUM_WIFI_SUPPL_STATES];
3095 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3096 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3097 }
3098 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3099 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3100 args[i] = getWifiSupplStateCount(i, which);
3101 }
3102 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3103
3104 // Dump wifi signal strength stats
3105 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3106 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3107 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3108 }
3109 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3110 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3111 args[i] = getWifiSignalStrengthCount(i, which);
3112 }
3113 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3114
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003115 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003116 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003117 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003120 if (which == STATS_SINCE_UNPLUGGED) {
3121 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3122 getDischargeStartLevel()-getDischargeCurrentLevel(),
3123 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003124 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
3125 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003126 } else {
3127 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3128 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003129 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003130 getDischargeAmountScreenOffSinceCharge(),
3131 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003132 }
3133
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003134 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003135 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003136 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003137 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003138 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003139 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003140 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003141 sb.toString());
3142 }
Evan Millarc64edde2009-04-18 12:26:32 -07003143 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003144 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003145 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003146 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3147 // Not doing the regular wake lock formatting to remain compatible
3148 // with the old checkin format.
3149 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3150 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003151 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003152 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003153 }
3154 }
Evan Millarc64edde2009-04-18 12:26:32 -07003155 }
3156
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003157 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003158 helper.create(this);
3159 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003160 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003161 if (sippers != null && sippers.size() > 0) {
3162 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3163 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003164 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003165 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3166 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
3167 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003168 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003169 int uid = 0;
3170 String label;
3171 switch (bs.drainType) {
3172 case IDLE:
3173 label="idle";
3174 break;
3175 case CELL:
3176 label="cell";
3177 break;
3178 case PHONE:
3179 label="phone";
3180 break;
3181 case WIFI:
3182 label="wifi";
3183 break;
3184 case BLUETOOTH:
3185 label="blue";
3186 break;
3187 case SCREEN:
3188 label="scrn";
3189 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003190 case FLASHLIGHT:
3191 label="flashlight";
3192 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003193 case APP:
3194 uid = bs.uidObj.getUid();
3195 label = "uid";
3196 break;
3197 case USER:
3198 uid = UserHandle.getUid(bs.userId, 0);
3199 label = "user";
3200 break;
3201 case UNACCOUNTED:
3202 label = "unacc";
3203 break;
3204 case OVERCOUNTED:
3205 label = "over";
3206 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003207 case CAMERA:
3208 label = "camera";
3209 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003210 default:
3211 label = "???";
3212 }
3213 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Adam Lesinskie08af192015-03-25 16:42:59 -07003214 BatteryStatsHelper.makemAh(bs.totalPowerMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003215 }
3216 }
3217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 for (int iu = 0; iu < NU; iu++) {
3219 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003220 if (reqUid >= 0 && uid != reqUid) {
3221 continue;
3222 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003223 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003226 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3227 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3228 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3229 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3230 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3231 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3232 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3233 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003234 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003235 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3236 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003237 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003238 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3239 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003240 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3241 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003242 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Adam Lesinski5f056f62016-07-14 16:56:08 -07003243 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003244 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3245 wifiBytesRx, wifiBytesTx,
3246 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003247 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003248 mobileActiveTime, mobileActiveCount,
Adam Lesinski5f056f62016-07-14 16:56:08 -07003249 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003250 }
3251
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003252 // Dump modem controller data, per UID.
3253 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3254 u.getModemControllerActivity(), which);
3255
3256 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003257 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3258 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3259 final int wifiScanCount = u.getWifiScanCount(which);
3260 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003261 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003262 || uidWifiRunningTime != 0) {
3263 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3264 uidWifiRunningTime, wifiScanCount,
Kweku Adams5b2747e2016-04-25 10:44:05 -07003265 /* legacy fields follow, keep at 0 */ 0, 0, 0);
The Android Open Source Project10592532009-03-18 17:39:46 -07003266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003267
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003268 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3269 u.getWifiControllerActivity(), which);
3270
Adam Lesinskiba9d8932016-04-12 12:06:03 -07003271 dumpTimer(pw, uid, category, BLUETOOTH_MISC_DATA, u.getBluetoothScanTimer(),
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003272 rawRealtime, which);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003273
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003274 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3275 u.getBluetoothControllerActivity(), which);
3276
Dianne Hackborn617f8772009-03-31 15:04:46 -07003277 if (u.hasUserActivity()) {
3278 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3279 boolean hasData = false;
3280 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3281 int val = u.getUserActivityCount(i, which);
3282 args[i] = val;
3283 if (val != 0) hasData = true;
3284 }
3285 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003286 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003287 }
3288 }
3289
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003290 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3291 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3292 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3293 String linePrefix = "";
3294 sb.setLength(0);
3295 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3296 rawRealtime, "f", which, linePrefix);
3297 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
3298 rawRealtime, "p", which, linePrefix);
3299 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3300 rawRealtime, "w", which, linePrefix);
3301
3302 // Only log if we had at lease one wakelock...
3303 if (sb.length() > 0) {
3304 String name = wakelocks.keyAt(iw);
3305 if (name.indexOf(',') >= 0) {
3306 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003307 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003308 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 }
3310 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003311
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003312 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3313 for (int isy=syncs.size()-1; isy>=0; isy--) {
3314 final Timer timer = syncs.valueAt(isy);
3315 // Convert from microseconds to milliseconds with rounding
3316 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3317 final int count = timer.getCountLocked(which);
3318 if (totalTime != 0) {
3319 dumpLine(pw, uid, category, SYNC_DATA, syncs.keyAt(isy), totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003320 }
3321 }
3322
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003323 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3324 for (int ij=jobs.size()-1; ij>=0; ij--) {
3325 final Timer timer = jobs.valueAt(ij);
3326 // Convert from microseconds to milliseconds with rounding
3327 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3328 final int count = timer.getCountLocked(which);
3329 if (totalTime != 0) {
3330 dumpLine(pw, uid, category, JOB_DATA, jobs.keyAt(ij), totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003331 }
3332 }
3333
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003334 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3335 rawRealtime, which);
3336 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3337 rawRealtime, which);
3338 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3339 rawRealtime, which);
3340 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3341 rawRealtime, which);
3342
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003343 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3344 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003345 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003346 final Uid.Sensor se = sensors.valueAt(ise);
3347 final int sensorNumber = sensors.keyAt(ise);
3348 final Timer timer = se.getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -08003349 final Counter bgCounter = se.getSensorBgCount();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003350 if (timer != null) {
3351 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003352 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3353 / 1000;
3354 final int count = timer.getCountLocked(which);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08003355 final int bgCount = bgCounter != null ? bgCounter.getCountLocked(which) : 0;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003356 if (totalTime != 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08003357 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count,
3358 bgCount);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 }
3361 }
3362
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003363 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3364 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003365
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003366 dumpTimer(pw, uid, category, FOREGROUND_DATA, u.getForegroundActivityTimer(),
3367 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003368
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003369 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003370 long totalStateTime = 0;
3371 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003372 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3373 totalStateTime += time;
3374 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003375 }
3376 if (totalStateTime > 0) {
3377 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3378 }
3379
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003380 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3381 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
3382 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
3383 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
3384 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
3385 powerCpuMaUs / 1000);
3386 }
3387
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003388 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3389 = u.getProcessStats();
3390 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3391 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003392
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003393 final long userMillis = ps.getUserTime(which);
3394 final long systemMillis = ps.getSystemTime(which);
3395 final long foregroundMillis = ps.getForegroundTime(which);
3396 final int starts = ps.getStarts(which);
3397 final int numCrashes = ps.getNumCrashes(which);
3398 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003399
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003400 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3401 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
3402 dumpLine(pw, uid, category, PROCESS_DATA, processStats.keyAt(ipr), userMillis,
3403 systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 }
3405 }
3406
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003407 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3408 = u.getPackageStats();
3409 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3410 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3411 int wakeups = 0;
3412 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3413 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07003414 int count = alarms.valueAt(iwa).getCountLocked(which);
3415 wakeups += count;
3416 String name = alarms.keyAt(iwa).replace(',', '_');
3417 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003418 }
3419 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3420 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3421 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3422 final long startTime = ss.getStartTime(batteryUptime, which);
3423 final int starts = ss.getStarts(which);
3424 final int launches = ss.getLaunches(which);
3425 if (startTime != 0 || starts != 0 || launches != 0) {
3426 dumpLine(pw, uid, category, APK_DATA,
3427 wakeups, // wakeup alarms
3428 packageStats.keyAt(ipkg), // Apk
3429 serviceStats.keyAt(isvc), // service
3430 startTime / 1000, // time spent started, in ms
3431 starts,
3432 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 }
3434 }
3435 }
3436 }
3437 }
3438
Dianne Hackborn81038902012-11-26 17:04:09 -08003439 static final class TimerEntry {
3440 final String mName;
3441 final int mId;
3442 final BatteryStats.Timer mTimer;
3443 final long mTime;
3444 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3445 mName = name;
3446 mId = id;
3447 mTimer = timer;
3448 mTime = time;
3449 }
3450 }
3451
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003452 private void printmAh(PrintWriter printer, double power) {
3453 printer.print(BatteryStatsHelper.makemAh(power));
3454 }
3455
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003456 private void printmAh(StringBuilder sb, double power) {
3457 sb.append(BatteryStatsHelper.makemAh(power));
3458 }
3459
Dianne Hackbornd953c532014-08-16 18:17:38 -07003460 /**
3461 * Temporary for settings.
3462 */
3463 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3464 int reqUid) {
3465 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3466 }
3467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003469 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003470 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3472 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
3473 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003474
3475 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3476 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3477 final long totalRealtime = computeRealtime(rawRealtime, which);
3478 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003479 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3480 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3481 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003482 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3483 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003484
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003485 final StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07003486
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003487 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003488 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003490 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
3491 if (estimatedBatteryCapacity > 0) {
3492 sb.setLength(0);
3493 sb.append(prefix);
3494 sb.append(" Estimated battery capacity: ");
3495 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
3496 sb.append(" mAh");
3497 pw.println(sb.toString());
3498 }
3499
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003500 sb.setLength(0);
3501 sb.append(prefix);
3502 sb.append(" Time on battery: ");
3503 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3504 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3505 sb.append(") realtime, ");
3506 formatTimeMs(sb, whichBatteryUptime / 1000);
3507 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3508 sb.append(") uptime");
3509 pw.println(sb.toString());
3510 sb.setLength(0);
3511 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003512 sb.append(" Time on battery screen off: ");
3513 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3514 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3515 sb.append(") realtime, ");
3516 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3517 sb.append("(");
3518 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3519 sb.append(") uptime");
3520 pw.println(sb.toString());
3521 sb.setLength(0);
3522 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003523 sb.append(" Total run time: ");
3524 formatTimeMs(sb, totalRealtime / 1000);
3525 sb.append("realtime, ");
3526 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003527 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003528 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003529 if (batteryTimeRemaining >= 0) {
3530 sb.setLength(0);
3531 sb.append(prefix);
3532 sb.append(" Battery time remaining: ");
3533 formatTimeMs(sb, batteryTimeRemaining / 1000);
3534 pw.println(sb.toString());
3535 }
3536 if (chargeTimeRemaining >= 0) {
3537 sb.setLength(0);
3538 sb.append(prefix);
3539 sb.append(" Charge time remaining: ");
3540 formatTimeMs(sb, chargeTimeRemaining / 1000);
3541 pw.println(sb.toString());
3542 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003543
3544 final LongCounter dischargeCounter = getDischargeCoulombCounter();
3545 final long dischargeCount = dischargeCounter.getCountLocked(which);
3546 if (dischargeCount >= 0) {
3547 sb.setLength(0);
3548 sb.append(prefix);
3549 sb.append(" Discharge: ");
3550 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
3551 sb.append(" mAh");
3552 pw.println(sb.toString());
3553 }
3554
3555 final LongCounter dischargeScreenOffCounter = getDischargeScreenOffCoulombCounter();
3556 final long dischargeScreenOffCount = dischargeScreenOffCounter.getCountLocked(which);
3557 if (dischargeScreenOffCount >= 0) {
3558 sb.setLength(0);
3559 sb.append(prefix);
3560 sb.append(" Screen off discharge: ");
3561 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
3562 sb.append(" mAh");
3563 pw.println(sb.toString());
3564 }
3565
3566 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
3567 if (dischargeScreenOnCount >= 0) {
3568 sb.setLength(0);
3569 sb.append(prefix);
3570 sb.append(" Screen on discharge: ");
3571 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
3572 sb.append(" mAh");
3573 pw.println(sb.toString());
3574 }
3575
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003576 pw.print(" Start clock time: ");
3577 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3578
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003579 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003580 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003581 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003582 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3583 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003584 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003585 rawRealtime, which);
3586 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3587 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003588 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003589 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003590 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3591 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3592 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003593 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003594 sb.append(prefix);
3595 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3596 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003597 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003598 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3599 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003600 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003601 pw.println(sb.toString());
3602 sb.setLength(0);
3603 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003604 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003605 boolean didOne = false;
3606 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003607 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003608 if (time == 0) {
3609 continue;
3610 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003611 sb.append("\n ");
3612 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003613 didOne = true;
3614 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3615 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003616 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003617 sb.append("(");
3618 sb.append(formatRatioLocked(time, screenOnTime));
3619 sb.append(")");
3620 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003621 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003622 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003623 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003624 sb.setLength(0);
3625 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003626 sb.append(" Power save mode enabled: ");
3627 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003628 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003629 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003630 sb.append(")");
3631 pw.println(sb.toString());
3632 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003633 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003634 sb.setLength(0);
3635 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003636 sb.append(" Device light idling: ");
3637 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003638 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003639 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
3640 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003641 sb.append("x");
3642 pw.println(sb.toString());
3643 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003644 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003645 sb.setLength(0);
3646 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003647 sb.append(" Idle mode light time: ");
3648 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003649 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003650 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
3651 sb.append(") ");
3652 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003653 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003654 sb.append(" -- longest ");
3655 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
3656 pw.println(sb.toString());
3657 }
3658 if (deviceIdlingTime != 0) {
3659 sb.setLength(0);
3660 sb.append(prefix);
3661 sb.append(" Device full idling: ");
3662 formatTimeMs(sb, deviceIdlingTime / 1000);
3663 sb.append("(");
3664 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003665 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003666 sb.append("x");
3667 pw.println(sb.toString());
3668 }
3669 if (deviceIdleModeFullTime != 0) {
3670 sb.setLength(0);
3671 sb.append(prefix);
3672 sb.append(" Idle mode full time: ");
3673 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
3674 sb.append("(");
3675 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
3676 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003677 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003678 sb.append("x");
3679 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003680 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003681 pw.println(sb.toString());
3682 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003683 if (phoneOnTime != 0) {
3684 sb.setLength(0);
3685 sb.append(prefix);
3686 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3687 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003688 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003689 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003690 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003691 if (connChanges != 0) {
3692 pw.print(prefix);
3693 pw.print(" Connectivity changes: "); pw.println(connChanges);
3694 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003695
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003696 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003697 long fullWakeLockTimeTotalMicros = 0;
3698 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003699
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003700 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003701
Evan Millar22ac0432009-03-31 11:33:18 -07003702 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003703 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003704
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003705 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3706 = u.getWakelockStats();
3707 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3708 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003709
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003710 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3711 if (fullWakeTimer != null) {
3712 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3713 rawRealtime, which);
3714 }
3715
3716 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3717 if (partialWakeTimer != null) {
3718 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3719 rawRealtime, which);
3720 if (totalTimeMicros > 0) {
3721 if (reqUid < 0) {
3722 // Only show the ordered list of all wake
3723 // locks if the caller is not asking for data
3724 // about a specific uid.
3725 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3726 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003727 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003728 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003729 }
3730 }
3731 }
3732 }
3733
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003734 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3735 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3736 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3737 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3738 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3739 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3740 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3741 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003742 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3743 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003744
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003745 if (fullWakeLockTimeTotalMicros != 0) {
3746 sb.setLength(0);
3747 sb.append(prefix);
3748 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
3749 (fullWakeLockTimeTotalMicros + 500) / 1000);
3750 pw.println(sb.toString());
3751 }
3752
3753 if (partialWakeLockTimeTotalMicros != 0) {
3754 sb.setLength(0);
3755 sb.append(prefix);
3756 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
3757 (partialWakeLockTimeTotalMicros + 500) / 1000);
3758 pw.println(sb.toString());
3759 }
3760
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003761 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003762 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
3763 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
3764 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
3765 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003766 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003767 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07003768 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003769 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08003770 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003771 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003772 if (time == 0) {
3773 continue;
3774 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003775 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003776 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003777 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08003778 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003779 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003780 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003781 sb.append("(");
3782 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003783 sb.append(") ");
3784 sb.append(getPhoneSignalStrengthCount(i, which));
3785 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003786 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003787 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003788 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07003789
3790 sb.setLength(0);
3791 sb.append(prefix);
3792 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003793 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003794 pw.println(sb.toString());
3795
Dianne Hackborn627bba72009-03-24 22:32:56 -07003796 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003797 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003798 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003799 didOne = false;
3800 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003801 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003802 if (time == 0) {
3803 continue;
3804 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003805 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003806 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003807 didOne = true;
3808 sb.append(DATA_CONNECTION_NAMES[i]);
3809 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003810 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003811 sb.append("(");
3812 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003813 sb.append(") ");
3814 sb.append(getPhoneDataConnectionCount(i, which));
3815 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003816 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003817 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003818 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003819
3820 sb.setLength(0);
3821 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003822 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003823 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003824 formatTimeMs(sb, mobileActiveTime / 1000);
3825 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
3826 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
3827 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003828 pw.println(sb.toString());
3829
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003830 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
3831 if (mobileActiveUnknownTime != 0) {
3832 sb.setLength(0);
3833 sb.append(prefix);
3834 sb.append(" Mobile radio active unknown time: ");
3835 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
3836 sb.append("(");
3837 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
3838 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
3839 sb.append("x");
3840 pw.println(sb.toString());
3841 }
3842
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003843 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
3844 if (mobileActiveAdjustedTime != 0) {
3845 sb.setLength(0);
3846 sb.append(prefix);
3847 sb.append(" Mobile radio active adjusted time: ");
3848 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
3849 sb.append("(");
3850 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
3851 sb.append(")");
3852 pw.println(sb.toString());
3853 }
3854
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003855 printControllerActivity(pw, sb, prefix, "Radio", getModemControllerActivity(), which);
3856
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003857 pw.print(prefix);
3858 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
3859 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
3860 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
3861 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003862 sb.setLength(0);
3863 sb.append(prefix);
3864 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
3865 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
3866 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
3867 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003868 sb.append(")");
3869 pw.println(sb.toString());
3870
3871 sb.setLength(0);
3872 sb.append(prefix);
3873 sb.append(" Wifi states:");
3874 didOne = false;
3875 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003876 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003877 if (time == 0) {
3878 continue;
3879 }
3880 sb.append("\n ");
3881 didOne = true;
3882 sb.append(WIFI_STATE_NAMES[i]);
3883 sb.append(" ");
3884 formatTimeMs(sb, time/1000);
3885 sb.append("(");
3886 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3887 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07003888 sb.append(getWifiStateCount(i, which));
3889 sb.append("x");
3890 }
3891 if (!didOne) sb.append(" (no activity)");
3892 pw.println(sb.toString());
3893
3894 sb.setLength(0);
3895 sb.append(prefix);
3896 sb.append(" Wifi supplicant states:");
3897 didOne = false;
3898 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3899 final long time = getWifiSupplStateTime(i, rawRealtime, which);
3900 if (time == 0) {
3901 continue;
3902 }
3903 sb.append("\n ");
3904 didOne = true;
3905 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
3906 sb.append(" ");
3907 formatTimeMs(sb, time/1000);
3908 sb.append("(");
3909 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3910 sb.append(") ");
3911 sb.append(getWifiSupplStateCount(i, which));
3912 sb.append("x");
3913 }
3914 if (!didOne) sb.append(" (no activity)");
3915 pw.println(sb.toString());
3916
3917 sb.setLength(0);
3918 sb.append(prefix);
3919 sb.append(" Wifi signal levels:");
3920 didOne = false;
3921 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3922 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
3923 if (time == 0) {
3924 continue;
3925 }
3926 sb.append("\n ");
3927 sb.append(prefix);
3928 didOne = true;
3929 sb.append("level(");
3930 sb.append(i);
3931 sb.append(") ");
3932 formatTimeMs(sb, time/1000);
3933 sb.append("(");
3934 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3935 sb.append(") ");
3936 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003937 sb.append("x");
3938 }
3939 if (!didOne) sb.append(" (no activity)");
3940 pw.println(sb.toString());
3941
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003942 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07003943
Adam Lesinski50e47602015-12-04 17:04:54 -08003944 pw.print(prefix);
3945 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
3946 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
3947
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003948 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
3949 sb.setLength(0);
3950 sb.append(prefix);
3951 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
3952 pw.println(sb.toString());
3953
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003954 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
3955 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003956
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003957 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07003958
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003959 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003960 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003961 pw.print(prefix); pw.println(" Device is currently unplugged");
3962 pw.print(prefix); pw.print(" Discharge cycle start level: ");
3963 pw.println(getDischargeStartLevel());
3964 pw.print(prefix); pw.print(" Discharge cycle current level: ");
3965 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07003966 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003967 pw.print(prefix); pw.println(" Device is currently plugged into power");
3968 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
3969 pw.println(getDischargeStartLevel());
3970 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
3971 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003972 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003973 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
3974 pw.println(getDischargeAmountScreenOn());
3975 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
3976 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07003977 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003978 } else {
3979 pw.print(prefix); pw.println(" Device battery use since last full charge");
3980 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
3981 pw.println(getLowDischargeAmountSinceCharge());
3982 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
3983 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003984 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
3985 pw.println(getDischargeAmountScreenOnSinceCharge());
3986 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
3987 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08003988 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07003989 }
Dianne Hackborn81038902012-11-26 17:04:09 -08003990
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003991 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003992 helper.create(this);
3993 helper.refreshStats(which, UserHandle.USER_ALL);
3994 List<BatterySipper> sippers = helper.getUsageList();
3995 if (sippers != null && sippers.size() > 0) {
3996 pw.print(prefix); pw.println(" Estimated power use (mAh):");
3997 pw.print(prefix); pw.print(" Capacity: ");
3998 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08003999 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004000 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4001 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4002 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4003 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004004 pw.println();
4005 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004006 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004007 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004008 switch (bs.drainType) {
4009 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004010 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004011 break;
4012 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004013 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004014 break;
4015 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004016 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004017 break;
4018 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004019 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004020 break;
4021 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004022 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004023 break;
4024 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004025 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004026 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004027 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004028 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004029 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004030 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004031 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004032 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004033 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004034 break;
4035 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004036 pw.print(" User "); pw.print(bs.userId);
4037 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004038 break;
4039 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004040 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004041 break;
4042 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004043 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004044 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004045 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004046 pw.print(" Camera: ");
4047 break;
4048 default:
4049 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004050 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004051 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004052 printmAh(pw, bs.totalPowerMah);
4053
Adam Lesinski57123002015-06-12 16:12:07 -07004054 if (bs.usagePowerMah != bs.totalPowerMah) {
4055 // If the usage (generic power) isn't the whole amount, we list out
4056 // what components are involved in the calculation.
4057
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004058 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004059 if (bs.usagePowerMah != 0) {
4060 pw.print(" usage=");
4061 printmAh(pw, bs.usagePowerMah);
4062 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004063 if (bs.cpuPowerMah != 0) {
4064 pw.print(" cpu=");
4065 printmAh(pw, bs.cpuPowerMah);
4066 }
4067 if (bs.wakeLockPowerMah != 0) {
4068 pw.print(" wake=");
4069 printmAh(pw, bs.wakeLockPowerMah);
4070 }
4071 if (bs.mobileRadioPowerMah != 0) {
4072 pw.print(" radio=");
4073 printmAh(pw, bs.mobileRadioPowerMah);
4074 }
4075 if (bs.wifiPowerMah != 0) {
4076 pw.print(" wifi=");
4077 printmAh(pw, bs.wifiPowerMah);
4078 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004079 if (bs.bluetoothPowerMah != 0) {
4080 pw.print(" bt=");
4081 printmAh(pw, bs.bluetoothPowerMah);
4082 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004083 if (bs.gpsPowerMah != 0) {
4084 pw.print(" gps=");
4085 printmAh(pw, bs.gpsPowerMah);
4086 }
4087 if (bs.sensorPowerMah != 0) {
4088 pw.print(" sensor=");
4089 printmAh(pw, bs.sensorPowerMah);
4090 }
4091 if (bs.cameraPowerMah != 0) {
4092 pw.print(" camera=");
4093 printmAh(pw, bs.cameraPowerMah);
4094 }
4095 if (bs.flashlightPowerMah != 0) {
4096 pw.print(" flash=");
4097 printmAh(pw, bs.flashlightPowerMah);
4098 }
4099 pw.print(" )");
4100 }
4101 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004102 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004103 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004104 }
4105
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004106 sippers = helper.getMobilemsppList();
4107 if (sippers != null && sippers.size() > 0) {
4108 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004109 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004110 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004111 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004112 sb.setLength(0);
4113 sb.append(prefix); sb.append(" Uid ");
4114 UserHandle.formatUid(sb, bs.uidObj.getUid());
4115 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4116 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4117 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004118 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004119 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004120 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004121 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004122 sb.setLength(0);
4123 sb.append(prefix);
4124 sb.append(" TOTAL TIME: ");
4125 formatTimeMs(sb, totalTime);
4126 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4127 sb.append(")");
4128 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004129 pw.println();
4130 }
4131
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004132 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4133 @Override
4134 public int compare(TimerEntry lhs, TimerEntry rhs) {
4135 long lhsTime = lhs.mTime;
4136 long rhsTime = rhs.mTime;
4137 if (lhsTime < rhsTime) {
4138 return 1;
4139 }
4140 if (lhsTime > rhsTime) {
4141 return -1;
4142 }
4143 return 0;
4144 }
4145 };
4146
4147 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004148 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4149 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004150 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004151 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4152 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4153 : kernelWakelocks.entrySet()) {
4154 final BatteryStats.Timer timer = ent.getValue();
4155 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004156 if (totalTimeMillis > 0) {
4157 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4158 }
4159 }
4160 if (ktimers.size() > 0) {
4161 Collections.sort(ktimers, timerComparator);
4162 pw.print(prefix); pw.println(" All kernel wake locks:");
4163 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004164 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004165 String linePrefix = ": ";
4166 sb.setLength(0);
4167 sb.append(prefix);
4168 sb.append(" Kernel Wake lock ");
4169 sb.append(timer.mName);
4170 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4171 which, linePrefix);
4172 if (!linePrefix.equals(": ")) {
4173 sb.append(" realtime");
4174 // Only print out wake locks that were held
4175 pw.println(sb.toString());
4176 }
4177 }
4178 pw.println();
4179 }
4180 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004181
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004182 if (timers.size() > 0) {
4183 Collections.sort(timers, timerComparator);
4184 pw.print(prefix); pw.println(" All partial wake locks:");
4185 for (int i=0; i<timers.size(); i++) {
4186 TimerEntry timer = timers.get(i);
4187 sb.setLength(0);
4188 sb.append(" Wake lock ");
4189 UserHandle.formatUid(sb, timer.mId);
4190 sb.append(" ");
4191 sb.append(timer.mName);
4192 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4193 sb.append(" realtime");
4194 pw.println(sb.toString());
4195 }
4196 timers.clear();
4197 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08004198 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004199
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004200 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004201 if (wakeupReasons.size() > 0) {
4202 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004203 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004204 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004205 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004206 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
4207 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004208 }
4209 Collections.sort(reasons, timerComparator);
4210 for (int i=0; i<reasons.size(); i++) {
4211 TimerEntry timer = reasons.get(i);
4212 String linePrefix = ": ";
4213 sb.setLength(0);
4214 sb.append(prefix);
4215 sb.append(" Wakeup reason ");
4216 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004217 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4218 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004219 pw.println(sb.toString());
4220 }
4221 pw.println();
4222 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004223 }
Evan Millar22ac0432009-03-31 11:33:18 -07004224
James Carr2dd7e5e2016-07-20 18:48:39 -07004225 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
4226 pw.println("Memory Stats");
4227 for (int i = 0; i < mMemoryStats.size(); i++) {
4228 sb.setLength(0);
4229 sb.append("Bandwidth ");
4230 sb.append(mMemoryStats.keyAt(i));
4231 sb.append(" Time ");
4232 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
4233 pw.println(sb.toString());
4234 }
4235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004236 for (int iu=0; iu<NU; iu++) {
4237 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004238 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004239 continue;
4240 }
4241
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004242 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07004243
4244 pw.print(prefix);
4245 pw.print(" ");
4246 UserHandle.formatUid(pw, uid);
4247 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004249
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004250 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4251 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4252 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4253 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004254 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4255 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
4256
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004257 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4258 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004259 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4260 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004261
4262 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
4263 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
4264
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004265 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4266 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4267 final int wifiScanCount = u.getWifiScanCount(which);
4268 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004269
Adam Lesinski5f056f62016-07-14 16:56:08 -07004270 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
4271 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
4272
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004273 if (mobileRxBytes > 0 || mobileTxBytes > 0
4274 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004275 pw.print(prefix); pw.print(" Mobile network: ");
4276 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004277 pw.print(formatBytesLocked(mobileTxBytes));
4278 pw.print(" sent (packets "); pw.print(mobileRxPackets);
4279 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004280 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004281 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
4282 sb.setLength(0);
4283 sb.append(prefix); sb.append(" Mobile radio active: ");
4284 formatTimeMs(sb, uidMobileActiveTime / 1000);
4285 sb.append("(");
4286 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
4287 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
4288 long packets = mobileRxPackets + mobileTxPackets;
4289 if (packets == 0) {
4290 packets = 1;
4291 }
4292 sb.append(" @ ");
4293 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
4294 sb.append(" mspp");
4295 pw.println(sb.toString());
4296 }
4297
Adam Lesinski5f056f62016-07-14 16:56:08 -07004298 if (mobileWakeup > 0) {
4299 sb.setLength(0);
4300 sb.append(prefix);
4301 sb.append(" Mobile radio AP wakeups: ");
4302 sb.append(mobileWakeup);
4303 pw.println(sb.toString());
4304 }
4305
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004306 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
4307 u.getModemControllerActivity(), which);
4308
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004309 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004310 pw.print(prefix); pw.print(" Wi-Fi network: ");
4311 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004312 pw.print(formatBytesLocked(wifiTxBytes));
4313 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4314 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004315 }
4316
Dianne Hackborn62793e42015-03-09 11:15:41 -07004317 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004318 || uidWifiRunningTime != 0) {
4319 sb.setLength(0);
4320 sb.append(prefix); sb.append(" Wifi Running: ");
4321 formatTimeMs(sb, uidWifiRunningTime / 1000);
4322 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4323 whichBatteryRealtime)); sb.append(")\n");
4324 sb.append(prefix); sb.append(" Full Wifi Lock: ");
4325 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4326 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4327 whichBatteryRealtime)); sb.append(")\n");
4328 sb.append(prefix); sb.append(" Wifi Scan: ");
4329 formatTimeMs(sb, wifiScanTime / 1000);
4330 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004331 whichBatteryRealtime)); sb.append(") ");
4332 sb.append(wifiScanCount);
4333 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004334 pw.println(sb.toString());
4335 }
4336
Adam Lesinski5f056f62016-07-14 16:56:08 -07004337 if (wifiWakeup > 0) {
4338 sb.setLength(0);
4339 sb.append(prefix);
4340 sb.append(" WiFi AP wakeups: ");
4341 sb.append(wifiWakeup);
4342 pw.println(sb.toString());
4343 }
4344
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004345 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
4346 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07004347
Adam Lesinski50e47602015-12-04 17:04:54 -08004348 if (btRxBytes > 0 || btTxBytes > 0) {
4349 pw.print(prefix); pw.print(" Bluetooth network: ");
4350 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
4351 pw.print(formatBytesLocked(btTxBytes));
4352 pw.println(" sent");
4353 }
4354
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004355 uidActivity |= printTimer(pw, sb, u.getBluetoothScanTimer(), rawRealtime, which, prefix,
4356 "Bluetooth Scan");
4357
Dianne Hackborn617f8772009-03-31 15:04:46 -07004358 if (u.hasUserActivity()) {
4359 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07004360 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004361 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004362 if (val != 0) {
4363 if (!hasData) {
4364 sb.setLength(0);
4365 sb.append(" User activity: ");
4366 hasData = true;
4367 } else {
4368 sb.append(", ");
4369 }
4370 sb.append(val);
4371 sb.append(" ");
4372 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
4373 }
4374 }
4375 if (hasData) {
4376 pw.println(sb.toString());
4377 }
4378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004380 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4381 = u.getWakelockStats();
4382 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004383 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004384 int countWakelock = 0;
4385 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4386 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4387 String linePrefix = ": ";
4388 sb.setLength(0);
4389 sb.append(prefix);
4390 sb.append(" Wake lock ");
4391 sb.append(wakelocks.keyAt(iw));
4392 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
4393 "full", which, linePrefix);
4394 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
4395 "partial", which, linePrefix);
4396 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
4397 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004398 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
4399 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07004400 sb.append(" realtime");
4401 pw.println(sb.toString());
4402 uidActivity = true;
4403 countWakelock++;
4404
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004405 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
4406 rawRealtime, which);
4407 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
4408 rawRealtime, which);
4409 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
4410 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004411 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07004412 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004413 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004414 if (countWakelock > 1) {
4415 if (totalFullWakelock != 0 || totalPartialWakelock != 0
4416 || totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004417 sb.setLength(0);
4418 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004419 sb.append(" TOTAL wake: ");
4420 boolean needComma = false;
4421 if (totalFullWakelock != 0) {
4422 needComma = true;
4423 formatTimeMs(sb, totalFullWakelock);
4424 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004425 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004426 if (totalPartialWakelock != 0) {
4427 if (needComma) {
4428 sb.append(", ");
4429 }
4430 needComma = true;
4431 formatTimeMs(sb, totalPartialWakelock);
4432 sb.append("partial");
4433 }
4434 if (totalWindowWakelock != 0) {
4435 if (needComma) {
4436 sb.append(", ");
4437 }
4438 needComma = true;
4439 formatTimeMs(sb, totalWindowWakelock);
4440 sb.append("window");
4441 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004442 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07004443 if (needComma) {
4444 sb.append(",");
4445 }
4446 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004447 formatTimeMs(sb, totalDrawWakelock);
4448 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07004449 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004450 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004451 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004452 }
4453 }
4454
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004455 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4456 for (int isy=syncs.size()-1; isy>=0; isy--) {
4457 final Timer timer = syncs.valueAt(isy);
4458 // Convert from microseconds to milliseconds with rounding
4459 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4460 final int count = timer.getCountLocked(which);
4461 sb.setLength(0);
4462 sb.append(prefix);
4463 sb.append(" Sync ");
4464 sb.append(syncs.keyAt(isy));
4465 sb.append(": ");
4466 if (totalTime != 0) {
4467 formatTimeMs(sb, totalTime);
4468 sb.append("realtime (");
4469 sb.append(count);
4470 sb.append(" times)");
4471 } else {
4472 sb.append("(not used)");
4473 }
4474 pw.println(sb.toString());
4475 uidActivity = true;
4476 }
4477
4478 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4479 for (int ij=jobs.size()-1; ij>=0; ij--) {
4480 final Timer timer = jobs.valueAt(ij);
4481 // Convert from microseconds to milliseconds with rounding
4482 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4483 final int count = timer.getCountLocked(which);
4484 sb.setLength(0);
4485 sb.append(prefix);
4486 sb.append(" Job ");
4487 sb.append(jobs.keyAt(ij));
4488 sb.append(": ");
4489 if (totalTime != 0) {
4490 formatTimeMs(sb, totalTime);
4491 sb.append("realtime (");
4492 sb.append(count);
4493 sb.append(" times)");
4494 } else {
4495 sb.append("(not used)");
4496 }
4497 pw.println(sb.toString());
4498 uidActivity = true;
4499 }
4500
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004501 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
4502 prefix, "Flashlight");
4503 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
4504 prefix, "Camera");
4505 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
4506 prefix, "Video");
4507 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
4508 prefix, "Audio");
4509
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004510 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4511 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004512 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004513 final Uid.Sensor se = sensors.valueAt(ise);
4514 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004515 sb.setLength(0);
4516 sb.append(prefix);
4517 sb.append(" Sensor ");
4518 int handle = se.getHandle();
4519 if (handle == Uid.Sensor.GPS) {
4520 sb.append("GPS");
4521 } else {
4522 sb.append(handle);
4523 }
4524 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004525
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004526 final Timer timer = se.getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004527 final Counter bgCounter = se.getSensorBgCount();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004528 if (timer != null) {
4529 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004530 final long totalTime = (timer.getTotalTimeLocked(
Dianne Hackborn61659e52014-07-09 16:13:01 -07004531 rawRealtime, which) + 500) / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004532 final int count = timer.getCountLocked(which);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004533 final int bgCount = bgCounter != null ? bgCounter.getCountLocked(which) : 0;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004534 //timer.logState();
4535 if (totalTime != 0) {
4536 formatTimeMs(sb, totalTime);
4537 sb.append("realtime (");
4538 sb.append(count);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004539 sb.append(" times");
4540 if (bgCount > 0) {
4541 sb.append(", ");
4542 sb.append(bgCount);
4543 sb.append(" bg");
4544 }
4545 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004546 } else {
4547 sb.append("(not used)");
4548 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004549 } else {
4550 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004551 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004552
4553 pw.println(sb.toString());
4554 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004555 }
4556
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004557 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
4558 "Vibrator");
4559 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
4560 prefix, "Foreground activities");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004561
Dianne Hackborn61659e52014-07-09 16:13:01 -07004562 long totalStateTime = 0;
4563 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
4564 long time = u.getProcessStateTime(ips, rawRealtime, which);
4565 if (time > 0) {
4566 totalStateTime += time;
4567 sb.setLength(0);
4568 sb.append(prefix);
4569 sb.append(" ");
4570 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
4571 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08004572 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004573 pw.println(sb.toString());
4574 uidActivity = true;
4575 }
4576 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08004577 if (totalStateTime > 0) {
4578 sb.setLength(0);
4579 sb.append(prefix);
4580 sb.append(" Total running: ");
4581 formatTimeMs(sb, (totalStateTime + 500) / 1000);
4582 pw.println(sb.toString());
4583 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004584
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004585 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4586 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004587 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
4588 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004589 sb.setLength(0);
4590 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07004591 sb.append(" Total cpu time: u=");
4592 formatTimeMs(sb, userCpuTimeUs / 1000);
4593 sb.append("s=");
4594 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004595 sb.append("p=");
4596 printmAh(sb, powerCpuMaUs / (1000.0 * 1000.0 * 60.0 * 60.0));
4597 sb.append("mAh");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004598 pw.println(sb.toString());
4599 }
4600
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004601 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4602 = u.getProcessStats();
4603 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4604 final Uid.Proc ps = processStats.valueAt(ipr);
4605 long userTime;
4606 long systemTime;
4607 long foregroundTime;
4608 int starts;
4609 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004610
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004611 userTime = ps.getUserTime(which);
4612 systemTime = ps.getSystemTime(which);
4613 foregroundTime = ps.getForegroundTime(which);
4614 starts = ps.getStarts(which);
4615 final int numCrashes = ps.getNumCrashes(which);
4616 final int numAnrs = ps.getNumAnrs(which);
4617 numExcessive = which == STATS_SINCE_CHARGED
4618 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004619
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004620 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
4621 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
4622 sb.setLength(0);
4623 sb.append(prefix); sb.append(" Proc ");
4624 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
4625 sb.append(prefix); sb.append(" CPU: ");
4626 formatTimeMs(sb, userTime); sb.append("usr + ");
4627 formatTimeMs(sb, systemTime); sb.append("krn ; ");
4628 formatTimeMs(sb, foregroundTime); sb.append("fg");
4629 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
4630 sb.append("\n"); sb.append(prefix); sb.append(" ");
4631 boolean hasOne = false;
4632 if (starts != 0) {
4633 hasOne = true;
4634 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004635 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004636 if (numCrashes != 0) {
4637 if (hasOne) {
4638 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004639 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004640 hasOne = true;
4641 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004642 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004643 if (numAnrs != 0) {
4644 if (hasOne) {
4645 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004646 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004647 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004648 }
4649 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004650 pw.println(sb.toString());
4651 for (int e=0; e<numExcessive; e++) {
4652 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
4653 if (ew != null) {
4654 pw.print(prefix); pw.print(" * Killed for ");
4655 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
4656 pw.print("wake lock");
4657 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
4658 pw.print("cpu");
4659 } else {
4660 pw.print("unknown");
4661 }
4662 pw.print(" use: ");
4663 TimeUtils.formatDuration(ew.usedTime, pw);
4664 pw.print(" over ");
4665 TimeUtils.formatDuration(ew.overTime, pw);
4666 if (ew.overTime != 0) {
4667 pw.print(" (");
4668 pw.print((ew.usedTime*100)/ew.overTime);
4669 pw.println("%)");
4670 }
4671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004672 }
4673 uidActivity = true;
4674 }
4675 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004676
4677 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4678 = u.getPackageStats();
4679 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4680 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
4681 pw.println(":");
4682 boolean apkActivity = false;
4683 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4684 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4685 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
4686 pw.print(prefix); pw.print(" Wakeup alarm ");
4687 pw.print(alarms.keyAt(iwa)); pw.print(": ");
4688 pw.print(alarms.valueAt(iwa).getCountLocked(which));
4689 pw.println(" times");
4690 apkActivity = true;
4691 }
4692 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4693 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4694 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4695 final long startTime = ss.getStartTime(batteryUptime, which);
4696 final int starts = ss.getStarts(which);
4697 final int launches = ss.getLaunches(which);
4698 if (startTime != 0 || starts != 0 || launches != 0) {
4699 sb.setLength(0);
4700 sb.append(prefix); sb.append(" Service ");
4701 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
4702 sb.append(prefix); sb.append(" Created for: ");
4703 formatTimeMs(sb, startTime / 1000);
4704 sb.append("uptime\n");
4705 sb.append(prefix); sb.append(" Starts: ");
4706 sb.append(starts);
4707 sb.append(", launches: "); sb.append(launches);
4708 pw.println(sb.toString());
4709 apkActivity = true;
4710 }
4711 }
4712 if (!apkActivity) {
4713 pw.print(prefix); pw.println(" (nothing executed)");
4714 }
4715 uidActivity = true;
4716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004717 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004718 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004719 }
4720 }
4721 }
4722
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004723 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004724 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004725 int diff = oldval ^ newval;
4726 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004727 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004728 for (int i=0; i<descriptions.length; i++) {
4729 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004730 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004731 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004732 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004733 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004734 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004735 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
4736 didWake = true;
4737 pw.print("=");
4738 if (longNames) {
4739 UserHandle.formatUid(pw, wakelockTag.uid);
4740 pw.print(":\"");
4741 pw.print(wakelockTag.string);
4742 pw.print("\"");
4743 } else {
4744 pw.print(wakelockTag.poolIdx);
4745 }
4746 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004747 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004748 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004749 pw.print("=");
4750 int val = (newval&bd.mask)>>bd.shift;
4751 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004752 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004753 } else {
4754 pw.print(val);
4755 }
4756 }
4757 }
4758 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004759 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07004760 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004761 if (longNames) {
4762 UserHandle.formatUid(pw, wakelockTag.uid);
4763 pw.print(":\"");
4764 pw.print(wakelockTag.string);
4765 pw.print("\"");
4766 } else {
4767 pw.print(wakelockTag.poolIdx);
4768 }
4769 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004770 }
4771
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004772 public void prepareForDumpLocked() {
4773 }
4774
4775 public static class HistoryPrinter {
4776 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004777 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004778 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004779 int oldStatus = -1;
4780 int oldHealth = -1;
4781 int oldPlug = -1;
4782 int oldTemp = -1;
4783 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004784 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004785 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004786
Dianne Hackborn3251b902014-06-20 14:40:53 -07004787 void reset() {
4788 oldState = oldState2 = 0;
4789 oldLevel = -1;
4790 oldStatus = -1;
4791 oldHealth = -1;
4792 oldPlug = -1;
4793 oldTemp = -1;
4794 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004795 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07004796 }
4797
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004798 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004799 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004800 if (!checkin) {
4801 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004802 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004803 pw.print(" (");
4804 pw.print(rec.numReadInts);
4805 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004806 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004807 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4808 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004809 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004810 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004811 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004812 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004813 }
4814 lastTime = rec.time;
4815 }
4816 if (rec.cmd == HistoryItem.CMD_START) {
4817 if (checkin) {
4818 pw.print(":");
4819 }
4820 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004821 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004822 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
4823 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004824 if (checkin) {
4825 pw.print(":");
4826 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07004827 if (rec.cmd == HistoryItem.CMD_RESET) {
4828 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004829 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004830 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004831 pw.print("TIME:");
4832 if (checkin) {
4833 pw.println(rec.currentTime);
4834 } else {
4835 pw.print(" ");
4836 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
4837 rec.currentTime).toString());
4838 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08004839 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
4840 if (checkin) {
4841 pw.print(":");
4842 }
4843 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004844 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
4845 if (checkin) {
4846 pw.print(":");
4847 }
4848 pw.println("*OVERFLOW*");
4849 } else {
4850 if (!checkin) {
4851 if (rec.batteryLevel < 10) pw.print("00");
4852 else if (rec.batteryLevel < 100) pw.print("0");
4853 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004854 if (verbose) {
4855 pw.print(" ");
4856 if (rec.states < 0) ;
4857 else if (rec.states < 0x10) pw.print("0000000");
4858 else if (rec.states < 0x100) pw.print("000000");
4859 else if (rec.states < 0x1000) pw.print("00000");
4860 else if (rec.states < 0x10000) pw.print("0000");
4861 else if (rec.states < 0x100000) pw.print("000");
4862 else if (rec.states < 0x1000000) pw.print("00");
4863 else if (rec.states < 0x10000000) pw.print("0");
4864 pw.print(Integer.toHexString(rec.states));
4865 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004866 } else {
4867 if (oldLevel != rec.batteryLevel) {
4868 oldLevel = rec.batteryLevel;
4869 pw.print(",Bl="); pw.print(rec.batteryLevel);
4870 }
4871 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004872 if (oldStatus != rec.batteryStatus) {
4873 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004874 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004875 switch (oldStatus) {
4876 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004877 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004878 break;
4879 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004880 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004881 break;
4882 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004883 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004884 break;
4885 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004886 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004887 break;
4888 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004889 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004890 break;
4891 default:
4892 pw.print(oldStatus);
4893 break;
4894 }
4895 }
4896 if (oldHealth != rec.batteryHealth) {
4897 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004898 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004899 switch (oldHealth) {
4900 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004901 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004902 break;
4903 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004904 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004905 break;
4906 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004907 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004908 break;
4909 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004910 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004911 break;
4912 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004913 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004914 break;
4915 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004916 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004917 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004918 case BatteryManager.BATTERY_HEALTH_COLD:
4919 pw.print(checkin ? "c" : "cold");
4920 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004921 default:
4922 pw.print(oldHealth);
4923 break;
4924 }
4925 }
4926 if (oldPlug != rec.batteryPlugType) {
4927 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004928 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004929 switch (oldPlug) {
4930 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004931 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004932 break;
4933 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004934 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004935 break;
4936 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004937 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004938 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07004939 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004940 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07004941 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004942 default:
4943 pw.print(oldPlug);
4944 break;
4945 }
4946 }
4947 if (oldTemp != rec.batteryTemperature) {
4948 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004949 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004950 pw.print(oldTemp);
4951 }
4952 if (oldVolt != rec.batteryVoltage) {
4953 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004954 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004955 pw.print(oldVolt);
4956 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004957 final int chargeMAh = rec.batteryChargeUAh / 1000;
4958 if (oldChargeMAh != chargeMAh) {
4959 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07004960 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004961 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07004962 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004963 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004964 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004965 printBitDescriptions(pw, oldState2, rec.states2, null,
4966 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004967 if (rec.wakeReasonTag != null) {
4968 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07004969 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004970 pw.print(rec.wakeReasonTag.poolIdx);
4971 } else {
4972 pw.print(" wake_reason=");
4973 pw.print(rec.wakeReasonTag.uid);
4974 pw.print(":\"");
4975 pw.print(rec.wakeReasonTag.string);
4976 pw.print("\"");
4977 }
4978 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004979 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004980 pw.print(checkin ? "," : " ");
4981 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
4982 pw.print("+");
4983 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
4984 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004985 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004986 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
4987 : HISTORY_EVENT_NAMES;
4988 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
4989 | HistoryItem.EVENT_FLAG_FINISH);
4990 if (idx >= 0 && idx < eventNames.length) {
4991 pw.print(eventNames[idx]);
4992 } else {
4993 pw.print(checkin ? "Ev" : "event");
4994 pw.print(idx);
4995 }
4996 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004997 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004998 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08004999 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08005000 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
5001 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005002 pw.print(":\"");
5003 pw.print(rec.eventTag.string);
5004 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005005 }
5006 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005007 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005008 if (rec.stepDetails != null) {
5009 if (!checkin) {
5010 pw.print(" Details: cpu=");
5011 pw.print(rec.stepDetails.userTime);
5012 pw.print("u+");
5013 pw.print(rec.stepDetails.systemTime);
5014 pw.print("s");
5015 if (rec.stepDetails.appCpuUid1 >= 0) {
5016 pw.print(" (");
5017 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
5018 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5019 if (rec.stepDetails.appCpuUid2 >= 0) {
5020 pw.print(", ");
5021 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
5022 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5023 }
5024 if (rec.stepDetails.appCpuUid3 >= 0) {
5025 pw.print(", ");
5026 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
5027 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5028 }
5029 pw.print(')');
5030 }
5031 pw.println();
5032 pw.print(" /proc/stat=");
5033 pw.print(rec.stepDetails.statUserTime);
5034 pw.print(" usr, ");
5035 pw.print(rec.stepDetails.statSystemTime);
5036 pw.print(" sys, ");
5037 pw.print(rec.stepDetails.statIOWaitTime);
5038 pw.print(" io, ");
5039 pw.print(rec.stepDetails.statIrqTime);
5040 pw.print(" irq, ");
5041 pw.print(rec.stepDetails.statSoftIrqTime);
5042 pw.print(" sirq, ");
5043 pw.print(rec.stepDetails.statIdlTime);
5044 pw.print(" idle");
5045 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
5046 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
5047 + rec.stepDetails.statSoftIrqTime;
5048 int total = totalRun + rec.stepDetails.statIdlTime;
5049 if (total > 0) {
5050 pw.print(" (");
5051 float perc = ((float)totalRun) / ((float)total) * 100;
5052 pw.print(String.format("%.1f%%", perc));
5053 pw.print(" of ");
5054 StringBuilder sb = new StringBuilder(64);
5055 formatTimeMsNoSpace(sb, total*10);
5056 pw.print(sb);
5057 pw.print(")");
5058 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005059 pw.print(", PlatformIdleStat ");
5060 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005061 pw.println();
5062 } else {
5063 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5064 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
5065 pw.print(rec.stepDetails.userTime);
5066 pw.print(":");
5067 pw.print(rec.stepDetails.systemTime);
5068 if (rec.stepDetails.appCpuUid1 >= 0) {
5069 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
5070 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5071 if (rec.stepDetails.appCpuUid2 >= 0) {
5072 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
5073 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5074 }
5075 if (rec.stepDetails.appCpuUid3 >= 0) {
5076 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
5077 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5078 }
5079 }
5080 pw.println();
5081 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5082 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
5083 pw.print(rec.stepDetails.statUserTime);
5084 pw.print(',');
5085 pw.print(rec.stepDetails.statSystemTime);
5086 pw.print(',');
5087 pw.print(rec.stepDetails.statIOWaitTime);
5088 pw.print(',');
5089 pw.print(rec.stepDetails.statIrqTime);
5090 pw.print(',');
5091 pw.print(rec.stepDetails.statSoftIrqTime);
5092 pw.print(',');
5093 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005094 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005095 if (rec.stepDetails.statPlatformIdleState != null) {
5096 pw.print(rec.stepDetails.statPlatformIdleState);
5097 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005098 pw.println();
5099 }
5100 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005101 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005102 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005103 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005104 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005105
5106 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
5107 UserHandle.formatUid(pw, uid);
5108 pw.print("=");
5109 pw.print(utime);
5110 pw.print("u+");
5111 pw.print(stime);
5112 pw.print("s");
5113 }
5114
5115 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
5116 pw.print('/');
5117 pw.print(uid);
5118 pw.print(":");
5119 pw.print(utime);
5120 pw.print(":");
5121 pw.print(stime);
5122 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005123 }
5124
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005125 private void printSizeValue(PrintWriter pw, long size) {
5126 float result = size;
5127 String suffix = "";
5128 if (result >= 10*1024) {
5129 suffix = "KB";
5130 result = result / 1024;
5131 }
5132 if (result >= 10*1024) {
5133 suffix = "MB";
5134 result = result / 1024;
5135 }
5136 if (result >= 10*1024) {
5137 suffix = "GB";
5138 result = result / 1024;
5139 }
5140 if (result >= 10*1024) {
5141 suffix = "TB";
5142 result = result / 1024;
5143 }
5144 if (result >= 10*1024) {
5145 suffix = "PB";
5146 result = result / 1024;
5147 }
5148 pw.print((int)result);
5149 pw.print(suffix);
5150 }
5151
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005152 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
5153 String label3, long estimatedTime) {
5154 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005155 return false;
5156 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005157 pw.print(label1);
5158 pw.print(label2);
5159 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005160 StringBuilder sb = new StringBuilder(64);
5161 formatTimeMs(sb, estimatedTime);
5162 pw.print(sb);
5163 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005164 return true;
5165 }
5166
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005167 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
5168 LevelStepTracker steps, boolean checkin) {
5169 if (steps == null) {
5170 return false;
5171 }
5172 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005173 if (count <= 0) {
5174 return false;
5175 }
5176 if (!checkin) {
5177 pw.println(header);
5178 }
Kweku Adams030980a2015-04-01 16:07:48 -07005179 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005180 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005181 long duration = steps.getDurationAt(i);
5182 int level = steps.getLevelAt(i);
5183 long initMode = steps.getInitModeAt(i);
5184 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005185 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005186 lineArgs[0] = Long.toString(duration);
5187 lineArgs[1] = Integer.toString(level);
5188 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5189 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5190 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
5191 case Display.STATE_ON: lineArgs[2] = "s+"; break;
5192 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
5193 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07005194 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005195 }
5196 } else {
5197 lineArgs[2] = "";
5198 }
5199 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5200 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
5201 } else {
5202 lineArgs[3] = "";
5203 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005204 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07005205 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005206 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07005207 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005208 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005209 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
5210 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005211 pw.print(prefix);
5212 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005213 TimeUtils.formatDuration(duration, pw);
5214 pw.print(" to "); pw.print(level);
5215 boolean haveModes = false;
5216 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5217 pw.print(" (");
5218 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5219 case Display.STATE_OFF: pw.print("screen-off"); break;
5220 case Display.STATE_ON: pw.print("screen-on"); break;
5221 case Display.STATE_DOZE: pw.print("screen-doze"); break;
5222 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07005223 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005224 }
5225 haveModes = true;
5226 }
5227 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5228 pw.print(haveModes ? ", " : " (");
5229 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
5230 ? "power-save-on" : "power-save-off");
5231 haveModes = true;
5232 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005233 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
5234 pw.print(haveModes ? ", " : " (");
5235 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
5236 ? "device-idle-on" : "device-idle-off");
5237 haveModes = true;
5238 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005239 if (haveModes) {
5240 pw.print(")");
5241 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005242 pw.println();
5243 }
5244 }
5245 return true;
5246 }
5247
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005248 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005249 public static final int DUMP_DAILY_ONLY = 1<<2;
5250 public static final int DUMP_HISTORY_ONLY = 1<<3;
5251 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
5252 public static final int DUMP_VERBOSE = 1<<5;
5253 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005254
Dianne Hackborn37de0982014-05-09 09:32:18 -07005255 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
5256 final HistoryPrinter hprinter = new HistoryPrinter();
5257 final HistoryItem rec = new HistoryItem();
5258 long lastTime = -1;
5259 long baseTime = -1;
5260 boolean printed = false;
5261 HistoryEventTracker tracker = null;
5262 while (getNextHistoryLocked(rec)) {
5263 lastTime = rec.time;
5264 if (baseTime < 0) {
5265 baseTime = lastTime;
5266 }
5267 if (rec.time >= histStart) {
5268 if (histStart >= 0 && !printed) {
5269 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07005270 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005271 || rec.cmd == HistoryItem.CMD_START
5272 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005273 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005274 hprinter.printNextItem(pw, rec, baseTime, checkin,
5275 (flags&DUMP_VERBOSE) != 0);
5276 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005277 } else if (rec.currentTime != 0) {
5278 printed = true;
5279 byte cmd = rec.cmd;
5280 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005281 hprinter.printNextItem(pw, rec, baseTime, checkin,
5282 (flags&DUMP_VERBOSE) != 0);
5283 rec.cmd = cmd;
5284 }
5285 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005286 if (rec.cmd != HistoryItem.CMD_UPDATE) {
5287 hprinter.printNextItem(pw, rec, baseTime, checkin,
5288 (flags&DUMP_VERBOSE) != 0);
5289 rec.cmd = HistoryItem.CMD_UPDATE;
5290 }
5291 int oldEventCode = rec.eventCode;
5292 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005293 rec.eventTag = new HistoryTag();
5294 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
5295 HashMap<String, SparseIntArray> active
5296 = tracker.getStateForEvent(i);
5297 if (active == null) {
5298 continue;
5299 }
5300 for (HashMap.Entry<String, SparseIntArray> ent
5301 : active.entrySet()) {
5302 SparseIntArray uids = ent.getValue();
5303 for (int j=0; j<uids.size(); j++) {
5304 rec.eventCode = i;
5305 rec.eventTag.string = ent.getKey();
5306 rec.eventTag.uid = uids.keyAt(j);
5307 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07005308 hprinter.printNextItem(pw, rec, baseTime, checkin,
5309 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005310 rec.wakeReasonTag = null;
5311 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005312 }
5313 }
5314 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005315 rec.eventCode = oldEventCode;
5316 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005317 tracker = null;
5318 }
5319 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005320 hprinter.printNextItem(pw, rec, baseTime, checkin,
5321 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07005322 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
5323 // This is an attempt to aggregate the previous state and generate
5324 // fake events to reflect that state at the point where we start
5325 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07005326 if (tracker == null) {
5327 tracker = new HistoryEventTracker();
5328 }
5329 tracker.updateState(rec.eventCode, rec.eventTag.string,
5330 rec.eventTag.uid, rec.eventTag.poolIdx);
5331 }
5332 }
5333 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07005334 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005335 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
5336 }
5337 }
5338
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005339 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
5340 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
5341 if (steps == null) {
5342 return;
5343 }
5344 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
5345 if (timeRemaining >= 0) {
5346 pw.print(prefix); pw.print(label); pw.print(" total time: ");
5347 tmpSb.setLength(0);
5348 formatTimeMs(tmpSb, timeRemaining);
5349 pw.print(tmpSb);
5350 pw.print(" (from "); pw.print(tmpOutInt[0]);
5351 pw.println(" steps)");
5352 }
5353 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5354 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5355 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
5356 if (estimatedTime > 0) {
5357 pw.print(prefix); pw.print(label); pw.print(" ");
5358 pw.print(STEP_LEVEL_MODE_LABELS[i]);
5359 pw.print(" time: ");
5360 tmpSb.setLength(0);
5361 formatTimeMs(tmpSb, estimatedTime);
5362 pw.print(tmpSb);
5363 pw.print(" (from "); pw.print(tmpOutInt[0]);
5364 pw.println(" steps)");
5365 }
5366 }
5367 }
5368
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005369 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
5370 ArrayList<PackageChange> changes) {
5371 if (changes == null) {
5372 return;
5373 }
5374 pw.print(prefix); pw.println("Package changes:");
5375 for (int i=0; i<changes.size(); i++) {
5376 PackageChange pc = changes.get(i);
5377 if (pc.mUpdate) {
5378 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
5379 pw.print(" vers="); pw.println(pc.mVersionCode);
5380 } else {
5381 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
5382 }
5383 }
5384 }
5385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005386 /**
5387 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
5388 *
5389 * @param pw a Printer to receive the dump output.
5390 */
5391 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005392 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005393 prepareForDumpLocked();
5394
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005395 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005396 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005397
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005398 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005399 final long historyTotalSize = getHistoryTotalSize();
5400 final long historyUsedSize = getHistoryUsedSize();
5401 if (startIteratingHistoryLocked()) {
5402 try {
5403 pw.print("Battery History (");
5404 pw.print((100*historyUsedSize)/historyTotalSize);
5405 pw.print("% used, ");
5406 printSizeValue(pw, historyUsedSize);
5407 pw.print(" used of ");
5408 printSizeValue(pw, historyTotalSize);
5409 pw.print(", ");
5410 pw.print(getHistoryStringPoolSize());
5411 pw.print(" strings using ");
5412 printSizeValue(pw, getHistoryStringPoolBytes());
5413 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07005414 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005415 pw.println();
5416 } finally {
5417 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005418 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005419 }
5420
5421 if (startIteratingOldHistoryLocked()) {
5422 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005423 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005424 pw.println("Old battery History:");
5425 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005426 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005427 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005428 if (baseTime < 0) {
5429 baseTime = rec.time;
5430 }
5431 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005432 }
5433 pw.println();
5434 } finally {
5435 finishIteratingOldHistoryLocked();
5436 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005437 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005438 }
5439
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005440 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005441 return;
5442 }
5443
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005444 if (!filtering) {
5445 SparseArray<? extends Uid> uidStats = getUidStats();
5446 final int NU = uidStats.size();
5447 boolean didPid = false;
5448 long nowRealtime = SystemClock.elapsedRealtime();
5449 for (int i=0; i<NU; i++) {
5450 Uid uid = uidStats.valueAt(i);
5451 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
5452 if (pids != null) {
5453 for (int j=0; j<pids.size(); j++) {
5454 Uid.Pid pid = pids.valueAt(j);
5455 if (!didPid) {
5456 pw.println("Per-PID Stats:");
5457 didPid = true;
5458 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005459 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
5460 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005461 pw.print(" PID "); pw.print(pids.keyAt(j));
5462 pw.print(" wake time: ");
5463 TimeUtils.formatDuration(time, pw);
5464 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005465 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005466 }
5467 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005468 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005469 pw.println();
5470 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005471 }
5472
5473 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005474 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
5475 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005476 long timeRemaining = computeBatteryTimeRemaining(
5477 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005478 if (timeRemaining >= 0) {
5479 pw.print(" Estimated discharge time remaining: ");
5480 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5481 pw.println();
5482 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005483 final LevelStepTracker steps = getDischargeLevelStepTracker();
5484 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5485 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
5486 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5487 STEP_LEVEL_MODE_VALUES[i], null));
5488 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005489 pw.println();
5490 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005491 if (dumpDurationSteps(pw, " ", "Charge step durations:",
5492 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005493 long timeRemaining = computeChargeTimeRemaining(
5494 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005495 if (timeRemaining >= 0) {
5496 pw.print(" Estimated charge time remaining: ");
5497 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5498 pw.println();
5499 }
5500 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005501 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005502 }
5503 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
5504 pw.println("Daily stats:");
5505 pw.print(" Current start time: ");
5506 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5507 getCurrentDailyStartTime()).toString());
5508 pw.print(" Next min deadline: ");
5509 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5510 getNextMinDailyDeadline()).toString());
5511 pw.print(" Next max deadline: ");
5512 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5513 getNextMaxDailyDeadline()).toString());
5514 StringBuilder sb = new StringBuilder(64);
5515 int[] outInt = new int[1];
5516 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
5517 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005518 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
5519 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005520 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005521 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
5522 dsteps, false)) {
5523 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5524 sb, outInt);
5525 }
5526 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
5527 csteps, false)) {
5528 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5529 sb, outInt);
5530 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005531 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005532 } else {
5533 pw.println(" Current daily steps:");
5534 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5535 sb, outInt);
5536 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5537 sb, outInt);
5538 }
5539 }
5540 DailyItem dit;
5541 int curIndex = 0;
5542 while ((dit=getDailyItemLocked(curIndex)) != null) {
5543 curIndex++;
5544 if ((flags&DUMP_DAILY_ONLY) != 0) {
5545 pw.println();
5546 }
5547 pw.print(" Daily from ");
5548 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
5549 pw.print(" to ");
5550 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
5551 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005552 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005553 if (dumpDurationSteps(pw, " ",
5554 " Discharge step durations:", dit.mDischargeSteps, false)) {
5555 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5556 sb, outInt);
5557 }
5558 if (dumpDurationSteps(pw, " ",
5559 " Charge step durations:", dit.mChargeSteps, false)) {
5560 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5561 sb, outInt);
5562 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005563 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005564 } else {
5565 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5566 sb, outInt);
5567 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5568 sb, outInt);
5569 }
5570 }
5571 pw.println();
5572 }
5573 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005574 pw.println("Statistics since last charge:");
5575 pw.println(" System starts: " + getStartCount()
5576 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07005577 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
5578 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005579 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005581 }
5582
5583 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005584 public void dumpCheckinLocked(Context context, PrintWriter pw,
5585 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005586 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005587
5588 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005589 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
5590 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005591
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005592 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
5593
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005594 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005595 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005596
5597 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07005598 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005599 try {
5600 for (int i=0; i<getHistoryStringPoolSize(); i++) {
5601 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5602 pw.print(HISTORY_STRING_POOL); pw.print(',');
5603 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005604 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005605 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005606 pw.print(",\"");
5607 String str = getHistoryTagPoolString(i);
5608 str = str.replace("\\", "\\\\");
5609 str = str.replace("\"", "\\\"");
5610 pw.print(str);
5611 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005612 pw.println();
5613 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005614 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005615 } finally {
5616 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08005617 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005618 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005619 }
5620
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005621 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005622 return;
5623 }
5624
Dianne Hackborne4a59512010-12-07 11:08:07 -08005625 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005626 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08005627 for (int i=0; i<apps.size(); i++) {
5628 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005629 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
5630 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08005631 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005632 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
5633 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005634 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005635 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005636 }
5637 SparseArray<? extends Uid> uidStats = getUidStats();
5638 final int NU = uidStats.size();
5639 String[] lineArgs = new String[2];
5640 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005641 int uid = UserHandle.getAppId(uidStats.keyAt(i));
5642 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
5643 if (pkgs != null && !pkgs.second.value) {
5644 pkgs.second.value = true;
5645 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08005646 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005647 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005648 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
5649 (Object[])lineArgs);
5650 }
5651 }
5652 }
5653 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005654 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005655 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005656 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07005657 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005658 if (timeRemaining >= 0) {
5659 lineArgs[0] = Long.toString(timeRemaining);
5660 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
5661 (Object[])lineArgs);
5662 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005663 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07005664 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005665 if (timeRemaining >= 0) {
5666 lineArgs[0] = Long.toString(timeRemaining);
5667 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
5668 (Object[])lineArgs);
5669 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005670 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
5671 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005674}