blob: e1c7ad77bd25ff1c39278b4d66c70231edf9c1c2 [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.os;
18
19import java.io.PrintWriter;
Dianne Hackborne4a59512010-12-07 11:08:07 -080020import java.util.ArrayList;
Dianne Hackborn81038902012-11-26 17:04:09 -080021import java.util.Collections;
22import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import java.util.Formatter;
Dianne Hackborn37de0982014-05-09 09:32:18 -070024import java.util.HashMap;
Dianne Hackborne4a59512010-12-07 11:08:07 -080025import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import java.util.Map;
27
Dianne Hackborna7c837f2014-01-15 16:20:44 -080028import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080029import android.content.pm.ApplicationInfo;
Wink Saville52840902011-02-18 12:40:47 -080030import android.telephony.SignalStrength;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080031import android.text.format.DateFormat;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070032import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.util.Printer;
34import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070035import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070036import android.util.TimeUtils;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070037import android.view.Display;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080038import com.android.internal.os.BatterySipper;
39import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41/**
42 * A class providing access to battery usage statistics, including information on
43 * wakelocks, processes, packages, and services. All times are represented in microseconds
44 * except where indicated otherwise.
45 * @hide
46 */
47public abstract class BatteryStats implements Parcelable {
48
49 private static final boolean LOCAL_LOGV = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070050
51 /** @hide */
52 public static final String SERVICE_NAME = "batterystats";
53
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 /**
55 * A constant indicating a partial wake lock timer.
56 */
57 public static final int WAKE_TYPE_PARTIAL = 0;
58
59 /**
60 * A constant indicating a full wake lock timer.
61 */
62 public static final int WAKE_TYPE_FULL = 1;
63
64 /**
65 * A constant indicating a window wake lock timer.
66 */
67 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 /**
70 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 */
72 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070073
74 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070075 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070076 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070077 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070078
79 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070080 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070081 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070082 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070083
84 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070085 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070086 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070087 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
Dianne Hackborn62793e42015-03-09 11:15:41 -070089 /**
90 * A constant indicating a wifi multicast timer
91 */
92 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -070093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -070095 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070096 */
97 public static final int VIDEO_TURNED_ON = 8;
98
99 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800100 * A constant indicating a vibrator on timer
101 */
102 public static final int VIBRATOR_ON = 9;
103
104 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700105 * A constant indicating a foreground activity timer
106 */
107 public static final int FOREGROUND_ACTIVITY = 10;
108
109 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700110 * A constant indicating a wifi batched scan is active
111 */
112 public static final int WIFI_BATCHED_SCAN = 11;
113
114 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700115 * A constant indicating a process state timer
116 */
117 public static final int PROCESS_STATE = 12;
118
119 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700120 * A constant indicating a sync timer
121 */
122 public static final int SYNC = 13;
123
124 /**
125 * A constant indicating a job timer
126 */
127 public static final int JOB = 14;
128
129 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800130 * A constant indicating an audio turn on timer
131 */
132 public static final int AUDIO_TURNED_ON = 15;
133
134 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700135 * A constant indicating a flashlight turn on timer
136 */
137 public static final int FLASHLIGHT_TURNED_ON = 16;
138
139 /**
140 * A constant indicating a camera turn on timer
141 */
142 public static final int CAMERA_TURNED_ON = 17;
143
144 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700145 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700146 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700147 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700148
149 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800150 * A constant indicating a bluetooth scan timer.
151 */
152 public static final int BLUETOOTH_SCAN_ON = 19;
153
154 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 * Include all of the data in the stats, including previously saved data.
156 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700157 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158
159 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 * Include only the current run in the stats.
161 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700162 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163
164 /**
165 * Include only the run since the last time the device was unplugged in the stats.
166 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700167 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700168
169 // NOTE: Update this list if you add/change any stats above.
170 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700171 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700172 private static final String[] STAT_NAMES = { "l", "c", "u" };
173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700175 * Current version of checkin data format.
176 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800177 static final String CHECKIN_VERSION = "17";
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700178
179 /**
180 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700182 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700183
Evan Millar22ac0432009-03-31 11:33:18 -0700184 private static final long BYTES_PER_KB = 1024;
185 private static final long BYTES_PER_MB = 1048576; // 1024^2
186 private static final long BYTES_PER_GB = 1073741824; //1024^3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700188 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800189 private static final String UID_DATA = "uid";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700191 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700192 private static final String CPU_DATA = "cpu";
Evan Millare84de8d2009-04-02 22:16:12 -0700193 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800194 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700195 private static final String FOREGROUND_DATA = "fg";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700196 private static final String STATE_TIME_DATA = "st";
Evan Millare84de8d2009-04-02 22:16:12 -0700197 private static final String WAKELOCK_DATA = "wl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700198 private static final String SYNC_DATA = "sy";
199 private static final String JOB_DATA = "jb";
Evan Millarc64edde2009-04-18 12:26:32 -0700200 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700201 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700202 private static final String NETWORK_DATA = "nt";
203 private static final String USER_ACTIVITY_DATA = "ua";
204 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800205 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700206 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700207 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700208 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800209 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
210 private static final String WIFI_CONTROLLER_DATA = "wfcd";
211 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
212 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Evan Millare84de8d2009-04-02 22:16:12 -0700213 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800214 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800215 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
216 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800217 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700218 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700219 private static final String SCREEN_BRIGHTNESS_DATA = "br";
220 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700221 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700222 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
223 private static final String DATA_CONNECTION_TIME_DATA = "dct";
224 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800225 private static final String WIFI_STATE_TIME_DATA = "wst";
226 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700227 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
228 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
229 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
230 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800231 private static final String POWER_USE_SUMMARY_DATA = "pws";
232 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700233 private static final String DISCHARGE_STEP_DATA = "dsd";
234 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700235 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
236 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700237 private static final String FLASHLIGHT_DATA = "fla";
238 private static final String CAMERA_DATA = "cam";
239 private static final String VIDEO_DATA = "vid";
240 private static final String AUDIO_DATA = "aud";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700242 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 private final Formatter mFormatter = new Formatter(mFormatBuilder);
244
245 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700246 * State for keeping track of counting information.
247 */
248 public static abstract class Counter {
249
250 /**
251 * Returns the count associated with this Counter for the
252 * selected type of statistics.
253 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700254 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700255 */
Evan Millarc64edde2009-04-18 12:26:32 -0700256 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700257
258 /**
259 * Temporary for debugging.
260 */
261 public abstract void logState(Printer pw, String prefix);
262 }
263
264 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700265 * State for keeping track of long counting information.
266 */
267 public static abstract class LongCounter {
268
269 /**
270 * Returns the count associated with this Counter for the
271 * selected type of statistics.
272 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700273 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700274 */
275 public abstract long getCountLocked(int which);
276
277 /**
278 * Temporary for debugging.
279 */
280 public abstract void logState(Printer pw, String prefix);
281 }
282
283 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800284 * Container class that aggregates counters for transmit, receive, and idle state of a
285 * radio controller.
286 */
287 public static abstract class ControllerActivityCounter {
288 /**
289 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
290 * idle state.
291 */
292 public abstract LongCounter getIdleTimeCounter();
293
294 /**
295 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
296 * receive state.
297 */
298 public abstract LongCounter getRxTimeCounter();
299
300 /**
301 * An array of {@link LongCounter}, representing various transmit levels, where each level
302 * may draw a different amount of power. The levels themselves are controller-specific.
303 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
304 * various transmit level states.
305 */
306 public abstract LongCounter[] getTxTimeCounters();
307
308 /**
309 * @return a non-null {@link LongCounter} representing the power consumed by the controller
310 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
311 * yield a value of 0 if the device doesn't support power calculations.
312 */
313 public abstract LongCounter getPowerCounter();
314 }
315
316 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 * State for keeping track of timing information.
318 */
319 public static abstract class Timer {
320
321 /**
322 * Returns the count associated with this Timer for the
323 * selected type of statistics.
324 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700325 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 */
Evan Millarc64edde2009-04-18 12:26:32 -0700327 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328
329 /**
330 * Returns the total time in microseconds associated with this Timer for the
331 * selected type of statistics.
332 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800333 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700334 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 * @return a time in microseconds
336 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800337 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700340 * Returns the total time in microseconds associated with this Timer since the
341 * 'mark' was last set.
342 *
343 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
344 * @return a time in microseconds
345 */
346 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
347
348 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * Temporary for debugging.
350 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700351 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 }
353
354 /**
355 * The statistics associated with a particular uid.
356 */
357 public static abstract class Uid {
358
359 /**
360 * Returns a mapping containing wakelock statistics.
361 *
362 * @return a Map from Strings to Uid.Wakelock objects.
363 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700364 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365
366 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700367 * Returns a mapping containing sync statistics.
368 *
369 * @return a Map from Strings to Timer objects.
370 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700371 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700372
373 /**
374 * Returns a mapping containing scheduled job statistics.
375 *
376 * @return a Map from Strings to Timer objects.
377 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700378 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700379
380 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 * The statistics associated with a particular wake lock.
382 */
383 public static abstract class Wakelock {
384 public abstract Timer getWakeTime(int type);
385 }
386
387 /**
388 * Returns a mapping containing sensor statistics.
389 *
390 * @return a Map from Integer sensor ids to Uid.Sensor objects.
391 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700392 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393
394 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700395 * Returns a mapping containing active process data.
396 */
397 public abstract SparseArray<? extends Pid> getPidStats();
398
399 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 * Returns a mapping containing process statistics.
401 *
402 * @return a Map from Strings to Uid.Proc objects.
403 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700404 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405
406 /**
407 * Returns a mapping containing package statistics.
408 *
409 * @return a Map from Strings to Uid.Pkg objects.
410 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700411 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700412
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800413 public abstract ControllerActivityCounter getWifiControllerActivity();
414 public abstract ControllerActivityCounter getBluetoothControllerActivity();
415 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800416
417 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 * {@hide}
419 */
420 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700421
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800422 public abstract void noteWifiRunningLocked(long elapsedRealtime);
423 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
424 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
425 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
426 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
427 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
428 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
429 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
430 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
431 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800432 public abstract void noteActivityResumedLocked(long elapsedRealtime);
433 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800434 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
435 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
436 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700437 public abstract int getWifiScanCount(int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800438 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700439 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800440 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700441 public abstract Timer getAudioTurnedOnTimer();
442 public abstract Timer getVideoTurnedOnTimer();
443 public abstract Timer getFlashlightTurnedOnTimer();
444 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700445 public abstract Timer getForegroundActivityTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800446 public abstract Timer getBluetoothScanTimer();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700447
Dianne Hackborna8d10942015-11-19 17:55:19 -0800448 // Time this uid has any processes in the top state.
449 public static final int PROCESS_STATE_TOP = 0;
450 // Time this uid has any process with a started out bound foreground service.
451 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
452 // Time this uid has any process that is top while the device is sleeping.
453 public static final int PROCESS_STATE_TOP_SLEEPING = 2;
454 // Time this uid has any process in an active foreground state.
455 public static final int PROCESS_STATE_FOREGROUND = 3;
456 // Time this uid has any process in an active background state.
457 public static final int PROCESS_STATE_BACKGROUND = 4;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700458 // Time this uid has any processes running at all.
Dianne Hackborna8d10942015-11-19 17:55:19 -0800459 public static final int PROCESS_STATE_CACHED = 5;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700460 // Total number of process states we track.
Dianne Hackborna8d10942015-11-19 17:55:19 -0800461 public static final int NUM_PROCESS_STATE = 6;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700462
463 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackborna8d10942015-11-19 17:55:19 -0800464 "Top", "Fg Service", "Top Sleeping", "Foreground", "Background", "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700465 };
466
467 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800468 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700469
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800470 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471
Robert Greenwalta029ea12013-09-25 16:38:12 -0700472 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
473
Dianne Hackborn617f8772009-03-31 15:04:46 -0700474 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700475 * Note that these must match the constants in android.os.PowerManager.
476 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
477 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700478 */
479 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700480 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700481 };
482
Jeff Browndf693de2012-07-27 12:03:38 -0700483 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700484
Dianne Hackborn617f8772009-03-31 15:04:46 -0700485 public abstract void noteUserActivityLocked(int type);
486 public abstract boolean hasUserActivity();
487 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700488
489 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800490 public abstract long getNetworkActivityBytes(int type, int which);
491 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800492 public abstract long getMobileRadioActiveTime(int which);
493 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700494
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700495 /**
496 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
497 */
498 public abstract long getUserCpuTimeUs(int which);
499
500 /**
501 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
502 */
503 public abstract long getSystemCpuTimeUs(int which);
504
505 /**
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700506 * Get the total cpu power consumed (in milli-ampere-microseconds).
507 */
508 public abstract long getCpuPowerMaUs(int which);
509
510 /**
Adam Lesinski6832f392015-09-05 18:05:40 -0700511 * Returns the approximate cpu time (in milliseconds) spent at a certain CPU speed for a
512 * given CPU cluster.
513 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700514 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700515 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700516 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
517 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700518 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700519 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800522 /*
523 * FIXME: it's not correct to use this magic value because it
524 * could clash with a sensor handle (which are defined by
525 * the sensor HAL, and therefore out of our control
526 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 // Magic sensor number for the GPS.
528 public static final int GPS = -10000;
529
530 public abstract int getHandle();
531
532 public abstract Timer getSensorTime();
533 }
534
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700535 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800536 public int mWakeNesting;
537 public long mWakeSumMs;
538 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700539 }
540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 /**
542 * The statistics associated with a particular process.
543 */
544 public static abstract class Proc {
545
Dianne Hackborn287952c2010-09-22 22:34:31 -0700546 public static class ExcessivePower {
547 public static final int TYPE_WAKE = 1;
548 public static final int TYPE_CPU = 2;
549
550 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700551 public long overTime;
552 public long usedTime;
553 }
554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800556 * Returns true if this process is still active in the battery stats.
557 */
558 public abstract boolean isActive();
559
560 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700561 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700563 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 */
565 public abstract long getUserTime(int which);
566
567 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700568 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700570 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 */
572 public abstract long getSystemTime(int which);
573
574 /**
575 * Returns the number of times the process has been started.
576 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700577 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 */
579 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700580
581 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800582 * Returns the number of times the process has crashed.
583 *
584 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
585 */
586 public abstract int getNumCrashes(int which);
587
588 /**
589 * Returns the number of times the process has ANRed.
590 *
591 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
592 */
593 public abstract int getNumAnrs(int which);
594
595 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700596 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700597 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700598 * @return foreground cpu time in microseconds
599 */
600 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700601
Dianne Hackborn287952c2010-09-22 22:34:31 -0700602 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700603
Dianne Hackborn287952c2010-09-22 22:34:31 -0700604 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 }
606
607 /**
608 * The statistics associated with a particular package.
609 */
610 public static abstract class Pkg {
611
612 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700613 * Returns information about all wakeup alarms that have been triggered for this
614 * package. The mapping keys are tag names for the alarms, the counter contains
615 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700617 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618
619 /**
620 * Returns a mapping containing service statistics.
621 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700622 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623
624 /**
625 * The statistics associated with a particular service.
626 */
Joe Onoratoabded112016-02-08 16:49:39 -0800627 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628
629 /**
630 * Returns the amount of time spent started.
631 *
632 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700633 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 * @return
635 */
636 public abstract long getStartTime(long batteryUptime, int which);
637
638 /**
639 * Returns the total number of times startService() has been called.
640 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700641 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 */
643 public abstract int getStarts(int which);
644
645 /**
646 * Returns the total number times the service has been launched.
647 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700648 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 */
650 public abstract int getLaunches(int which);
651 }
652 }
653 }
654
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800655 public static final class LevelStepTracker {
656 public long mLastStepTime = -1;
657 public int mNumStepDurations;
658 public final long[] mStepDurations;
659
660 public LevelStepTracker(int maxLevelSteps) {
661 mStepDurations = new long[maxLevelSteps];
662 }
663
664 public LevelStepTracker(int numSteps, long[] steps) {
665 mNumStepDurations = numSteps;
666 mStepDurations = new long[numSteps];
667 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
668 }
669
670 public long getDurationAt(int index) {
671 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
672 }
673
674 public int getLevelAt(int index) {
675 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
676 >> STEP_LEVEL_LEVEL_SHIFT);
677 }
678
679 public int getInitModeAt(int index) {
680 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
681 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
682 }
683
684 public int getModModeAt(int index) {
685 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
686 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
687 }
688
689 private void appendHex(long val, int topOffset, StringBuilder out) {
690 boolean hasData = false;
691 while (topOffset >= 0) {
692 int digit = (int)( (val>>topOffset) & 0xf );
693 topOffset -= 4;
694 if (!hasData && digit == 0) {
695 continue;
696 }
697 hasData = true;
698 if (digit >= 0 && digit <= 9) {
699 out.append((char)('0' + digit));
700 } else {
701 out.append((char)('a' + digit - 10));
702 }
703 }
704 }
705
706 public void encodeEntryAt(int index, StringBuilder out) {
707 long item = mStepDurations[index];
708 long duration = item & STEP_LEVEL_TIME_MASK;
709 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
710 >> STEP_LEVEL_LEVEL_SHIFT);
711 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
712 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
713 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
714 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
715 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
716 case Display.STATE_OFF: out.append('f'); break;
717 case Display.STATE_ON: out.append('o'); break;
718 case Display.STATE_DOZE: out.append('d'); break;
719 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
720 }
721 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
722 out.append('p');
723 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700724 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
725 out.append('i');
726 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800727 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
728 case Display.STATE_OFF: out.append('F'); break;
729 case Display.STATE_ON: out.append('O'); break;
730 case Display.STATE_DOZE: out.append('D'); break;
731 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
732 }
733 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
734 out.append('P');
735 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700736 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
737 out.append('I');
738 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800739 out.append('-');
740 appendHex(level, 4, out);
741 out.append('-');
742 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
743 }
744
745 public void decodeEntryAt(int index, String value) {
746 final int N = value.length();
747 int i = 0;
748 char c;
749 long out = 0;
750 while (i < N && (c=value.charAt(i)) != '-') {
751 i++;
752 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800753 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800754 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800755 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800756 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800757 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800758 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800759 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
760 << STEP_LEVEL_INITIAL_MODE_SHIFT);
761 break;
762 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
763 << STEP_LEVEL_INITIAL_MODE_SHIFT);
764 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700765 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
766 << STEP_LEVEL_INITIAL_MODE_SHIFT);
767 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800768 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
769 break;
770 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
771 break;
772 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
773 break;
774 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
775 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
776 break;
777 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
778 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800779 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700780 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
781 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
782 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800783 }
784 }
785 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800786 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800787 while (i < N && (c=value.charAt(i)) != '-') {
788 i++;
789 level <<= 4;
790 if (c >= '0' && c <= '9') {
791 level += c - '0';
792 } else if (c >= 'a' && c <= 'f') {
793 level += c - 'a' + 10;
794 } else if (c >= 'A' && c <= 'F') {
795 level += c - 'A' + 10;
796 }
797 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800798 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800799 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
800 long duration = 0;
801 while (i < N && (c=value.charAt(i)) != '-') {
802 i++;
803 duration <<= 4;
804 if (c >= '0' && c <= '9') {
805 duration += c - '0';
806 } else if (c >= 'a' && c <= 'f') {
807 duration += c - 'a' + 10;
808 } else if (c >= 'A' && c <= 'F') {
809 duration += c - 'A' + 10;
810 }
811 }
812 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
813 }
814
815 public void init() {
816 mLastStepTime = -1;
817 mNumStepDurations = 0;
818 }
819
820 public void clearTime() {
821 mLastStepTime = -1;
822 }
823
824 public long computeTimePerLevel() {
825 final long[] steps = mStepDurations;
826 final int numSteps = mNumStepDurations;
827
828 // For now we'll do a simple average across all steps.
829 if (numSteps <= 0) {
830 return -1;
831 }
832 long total = 0;
833 for (int i=0; i<numSteps; i++) {
834 total += steps[i] & STEP_LEVEL_TIME_MASK;
835 }
836 return total / numSteps;
837 /*
838 long[] buckets = new long[numSteps];
839 int numBuckets = 0;
840 int numToAverage = 4;
841 int i = 0;
842 while (i < numSteps) {
843 long totalTime = 0;
844 int num = 0;
845 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
846 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
847 num++;
848 }
849 buckets[numBuckets] = totalTime / num;
850 numBuckets++;
851 numToAverage *= 2;
852 i += num;
853 }
854 if (numBuckets < 1) {
855 return -1;
856 }
857 long averageTime = buckets[numBuckets-1];
858 for (i=numBuckets-2; i>=0; i--) {
859 averageTime = (averageTime + buckets[i]) / 2;
860 }
861 return averageTime;
862 */
863 }
864
865 public long computeTimeEstimate(long modesOfInterest, long modeValues,
866 int[] outNumOfInterest) {
867 final long[] steps = mStepDurations;
868 final int count = mNumStepDurations;
869 if (count <= 0) {
870 return -1;
871 }
872 long total = 0;
873 int numOfInterest = 0;
874 for (int i=0; i<count; i++) {
875 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
876 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
877 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
878 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
879 // If the modes of interest didn't change during this step period...
880 if ((modMode&modesOfInterest) == 0) {
881 // And the mode values during this period match those we are measuring...
882 if ((initMode&modesOfInterest) == modeValues) {
883 // Then this can be used to estimate the total time!
884 numOfInterest++;
885 total += steps[i] & STEP_LEVEL_TIME_MASK;
886 }
887 }
888 }
889 if (numOfInterest <= 0) {
890 return -1;
891 }
892
893 if (outNumOfInterest != null) {
894 outNumOfInterest[0] = numOfInterest;
895 }
896
897 // The estimated time is the average time we spend in each level, multipled
898 // by 100 -- the total number of battery levels
899 return (total / numOfInterest) * 100;
900 }
901
902 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
903 int stepCount = mNumStepDurations;
904 final long lastStepTime = mLastStepTime;
905 if (lastStepTime >= 0 && numStepLevels > 0) {
906 final long[] steps = mStepDurations;
907 long duration = elapsedRealtime - lastStepTime;
908 for (int i=0; i<numStepLevels; i++) {
909 System.arraycopy(steps, 0, steps, 1, steps.length-1);
910 long thisDuration = duration / (numStepLevels-i);
911 duration -= thisDuration;
912 if (thisDuration > STEP_LEVEL_TIME_MASK) {
913 thisDuration = STEP_LEVEL_TIME_MASK;
914 }
915 steps[0] = thisDuration | modeBits;
916 }
917 stepCount += numStepLevels;
918 if (stepCount > steps.length) {
919 stepCount = steps.length;
920 }
921 }
922 mNumStepDurations = stepCount;
923 mLastStepTime = elapsedRealtime;
924 }
925
926 public void readFromParcel(Parcel in) {
927 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -0700928 if (N > mStepDurations.length) {
929 throw new ParcelFormatException("more step durations than available: " + N);
930 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800931 mNumStepDurations = N;
932 for (int i=0; i<N; i++) {
933 mStepDurations[i] = in.readLong();
934 }
935 }
936
937 public void writeToParcel(Parcel out) {
938 final int N = mNumStepDurations;
939 out.writeInt(N);
940 for (int i=0; i<N; i++) {
941 out.writeLong(mStepDurations[i]);
942 }
943 }
944 }
945
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700946 public static final class PackageChange {
947 public String mPackageName;
948 public boolean mUpdate;
949 public int mVersionCode;
950 }
951
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800952 public static final class DailyItem {
953 public long mStartTime;
954 public long mEndTime;
955 public LevelStepTracker mDischargeSteps;
956 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700957 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800958 }
959
960 public abstract DailyItem getDailyItemLocked(int daysAgo);
961
962 public abstract long getCurrentDailyStartTime();
963
964 public abstract long getNextMinDailyDeadline();
965
966 public abstract long getNextMaxDailyDeadline();
967
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800968 public final static class HistoryTag {
969 public String string;
970 public int uid;
971
972 public int poolIdx;
973
974 public void setTo(HistoryTag o) {
975 string = o.string;
976 uid = o.uid;
977 poolIdx = o.poolIdx;
978 }
979
980 public void setTo(String _string, int _uid) {
981 string = _string;
982 uid = _uid;
983 poolIdx = -1;
984 }
985
986 public void writeToParcel(Parcel dest, int flags) {
987 dest.writeString(string);
988 dest.writeInt(uid);
989 }
990
991 public void readFromParcel(Parcel src) {
992 string = src.readString();
993 uid = src.readInt();
994 poolIdx = -1;
995 }
996
997 @Override
998 public boolean equals(Object o) {
999 if (this == o) return true;
1000 if (o == null || getClass() != o.getClass()) return false;
1001
1002 HistoryTag that = (HistoryTag) o;
1003
1004 if (uid != that.uid) return false;
1005 if (!string.equals(that.string)) return false;
1006
1007 return true;
1008 }
1009
1010 @Override
1011 public int hashCode() {
1012 int result = string.hashCode();
1013 result = 31 * result + uid;
1014 return result;
1015 }
1016 }
1017
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001018 /**
1019 * Optional detailed information that can go into a history step. This is typically
1020 * generated each time the battery level changes.
1021 */
1022 public final static class HistoryStepDetails {
1023 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1024 public int userTime;
1025 public int systemTime;
1026
1027 // Top three apps using CPU in the last step, with times in 1/100 second.
1028 public int appCpuUid1;
1029 public int appCpuUTime1;
1030 public int appCpuSTime1;
1031 public int appCpuUid2;
1032 public int appCpuUTime2;
1033 public int appCpuSTime2;
1034 public int appCpuUid3;
1035 public int appCpuUTime3;
1036 public int appCpuSTime3;
1037
1038 // Information from /proc/stat
1039 public int statUserTime;
1040 public int statSystemTime;
1041 public int statIOWaitTime;
1042 public int statIrqTime;
1043 public int statSoftIrqTime;
1044 public int statIdlTime;
1045
1046 public HistoryStepDetails() {
1047 clear();
1048 }
1049
1050 public void clear() {
1051 userTime = systemTime = 0;
1052 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1053 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1054 = appCpuUTime3 = appCpuSTime3 = 0;
1055 }
1056
1057 public void writeToParcel(Parcel out) {
1058 out.writeInt(userTime);
1059 out.writeInt(systemTime);
1060 out.writeInt(appCpuUid1);
1061 out.writeInt(appCpuUTime1);
1062 out.writeInt(appCpuSTime1);
1063 out.writeInt(appCpuUid2);
1064 out.writeInt(appCpuUTime2);
1065 out.writeInt(appCpuSTime2);
1066 out.writeInt(appCpuUid3);
1067 out.writeInt(appCpuUTime3);
1068 out.writeInt(appCpuSTime3);
1069 out.writeInt(statUserTime);
1070 out.writeInt(statSystemTime);
1071 out.writeInt(statIOWaitTime);
1072 out.writeInt(statIrqTime);
1073 out.writeInt(statSoftIrqTime);
1074 out.writeInt(statIdlTime);
1075 }
1076
1077 public void readFromParcel(Parcel in) {
1078 userTime = in.readInt();
1079 systemTime = in.readInt();
1080 appCpuUid1 = in.readInt();
1081 appCpuUTime1 = in.readInt();
1082 appCpuSTime1 = in.readInt();
1083 appCpuUid2 = in.readInt();
1084 appCpuUTime2 = in.readInt();
1085 appCpuSTime2 = in.readInt();
1086 appCpuUid3 = in.readInt();
1087 appCpuUTime3 = in.readInt();
1088 appCpuSTime3 = in.readInt();
1089 statUserTime = in.readInt();
1090 statSystemTime = in.readInt();
1091 statIOWaitTime = in.readInt();
1092 statIrqTime = in.readInt();
1093 statSoftIrqTime = in.readInt();
1094 statIdlTime = in.readInt();
1095 }
1096 }
1097
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001098 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001099 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001100
1101 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001102 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001103
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001104 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001105 public static final byte CMD_NULL = -1;
1106 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001107 public static final byte CMD_CURRENT_TIME = 5;
1108 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001109 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001110 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001111
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001112 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001113
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001114 /**
1115 * Return whether the command code is a delta data update.
1116 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001117 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001118 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001119 }
1120
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001121 public byte batteryLevel;
1122 public byte batteryStatus;
1123 public byte batteryHealth;
1124 public byte batteryPlugType;
1125
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001126 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001127 public char batteryVoltage;
1128
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001129 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001130 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001131 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001132 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001133 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1134 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001135 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001136 public static final int STATE_PHONE_STATE_SHIFT = 6;
1137 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001138 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001139 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001140 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001141
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001142 // These states always appear directly in the first int token
1143 // of a delta change; they should be ones that change relatively
1144 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001145 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1146 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001147 public static final int STATE_GPS_ON_FLAG = 1<<29;
1148 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001149 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001150 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001151 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001152 // These are on the lower bits used for the command; if they change
1153 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001154 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001155 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001156 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001157 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1158 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1159 // empty slot
1160 // empty slot
1161 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001162
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001163 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001164 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1165
1166 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001167
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001168 public int states;
1169
Dianne Hackborn3251b902014-06-20 14:40:53 -07001170 // Constants from WIFI_SUPPL_STATE_*
1171 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1172 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1173 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1174 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1175 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1176 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1177
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001178 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001179 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1180 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1181 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001182 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001183 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1184 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1185 public static final int STATE2_CHARGING_FLAG = 1<<24;
1186 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1187 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1188 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001189 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001190
1191 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001192 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001193 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1194
1195 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001196
Dianne Hackborn40c87252014-03-19 16:55:40 -07001197 public int states2;
1198
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001199 // The wake lock that was acquired at this point.
1200 public HistoryTag wakelockTag;
1201
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001202 // Kernel wakeup reason at this point.
1203 public HistoryTag wakeReasonTag;
1204
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001205 // Non-null when there is more detailed information at this step.
1206 public HistoryStepDetails stepDetails;
1207
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001208 public static final int EVENT_FLAG_START = 0x8000;
1209 public static final int EVENT_FLAG_FINISH = 0x4000;
1210
1211 // No event in this item.
1212 public static final int EVENT_NONE = 0x0000;
1213 // Event is about a process that is running.
1214 public static final int EVENT_PROC = 0x0001;
1215 // Event is about an application package that is in the foreground.
1216 public static final int EVENT_FOREGROUND = 0x0002;
1217 // Event is about an application package that is at the top of the screen.
1218 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001219 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001220 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001221 // Events for all additional wake locks aquired/release within a wake block.
1222 // These are not generated by default.
1223 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001224 // Event is about an application executing a scheduled job.
1225 public static final int EVENT_JOB = 0x0006;
1226 // Events for users running.
1227 public static final int EVENT_USER_RUNNING = 0x0007;
1228 // Events for foreground user.
1229 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001230 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001231 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001232 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001233 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001234 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001235 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001236 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001237 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001238 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001239 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001240 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001241 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001242 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001243 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001244 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001245 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001246 // Event for a package being on the temporary whitelist.
1247 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001248 // Event for the screen waking up.
1249 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Amith Yamasani67768492015-06-09 12:23:58 -07001250
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001251 // Number of event types.
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001252 public static final int EVENT_COUNT = 0x0013;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001253 // Mask to extract out only the type part of the event.
1254 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001255
1256 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1257 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1258 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1259 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1260 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1261 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001262 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1263 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001264 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1265 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001266 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1267 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1268 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1269 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1270 public static final int EVENT_USER_FOREGROUND_START =
1271 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1272 public static final int EVENT_USER_FOREGROUND_FINISH =
1273 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001274 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1275 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001276 public static final int EVENT_TEMP_WHITELIST_START =
1277 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1278 public static final int EVENT_TEMP_WHITELIST_FINISH =
1279 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001280
1281 // For CMD_EVENT.
1282 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001283 public HistoryTag eventTag;
1284
Dianne Hackborn9a755432014-05-15 17:05:22 -07001285 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001286 public long currentTime;
1287
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001288 // Meta-data when reading.
1289 public int numReadInts;
1290
1291 // Pre-allocated objects.
1292 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001293 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001294 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001295
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001296 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001297 }
1298
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001299 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001300 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001301 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001302 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001303 }
1304
1305 public int describeContents() {
1306 return 0;
1307 }
1308
1309 public void writeToParcel(Parcel dest, int flags) {
1310 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001311 int bat = (((int)cmd)&0xff)
1312 | ((((int)batteryLevel)<<8)&0xff00)
1313 | ((((int)batteryStatus)<<16)&0xf0000)
1314 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001315 | ((((int)batteryPlugType)<<24)&0xf000000)
1316 | (wakelockTag != null ? 0x10000000 : 0)
1317 | (wakeReasonTag != null ? 0x20000000 : 0)
1318 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001319 dest.writeInt(bat);
1320 bat = (((int)batteryTemperature)&0xffff)
1321 | ((((int)batteryVoltage)<<16)&0xffff0000);
1322 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001323 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001324 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001325 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001326 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001327 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001328 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001329 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001330 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001331 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001332 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001333 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001334 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001335 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001336 dest.writeLong(currentTime);
1337 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001338 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001339
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001340 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001341 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001342 int bat = src.readInt();
1343 cmd = (byte)(bat&0xff);
1344 batteryLevel = (byte)((bat>>8)&0xff);
1345 batteryStatus = (byte)((bat>>16)&0xf);
1346 batteryHealth = (byte)((bat>>20)&0xf);
1347 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001348 int bat2 = src.readInt();
1349 batteryTemperature = (short)(bat2&0xffff);
1350 batteryVoltage = (char)((bat2>>16)&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001351 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001352 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001353 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001354 wakelockTag = localWakelockTag;
1355 wakelockTag.readFromParcel(src);
1356 } else {
1357 wakelockTag = null;
1358 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001359 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001360 wakeReasonTag = localWakeReasonTag;
1361 wakeReasonTag.readFromParcel(src);
1362 } else {
1363 wakeReasonTag = null;
1364 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001365 if ((bat&0x40000000) != 0) {
1366 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001367 eventTag = localEventTag;
1368 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001369 } else {
1370 eventCode = EVENT_NONE;
1371 eventTag = null;
1372 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001373 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001374 currentTime = src.readLong();
1375 } else {
1376 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001377 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001378 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001379 }
1380
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001381 public void clear() {
1382 time = 0;
1383 cmd = CMD_NULL;
1384 batteryLevel = 0;
1385 batteryStatus = 0;
1386 batteryHealth = 0;
1387 batteryPlugType = 0;
1388 batteryTemperature = 0;
1389 batteryVoltage = 0;
1390 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001391 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001392 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001393 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001394 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001395 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001396 }
1397
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001398 public void setTo(HistoryItem o) {
1399 time = o.time;
1400 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001401 setToCommon(o);
1402 }
1403
1404 public void setTo(long time, byte cmd, HistoryItem o) {
1405 this.time = time;
1406 this.cmd = cmd;
1407 setToCommon(o);
1408 }
1409
1410 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001411 batteryLevel = o.batteryLevel;
1412 batteryStatus = o.batteryStatus;
1413 batteryHealth = o.batteryHealth;
1414 batteryPlugType = o.batteryPlugType;
1415 batteryTemperature = o.batteryTemperature;
1416 batteryVoltage = o.batteryVoltage;
1417 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001418 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001419 if (o.wakelockTag != null) {
1420 wakelockTag = localWakelockTag;
1421 wakelockTag.setTo(o.wakelockTag);
1422 } else {
1423 wakelockTag = null;
1424 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001425 if (o.wakeReasonTag != null) {
1426 wakeReasonTag = localWakeReasonTag;
1427 wakeReasonTag.setTo(o.wakeReasonTag);
1428 } else {
1429 wakeReasonTag = null;
1430 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001431 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001432 if (o.eventTag != null) {
1433 eventTag = localEventTag;
1434 eventTag.setTo(o.eventTag);
1435 } else {
1436 eventTag = null;
1437 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001438 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001439 }
1440
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001441 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001442 return batteryLevel == o.batteryLevel
1443 && batteryStatus == o.batteryStatus
1444 && batteryHealth == o.batteryHealth
1445 && batteryPlugType == o.batteryPlugType
1446 && batteryTemperature == o.batteryTemperature
1447 && batteryVoltage == o.batteryVoltage
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001448 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001449 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001450 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001451 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001452
1453 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001454 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001455 return false;
1456 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001457 if (wakelockTag != o.wakelockTag) {
1458 if (wakelockTag == null || o.wakelockTag == null) {
1459 return false;
1460 }
1461 if (!wakelockTag.equals(o.wakelockTag)) {
1462 return false;
1463 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001464 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001465 if (wakeReasonTag != o.wakeReasonTag) {
1466 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1467 return false;
1468 }
1469 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1470 return false;
1471 }
1472 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001473 if (eventTag != o.eventTag) {
1474 if (eventTag == null || o.eventTag == null) {
1475 return false;
1476 }
1477 if (!eventTag.equals(o.eventTag)) {
1478 return false;
1479 }
1480 }
1481 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001482 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001483 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001484
1485 public final static class HistoryEventTracker {
1486 private final HashMap<String, SparseIntArray>[] mActiveEvents
1487 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1488
1489 public boolean updateState(int code, String name, int uid, int poolIdx) {
1490 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1491 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1492 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1493 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001494 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001495 mActiveEvents[idx] = active;
1496 }
1497 SparseIntArray uids = active.get(name);
1498 if (uids == null) {
1499 uids = new SparseIntArray();
1500 active.put(name, uids);
1501 }
1502 if (uids.indexOfKey(uid) >= 0) {
1503 // Already set, nothing to do!
1504 return false;
1505 }
1506 uids.put(uid, poolIdx);
1507 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1508 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1509 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1510 if (active == null) {
1511 // not currently active, nothing to do.
1512 return false;
1513 }
1514 SparseIntArray uids = active.get(name);
1515 if (uids == null) {
1516 // not currently active, nothing to do.
1517 return false;
1518 }
1519 idx = uids.indexOfKey(uid);
1520 if (idx < 0) {
1521 // not currently active, nothing to do.
1522 return false;
1523 }
1524 uids.removeAt(idx);
1525 if (uids.size() <= 0) {
1526 active.remove(name);
1527 }
1528 }
1529 return true;
1530 }
1531
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001532 public void removeEvents(int code) {
1533 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1534 mActiveEvents[idx] = null;
1535 }
1536
Dianne Hackborn37de0982014-05-09 09:32:18 -07001537 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1538 return mActiveEvents[code];
1539 }
1540 }
1541
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001542 public static final class BitDescription {
1543 public final int mask;
1544 public final int shift;
1545 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001546 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001547 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001548 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001549
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001550 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001551 this.mask = mask;
1552 this.shift = -1;
1553 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001554 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001555 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001556 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001557 }
1558
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001559 public BitDescription(int mask, int shift, String name, String shortName,
1560 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001561 this.mask = mask;
1562 this.shift = shift;
1563 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001564 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001565 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001566 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001567 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001568 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001569
Dianne Hackbornfc064132014-06-02 12:42:12 -07001570 /**
1571 * Don't allow any more batching in to the current history event. This
1572 * is called when printing partial histories, so to ensure that the next
1573 * history event will go in to a new batch after what was printed in the
1574 * last partial history.
1575 */
1576 public abstract void commitCurrentHistoryBatchLocked();
1577
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001578 public abstract int getHistoryTotalSize();
1579
1580 public abstract int getHistoryUsedSize();
1581
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001582 public abstract boolean startIteratingHistoryLocked();
1583
Dianne Hackborn099bc622014-01-22 13:39:16 -08001584 public abstract int getHistoryStringPoolSize();
1585
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001586 public abstract int getHistoryStringPoolBytes();
1587
1588 public abstract String getHistoryTagPoolString(int index);
1589
1590 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001591
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001592 public abstract boolean getNextHistoryLocked(HistoryItem out);
1593
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001594 public abstract void finishIteratingHistoryLocked();
1595
1596 public abstract boolean startIteratingOldHistoryLocked();
1597
1598 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1599
1600 public abstract void finishIteratingOldHistoryLocked();
1601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001603 * Return the base time offset for the battery history.
1604 */
1605 public abstract long getHistoryBaseTime();
1606
1607 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 * Returns the number of times the device has been started.
1609 */
1610 public abstract int getStartCount();
1611
1612 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001613 * 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 -08001614 * running on battery.
1615 *
1616 * {@hide}
1617 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001618 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001620 /**
1621 * Returns the number of times the screen was turned on.
1622 *
1623 * {@hide}
1624 */
1625 public abstract int getScreenOnCount(int which);
1626
Jeff Browne95c3cd2014-05-02 16:59:26 -07001627 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1628
Dianne Hackborn617f8772009-03-31 15:04:46 -07001629 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1630 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1631 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1632 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1633 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
1634
1635 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1636 "dark", "dim", "medium", "light", "bright"
1637 };
1638
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001639 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1640 "0", "1", "2", "3", "4"
1641 };
1642
Dianne Hackborn617f8772009-03-31 15:04:46 -07001643 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001644
Dianne Hackborn617f8772009-03-31 15:04:46 -07001645 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001646 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001647 * the given brightness
1648 *
1649 * {@hide}
1650 */
1651 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001652 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001655 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001656 * running on battery.
1657 *
1658 * {@hide}
1659 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001660 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001661
1662 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001663 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001664 *
1665 * {@hide}
1666 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001667 public abstract int getPowerSaveModeEnabledCount(int which);
1668
1669 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001670 * Constant for device idle mode: not active.
1671 */
1672 public static final int DEVICE_IDLE_MODE_OFF = 0;
1673
1674 /**
1675 * Constant for device idle mode: active in lightweight mode.
1676 */
1677 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1678
1679 /**
1680 * Constant for device idle mode: active in full mode.
1681 */
1682 public static final int DEVICE_IDLE_MODE_FULL = 2;
1683
1684 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001685 * Returns the time in microseconds that device has been in idle mode while
1686 * running on battery.
1687 *
1688 * {@hide}
1689 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001690 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001691
1692 /**
1693 * Returns the number of times that the devie has gone in to idle mode.
1694 *
1695 * {@hide}
1696 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001697 public abstract int getDeviceIdleModeCount(int mode, int which);
1698
1699 /**
1700 * Return the longest duration we spent in a particular device idle mode (fully in the
1701 * mode, not in idle maintenance etc).
1702 */
1703 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001704
1705 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001706 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001707 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001708 * counts all of the time that we consider the device to be idle, whether or not
1709 * it is currently in the actual device idle mode.
1710 *
1711 * {@hide}
1712 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001713 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001714
1715 /**
1716 * Returns the number of times that the devie has started idling.
1717 *
1718 * {@hide}
1719 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001720 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001721
1722 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001723 * Returns the number of times that connectivity state changed.
1724 *
1725 * {@hide}
1726 */
1727 public abstract int getNumConnectivityChange(int which);
1728
1729 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001730 * 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 -08001731 * running on battery.
1732 *
1733 * {@hide}
1734 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001735 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001736
1737 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001738 * Returns the number of times a phone call was activated.
1739 *
1740 * {@hide}
1741 */
1742 public abstract int getPhoneOnCount(int which);
1743
1744 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001745 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001746 * the given signal strength.
1747 *
1748 * {@hide}
1749 */
1750 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001751 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001752
Dianne Hackborn617f8772009-03-31 15:04:46 -07001753 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001754 * Returns the time in microseconds that the phone has been trying to
1755 * acquire a signal.
1756 *
1757 * {@hide}
1758 */
1759 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001760 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001761
1762 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001763 * Returns the number of times the phone has entered the given signal strength.
1764 *
1765 * {@hide}
1766 */
1767 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1768
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001769 /**
1770 * Returns the time in microseconds that the mobile network has been active
1771 * (in a high power state).
1772 *
1773 * {@hide}
1774 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001775 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001776
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001777 /**
1778 * Returns the number of times that the mobile network has transitioned to the
1779 * active state.
1780 *
1781 * {@hide}
1782 */
1783 public abstract int getMobileRadioActiveCount(int which);
1784
1785 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001786 * Returns the time in microseconds that is the difference between the mobile radio
1787 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1788 * from the radio.
1789 *
1790 * {@hide}
1791 */
1792 public abstract long getMobileRadioActiveAdjustedTime(int which);
1793
1794 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001795 * Returns the time in microseconds that the mobile network has been active
1796 * (in a high power state) but not being able to blame on an app.
1797 *
1798 * {@hide}
1799 */
1800 public abstract long getMobileRadioActiveUnknownTime(int which);
1801
1802 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001803 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001804 *
1805 * {@hide}
1806 */
1807 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001808
Dianne Hackborn627bba72009-03-24 22:32:56 -07001809 public static final int DATA_CONNECTION_NONE = 0;
1810 public static final int DATA_CONNECTION_GPRS = 1;
1811 public static final int DATA_CONNECTION_EDGE = 2;
1812 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001813 public static final int DATA_CONNECTION_CDMA = 4;
1814 public static final int DATA_CONNECTION_EVDO_0 = 5;
1815 public static final int DATA_CONNECTION_EVDO_A = 6;
1816 public static final int DATA_CONNECTION_1xRTT = 7;
1817 public static final int DATA_CONNECTION_HSDPA = 8;
1818 public static final int DATA_CONNECTION_HSUPA = 9;
1819 public static final int DATA_CONNECTION_HSPA = 10;
1820 public static final int DATA_CONNECTION_IDEN = 11;
1821 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001822 public static final int DATA_CONNECTION_LTE = 13;
1823 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001824 public static final int DATA_CONNECTION_HSPAP = 15;
1825 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001826
Dianne Hackborn627bba72009-03-24 22:32:56 -07001827 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001828 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001829 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001830 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001831 };
1832
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001833 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001834
1835 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001836 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001837 * the given data connection.
1838 *
1839 * {@hide}
1840 */
1841 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001842 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001845 * Returns the number of times the phone has entered the given data
1846 * connection type.
1847 *
1848 * {@hide}
1849 */
1850 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001851
Dianne Hackborn3251b902014-06-20 14:40:53 -07001852 public static final int WIFI_SUPPL_STATE_INVALID = 0;
1853 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
1854 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
1855 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
1856 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
1857 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
1858 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
1859 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
1860 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
1861 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
1862 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
1863 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
1864 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
1865
1866 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
1867
1868 static final String[] WIFI_SUPPL_STATE_NAMES = {
1869 "invalid", "disconn", "disabled", "inactive", "scanning",
1870 "authenticating", "associating", "associated", "4-way-handshake",
1871 "group-handshake", "completed", "dormant", "uninit"
1872 };
1873
1874 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
1875 "inv", "dsc", "dis", "inact", "scan",
1876 "auth", "ascing", "asced", "4-way",
1877 "group", "compl", "dorm", "uninit"
1878 };
1879
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001880 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
1881 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001882 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001883 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
1884 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001885 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001886 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
1887 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
1888 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001889 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001890 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001891 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001892 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001893 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
1894 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001895 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
1896 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
1897 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
1898 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
1899 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
1900 new String[] {"in", "out", "emergency", "off"},
1901 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001902 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
1903 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
1904 SignalStrength.SIGNAL_STRENGTH_NAMES,
1905 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001906 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
1907 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
1908 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001909 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07001910
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001911 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
1912 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001913 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001914 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001915 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001916 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001917 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001918 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
1919 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
1920 new String[] { "off", "light", "full", "???" },
1921 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001922 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
1923 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
1924 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07001925 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
1926 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
1927 new String[] { "0", "1", "2", "3", "4" },
1928 new String[] { "0", "1", "2", "3", "4" }),
1929 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
1930 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
1931 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07001932 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001933 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001934 };
1935
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001936 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001937 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001938 "active", "pkginst", "pkgunin", "alarm", "stats", "inactive", "active", "tmpwhitelist",
1939 "screenwake",
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001940 };
1941
1942 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001943 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001944 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
1945 "Esw",
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001946 };
1947
Dianne Hackborn617f8772009-03-31 15:04:46 -07001948 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001949 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001950 * running on battery.
1951 *
1952 * {@hide}
1953 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001954 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001955
1956 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001957 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001958 * been in the running state while the device was running on battery.
1959 *
1960 * {@hide}
1961 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001962 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001963
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001964 public static final int WIFI_STATE_OFF = 0;
1965 public static final int WIFI_STATE_OFF_SCANNING = 1;
1966 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
1967 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
1968 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
1969 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
1970 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
1971 public static final int WIFI_STATE_SOFT_AP = 7;
1972
1973 static final String[] WIFI_STATE_NAMES = {
1974 "off", "scanning", "no_net", "disconn",
1975 "sta", "p2p", "sta_p2p", "soft_ap"
1976 };
1977
1978 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
1979
1980 /**
1981 * Returns the time in microseconds that WiFi has been running in the given state.
1982 *
1983 * {@hide}
1984 */
1985 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001986 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001987
1988 /**
1989 * Returns the number of times that WiFi has entered the given state.
1990 *
1991 * {@hide}
1992 */
1993 public abstract int getWifiStateCount(int wifiState, int which);
1994
The Android Open Source Project10592532009-03-18 17:39:46 -07001995 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07001996 * Returns the time in microseconds that the wifi supplicant has been
1997 * in a given state.
1998 *
1999 * {@hide}
2000 */
2001 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2002
2003 /**
2004 * Returns the number of times that the wifi supplicant has transitioned
2005 * to a given state.
2006 *
2007 * {@hide}
2008 */
2009 public abstract int getWifiSupplStateCount(int state, int which);
2010
2011 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2012
2013 /**
2014 * Returns the time in microseconds that WIFI has been running with
2015 * the given signal strength.
2016 *
2017 * {@hide}
2018 */
2019 public abstract long getWifiSignalStrengthTime(int strengthBin,
2020 long elapsedRealtimeUs, int which);
2021
2022 /**
2023 * Returns the number of times WIFI has entered the given signal strength.
2024 *
2025 * {@hide}
2026 */
2027 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2028
2029 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002030 * Returns the time in microseconds that the flashlight has been on while the device was
2031 * running on battery.
2032 *
2033 * {@hide}
2034 */
2035 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2036
2037 /**
2038 * Returns the number of times that the flashlight has been turned on while the device was
2039 * running on battery.
2040 *
2041 * {@hide}
2042 */
2043 public abstract long getFlashlightOnCount(int which);
2044
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002045 /**
2046 * Returns the time in microseconds that the camera has been on while the device was
2047 * running on battery.
2048 *
2049 * {@hide}
2050 */
2051 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2052
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002053 /**
2054 * Returns the time in microseconds that bluetooth scans were running while the device was
2055 * on battery.
2056 *
2057 * {@hide}
2058 */
2059 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002060
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002061 public static final int NETWORK_MOBILE_RX_DATA = 0;
2062 public static final int NETWORK_MOBILE_TX_DATA = 1;
2063 public static final int NETWORK_WIFI_RX_DATA = 2;
2064 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002065 public static final int NETWORK_BT_RX_DATA = 4;
2066 public static final int NETWORK_BT_TX_DATA = 5;
2067 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_BT_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002068
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002069 public abstract long getNetworkActivityBytes(int type, int which);
2070 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002071
Adam Lesinskie08af192015-03-25 16:42:59 -07002072 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002073 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002074 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002075 * actual power data.
2076 */
2077 public abstract boolean hasWifiActivityReporting();
2078
2079 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002080 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2081 * in various radio controller states, such as transmit, receive, and idle.
2082 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002083 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002084 public abstract ControllerActivityCounter getWifiControllerActivity();
2085
2086 /**
2087 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2088 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2089 * actual power data.
2090 */
2091 public abstract boolean hasBluetoothActivityReporting();
2092
2093 /**
2094 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2095 * in various radio controller states, such as transmit, receive, and idle.
2096 * @return non-null {@link ControllerActivityCounter}
2097 */
2098 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2099
2100 /**
2101 * Returns true if the BatteryStats object has detailed modem power reports.
2102 * When true, calling {@link #getModemControllerActivity()} will yield the
2103 * actual power data.
2104 */
2105 public abstract boolean hasModemActivityReporting();
2106
2107 /**
2108 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2109 * in various radio controller states, such as transmit, receive, and idle.
2110 * @return non-null {@link ControllerActivityCounter}
2111 */
2112 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002113
The Android Open Source Project10592532009-03-18 17:39:46 -07002114 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002115 * Return the wall clock time when battery stats data collection started.
2116 */
2117 public abstract long getStartClockTime();
2118
2119 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002120 * Return platform version tag that we were running in when the battery stats started.
2121 */
2122 public abstract String getStartPlatformVersion();
2123
2124 /**
2125 * Return platform version tag that we were running in when the battery stats ended.
2126 */
2127 public abstract String getEndPlatformVersion();
2128
2129 /**
2130 * Return the internal version code of the parcelled format.
2131 */
2132 public abstract int getParcelVersion();
2133
2134 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 * Return whether we are currently running on battery.
2136 */
2137 public abstract boolean getIsOnBattery();
2138
2139 /**
2140 * Returns a SparseArray containing the statistics for each uid.
2141 */
2142 public abstract SparseArray<? extends Uid> getUidStats();
2143
2144 /**
2145 * Returns the current battery uptime in microseconds.
2146 *
2147 * @param curTime the amount of elapsed realtime in microseconds.
2148 */
2149 public abstract long getBatteryUptime(long curTime);
2150
2151 /**
2152 * Returns the current battery realtime in microseconds.
2153 *
2154 * @param curTime the amount of elapsed realtime in microseconds.
2155 */
2156 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07002157
2158 /**
Evan Millar633a1742009-04-02 16:36:33 -07002159 * Returns the battery percentage level at the last time the device was unplugged from power, or
2160 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002161 */
Evan Millar633a1742009-04-02 16:36:33 -07002162 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07002163
2164 /**
Evan Millar633a1742009-04-02 16:36:33 -07002165 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2166 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002167 */
Evan Millar633a1742009-04-02 16:36:33 -07002168 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169
2170 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002171 * Get the amount the battery has discharged since the stats were
2172 * last reset after charging, as a lower-end approximation.
2173 */
2174 public abstract int getLowDischargeAmountSinceCharge();
2175
2176 /**
2177 * Get the amount the battery has discharged since the stats were
2178 * last reset after charging, as an upper-end approximation.
2179 */
2180 public abstract int getHighDischargeAmountSinceCharge();
2181
2182 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002183 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2184 */
2185 public abstract int getDischargeAmount(int which);
2186
2187 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002188 * Get the amount the battery has discharged while the screen was on,
2189 * since the last time power was unplugged.
2190 */
2191 public abstract int getDischargeAmountScreenOn();
2192
2193 /**
2194 * Get the amount the battery has discharged while the screen was on,
2195 * since the last time the device was charged.
2196 */
2197 public abstract int getDischargeAmountScreenOnSinceCharge();
2198
2199 /**
2200 * Get the amount the battery has discharged while the screen was off,
2201 * since the last time power was unplugged.
2202 */
2203 public abstract int getDischargeAmountScreenOff();
2204
2205 /**
2206 * Get the amount the battery has discharged while the screen was off,
2207 * since the last time the device was charged.
2208 */
2209 public abstract int getDischargeAmountScreenOffSinceCharge();
2210
2211 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 * Returns the total, last, or current battery uptime in microseconds.
2213 *
2214 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002215 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 */
2217 public abstract long computeBatteryUptime(long curTime, int which);
2218
2219 /**
2220 * Returns the total, last, or current battery realtime in microseconds.
2221 *
2222 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002223 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 */
2225 public abstract long computeBatteryRealtime(long curTime, int which);
2226
2227 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002228 * Returns the total, last, or current battery screen off uptime in microseconds.
2229 *
2230 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002231 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002232 */
2233 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2234
2235 /**
2236 * Returns the total, last, or current battery screen off realtime in microseconds.
2237 *
2238 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002239 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002240 */
2241 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2242
2243 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 * Returns the total, last, or current uptime in microseconds.
2245 *
2246 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002247 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 */
2249 public abstract long computeUptime(long curTime, int which);
2250
2251 /**
2252 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002253 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002255 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 */
2257 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002258
2259 /**
2260 * Compute an approximation for how much run time (in microseconds) is remaining on
2261 * the battery. Returns -1 if no time can be computed: either there is not
2262 * enough current data to make a decision, or the battery is currently
2263 * charging.
2264 *
2265 * @param curTime The current elepsed realtime in microseconds.
2266 */
2267 public abstract long computeBatteryTimeRemaining(long curTime);
2268
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002269 // The part of a step duration that is the actual time.
2270 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2271
2272 // Bits in a step duration that are the new battery level we are at.
2273 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002274 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002275
2276 // Bits in a step duration that are the initial mode we were in at that step.
2277 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002278 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002279
2280 // Bits in a step duration that indicate which modes changed during that step.
2281 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002282 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002283
2284 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2285 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2286
2287 // Step duration mode: power save is on.
2288 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2289
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002290 // Step duration mode: device is currently in idle mode.
2291 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2292
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002293 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2294 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002295 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2296 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002297 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2298 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2299 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2300 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2301 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002302 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2303 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002304 };
2305 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2306 (Display.STATE_OFF-1),
2307 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002308 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002309 (Display.STATE_ON-1),
2310 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2311 (Display.STATE_DOZE-1),
2312 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2313 (Display.STATE_DOZE_SUSPEND-1),
2314 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002315 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002316 };
2317 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2318 "screen off",
2319 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002320 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002321 "screen on",
2322 "screen on power save",
2323 "screen doze",
2324 "screen doze power save",
2325 "screen doze-suspend",
2326 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002327 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002328 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002329
2330 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002331 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002332 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002333 public abstract LevelStepTracker getDischargeLevelStepTracker();
2334
2335 /**
2336 * Return the array of daily discharge step durations.
2337 */
2338 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002339
2340 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002341 * Compute an approximation for how much time (in microseconds) remains until the battery
2342 * is fully charged. Returns -1 if no time can be computed: either there is not
2343 * enough current data to make a decision, or the battery is currently
2344 * discharging.
2345 *
2346 * @param curTime The current elepsed realtime in microseconds.
2347 */
2348 public abstract long computeChargeTimeRemaining(long curTime);
2349
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002350 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002351 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002352 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002353 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002354
2355 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002356 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002357 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002358 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002359
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002360 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2361
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002362 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002363
Evan Millarc64edde2009-04-18 12:26:32 -07002364 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002366 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2367
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002368 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 long days = seconds / (60 * 60 * 24);
2370 if (days != 0) {
2371 out.append(days);
2372 out.append("d ");
2373 }
2374 long used = days * 60 * 60 * 24;
2375
2376 long hours = (seconds - used) / (60 * 60);
2377 if (hours != 0 || used != 0) {
2378 out.append(hours);
2379 out.append("h ");
2380 }
2381 used += hours * 60 * 60;
2382
2383 long mins = (seconds-used) / 60;
2384 if (mins != 0 || used != 0) {
2385 out.append(mins);
2386 out.append("m ");
2387 }
2388 used += mins * 60;
2389
2390 if (seconds != 0 || used != 0) {
2391 out.append(seconds-used);
2392 out.append("s ");
2393 }
2394 }
2395
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002396 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002398 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 sb.append(time - (sec * 1000));
2400 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 }
2402
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002403 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002404 long sec = time / 1000;
2405 formatTimeRaw(sb, sec);
2406 sb.append(time - (sec * 1000));
2407 sb.append("ms");
2408 }
2409
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002410 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002412 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 }
2414 float perc = ((float)num) / ((float)den) * 100;
2415 mFormatBuilder.setLength(0);
2416 mFormatter.format("%.1f%%", perc);
2417 return mFormatBuilder.toString();
2418 }
2419
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002420 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002421 mFormatBuilder.setLength(0);
2422
2423 if (bytes < BYTES_PER_KB) {
2424 return bytes + "B";
2425 } else if (bytes < BYTES_PER_MB) {
2426 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2427 return mFormatBuilder.toString();
2428 } else if (bytes < BYTES_PER_GB){
2429 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2430 return mFormatBuilder.toString();
2431 } else {
2432 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2433 return mFormatBuilder.toString();
2434 }
2435 }
2436
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002437 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002438 if (timer != null) {
2439 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002440 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002441 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2442 return totalTimeMillis;
2443 }
2444 return 0;
2445 }
2446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 /**
2448 *
2449 * @param sb a StringBuilder object.
2450 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002451 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002453 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002454 * @param linePrefix a String to be prepended to each line of output.
2455 * @return the line prefix
2456 */
2457 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002458 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002461 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002462
Evan Millarc64edde2009-04-18 12:26:32 -07002463 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 if (totalTimeMillis != 0) {
2465 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002466 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002467 if (name != null) {
2468 sb.append(name);
2469 sb.append(' ');
2470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 sb.append('(');
2472 sb.append(count);
2473 sb.append(" times)");
2474 return ", ";
2475 }
2476 }
2477 return linePrefix;
2478 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002479
2480 /**
2481 *
2482 * @param pw a PrintWriter object to print to.
2483 * @param sb a StringBuilder object.
2484 * @param timer a Timer object contining the wakelock times.
2485 * @param rawRealtime the current on-battery time in microseconds.
2486 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2487 * @param prefix a String to be prepended to each line of output.
2488 * @param type the name of the timer.
2489 */
2490 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
2491 long rawRealtime, int which, String prefix, String type) {
2492 if (timer != null) {
2493 // Convert from microseconds to milliseconds with rounding
2494 final long totalTime = (timer.getTotalTimeLocked(
2495 rawRealtime, which) + 500) / 1000;
2496 final int count = timer.getCountLocked(which);
2497 if (totalTime != 0) {
2498 sb.setLength(0);
2499 sb.append(prefix);
2500 sb.append(" ");
2501 sb.append(type);
2502 sb.append(": ");
2503 formatTimeMs(sb, totalTime);
2504 sb.append("realtime (");
2505 sb.append(count);
2506 sb.append(" times)");
2507 pw.println(sb.toString());
2508 return true;
2509 }
2510 }
2511 return false;
2512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513
2514 /**
2515 * Checkin version of wakelock printer. Prints simple comma-separated list.
2516 *
2517 * @param sb a StringBuilder object.
2518 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002519 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002521 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 * @param linePrefix a String to be prepended to each line of output.
2523 * @return the line prefix
2524 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002525 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2526 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 long totalTimeMicros = 0;
2528 int count = 0;
2529 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002530 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07002531 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 }
2533 sb.append(linePrefix);
2534 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2535 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002536 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 sb.append(count);
2538 return ",";
2539 }
2540
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002541 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
2542 String type) {
2543 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2544 pw.print(',');
2545 pw.print(uid);
2546 pw.print(',');
2547 pw.print(category);
2548 pw.print(',');
2549 pw.print(type);
2550 }
2551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 /**
2553 * Dump a comma-separated line of values for terse checkin mode.
2554 *
2555 * @param pw the PageWriter to dump log to
2556 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2557 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2558 * @param args type-dependent data arguments
2559 */
2560 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
2561 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002562 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002563 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002564 pw.print(',');
2565 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002567 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002569
2570 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002571 * Dump a given timer stat for terse checkin mode.
2572 *
2573 * @param pw the PageWriter to dump log to
2574 * @param uid the UID to log
2575 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2576 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2577 * @param timer a {@link Timer} to dump stats for
2578 * @param rawRealtime the current elapsed realtime of the system in microseconds
2579 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2580 */
2581 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2582 Timer timer, long rawRealtime, int which) {
2583 if (timer != null) {
2584 // Convert from microseconds to milliseconds with rounding
2585 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2586 / 1000;
2587 final int count = timer.getCountLocked(which);
2588 if (totalTime != 0) {
2589 dumpLine(pw, uid, category, type, totalTime, count);
2590 }
2591 }
2592 }
2593
2594 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002595 * Checks if the ControllerActivityCounter has any data worth dumping.
2596 */
2597 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
2598 if (counter == null) {
2599 return false;
2600 }
2601
2602 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
2603 || counter.getRxTimeCounter().getCountLocked(which) != 0
2604 || counter.getPowerCounter().getCountLocked(which) != 0) {
2605 return true;
2606 }
2607
2608 for (LongCounter c : counter.getTxTimeCounters()) {
2609 if (c.getCountLocked(which) != 0) {
2610 return true;
2611 }
2612 }
2613 return false;
2614 }
2615
2616 /**
2617 * Dumps the ControllerActivityCounter if it has any data worth dumping.
2618 * The order of the arguments in the final check in line is:
2619 *
2620 * idle, rx, power, tx...
2621 *
2622 * where tx... is one or more transmit level times.
2623 */
2624 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
2625 String type,
2626 ControllerActivityCounter counter,
2627 int which) {
2628 if (!controllerActivityHasData(counter, which)) {
2629 return;
2630 }
2631
2632 dumpLineHeader(pw, uid, category, type);
2633 pw.print(",");
2634 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
2635 pw.print(",");
2636 pw.print(counter.getRxTimeCounter().getCountLocked(which));
2637 pw.print(",");
2638 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
2639 for (LongCounter c : counter.getTxTimeCounters()) {
2640 pw.print(",");
2641 pw.print(c.getCountLocked(which));
2642 }
2643 pw.println();
2644 }
2645
2646 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
2647 String prefix, String controllerName,
2648 ControllerActivityCounter counter,
2649 int which) {
2650 if (controllerActivityHasData(counter, which)) {
2651 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
2652 }
2653 }
2654
2655 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
2656 String controllerName,
2657 ControllerActivityCounter counter, int which) {
2658 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
2659 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
2660 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
2661 long totalTxTimeMs = 0;
2662 for (LongCounter txState : counter.getTxTimeCounters()) {
2663 totalTxTimeMs += txState.getCountLocked(which);
2664 }
2665
2666 final long totalTimeMs = idleTimeMs + rxTimeMs + totalTxTimeMs;
2667
2668 sb.setLength(0);
2669 sb.append(prefix);
2670 sb.append(" ");
2671 sb.append(controllerName);
2672 sb.append(" Idle time: ");
2673 formatTimeMs(sb, idleTimeMs);
2674 sb.append("(");
2675 sb.append(formatRatioLocked(idleTimeMs, totalTimeMs));
2676 sb.append(")");
2677 pw.println(sb.toString());
2678
2679 sb.setLength(0);
2680 sb.append(prefix);
2681 sb.append(" ");
2682 sb.append(controllerName);
2683 sb.append(" Rx time: ");
2684 formatTimeMs(sb, rxTimeMs);
2685 sb.append("(");
2686 sb.append(formatRatioLocked(rxTimeMs, totalTimeMs));
2687 sb.append(")");
2688 pw.println(sb.toString());
2689
2690 sb.setLength(0);
2691 sb.append(prefix);
2692 sb.append(" ");
2693 sb.append(controllerName);
2694 sb.append(" Tx time: ");
2695 formatTimeMs(sb, totalTxTimeMs);
2696 sb.append("(");
2697 sb.append(formatRatioLocked(totalTxTimeMs, totalTimeMs));
2698 sb.append(")");
2699 pw.println(sb.toString());
2700
2701 final int numTxLvls = counter.getTxTimeCounters().length;
2702 if (numTxLvls > 1) {
2703 for (int lvl = 0; lvl < numTxLvls; lvl++) {
2704 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
2705 sb.setLength(0);
2706 sb.append(prefix);
2707 sb.append(" [");
2708 sb.append(lvl);
2709 sb.append("] ");
2710 formatTimeMs(sb, txLvlTimeMs);
2711 sb.append("(");
2712 sb.append(formatRatioLocked(txLvlTimeMs, totalTxTimeMs));
2713 sb.append(")");
2714 pw.println(sb.toString());
2715 }
2716 }
2717
2718 sb.setLength(0);
2719 sb.append(prefix);
2720 sb.append(" ");
2721 sb.append(controllerName);
2722 sb.append(" Power drain: ").append(
2723 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
2724 sb.append("mAh");
2725 pw.println(sb.toString());
2726 }
2727
2728 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07002729 * Temporary for settings.
2730 */
2731 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
2732 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2733 }
2734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 /**
2736 * Checkin server version of dump to produce more compact, computer-readable log.
2737 *
2738 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07002740 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
2741 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2743 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2744 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2746 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002747 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2748 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2749 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 final long totalRealtime = computeRealtime(rawRealtime, which);
2751 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002752 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002753 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002754 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002755 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
2756 rawRealtime, which);
2757 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_FULL,
2758 rawRealtime, which);
2759 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
2760 rawRealtime, which);
2761 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_FULL,
2762 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002763 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002764 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002765
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002766 final StringBuilder sb = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002768 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07002769 final int NU = uidStats.size();
2770
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002771 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 // Dump "battery" stat
2774 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002775 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07002776 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002777 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002778 getStartClockTime(),
2779 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002781 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07002782 long fullWakeLockTimeTotal = 0;
2783 long partialWakeLockTimeTotal = 0;
2784
2785 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002786 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002787
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002788 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
2789 = u.getWakelockStats();
2790 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
2791 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07002792
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002793 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
2794 if (fullWakeTimer != null) {
2795 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
2796 which);
2797 }
2798
2799 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
2800 if (partialWakeTimer != null) {
2801 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
2802 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07002803 }
2804 }
2805 }
Adam Lesinskie283d332015-04-16 12:29:25 -07002806
2807 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002808 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2809 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2810 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2811 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2812 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2813 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2814 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2815 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002816 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
2817 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002818 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
2819 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002820 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
2821 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002822
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002823 // Dump Modem controller stats
2824 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
2825 getModemControllerActivity(), which);
2826
Adam Lesinskie283d332015-04-16 12:29:25 -07002827 // Dump Wifi controller stats
2828 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
2829 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002830 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08002831 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002832
2833 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
2834 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07002835
2836 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002837 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
2838 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07002839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 // Dump misc stats
2841 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07002842 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07002843 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07002844 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07002845 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002846 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
2847 getDeviceIdleModeCount(DEVICE_IDLE_MODE_FULL, which), deviceIdlingTime / 1000,
2848 getDeviceIdlingCount(DEVICE_IDLE_MODE_FULL, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07002849 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002850 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
2851 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
2852 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
2853 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
2854 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_FULL));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002855
2856 // Dump screen brightness stats
2857 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
2858 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002859 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07002860 }
2861 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07002862
Dianne Hackborn627bba72009-03-24 22:32:56 -07002863 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08002864 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
2865 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002866 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07002867 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07002868 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002869 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002870 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08002871 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07002872 args[i] = getPhoneSignalStrengthCount(i, which);
2873 }
2874 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002875
Dianne Hackborn627bba72009-03-24 22:32:56 -07002876 // Dump network type stats
2877 args = new Object[NUM_DATA_CONNECTION_TYPES];
2878 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002879 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07002880 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07002881 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
2882 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
2883 args[i] = getPhoneDataConnectionCount(i, which);
2884 }
2885 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002886
2887 // Dump wifi state stats
2888 args = new Object[NUM_WIFI_STATES];
2889 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002890 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002891 }
2892 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
2893 for (int i=0; i<NUM_WIFI_STATES; i++) {
2894 args[i] = getWifiStateCount(i, which);
2895 }
2896 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
2897
Dianne Hackborn3251b902014-06-20 14:40:53 -07002898 // Dump wifi suppl state stats
2899 args = new Object[NUM_WIFI_SUPPL_STATES];
2900 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
2901 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
2902 }
2903 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
2904 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
2905 args[i] = getWifiSupplStateCount(i, which);
2906 }
2907 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
2908
2909 // Dump wifi signal strength stats
2910 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
2911 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2912 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
2913 }
2914 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
2915 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2916 args[i] = getWifiSignalStrengthCount(i, which);
2917 }
2918 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
2919
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002920 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002921 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07002922 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07002923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002925 if (which == STATS_SINCE_UNPLUGGED) {
2926 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
2927 getDischargeStartLevel()-getDischargeCurrentLevel(),
2928 getDischargeStartLevel()-getDischargeCurrentLevel(),
2929 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
2930 } else {
2931 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
2932 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002933 getDischargeAmountScreenOnSinceCharge(),
2934 getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002935 }
2936
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002937 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002938 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002939 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002940 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002941 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002942 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002943 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002944 sb.toString());
2945 }
Evan Millarc64edde2009-04-18 12:26:32 -07002946 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002947 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002948 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002949 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
2950 // Not doing the regular wake lock formatting to remain compatible
2951 // with the old checkin format.
2952 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
2953 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002954 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002955 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002956 }
2957 }
Evan Millarc64edde2009-04-18 12:26:32 -07002958 }
2959
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002960 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002961 helper.create(this);
2962 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002963 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002964 if (sippers != null && sippers.size() > 0) {
2965 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
2966 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08002967 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002968 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
2969 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
2970 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002971 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002972 int uid = 0;
2973 String label;
2974 switch (bs.drainType) {
2975 case IDLE:
2976 label="idle";
2977 break;
2978 case CELL:
2979 label="cell";
2980 break;
2981 case PHONE:
2982 label="phone";
2983 break;
2984 case WIFI:
2985 label="wifi";
2986 break;
2987 case BLUETOOTH:
2988 label="blue";
2989 break;
2990 case SCREEN:
2991 label="scrn";
2992 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002993 case FLASHLIGHT:
2994 label="flashlight";
2995 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002996 case APP:
2997 uid = bs.uidObj.getUid();
2998 label = "uid";
2999 break;
3000 case USER:
3001 uid = UserHandle.getUid(bs.userId, 0);
3002 label = "user";
3003 break;
3004 case UNACCOUNTED:
3005 label = "unacc";
3006 break;
3007 case OVERCOUNTED:
3008 label = "over";
3009 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003010 case CAMERA:
3011 label = "camera";
3012 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003013 default:
3014 label = "???";
3015 }
3016 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Adam Lesinskie08af192015-03-25 16:42:59 -07003017 BatteryStatsHelper.makemAh(bs.totalPowerMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003018 }
3019 }
3020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 for (int iu = 0; iu < NU; iu++) {
3022 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003023 if (reqUid >= 0 && uid != reqUid) {
3024 continue;
3025 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003026 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003029 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3030 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3031 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3032 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3033 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3034 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3035 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3036 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
3037 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3038 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003039 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3040 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003041 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3042 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003043 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
3044 || btBytesRx > 0 || btBytesTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003045 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3046 wifiBytesRx, wifiBytesTx,
3047 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003048 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003049 mobileActiveTime, mobileActiveCount,
3050 btBytesRx, btBytesTx);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003051 }
3052
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003053 // Dump modem controller data, per UID.
3054 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3055 u.getModemControllerActivity(), which);
3056
3057 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003058 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3059 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3060 final int wifiScanCount = u.getWifiScanCount(which);
3061 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003062 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003063 || uidWifiRunningTime != 0) {
3064 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3065 uidWifiRunningTime, wifiScanCount,
3066 /* legacy fields follow, keep at 0 */ 0, 0, 0, 0);
The Android Open Source Project10592532009-03-18 17:39:46 -07003067 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003068
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003069 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3070 u.getWifiControllerActivity(), which);
3071
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003072 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3073 u.getBluetoothControllerActivity(), which);
3074
Dianne Hackborn617f8772009-03-31 15:04:46 -07003075 if (u.hasUserActivity()) {
3076 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3077 boolean hasData = false;
3078 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3079 int val = u.getUserActivityCount(i, which);
3080 args[i] = val;
3081 if (val != 0) hasData = true;
3082 }
3083 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003084 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003085 }
3086 }
3087
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003088 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3089 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3090 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3091 String linePrefix = "";
3092 sb.setLength(0);
3093 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3094 rawRealtime, "f", which, linePrefix);
3095 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
3096 rawRealtime, "p", which, linePrefix);
3097 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3098 rawRealtime, "w", which, linePrefix);
3099
3100 // Only log if we had at lease one wakelock...
3101 if (sb.length() > 0) {
3102 String name = wakelocks.keyAt(iw);
3103 if (name.indexOf(',') >= 0) {
3104 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003105 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003106 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 }
3108 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003109
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003110 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3111 for (int isy=syncs.size()-1; isy>=0; isy--) {
3112 final Timer timer = syncs.valueAt(isy);
3113 // Convert from microseconds to milliseconds with rounding
3114 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3115 final int count = timer.getCountLocked(which);
3116 if (totalTime != 0) {
3117 dumpLine(pw, uid, category, SYNC_DATA, syncs.keyAt(isy), totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003118 }
3119 }
3120
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003121 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3122 for (int ij=jobs.size()-1; ij>=0; ij--) {
3123 final Timer timer = jobs.valueAt(ij);
3124 // Convert from microseconds to milliseconds with rounding
3125 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3126 final int count = timer.getCountLocked(which);
3127 if (totalTime != 0) {
3128 dumpLine(pw, uid, category, JOB_DATA, jobs.keyAt(ij), totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003129 }
3130 }
3131
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003132 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3133 rawRealtime, which);
3134 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3135 rawRealtime, which);
3136 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3137 rawRealtime, which);
3138 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3139 rawRealtime, which);
3140
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003141 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3142 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003143 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003144 final Uid.Sensor se = sensors.valueAt(ise);
3145 final int sensorNumber = sensors.keyAt(ise);
3146 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003147 if (timer != null) {
3148 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003149 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3150 / 1000;
3151 final int count = timer.getCountLocked(which);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003152 if (totalTime != 0) {
3153 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
3154 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003155 }
3156 }
3157
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003158 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3159 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003160
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003161 dumpTimer(pw, uid, category, FOREGROUND_DATA, u.getForegroundActivityTimer(),
3162 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003163
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003164 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003165 long totalStateTime = 0;
3166 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003167 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3168 totalStateTime += time;
3169 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003170 }
3171 if (totalStateTime > 0) {
3172 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3173 }
3174
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003175 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3176 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
3177 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
3178 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
3179 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
3180 powerCpuMaUs / 1000);
3181 }
3182
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003183 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3184 = u.getProcessStats();
3185 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3186 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003187
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003188 final long userMillis = ps.getUserTime(which);
3189 final long systemMillis = ps.getSystemTime(which);
3190 final long foregroundMillis = ps.getForegroundTime(which);
3191 final int starts = ps.getStarts(which);
3192 final int numCrashes = ps.getNumCrashes(which);
3193 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003194
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003195 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3196 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
3197 dumpLine(pw, uid, category, PROCESS_DATA, processStats.keyAt(ipr), userMillis,
3198 systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 }
3200 }
3201
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003202 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3203 = u.getPackageStats();
3204 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3205 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3206 int wakeups = 0;
3207 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3208 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
3209 wakeups += alarms.valueAt(iwa).getCountLocked(which);
3210 }
3211 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3212 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3213 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3214 final long startTime = ss.getStartTime(batteryUptime, which);
3215 final int starts = ss.getStarts(which);
3216 final int launches = ss.getLaunches(which);
3217 if (startTime != 0 || starts != 0 || launches != 0) {
3218 dumpLine(pw, uid, category, APK_DATA,
3219 wakeups, // wakeup alarms
3220 packageStats.keyAt(ipkg), // Apk
3221 serviceStats.keyAt(isvc), // service
3222 startTime / 1000, // time spent started, in ms
3223 starts,
3224 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 }
3226 }
3227 }
3228 }
3229 }
3230
Dianne Hackborn81038902012-11-26 17:04:09 -08003231 static final class TimerEntry {
3232 final String mName;
3233 final int mId;
3234 final BatteryStats.Timer mTimer;
3235 final long mTime;
3236 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3237 mName = name;
3238 mId = id;
3239 mTimer = timer;
3240 mTime = time;
3241 }
3242 }
3243
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003244 private void printmAh(PrintWriter printer, double power) {
3245 printer.print(BatteryStatsHelper.makemAh(power));
3246 }
3247
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003248 private void printmAh(StringBuilder sb, double power) {
3249 sb.append(BatteryStatsHelper.makemAh(power));
3250 }
3251
Dianne Hackbornd953c532014-08-16 18:17:38 -07003252 /**
3253 * Temporary for settings.
3254 */
3255 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3256 int reqUid) {
3257 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3258 }
3259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003261 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003262 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3264 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
3265 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266
3267 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3268 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3269 final long totalRealtime = computeRealtime(rawRealtime, which);
3270 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003271 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3272 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3273 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003274 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3275 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003276
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003277 final StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07003278
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003279 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003280 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003282 sb.setLength(0);
3283 sb.append(prefix);
3284 sb.append(" Time on battery: ");
3285 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3286 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3287 sb.append(") realtime, ");
3288 formatTimeMs(sb, whichBatteryUptime / 1000);
3289 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3290 sb.append(") uptime");
3291 pw.println(sb.toString());
3292 sb.setLength(0);
3293 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003294 sb.append(" Time on battery screen off: ");
3295 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3296 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3297 sb.append(") realtime, ");
3298 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3299 sb.append("(");
3300 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3301 sb.append(") uptime");
3302 pw.println(sb.toString());
3303 sb.setLength(0);
3304 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003305 sb.append(" Total run time: ");
3306 formatTimeMs(sb, totalRealtime / 1000);
3307 sb.append("realtime, ");
3308 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003309 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003310 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003311 if (batteryTimeRemaining >= 0) {
3312 sb.setLength(0);
3313 sb.append(prefix);
3314 sb.append(" Battery time remaining: ");
3315 formatTimeMs(sb, batteryTimeRemaining / 1000);
3316 pw.println(sb.toString());
3317 }
3318 if (chargeTimeRemaining >= 0) {
3319 sb.setLength(0);
3320 sb.append(prefix);
3321 sb.append(" Charge time remaining: ");
3322 formatTimeMs(sb, chargeTimeRemaining / 1000);
3323 pw.println(sb.toString());
3324 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003325 pw.print(" Start clock time: ");
3326 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3327
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003328 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003329 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003330 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003331 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3332 rawRealtime, which);
3333 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_FULL,
3334 rawRealtime, which);
3335 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3336 rawRealtime, which);
3337 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_FULL,
3338 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003339 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3340 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3341 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003342 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003343 sb.append(prefix);
3344 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3345 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003346 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003347 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3348 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003349 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003350 pw.println(sb.toString());
3351 sb.setLength(0);
3352 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003353 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003354 boolean didOne = false;
3355 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003356 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003357 if (time == 0) {
3358 continue;
3359 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003360 sb.append("\n ");
3361 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003362 didOne = true;
3363 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3364 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003365 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003366 sb.append("(");
3367 sb.append(formatRatioLocked(time, screenOnTime));
3368 sb.append(")");
3369 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003370 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003371 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003372 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003373 sb.setLength(0);
3374 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003375 sb.append(" Power save mode enabled: ");
3376 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003377 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003378 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003379 sb.append(")");
3380 pw.println(sb.toString());
3381 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003382 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003383 sb.setLength(0);
3384 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003385 sb.append(" Device light idling: ");
3386 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003387 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003388 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
3389 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003390 sb.append("x");
3391 pw.println(sb.toString());
3392 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003393 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003394 sb.setLength(0);
3395 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003396 sb.append(" Idle mode light time: ");
3397 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003398 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003399 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
3400 sb.append(") ");
3401 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003402 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003403 sb.append(" -- longest ");
3404 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
3405 pw.println(sb.toString());
3406 }
3407 if (deviceIdlingTime != 0) {
3408 sb.setLength(0);
3409 sb.append(prefix);
3410 sb.append(" Device full idling: ");
3411 formatTimeMs(sb, deviceIdlingTime / 1000);
3412 sb.append("(");
3413 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
3414 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_FULL, which));
3415 sb.append("x");
3416 pw.println(sb.toString());
3417 }
3418 if (deviceIdleModeFullTime != 0) {
3419 sb.setLength(0);
3420 sb.append(prefix);
3421 sb.append(" Idle mode full time: ");
3422 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
3423 sb.append("(");
3424 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
3425 sb.append(") ");
3426 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_FULL, which));
3427 sb.append("x");
3428 sb.append(" -- longest ");
3429 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_FULL));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003430 pw.println(sb.toString());
3431 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003432 if (phoneOnTime != 0) {
3433 sb.setLength(0);
3434 sb.append(prefix);
3435 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3436 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003437 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003438 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003439 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003440 if (connChanges != 0) {
3441 pw.print(prefix);
3442 pw.print(" Connectivity changes: "); pw.println(connChanges);
3443 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003444
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003445 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003446 long fullWakeLockTimeTotalMicros = 0;
3447 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003448
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003449 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003450
Evan Millar22ac0432009-03-31 11:33:18 -07003451 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003452 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003453
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003454 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3455 = u.getWakelockStats();
3456 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3457 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003458
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003459 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3460 if (fullWakeTimer != null) {
3461 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3462 rawRealtime, which);
3463 }
3464
3465 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3466 if (partialWakeTimer != null) {
3467 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3468 rawRealtime, which);
3469 if (totalTimeMicros > 0) {
3470 if (reqUid < 0) {
3471 // Only show the ordered list of all wake
3472 // locks if the caller is not asking for data
3473 // about a specific uid.
3474 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3475 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003476 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003477 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003478 }
3479 }
3480 }
3481 }
3482
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003483 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3484 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3485 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3486 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3487 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3488 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3489 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3490 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003491 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3492 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003493
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003494 if (fullWakeLockTimeTotalMicros != 0) {
3495 sb.setLength(0);
3496 sb.append(prefix);
3497 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
3498 (fullWakeLockTimeTotalMicros + 500) / 1000);
3499 pw.println(sb.toString());
3500 }
3501
3502 if (partialWakeLockTimeTotalMicros != 0) {
3503 sb.setLength(0);
3504 sb.append(prefix);
3505 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
3506 (partialWakeLockTimeTotalMicros + 500) / 1000);
3507 pw.println(sb.toString());
3508 }
3509
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003510 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003511 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
3512 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
3513 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
3514 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003515 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003516 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07003517 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003518 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08003519 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003520 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003521 if (time == 0) {
3522 continue;
3523 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003524 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003525 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003526 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08003527 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003528 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003529 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003530 sb.append("(");
3531 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003532 sb.append(") ");
3533 sb.append(getPhoneSignalStrengthCount(i, which));
3534 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003535 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003536 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003537 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07003538
3539 sb.setLength(0);
3540 sb.append(prefix);
3541 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003542 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003543 pw.println(sb.toString());
3544
Dianne Hackborn627bba72009-03-24 22:32:56 -07003545 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003546 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003547 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003548 didOne = false;
3549 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003550 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003551 if (time == 0) {
3552 continue;
3553 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003554 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003555 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003556 didOne = true;
3557 sb.append(DATA_CONNECTION_NAMES[i]);
3558 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003559 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003560 sb.append("(");
3561 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003562 sb.append(") ");
3563 sb.append(getPhoneDataConnectionCount(i, which));
3564 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003565 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003566 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003567 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003568
3569 sb.setLength(0);
3570 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003571 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003572 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003573 formatTimeMs(sb, mobileActiveTime / 1000);
3574 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
3575 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
3576 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003577 pw.println(sb.toString());
3578
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003579 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
3580 if (mobileActiveUnknownTime != 0) {
3581 sb.setLength(0);
3582 sb.append(prefix);
3583 sb.append(" Mobile radio active unknown time: ");
3584 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
3585 sb.append("(");
3586 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
3587 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
3588 sb.append("x");
3589 pw.println(sb.toString());
3590 }
3591
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003592 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
3593 if (mobileActiveAdjustedTime != 0) {
3594 sb.setLength(0);
3595 sb.append(prefix);
3596 sb.append(" Mobile radio active adjusted time: ");
3597 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
3598 sb.append("(");
3599 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
3600 sb.append(")");
3601 pw.println(sb.toString());
3602 }
3603
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003604 printControllerActivity(pw, sb, prefix, "Radio", getModemControllerActivity(), which);
3605
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003606 pw.print(prefix);
3607 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
3608 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
3609 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
3610 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003611 sb.setLength(0);
3612 sb.append(prefix);
3613 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
3614 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
3615 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
3616 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003617 sb.append(")");
3618 pw.println(sb.toString());
3619
3620 sb.setLength(0);
3621 sb.append(prefix);
3622 sb.append(" Wifi states:");
3623 didOne = false;
3624 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003625 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003626 if (time == 0) {
3627 continue;
3628 }
3629 sb.append("\n ");
3630 didOne = true;
3631 sb.append(WIFI_STATE_NAMES[i]);
3632 sb.append(" ");
3633 formatTimeMs(sb, time/1000);
3634 sb.append("(");
3635 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3636 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07003637 sb.append(getWifiStateCount(i, which));
3638 sb.append("x");
3639 }
3640 if (!didOne) sb.append(" (no activity)");
3641 pw.println(sb.toString());
3642
3643 sb.setLength(0);
3644 sb.append(prefix);
3645 sb.append(" Wifi supplicant states:");
3646 didOne = false;
3647 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3648 final long time = getWifiSupplStateTime(i, rawRealtime, which);
3649 if (time == 0) {
3650 continue;
3651 }
3652 sb.append("\n ");
3653 didOne = true;
3654 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
3655 sb.append(" ");
3656 formatTimeMs(sb, time/1000);
3657 sb.append("(");
3658 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3659 sb.append(") ");
3660 sb.append(getWifiSupplStateCount(i, which));
3661 sb.append("x");
3662 }
3663 if (!didOne) sb.append(" (no activity)");
3664 pw.println(sb.toString());
3665
3666 sb.setLength(0);
3667 sb.append(prefix);
3668 sb.append(" Wifi signal levels:");
3669 didOne = false;
3670 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3671 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
3672 if (time == 0) {
3673 continue;
3674 }
3675 sb.append("\n ");
3676 sb.append(prefix);
3677 didOne = true;
3678 sb.append("level(");
3679 sb.append(i);
3680 sb.append(") ");
3681 formatTimeMs(sb, time/1000);
3682 sb.append("(");
3683 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3684 sb.append(") ");
3685 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003686 sb.append("x");
3687 }
3688 if (!didOne) sb.append(" (no activity)");
3689 pw.println(sb.toString());
3690
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003691 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07003692
Adam Lesinski50e47602015-12-04 17:04:54 -08003693 pw.print(prefix);
3694 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
3695 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
3696
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003697 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
3698 sb.setLength(0);
3699 sb.append(prefix);
3700 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
3701 pw.println(sb.toString());
3702
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003703 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
3704 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003705
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003706 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07003707
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003708 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003709 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003710 pw.print(prefix); pw.println(" Device is currently unplugged");
3711 pw.print(prefix); pw.print(" Discharge cycle start level: ");
3712 pw.println(getDischargeStartLevel());
3713 pw.print(prefix); pw.print(" Discharge cycle current level: ");
3714 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07003715 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003716 pw.print(prefix); pw.println(" Device is currently plugged into power");
3717 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
3718 pw.println(getDischargeStartLevel());
3719 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
3720 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003721 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003722 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
3723 pw.println(getDischargeAmountScreenOn());
3724 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
3725 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07003726 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003727 } else {
3728 pw.print(prefix); pw.println(" Device battery use since last full charge");
3729 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
3730 pw.println(getLowDischargeAmountSinceCharge());
3731 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
3732 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003733 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
3734 pw.println(getDischargeAmountScreenOnSinceCharge());
3735 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
3736 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08003737 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07003738 }
Dianne Hackborn81038902012-11-26 17:04:09 -08003739
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003740 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003741 helper.create(this);
3742 helper.refreshStats(which, UserHandle.USER_ALL);
3743 List<BatterySipper> sippers = helper.getUsageList();
3744 if (sippers != null && sippers.size() > 0) {
3745 pw.print(prefix); pw.println(" Estimated power use (mAh):");
3746 pw.print(prefix); pw.print(" Capacity: ");
3747 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08003748 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07003749 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
3750 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
3751 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
3752 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003753 pw.println();
3754 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003755 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003756 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003757 switch (bs.drainType) {
3758 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003759 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003760 break;
3761 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003762 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003763 break;
3764 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003765 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003766 break;
3767 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003768 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003769 break;
3770 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003771 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003772 break;
3773 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003774 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003775 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003776 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003777 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003778 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003779 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003780 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003781 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003782 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003783 break;
3784 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003785 pw.print(" User "); pw.print(bs.userId);
3786 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003787 break;
3788 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003789 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003790 break;
3791 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003792 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003793 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003794 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003795 pw.print(" Camera: ");
3796 break;
3797 default:
3798 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003799 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003800 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003801 printmAh(pw, bs.totalPowerMah);
3802
Adam Lesinski57123002015-06-12 16:12:07 -07003803 if (bs.usagePowerMah != bs.totalPowerMah) {
3804 // If the usage (generic power) isn't the whole amount, we list out
3805 // what components are involved in the calculation.
3806
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003807 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07003808 if (bs.usagePowerMah != 0) {
3809 pw.print(" usage=");
3810 printmAh(pw, bs.usagePowerMah);
3811 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003812 if (bs.cpuPowerMah != 0) {
3813 pw.print(" cpu=");
3814 printmAh(pw, bs.cpuPowerMah);
3815 }
3816 if (bs.wakeLockPowerMah != 0) {
3817 pw.print(" wake=");
3818 printmAh(pw, bs.wakeLockPowerMah);
3819 }
3820 if (bs.mobileRadioPowerMah != 0) {
3821 pw.print(" radio=");
3822 printmAh(pw, bs.mobileRadioPowerMah);
3823 }
3824 if (bs.wifiPowerMah != 0) {
3825 pw.print(" wifi=");
3826 printmAh(pw, bs.wifiPowerMah);
3827 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003828 if (bs.bluetoothPowerMah != 0) {
3829 pw.print(" bt=");
3830 printmAh(pw, bs.bluetoothPowerMah);
3831 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07003832 if (bs.gpsPowerMah != 0) {
3833 pw.print(" gps=");
3834 printmAh(pw, bs.gpsPowerMah);
3835 }
3836 if (bs.sensorPowerMah != 0) {
3837 pw.print(" sensor=");
3838 printmAh(pw, bs.sensorPowerMah);
3839 }
3840 if (bs.cameraPowerMah != 0) {
3841 pw.print(" camera=");
3842 printmAh(pw, bs.cameraPowerMah);
3843 }
3844 if (bs.flashlightPowerMah != 0) {
3845 pw.print(" flash=");
3846 printmAh(pw, bs.flashlightPowerMah);
3847 }
3848 pw.print(" )");
3849 }
3850 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003851 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08003852 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003853 }
3854
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003855 sippers = helper.getMobilemsppList();
3856 if (sippers != null && sippers.size() > 0) {
3857 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003858 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003859 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003860 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003861 sb.setLength(0);
3862 sb.append(prefix); sb.append(" Uid ");
3863 UserHandle.formatUid(sb, bs.uidObj.getUid());
3864 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
3865 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
3866 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003867 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003868 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003869 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003870 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003871 sb.setLength(0);
3872 sb.append(prefix);
3873 sb.append(" TOTAL TIME: ");
3874 formatTimeMs(sb, totalTime);
3875 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
3876 sb.append(")");
3877 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003878 pw.println();
3879 }
3880
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003881 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
3882 @Override
3883 public int compare(TimerEntry lhs, TimerEntry rhs) {
3884 long lhsTime = lhs.mTime;
3885 long rhsTime = rhs.mTime;
3886 if (lhsTime < rhsTime) {
3887 return 1;
3888 }
3889 if (lhsTime > rhsTime) {
3890 return -1;
3891 }
3892 return 0;
3893 }
3894 };
3895
3896 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003897 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
3898 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003899 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003900 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
3901 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
3902 : kernelWakelocks.entrySet()) {
3903 final BatteryStats.Timer timer = ent.getValue();
3904 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003905 if (totalTimeMillis > 0) {
3906 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
3907 }
3908 }
3909 if (ktimers.size() > 0) {
3910 Collections.sort(ktimers, timerComparator);
3911 pw.print(prefix); pw.println(" All kernel wake locks:");
3912 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003913 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003914 String linePrefix = ": ";
3915 sb.setLength(0);
3916 sb.append(prefix);
3917 sb.append(" Kernel Wake lock ");
3918 sb.append(timer.mName);
3919 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
3920 which, linePrefix);
3921 if (!linePrefix.equals(": ")) {
3922 sb.append(" realtime");
3923 // Only print out wake locks that were held
3924 pw.println(sb.toString());
3925 }
3926 }
3927 pw.println();
3928 }
3929 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003930
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003931 if (timers.size() > 0) {
3932 Collections.sort(timers, timerComparator);
3933 pw.print(prefix); pw.println(" All partial wake locks:");
3934 for (int i=0; i<timers.size(); i++) {
3935 TimerEntry timer = timers.get(i);
3936 sb.setLength(0);
3937 sb.append(" Wake lock ");
3938 UserHandle.formatUid(sb, timer.mId);
3939 sb.append(" ");
3940 sb.append(timer.mName);
3941 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
3942 sb.append(" realtime");
3943 pw.println(sb.toString());
3944 }
3945 timers.clear();
3946 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08003947 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003948
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003949 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003950 if (wakeupReasons.size() > 0) {
3951 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003952 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003953 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003954 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003955 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
3956 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003957 }
3958 Collections.sort(reasons, timerComparator);
3959 for (int i=0; i<reasons.size(); i++) {
3960 TimerEntry timer = reasons.get(i);
3961 String linePrefix = ": ";
3962 sb.setLength(0);
3963 sb.append(prefix);
3964 sb.append(" Wakeup reason ");
3965 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003966 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
3967 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003968 pw.println(sb.toString());
3969 }
3970 pw.println();
3971 }
Dianne Hackborn81038902012-11-26 17:04:09 -08003972 }
Evan Millar22ac0432009-03-31 11:33:18 -07003973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003974 for (int iu=0; iu<NU; iu++) {
3975 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003976 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003977 continue;
3978 }
3979
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003980 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07003981
3982 pw.print(prefix);
3983 pw.print(" ");
3984 UserHandle.formatUid(pw, uid);
3985 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003986 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003987
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003988 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3989 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3990 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3991 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003992 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3993 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
3994
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003995 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3996 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003997 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3998 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003999
4000 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
4001 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
4002
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004003 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4004 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4005 final int wifiScanCount = u.getWifiScanCount(which);
4006 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004007
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004008 if (mobileRxBytes > 0 || mobileTxBytes > 0
4009 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004010 pw.print(prefix); pw.print(" Mobile network: ");
4011 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004012 pw.print(formatBytesLocked(mobileTxBytes));
4013 pw.print(" sent (packets "); pw.print(mobileRxPackets);
4014 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004015 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004016 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
4017 sb.setLength(0);
4018 sb.append(prefix); sb.append(" Mobile radio active: ");
4019 formatTimeMs(sb, uidMobileActiveTime / 1000);
4020 sb.append("(");
4021 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
4022 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
4023 long packets = mobileRxPackets + mobileTxPackets;
4024 if (packets == 0) {
4025 packets = 1;
4026 }
4027 sb.append(" @ ");
4028 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
4029 sb.append(" mspp");
4030 pw.println(sb.toString());
4031 }
4032
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004033 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
4034 u.getModemControllerActivity(), which);
4035
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004036 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004037 pw.print(prefix); pw.print(" Wi-Fi network: ");
4038 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004039 pw.print(formatBytesLocked(wifiTxBytes));
4040 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4041 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004042 }
4043
Dianne Hackborn62793e42015-03-09 11:15:41 -07004044 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004045 || uidWifiRunningTime != 0) {
4046 sb.setLength(0);
4047 sb.append(prefix); sb.append(" Wifi Running: ");
4048 formatTimeMs(sb, uidWifiRunningTime / 1000);
4049 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4050 whichBatteryRealtime)); sb.append(")\n");
4051 sb.append(prefix); sb.append(" Full Wifi Lock: ");
4052 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4053 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4054 whichBatteryRealtime)); sb.append(")\n");
4055 sb.append(prefix); sb.append(" Wifi Scan: ");
4056 formatTimeMs(sb, wifiScanTime / 1000);
4057 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004058 whichBatteryRealtime)); sb.append(") ");
4059 sb.append(wifiScanCount);
4060 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004061 pw.println(sb.toString());
4062 }
4063
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004064 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
4065 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07004066
Adam Lesinski50e47602015-12-04 17:04:54 -08004067 if (btRxBytes > 0 || btTxBytes > 0) {
4068 pw.print(prefix); pw.print(" Bluetooth network: ");
4069 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
4070 pw.print(formatBytesLocked(btTxBytes));
4071 pw.println(" sent");
4072 }
4073
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004074 uidActivity |= printTimer(pw, sb, u.getBluetoothScanTimer(), rawRealtime, which, prefix,
4075 "Bluetooth Scan");
4076
Dianne Hackborn617f8772009-03-31 15:04:46 -07004077 if (u.hasUserActivity()) {
4078 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07004079 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004080 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004081 if (val != 0) {
4082 if (!hasData) {
4083 sb.setLength(0);
4084 sb.append(" User activity: ");
4085 hasData = true;
4086 } else {
4087 sb.append(", ");
4088 }
4089 sb.append(val);
4090 sb.append(" ");
4091 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
4092 }
4093 }
4094 if (hasData) {
4095 pw.println(sb.toString());
4096 }
4097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004099 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4100 = u.getWakelockStats();
4101 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004102 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004103 int countWakelock = 0;
4104 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4105 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4106 String linePrefix = ": ";
4107 sb.setLength(0);
4108 sb.append(prefix);
4109 sb.append(" Wake lock ");
4110 sb.append(wakelocks.keyAt(iw));
4111 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
4112 "full", which, linePrefix);
4113 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
4114 "partial", which, linePrefix);
4115 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
4116 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004117 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
4118 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07004119 sb.append(" realtime");
4120 pw.println(sb.toString());
4121 uidActivity = true;
4122 countWakelock++;
4123
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004124 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
4125 rawRealtime, which);
4126 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
4127 rawRealtime, which);
4128 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
4129 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004130 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07004131 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004132 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004133 if (countWakelock > 1) {
4134 if (totalFullWakelock != 0 || totalPartialWakelock != 0
4135 || totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004136 sb.setLength(0);
4137 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004138 sb.append(" TOTAL wake: ");
4139 boolean needComma = false;
4140 if (totalFullWakelock != 0) {
4141 needComma = true;
4142 formatTimeMs(sb, totalFullWakelock);
4143 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004144 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004145 if (totalPartialWakelock != 0) {
4146 if (needComma) {
4147 sb.append(", ");
4148 }
4149 needComma = true;
4150 formatTimeMs(sb, totalPartialWakelock);
4151 sb.append("partial");
4152 }
4153 if (totalWindowWakelock != 0) {
4154 if (needComma) {
4155 sb.append(", ");
4156 }
4157 needComma = true;
4158 formatTimeMs(sb, totalWindowWakelock);
4159 sb.append("window");
4160 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004161 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07004162 if (needComma) {
4163 sb.append(",");
4164 }
4165 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004166 formatTimeMs(sb, totalDrawWakelock);
4167 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07004168 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004169 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004170 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004171 }
4172 }
4173
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004174 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4175 for (int isy=syncs.size()-1; isy>=0; isy--) {
4176 final Timer timer = syncs.valueAt(isy);
4177 // Convert from microseconds to milliseconds with rounding
4178 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4179 final int count = timer.getCountLocked(which);
4180 sb.setLength(0);
4181 sb.append(prefix);
4182 sb.append(" Sync ");
4183 sb.append(syncs.keyAt(isy));
4184 sb.append(": ");
4185 if (totalTime != 0) {
4186 formatTimeMs(sb, totalTime);
4187 sb.append("realtime (");
4188 sb.append(count);
4189 sb.append(" times)");
4190 } else {
4191 sb.append("(not used)");
4192 }
4193 pw.println(sb.toString());
4194 uidActivity = true;
4195 }
4196
4197 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4198 for (int ij=jobs.size()-1; ij>=0; ij--) {
4199 final Timer timer = jobs.valueAt(ij);
4200 // Convert from microseconds to milliseconds with rounding
4201 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4202 final int count = timer.getCountLocked(which);
4203 sb.setLength(0);
4204 sb.append(prefix);
4205 sb.append(" Job ");
4206 sb.append(jobs.keyAt(ij));
4207 sb.append(": ");
4208 if (totalTime != 0) {
4209 formatTimeMs(sb, totalTime);
4210 sb.append("realtime (");
4211 sb.append(count);
4212 sb.append(" times)");
4213 } else {
4214 sb.append("(not used)");
4215 }
4216 pw.println(sb.toString());
4217 uidActivity = true;
4218 }
4219
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004220 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
4221 prefix, "Flashlight");
4222 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
4223 prefix, "Camera");
4224 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
4225 prefix, "Video");
4226 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
4227 prefix, "Audio");
4228
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004229 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4230 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004231 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004232 final Uid.Sensor se = sensors.valueAt(ise);
4233 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004234 sb.setLength(0);
4235 sb.append(prefix);
4236 sb.append(" Sensor ");
4237 int handle = se.getHandle();
4238 if (handle == Uid.Sensor.GPS) {
4239 sb.append("GPS");
4240 } else {
4241 sb.append(handle);
4242 }
4243 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004244
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004245 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004246 if (timer != null) {
4247 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004248 final long totalTime = (timer.getTotalTimeLocked(
Dianne Hackborn61659e52014-07-09 16:13:01 -07004249 rawRealtime, which) + 500) / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004250 final int count = timer.getCountLocked(which);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004251 //timer.logState();
4252 if (totalTime != 0) {
4253 formatTimeMs(sb, totalTime);
4254 sb.append("realtime (");
4255 sb.append(count);
4256 sb.append(" times)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257 } else {
4258 sb.append("(not used)");
4259 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004260 } else {
4261 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004262 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004263
4264 pw.println(sb.toString());
4265 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004266 }
4267
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004268 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
4269 "Vibrator");
4270 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
4271 prefix, "Foreground activities");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004272
Dianne Hackborn61659e52014-07-09 16:13:01 -07004273 long totalStateTime = 0;
4274 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
4275 long time = u.getProcessStateTime(ips, rawRealtime, which);
4276 if (time > 0) {
4277 totalStateTime += time;
4278 sb.setLength(0);
4279 sb.append(prefix);
4280 sb.append(" ");
4281 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
4282 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08004283 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004284 pw.println(sb.toString());
4285 uidActivity = true;
4286 }
4287 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08004288 if (totalStateTime > 0) {
4289 sb.setLength(0);
4290 sb.append(prefix);
4291 sb.append(" Total running: ");
4292 formatTimeMs(sb, (totalStateTime + 500) / 1000);
4293 pw.println(sb.toString());
4294 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004295
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004296 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4297 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004298 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
4299 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004300 sb.setLength(0);
4301 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07004302 sb.append(" Total cpu time: u=");
4303 formatTimeMs(sb, userCpuTimeUs / 1000);
4304 sb.append("s=");
4305 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004306 sb.append("p=");
4307 printmAh(sb, powerCpuMaUs / (1000.0 * 1000.0 * 60.0 * 60.0));
4308 sb.append("mAh");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004309 pw.println(sb.toString());
4310 }
4311
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004312 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4313 = u.getProcessStats();
4314 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4315 final Uid.Proc ps = processStats.valueAt(ipr);
4316 long userTime;
4317 long systemTime;
4318 long foregroundTime;
4319 int starts;
4320 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004322 userTime = ps.getUserTime(which);
4323 systemTime = ps.getSystemTime(which);
4324 foregroundTime = ps.getForegroundTime(which);
4325 starts = ps.getStarts(which);
4326 final int numCrashes = ps.getNumCrashes(which);
4327 final int numAnrs = ps.getNumAnrs(which);
4328 numExcessive = which == STATS_SINCE_CHARGED
4329 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004330
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004331 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
4332 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
4333 sb.setLength(0);
4334 sb.append(prefix); sb.append(" Proc ");
4335 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
4336 sb.append(prefix); sb.append(" CPU: ");
4337 formatTimeMs(sb, userTime); sb.append("usr + ");
4338 formatTimeMs(sb, systemTime); sb.append("krn ; ");
4339 formatTimeMs(sb, foregroundTime); sb.append("fg");
4340 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
4341 sb.append("\n"); sb.append(prefix); sb.append(" ");
4342 boolean hasOne = false;
4343 if (starts != 0) {
4344 hasOne = true;
4345 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004346 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004347 if (numCrashes != 0) {
4348 if (hasOne) {
4349 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004350 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004351 hasOne = true;
4352 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004353 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004354 if (numAnrs != 0) {
4355 if (hasOne) {
4356 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004358 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004359 }
4360 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004361 pw.println(sb.toString());
4362 for (int e=0; e<numExcessive; e++) {
4363 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
4364 if (ew != null) {
4365 pw.print(prefix); pw.print(" * Killed for ");
4366 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
4367 pw.print("wake lock");
4368 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
4369 pw.print("cpu");
4370 } else {
4371 pw.print("unknown");
4372 }
4373 pw.print(" use: ");
4374 TimeUtils.formatDuration(ew.usedTime, pw);
4375 pw.print(" over ");
4376 TimeUtils.formatDuration(ew.overTime, pw);
4377 if (ew.overTime != 0) {
4378 pw.print(" (");
4379 pw.print((ew.usedTime*100)/ew.overTime);
4380 pw.println("%)");
4381 }
4382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004383 }
4384 uidActivity = true;
4385 }
4386 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004387
4388 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4389 = u.getPackageStats();
4390 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4391 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
4392 pw.println(":");
4393 boolean apkActivity = false;
4394 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4395 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4396 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
4397 pw.print(prefix); pw.print(" Wakeup alarm ");
4398 pw.print(alarms.keyAt(iwa)); pw.print(": ");
4399 pw.print(alarms.valueAt(iwa).getCountLocked(which));
4400 pw.println(" times");
4401 apkActivity = true;
4402 }
4403 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4404 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4405 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4406 final long startTime = ss.getStartTime(batteryUptime, which);
4407 final int starts = ss.getStarts(which);
4408 final int launches = ss.getLaunches(which);
4409 if (startTime != 0 || starts != 0 || launches != 0) {
4410 sb.setLength(0);
4411 sb.append(prefix); sb.append(" Service ");
4412 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
4413 sb.append(prefix); sb.append(" Created for: ");
4414 formatTimeMs(sb, startTime / 1000);
4415 sb.append("uptime\n");
4416 sb.append(prefix); sb.append(" Starts: ");
4417 sb.append(starts);
4418 sb.append(", launches: "); sb.append(launches);
4419 pw.println(sb.toString());
4420 apkActivity = true;
4421 }
4422 }
4423 if (!apkActivity) {
4424 pw.print(prefix); pw.println(" (nothing executed)");
4425 }
4426 uidActivity = true;
4427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004428 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004429 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004430 }
4431 }
4432 }
4433
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004434 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004435 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004436 int diff = oldval ^ newval;
4437 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004438 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004439 for (int i=0; i<descriptions.length; i++) {
4440 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004441 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004442 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004443 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004444 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004445 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004446 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
4447 didWake = true;
4448 pw.print("=");
4449 if (longNames) {
4450 UserHandle.formatUid(pw, wakelockTag.uid);
4451 pw.print(":\"");
4452 pw.print(wakelockTag.string);
4453 pw.print("\"");
4454 } else {
4455 pw.print(wakelockTag.poolIdx);
4456 }
4457 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004458 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004459 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004460 pw.print("=");
4461 int val = (newval&bd.mask)>>bd.shift;
4462 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004463 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004464 } else {
4465 pw.print(val);
4466 }
4467 }
4468 }
4469 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004470 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07004471 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004472 if (longNames) {
4473 UserHandle.formatUid(pw, wakelockTag.uid);
4474 pw.print(":\"");
4475 pw.print(wakelockTag.string);
4476 pw.print("\"");
4477 } else {
4478 pw.print(wakelockTag.poolIdx);
4479 }
4480 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004481 }
4482
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004483 public void prepareForDumpLocked() {
4484 }
4485
4486 public static class HistoryPrinter {
4487 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004488 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004489 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004490 int oldStatus = -1;
4491 int oldHealth = -1;
4492 int oldPlug = -1;
4493 int oldTemp = -1;
4494 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004495 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004496
Dianne Hackborn3251b902014-06-20 14:40:53 -07004497 void reset() {
4498 oldState = oldState2 = 0;
4499 oldLevel = -1;
4500 oldStatus = -1;
4501 oldHealth = -1;
4502 oldPlug = -1;
4503 oldTemp = -1;
4504 oldVolt = -1;
4505 }
4506
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004507 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004508 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004509 if (!checkin) {
4510 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004511 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004512 pw.print(" (");
4513 pw.print(rec.numReadInts);
4514 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004515 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004516 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4517 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004518 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004519 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004520 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004521 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004522 }
4523 lastTime = rec.time;
4524 }
4525 if (rec.cmd == HistoryItem.CMD_START) {
4526 if (checkin) {
4527 pw.print(":");
4528 }
4529 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004530 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004531 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
4532 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004533 if (checkin) {
4534 pw.print(":");
4535 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07004536 if (rec.cmd == HistoryItem.CMD_RESET) {
4537 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004538 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004539 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004540 pw.print("TIME:");
4541 if (checkin) {
4542 pw.println(rec.currentTime);
4543 } else {
4544 pw.print(" ");
4545 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
4546 rec.currentTime).toString());
4547 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08004548 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
4549 if (checkin) {
4550 pw.print(":");
4551 }
4552 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004553 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
4554 if (checkin) {
4555 pw.print(":");
4556 }
4557 pw.println("*OVERFLOW*");
4558 } else {
4559 if (!checkin) {
4560 if (rec.batteryLevel < 10) pw.print("00");
4561 else if (rec.batteryLevel < 100) pw.print("0");
4562 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004563 if (verbose) {
4564 pw.print(" ");
4565 if (rec.states < 0) ;
4566 else if (rec.states < 0x10) pw.print("0000000");
4567 else if (rec.states < 0x100) pw.print("000000");
4568 else if (rec.states < 0x1000) pw.print("00000");
4569 else if (rec.states < 0x10000) pw.print("0000");
4570 else if (rec.states < 0x100000) pw.print("000");
4571 else if (rec.states < 0x1000000) pw.print("00");
4572 else if (rec.states < 0x10000000) pw.print("0");
4573 pw.print(Integer.toHexString(rec.states));
4574 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004575 } else {
4576 if (oldLevel != rec.batteryLevel) {
4577 oldLevel = rec.batteryLevel;
4578 pw.print(",Bl="); pw.print(rec.batteryLevel);
4579 }
4580 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004581 if (oldStatus != rec.batteryStatus) {
4582 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004583 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004584 switch (oldStatus) {
4585 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004586 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004587 break;
4588 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004589 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004590 break;
4591 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004592 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004593 break;
4594 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004595 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004596 break;
4597 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004598 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004599 break;
4600 default:
4601 pw.print(oldStatus);
4602 break;
4603 }
4604 }
4605 if (oldHealth != rec.batteryHealth) {
4606 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004607 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004608 switch (oldHealth) {
4609 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004610 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004611 break;
4612 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004613 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004614 break;
4615 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004616 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004617 break;
4618 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004619 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004620 break;
4621 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004622 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004623 break;
4624 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004625 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004626 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004627 case BatteryManager.BATTERY_HEALTH_COLD:
4628 pw.print(checkin ? "c" : "cold");
4629 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004630 default:
4631 pw.print(oldHealth);
4632 break;
4633 }
4634 }
4635 if (oldPlug != rec.batteryPlugType) {
4636 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004637 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004638 switch (oldPlug) {
4639 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004640 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004641 break;
4642 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004643 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004644 break;
4645 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004646 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004647 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07004648 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004649 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07004650 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004651 default:
4652 pw.print(oldPlug);
4653 break;
4654 }
4655 }
4656 if (oldTemp != rec.batteryTemperature) {
4657 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004658 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004659 pw.print(oldTemp);
4660 }
4661 if (oldVolt != rec.batteryVoltage) {
4662 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004663 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004664 pw.print(oldVolt);
4665 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004666 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004667 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004668 printBitDescriptions(pw, oldState2, rec.states2, null,
4669 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004670 if (rec.wakeReasonTag != null) {
4671 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07004672 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004673 pw.print(rec.wakeReasonTag.poolIdx);
4674 } else {
4675 pw.print(" wake_reason=");
4676 pw.print(rec.wakeReasonTag.uid);
4677 pw.print(":\"");
4678 pw.print(rec.wakeReasonTag.string);
4679 pw.print("\"");
4680 }
4681 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004682 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004683 pw.print(checkin ? "," : " ");
4684 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
4685 pw.print("+");
4686 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
4687 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004688 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08004689 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
4690 : HISTORY_EVENT_NAMES;
4691 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
4692 | HistoryItem.EVENT_FLAG_FINISH);
4693 if (idx >= 0 && idx < eventNames.length) {
4694 pw.print(eventNames[idx]);
4695 } else {
4696 pw.print(checkin ? "Ev" : "event");
4697 pw.print(idx);
4698 }
4699 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004700 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004701 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08004702 } else {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004703 UserHandle.formatUid(pw, rec.eventTag.uid);
4704 pw.print(":\"");
4705 pw.print(rec.eventTag.string);
4706 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08004707 }
4708 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004709 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004710 if (rec.stepDetails != null) {
4711 if (!checkin) {
4712 pw.print(" Details: cpu=");
4713 pw.print(rec.stepDetails.userTime);
4714 pw.print("u+");
4715 pw.print(rec.stepDetails.systemTime);
4716 pw.print("s");
4717 if (rec.stepDetails.appCpuUid1 >= 0) {
4718 pw.print(" (");
4719 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
4720 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
4721 if (rec.stepDetails.appCpuUid2 >= 0) {
4722 pw.print(", ");
4723 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
4724 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
4725 }
4726 if (rec.stepDetails.appCpuUid3 >= 0) {
4727 pw.print(", ");
4728 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
4729 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
4730 }
4731 pw.print(')');
4732 }
4733 pw.println();
4734 pw.print(" /proc/stat=");
4735 pw.print(rec.stepDetails.statUserTime);
4736 pw.print(" usr, ");
4737 pw.print(rec.stepDetails.statSystemTime);
4738 pw.print(" sys, ");
4739 pw.print(rec.stepDetails.statIOWaitTime);
4740 pw.print(" io, ");
4741 pw.print(rec.stepDetails.statIrqTime);
4742 pw.print(" irq, ");
4743 pw.print(rec.stepDetails.statSoftIrqTime);
4744 pw.print(" sirq, ");
4745 pw.print(rec.stepDetails.statIdlTime);
4746 pw.print(" idle");
4747 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
4748 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
4749 + rec.stepDetails.statSoftIrqTime;
4750 int total = totalRun + rec.stepDetails.statIdlTime;
4751 if (total > 0) {
4752 pw.print(" (");
4753 float perc = ((float)totalRun) / ((float)total) * 100;
4754 pw.print(String.format("%.1f%%", perc));
4755 pw.print(" of ");
4756 StringBuilder sb = new StringBuilder(64);
4757 formatTimeMsNoSpace(sb, total*10);
4758 pw.print(sb);
4759 pw.print(")");
4760 }
4761 pw.println();
4762 } else {
4763 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4764 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
4765 pw.print(rec.stepDetails.userTime);
4766 pw.print(":");
4767 pw.print(rec.stepDetails.systemTime);
4768 if (rec.stepDetails.appCpuUid1 >= 0) {
4769 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
4770 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
4771 if (rec.stepDetails.appCpuUid2 >= 0) {
4772 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
4773 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
4774 }
4775 if (rec.stepDetails.appCpuUid3 >= 0) {
4776 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
4777 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
4778 }
4779 }
4780 pw.println();
4781 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4782 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
4783 pw.print(rec.stepDetails.statUserTime);
4784 pw.print(',');
4785 pw.print(rec.stepDetails.statSystemTime);
4786 pw.print(',');
4787 pw.print(rec.stepDetails.statIOWaitTime);
4788 pw.print(',');
4789 pw.print(rec.stepDetails.statIrqTime);
4790 pw.print(',');
4791 pw.print(rec.stepDetails.statSoftIrqTime);
4792 pw.print(',');
4793 pw.print(rec.stepDetails.statIdlTime);
4794 pw.println();
4795 }
4796 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004797 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004798 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004799 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004800 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004801
4802 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
4803 UserHandle.formatUid(pw, uid);
4804 pw.print("=");
4805 pw.print(utime);
4806 pw.print("u+");
4807 pw.print(stime);
4808 pw.print("s");
4809 }
4810
4811 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
4812 pw.print('/');
4813 pw.print(uid);
4814 pw.print(":");
4815 pw.print(utime);
4816 pw.print(":");
4817 pw.print(stime);
4818 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004819 }
4820
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004821 private void printSizeValue(PrintWriter pw, long size) {
4822 float result = size;
4823 String suffix = "";
4824 if (result >= 10*1024) {
4825 suffix = "KB";
4826 result = result / 1024;
4827 }
4828 if (result >= 10*1024) {
4829 suffix = "MB";
4830 result = result / 1024;
4831 }
4832 if (result >= 10*1024) {
4833 suffix = "GB";
4834 result = result / 1024;
4835 }
4836 if (result >= 10*1024) {
4837 suffix = "TB";
4838 result = result / 1024;
4839 }
4840 if (result >= 10*1024) {
4841 suffix = "PB";
4842 result = result / 1024;
4843 }
4844 pw.print((int)result);
4845 pw.print(suffix);
4846 }
4847
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004848 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
4849 String label3, long estimatedTime) {
4850 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004851 return false;
4852 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004853 pw.print(label1);
4854 pw.print(label2);
4855 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004856 StringBuilder sb = new StringBuilder(64);
4857 formatTimeMs(sb, estimatedTime);
4858 pw.print(sb);
4859 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08004860 return true;
4861 }
4862
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004863 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
4864 LevelStepTracker steps, boolean checkin) {
4865 if (steps == null) {
4866 return false;
4867 }
4868 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004869 if (count <= 0) {
4870 return false;
4871 }
4872 if (!checkin) {
4873 pw.println(header);
4874 }
Kweku Adams030980a2015-04-01 16:07:48 -07004875 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004876 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004877 long duration = steps.getDurationAt(i);
4878 int level = steps.getLevelAt(i);
4879 long initMode = steps.getInitModeAt(i);
4880 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004881 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004882 lineArgs[0] = Long.toString(duration);
4883 lineArgs[1] = Integer.toString(level);
4884 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
4885 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
4886 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
4887 case Display.STATE_ON: lineArgs[2] = "s+"; break;
4888 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
4889 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07004890 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004891 }
4892 } else {
4893 lineArgs[2] = "";
4894 }
4895 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
4896 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
4897 } else {
4898 lineArgs[3] = "";
4899 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004900 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07004901 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004902 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07004903 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004904 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004905 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
4906 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004907 pw.print(prefix);
4908 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004909 TimeUtils.formatDuration(duration, pw);
4910 pw.print(" to "); pw.print(level);
4911 boolean haveModes = false;
4912 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
4913 pw.print(" (");
4914 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
4915 case Display.STATE_OFF: pw.print("screen-off"); break;
4916 case Display.STATE_ON: pw.print("screen-on"); break;
4917 case Display.STATE_DOZE: pw.print("screen-doze"); break;
4918 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07004919 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004920 }
4921 haveModes = true;
4922 }
4923 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
4924 pw.print(haveModes ? ", " : " (");
4925 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
4926 ? "power-save-on" : "power-save-off");
4927 haveModes = true;
4928 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004929 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
4930 pw.print(haveModes ? ", " : " (");
4931 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
4932 ? "device-idle-on" : "device-idle-off");
4933 haveModes = true;
4934 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004935 if (haveModes) {
4936 pw.print(")");
4937 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07004938 pw.println();
4939 }
4940 }
4941 return true;
4942 }
4943
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004944 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004945 public static final int DUMP_DAILY_ONLY = 1<<2;
4946 public static final int DUMP_HISTORY_ONLY = 1<<3;
4947 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
4948 public static final int DUMP_VERBOSE = 1<<5;
4949 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004950
Dianne Hackborn37de0982014-05-09 09:32:18 -07004951 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
4952 final HistoryPrinter hprinter = new HistoryPrinter();
4953 final HistoryItem rec = new HistoryItem();
4954 long lastTime = -1;
4955 long baseTime = -1;
4956 boolean printed = false;
4957 HistoryEventTracker tracker = null;
4958 while (getNextHistoryLocked(rec)) {
4959 lastTime = rec.time;
4960 if (baseTime < 0) {
4961 baseTime = lastTime;
4962 }
4963 if (rec.time >= histStart) {
4964 if (histStart >= 0 && !printed) {
4965 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07004966 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08004967 || rec.cmd == HistoryItem.CMD_START
4968 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07004969 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004970 hprinter.printNextItem(pw, rec, baseTime, checkin,
4971 (flags&DUMP_VERBOSE) != 0);
4972 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004973 } else if (rec.currentTime != 0) {
4974 printed = true;
4975 byte cmd = rec.cmd;
4976 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004977 hprinter.printNextItem(pw, rec, baseTime, checkin,
4978 (flags&DUMP_VERBOSE) != 0);
4979 rec.cmd = cmd;
4980 }
4981 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004982 if (rec.cmd != HistoryItem.CMD_UPDATE) {
4983 hprinter.printNextItem(pw, rec, baseTime, checkin,
4984 (flags&DUMP_VERBOSE) != 0);
4985 rec.cmd = HistoryItem.CMD_UPDATE;
4986 }
4987 int oldEventCode = rec.eventCode;
4988 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07004989 rec.eventTag = new HistoryTag();
4990 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
4991 HashMap<String, SparseIntArray> active
4992 = tracker.getStateForEvent(i);
4993 if (active == null) {
4994 continue;
4995 }
4996 for (HashMap.Entry<String, SparseIntArray> ent
4997 : active.entrySet()) {
4998 SparseIntArray uids = ent.getValue();
4999 for (int j=0; j<uids.size(); j++) {
5000 rec.eventCode = i;
5001 rec.eventTag.string = ent.getKey();
5002 rec.eventTag.uid = uids.keyAt(j);
5003 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07005004 hprinter.printNextItem(pw, rec, baseTime, checkin,
5005 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005006 rec.wakeReasonTag = null;
5007 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005008 }
5009 }
5010 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005011 rec.eventCode = oldEventCode;
5012 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005013 tracker = null;
5014 }
5015 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005016 hprinter.printNextItem(pw, rec, baseTime, checkin,
5017 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07005018 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
5019 // This is an attempt to aggregate the previous state and generate
5020 // fake events to reflect that state at the point where we start
5021 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07005022 if (tracker == null) {
5023 tracker = new HistoryEventTracker();
5024 }
5025 tracker.updateState(rec.eventCode, rec.eventTag.string,
5026 rec.eventTag.uid, rec.eventTag.poolIdx);
5027 }
5028 }
5029 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07005030 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005031 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
5032 }
5033 }
5034
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005035 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
5036 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
5037 if (steps == null) {
5038 return;
5039 }
5040 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
5041 if (timeRemaining >= 0) {
5042 pw.print(prefix); pw.print(label); pw.print(" total time: ");
5043 tmpSb.setLength(0);
5044 formatTimeMs(tmpSb, timeRemaining);
5045 pw.print(tmpSb);
5046 pw.print(" (from "); pw.print(tmpOutInt[0]);
5047 pw.println(" steps)");
5048 }
5049 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5050 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5051 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
5052 if (estimatedTime > 0) {
5053 pw.print(prefix); pw.print(label); pw.print(" ");
5054 pw.print(STEP_LEVEL_MODE_LABELS[i]);
5055 pw.print(" time: ");
5056 tmpSb.setLength(0);
5057 formatTimeMs(tmpSb, estimatedTime);
5058 pw.print(tmpSb);
5059 pw.print(" (from "); pw.print(tmpOutInt[0]);
5060 pw.println(" steps)");
5061 }
5062 }
5063 }
5064
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005065 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
5066 ArrayList<PackageChange> changes) {
5067 if (changes == null) {
5068 return;
5069 }
5070 pw.print(prefix); pw.println("Package changes:");
5071 for (int i=0; i<changes.size(); i++) {
5072 PackageChange pc = changes.get(i);
5073 if (pc.mUpdate) {
5074 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
5075 pw.print(" vers="); pw.println(pc.mVersionCode);
5076 } else {
5077 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
5078 }
5079 }
5080 }
5081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005082 /**
5083 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
5084 *
5085 * @param pw a Printer to receive the dump output.
5086 */
5087 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005088 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005089 prepareForDumpLocked();
5090
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005091 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005092 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005093
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005094 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005095 final long historyTotalSize = getHistoryTotalSize();
5096 final long historyUsedSize = getHistoryUsedSize();
5097 if (startIteratingHistoryLocked()) {
5098 try {
5099 pw.print("Battery History (");
5100 pw.print((100*historyUsedSize)/historyTotalSize);
5101 pw.print("% used, ");
5102 printSizeValue(pw, historyUsedSize);
5103 pw.print(" used of ");
5104 printSizeValue(pw, historyTotalSize);
5105 pw.print(", ");
5106 pw.print(getHistoryStringPoolSize());
5107 pw.print(" strings using ");
5108 printSizeValue(pw, getHistoryStringPoolBytes());
5109 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07005110 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005111 pw.println();
5112 } finally {
5113 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005114 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005115 }
5116
5117 if (startIteratingOldHistoryLocked()) {
5118 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005119 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005120 pw.println("Old battery History:");
5121 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005122 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005123 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005124 if (baseTime < 0) {
5125 baseTime = rec.time;
5126 }
5127 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005128 }
5129 pw.println();
5130 } finally {
5131 finishIteratingOldHistoryLocked();
5132 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005133 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005134 }
5135
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005136 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005137 return;
5138 }
5139
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005140 if (!filtering) {
5141 SparseArray<? extends Uid> uidStats = getUidStats();
5142 final int NU = uidStats.size();
5143 boolean didPid = false;
5144 long nowRealtime = SystemClock.elapsedRealtime();
5145 for (int i=0; i<NU; i++) {
5146 Uid uid = uidStats.valueAt(i);
5147 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
5148 if (pids != null) {
5149 for (int j=0; j<pids.size(); j++) {
5150 Uid.Pid pid = pids.valueAt(j);
5151 if (!didPid) {
5152 pw.println("Per-PID Stats:");
5153 didPid = true;
5154 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005155 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
5156 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005157 pw.print(" PID "); pw.print(pids.keyAt(j));
5158 pw.print(" wake time: ");
5159 TimeUtils.formatDuration(time, pw);
5160 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005161 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005162 }
5163 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005164 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005165 pw.println();
5166 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005167 }
5168
5169 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005170 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
5171 getDischargeLevelStepTracker(), false)) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005172 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
5173 if (timeRemaining >= 0) {
5174 pw.print(" Estimated discharge time remaining: ");
5175 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5176 pw.println();
5177 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005178 final LevelStepTracker steps = getDischargeLevelStepTracker();
5179 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5180 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
5181 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5182 STEP_LEVEL_MODE_VALUES[i], null));
5183 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005184 pw.println();
5185 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005186 if (dumpDurationSteps(pw, " ", "Charge step durations:",
5187 getChargeLevelStepTracker(), false)) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005188 long timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
5189 if (timeRemaining >= 0) {
5190 pw.print(" Estimated charge time remaining: ");
5191 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5192 pw.println();
5193 }
5194 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005195 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005196 }
5197 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
5198 pw.println("Daily stats:");
5199 pw.print(" Current start time: ");
5200 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5201 getCurrentDailyStartTime()).toString());
5202 pw.print(" Next min deadline: ");
5203 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5204 getNextMinDailyDeadline()).toString());
5205 pw.print(" Next max deadline: ");
5206 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5207 getNextMaxDailyDeadline()).toString());
5208 StringBuilder sb = new StringBuilder(64);
5209 int[] outInt = new int[1];
5210 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
5211 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005212 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
5213 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005214 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005215 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
5216 dsteps, false)) {
5217 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5218 sb, outInt);
5219 }
5220 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
5221 csteps, false)) {
5222 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5223 sb, outInt);
5224 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005225 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005226 } else {
5227 pw.println(" Current daily steps:");
5228 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5229 sb, outInt);
5230 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5231 sb, outInt);
5232 }
5233 }
5234 DailyItem dit;
5235 int curIndex = 0;
5236 while ((dit=getDailyItemLocked(curIndex)) != null) {
5237 curIndex++;
5238 if ((flags&DUMP_DAILY_ONLY) != 0) {
5239 pw.println();
5240 }
5241 pw.print(" Daily from ");
5242 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
5243 pw.print(" to ");
5244 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
5245 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005246 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005247 if (dumpDurationSteps(pw, " ",
5248 " Discharge step durations:", dit.mDischargeSteps, false)) {
5249 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5250 sb, outInt);
5251 }
5252 if (dumpDurationSteps(pw, " ",
5253 " Charge step durations:", dit.mChargeSteps, false)) {
5254 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5255 sb, outInt);
5256 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005257 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005258 } else {
5259 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5260 sb, outInt);
5261 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5262 sb, outInt);
5263 }
5264 }
5265 pw.println();
5266 }
5267 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005268 pw.println("Statistics since last charge:");
5269 pw.println(" System starts: " + getStartCount()
5270 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07005271 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
5272 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005273 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005274 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005275 }
5276
5277 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005278 public void dumpCheckinLocked(Context context, PrintWriter pw,
5279 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005280 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005281
5282 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005283 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
5284 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005285
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005286 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
5287
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005288 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005289 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005290
5291 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07005292 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005293 try {
5294 for (int i=0; i<getHistoryStringPoolSize(); i++) {
5295 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5296 pw.print(HISTORY_STRING_POOL); pw.print(',');
5297 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005298 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005299 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005300 pw.print(",\"");
5301 String str = getHistoryTagPoolString(i);
5302 str = str.replace("\\", "\\\\");
5303 str = str.replace("\"", "\\\"");
5304 pw.print(str);
5305 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005306 pw.println();
5307 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005308 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005309 } finally {
5310 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08005311 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005312 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005313 }
5314
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005315 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005316 return;
5317 }
5318
Dianne Hackborne4a59512010-12-07 11:08:07 -08005319 if (apps != null) {
5320 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
5321 for (int i=0; i<apps.size(); i++) {
5322 ApplicationInfo ai = apps.get(i);
5323 ArrayList<String> pkgs = uids.get(ai.uid);
5324 if (pkgs == null) {
5325 pkgs = new ArrayList<String>();
5326 uids.put(ai.uid, pkgs);
5327 }
5328 pkgs.add(ai.packageName);
5329 }
5330 SparseArray<? extends Uid> uidStats = getUidStats();
5331 final int NU = uidStats.size();
5332 String[] lineArgs = new String[2];
5333 for (int i=0; i<NU; i++) {
5334 int uid = uidStats.keyAt(i);
5335 ArrayList<String> pkgs = uids.get(uid);
5336 if (pkgs != null) {
5337 for (int j=0; j<pkgs.size(); j++) {
5338 lineArgs[0] = Integer.toString(uid);
5339 lineArgs[1] = pkgs.get(j);
5340 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
5341 (Object[])lineArgs);
5342 }
5343 }
5344 }
5345 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005346 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005347 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005348 String[] lineArgs = new String[1];
5349 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
5350 if (timeRemaining >= 0) {
5351 lineArgs[0] = Long.toString(timeRemaining);
5352 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
5353 (Object[])lineArgs);
5354 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005355 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005356 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
5357 if (timeRemaining >= 0) {
5358 lineArgs[0] = Long.toString(timeRemaining);
5359 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
5360 (Object[])lineArgs);
5361 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005362 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
5363 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005366}