blob: 29884b132e5aacd3517396d19458bc7220788317 [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",
Kweku Adams134c59b2017-03-08 16:48:01 -08002045 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2046 "tmpwhitelist", "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;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002189 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2190 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2191 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2192 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2193 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002194
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002195 public abstract long getNetworkActivityBytes(int type, int which);
2196 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002197
Adam Lesinskie08af192015-03-25 16:42:59 -07002198 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002199 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002200 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002201 * actual power data.
2202 */
2203 public abstract boolean hasWifiActivityReporting();
2204
2205 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002206 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2207 * in various radio controller states, such as transmit, receive, and idle.
2208 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002209 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002210 public abstract ControllerActivityCounter getWifiControllerActivity();
2211
2212 /**
2213 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2214 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2215 * actual power data.
2216 */
2217 public abstract boolean hasBluetoothActivityReporting();
2218
2219 /**
2220 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2221 * in various radio controller states, such as transmit, receive, and idle.
2222 * @return non-null {@link ControllerActivityCounter}
2223 */
2224 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2225
2226 /**
2227 * Returns true if the BatteryStats object has detailed modem power reports.
2228 * When true, calling {@link #getModemControllerActivity()} will yield the
2229 * actual power data.
2230 */
2231 public abstract boolean hasModemActivityReporting();
2232
2233 /**
2234 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2235 * in various radio controller states, such as transmit, receive, and idle.
2236 * @return non-null {@link ControllerActivityCounter}
2237 */
2238 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002239
The Android Open Source Project10592532009-03-18 17:39:46 -07002240 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002241 * Return the wall clock time when battery stats data collection started.
2242 */
2243 public abstract long getStartClockTime();
2244
2245 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002246 * Return platform version tag that we were running in when the battery stats started.
2247 */
2248 public abstract String getStartPlatformVersion();
2249
2250 /**
2251 * Return platform version tag that we were running in when the battery stats ended.
2252 */
2253 public abstract String getEndPlatformVersion();
2254
2255 /**
2256 * Return the internal version code of the parcelled format.
2257 */
2258 public abstract int getParcelVersion();
2259
2260 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 * Return whether we are currently running on battery.
2262 */
2263 public abstract boolean getIsOnBattery();
2264
2265 /**
2266 * Returns a SparseArray containing the statistics for each uid.
2267 */
2268 public abstract SparseArray<? extends Uid> getUidStats();
2269
2270 /**
2271 * Returns the current battery uptime in microseconds.
2272 *
2273 * @param curTime the amount of elapsed realtime in microseconds.
2274 */
2275 public abstract long getBatteryUptime(long curTime);
2276
2277 /**
2278 * Returns the current battery realtime in microseconds.
2279 *
2280 * @param curTime the amount of elapsed realtime in microseconds.
2281 */
2282 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07002283
2284 /**
Evan Millar633a1742009-04-02 16:36:33 -07002285 * Returns the battery percentage level at the last time the device was unplugged from power, or
2286 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002287 */
Evan Millar633a1742009-04-02 16:36:33 -07002288 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07002289
2290 /**
Evan Millar633a1742009-04-02 16:36:33 -07002291 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2292 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002293 */
Evan Millar633a1742009-04-02 16:36:33 -07002294 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295
2296 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002297 * Get the amount the battery has discharged since the stats were
2298 * last reset after charging, as a lower-end approximation.
2299 */
2300 public abstract int getLowDischargeAmountSinceCharge();
2301
2302 /**
2303 * Get the amount the battery has discharged since the stats were
2304 * last reset after charging, as an upper-end approximation.
2305 */
2306 public abstract int getHighDischargeAmountSinceCharge();
2307
2308 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002309 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2310 */
2311 public abstract int getDischargeAmount(int which);
2312
2313 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002314 * Get the amount the battery has discharged while the screen was on,
2315 * since the last time power was unplugged.
2316 */
2317 public abstract int getDischargeAmountScreenOn();
2318
2319 /**
2320 * Get the amount the battery has discharged while the screen was on,
2321 * since the last time the device was charged.
2322 */
2323 public abstract int getDischargeAmountScreenOnSinceCharge();
2324
2325 /**
2326 * Get the amount the battery has discharged while the screen was off,
2327 * since the last time power was unplugged.
2328 */
2329 public abstract int getDischargeAmountScreenOff();
2330
2331 /**
2332 * Get the amount the battery has discharged while the screen was off,
2333 * since the last time the device was charged.
2334 */
2335 public abstract int getDischargeAmountScreenOffSinceCharge();
2336
2337 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 * Returns the total, last, or current battery uptime in microseconds.
2339 *
2340 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002341 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 */
2343 public abstract long computeBatteryUptime(long curTime, int which);
2344
2345 /**
2346 * Returns the total, last, or current battery realtime in microseconds.
2347 *
2348 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002349 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 */
2351 public abstract long computeBatteryRealtime(long curTime, int which);
2352
2353 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002354 * Returns the total, last, or current battery screen off uptime in microseconds.
2355 *
2356 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002357 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002358 */
2359 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2360
2361 /**
2362 * Returns the total, last, or current battery screen off realtime in microseconds.
2363 *
2364 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002365 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002366 */
2367 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2368
2369 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 * Returns the total, last, or current uptime in microseconds.
2371 *
2372 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002373 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 */
2375 public abstract long computeUptime(long curTime, int which);
2376
2377 /**
2378 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002379 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002381 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 */
2383 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002384
2385 /**
2386 * Compute an approximation for how much run time (in microseconds) is remaining on
2387 * the battery. Returns -1 if no time can be computed: either there is not
2388 * enough current data to make a decision, or the battery is currently
2389 * charging.
2390 *
2391 * @param curTime The current elepsed realtime in microseconds.
2392 */
2393 public abstract long computeBatteryTimeRemaining(long curTime);
2394
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002395 // The part of a step duration that is the actual time.
2396 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2397
2398 // Bits in a step duration that are the new battery level we are at.
2399 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002400 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002401
2402 // Bits in a step duration that are the initial mode we were in at that step.
2403 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002404 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002405
2406 // Bits in a step duration that indicate which modes changed during that step.
2407 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002408 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002409
2410 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2411 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2412
Santos Cordone94f0502017-02-24 12:31:20 -08002413 // The largest value for screen state that is tracked in battery states. Any values above
2414 // this should be mapped back to one of the tracked values before being tracked here.
2415 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2416
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002417 // Step duration mode: power save is on.
2418 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2419
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002420 // Step duration mode: device is currently in idle mode.
2421 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2422
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002423 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2424 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002425 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2426 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002427 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2428 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2429 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2430 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2431 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002432 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2433 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002434 };
2435 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2436 (Display.STATE_OFF-1),
2437 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002438 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002439 (Display.STATE_ON-1),
2440 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2441 (Display.STATE_DOZE-1),
2442 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2443 (Display.STATE_DOZE_SUSPEND-1),
2444 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002445 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002446 };
2447 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2448 "screen off",
2449 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002450 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002451 "screen on",
2452 "screen on power save",
2453 "screen doze",
2454 "screen doze power save",
2455 "screen doze-suspend",
2456 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002457 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002458 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002459
2460 /**
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002461 * Return the counter keeping track of the amount of battery discharge while the screen was off,
2462 * measured in micro-Ampere-hours. This will be non-zero only if the device's battery has
2463 * a coulomb counter.
2464 */
2465 public abstract LongCounter getDischargeScreenOffCoulombCounter();
2466
2467 /**
2468 * Return the counter keeping track of the amount of battery discharge measured in
2469 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2470 * a coulomb counter.
2471 */
2472 public abstract LongCounter getDischargeCoulombCounter();
2473
2474 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002475 * Returns the estimated real battery capacity, which may be less than the capacity
2476 * declared by the PowerProfile.
2477 * @return The estimated battery capacity in mAh.
2478 */
2479 public abstract int getEstimatedBatteryCapacity();
2480
2481 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002482 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002483 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002484 public abstract LevelStepTracker getDischargeLevelStepTracker();
2485
2486 /**
2487 * Return the array of daily discharge step durations.
2488 */
2489 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002490
2491 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002492 * Compute an approximation for how much time (in microseconds) remains until the battery
2493 * is fully charged. Returns -1 if no time can be computed: either there is not
2494 * enough current data to make a decision, or the battery is currently
2495 * discharging.
2496 *
2497 * @param curTime The current elepsed realtime in microseconds.
2498 */
2499 public abstract long computeChargeTimeRemaining(long curTime);
2500
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002501 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002502 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002503 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002504 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002505
2506 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002507 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002508 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002509 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002510
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002511 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2512
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002513 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002514
Evan Millarc64edde2009-04-18 12:26:32 -07002515 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516
James Carr2dd7e5e2016-07-20 18:48:39 -07002517 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2518
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002519 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2520
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002521 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 long days = seconds / (60 * 60 * 24);
2523 if (days != 0) {
2524 out.append(days);
2525 out.append("d ");
2526 }
2527 long used = days * 60 * 60 * 24;
2528
2529 long hours = (seconds - used) / (60 * 60);
2530 if (hours != 0 || used != 0) {
2531 out.append(hours);
2532 out.append("h ");
2533 }
2534 used += hours * 60 * 60;
2535
2536 long mins = (seconds-used) / 60;
2537 if (mins != 0 || used != 0) {
2538 out.append(mins);
2539 out.append("m ");
2540 }
2541 used += mins * 60;
2542
2543 if (seconds != 0 || used != 0) {
2544 out.append(seconds-used);
2545 out.append("s ");
2546 }
2547 }
2548
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002549 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002551 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 sb.append(time - (sec * 1000));
2553 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 }
2555
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002556 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002557 long sec = time / 1000;
2558 formatTimeRaw(sb, sec);
2559 sb.append(time - (sec * 1000));
2560 sb.append("ms");
2561 }
2562
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002563 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002565 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 }
2567 float perc = ((float)num) / ((float)den) * 100;
2568 mFormatBuilder.setLength(0);
2569 mFormatter.format("%.1f%%", perc);
2570 return mFormatBuilder.toString();
2571 }
2572
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002573 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002574 mFormatBuilder.setLength(0);
2575
2576 if (bytes < BYTES_PER_KB) {
2577 return bytes + "B";
2578 } else if (bytes < BYTES_PER_MB) {
2579 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2580 return mFormatBuilder.toString();
2581 } else if (bytes < BYTES_PER_GB){
2582 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2583 return mFormatBuilder.toString();
2584 } else {
2585 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2586 return mFormatBuilder.toString();
2587 }
2588 }
2589
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002590 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002591 if (timer != null) {
2592 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002593 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002594 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2595 return totalTimeMillis;
2596 }
2597 return 0;
2598 }
2599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 /**
2601 *
2602 * @param sb a StringBuilder object.
2603 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002604 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002606 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 * @param linePrefix a String to be prepended to each line of output.
2608 * @return the line prefix
2609 */
2610 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002611 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002614 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002615
Evan Millarc64edde2009-04-18 12:26:32 -07002616 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 if (totalTimeMillis != 0) {
2618 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002619 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002620 if (name != null) {
2621 sb.append(name);
2622 sb.append(' ');
2623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 sb.append('(');
2625 sb.append(count);
2626 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002627 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2628 if (maxDurationMs >= 0) {
2629 sb.append(" max=");
2630 sb.append(maxDurationMs);
2631 }
2632 if (timer.isRunningLocked()) {
2633 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2634 if (currentMs >= 0) {
2635 sb.append(" (running for ");
2636 sb.append(currentMs);
2637 sb.append("ms)");
2638 } else {
2639 sb.append(" (running)");
2640 }
2641 }
2642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 return ", ";
2644 }
2645 }
2646 return linePrefix;
2647 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002648
2649 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07002650 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002651 *
2652 * @param pw a PrintWriter object to print to.
2653 * @param sb a StringBuilder object.
2654 * @param timer a Timer object contining the wakelock times.
2655 * @param rawRealtime the current on-battery time in microseconds.
2656 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2657 * @param prefix a String to be prepended to each line of output.
2658 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002659 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002660 */
2661 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07002662 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002663 if (timer != null) {
2664 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07002665 final long totalTimeMs = (timer.getTotalTimeLocked(
2666 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002667 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002668 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002669 sb.setLength(0);
2670 sb.append(prefix);
2671 sb.append(" ");
2672 sb.append(type);
2673 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002674 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002675 sb.append("realtime (");
2676 sb.append(count);
2677 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002678 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
2679 if (maxDurationMs >= 0) {
2680 sb.append(" max=");
2681 sb.append(maxDurationMs);
2682 }
2683 if (timer.isRunningLocked()) {
2684 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
2685 if (currentMs >= 0) {
2686 sb.append(" (running for ");
2687 sb.append(currentMs);
2688 sb.append("ms)");
2689 } else {
2690 sb.append(" (running)");
2691 }
2692 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002693 pw.println(sb.toString());
2694 return true;
2695 }
2696 }
2697 return false;
2698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699
2700 /**
2701 * Checkin version of wakelock printer. Prints simple comma-separated list.
2702 *
2703 * @param sb a StringBuilder object.
2704 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002705 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002707 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 * @param linePrefix a String to be prepended to each line of output.
2709 * @return the line prefix
2710 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002711 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2712 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 long totalTimeMicros = 0;
2714 int count = 0;
Joe Onorato92fd23f2016-07-25 11:18:42 -07002715 long max = -1;
2716 long current = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002718 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07002719 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002720 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2721 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 }
2723 sb.append(linePrefix);
2724 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2725 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002726 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002728 sb.append(',');
2729 sb.append(current);
2730 sb.append(',');
2731 sb.append(max);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 return ",";
2733 }
2734
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002735 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
2736 String type) {
2737 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2738 pw.print(',');
2739 pw.print(uid);
2740 pw.print(',');
2741 pw.print(category);
2742 pw.print(',');
2743 pw.print(type);
2744 }
2745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 /**
2747 * Dump a comma-separated line of values for terse checkin mode.
2748 *
2749 * @param pw the PageWriter to dump log to
2750 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2751 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2752 * @param args type-dependent data arguments
2753 */
2754 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
2755 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002756 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002757 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002758 pw.print(',');
2759 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002761 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002763
2764 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002765 * Dump a given timer stat for terse checkin mode.
2766 *
2767 * @param pw the PageWriter to dump log to
2768 * @param uid the UID to log
2769 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2770 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2771 * @param timer a {@link Timer} to dump stats for
2772 * @param rawRealtime the current elapsed realtime of the system in microseconds
2773 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2774 */
2775 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2776 Timer timer, long rawRealtime, int which) {
2777 if (timer != null) {
2778 // Convert from microseconds to milliseconds with rounding
2779 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2780 / 1000;
2781 final int count = timer.getCountLocked(which);
2782 if (totalTime != 0) {
2783 dumpLine(pw, uid, category, type, totalTime, count);
2784 }
2785 }
2786 }
2787
2788 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002789 * Checks if the ControllerActivityCounter has any data worth dumping.
2790 */
2791 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
2792 if (counter == null) {
2793 return false;
2794 }
2795
2796 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
2797 || counter.getRxTimeCounter().getCountLocked(which) != 0
2798 || counter.getPowerCounter().getCountLocked(which) != 0) {
2799 return true;
2800 }
2801
2802 for (LongCounter c : counter.getTxTimeCounters()) {
2803 if (c.getCountLocked(which) != 0) {
2804 return true;
2805 }
2806 }
2807 return false;
2808 }
2809
2810 /**
2811 * Dumps the ControllerActivityCounter if it has any data worth dumping.
2812 * The order of the arguments in the final check in line is:
2813 *
2814 * idle, rx, power, tx...
2815 *
2816 * where tx... is one or more transmit level times.
2817 */
2818 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
2819 String type,
2820 ControllerActivityCounter counter,
2821 int which) {
2822 if (!controllerActivityHasData(counter, which)) {
2823 return;
2824 }
2825
2826 dumpLineHeader(pw, uid, category, type);
2827 pw.print(",");
2828 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
2829 pw.print(",");
2830 pw.print(counter.getRxTimeCounter().getCountLocked(which));
2831 pw.print(",");
2832 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
2833 for (LongCounter c : counter.getTxTimeCounters()) {
2834 pw.print(",");
2835 pw.print(c.getCountLocked(which));
2836 }
2837 pw.println();
2838 }
2839
2840 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
2841 String prefix, String controllerName,
2842 ControllerActivityCounter counter,
2843 int which) {
2844 if (controllerActivityHasData(counter, which)) {
2845 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
2846 }
2847 }
2848
2849 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
2850 String controllerName,
2851 ControllerActivityCounter counter, int which) {
2852 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
2853 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
2854 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
2855 long totalTxTimeMs = 0;
2856 for (LongCounter txState : counter.getTxTimeCounters()) {
2857 totalTxTimeMs += txState.getCountLocked(which);
2858 }
2859
2860 final long totalTimeMs = idleTimeMs + rxTimeMs + totalTxTimeMs;
2861
2862 sb.setLength(0);
2863 sb.append(prefix);
2864 sb.append(" ");
2865 sb.append(controllerName);
2866 sb.append(" Idle time: ");
2867 formatTimeMs(sb, idleTimeMs);
2868 sb.append("(");
2869 sb.append(formatRatioLocked(idleTimeMs, totalTimeMs));
2870 sb.append(")");
2871 pw.println(sb.toString());
2872
2873 sb.setLength(0);
2874 sb.append(prefix);
2875 sb.append(" ");
2876 sb.append(controllerName);
2877 sb.append(" Rx time: ");
2878 formatTimeMs(sb, rxTimeMs);
2879 sb.append("(");
2880 sb.append(formatRatioLocked(rxTimeMs, totalTimeMs));
2881 sb.append(")");
2882 pw.println(sb.toString());
2883
2884 sb.setLength(0);
2885 sb.append(prefix);
2886 sb.append(" ");
2887 sb.append(controllerName);
2888 sb.append(" Tx time: ");
2889 formatTimeMs(sb, totalTxTimeMs);
2890 sb.append("(");
2891 sb.append(formatRatioLocked(totalTxTimeMs, totalTimeMs));
2892 sb.append(")");
2893 pw.println(sb.toString());
2894
2895 final int numTxLvls = counter.getTxTimeCounters().length;
2896 if (numTxLvls > 1) {
2897 for (int lvl = 0; lvl < numTxLvls; lvl++) {
2898 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
2899 sb.setLength(0);
2900 sb.append(prefix);
2901 sb.append(" [");
2902 sb.append(lvl);
2903 sb.append("] ");
2904 formatTimeMs(sb, txLvlTimeMs);
2905 sb.append("(");
2906 sb.append(formatRatioLocked(txLvlTimeMs, totalTxTimeMs));
2907 sb.append(")");
2908 pw.println(sb.toString());
2909 }
2910 }
2911
2912 sb.setLength(0);
2913 sb.append(prefix);
2914 sb.append(" ");
2915 sb.append(controllerName);
2916 sb.append(" Power drain: ").append(
2917 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
2918 sb.append("mAh");
2919 pw.println(sb.toString());
2920 }
2921
2922 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07002923 * Temporary for settings.
2924 */
2925 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
2926 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2927 }
2928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 /**
2930 * Checkin server version of dump to produce more compact, computer-readable log.
2931 *
2932 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07002934 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
2935 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2937 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2938 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2940 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002941 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2942 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2943 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 final long totalRealtime = computeRealtime(rawRealtime, which);
2945 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002946 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002947 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002948 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002949 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
2950 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002951 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002952 rawRealtime, which);
2953 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
2954 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002955 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002956 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002957 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002958 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002959 final long dischargeCount = getDischargeCoulombCounter().getCountLocked(which);
2960 final long dischargeScreenOffCount = getDischargeScreenOffCoulombCounter()
2961 .getCountLocked(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002962
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002963 final StringBuilder sb = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002965 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07002966 final int NU = uidStats.size();
2967
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002968 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 // Dump "battery" stat
2971 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002972 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07002973 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002974 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002975 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002976 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
2977 getEstimatedBatteryCapacity());
Adam Lesinski67c134f2016-06-10 15:15:08 -07002978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002980 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07002981 long fullWakeLockTimeTotal = 0;
2982 long partialWakeLockTimeTotal = 0;
2983
2984 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002985 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002986
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002987 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
2988 = u.getWakelockStats();
2989 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
2990 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07002991
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002992 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
2993 if (fullWakeTimer != null) {
2994 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
2995 which);
2996 }
2997
2998 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
2999 if (partialWakeTimer != null) {
3000 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3001 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003002 }
3003 }
3004 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003005
3006 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003007 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3008 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3009 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3010 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3011 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3012 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3013 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3014 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003015 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3016 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003017 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3018 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003019 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3020 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003021
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003022 // Dump Modem controller stats
3023 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3024 getModemControllerActivity(), which);
3025
Adam Lesinskie283d332015-04-16 12:29:25 -07003026 // Dump Wifi controller stats
3027 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3028 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003029 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003030 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003031
3032 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3033 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003034
3035 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003036 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3037 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003039 // Dump misc stats
3040 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003041 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003042 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003043 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003044 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003045 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003046 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3047 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003048 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003049 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3050 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3051 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3052 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003053 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003054
3055 // Dump screen brightness stats
3056 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3057 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003058 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003059 }
3060 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07003061
Dianne Hackborn627bba72009-03-24 22:32:56 -07003062 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003063 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3064 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003065 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003066 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003067 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003068 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003069 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003070 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003071 args[i] = getPhoneSignalStrengthCount(i, which);
3072 }
3073 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003074
Dianne Hackborn627bba72009-03-24 22:32:56 -07003075 // Dump network type stats
3076 args = new Object[NUM_DATA_CONNECTION_TYPES];
3077 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003078 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003079 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003080 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3081 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3082 args[i] = getPhoneDataConnectionCount(i, which);
3083 }
3084 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003085
3086 // Dump wifi state stats
3087 args = new Object[NUM_WIFI_STATES];
3088 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003089 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003090 }
3091 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3092 for (int i=0; i<NUM_WIFI_STATES; i++) {
3093 args[i] = getWifiStateCount(i, which);
3094 }
3095 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3096
Dianne Hackborn3251b902014-06-20 14:40:53 -07003097 // Dump wifi suppl state stats
3098 args = new Object[NUM_WIFI_SUPPL_STATES];
3099 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3100 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3101 }
3102 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3103 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3104 args[i] = getWifiSupplStateCount(i, which);
3105 }
3106 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3107
3108 // Dump wifi signal strength stats
3109 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3110 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3111 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3112 }
3113 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3114 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3115 args[i] = getWifiSignalStrengthCount(i, which);
3116 }
3117 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3118
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003119 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003120 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003121 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003124 if (which == STATS_SINCE_UNPLUGGED) {
3125 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3126 getDischargeStartLevel()-getDischargeCurrentLevel(),
3127 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003128 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
3129 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003130 } else {
3131 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3132 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003133 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003134 getDischargeAmountScreenOffSinceCharge(),
3135 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003136 }
3137
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003138 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003139 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003140 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003141 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003142 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003143 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003144 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3145 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003146 }
Evan Millarc64edde2009-04-18 12:26:32 -07003147 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003148 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003149 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003150 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3151 // Not doing the regular wake lock formatting to remain compatible
3152 // with the old checkin format.
3153 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3154 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003155 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003156 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003157 }
3158 }
Evan Millarc64edde2009-04-18 12:26:32 -07003159 }
3160
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003161 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003162 helper.create(this);
3163 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003164 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003165 if (sippers != null && sippers.size() > 0) {
3166 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3167 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003168 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003169 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3170 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
3171 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003172 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003173 int uid = 0;
3174 String label;
3175 switch (bs.drainType) {
3176 case IDLE:
3177 label="idle";
3178 break;
3179 case CELL:
3180 label="cell";
3181 break;
3182 case PHONE:
3183 label="phone";
3184 break;
3185 case WIFI:
3186 label="wifi";
3187 break;
3188 case BLUETOOTH:
3189 label="blue";
3190 break;
3191 case SCREEN:
3192 label="scrn";
3193 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003194 case FLASHLIGHT:
3195 label="flashlight";
3196 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003197 case APP:
3198 uid = bs.uidObj.getUid();
3199 label = "uid";
3200 break;
3201 case USER:
3202 uid = UserHandle.getUid(bs.userId, 0);
3203 label = "user";
3204 break;
3205 case UNACCOUNTED:
3206 label = "unacc";
3207 break;
3208 case OVERCOUNTED:
3209 label = "over";
3210 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003211 case CAMERA:
3212 label = "camera";
3213 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003214 default:
3215 label = "???";
3216 }
3217 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Adam Lesinskie08af192015-03-25 16:42:59 -07003218 BatteryStatsHelper.makemAh(bs.totalPowerMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003219 }
3220 }
3221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 for (int iu = 0; iu < NU; iu++) {
3223 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003224 if (reqUid >= 0 && uid != reqUid) {
3225 continue;
3226 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003227 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003230 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3231 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3232 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3233 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3234 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3235 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3236 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3237 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003238 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003239 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3240 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003241 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003242 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3243 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003244 // Background data transfers
3245 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3246 which);
3247 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3248 which);
3249 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3250 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3251 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3252 which);
3253 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3254 which);
3255 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3256 which);
3257 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3258 which);
3259
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003260 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3261 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003262 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003263 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3264 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3265 || wifiBytesBgTx > 0
3266 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3267 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003268 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3269 wifiBytesRx, wifiBytesTx,
3270 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003271 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003272 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003273 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3274 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3275 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3276 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003277 }
3278
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003279 // Dump modem controller data, per UID.
3280 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3281 u.getModemControllerActivity(), which);
3282
3283 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003284 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3285 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3286 final int wifiScanCount = u.getWifiScanCount(which);
3287 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003288 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003289 || uidWifiRunningTime != 0) {
3290 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3291 uidWifiRunningTime, wifiScanCount,
Kweku Adams5b2747e2016-04-25 10:44:05 -07003292 /* legacy fields follow, keep at 0 */ 0, 0, 0);
The Android Open Source Project10592532009-03-18 17:39:46 -07003293 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003294
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003295 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3296 u.getWifiControllerActivity(), which);
3297
Adam Lesinskiba9d8932016-04-12 12:06:03 -07003298 dumpTimer(pw, uid, category, BLUETOOTH_MISC_DATA, u.getBluetoothScanTimer(),
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003299 rawRealtime, which);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003300
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003301 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3302 u.getBluetoothControllerActivity(), which);
3303
Dianne Hackborn617f8772009-03-31 15:04:46 -07003304 if (u.hasUserActivity()) {
3305 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3306 boolean hasData = false;
3307 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3308 int val = u.getUserActivityCount(i, which);
3309 args[i] = val;
3310 if (val != 0) hasData = true;
3311 }
3312 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003313 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003314 }
3315 }
3316
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003317 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3318 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3319 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3320 String linePrefix = "";
3321 sb.setLength(0);
3322 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3323 rawRealtime, "f", which, linePrefix);
3324 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
3325 rawRealtime, "p", which, linePrefix);
3326 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3327 rawRealtime, "w", which, linePrefix);
3328
3329 // Only log if we had at lease one wakelock...
3330 if (sb.length() > 0) {
3331 String name = wakelocks.keyAt(iw);
3332 if (name.indexOf(',') >= 0) {
3333 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003335 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 }
3337 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003338
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003339 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3340 for (int isy=syncs.size()-1; isy>=0; isy--) {
3341 final Timer timer = syncs.valueAt(isy);
3342 // Convert from microseconds to milliseconds with rounding
3343 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3344 final int count = timer.getCountLocked(which);
3345 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003346 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
3347 totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003348 }
3349 }
3350
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003351 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3352 for (int ij=jobs.size()-1; ij>=0; ij--) {
3353 final Timer timer = jobs.valueAt(ij);
3354 // Convert from microseconds to milliseconds with rounding
3355 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3356 final int count = timer.getCountLocked(which);
3357 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003358 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
3359 totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003360 }
3361 }
3362
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003363 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3364 rawRealtime, which);
3365 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3366 rawRealtime, which);
3367 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3368 rawRealtime, which);
3369 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3370 rawRealtime, which);
3371
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003372 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3373 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003374 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003375 final Uid.Sensor se = sensors.valueAt(ise);
3376 final int sensorNumber = sensors.keyAt(ise);
3377 final Timer timer = se.getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -08003378 final Counter bgCounter = se.getSensorBgCount();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003379 if (timer != null) {
3380 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003381 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3382 / 1000;
3383 final int count = timer.getCountLocked(which);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08003384 final int bgCount = bgCounter != null ? bgCounter.getCountLocked(which) : 0;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003385 if (totalTime != 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08003386 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count,
3387 bgCount);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003389 }
3390 }
3391
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003392 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3393 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003394
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003395 dumpTimer(pw, uid, category, FOREGROUND_DATA, u.getForegroundActivityTimer(),
3396 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003397
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003398 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003399 long totalStateTime = 0;
3400 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003401 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3402 totalStateTime += time;
3403 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003404 }
3405 if (totalStateTime > 0) {
3406 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3407 }
3408
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003409 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3410 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
3411 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
3412 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
3413 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
3414 powerCpuMaUs / 1000);
3415 }
3416
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003417 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3418 = u.getProcessStats();
3419 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3420 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003421
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003422 final long userMillis = ps.getUserTime(which);
3423 final long systemMillis = ps.getSystemTime(which);
3424 final long foregroundMillis = ps.getForegroundTime(which);
3425 final int starts = ps.getStarts(which);
3426 final int numCrashes = ps.getNumCrashes(which);
3427 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003428
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003429 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3430 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003431 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
3432 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 }
3434 }
3435
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003436 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3437 = u.getPackageStats();
3438 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3439 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3440 int wakeups = 0;
3441 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3442 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07003443 int count = alarms.valueAt(iwa).getCountLocked(which);
3444 wakeups += count;
3445 String name = alarms.keyAt(iwa).replace(',', '_');
3446 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003447 }
3448 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3449 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3450 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3451 final long startTime = ss.getStartTime(batteryUptime, which);
3452 final int starts = ss.getStarts(which);
3453 final int launches = ss.getLaunches(which);
3454 if (startTime != 0 || starts != 0 || launches != 0) {
3455 dumpLine(pw, uid, category, APK_DATA,
3456 wakeups, // wakeup alarms
3457 packageStats.keyAt(ipkg), // Apk
3458 serviceStats.keyAt(isvc), // service
3459 startTime / 1000, // time spent started, in ms
3460 starts,
3461 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003462 }
3463 }
3464 }
3465 }
3466 }
3467
Dianne Hackborn81038902012-11-26 17:04:09 -08003468 static final class TimerEntry {
3469 final String mName;
3470 final int mId;
3471 final BatteryStats.Timer mTimer;
3472 final long mTime;
3473 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3474 mName = name;
3475 mId = id;
3476 mTimer = timer;
3477 mTime = time;
3478 }
3479 }
3480
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003481 private void printmAh(PrintWriter printer, double power) {
3482 printer.print(BatteryStatsHelper.makemAh(power));
3483 }
3484
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003485 private void printmAh(StringBuilder sb, double power) {
3486 sb.append(BatteryStatsHelper.makemAh(power));
3487 }
3488
Dianne Hackbornd953c532014-08-16 18:17:38 -07003489 /**
3490 * Temporary for settings.
3491 */
3492 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3493 int reqUid) {
3494 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3495 }
3496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003498 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003499 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003500 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3501 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
3502 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503
3504 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3505 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3506 final long totalRealtime = computeRealtime(rawRealtime, which);
3507 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003508 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3509 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3510 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003511 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3512 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003513
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003514 final StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07003515
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003516 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003517 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003519 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
3520 if (estimatedBatteryCapacity > 0) {
3521 sb.setLength(0);
3522 sb.append(prefix);
3523 sb.append(" Estimated battery capacity: ");
3524 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
3525 sb.append(" mAh");
3526 pw.println(sb.toString());
3527 }
3528
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003529 sb.setLength(0);
3530 sb.append(prefix);
3531 sb.append(" Time on battery: ");
3532 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3533 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3534 sb.append(") realtime, ");
3535 formatTimeMs(sb, whichBatteryUptime / 1000);
3536 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3537 sb.append(") uptime");
3538 pw.println(sb.toString());
3539 sb.setLength(0);
3540 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003541 sb.append(" Time on battery screen off: ");
3542 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3543 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3544 sb.append(") realtime, ");
3545 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3546 sb.append("(");
3547 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3548 sb.append(") uptime");
3549 pw.println(sb.toString());
3550 sb.setLength(0);
3551 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003552 sb.append(" Total run time: ");
3553 formatTimeMs(sb, totalRealtime / 1000);
3554 sb.append("realtime, ");
3555 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003556 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003557 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003558 if (batteryTimeRemaining >= 0) {
3559 sb.setLength(0);
3560 sb.append(prefix);
3561 sb.append(" Battery time remaining: ");
3562 formatTimeMs(sb, batteryTimeRemaining / 1000);
3563 pw.println(sb.toString());
3564 }
3565 if (chargeTimeRemaining >= 0) {
3566 sb.setLength(0);
3567 sb.append(prefix);
3568 sb.append(" Charge time remaining: ");
3569 formatTimeMs(sb, chargeTimeRemaining / 1000);
3570 pw.println(sb.toString());
3571 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003572
3573 final LongCounter dischargeCounter = getDischargeCoulombCounter();
3574 final long dischargeCount = dischargeCounter.getCountLocked(which);
3575 if (dischargeCount >= 0) {
3576 sb.setLength(0);
3577 sb.append(prefix);
3578 sb.append(" Discharge: ");
3579 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
3580 sb.append(" mAh");
3581 pw.println(sb.toString());
3582 }
3583
3584 final LongCounter dischargeScreenOffCounter = getDischargeScreenOffCoulombCounter();
3585 final long dischargeScreenOffCount = dischargeScreenOffCounter.getCountLocked(which);
3586 if (dischargeScreenOffCount >= 0) {
3587 sb.setLength(0);
3588 sb.append(prefix);
3589 sb.append(" Screen off discharge: ");
3590 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
3591 sb.append(" mAh");
3592 pw.println(sb.toString());
3593 }
3594
3595 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
3596 if (dischargeScreenOnCount >= 0) {
3597 sb.setLength(0);
3598 sb.append(prefix);
3599 sb.append(" Screen on discharge: ");
3600 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
3601 sb.append(" mAh");
3602 pw.println(sb.toString());
3603 }
3604
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003605 pw.print(" Start clock time: ");
3606 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3607
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003608 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003609 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003610 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003611 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3612 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003613 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003614 rawRealtime, which);
3615 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3616 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003617 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003618 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003619 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3620 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3621 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003622 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003623 sb.append(prefix);
3624 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3625 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003626 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003627 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3628 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003629 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003630 pw.println(sb.toString());
3631 sb.setLength(0);
3632 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003633 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003634 boolean didOne = false;
3635 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003636 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003637 if (time == 0) {
3638 continue;
3639 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003640 sb.append("\n ");
3641 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003642 didOne = true;
3643 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3644 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003645 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003646 sb.append("(");
3647 sb.append(formatRatioLocked(time, screenOnTime));
3648 sb.append(")");
3649 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003650 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003651 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003652 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003653 sb.setLength(0);
3654 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003655 sb.append(" Power save mode enabled: ");
3656 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003657 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003658 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003659 sb.append(")");
3660 pw.println(sb.toString());
3661 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003662 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003663 sb.setLength(0);
3664 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003665 sb.append(" Device light idling: ");
3666 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003667 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003668 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
3669 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003670 sb.append("x");
3671 pw.println(sb.toString());
3672 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003673 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003674 sb.setLength(0);
3675 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003676 sb.append(" Idle mode light time: ");
3677 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003678 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003679 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
3680 sb.append(") ");
3681 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003682 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003683 sb.append(" -- longest ");
3684 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
3685 pw.println(sb.toString());
3686 }
3687 if (deviceIdlingTime != 0) {
3688 sb.setLength(0);
3689 sb.append(prefix);
3690 sb.append(" Device full idling: ");
3691 formatTimeMs(sb, deviceIdlingTime / 1000);
3692 sb.append("(");
3693 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003694 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003695 sb.append("x");
3696 pw.println(sb.toString());
3697 }
3698 if (deviceIdleModeFullTime != 0) {
3699 sb.setLength(0);
3700 sb.append(prefix);
3701 sb.append(" Idle mode full time: ");
3702 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
3703 sb.append("(");
3704 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
3705 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003706 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003707 sb.append("x");
3708 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003709 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003710 pw.println(sb.toString());
3711 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003712 if (phoneOnTime != 0) {
3713 sb.setLength(0);
3714 sb.append(prefix);
3715 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3716 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003717 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003718 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003719 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003720 if (connChanges != 0) {
3721 pw.print(prefix);
3722 pw.print(" Connectivity changes: "); pw.println(connChanges);
3723 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003724
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003725 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003726 long fullWakeLockTimeTotalMicros = 0;
3727 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003728
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003729 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003730
Evan Millar22ac0432009-03-31 11:33:18 -07003731 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003732 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003733
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003734 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3735 = u.getWakelockStats();
3736 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3737 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003738
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003739 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3740 if (fullWakeTimer != null) {
3741 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3742 rawRealtime, which);
3743 }
3744
3745 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3746 if (partialWakeTimer != null) {
3747 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3748 rawRealtime, which);
3749 if (totalTimeMicros > 0) {
3750 if (reqUid < 0) {
3751 // Only show the ordered list of all wake
3752 // locks if the caller is not asking for data
3753 // about a specific uid.
3754 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3755 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003756 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003757 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003758 }
3759 }
3760 }
3761 }
3762
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003763 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3764 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3765 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3766 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3767 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3768 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3769 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3770 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003771 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3772 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003773
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003774 if (fullWakeLockTimeTotalMicros != 0) {
3775 sb.setLength(0);
3776 sb.append(prefix);
3777 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
3778 (fullWakeLockTimeTotalMicros + 500) / 1000);
3779 pw.println(sb.toString());
3780 }
3781
3782 if (partialWakeLockTimeTotalMicros != 0) {
3783 sb.setLength(0);
3784 sb.append(prefix);
3785 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
3786 (partialWakeLockTimeTotalMicros + 500) / 1000);
3787 pw.println(sb.toString());
3788 }
3789
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003790 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003791 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
3792 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
3793 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
3794 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003795 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003796 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07003797 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003798 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08003799 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003800 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003801 if (time == 0) {
3802 continue;
3803 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003804 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003805 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003806 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08003807 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003808 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003809 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003810 sb.append("(");
3811 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003812 sb.append(") ");
3813 sb.append(getPhoneSignalStrengthCount(i, which));
3814 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003815 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003816 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003817 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07003818
3819 sb.setLength(0);
3820 sb.append(prefix);
3821 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003822 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003823 pw.println(sb.toString());
3824
Dianne Hackborn627bba72009-03-24 22:32:56 -07003825 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003826 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003827 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003828 didOne = false;
3829 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003830 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003831 if (time == 0) {
3832 continue;
3833 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003834 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003835 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003836 didOne = true;
3837 sb.append(DATA_CONNECTION_NAMES[i]);
3838 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003839 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003840 sb.append("(");
3841 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003842 sb.append(") ");
3843 sb.append(getPhoneDataConnectionCount(i, which));
3844 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003845 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003846 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003847 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003848
3849 sb.setLength(0);
3850 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003851 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003852 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003853 formatTimeMs(sb, mobileActiveTime / 1000);
3854 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
3855 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
3856 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003857 pw.println(sb.toString());
3858
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003859 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
3860 if (mobileActiveUnknownTime != 0) {
3861 sb.setLength(0);
3862 sb.append(prefix);
3863 sb.append(" Mobile radio active unknown time: ");
3864 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
3865 sb.append("(");
3866 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
3867 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
3868 sb.append("x");
3869 pw.println(sb.toString());
3870 }
3871
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003872 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
3873 if (mobileActiveAdjustedTime != 0) {
3874 sb.setLength(0);
3875 sb.append(prefix);
3876 sb.append(" Mobile radio active adjusted time: ");
3877 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
3878 sb.append("(");
3879 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
3880 sb.append(")");
3881 pw.println(sb.toString());
3882 }
3883
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003884 printControllerActivity(pw, sb, prefix, "Radio", getModemControllerActivity(), which);
3885
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003886 pw.print(prefix);
3887 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
3888 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
3889 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
3890 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003891 sb.setLength(0);
3892 sb.append(prefix);
3893 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
3894 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
3895 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
3896 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003897 sb.append(")");
3898 pw.println(sb.toString());
3899
3900 sb.setLength(0);
3901 sb.append(prefix);
3902 sb.append(" Wifi states:");
3903 didOne = false;
3904 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003905 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003906 if (time == 0) {
3907 continue;
3908 }
3909 sb.append("\n ");
3910 didOne = true;
3911 sb.append(WIFI_STATE_NAMES[i]);
3912 sb.append(" ");
3913 formatTimeMs(sb, time/1000);
3914 sb.append("(");
3915 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3916 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07003917 sb.append(getWifiStateCount(i, which));
3918 sb.append("x");
3919 }
3920 if (!didOne) sb.append(" (no activity)");
3921 pw.println(sb.toString());
3922
3923 sb.setLength(0);
3924 sb.append(prefix);
3925 sb.append(" Wifi supplicant states:");
3926 didOne = false;
3927 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3928 final long time = getWifiSupplStateTime(i, rawRealtime, which);
3929 if (time == 0) {
3930 continue;
3931 }
3932 sb.append("\n ");
3933 didOne = true;
3934 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
3935 sb.append(" ");
3936 formatTimeMs(sb, time/1000);
3937 sb.append("(");
3938 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3939 sb.append(") ");
3940 sb.append(getWifiSupplStateCount(i, which));
3941 sb.append("x");
3942 }
3943 if (!didOne) sb.append(" (no activity)");
3944 pw.println(sb.toString());
3945
3946 sb.setLength(0);
3947 sb.append(prefix);
3948 sb.append(" Wifi signal levels:");
3949 didOne = false;
3950 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3951 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
3952 if (time == 0) {
3953 continue;
3954 }
3955 sb.append("\n ");
3956 sb.append(prefix);
3957 didOne = true;
3958 sb.append("level(");
3959 sb.append(i);
3960 sb.append(") ");
3961 formatTimeMs(sb, time/1000);
3962 sb.append("(");
3963 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3964 sb.append(") ");
3965 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003966 sb.append("x");
3967 }
3968 if (!didOne) sb.append(" (no activity)");
3969 pw.println(sb.toString());
3970
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003971 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07003972
Adam Lesinski50e47602015-12-04 17:04:54 -08003973 pw.print(prefix);
3974 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
3975 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
3976
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003977 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
3978 sb.setLength(0);
3979 sb.append(prefix);
3980 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
3981 pw.println(sb.toString());
3982
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003983 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
3984 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003985
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003986 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07003987
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003988 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003989 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003990 pw.print(prefix); pw.println(" Device is currently unplugged");
3991 pw.print(prefix); pw.print(" Discharge cycle start level: ");
3992 pw.println(getDischargeStartLevel());
3993 pw.print(prefix); pw.print(" Discharge cycle current level: ");
3994 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07003995 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003996 pw.print(prefix); pw.println(" Device is currently plugged into power");
3997 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
3998 pw.println(getDischargeStartLevel());
3999 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
4000 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004001 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004002 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4003 pw.println(getDischargeAmountScreenOn());
4004 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4005 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004006 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004007 } else {
4008 pw.print(prefix); pw.println(" Device battery use since last full charge");
4009 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
4010 pw.println(getLowDischargeAmountSinceCharge());
4011 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
4012 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004013 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4014 pw.println(getDischargeAmountScreenOnSinceCharge());
4015 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4016 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004017 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004018 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004019
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004020 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004021 helper.create(this);
4022 helper.refreshStats(which, UserHandle.USER_ALL);
4023 List<BatterySipper> sippers = helper.getUsageList();
4024 if (sippers != null && sippers.size() > 0) {
4025 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4026 pw.print(prefix); pw.print(" Capacity: ");
4027 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004028 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004029 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4030 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4031 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4032 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004033 pw.println();
4034 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004035 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004036 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004037 switch (bs.drainType) {
4038 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004039 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004040 break;
4041 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004042 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004043 break;
4044 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004045 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004046 break;
4047 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004048 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004049 break;
4050 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004051 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004052 break;
4053 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004054 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004055 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004056 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004057 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004058 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004059 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004060 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004061 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004062 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004063 break;
4064 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004065 pw.print(" User "); pw.print(bs.userId);
4066 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004067 break;
4068 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004069 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004070 break;
4071 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004072 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004073 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004074 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004075 pw.print(" Camera: ");
4076 break;
4077 default:
4078 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004079 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004080 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004081 printmAh(pw, bs.totalPowerMah);
4082
Adam Lesinski57123002015-06-12 16:12:07 -07004083 if (bs.usagePowerMah != bs.totalPowerMah) {
4084 // If the usage (generic power) isn't the whole amount, we list out
4085 // what components are involved in the calculation.
4086
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004087 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004088 if (bs.usagePowerMah != 0) {
4089 pw.print(" usage=");
4090 printmAh(pw, bs.usagePowerMah);
4091 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004092 if (bs.cpuPowerMah != 0) {
4093 pw.print(" cpu=");
4094 printmAh(pw, bs.cpuPowerMah);
4095 }
4096 if (bs.wakeLockPowerMah != 0) {
4097 pw.print(" wake=");
4098 printmAh(pw, bs.wakeLockPowerMah);
4099 }
4100 if (bs.mobileRadioPowerMah != 0) {
4101 pw.print(" radio=");
4102 printmAh(pw, bs.mobileRadioPowerMah);
4103 }
4104 if (bs.wifiPowerMah != 0) {
4105 pw.print(" wifi=");
4106 printmAh(pw, bs.wifiPowerMah);
4107 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004108 if (bs.bluetoothPowerMah != 0) {
4109 pw.print(" bt=");
4110 printmAh(pw, bs.bluetoothPowerMah);
4111 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004112 if (bs.gpsPowerMah != 0) {
4113 pw.print(" gps=");
4114 printmAh(pw, bs.gpsPowerMah);
4115 }
4116 if (bs.sensorPowerMah != 0) {
4117 pw.print(" sensor=");
4118 printmAh(pw, bs.sensorPowerMah);
4119 }
4120 if (bs.cameraPowerMah != 0) {
4121 pw.print(" camera=");
4122 printmAh(pw, bs.cameraPowerMah);
4123 }
4124 if (bs.flashlightPowerMah != 0) {
4125 pw.print(" flash=");
4126 printmAh(pw, bs.flashlightPowerMah);
4127 }
4128 pw.print(" )");
4129 }
4130 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004131 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004132 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004133 }
4134
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004135 sippers = helper.getMobilemsppList();
4136 if (sippers != null && sippers.size() > 0) {
4137 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004138 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004139 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004140 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004141 sb.setLength(0);
4142 sb.append(prefix); sb.append(" Uid ");
4143 UserHandle.formatUid(sb, bs.uidObj.getUid());
4144 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4145 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4146 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004147 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004148 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004149 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004150 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004151 sb.setLength(0);
4152 sb.append(prefix);
4153 sb.append(" TOTAL TIME: ");
4154 formatTimeMs(sb, totalTime);
4155 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4156 sb.append(")");
4157 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004158 pw.println();
4159 }
4160
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004161 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4162 @Override
4163 public int compare(TimerEntry lhs, TimerEntry rhs) {
4164 long lhsTime = lhs.mTime;
4165 long rhsTime = rhs.mTime;
4166 if (lhsTime < rhsTime) {
4167 return 1;
4168 }
4169 if (lhsTime > rhsTime) {
4170 return -1;
4171 }
4172 return 0;
4173 }
4174 };
4175
4176 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004177 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4178 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004179 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004180 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4181 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4182 : kernelWakelocks.entrySet()) {
4183 final BatteryStats.Timer timer = ent.getValue();
4184 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004185 if (totalTimeMillis > 0) {
4186 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4187 }
4188 }
4189 if (ktimers.size() > 0) {
4190 Collections.sort(ktimers, timerComparator);
4191 pw.print(prefix); pw.println(" All kernel wake locks:");
4192 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004193 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004194 String linePrefix = ": ";
4195 sb.setLength(0);
4196 sb.append(prefix);
4197 sb.append(" Kernel Wake lock ");
4198 sb.append(timer.mName);
4199 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4200 which, linePrefix);
4201 if (!linePrefix.equals(": ")) {
4202 sb.append(" realtime");
4203 // Only print out wake locks that were held
4204 pw.println(sb.toString());
4205 }
4206 }
4207 pw.println();
4208 }
4209 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004210
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004211 if (timers.size() > 0) {
4212 Collections.sort(timers, timerComparator);
4213 pw.print(prefix); pw.println(" All partial wake locks:");
4214 for (int i=0; i<timers.size(); i++) {
4215 TimerEntry timer = timers.get(i);
4216 sb.setLength(0);
4217 sb.append(" Wake lock ");
4218 UserHandle.formatUid(sb, timer.mId);
4219 sb.append(" ");
4220 sb.append(timer.mName);
4221 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4222 sb.append(" realtime");
4223 pw.println(sb.toString());
4224 }
4225 timers.clear();
4226 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08004227 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004228
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004229 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004230 if (wakeupReasons.size() > 0) {
4231 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004232 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004233 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004234 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004235 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
4236 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004237 }
4238 Collections.sort(reasons, timerComparator);
4239 for (int i=0; i<reasons.size(); i++) {
4240 TimerEntry timer = reasons.get(i);
4241 String linePrefix = ": ";
4242 sb.setLength(0);
4243 sb.append(prefix);
4244 sb.append(" Wakeup reason ");
4245 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004246 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4247 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004248 pw.println(sb.toString());
4249 }
4250 pw.println();
4251 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004252 }
Evan Millar22ac0432009-03-31 11:33:18 -07004253
James Carr2dd7e5e2016-07-20 18:48:39 -07004254 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
4255 pw.println("Memory Stats");
4256 for (int i = 0; i < mMemoryStats.size(); i++) {
4257 sb.setLength(0);
4258 sb.append("Bandwidth ");
4259 sb.append(mMemoryStats.keyAt(i));
4260 sb.append(" Time ");
4261 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
4262 pw.println(sb.toString());
4263 }
4264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004265 for (int iu=0; iu<NU; iu++) {
4266 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004267 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004268 continue;
4269 }
4270
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004271 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07004272
4273 pw.print(prefix);
4274 pw.print(" ");
4275 UserHandle.formatUid(pw, uid);
4276 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004277 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004278
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004279 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4280 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4281 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4282 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004283 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4284 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
4285
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004286 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4287 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004288 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4289 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004290
4291 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
4292 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
4293
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004294 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4295 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4296 final int wifiScanCount = u.getWifiScanCount(which);
4297 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004298
Adam Lesinski5f056f62016-07-14 16:56:08 -07004299 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
4300 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
4301
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004302 if (mobileRxBytes > 0 || mobileTxBytes > 0
4303 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004304 pw.print(prefix); pw.print(" Mobile network: ");
4305 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004306 pw.print(formatBytesLocked(mobileTxBytes));
4307 pw.print(" sent (packets "); pw.print(mobileRxPackets);
4308 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004310 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
4311 sb.setLength(0);
4312 sb.append(prefix); sb.append(" Mobile radio active: ");
4313 formatTimeMs(sb, uidMobileActiveTime / 1000);
4314 sb.append("(");
4315 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
4316 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
4317 long packets = mobileRxPackets + mobileTxPackets;
4318 if (packets == 0) {
4319 packets = 1;
4320 }
4321 sb.append(" @ ");
4322 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
4323 sb.append(" mspp");
4324 pw.println(sb.toString());
4325 }
4326
Adam Lesinski5f056f62016-07-14 16:56:08 -07004327 if (mobileWakeup > 0) {
4328 sb.setLength(0);
4329 sb.append(prefix);
4330 sb.append(" Mobile radio AP wakeups: ");
4331 sb.append(mobileWakeup);
4332 pw.println(sb.toString());
4333 }
4334
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004335 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
4336 u.getModemControllerActivity(), which);
4337
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004338 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004339 pw.print(prefix); pw.print(" Wi-Fi network: ");
4340 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004341 pw.print(formatBytesLocked(wifiTxBytes));
4342 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4343 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004344 }
4345
Dianne Hackborn62793e42015-03-09 11:15:41 -07004346 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004347 || uidWifiRunningTime != 0) {
4348 sb.setLength(0);
4349 sb.append(prefix); sb.append(" Wifi Running: ");
4350 formatTimeMs(sb, uidWifiRunningTime / 1000);
4351 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4352 whichBatteryRealtime)); sb.append(")\n");
4353 sb.append(prefix); sb.append(" Full Wifi Lock: ");
4354 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4355 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4356 whichBatteryRealtime)); sb.append(")\n");
4357 sb.append(prefix); sb.append(" Wifi Scan: ");
4358 formatTimeMs(sb, wifiScanTime / 1000);
4359 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004360 whichBatteryRealtime)); sb.append(") ");
4361 sb.append(wifiScanCount);
4362 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004363 pw.println(sb.toString());
4364 }
4365
Adam Lesinski5f056f62016-07-14 16:56:08 -07004366 if (wifiWakeup > 0) {
4367 sb.setLength(0);
4368 sb.append(prefix);
4369 sb.append(" WiFi AP wakeups: ");
4370 sb.append(wifiWakeup);
4371 pw.println(sb.toString());
4372 }
4373
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004374 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
4375 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07004376
Adam Lesinski50e47602015-12-04 17:04:54 -08004377 if (btRxBytes > 0 || btTxBytes > 0) {
4378 pw.print(prefix); pw.print(" Bluetooth network: ");
4379 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
4380 pw.print(formatBytesLocked(btTxBytes));
4381 pw.println(" sent");
4382 }
4383
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004384 uidActivity |= printTimer(pw, sb, u.getBluetoothScanTimer(), rawRealtime, which, prefix,
4385 "Bluetooth Scan");
4386
Dianne Hackborn617f8772009-03-31 15:04:46 -07004387 if (u.hasUserActivity()) {
4388 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07004389 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004390 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004391 if (val != 0) {
4392 if (!hasData) {
4393 sb.setLength(0);
4394 sb.append(" User activity: ");
4395 hasData = true;
4396 } else {
4397 sb.append(", ");
4398 }
4399 sb.append(val);
4400 sb.append(" ");
4401 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
4402 }
4403 }
4404 if (hasData) {
4405 pw.println(sb.toString());
4406 }
4407 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004409 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4410 = u.getWakelockStats();
4411 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004412 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004413 int countWakelock = 0;
4414 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4415 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4416 String linePrefix = ": ";
4417 sb.setLength(0);
4418 sb.append(prefix);
4419 sb.append(" Wake lock ");
4420 sb.append(wakelocks.keyAt(iw));
4421 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
4422 "full", which, linePrefix);
4423 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
4424 "partial", which, linePrefix);
4425 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
4426 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004427 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
4428 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07004429 sb.append(" realtime");
4430 pw.println(sb.toString());
4431 uidActivity = true;
4432 countWakelock++;
4433
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004434 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
4435 rawRealtime, which);
4436 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
4437 rawRealtime, which);
4438 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
4439 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004440 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07004441 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004442 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004443 if (countWakelock > 1) {
4444 if (totalFullWakelock != 0 || totalPartialWakelock != 0
4445 || totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004446 sb.setLength(0);
4447 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004448 sb.append(" TOTAL wake: ");
4449 boolean needComma = false;
4450 if (totalFullWakelock != 0) {
4451 needComma = true;
4452 formatTimeMs(sb, totalFullWakelock);
4453 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004454 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004455 if (totalPartialWakelock != 0) {
4456 if (needComma) {
4457 sb.append(", ");
4458 }
4459 needComma = true;
4460 formatTimeMs(sb, totalPartialWakelock);
4461 sb.append("partial");
4462 }
4463 if (totalWindowWakelock != 0) {
4464 if (needComma) {
4465 sb.append(", ");
4466 }
4467 needComma = true;
4468 formatTimeMs(sb, totalWindowWakelock);
4469 sb.append("window");
4470 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004471 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07004472 if (needComma) {
4473 sb.append(",");
4474 }
4475 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004476 formatTimeMs(sb, totalDrawWakelock);
4477 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07004478 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004479 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004480 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004481 }
4482 }
4483
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004484 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4485 for (int isy=syncs.size()-1; isy>=0; isy--) {
4486 final Timer timer = syncs.valueAt(isy);
4487 // Convert from microseconds to milliseconds with rounding
4488 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4489 final int count = timer.getCountLocked(which);
4490 sb.setLength(0);
4491 sb.append(prefix);
4492 sb.append(" Sync ");
4493 sb.append(syncs.keyAt(isy));
4494 sb.append(": ");
4495 if (totalTime != 0) {
4496 formatTimeMs(sb, totalTime);
4497 sb.append("realtime (");
4498 sb.append(count);
4499 sb.append(" times)");
4500 } else {
4501 sb.append("(not used)");
4502 }
4503 pw.println(sb.toString());
4504 uidActivity = true;
4505 }
4506
4507 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4508 for (int ij=jobs.size()-1; ij>=0; ij--) {
4509 final Timer timer = jobs.valueAt(ij);
4510 // Convert from microseconds to milliseconds with rounding
4511 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4512 final int count = timer.getCountLocked(which);
4513 sb.setLength(0);
4514 sb.append(prefix);
4515 sb.append(" Job ");
4516 sb.append(jobs.keyAt(ij));
4517 sb.append(": ");
4518 if (totalTime != 0) {
4519 formatTimeMs(sb, totalTime);
4520 sb.append("realtime (");
4521 sb.append(count);
4522 sb.append(" times)");
4523 } else {
4524 sb.append("(not used)");
4525 }
4526 pw.println(sb.toString());
4527 uidActivity = true;
4528 }
4529
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004530 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
4531 prefix, "Flashlight");
4532 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
4533 prefix, "Camera");
4534 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
4535 prefix, "Video");
4536 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
4537 prefix, "Audio");
4538
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004539 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4540 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004541 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004542 final Uid.Sensor se = sensors.valueAt(ise);
4543 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004544 sb.setLength(0);
4545 sb.append(prefix);
4546 sb.append(" Sensor ");
4547 int handle = se.getHandle();
4548 if (handle == Uid.Sensor.GPS) {
4549 sb.append("GPS");
4550 } else {
4551 sb.append(handle);
4552 }
4553 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004554
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004555 final Timer timer = se.getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004556 final Counter bgCounter = se.getSensorBgCount();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004557 if (timer != null) {
4558 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004559 final long totalTime = (timer.getTotalTimeLocked(
Dianne Hackborn61659e52014-07-09 16:13:01 -07004560 rawRealtime, which) + 500) / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004561 final int count = timer.getCountLocked(which);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004562 final int bgCount = bgCounter != null ? bgCounter.getCountLocked(which) : 0;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004563 //timer.logState();
4564 if (totalTime != 0) {
4565 formatTimeMs(sb, totalTime);
4566 sb.append("realtime (");
4567 sb.append(count);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004568 sb.append(" times");
4569 if (bgCount > 0) {
4570 sb.append(", ");
4571 sb.append(bgCount);
4572 sb.append(" bg");
4573 }
4574 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004575 } else {
4576 sb.append("(not used)");
4577 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004578 } else {
4579 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004580 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004581
4582 pw.println(sb.toString());
4583 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004584 }
4585
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004586 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
4587 "Vibrator");
4588 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
4589 prefix, "Foreground activities");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004590
Dianne Hackborn61659e52014-07-09 16:13:01 -07004591 long totalStateTime = 0;
4592 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
4593 long time = u.getProcessStateTime(ips, rawRealtime, which);
4594 if (time > 0) {
4595 totalStateTime += time;
4596 sb.setLength(0);
4597 sb.append(prefix);
4598 sb.append(" ");
4599 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
4600 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08004601 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004602 pw.println(sb.toString());
4603 uidActivity = true;
4604 }
4605 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08004606 if (totalStateTime > 0) {
4607 sb.setLength(0);
4608 sb.append(prefix);
4609 sb.append(" Total running: ");
4610 formatTimeMs(sb, (totalStateTime + 500) / 1000);
4611 pw.println(sb.toString());
4612 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004613
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004614 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4615 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004616 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
4617 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004618 sb.setLength(0);
4619 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07004620 sb.append(" Total cpu time: u=");
4621 formatTimeMs(sb, userCpuTimeUs / 1000);
4622 sb.append("s=");
4623 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004624 sb.append("p=");
4625 printmAh(sb, powerCpuMaUs / (1000.0 * 1000.0 * 60.0 * 60.0));
4626 sb.append("mAh");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004627 pw.println(sb.toString());
4628 }
4629
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004630 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4631 = u.getProcessStats();
4632 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4633 final Uid.Proc ps = processStats.valueAt(ipr);
4634 long userTime;
4635 long systemTime;
4636 long foregroundTime;
4637 int starts;
4638 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004639
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004640 userTime = ps.getUserTime(which);
4641 systemTime = ps.getSystemTime(which);
4642 foregroundTime = ps.getForegroundTime(which);
4643 starts = ps.getStarts(which);
4644 final int numCrashes = ps.getNumCrashes(which);
4645 final int numAnrs = ps.getNumAnrs(which);
4646 numExcessive = which == STATS_SINCE_CHARGED
4647 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004648
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004649 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
4650 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
4651 sb.setLength(0);
4652 sb.append(prefix); sb.append(" Proc ");
4653 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
4654 sb.append(prefix); sb.append(" CPU: ");
4655 formatTimeMs(sb, userTime); sb.append("usr + ");
4656 formatTimeMs(sb, systemTime); sb.append("krn ; ");
4657 formatTimeMs(sb, foregroundTime); sb.append("fg");
4658 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
4659 sb.append("\n"); sb.append(prefix); sb.append(" ");
4660 boolean hasOne = false;
4661 if (starts != 0) {
4662 hasOne = true;
4663 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004664 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004665 if (numCrashes != 0) {
4666 if (hasOne) {
4667 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004668 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004669 hasOne = true;
4670 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004671 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004672 if (numAnrs != 0) {
4673 if (hasOne) {
4674 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004675 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004676 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004677 }
4678 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004679 pw.println(sb.toString());
4680 for (int e=0; e<numExcessive; e++) {
4681 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
4682 if (ew != null) {
4683 pw.print(prefix); pw.print(" * Killed for ");
4684 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
4685 pw.print("wake lock");
4686 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
4687 pw.print("cpu");
4688 } else {
4689 pw.print("unknown");
4690 }
4691 pw.print(" use: ");
4692 TimeUtils.formatDuration(ew.usedTime, pw);
4693 pw.print(" over ");
4694 TimeUtils.formatDuration(ew.overTime, pw);
4695 if (ew.overTime != 0) {
4696 pw.print(" (");
4697 pw.print((ew.usedTime*100)/ew.overTime);
4698 pw.println("%)");
4699 }
4700 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004701 }
4702 uidActivity = true;
4703 }
4704 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004705
4706 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4707 = u.getPackageStats();
4708 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4709 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
4710 pw.println(":");
4711 boolean apkActivity = false;
4712 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4713 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4714 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
4715 pw.print(prefix); pw.print(" Wakeup alarm ");
4716 pw.print(alarms.keyAt(iwa)); pw.print(": ");
4717 pw.print(alarms.valueAt(iwa).getCountLocked(which));
4718 pw.println(" times");
4719 apkActivity = true;
4720 }
4721 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4722 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4723 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4724 final long startTime = ss.getStartTime(batteryUptime, which);
4725 final int starts = ss.getStarts(which);
4726 final int launches = ss.getLaunches(which);
4727 if (startTime != 0 || starts != 0 || launches != 0) {
4728 sb.setLength(0);
4729 sb.append(prefix); sb.append(" Service ");
4730 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
4731 sb.append(prefix); sb.append(" Created for: ");
4732 formatTimeMs(sb, startTime / 1000);
4733 sb.append("uptime\n");
4734 sb.append(prefix); sb.append(" Starts: ");
4735 sb.append(starts);
4736 sb.append(", launches: "); sb.append(launches);
4737 pw.println(sb.toString());
4738 apkActivity = true;
4739 }
4740 }
4741 if (!apkActivity) {
4742 pw.print(prefix); pw.println(" (nothing executed)");
4743 }
4744 uidActivity = true;
4745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004746 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004747 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004748 }
4749 }
4750 }
4751
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004752 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004753 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004754 int diff = oldval ^ newval;
4755 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004756 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004757 for (int i=0; i<descriptions.length; i++) {
4758 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004759 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004760 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004761 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004762 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004763 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004764 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
4765 didWake = true;
4766 pw.print("=");
4767 if (longNames) {
4768 UserHandle.formatUid(pw, wakelockTag.uid);
4769 pw.print(":\"");
4770 pw.print(wakelockTag.string);
4771 pw.print("\"");
4772 } else {
4773 pw.print(wakelockTag.poolIdx);
4774 }
4775 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004776 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004777 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004778 pw.print("=");
4779 int val = (newval&bd.mask)>>bd.shift;
4780 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004781 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004782 } else {
4783 pw.print(val);
4784 }
4785 }
4786 }
4787 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004788 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07004789 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004790 if (longNames) {
4791 UserHandle.formatUid(pw, wakelockTag.uid);
4792 pw.print(":\"");
4793 pw.print(wakelockTag.string);
4794 pw.print("\"");
4795 } else {
4796 pw.print(wakelockTag.poolIdx);
4797 }
4798 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004799 }
4800
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004801 public void prepareForDumpLocked() {
4802 }
4803
4804 public static class HistoryPrinter {
4805 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004806 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004807 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004808 int oldStatus = -1;
4809 int oldHealth = -1;
4810 int oldPlug = -1;
4811 int oldTemp = -1;
4812 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004813 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004814 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004815
Dianne Hackborn3251b902014-06-20 14:40:53 -07004816 void reset() {
4817 oldState = oldState2 = 0;
4818 oldLevel = -1;
4819 oldStatus = -1;
4820 oldHealth = -1;
4821 oldPlug = -1;
4822 oldTemp = -1;
4823 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004824 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07004825 }
4826
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004827 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004828 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004829 if (!checkin) {
4830 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004831 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004832 pw.print(" (");
4833 pw.print(rec.numReadInts);
4834 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004835 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004836 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4837 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004838 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004839 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004840 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004841 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004842 }
4843 lastTime = rec.time;
4844 }
4845 if (rec.cmd == HistoryItem.CMD_START) {
4846 if (checkin) {
4847 pw.print(":");
4848 }
4849 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004850 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004851 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
4852 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004853 if (checkin) {
4854 pw.print(":");
4855 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07004856 if (rec.cmd == HistoryItem.CMD_RESET) {
4857 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004858 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004859 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004860 pw.print("TIME:");
4861 if (checkin) {
4862 pw.println(rec.currentTime);
4863 } else {
4864 pw.print(" ");
4865 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
4866 rec.currentTime).toString());
4867 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08004868 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
4869 if (checkin) {
4870 pw.print(":");
4871 }
4872 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004873 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
4874 if (checkin) {
4875 pw.print(":");
4876 }
4877 pw.println("*OVERFLOW*");
4878 } else {
4879 if (!checkin) {
4880 if (rec.batteryLevel < 10) pw.print("00");
4881 else if (rec.batteryLevel < 100) pw.print("0");
4882 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004883 if (verbose) {
4884 pw.print(" ");
4885 if (rec.states < 0) ;
4886 else if (rec.states < 0x10) pw.print("0000000");
4887 else if (rec.states < 0x100) pw.print("000000");
4888 else if (rec.states < 0x1000) pw.print("00000");
4889 else if (rec.states < 0x10000) pw.print("0000");
4890 else if (rec.states < 0x100000) pw.print("000");
4891 else if (rec.states < 0x1000000) pw.print("00");
4892 else if (rec.states < 0x10000000) pw.print("0");
4893 pw.print(Integer.toHexString(rec.states));
4894 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004895 } else {
4896 if (oldLevel != rec.batteryLevel) {
4897 oldLevel = rec.batteryLevel;
4898 pw.print(",Bl="); pw.print(rec.batteryLevel);
4899 }
4900 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004901 if (oldStatus != rec.batteryStatus) {
4902 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004903 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004904 switch (oldStatus) {
4905 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004906 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004907 break;
4908 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004909 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004910 break;
4911 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004912 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004913 break;
4914 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004915 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004916 break;
4917 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004918 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004919 break;
4920 default:
4921 pw.print(oldStatus);
4922 break;
4923 }
4924 }
4925 if (oldHealth != rec.batteryHealth) {
4926 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004927 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004928 switch (oldHealth) {
4929 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004930 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004931 break;
4932 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004933 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004934 break;
4935 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004936 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004937 break;
4938 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004939 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004940 break;
4941 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004942 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004943 break;
4944 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004945 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004946 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004947 case BatteryManager.BATTERY_HEALTH_COLD:
4948 pw.print(checkin ? "c" : "cold");
4949 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004950 default:
4951 pw.print(oldHealth);
4952 break;
4953 }
4954 }
4955 if (oldPlug != rec.batteryPlugType) {
4956 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004957 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004958 switch (oldPlug) {
4959 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004960 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004961 break;
4962 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004963 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004964 break;
4965 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004966 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004967 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07004968 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004969 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07004970 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004971 default:
4972 pw.print(oldPlug);
4973 break;
4974 }
4975 }
4976 if (oldTemp != rec.batteryTemperature) {
4977 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004978 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004979 pw.print(oldTemp);
4980 }
4981 if (oldVolt != rec.batteryVoltage) {
4982 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004983 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004984 pw.print(oldVolt);
4985 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004986 final int chargeMAh = rec.batteryChargeUAh / 1000;
4987 if (oldChargeMAh != chargeMAh) {
4988 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07004989 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004990 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07004991 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004992 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004993 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004994 printBitDescriptions(pw, oldState2, rec.states2, null,
4995 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004996 if (rec.wakeReasonTag != null) {
4997 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07004998 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004999 pw.print(rec.wakeReasonTag.poolIdx);
5000 } else {
5001 pw.print(" wake_reason=");
5002 pw.print(rec.wakeReasonTag.uid);
5003 pw.print(":\"");
5004 pw.print(rec.wakeReasonTag.string);
5005 pw.print("\"");
5006 }
5007 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08005008 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005009 pw.print(checkin ? "," : " ");
5010 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
5011 pw.print("+");
5012 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
5013 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005014 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005015 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
5016 : HISTORY_EVENT_NAMES;
5017 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
5018 | HistoryItem.EVENT_FLAG_FINISH);
5019 if (idx >= 0 && idx < eventNames.length) {
5020 pw.print(eventNames[idx]);
5021 } else {
5022 pw.print(checkin ? "Ev" : "event");
5023 pw.print(idx);
5024 }
5025 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005026 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005027 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08005028 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08005029 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
5030 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005031 pw.print(":\"");
5032 pw.print(rec.eventTag.string);
5033 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005034 }
5035 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005036 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005037 if (rec.stepDetails != null) {
5038 if (!checkin) {
5039 pw.print(" Details: cpu=");
5040 pw.print(rec.stepDetails.userTime);
5041 pw.print("u+");
5042 pw.print(rec.stepDetails.systemTime);
5043 pw.print("s");
5044 if (rec.stepDetails.appCpuUid1 >= 0) {
5045 pw.print(" (");
5046 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
5047 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5048 if (rec.stepDetails.appCpuUid2 >= 0) {
5049 pw.print(", ");
5050 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
5051 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5052 }
5053 if (rec.stepDetails.appCpuUid3 >= 0) {
5054 pw.print(", ");
5055 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
5056 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5057 }
5058 pw.print(')');
5059 }
5060 pw.println();
5061 pw.print(" /proc/stat=");
5062 pw.print(rec.stepDetails.statUserTime);
5063 pw.print(" usr, ");
5064 pw.print(rec.stepDetails.statSystemTime);
5065 pw.print(" sys, ");
5066 pw.print(rec.stepDetails.statIOWaitTime);
5067 pw.print(" io, ");
5068 pw.print(rec.stepDetails.statIrqTime);
5069 pw.print(" irq, ");
5070 pw.print(rec.stepDetails.statSoftIrqTime);
5071 pw.print(" sirq, ");
5072 pw.print(rec.stepDetails.statIdlTime);
5073 pw.print(" idle");
5074 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
5075 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
5076 + rec.stepDetails.statSoftIrqTime;
5077 int total = totalRun + rec.stepDetails.statIdlTime;
5078 if (total > 0) {
5079 pw.print(" (");
5080 float perc = ((float)totalRun) / ((float)total) * 100;
5081 pw.print(String.format("%.1f%%", perc));
5082 pw.print(" of ");
5083 StringBuilder sb = new StringBuilder(64);
5084 formatTimeMsNoSpace(sb, total*10);
5085 pw.print(sb);
5086 pw.print(")");
5087 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005088 pw.print(", PlatformIdleStat ");
5089 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005090 pw.println();
5091 } else {
5092 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5093 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
5094 pw.print(rec.stepDetails.userTime);
5095 pw.print(":");
5096 pw.print(rec.stepDetails.systemTime);
5097 if (rec.stepDetails.appCpuUid1 >= 0) {
5098 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
5099 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5100 if (rec.stepDetails.appCpuUid2 >= 0) {
5101 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
5102 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5103 }
5104 if (rec.stepDetails.appCpuUid3 >= 0) {
5105 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
5106 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5107 }
5108 }
5109 pw.println();
5110 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5111 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
5112 pw.print(rec.stepDetails.statUserTime);
5113 pw.print(',');
5114 pw.print(rec.stepDetails.statSystemTime);
5115 pw.print(',');
5116 pw.print(rec.stepDetails.statIOWaitTime);
5117 pw.print(',');
5118 pw.print(rec.stepDetails.statIrqTime);
5119 pw.print(',');
5120 pw.print(rec.stepDetails.statSoftIrqTime);
5121 pw.print(',');
5122 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005123 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005124 if (rec.stepDetails.statPlatformIdleState != null) {
5125 pw.print(rec.stepDetails.statPlatformIdleState);
5126 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005127 pw.println();
5128 }
5129 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005130 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005131 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005132 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005133 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005134
5135 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
5136 UserHandle.formatUid(pw, uid);
5137 pw.print("=");
5138 pw.print(utime);
5139 pw.print("u+");
5140 pw.print(stime);
5141 pw.print("s");
5142 }
5143
5144 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
5145 pw.print('/');
5146 pw.print(uid);
5147 pw.print(":");
5148 pw.print(utime);
5149 pw.print(":");
5150 pw.print(stime);
5151 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005152 }
5153
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005154 private void printSizeValue(PrintWriter pw, long size) {
5155 float result = size;
5156 String suffix = "";
5157 if (result >= 10*1024) {
5158 suffix = "KB";
5159 result = result / 1024;
5160 }
5161 if (result >= 10*1024) {
5162 suffix = "MB";
5163 result = result / 1024;
5164 }
5165 if (result >= 10*1024) {
5166 suffix = "GB";
5167 result = result / 1024;
5168 }
5169 if (result >= 10*1024) {
5170 suffix = "TB";
5171 result = result / 1024;
5172 }
5173 if (result >= 10*1024) {
5174 suffix = "PB";
5175 result = result / 1024;
5176 }
5177 pw.print((int)result);
5178 pw.print(suffix);
5179 }
5180
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005181 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
5182 String label3, long estimatedTime) {
5183 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005184 return false;
5185 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005186 pw.print(label1);
5187 pw.print(label2);
5188 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005189 StringBuilder sb = new StringBuilder(64);
5190 formatTimeMs(sb, estimatedTime);
5191 pw.print(sb);
5192 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005193 return true;
5194 }
5195
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005196 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
5197 LevelStepTracker steps, boolean checkin) {
5198 if (steps == null) {
5199 return false;
5200 }
5201 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005202 if (count <= 0) {
5203 return false;
5204 }
5205 if (!checkin) {
5206 pw.println(header);
5207 }
Kweku Adams030980a2015-04-01 16:07:48 -07005208 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005209 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005210 long duration = steps.getDurationAt(i);
5211 int level = steps.getLevelAt(i);
5212 long initMode = steps.getInitModeAt(i);
5213 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005214 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005215 lineArgs[0] = Long.toString(duration);
5216 lineArgs[1] = Integer.toString(level);
5217 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5218 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5219 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
5220 case Display.STATE_ON: lineArgs[2] = "s+"; break;
5221 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
5222 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07005223 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005224 }
5225 } else {
5226 lineArgs[2] = "";
5227 }
5228 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5229 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
5230 } else {
5231 lineArgs[3] = "";
5232 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005233 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07005234 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005235 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07005236 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005237 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005238 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
5239 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005240 pw.print(prefix);
5241 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005242 TimeUtils.formatDuration(duration, pw);
5243 pw.print(" to "); pw.print(level);
5244 boolean haveModes = false;
5245 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5246 pw.print(" (");
5247 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5248 case Display.STATE_OFF: pw.print("screen-off"); break;
5249 case Display.STATE_ON: pw.print("screen-on"); break;
5250 case Display.STATE_DOZE: pw.print("screen-doze"); break;
5251 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07005252 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005253 }
5254 haveModes = true;
5255 }
5256 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5257 pw.print(haveModes ? ", " : " (");
5258 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
5259 ? "power-save-on" : "power-save-off");
5260 haveModes = true;
5261 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005262 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
5263 pw.print(haveModes ? ", " : " (");
5264 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
5265 ? "device-idle-on" : "device-idle-off");
5266 haveModes = true;
5267 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005268 if (haveModes) {
5269 pw.print(")");
5270 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005271 pw.println();
5272 }
5273 }
5274 return true;
5275 }
5276
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005277 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005278 public static final int DUMP_DAILY_ONLY = 1<<2;
5279 public static final int DUMP_HISTORY_ONLY = 1<<3;
5280 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
5281 public static final int DUMP_VERBOSE = 1<<5;
5282 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005283
Dianne Hackborn37de0982014-05-09 09:32:18 -07005284 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
5285 final HistoryPrinter hprinter = new HistoryPrinter();
5286 final HistoryItem rec = new HistoryItem();
5287 long lastTime = -1;
5288 long baseTime = -1;
5289 boolean printed = false;
5290 HistoryEventTracker tracker = null;
5291 while (getNextHistoryLocked(rec)) {
5292 lastTime = rec.time;
5293 if (baseTime < 0) {
5294 baseTime = lastTime;
5295 }
5296 if (rec.time >= histStart) {
5297 if (histStart >= 0 && !printed) {
5298 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07005299 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005300 || rec.cmd == HistoryItem.CMD_START
5301 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005302 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005303 hprinter.printNextItem(pw, rec, baseTime, checkin,
5304 (flags&DUMP_VERBOSE) != 0);
5305 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005306 } else if (rec.currentTime != 0) {
5307 printed = true;
5308 byte cmd = rec.cmd;
5309 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005310 hprinter.printNextItem(pw, rec, baseTime, checkin,
5311 (flags&DUMP_VERBOSE) != 0);
5312 rec.cmd = cmd;
5313 }
5314 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005315 if (rec.cmd != HistoryItem.CMD_UPDATE) {
5316 hprinter.printNextItem(pw, rec, baseTime, checkin,
5317 (flags&DUMP_VERBOSE) != 0);
5318 rec.cmd = HistoryItem.CMD_UPDATE;
5319 }
5320 int oldEventCode = rec.eventCode;
5321 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005322 rec.eventTag = new HistoryTag();
5323 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
5324 HashMap<String, SparseIntArray> active
5325 = tracker.getStateForEvent(i);
5326 if (active == null) {
5327 continue;
5328 }
5329 for (HashMap.Entry<String, SparseIntArray> ent
5330 : active.entrySet()) {
5331 SparseIntArray uids = ent.getValue();
5332 for (int j=0; j<uids.size(); j++) {
5333 rec.eventCode = i;
5334 rec.eventTag.string = ent.getKey();
5335 rec.eventTag.uid = uids.keyAt(j);
5336 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07005337 hprinter.printNextItem(pw, rec, baseTime, checkin,
5338 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005339 rec.wakeReasonTag = null;
5340 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005341 }
5342 }
5343 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005344 rec.eventCode = oldEventCode;
5345 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005346 tracker = null;
5347 }
5348 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005349 hprinter.printNextItem(pw, rec, baseTime, checkin,
5350 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07005351 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
5352 // This is an attempt to aggregate the previous state and generate
5353 // fake events to reflect that state at the point where we start
5354 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07005355 if (tracker == null) {
5356 tracker = new HistoryEventTracker();
5357 }
5358 tracker.updateState(rec.eventCode, rec.eventTag.string,
5359 rec.eventTag.uid, rec.eventTag.poolIdx);
5360 }
5361 }
5362 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07005363 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005364 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
5365 }
5366 }
5367
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005368 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
5369 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
5370 if (steps == null) {
5371 return;
5372 }
5373 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
5374 if (timeRemaining >= 0) {
5375 pw.print(prefix); pw.print(label); pw.print(" total time: ");
5376 tmpSb.setLength(0);
5377 formatTimeMs(tmpSb, timeRemaining);
5378 pw.print(tmpSb);
5379 pw.print(" (from "); pw.print(tmpOutInt[0]);
5380 pw.println(" steps)");
5381 }
5382 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5383 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5384 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
5385 if (estimatedTime > 0) {
5386 pw.print(prefix); pw.print(label); pw.print(" ");
5387 pw.print(STEP_LEVEL_MODE_LABELS[i]);
5388 pw.print(" time: ");
5389 tmpSb.setLength(0);
5390 formatTimeMs(tmpSb, estimatedTime);
5391 pw.print(tmpSb);
5392 pw.print(" (from "); pw.print(tmpOutInt[0]);
5393 pw.println(" steps)");
5394 }
5395 }
5396 }
5397
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005398 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
5399 ArrayList<PackageChange> changes) {
5400 if (changes == null) {
5401 return;
5402 }
5403 pw.print(prefix); pw.println("Package changes:");
5404 for (int i=0; i<changes.size(); i++) {
5405 PackageChange pc = changes.get(i);
5406 if (pc.mUpdate) {
5407 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
5408 pw.print(" vers="); pw.println(pc.mVersionCode);
5409 } else {
5410 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
5411 }
5412 }
5413 }
5414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005415 /**
5416 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
5417 *
5418 * @param pw a Printer to receive the dump output.
5419 */
5420 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005421 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005422 prepareForDumpLocked();
5423
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005424 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005425 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005426
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005427 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005428 final long historyTotalSize = getHistoryTotalSize();
5429 final long historyUsedSize = getHistoryUsedSize();
5430 if (startIteratingHistoryLocked()) {
5431 try {
5432 pw.print("Battery History (");
5433 pw.print((100*historyUsedSize)/historyTotalSize);
5434 pw.print("% used, ");
5435 printSizeValue(pw, historyUsedSize);
5436 pw.print(" used of ");
5437 printSizeValue(pw, historyTotalSize);
5438 pw.print(", ");
5439 pw.print(getHistoryStringPoolSize());
5440 pw.print(" strings using ");
5441 printSizeValue(pw, getHistoryStringPoolBytes());
5442 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07005443 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005444 pw.println();
5445 } finally {
5446 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005447 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005448 }
5449
5450 if (startIteratingOldHistoryLocked()) {
5451 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005452 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005453 pw.println("Old battery History:");
5454 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005455 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005456 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005457 if (baseTime < 0) {
5458 baseTime = rec.time;
5459 }
5460 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005461 }
5462 pw.println();
5463 } finally {
5464 finishIteratingOldHistoryLocked();
5465 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005466 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005467 }
5468
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005469 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005470 return;
5471 }
5472
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005473 if (!filtering) {
5474 SparseArray<? extends Uid> uidStats = getUidStats();
5475 final int NU = uidStats.size();
5476 boolean didPid = false;
5477 long nowRealtime = SystemClock.elapsedRealtime();
5478 for (int i=0; i<NU; i++) {
5479 Uid uid = uidStats.valueAt(i);
5480 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
5481 if (pids != null) {
5482 for (int j=0; j<pids.size(); j++) {
5483 Uid.Pid pid = pids.valueAt(j);
5484 if (!didPid) {
5485 pw.println("Per-PID Stats:");
5486 didPid = true;
5487 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005488 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
5489 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005490 pw.print(" PID "); pw.print(pids.keyAt(j));
5491 pw.print(" wake time: ");
5492 TimeUtils.formatDuration(time, pw);
5493 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005494 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005495 }
5496 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005497 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005498 pw.println();
5499 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005500 }
5501
5502 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005503 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
5504 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005505 long timeRemaining = computeBatteryTimeRemaining(
5506 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005507 if (timeRemaining >= 0) {
5508 pw.print(" Estimated discharge time remaining: ");
5509 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5510 pw.println();
5511 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005512 final LevelStepTracker steps = getDischargeLevelStepTracker();
5513 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5514 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
5515 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5516 STEP_LEVEL_MODE_VALUES[i], null));
5517 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005518 pw.println();
5519 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005520 if (dumpDurationSteps(pw, " ", "Charge step durations:",
5521 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005522 long timeRemaining = computeChargeTimeRemaining(
5523 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005524 if (timeRemaining >= 0) {
5525 pw.print(" Estimated charge time remaining: ");
5526 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5527 pw.println();
5528 }
5529 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005530 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005531 }
5532 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
5533 pw.println("Daily stats:");
5534 pw.print(" Current start time: ");
5535 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5536 getCurrentDailyStartTime()).toString());
5537 pw.print(" Next min deadline: ");
5538 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5539 getNextMinDailyDeadline()).toString());
5540 pw.print(" Next max deadline: ");
5541 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5542 getNextMaxDailyDeadline()).toString());
5543 StringBuilder sb = new StringBuilder(64);
5544 int[] outInt = new int[1];
5545 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
5546 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005547 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
5548 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005549 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005550 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
5551 dsteps, false)) {
5552 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5553 sb, outInt);
5554 }
5555 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
5556 csteps, false)) {
5557 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5558 sb, outInt);
5559 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005560 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005561 } else {
5562 pw.println(" Current daily steps:");
5563 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5564 sb, outInt);
5565 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5566 sb, outInt);
5567 }
5568 }
5569 DailyItem dit;
5570 int curIndex = 0;
5571 while ((dit=getDailyItemLocked(curIndex)) != null) {
5572 curIndex++;
5573 if ((flags&DUMP_DAILY_ONLY) != 0) {
5574 pw.println();
5575 }
5576 pw.print(" Daily from ");
5577 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
5578 pw.print(" to ");
5579 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
5580 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005581 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005582 if (dumpDurationSteps(pw, " ",
5583 " Discharge step durations:", dit.mDischargeSteps, false)) {
5584 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5585 sb, outInt);
5586 }
5587 if (dumpDurationSteps(pw, " ",
5588 " Charge step durations:", dit.mChargeSteps, false)) {
5589 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5590 sb, outInt);
5591 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005592 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005593 } else {
5594 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5595 sb, outInt);
5596 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5597 sb, outInt);
5598 }
5599 }
5600 pw.println();
5601 }
5602 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005603 pw.println("Statistics since last charge:");
5604 pw.println(" System starts: " + getStartCount()
5605 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07005606 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
5607 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005608 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005610 }
5611
5612 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005613 public void dumpCheckinLocked(Context context, PrintWriter pw,
5614 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005615 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005616
5617 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005618 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
5619 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005620
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005621 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
5622
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005623 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005624 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005625
5626 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07005627 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005628 try {
5629 for (int i=0; i<getHistoryStringPoolSize(); i++) {
5630 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5631 pw.print(HISTORY_STRING_POOL); pw.print(',');
5632 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005633 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005634 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005635 pw.print(",\"");
5636 String str = getHistoryTagPoolString(i);
5637 str = str.replace("\\", "\\\\");
5638 str = str.replace("\"", "\\\"");
5639 pw.print(str);
5640 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005641 pw.println();
5642 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005643 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005644 } finally {
5645 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08005646 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005647 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005648 }
5649
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005650 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005651 return;
5652 }
5653
Dianne Hackborne4a59512010-12-07 11:08:07 -08005654 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005655 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08005656 for (int i=0; i<apps.size(); i++) {
5657 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005658 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
5659 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08005660 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005661 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
5662 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005663 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005664 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005665 }
5666 SparseArray<? extends Uid> uidStats = getUidStats();
5667 final int NU = uidStats.size();
5668 String[] lineArgs = new String[2];
5669 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005670 int uid = UserHandle.getAppId(uidStats.keyAt(i));
5671 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
5672 if (pkgs != null && !pkgs.second.value) {
5673 pkgs.second.value = true;
5674 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08005675 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005676 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005677 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
5678 (Object[])lineArgs);
5679 }
5680 }
5681 }
5682 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005683 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005684 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005685 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07005686 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005687 if (timeRemaining >= 0) {
5688 lineArgs[0] = Long.toString(timeRemaining);
5689 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
5690 (Object[])lineArgs);
5691 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005692 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07005693 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005694 if (timeRemaining >= 0) {
5695 lineArgs[0] = Long.toString(timeRemaining);
5696 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
5697 (Object[])lineArgs);
5698 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005699 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
5700 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005703}